HDU 6130 Kolakoski【找规律】

来源:互联网 发布:php蜘蛛强制劫持跳转 编辑:程序博客网 时间:2024/05/17 09:33

题目来戳呀

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……. 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:1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1……,如果将这个数组相同的数字合并成一个新数列:1,22,11,2,1,22,1,22,11,2,11,22,1……,第二个数列中第几组数中的个数就是第一个数列中的第几个数。现在要求出第一个数列第n个数。

想法:

前几个数相当于已经定下来了,我们发现这并没有什么规律可言+_+
于是从给的第二个数列入手,a(i)表示第i组的个数及第i个数:
由于a(2)=2,因此第2组数的长度是2,因此a(3)=2;
由于a(3)=2,所以第三组数的长度是2,因此a(4)=a(5)=1;(因为若a(4)=2,就与前面两个2重复了,第二组数的个数就不会是2了
由于a(4)=1,a(5)=1,所以第四组数和第五组数的长度都为1,因此a(6)=2,a(7)=1,以此类推。
根据前面的能推出后面的 。

#include<bits/stdc++.h>using namespace std;const int maxn=1e7+10;int a[maxn];int main(){    int n,t;    scanf("%d",&t);    while(t--)    {        scanf("%d",&n);        a[1]=1;        a[2]=a[3]=2;        int tmp=3,flag=2;///flag记录前一个数        for(int i=4;i<maxn;)///从第3组开始后的第4个数之后就能依次求出了        {            int num=a[tmp];///第tmp组的长度            for(int j=1;j<=num;++j)            {                if(flag==2)                    a[i]=1;                else                    a[i]=2;                if(j==num)///当前到了该组的最后一个数 要记录下来                    flag=a[i];                ++i;///下一个数            }            tmp++;///下一组        }        printf("%d\n",a[n]);    }    return 0;}

ps:队友竟然去找Kolakoski这个数列了+_+还他瓜找到了,但是tle了QAQ

原创粉丝点击