HDU6130 Kolakoski(模拟,2017 HDU多校联赛 第7场)

来源:互联网 发布:新生分班软件 编辑:程序博客网 时间:2024/05/18 02:19

题目:

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(1T5), denoting the number of test cases.
For each test case:
A single line contains a positive integer n(1n107).
 

Output
For each test case:
A single line contains a nonnegative integer, denoting the answer.
 

Sample Input
212
 

Sample Output
12
 

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 <cstdio>#include <cstring>#include <cctype>#include <string>#include <set>#include <iostream>#include <stack>#include <cmath>#include <queue>#include <vector>#include <algorithm>#define mem(a,b) memset(a,b,sizeof(a))#define inf 0x3f3f3f3f#define mod 1000007#define ll long longusing 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<N;){int num=a[cnt];for(int j=0; j<num; j++){if(flag==2)a[i]=1;elsea[i]=2;if(j==num-1) flag=a[i];i++;}cnt++;}}int main(){init();int t,n;scanf("%d",&t);while(t--){scanf("%d",&n);printf("%d\n",a[n]);}return 0;}


原创粉丝点击