hdu 6130 _数学规律

来源:互联网 发布:cpu温度检测软件 编辑:程序博客网 时间:2024/05/01 00:24

Kolakoski

This is Kolakosiki sequence: 1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1……. This sequence consists of 1 and 2, and its first term equals 1. 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 nth element.
Input
The first line contains a positive integer T(1≤T≤5), denoting the number of test cases.
For each test case:
A single line contains a positive integer n(1≤n≤107).
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

按Kolakoski数列规律模拟就好

#include<iostream>#include<cstdio>#include<cstring>#include<map>typedef long long ll;using namespace std;const ll N=(ll)1e7+1;ll num[N]= {0,1,2,2};int main(){    ll i,j,t,n,k=3;    for(i=3; k<N; i++)    {        if(num[i]==2)        {            if(num[k]==2)            {                num[k+1]=1;                num[k+2]=1;            }            else            {                num[k+1]=2;                num[k+2]=2;            }            k+=2;        }        else        {            if(num[k]==1)                num[k+1]=2;            else                num[k+1]=1;            k+=1;        }    }   scanf("%lld",&t);   while(t--)   {       scanf("%lld",&n);       printf("%lld\n",num[n]);   }    return 0;}