您的位置:首页 > 其它

hdu 6130 Kolakoski

2017-10-30 20:52 357 查看
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

 

Recommend

liuyiding   |   We have carefully selected several similar problems for you:  6216 6215 6214 6213 6212 

题意:给两组数,两组数都只由1、2组成,上边那组数对应位置的数字是下边那组数的对应位置的数字的个数,而下边那组数拆开后和上边的数完全一样。

刚开始做的时候意味着两组数之后的数是有一定规律的,推了半天的规律,没找到,然后又看了一下给的数的范围和时间,然后就用模拟过了........

直接按照题目给的规律模拟,我是用的队列来模拟的。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
int main(){
int T;
ll m,n,a,b;
scanf("%d",&T);
while(T--){
queue<ll> q;
scanf("%lld",&n);
if(n==1){
printf("1\n");
}else if(n==2){
printf("2\n");
}else if(n==3){
printf("2\n");
}else{
n-=3;
q.push(2);
while(n--){
ll w=q.back();
ll t=q.front();
if(t==1&&w==1){
q.push(2);
q.pop();
}
else if(t==1&&w==2){
q.push(1);
q.pop();
}
else if(t==2&&w==2){
q.push(1);
q.push(1);
q.pop();
}
else if(t==2&&w==1){
q.push(2);
q.push(2);
q.pop();
}
}
printf("%lld\n",q.front());
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: