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
2
1
2
 
Sample Output
1
2
 
题意:

Kolakoski序列是一个仅由1和2组成的无限数列,是一种通过“自描述”来定义的数列[1]  。他的前几项为
1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1,2,1,1,2,1,2,2,1,1,…
它的定义很简单,若把数列中相同的数定为一组,令a(1)=1,a(2)=2,则a(n)等于第n组数的长度。
可以根据这个定义来推算第三项以后的数:例如由于a(2)=2,因此第2组数的长度是2,因此a(3)=2,;
由于a(3)=2,所以第三组数的长度是2,因此a(4)=a(5)=1;由于a(4)=1,a(5)=1,所以第四组数和第五组数的长度都为1,因此a(6)=2,a(7)=1,以此类推。
给出n求第n项的数字是多少
题解:
直接预处理根据性质算出序列
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=1e7+;
int a[maxn];
int b[maxn]; void pre()
{
int cnta=,cntb=;
a[]=;b[]=;b[]=;
for(cnta=,cntb=;cnta<maxn&&cntb<maxn;cnta++)
{
a[cnta]=b[cnta];
if(a[cnta]==)
{
if(b[cntb]==)
{
b[cntb+]=;
}
else
{
b[cntb+]=;
}
cntb++;
}
else
{
if(b[cntb]==)
{
b[cntb+]=;
b[cntb+]=;
}
else
{
b[cntb+]=;
b[cntb+]=;
}
cntb+=;
}
}
} int main()
{
pre();
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
printf("%d\n",b[n]);
}
return ;
}
 

HDU 3130 17多校7 Kolakoski(思维简单)的更多相关文章

  1. HDU 6140 17多校8 Hybrid Crystals(思维题)

    题目传送: Hybrid Crystals Problem Description > Kyber crystals, also called the living crystal or sim ...

  2. HDU 6124 17多校7 Euler theorem(简单思维题)

    Problem Description HazelFan is given two positive integers a,b, and he wants to calculate amodb. Bu ...

  3. HDU 6034 17多校1 Balala Power!(思维 排序)

    Problem Description Talented Mr.Tang has n strings consisting of only lower case characters. He want ...

  4. HDU 6098 17多校6 Inversion(思维+优化)

    Problem Description Give an array A, the index starts from 1.Now we want to know Bi=maxi∤jAj , i≥2. ...

  5. HDU 6092 17多校5 Rikka with Subset(dp+思维)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  6. HDU 6090 17多校5 Rikka with Graph(思维简单题)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  7. HDU 6095 17多校5 Rikka with Competition(思维简单题)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  8. HDU 6049 17多校2 Sdjpx Is Happy(思维题difficult)

    Problem Description Sdjpx is a powful man,he controls a big country.There are n soldiers numbered 1~ ...

  9. HDU 6143 17多校8 Killer Names(组合数学)

    题目传送:Killer Names Problem Description > Galen Marek, codenamed Starkiller, was a male Human appre ...

随机推荐

  1. 什么是CSS hack?

    1.什么是CSS hack? CSS hack是通过在CSS样式中加入一些特殊的符号,让不同的浏览器识别不同的符号(什么样的浏览器识别什么样的符号是有标准的,CSS hack就是让你记住这个标准),以 ...

  2. python爬虫---BeautifulSoup的用法

    BeautifulSoup是一个灵活的网页解析库,不需要编写正则表达式即可提取有效信息. 推荐使用lxml作为解析器,因为效率更高. 在Python2.7.3之前的版本和Python3中3.2.2之前 ...

  3. suffix word al ain aire out ~A1

    1◆ al 2◆ ain ~的人   3◆ aire 表名词  

  4. sigmoid belief network boltszmann machine

    because of explaining away, the hidden weights in sigmoid belief network is no longer independent

  5. POJ 2243 Knight Moves(BFS)

    POJ 2243 Knight Moves A friend of you is doing research on the Traveling Knight Problem (TKP) where ...

  6. 普通01背包问题(dp)

    有n个物品,重量和价值分别为wi和vi,从这些物品中挑选出重量不超过W的物品,求所有挑选方案中物品价值总和的最大值 限制条件: 1 <= n <= 100; 1 <= wi,vi & ...

  7. webpack分离打包css和less

    github仓库:https://github.com/llcMite/webpack.git 为什么要分离打包?       答:刚开始学webpack的时候就很郁闷,明明没几个文件,打包出来体积特 ...

  8. day25 模块04_模块和包

    休养生息--模块04 1.导入模块的执行的步骤 2.自定义模块 3.自定义包 一.导入模块的执行步骤 1).判断当前正在导入的模块是否已经导入过 2).如果已经导入过,不会重新导入该模块 3).如果没 ...

  9. linux生成SSH key

    1. 检查SSH keys是否存在 ls -al ~/.ssh2. 生成新的ssh key 输入 ssh-keygen -t rsa -C your_email@example.com

  10. svn服务器搭建及使用(三)

    接下来,试试用TortoiseSVN修改文件,添加文件,删除文件,以及如何解决冲突等. 添加文件 在检出的工作副本中添加一个Readme.txt文本文件,这时候这个文本文件会显示为没有版本控制的状态, ...