poj 1019 Number Sequence && hdu 1597 find the nth digit(二分,模拟)
来源:互联网 发布:火星时代客观评价知乎 编辑:程序博客网 时间:2024/06/06 12:50
hdu1597 地址:点击打开链接
poj 地址:点击打开链接
两个题很相似,都是问你第i位是几,但排列的顺序有点不太一样.
hdu 1597的每个小串是前一个小串后加个1-9中的一个循环添加;
poj 1019的每个小串是前一个小串后加(i+1)
思路都一样,想办法记录截止到某个小串其前缀一共有几位,这样就可以二分快速找出第i
位在哪个小串,知道小串后,hdu在小串中因为都是1-9循环所以模9就是答案(注意
9%9==0); poj在小串中又是12345678910111213....所以还要二分出具体在哪个数字,知
道在哪个数字后再对这个数字拆解就能找到那位数。
hdu 1597:
#include<iostream>#include<algorithm>#include<cstdio>using namespace std;typedef long long ll;const int maxn = 1<<19;const ll INF = 0x7fffffff;ll a[maxn], k;void init(){ for(k = 1; a[k-1]+k <= INF; k++) a[k] = a[k-1]+k;}int main(void){ int t; init(); cin >> t; while(t--) { ll n; scanf("%lld", &n); ll l = 1, r = k-1, ans; ans = lower_bound(a, a+k, n)-a; if(n == a[ans]) printf("%d\n", ans%9 == 0 ? 9 : ans%9); else printf("%d\n", (n-a[ans-1])%9 == 0 ? 9 : (n-a[ans-1])%9); } return 0;}
poj 1019:
#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>using namespace std;typedef long long ll;const int maxn = 1e6+5;const int INF = 0x7fffffff;ll group[maxn], sum[maxn];int main(void){ int i, t; cin >> t; for(i = 1; ; i++) { group[i] = group[i-1]+1+(i/10>0)+(i/100>0)+(i/1000>0)+(i/10000>0)+(i/100000>0); sum[i] = sum[i-1]+group[i]; if(sum[i] > INF) break; } while(t--) { ll n; scanf("%lld", &n); ll ans = lower_bound(sum, sum+i, n)-sum; n = n-sum[ans-1]; ans = lower_bound(group, group+i, n)-group; n -= group[ans-1]; int len = 0, tmp[15]; while(ans) { tmp[len++] = ans%10; ans /= 10; } printf("%d\n", tmp[len-n]); } return 0;}
0 0
- poj 1019 Number Sequence && 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 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
- 读书笔记 effective c++ Item 15 在资源管理类中提供对原生(raw)资源的访问
- eclipse如何重新编译项目
- GraphicsLab Project之Screen Space Ambient Occlusion(SSAO)
- PHPword解决中文乱码
- java__给定一个数组和一个数(该数不一定在数组中),从数组里删掉这个数字,返回剩下的数组长度。
- poj 1019 Number Sequence && hdu 1597 find the nth digit(二分,模拟)
- Codeforces Round #402 (Div. 2)
- c++第一次上机实验:求分段函数值
- 轮胎打气的正确方法
- String类的拷贝(浅拷贝,深拷贝,写时拷贝)
- 十八哥视频教程中html和css的tips
- 流程控制
- R中的数据抽样SMOTE (谢佳标老师讲课笔记)
- hdu1086 You can Solve a Geometry Problem too【排斥实验+跨立实验】