题目:
Kolakoski
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 540 Accepted Submission(s): 281
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
Sample Output
Source
2017 Multi-University Training Contest - Team 7
Recommend
liuyiding | We have carefully selected several similar problems for you: 6132 6131 6130 6129 6128
Statistic | Submit | Discuss | Note
思路:
关于这个数列的定义:Kolakoski序列
关于这个数列的推导:
首先我们定义:
a[n]代表第n组数的个数,把数列中相同的数定义为一组
首先给出a[1]=1,a[2]=2
因为a[2]=2,那么就证明第二组数有连续的两个2,那么数列现在是这样的
1 2 2
现在数列的a[3]位置变成了2,就证明这个数列后面要加两个1,也就是这样的:
1 2 2 1 1
现在a[4]=1,a[5]=1,我们就能推出a[6]=2,a[7]=1
做法就是模拟这个过程,打表~
但是不知道为啥奇数输出1,偶数输出2,也能AC
代码:
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define mod 1000007
#define ll long long
using namespace std;
const int N=1e7+5;
int a[N];
void init()
{
a[1]=1;
a[2]=2;
a[3]=2;
int cnt=3;
int flag=2;
for(int i=4; i