HDU-1597 find the nth digit【二分法】
来源:互联网 发布:亚投行 知乎 编辑:程序博客网 时间:2024/06/06 00:58
find the nth digit
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 13828 Accepted Submission(s): 4249
Problem Description
假设:
S1 = 1
S2 = 12
S3 = 123
S4 = 1234
.........
S9 = 123456789
S10 = 1234567891
S11 = 12345678912
............
S18 = 123456789123456789
..................
现在我们把所有的串连接起来
S = 1121231234.......123456789123456789112345678912.........
那么你能告诉我在S串中的第N个数字是多少吗?
S1 = 1
S2 = 12
S3 = 123
S4 = 1234
.........
S9 = 123456789
S10 = 1234567891
S11 = 12345678912
............
S18 = 123456789123456789
..................
现在我们把所有的串连接起来
S = 1121231234.......123456789123456789112345678912.........
那么你能告诉我在S串中的第N个数字是多少吗?
Input
输入首先是一个数字K,代表有K次询问。
接下来的K行每行有一个整数N(1 <= N < 2^31)。
接下来的K行每行有一个整数N(1 <= N < 2^31)。
Output
对于每个N,输出S中第N个对应的数字.
Sample Input
61234510
Sample Output
112124
二分查找的经典应用,
题目很容易理解,给定一个字符串,是有1-9字符组成,在里面1就是1;2就是12;3就是123;10就是1234567891。依次连接。
给定一个位数需要去确定这个位数上的数到底是几?
本题看似一个字符串题目,但是因为数据量太大,却模拟确定到底是几,很不现实。所以需要确定这个数是由数值几来表示出来的。然后找到是里面的第几位,再对9取余,确定到底是几。
这就需要一个数一个数的记录下这个数前面所有数值总共占多少位。用2分查找,找到n所在的位置。也就是找到n所在数前面的那个数的位置,用n 减去,也就是自己在当前这个数的第多少位,对9取余即可。难度就是想到用二分查找所在位置进行计算。
在二分算法中,细节在于我们最后的处理方式是用n-保存位数的数组数值,这就需要我们得到在n值所对应的偏左一位的存数数组的值,所以对于个别等号的处理如下
#include<bits/stdc++.h>using namespace std;long long cs[1000000];long long n;long long w;void ef(int l,int r){ int m; m=(r+l)/2; if(cs[m]<n&&cs[m+1]>=n) { w=m; return; } if(cs[m]>=n) ef(l,m-1); if(cs[m+1]<n) ef(m+1,r);}int main(){ int t; int q; for(int i=1;i<=65540;i++) { cs[i]=cs[i-1]+i; } cin>>t; while(t--) { cin>>n; w=0; ef(1,65536); q=n-cs[w]; q=q%9; if(q==0) q=9; cout<<q<<endl; } return 0;}
阅读全文
0 0
- HDU-1597 find the nth digit【二分法】
- hdu Problem - 1597 find the nth digit
- HDU 1597 find the nth digit
- hdu 1597 find the nth digit
- Hdu 1597 find the nth digit
- hdu 1597 find the nth digit
- hdu 1597 find the nth digit
- HDU 1597 find the nth digit
- HDU 1597:find the nth digit
- hdu 1597 find the nth digit
- hdu find the nth digit(1597)
- hdu 1597 find the nth digit
- hdu 1597 数学题 find the nth digit
- HDU 1597 find the nth digit
- HDU 1597 find the nth digit
- HDU 1597 find the nth digit 水题
- HDU 1597:find the nth digit【规律】
- HDU 1597 find the nth digit
- MVP框架使用Fretiofit获取网络数据使用xrecycleview实现上拉加载下拉刷新
- es6学习第1天,箭头函数 块级作用域
- 【bzoj 1036】树的统计(树链剖分)
- selenium模块
- 引水入城
- HDU-1597 find the nth digit【二分法】
- 使用div的float样式时,div的parent 的height属性是0
- c#中的自定义属性
- 创建登录界面
- python基础系列教程——python所有包库的下载
- hdfs写入无权限解决方法
- Sublime Text3使用总章
- ftp命令的使用
- 微商怎么做?看看月入破万的老司机们是如何开车的