HDU 6130 Kolakoski(数学)

Kolakoski

2017 Multi-University Training Contest - Team 7
题目链接

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 281 Accepted Submission(s): 122

Problem Description
This is Kolakosiki sequence: 1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1,2,1,1,2,1,2,2,1,1. This sequence consists of and , and its first term equals . Besides, if you see adjacent and equal terms as one group, you will get 1,22,11,2,1,22,1,22,11,2,11,22,1……. . Count number of terms in every group, you will get the sequence itself. Now, the sequence can be uniquely determined. Please tell HazelFan its th element.

Input
The first line contains a positive integer , denoting the number of test cases.
For each test case:
A single line contains a positive integer .

Output
For each test case:
A single line contains a nonnegative integer, denoting the answer.

Sample Input
2
1
2

Sample Output
1
2

Source
2017 Multi-University Training Contest - Team 7

Recommend
liuyiding

一道数学题
Kolakoski序列

Kolakoski序列是一个仅由1和2组成的无限数列,是一种通过“自描述”来定义的数列[1] 。
他的前几项为
1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1,2,1,1,2,1,2,2,1,1,…(OEIS上的A000002)

它的定义很简单,若把数列中相同的数定为一组,令a(1)=1,a(2)=2,则a(n)等于第n组数的长度。

可以根据这个定义来推算第三项以后的数:例如由于a(2)=2,因此第2组数的长度是2,因此a(3)=2,;

由于a(3)=2,所以第三组数的长度是2,因此a(4)=a(5)=1;由于a(4)=1,a(5)=1,所以第四组数和第五组数的长度都为1,因此a(6)=2,a(7)=1,以此类推。

模拟打表暴力求解

具体看代码吧。

#include<cstdio>
#include<cstring>
using namespace std;
int a[10000007];
int main()
{
    a[1]=1;
    a[2]=2;
    int n=2;
    for(int i=2;i<10000007;)
    {
        int num;
        num=a[n++];
        if(n%2==1)
        {
            for(int j=1;j<=num;j++)
            {
                a[i++]=2;
            }
        }
        else
        {
            for(int j=1;j<=num;j++)
            {
                a[i++]=1;
            }
        }
    }
    int T,x;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&x);
        printf("%d
",a[x]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/nanfenggu/p/7900055.html