hdu-6130-Kolakoski

来源:互联网 发布:高达00和高达seed知乎 编辑:程序博客网 时间:2024/06/05 22:53

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6130


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,…


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


打表直接求

k表示的前面那一组的值,相邻两组不相等

a[j]就是所求组的个数,1个还是2个


#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <cmath>#include <queue>#include <set>#include <map>#define N 10000005using namespace std;int a[N];void init(){    a[1] = 1;    a[2] = 2;    a[3] = 2;    int k = 2;    for(int i = 4, j = 3; i < N; i++)    {        if(a[j] == 1)        {            if(k == 1)                a[i] = 2, k = 2;            else                a[i] = 1, k = 1;        }        else        {            if(k == 1)                a[i] = 2, a[++i] = 2, k = 2;            else                a[i] = 1, a[++i] = 1, k = 1;        }        j ++;    }}int main(){    init();    int T;    scanf("%d", &T);    while(T--)    {        int b;        scanf("%d", &b);        printf("%d\n", a[b]);    }    return 0;}