http://poj.org/problem?id=2514

Ridiculous Addition
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1954   Accepted: 412

Description

Let us write down the infinite consecutive integers in a sequence in one line without any space and their squares in the second line. This will generate two different long numbers, now we want to find the sum of these two numbers. The calculation of the first 30 digits is just as below. 

The first digit of the result is 2, the second digit is 7, and the third is 2 and so on. Given an integer k, you should output the digit at position k in the resulting number. 

Input

The input file will contain several test cases. In each of the test cases, there is an integer k (0 < k <= 2 ^ 31 - 1) in one line.

A line containing a number "0" terminates input, and this line need not be processed.

Output

For each test case you should generate a line of output, which is the digit in the k-th place of the addition result.

Sample Input

2
5
30
0

Sample Output

7
1
8

Source

POJ Monthly--2005.07.31, Islamic Azad University of Mashhad – Collegiate Coding Challenge 1
 
    给出两个无限长的数 A=123456789101112131415......   和B=149162536496481100121144......   求A+B的第K位数是多少。
      由于是加法运算所以进位最多就是1,只要计算出A的第k位和B的第k位就差不多解决了,问题转化为求解A和B的第k位是多少。
    按照数位dp那种思想按位数分一下类就好了,对于同一位数的数做出的贡献很好计算,就是 len(num)*sum , A就分成[0,9,99,999,9999......],B的话就是[0,sqrt(10)-eps,sqrt(100)-eps,sqrt(1000)-eps......]
设置eps的必要性在于对于10000=100*100来说,我想得到的是99而不是100,所以减去一个eps来得到,就是因为这里计算错误导致一直WA= =
    做出位数贡献的前缀和然后xjb二分下就好了。
    

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
#define LL long long
vector<LL>g[];
LL a[],b[];
LL max_int=;
void init()
{
g[].push_back();
g[].push_back();
LL x=,y=;
for(int i=; i<=; ++i,x=x*+,y*=)
{
g[].push_back(x);
g[].push_back((LL)(sqrt(y)-0.00001));
}
LL o=;
for(int i=; i<=; ++i,o*=)
{
a[i]=a[i-]+i*(o-o/);
}
for(LL i=; i<=; ++i)
{
b[i]=b[i-]+i*(g[][i]-g[][i-]);
}
}
int getl(LL n)
{
int ans=;
while(n) ans++,n/=;
return ans;
}
int get_a(LL n)
{
LL l=,r=;
while(l<r)
{
LL mid=l+(r-l)/;
LL len=getl(mid);
LL s=a[len-]+(len*(mid-g[][len-]));
if(s>n)
{
r=mid;
}
else if(s<n)
{
l=mid+;
}
else
{
return mid%;
}
}
LL len=getl(l);
LL s=a[len-]+(len*(l-g[][len-]));
while(s>n) s--,l/=;
return l%;
} int get_b(LL n)
{
LL l=,r=;
while(l<r)
{
LL mid=l+(r-l)/;
LL len=getl(mid*mid);
LL s=b[len-]+(len*(mid-g[][len-]));
if(s>n)
{
r=mid;
}
else if(s<n)
{
l=mid+;
}
else
{
return mid*mid%;
}
}
LL len=getl(l*l);
LL s=b[len-]+(len*(l-g[][len-]));
l=l*l;
while(s>n) s--,l/=;
return l%;
} int main()
{
LL n;
init();
while(scanf("%lld",&n)!=EOF)
{
if(!n) break;
LL a=get_a(n),b=get_b(n),
c=get_a(n+),d=get_b(n+),jin=;
while(c+d>)
{
if(c+d>)
{
jin=;
break;
}
n++;
c=get_a(n+);
d=get_b(n+);
}
cout<<(a+b+jin)%<<endl;
}
return ;
}

最新文章

  1. [原创]使用Gradle的插件EnIDE build的时候,输出的中文总是乱码。
  2. mac系统mysql配置环境变量(转)
  3. i-doit
  4. 正确理解DTO、值对象和POCO
  5. Thinkphp中eq,neq,gt,lt等表达式缩写
  6. HttpRuntime.Cache的使用经验
  7. autolayout 总结
  8. iOS系统自带的 UIAlertView 自动旋转的实现
  9. iOS打包app发给测试人员测试
  10. JAVA的Date类与Calendar类(常用方法)
  11. C# 如何更改Word语言设置
  12. JavaScript进度条(datalist/repeater等多个进度条)
  13. 洛谷 P1856 【Picture】
  14. ida6.8 android so调试
  15. imageio.ffmpeg.download() has been deprecated. Use &#39;pip install im ageio-ffmpeg&#39; instead.&#39;
  16. iPhone投影
  17. 【python-字典】判断python字典中key是否存在的
  18. linq partition by
  19. django的serializers
  20. ETH智能合约测试

热门文章

  1. OpenCV学习一《Linux下安装OpenCV》
  2. IOS学习笔记一1
  3. RN 调用安卓的原生方法(实现Toast效果)
  4. go 接口以及对象的使用
  5. IIS上部署MVC网站,打开后ExtensionlessUrlHandler-4.0
  6. 一个请求需要调用两个不同的数据库 添加DbContext
  7. Grunt、Gulp区别 webpack、 requirejs区别
  8. 力扣(LeetCode)976. 三角形的最大周长
  9. zlib交叉编译
  10. Asp.net core 学习笔记 ( IIS, static file 性能优化 )