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. javaee登录界面

    首先在此之前我们应该正确安装数据库,以及eclipse(javaee)文件包. 1.首先在javaee中建立新的项目 2.右键点击WebContent-New-JSP File,新建jsp(动态)文件 ...

  2. leetcode-algorithms-15 3Sum

    leetcode-algorithms-15 3Sum Given an array nums of n integers, are there elements a, b, c in nums su ...

  3. Python序列化-pickle和json模块

    Python的“file-like object“就是一种鸭子类型.对真正的文件对象,它有一个read()方法,返回其内容.但是,许多对象,只要有read()方法,都被视为“file-like obj ...

  4. 【Linux】bash shell学习

    Bash Shell Linux系统的合法shell都写入/etc/shells这个文件,默认使用的shell版本称为“Bourne Again Shell(简称bash)” 用户登录时系统会分配一个 ...

  5. Xshell中文乱码怎么处理?

    改成如下图:

  6. Linux Shell数值比较和字符串比较及相关

    说明: 1. 把字符串当成整型进行比较,由于abcd等字符对不上0123当程序尝试去转成二进制时无法完成转换,所以用于数值比较的运算不能用于字符串比较:但是把整型当成字符串进行比较,0123这些数值完 ...

  7. S3cmd命令行管理对象存储

    我的使用步骤 cd /usr/    目录 git clone https://github.com/jdcloud-cmw/s3cmd.git   下载文件 ln -s /usr/s3cmd/s3c ...

  8. django自定义标签,int转化为str类型

    1.在app中创建templatetags目录,目录名必须为templatetags 2.在目录templatetags中创建一个.py文件,例如 strFilter.py strFilter.py ...

  9. flask+apache+mod-wsgi部署遇到的坑

    首先,看到这种方式部署,我也有疑问,为什么不用nginx,gunicorn.接手的项目,就先按照前人思路run起来. 线上使用ubuntu系统,apache2,而给我玩耍的测试机是centos6.5, ...

  10. flask-admin fileadmin 上传文件,中文名的解决方案 重写部分secure_filename

    class upload_view(FileAdmin): def _save_form_files(self, directory, path, form): super() filename = ...