ZZU PRACTICE 4  URAL 1209(郑大…


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 ≤ 2 31 − 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的公式就不用说了吧,最后找到的值一定离确切的值很近(二分学的不好,所以我开的范围大了ZZU <wbr>PRACTICE <wbr>4 <wbr> <wbr>URAL <wbr>1209(郑大友情参赛)),注意,算值的时候一定要把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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值