Gijswijt的序列是一个自描述序列,其中每个项的值等于该数字之前的序列中重复的数字块的最大数量。
让我们采用序列a(i)的第i项,然后对于这个吉斯维特序列:
其中k是最大自然数,因此单词a(1)a(2).. a(n)可以表示为x *(y ^ k) ,其中y的长度不为零。
Gijswijt的顺序如下:
1, 1, 2, 1, 1, 2, 2, 2, 3, 1, ...
可以证明,每个自然数至少在该序列中出现一次,但是该序列的增长非常缓慢。第220个项是4,而第5个项出现在第10 ^ 10 ^ 23个项附近。
例子:
Input: n = 10
Output: 1, 1, 2, 1, 1, 2, 2, 2, 3, 1
Input: n = 220
Output: 1, 1, 2, 1, 1, 2, 2, 2, 3, 1, …, 3, 2, 1, 1, 2, 1, 1, 2, 2, 2, 3, 1, 1, 2, 1, 1, 2, 2, 2, 3, 2, 2, 2, 3, 2, 2, 2, 3, 3, 2, 2, 2, 3, 2, 2, 2, 3, 2, 2, 2, 3, 3, 2, 2, 2, 3, 2, 2, 2, 3, 2, 2, 2, 3, 3, 3, 3, 4,
方法:我们必须打印该系列的前n个术语。我们将保留一个向量,该向量将存储序列的所有前一个元素,下一个术语将是任意长度的重复块的最大数量。因此,我们将长度从1更改为n-1,然后使用循环找出计数。
例子:
- 令序列为1、1、2、1、1、2
- 从0到5取i值,第一个模式包含“ 2”。
- 从数组末尾仅重复2,下一个模式是“ 1、2”。
- 1,2从数组末尾仅重复一次,下一个模式是“ 1,1,2”。
- 1、1、2从数组末尾开始重复两次,因此计数为2。
下面是上述方法的实现:
C++
// C++ program to demonstrate
// Gijswijt's sequence
#include
using namespace std;
// if the sequence is a(1)a(2)a(3)..a(n-1)
// check if the sequence can be represented as
// x*(y^k) find the largest value of k
int find_count(vector ele)
{
// count
int count = 0;
for (int i = 0; i < ele.size(); i++) {
// pattern of elements of size
// i from the end of sequence
vector p;
// count
int c = 0;
// extract the pattern in a reverse order
for (int j = ele.size() - 1;
j >= (ele.size() - 1 - i) && j >= 0;
j--)
p.push_back(ele[j]);
int j = ele.size() - 1, k = 0;
// check how many times
// the pattern is repeated
while (j >= 0) {
// if the element dosent match
if (ele[j] != p[k])
break;
j--;
k++;
// if the end of pattern is reached
// set value of k=0 and
// increase the count
if (k == p.size()) {
c++;
k = 0;
}
}
count = max(count, c);
}
// return the max count
return count;
}
// print first n terms of
// Gijswijt's sequence
void solve(int n)
{
// set the count
int count = 1;
// stoes the element
vector ele;
// print the first n terms of
// the sequence
for (int i = 0; i < n; i++) {
cout << count << ", ";
// push the element
ele.push_back(count);
// find the count for next number
count = find_count(ele);
}
}
// Driver code
int main()
{
int n = 10;
solve(n);
return 0;
}
Java
// Java program to demonstrate
// Gijswijt's sequence
import java.util.*;
class GFG
{
// if the sequence is a(1)a(2)a(3)..a(n-1)
// check if the sequence can be represented as
// x*(y^k) find the largest value of k
static int find_count(Vector ele)
{
// count
int count = 0;
for (int i = 0; i < ele.size(); i++)
{
// pattern of elements of size
// i from the end of sequence
Vector p = new Vector();
// count
int c = 0;
// extract the pattern in a reverse order
for (int j = ele.size() - 1;
j >= (ele.size() - 1 - i) && j >= 0;
j--)
{
p.add(ele.get(j));
}
int j = ele.size() - 1, k = 0;
// check how many times
// the pattern is repeated
while (j >= 0)
{
// if the element dosent match
if (ele.get(j) != p.get(k))
{
break;
}
j--;
k++;
// if the end of pattern is reached
// set value of k=0 and
// increase the count
if (k == p.size())
{
c++;
k = 0;
}
}
count = Math.max(count, c);
}
// return the max count
return count;
}
// print first n terms of
// Gijswijt's sequence
static void solve(int n)
{
// set the count
int count = 1;
// stoes the element
Vector ele = new Vector();
// print the first n terms of
// the sequence
for (int i = 0; i < n; i++)
{
System.out.print(count + ", ");
// push the element
ele.add(count);
// find the count for next number
count = find_count(ele);
}
}
// Driver code
public static void main(String[] args)
{
int n = 10;
solve(n);
}
}
// This code is contributed by PrinciRaj1992
Python3
# Python3 program to demonstrate
# Gijswijt's sequence
# if the sequence is a(1)a(2)a(3)..a(n-1)
# check if the sequence can be represented as
# x*(y^k) find the largest value of k
def find_count(ele):
# count
count = 0
for i in range(len(ele)):
# pattern of elements of size
# i from the end of sequence
p = []
# count
c = 0
j = len(ele) - 1
# extract the pattern in a reverse order
while j >= (len(ele) - 1 - i) and j >= 0:
p.append(ele[j])
j -= 1
j = len(ele) - 1
k = 0
# check how many times
# the pattern is repeated
while j >= 0:
# if the element dosent match
if ele[j] != p[k]:
break
j -= 1
k += 1
# if the end of pattern is reached
# set value of k=0 and
# increase the count
if k == len(p):
c += 1
k = 0
count = max(count, c)
# return the max count
return count
# print first n terms of
# Gijswijt's sequence
def solve(n):
# set the count
count = 1
# stoes the element
ele = []
# print the first n terms of
# the sequence
for i in range(n):
print(count, end = " ")
# push the element
ele.append(count)
# find the count for next number
count = find_count(ele)
# Driver Code
if __name__ == "__main__":
n = 10
solve(n)
# This code is contributed by
# sanjeev2552
C#
// C# program to demonstrate
// Gijswijt's sequence
using System;
using System.Collections.Generic;
class GFG
{
// if the sequence is a(1)a(2)a(3)..a(n-1)
// check if the sequence can be represented as
// x*(y^k) find the largest value of k
static int find_count(List ele)
{
// count
int count = 0;
for (int i = 0; i < ele.Count; i++)
{
// pattern of elements of size
// i from the end of sequence
List p = new List();
// count
int c = 0, j;
// extract the pattern in a reverse order
for (j = ele.Count - 1;
j >= (ele.Count - 1 - i) && j >= 0;
j--)
{
p.Add(ele[j]);
}
j = ele.Count - 1;
int k = 0;
// check how many times
// the pattern is repeated
while (j >= 0)
{
// if the element doesn't match
if (ele[j] != p[k])
{
break;
}
j--;
k++;
// if the end of pattern is reached
// set value of k=0 and
// increase the count
if (k == p.Count)
{
c++;
k = 0;
}
}
count = Math.Max(count, c);
}
// return the max count
return count;
}
// print first n terms of
// Gijswijt's sequence
static void solve(int n)
{
// set the count
int count = 1;
// stoes the element
List ele = new List();
// print the first n terms of
// the sequence
for (int i = 0; i < n; i++)
{
Console.Write(count + ", ");
// push the element
ele.Add(count);
// find the count for next number
count = find_count(ele);
}
}
// Driver code
public static void Main(String[] args)
{
int n = 10;
solve(n);
}
}
// This code is contributed by Rajput-Ji
输出:
1, 1, 2, 1, 1, 2, 2, 2, 3, 1,