Kolakoski

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)

Total Submission(s): 0    Accepted Submission(s): 0


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
 

Statistic | Submit | Clarifications | Back

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 1e7 + 100;
int table[maxn] = { 0,1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1 };
int table1[maxn] = { 0,1 };
long long int ti = 2;
int tem = 1;
void init() {
for (int i = 2; ti < maxn-50; ++i) {
tem = tem % 2 + 1;
if (table[i] == 2) {
table1[ti] = tem;
table[ti++] = tem;
table1[ti] = tem;
table[ti++] = tem;
}
else {
table1[ti] = tem;
table[ti++] = tem;
}
}
}
int main()
{
init();
int t;
cin >> t;
long long int n;
while (t--)
{
cin >> n;
cout << table1[n] << endl; }
return 0;
}

最新文章

  1. 前端HTML5几种存储方式的总结
  2. 从Erlang进程看协程思想
  3. ping脚本
  4. Java笔记4-do while循环,break,修饰符,方法的调用
  5. [转]GridView排序——微软提供Sort
  6. Photoshop:建议设置
  7. 将数据库从普通文件系统迁移到ASM中
  8. Objective-C基础之──多态
  9. win32 清空ListBox所有内容
  10. Python之路Day19
  11. 用于Mysql操作的MySqlHelper类
  12. memcached 安装以及在php中使用
  13. angular2 学习笔记 (Typescript - Attribute &amp; reflection &amp; decorator)
  14. C语言强化——指针
  15. Django:全文检索功能可参考博客
  16. Unity协程Coroutine使用总结和一些坑
  17. element-ui upload组建上传 file-list踩过的坑
  18. iOS:仿写探探App动画
  19. P1110 [ZJOI2007]报表统计
  20. MySQL使用笔记(八)统计函数和分组数据记录查询

热门文章

  1. uva10537 最短路 倒推
  2. node.js 读取文件--createReadStream
  3. 2.3 comparator(比较器)
  4. POJ1068 Parencodings 解题报告
  5. HTML学习之Flex 布局
  6. Array.prototype.map()方法详解
  7. HDU 4983 Goffi and GCD(数论)
  8. android 加密手机完毕后待机两分钟出现有频率的杂音
  9. react 执行 yarn build 页面无法显示
  10. Silverlight+WCF实现跨域调用