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 ;
}

poj-2514-模拟的更多相关文章

  1. POJ 1016 模拟字符串

    Numbers That Count Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20396   Accepted: 68 ...

  2. POJ 1208 模拟

    2017-08-28 15:07:16 writer:pprp 好开心,这道题本来在集训的时候做了很长很长时间,但是还是没有做出来,但是这次的话,只花了两个小时就做出来了 好开心,这次采用的是仔细分析 ...

  3. POJ - 3087 模拟 [kuangbin带你飞]专题一

    模拟洗牌的过程,合并两堆拍的方式:使先取s2,再取s1:分离成两堆的方式:下面C张放到s1,上面C张到s2.当前牌型与第一次相同时,说明不能搜索到答案. AC代码 #include<cstdio ...

  4. Shuffle'm Up POJ - 3087(模拟)

    Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15249   Accepted: 6962 Des ...

  5. poj 1379 模拟退火法

    /* 模拟退火法: 找到一些随机点,从这些点出发,随机的方向坐标向外搜索: 最后找到这些随机点的最大值: 坑://if(xx>-eps&&xx<x+eps&& ...

  6. POJ 1471 模拟?

    题意:求最大无坏点三角形 思路: 模拟? (为什么我模拟过了...) 有人用 DP,有人用 搜索... // by SiriusRen #include <cstdio> #include ...

  7. POJ 1951 模拟

    思路: 坑爹模拟毁我一生 给两组数据: 输入: YOURE TRAVELING THROUGH ANOTHER DIMENSION A DIMENSION NOT OF SIGHT. 输出: YR T ...

  8. POJ 2141 模拟

    思路:字符串解密 啥都告诉你了 模拟就好 //By SiriusRen #include <cstdio> #include <cstring> using namespace ...

  9. POJ 2459 模拟

    题意: 思路: 按照题意模拟即可 //By SiriusRen #include <cstdio> using namespace std; int c,f1,f2,d,xx,yy,vis ...

  10. poj 1068 模拟

    题目链接 大概题意就是告诉你有个n个小括号,每一个")"左边有多少个"("都告诉你了,然后让你求出每一对括号之间有多少对括号(包含自己本身). 思路: 我先计算 ...

随机推荐

  1. (转载)C#控件缩写规范

    标准控件缩写规范 类 型 前 缀 示 例 Adrotator adrt adrtTopAd BulletedList blst blstCity Button btn btnSubmit Calend ...

  2. Latex: 保持参考文献大小写

    参考: BibTeX loses capitals when creating .bbl file Latex: 保持参考文献大小写 在排版时,BibTeX会根据参考文献的格式将除了title中的第一 ...

  3. xdg-open命令智能打开各种格式的文件

    在linux中,通常用命令行打开文本文件,比如用命令gedit.more.cat.vim.less.但当需要打开其他格式文件时,比如pdf.jpg.mp3格式文件,咱们通常做法是进入到文件所在的目录, ...

  4. 网页中Div的打印

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  5. 在用node安装某个全局模块的时候,没有权限修改node_modules

    一.问题 今天在安装公司内部的一个npm模块的时候,发现报错了⬇ 第一行错误: deprecated graceful-fs@1.2.3: please upgrade to graceful-fs ...

  6. js中setTimeout和clearTimeout的使用

    setTimeout,延迟n秒后执行指定代码 clearTimeout,清除计时器 <html> <head> <script type="text/javas ...

  7. 自定义 Git - 配置 Git

    用git config配置 Git,要做的第一件事就是设置名字和邮箱地址: $ git config --global user.name "John Doe" $ git con ...

  8. leecode第一百三十六题(只出现一次的数字)

    class Solution { public: int singleNumber(vector<int>& nums) { int len=nums.size(); ; ;i&l ...

  9. Qt网络应用开发初步

      应用层的网络协议,如HTTP/FTP/SMTP等简称"应用协议",他们运行在TCP/UDP之上,从Qt5开始,已经不再分别提供QHttp类,QFtp类,应用层的编程使用QNet ...

  10. thinkphp中出现unserialize(): Error at offset 533 of 1857 bytes如何解决

    thinkphp中出现unserialize(): Error at offset 533 of 1857 bytes如何解决 一.总结 一句话总结:清缓存就好了,所以框架有问题可以考虑清缓存 清缓存 ...