ZZU PRACTICE 4 URAL 1209(郑大…
2012-12-05 17:11
351 查看
A - 1, 10, 100, 1000...
p
[Submit]
[Go
Back] [Status]
Description
Let's consider an infinite sequence
of digits constructed of ascending powers of 10 written one after
another. Here is the beginning of the sequence: 110100100010000…
You are to find out what digit is located at the definite position
of the sequence.
Input
There is the only integer N
in the first line (1 ≤ N ≤ 65535). The i-th of
N left lines contains the integer Ki — the
number of position in the sequence (1 ≤ Ki
≤ 231 − 1).
Output
You are to output N digits 0
or 1 separated with a space. More precisely, the i-th digit
of output is to be equal to the Ki-th digit of
described above sequence.
Sample Input
Sample Output
Hint
[Submit]
[Go
Back] [Status]
这道题挺有意思的,挺简单的题竟然也可以弄到这么难,经典啊,题意就是一个类似 110100100010000…的字符串 找出位于N的字符类型,显然只有0和1了,如果按照常规方法做的话,就是从1一直减,直到减不动为止,然后判定,这题如果这样做的话肯定是超时的,还有一个简单的方法,就是二分查找,从最大值(我定的是100000)开始二分查找,知道找到为止,这里想必从1加到N的公式就不用说了吧,最后找到的值一定离确切的值很近(二分学的不好,所以我开的范围大了
),注意,算值的时候一定要把mid和sum都定义为__int64类型的,不然你会一直WA的
代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
__int64
mid,sum;
int
main()
{
int
n,low,hig,t,i;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
if(n==1){printf("1\n");continue;}
low=1;hig=200000;
while(low<hig)
{
mid=(low+hig)/2;
sum=mid*(mid+1)/2;
if(sum>n)
hig=(int)mid-1;
else
low=(int)mid+1;
}
if(low<200)i=0;
else
i=low-200;
for(;i<=hig+200;i++)
{
mid=(__int64)i;
sum=mid*(mid+1)/2;
if(n<sum)
break;
}
i--;
mid=(__int64)i;
sum=mid*(mid+1)/2;
n=n-(int)sum;
if(n==1)printf("1\n");
else
printf("0\n");
}
return
0;
}
p
Time Limit: 1000MS | Memory Limit: 16384K | 64bit IO Format: %I64d & %I64u |
[Go
Back] [Status]
Description
Let's consider an infinite sequence
of digits constructed of ascending powers of 10 written one after
another. Here is the beginning of the sequence: 110100100010000…
You are to find out what digit is located at the definite position
of the sequence.
Input
There is the only integer N
in the first line (1 ≤ N ≤ 65535). The i-th of
N left lines contains the integer Ki — the
number of position in the sequence (1 ≤ Ki
≤ 231 − 1).
Output
You are to output N digits 0
or 1 separated with a space. More precisely, the i-th digit
of output is to be equal to the Ki-th digit of
described above sequence.
Sample Input
input | output |
---|---|
4 3 14 7 6 | 0 0 1 0 |
Hint
[Submit]
[Go
Back] [Status]
这道题挺有意思的,挺简单的题竟然也可以弄到这么难,经典啊,题意就是一个类似 110100100010000…的字符串 找出位于N的字符类型,显然只有0和1了,如果按照常规方法做的话,就是从1一直减,直到减不动为止,然后判定,这题如果这样做的话肯定是超时的,还有一个简单的方法,就是二分查找,从最大值(我定的是100000)开始二分查找,知道找到为止,这里想必从1加到N的公式就不用说了吧,最后找到的值一定离确切的值很近(二分学的不好,所以我开的范围大了
),注意,算值的时候一定要把mid和sum都定义为__int64类型的,不然你会一直WA的
代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
__int64
mid,sum;
int
main()
{
int
n,low,hig,t,i;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
if(n==1){printf("1\n");continue;}
low=1;hig=200000;
while(low<hig)
{
mid=(low+hig)/2;
sum=mid*(mid+1)/2;
if(sum>n)
hig=(int)mid-1;
else
low=(int)mid+1;
}
if(low<200)i=0;
else
i=low-200;
for(;i<=hig+200;i++)
{
mid=(__int64)i;
sum=mid*(mid+1)/2;
if(n<sum)
break;
}
i--;
mid=(__int64)i;
sum=mid*(mid+1)/2;
n=n-(int)sum;
if(n==1)printf("1\n");
else
printf("0\n");
}
return
0;
}
相关文章推荐
- Java学习笔记4 java中的运算符
- ZZULI_TEAM_PRACTICE(1) POJ 2181…
- Flex&nbsp;4&nbsp;的&nbsp;Spark&nbsp;组件
- poj&nbsp;2785&nbsp;4&nbsp;Values…
- ZZULI_SUMMER_PRACTICE(7) 1177 "…
- ZZULI_TEAM_PRACTICE(1) POJ 1099…
- ZZULI_TEAM_PRACTICE(1) POJ 3790…
- Matlab 4 parameters&nb…
- C++学习4 - 内联函数
- 【Objective-C 语言】4 …
- ZZULI_TEAM_PRACTICE(1) POJ 2259…
- SSH&nbsp;整合-&nbsp;4&nbsp;-&nbsp;add&nbsp;service_servic…
- 文件切割合并器&nbsp;4&nbsp;帮助类&nbsp;Help
- Flash AS3 学习4 - 方块的旋转效果
- bash shell笔记4 处理用…
- HDU 1405 The Last Practice
- 我的linux笔记4&nbsp;文件的压缩与打包
- Practice&nbsp;and&nbsp;Knowledge
- ZZULI_SUMMER_PRACTICE(8) POJ 10…
- [转]Qt-Embedded 4 install