您的位置:首页 > 其它

ZZU PRACTICE 4  URAL 1209(郑大…

2012-12-05 17:11 351 查看
A - 1, 10, 100, 1000...
p

Time Limit: 1000MSMemory Limit: 16384K64bit IO Format: %I64d & %I64u
[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

inputoutput
4
3
14
7
6
0 0 1 0
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: