Ural 1209. 1, 10, 100, 1000... 一道有趣的题
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 ≤ 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 | output |
---|---|
4 3 14 7 6 |
0 0 1 0 |
Problem Author: Alexey Lakhtin
Problem Source: USU Open Collegiate Programming Contest October'2002 Junior Session
Problem Source: USU Open Collegiate Programming Contest October'2002 Junior Session
Tags: problem for beginners
题目大意:
有一个奇怪的排列:$110100100010000\cdots$这样。问这个序列第$K$位是$0$还是$1$。
简单思路:
(听说有人拿二分来做?听说有人直接$O(N)$?咦?这不是简单算术题吗!)(逃)
仔细观察发现$1$的出现序号是$1,2,4,7,11,16,22\cdots$这样。你以为我接下来要说找规律吗?naive。
再仔细观察发现就是$x*(x+1)/2+1=K(x\geq0)$,这还能忍?化简移项得$x^2+x+2-2K=0$,$b^2-4ac=8K-7$,求根公式$\frac{-1\pm\sqrt{8K-7}}{2}$,那么,是要算一下求根公式的值看是不是整数吗?我们发现$8K-7$是一个奇数!它若能开平方出一个整数,那也是一个奇整数!因此只需要判断$\sqrt{8K-7}$是不是整数就好了。
参考代码:
1 #include<stdio.h> 2 #include<math.h> 3 4 int main() 5 { 6 int n; 7 double k, d; 8 scanf("%d", &n); 9 while(n--) { 10 scanf("%lf", &k); 11 k = 8*k-7; d = sqrt(k); 12 printf("%d%c", d==floor(d), n?' ':'\n'); 13 } 14 return 0; 15 }