Round A APAC Test 2016 Problem A. Googol String
2015-08-24 19:41
375 查看
Problem A. Googol String
This contest is open for practice. You can try every problem as many times as you like, though we won't keep track of which problems you solve. Read the Quick-StartGuide to get started.
Small input 7 points | Solve A-small |
Large input 12 points | Solve A-large |
Problem
A "0/1 string" is a string in which every character is either0or
1. There are two operations that
can be performed on a 0/1 string:
switch: Every
0becomes
1and every
1becomes
0.
For example, "100" becomes "011".
reverse: The string is reversed. For example, "100" becomes "001".
Consider this infinite sequence of 0/1 strings:
S0 = ""
S1 = "0"
S2 = "001"
S3 = "0010011"
S4 = "001001100011011"
...
SN = SN-1 + "0" + switch(reverse(SN-1)).
You need to figure out the Kth character of Sgoogol, where googol = 10100.
Input
The first line of the input gives the number of test cases, T. Each of the next T lines contains a number K.Output
For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the Kth character of Sgoogol.Limits
1 ≤ T ≤ 100.Small dataset
1 ≤ K ≤ 105.Large dataset
1 ≤ K ≤ 1018.Sample
Input | Output |
4 1 2 3 10 |
其实,我们可以发现,每个字符串都是成倍+1的增长,如果是在中心点(也就是2^n -1)则一定就是0,否则的话,按中心点对称,就是转化为2^n -2 - k;这样,很快就会变的很小或者为中心点,再记录翻转的次数,也就是这样转化的次数,如果是奇数次就是1,否则就是0,直接得到答案了。复杂度为o(log(k));
#define N 205
#define M 100005
#define maxn 205
#define MOD 1000000000000000007
int n,T;
ll p[100];
ll k;
int main()
{
//freopen("A-large.in", "r", stdin);
//freopen("A-large.out", "w", stdout);
p[0] = 1;
For(i,1,100){
p[i] = p[i-1] * 2;
printf("%lld %d\n",p[i],i);
}
while(S(T)!=EOF)
{
For(ta,1,T+1){
scanf("%lld",&k);k--;
int flag = 0,num = 0;
bool isEnd = false;
while(k){
//printf("%lld k ",k);
For(i,0,100){
if(p[i] - 2 >= k){
flag = i;
break;
}
}
if(k == p[flag] - 2ll - k){
isEnd = true;
}
if(isEnd) break;
num++;
k = p[flag] - 2ll - k;
}
//printf(" num %d ",num);
if(!(num & 1))
printf("Case #%d: 0\n",ta);
else
printf("Case #%d: 1\n",ta);
}
}
//fclose(stdin);
//fclose(stdout);
return 0;
}
相关文章推荐
- STL - vector algorithm
- google hosts google翻译ip
- 杭电3635-Dragon Balls-并查集之路径压缩
- django RESTful研究一环境搭建和理解RESTful
- UVA 11971 Polygon 多边形(连续概率)
- 第一个基于Django的页面: Hello World
- django model filter 条件过滤,及多表连接查询、反向查询
- go 的 time ticker 设置定时器
- Django项目 实例 日记
- Algorithms—235.Lowest Common Ancestor of a Binary Search Tree
- Django 文件下载功能
- 搭建GoldenGate的单向复制环境
- 设置textField的placegolder的字体大小和字体颜色
- Algorithms—242.Valid Anagram
- GOOGLE校招的笔试题解析
- whatsup gold snmp安装脚本
- Algorithms—264.Ugly Number II
- STL学习----入门(1)[algorithm]
- Algorithms—263.Ugly Number
- Algorithms—268.Missing Number