URAL 1209. 1, 10, 100, 1000... (规律 + 二分)
来源:互联网 发布:linux搜狗的手写输入 编辑:程序博客网 时间:2024/05/18 19:36
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
431476
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;}
0 0
- URAL 1209. 1, 10, 100, 1000... (规律 + 二分)
- ural 1209. 1, 10, 100, 1000... 二分
- URAL 1224. Spiral (规律)
- ural 1260(找规律)
- URAL 1924 Four Imps (博弈论 + 规律)
- URAL 1502. Domino Dots (找规律)
- URAL 1180. Stone Game (博弈 + 规律)
- URAL 1225-Flags(规律递推)
- Ural 1542. Autocompletion(二分)
- timus 1209. 1, 10, 100, 1000... URAL 解题报告
- POJ 3484(规律+二分)
- Ural 1998 The old Padawan(二分)
- URAL 1196. History Exam (二分)
- URAL 2034 Caravans (最短路 + 二分)
- URAL 1106. Two Teams (二分图)
- URAL - 1117 Hierarchy(找规律)
- (beginner problems) ural 1209 1, 10, 100, 1000...
- ural 1613【hash+二分】
- 1016. Phone Bills (25) 模拟(就是很繁琐 尤其是计算费用)
- Ubuntu 14.04 64位下安装Chrome浏览器
- [leetcode]15 Compare Version Numbers
- 扎金花游戏 PHP 实现代码之大小比赛
- SIFT 尺度不变特征变换匹配算法详解
- URAL 1209. 1, 10, 100, 1000... (规律 + 二分)
- LoadRunner教程09:LR自动关联
- 使用grep快速定位代码位置
- 解决火狐浏览器iframe的focus和blur事件触发无效问题
- POJ 3041 Asteroids 最小点覆盖==最大二分匹配
- oracle asm alias 文件别名
- 并行计算MPI(一):MPI入门
- web压力测试中常见错误
- 基于c#的winform