📜  计算Van Eck序列的前N个词中第N个词的出现

📅  最后修改于: 2021-05-06 09:15:14             🧑  作者: Mango

先决条件:范·埃克的序列

给定正整数N ,任务是计算Van Eck序列的前N项中第N项的出现。

例子:

  1. 天真的方法:
    • 生成Van Eck的序列直到N项
    • 遍历生成的序列并计数第N项的出现。

    为了处理多个查询,我们可以预先计算Van Eck的序列。

    下面是上述方法的实现:

    C++
    // C++ program to count the occurrence
    // of nth term in first n terms
    // of Van Eck's sequence
      
    #include 
    using namespace std;
      
    #define MAX 100000
    int sequence[MAX + 1];
      
    // Utility function to compute
    // Van Eck's sequence
    void vanEckSequence()
    {
      
        // Initialize sequence array
        for (int i = 0; i < MAX; i++) {
            sequence[i] = 0;
        }
      
        // Loop to generate sequence
        for (int i = 0; i < MAX; i++) {
      
            // Check if sequence[i] has occured
            // previously or is new to sequence
            for (int j = i - 1; j >= 0; j--) {
                if (sequence[j] == sequence[i]) {
      
                    // If occurrence found
                    // then the next term will be
                    // how far back this last term
                    // occured previously
                    sequence[i + 1] = i - j;
                    break;
                }
            }
        }
    }
      
    // Utility function to count
    // the occurrence of nth term
    // in first n terms of the sequence
    int getCount(int n)
    {
      
        // Get nth term of the sequence
        int nthTerm = sequence[n - 1];
      
        int count = 0;
      
        // Count the occurrence of nth term
        // in first n terms of the sequence
        for (int i = 0; i < n; i++) {
      
            if (sequence[i] == nthTerm)
                count++;
        }
      
        // Return count
        return count;
    }
      
    // Driver code
    int main()
    {
      
        // Pre-compute Van Eck's sequence
        vanEckSequence();
      
        int n = 5;
      
        // Print count of the occurrence of nth term
        // in first n terms of the sequence
        cout << getCount(n) << endl;
      
        n = 11;
      
        // Print count of the occurrence of nth term
        // in first n terms of the sequence
        cout << getCount(n) << endl;
      
        return 0;
    }


    Java
    // Java program to count the occurrence
    // of nth term in first n terms
    // of Van Eck's sequence
      
    class GFG {
      
        static int MAX = 100000;
        static int sequence[] = new int[MAX + 1];
      
        // Utility function to compute
        // Van Eck's sequence
        static void vanEckSequence()
        {
      
            // Initialize sequence array
            for (int i = 0; i < MAX; i++) {
                sequence[i] = 0;
            }
      
            // Loop to generate sequence
            for (int i = 0; i < MAX; i++) {
      
                // Check if sequence[i] has occured
                // previously or is new to sequence
                for (int j = i - 1; j >= 0; j--) {
                    if (sequence[j] == sequence[i]) {
      
                        // If occurrence found
                        // then the next term will be
                        // how far back this last term
                        // occured previously
                        sequence[i + 1] = i - j;
                        break;
                    }
                }
            }
        }
      
        // Utility function to count
        // the occurrence of nth term
        // in first n terms of the sequence
        static int getCount(int n)
        {
      
            // Get nth term of the sequence
            int nthTerm = sequence[n - 1];
      
            int count = 0;
      
            // Count the occurrence of nth term
            // in first n terms of the sequence
            for (int i = 0; i < n; i++) {
      
                if (sequence[i] == nthTerm)
                    count++;
            }
      
            // Return count
            return count;
        }
      
        // Driver code
        public static void main(String[] args)
        {
      
            // Pre-compute Van Eck's sequence
            vanEckSequence();
      
            int n = 5;
      
            // Print count of the occurrence of nth term
            // in first n terms of the sequence
            System.out.println(getCount(n));
      
            n = 11;
      
            // Print count of the occurrence of nth term
            // in first n terms of the sequence
            System.out.println(getCount(n));
        }
    }


    Python3
    # Python3 program to count the occurrence 
    # of nth term in first n terms 
    # of Van Eck's sequence 
      
    MAX = 10000
    sequence = [0]*(MAX + 1); 
      
    # Utility function to compute 
    # Van Eck's sequence 
    def vanEckSequence() :
          
        # Loop to generate sequence 
        for i in range(MAX) :
      
            # Check if sequence[i] has occured 
            # previously or is new to sequence 
            for j in range(i - 1, -1, -1) :
                if (sequence[j] == sequence[i]) :
      
                    # If occurrence found 
                    # then the next term will be 
                    # how far back this last term 
                    # occured previously 
                    sequence[i + 1] = i - j; 
                    break; 
      
    # Utility function to count 
    # the occurrence of nth term 
    # in first n terms of the sequence 
    def getCount(n) : 
      
        # Get nth term of the sequence 
        nthTerm = sequence[n - 1]; 
      
        count = 0; 
      
        # Count the occurrence of nth term 
        # in first n terms of the sequence 
        for i in range(n) :
      
            if (sequence[i] == nthTerm) :
                count += 1; 
      
        # Return count 
        return count; 
      
    # Driver code 
    if __name__ == "__main__" : 
      
        # Pre-compute Van Eck's sequence 
        vanEckSequence(); 
      
        n = 5; 
      
        # Print count of the occurrence of nth term 
        # in first n terms of the sequence 
        print(getCount(n)); 
      
        n = 11; 
      
        # Print count of the occurrence of nth term 
        # in first n terms of the sequence 
        print(getCount(n)); 
      
    # This code is contributed by AnkitRai01


    C#
    // C# program to count the occurrence
    // of nth term in first n terms
    // of Van Eck's sequence
      
    using System;
    class GFG {
      
        static int MAX = 100000;
        static int[] sequence = new int[MAX + 1];
      
        // Utility function to compute
        // Van Eck's sequence
        static void vanEckSequence()
        {
      
            // Initialize sequence array
            for (int i = 0; i < MAX; i++) {
                sequence[i] = 0;
            }
      
            // Loop to generate sequence
            for (int i = 0; i < MAX; i++) {
      
                // Check if sequence[i] has occured
                // previously or is new to sequence
                for (int j = i - 1; j >= 0; j--) {
                    if (sequence[j] == sequence[i]) {
      
                        // If occurrence found
                        // then the next term will be
                        // how far back this last term
                        // occured previously
                        sequence[i + 1] = i - j;
                        break;
                    }
                }
            }
        }
      
        // Utility function to count
        // the occurrence of nth term
        // in first n terms of the sequence
        static int getCount(int n)
        {
      
            // Get nth term of the sequence
            int nthTerm = sequence[n - 1];
      
            int count = 0;
      
            // Count the occurrence of nth term
            // in first n terms of the sequence
            for (int i = 0; i < n; i++) {
      
                if (sequence[i] == nthTerm)
                    count++;
            }
      
            // Return count
            return count;
        }
      
        // Driver code
        public static void Main()
        {
      
            // Pre-compute Van Eck's sequence
            vanEckSequence();
      
            int n = 5;
      
            // Print count of the occurrence of nth term
            // in first n terms of the sequence
            Console.WriteLine(getCount(n));
      
            n = 11;
      
            // Print count of the occurrence of nth term
            // in first n terms of the sequence
            Console.WriteLine(getCount(n));
        }
    }


    CPP
    // C++ program to count the occurrence
    // of nth term in first n terms
    // of Van Eck's sequence
      
    #include 
    using namespace std;
      
    #define MAX 100000
    int sequence[MAX + 1];
      
    // Utility function to compute
    // Van Eck's sequence
    void vanEckSequence()
    {
      
        // Initialize sequence array
        for (int i = 0; i < MAX; i++) {
            sequence[i] = 0;
        }
      
        // Loop to generate sequence
        for (int i = 0; i < MAX; i++) {
      
            // Check if sequence[i] has occured
            // previously or is new to sequence
            for (int j = i - 1; j >= 0; j--) {
                if (sequence[j] == sequence[i]) {
      
                    // If occurrence found
                    // then the next term will be
                    // how far back this last term
                    // occured previously
                    sequence[i + 1] = i - j;
                    break;
                }
            }
        }
    }
      
    // Utility function to count
    // the occurrence of nth term
    // in first n terms of the sequence
    int getCount(int n)
    {
      
        // Initialize count as 1
        int count = 1;
      
        int i = n - 1;
      
        while (sequence[i + 1] != 0) {
      
            // Increment count if (i+1)th term
            // is non-zero
            count++;
      
            // Previous occurrence of sequence[i]
            // will be it (i - sequence[i+1])th position
            i = i - sequence[i + 1];
        }
      
        // Return the count of occurrence
        return count;
    }
      
    // Driver code
    int main()
    {
      
        // Pre-compute Van Eck's sequence
        vanEckSequence();
      
        int n = 5;
      
        // Print count of the occurrence of nth term
        // in first n terms of the sequence
        cout << getCount(n) << endl;
      
        n = 11;
      
        // Print count of the occurrence of nth term
        // in first n terms of the sequence
        cout << getCount(n) << endl;
      
        return 0;
    }


    Java
    // Java program to count the occurrence
    // of nth term in first n terms
    // of Van Eck's sequence
      
    class GFG {
      
        static int MAX = 100000;
        static int sequence[] = new int[MAX + 1];
      
        // Utility function to compute
        // Van Eck's sequence
        static void vanEckSequence()
        {
      
            // Initialize sequence array
            for (int i = 0; i < MAX; i++) {
                sequence[i] = 0;
            }
      
            // Loop to generate sequence
            for (int i = 0; i < MAX; i++) {
      
                // Check if sequence[i] has occured
                // previously or is new to sequence
                for (int j = i - 1; j >= 0; j--) {
                    if (sequence[j] == sequence[i]) {
      
                        // If occurrence found
                        // then the next term will be
                        // how far back this last term
                        // occured previously
                        sequence[i + 1] = i - j;
                        break;
                    }
                }
            }
        }
      
        // Utility function to count
        // the occurrence of nth term
        // in first n terms of the sequence
        static int getCount(int n)
        {
      
            // Initialize count as 1
            int count = 1;
      
            int i = n - 1;
            while (sequence[i + 1] != 0) {
      
                // Increment count if (i+1)th term
                // is non-zero
                count++;
      
                // Previous occurrence of sequence[i]
                // will be it (i - sequence[i+1])th position
                i = i - sequence[i + 1];
            }
      
            // Return the count of occurrence
            return count;
        }
      
        // Driver code
        public static void main(String[] args)
        {
      
            // Pre-compute Van Eck's sequence
            vanEckSequence();
      
            int n = 5;
      
            // Print count of the occurrence of nth term
            // in first n terms of the sequence
            System.out.println(getCount(n));
      
            n = 11;
      
            // Print count of the occurrence of nth term
            // in first n terms of the sequence
            System.out.println(getCount(n));
        }
    }


    Python3
    # Python3 program to count the occurrence 
    # of nth term in first n terms 
    # of Van Eck's sequence 
    MAX = 10000
    sequence = [0] * (MAX + 1); 
      
    # Utility function to compute 
    # Van Eck's sequence 
    def vanEckSequence() :
      
        # Loop to generate sequence 
        for i in range(MAX) : 
      
            # Check if sequence[i] has occured 
            # previously or is new to sequence 
            for j in range( i - 1, -1, -1) : 
                if (sequence[j] == sequence[i]) :
                      
                    # If occurrence found 
                    # then the next term will be 
                    # how far back this last term 
                    # occured previously 
                    sequence[i + 1] = i - j; 
                    break; 
      
    # Utility function to count 
    # the occurrence of nth term 
    # in first n terms of the sequence 
    def getCount(n) : 
      
        # Initialize count as 1 
        count = 1; 
      
        i = n - 1; 
      
        while (sequence[i + 1] != 0) :
      
            # Increment count if (i+1)th term 
            # is non-zero 
            count += 1; 
      
            # Previous occurrence of sequence[i] 
            # will be it (i - sequence[i+1])th position 
            i = i - sequence[i + 1]; 
      
        # Return the count of occurrence 
        return count; 
      
    # Driver code 
    if __name__ == "__main__" : 
      
        # Pre-compute Van Eck's sequence 
        vanEckSequence(); 
      
        n = 5; 
      
        # Print count of the occurrence of nth term 
        # in first n terms of the sequence 
        print(getCount(n)); 
      
        n = 11; 
      
        # Print count of the occurrence of nth term 
        # in first n terms of the sequence 
        print(getCount(n)) ; 
      
    # This code is contributed by AnkitRai01


    C#
    // C# program to count the occurrence
    // of nth term in first n terms
    // of Van Eck's sequence
      
    using System;
    class GFG {
      
        static int MAX = 100000;
        static int[] sequence = new int[MAX + 1];
      
        // Utility function to compute
        // Van Eck's sequence
        static void vanEckSequence()
        {
      
            // Initialize sequence array
            for (int i = 0; i < MAX; i++) {
                sequence[i] = 0;
            }
      
            // Loop to generate sequence
            for (int i = 0; i < MAX; i++) {
      
                // Check if sequence[i] has occured
                // previously or is new to sequence
                for (int j = i - 1; j >= 0; j--) {
                    if (sequence[j] == sequence[i]) {
      
                        // If occurrence found
                        // then the next term will be
                        // how far back this last term
                        // occured previously
                        sequence[i + 1] = i - j;
                        break;
                    }
                }
            }
        }
      
        // Utility function to count
        // the occurrence of nth term
        // in first n terms of the sequence
        static int getCount(int n)
        {
      
            // Initialize count as 1
            int count = 1;
            int i = n - 1;
      
            while (sequence[i + 1] != 0) {
      
                // Increment count if (i+1)th term
                // is non-zero
                count++;
      
                // Previous occurrence of sequence[i]
                // will be it (i - sequence[i+1])th position
                i = i - sequence[i + 1];
            }
      
            // Return the count of occurrence
            return count;
        }
      
        // Driver code
        public static void Main(string[] args)
        {
      
            // Pre-compute Van Eck's sequence
            vanEckSequence();
      
            int n = 5;
      
            // Print count of the occurrence of nth term
            // in first n terms of the sequence
            Console.WriteLine(getCount(n));
      
            n = 11;
      
            // Print count of the occurrence of nth term
            // in first n terms of the sequence
            Console.WriteLine(getCount(n));
        }
    }


    输出:
    1
    5
    
  2. 高效方法:
    • 对于Van Eck序列中的给定术语,其下一个术语表示该给定术语的最后一次出现之间的距离。
    • 因此,对于i术语,其先前的发生将在I -值(i + 1)术语
      例如:
    • 另外,如果序列中的下一项为0,则表示该项之前未发生过。
      例如:
    • 算法:
      • 让我们将序列的N项视为S N
      • 如果S N + 1不为零,则递增计数
        并对第(N- S N + 1 )词做同样的事情
      • 如果S N + 1为零,则停止。

    下面是上述方法的实现:

    CPP

    // C++ program to count the occurrence
    // of nth term in first n terms
    // of Van Eck's sequence
      
    #include 
    using namespace std;
      
    #define MAX 100000
    int sequence[MAX + 1];
      
    // Utility function to compute
    // Van Eck's sequence
    void vanEckSequence()
    {
      
        // Initialize sequence array
        for (int i = 0; i < MAX; i++) {
            sequence[i] = 0;
        }
      
        // Loop to generate sequence
        for (int i = 0; i < MAX; i++) {
      
            // Check if sequence[i] has occured
            // previously or is new to sequence
            for (int j = i - 1; j >= 0; j--) {
                if (sequence[j] == sequence[i]) {
      
                    // If occurrence found
                    // then the next term will be
                    // how far back this last term
                    // occured previously
                    sequence[i + 1] = i - j;
                    break;
                }
            }
        }
    }
      
    // Utility function to count
    // the occurrence of nth term
    // in first n terms of the sequence
    int getCount(int n)
    {
      
        // Initialize count as 1
        int count = 1;
      
        int i = n - 1;
      
        while (sequence[i + 1] != 0) {
      
            // Increment count if (i+1)th term
            // is non-zero
            count++;
      
            // Previous occurrence of sequence[i]
            // will be it (i - sequence[i+1])th position
            i = i - sequence[i + 1];
        }
      
        // Return the count of occurrence
        return count;
    }
      
    // Driver code
    int main()
    {
      
        // Pre-compute Van Eck's sequence
        vanEckSequence();
      
        int n = 5;
      
        // Print count of the occurrence of nth term
        // in first n terms of the sequence
        cout << getCount(n) << endl;
      
        n = 11;
      
        // Print count of the occurrence of nth term
        // in first n terms of the sequence
        cout << getCount(n) << endl;
      
        return 0;
    }
    

    Java

    // Java program to count the occurrence
    // of nth term in first n terms
    // of Van Eck's sequence
      
    class GFG {
      
        static int MAX = 100000;
        static int sequence[] = new int[MAX + 1];
      
        // Utility function to compute
        // Van Eck's sequence
        static void vanEckSequence()
        {
      
            // Initialize sequence array
            for (int i = 0; i < MAX; i++) {
                sequence[i] = 0;
            }
      
            // Loop to generate sequence
            for (int i = 0; i < MAX; i++) {
      
                // Check if sequence[i] has occured
                // previously or is new to sequence
                for (int j = i - 1; j >= 0; j--) {
                    if (sequence[j] == sequence[i]) {
      
                        // If occurrence found
                        // then the next term will be
                        // how far back this last term
                        // occured previously
                        sequence[i + 1] = i - j;
                        break;
                    }
                }
            }
        }
      
        // Utility function to count
        // the occurrence of nth term
        // in first n terms of the sequence
        static int getCount(int n)
        {
      
            // Initialize count as 1
            int count = 1;
      
            int i = n - 1;
            while (sequence[i + 1] != 0) {
      
                // Increment count if (i+1)th term
                // is non-zero
                count++;
      
                // Previous occurrence of sequence[i]
                // will be it (i - sequence[i+1])th position
                i = i - sequence[i + 1];
            }
      
            // Return the count of occurrence
            return count;
        }
      
        // Driver code
        public static void main(String[] args)
        {
      
            // Pre-compute Van Eck's sequence
            vanEckSequence();
      
            int n = 5;
      
            // Print count of the occurrence of nth term
            // in first n terms of the sequence
            System.out.println(getCount(n));
      
            n = 11;
      
            // Print count of the occurrence of nth term
            // in first n terms of the sequence
            System.out.println(getCount(n));
        }
    }
    

    Python3

    # Python3 program to count the occurrence 
    # of nth term in first n terms 
    # of Van Eck's sequence 
    MAX = 10000
    sequence = [0] * (MAX + 1); 
      
    # Utility function to compute 
    # Van Eck's sequence 
    def vanEckSequence() :
      
        # Loop to generate sequence 
        for i in range(MAX) : 
      
            # Check if sequence[i] has occured 
            # previously or is new to sequence 
            for j in range( i - 1, -1, -1) : 
                if (sequence[j] == sequence[i]) :
                      
                    # If occurrence found 
                    # then the next term will be 
                    # how far back this last term 
                    # occured previously 
                    sequence[i + 1] = i - j; 
                    break; 
      
    # Utility function to count 
    # the occurrence of nth term 
    # in first n terms of the sequence 
    def getCount(n) : 
      
        # Initialize count as 1 
        count = 1; 
      
        i = n - 1; 
      
        while (sequence[i + 1] != 0) :
      
            # Increment count if (i+1)th term 
            # is non-zero 
            count += 1; 
      
            # Previous occurrence of sequence[i] 
            # will be it (i - sequence[i+1])th position 
            i = i - sequence[i + 1]; 
      
        # Return the count of occurrence 
        return count; 
      
    # Driver code 
    if __name__ == "__main__" : 
      
        # Pre-compute Van Eck's sequence 
        vanEckSequence(); 
      
        n = 5; 
      
        # Print count of the occurrence of nth term 
        # in first n terms of the sequence 
        print(getCount(n)); 
      
        n = 11; 
      
        # Print count of the occurrence of nth term 
        # in first n terms of the sequence 
        print(getCount(n)) ; 
      
    # This code is contributed by AnkitRai01
    

    C#

    // C# program to count the occurrence
    // of nth term in first n terms
    // of Van Eck's sequence
      
    using System;
    class GFG {
      
        static int MAX = 100000;
        static int[] sequence = new int[MAX + 1];
      
        // Utility function to compute
        // Van Eck's sequence
        static void vanEckSequence()
        {
      
            // Initialize sequence array
            for (int i = 0; i < MAX; i++) {
                sequence[i] = 0;
            }
      
            // Loop to generate sequence
            for (int i = 0; i < MAX; i++) {
      
                // Check if sequence[i] has occured
                // previously or is new to sequence
                for (int j = i - 1; j >= 0; j--) {
                    if (sequence[j] == sequence[i]) {
      
                        // If occurrence found
                        // then the next term will be
                        // how far back this last term
                        // occured previously
                        sequence[i + 1] = i - j;
                        break;
                    }
                }
            }
        }
      
        // Utility function to count
        // the occurrence of nth term
        // in first n terms of the sequence
        static int getCount(int n)
        {
      
            // Initialize count as 1
            int count = 1;
            int i = n - 1;
      
            while (sequence[i + 1] != 0) {
      
                // Increment count if (i+1)th term
                // is non-zero
                count++;
      
                // Previous occurrence of sequence[i]
                // will be it (i - sequence[i+1])th position
                i = i - sequence[i + 1];
            }
      
            // Return the count of occurrence
            return count;
        }
      
        // Driver code
        public static void Main(string[] args)
        {
      
            // Pre-compute Van Eck's sequence
            vanEckSequence();
      
            int n = 5;
      
            // Print count of the occurrence of nth term
            // in first n terms of the sequence
            Console.WriteLine(getCount(n));
      
            n = 11;
      
            // Print count of the occurrence of nth term
            // in first n terms of the sequence
            Console.WriteLine(getCount(n));
        }
    }
    
    输出:
    1
    5