您的位置:首页 > 大数据 > 人工智能

2017 Multi-University Training Contest - Team 7 1011 Kolakoski

2017-08-15 21:17 393 查看

Kolakoski

[align=center]Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 392    Accepted Submission(s): 188
[/align]

[align=left]Problem Description[/align]

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.
 

[align=left]Input[/align]

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≤10 
7   ).
 

[align=left]Output[/align]

For each test case:

A single line contains a nonnegative integer, denoting the answer.
 

[align=left]Sample Input[/align]

2
1
2

 

[align=left]Sample Output[/align]

1
2

 

题意:

有一种数列叫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;
typedef long long ll;
const int MAXN = 1 * 1e7 + 10000;
#define lp(i,l,r) for (int i=l;i<=r;i++)

int f[MAXN];
void init()
{
int tmp = 1, i = 1, j = 1;
while (j <= 1e7 + 500)
{
f[j] = tmp;
j++;
lp(k, 2, f[i])
{
f[j++] = tmp;
}
i++;
tmp = (tmp == 1) ? 2 : 1;
}
}
int main()
{
std::ios::sync_with_stdio(0);
init();
int T;
cin >> T;
while (T--)
{
int n;
cin >> n;
cout << f
<< endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  math hdu 多校 找规律
相关文章推荐