hdu 1597 find the nth digit(等差求和+二分)
来源:互联网 发布:《深入理解linux内核》 编辑:程序博客网 时间:2024/04/29 22:52
find the nth digit
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9045 Accepted Submission(s): 2586
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
Author
8600
题目分析:
可以得知第n个串长度为n,所以总长度为n(n+1)/2,那么我们找到完整的串,然后减掉,只剩下下一个不完整的,对9取模,就能得到要求的数字
题目分析:
可以得知第n个串长度为n,所以总长度为n(n+1)/2,那么我们找到完整的串,然后减掉,只剩下下一个不完整的,对9取模,就能得到要求的数字
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>using namespace std;typedef long long LL;int t;LL n;int mod[11] = {9,1,2,3,4,5,6,7,8};LL f ( LL x ){ return x*(x+1)/2;}LL search ( LL n ){ LL left = 1 , right = n , mid; while ( left != right ) { mid = (left + right+1) >> 1; if ( f(mid) > n ) right = mid-1; else left = mid; } return left;}int main ( ){ scanf ( "%d" , &t ); while ( t-- ) { scanf ( "%lld" , &n ); LL x = search ( n ); if ( f(x) == n ) printf ( "%d\n" , mod[x%9] ); else { x = n - f(x); printf ( "%d\n" , mod[x%9] ); } }}
0 0
- hdu 1597 find the nth digit(等差求和+二分)
- hdu 1597 find the nth digit 二分查找
- 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 水题
- 第四周上机实践项目2-分数类的雏形
- 指向学生类的指针
- SpringMVC 之类型转换 Converter
- 第四周 项目3-用对象数组操作长方柱类
- 项目一之三角形类的构造函数(2)
- hdu 1597 find the nth digit(等差求和+二分)
- JAVA学习笔记(四十六)- 内部类详解
- Java程序员不得不学习的SQL(以MySQL为例)(1)
- 第四周项目二分数类的雏形
- 第三周项目一带参数的构造函数
- iOS笔记6
- Log图文详解(Log.v,Log.d,Log.i,Log.w,Log.e)的用法
- 第四周项目一
- Median of Two Sorted Arrays--LeetCode