Ural 1209. 1, 10, 100, 1000... 一道有趣的题

2023-05-24,,

1209. 1, 10, 100, 1000...

Time limit: 1.0 second
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

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}$是不是整数就好了。
参考代码

 #include<stdio.h>
#include<math.h> int main()
{
int n;
double k, d;
scanf("%d", &n);
while(n--) {
scanf("%lf", &k);
k = *k-; d = sqrt(k);
printf("%d%c", d==floor(d), n?' ':'\n');
}
return ;
}

本文原创by BlackStorm,转载请注明出处

本文地址:http://www.cnblogs.com/BlackStorm/p/5320833.html

Ural 1209. 1, 10, 100, 1000... 一道有趣的题的相关教程结束。

《Ural 1209. 1, 10, 100, 1000... 一道有趣的题.doc》

下载本文的Word格式文档,以方便收藏与打印。