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

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

inputoutput
4
3
14
7
6
0 0 1 0
Problem Author: Alexey Lakhtin
Problem Source: USU Open Collegiate Programming Contest October'2002 Junior Session
 
 
 
题目大意
  有一个奇怪的排列:$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 }

 

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

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

转载于:https://www.cnblogs.com/BlackStorm/p/5320833.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值