1209. 1, 10, 100, 1000...
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
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 | output |
---|---|
4 3 14 7 6 | 0 0 1 0 |
题意:给定n个数ki,问1101001000......的第ki个位置的数字是0还是1.
解析:利用规律可推出第i个‘1’的位置编号为 i * (i - 1) / 2 + 1,利用二分判断询问的位置是否为1即可。
AC代码:
#include <cstdio>
int check(long long x){
long long r = 1LL<<31, l = 0, mid, y;
while(l <= r){ //二分判断是否为1
mid = l + (r - l)/2;
y = mid*(mid-1)/2 + 1;
if(y == x) return 1;
else if(y < x) l = mid + 1;
else r = mid - 1;
}
return l <= r;
}
int main(){
#ifdef sxk
freopen("in.txt", "r", stdin);
#endif //sxk
int n;
long long c;
scanf("%d", &n);
for(int i=0; i<n; i++){
scanf("%lld", &c);
printf("%d%c", check(c), i < n ? ' ' : '\n');
}
return 0;
}