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

【2017 Multi-University Training Contest - Team 7】Kolakoski

2017-10-04 18:44 567 查看

【Link】:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1011&cid=765

【Description】

有一种数列叫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个数。

【Solution】

相邻的相同的数字的个数这个数列f的增长速度是没有原数列a的增长速度快的.
则对于新添加在a末尾的数字,看看它改变的相应的f值的最后一项是不是和a的相应的位置相同即可。
只有1和2两个数字,两个数字都试试就好.
这样就能处理出来整个数列了,O(1)输出就好。

【NumberOf WA】

0

【Reviw】


【Code】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define ri(x) scanf("%d",&x)
#define rl(x) scanf("%lld",&x)
#define rs(x) scanf("%s",x+1)
#define oi(x) printf("%d",x)
#define ol(x) printf("%lld",x)
#define oc putchar(' ')
#define os(x) printf(x)
#define all(x) x.begin(),x.end()
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 110;

int n,a[10000000+10];

int main(){
//Open();
//Close();
a[1] = 1;
int j = 1;
int now = 1;
for (int i = 2;i <= 10000000+10;i++){
a[i] = a[i-1];
if (now + 1 != a[j]){
a[i] = 3 - a[i];
now = 1;
j++;
}else{
now++;
}
}
int T;
ri(T);
while (T--){
int n;
ri(n);
oi(a
);
puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐