codeforces817c Really Big Numbers
来源:互联网 发布:天翼飞young客户端mac 编辑:程序博客网 时间:2024/05/18 02:47
Ivan likes to learn different things about numbers, but he is especially interested in really big numbers. Ivan thinks that a positive integer number x is really big if the difference between x and the sum of its digits (in decimal representation) is not less than s. To prove that these numbers may have different special properties, he wants to know how rare (or not rare) they are — in fact, he needs to calculate the quantity of really big numbers that are not greater than n.
Ivan tried to do the calculations himself, but soon realized that it's too difficult for him. So he asked you to help him in calculations.
The first (and the only) line contains two integers n and s (1 ≤ n, s ≤ 1018).
Print one integer — the quantity of really big numbers that are not greater than n.
12 1
3
25 20
0
10 9
1
In the first example numbers 10, 11 and 12 are really big.
In the second example there are no really big numbers that are not greater than 25 (in fact, the first really big number is 30: 30 - 3 ≥ 20).
In the third example 10 is the only really big number (10 - 1 ≥ 9).
令f(x)为x-(x各位数之和)
可以看出f(x)是单调不减的
设y>x
若x 与 y只有个位不同 则f(x)==f(y)
反之f(x)>f(y)
若x各位数之和小于y 则必然成立
若x各位数之和等于y 因为x>y 所以成立
若x各位数之和大于y 则x至少有一位大于y对应的那一位
设只有一位
设其为右数第i位 则从最后一位-第i-1位f(y)-f(x)>0
又因为x各位数之和大于y所以到第i位f(x)-f(y)至少为(j+1)*10^(i-1)-j j为i位之后 y各位和-x各位和
显然成立
若有多位 则可把所有这样的位合并
同一位
所以可以二分答案(二分出合法的 再算出不合法的)
代码:
#include <iostream>#include <cstdio>#include <cstring>using namespace std;typedef long long LL;inline LL get(LL x){LL ans=0;while(x){ans+=x%10;x/=10;}return ans;}int main(){//freopen("a.in","r",stdin);//freopen("a.out","w",stdout);LL n,s;scanf("%I64d %I64d",&n,&s);LL l=0,r=n;while(l+1<r){LL mid=(l+r)>>1;if(mid-get(mid)<s)l=mid;else r=mid-1;}if(r-get(r)<s)printf("%I64d\n",n-r);else printf("%I64d\n",n-l);return 0;}
- codeforces817c Really Big Numbers
- 【二分】【codeforces817C】Really Big Numbers
- C. Really Big Numbers
- C. Really Big Numbers
- 补题补题补题~~~~really big numbers
- codeforces 817C Really Big Numbers 二分
- 【Codeforces 817C. Really Big Numbers】+ 二分
- 【Codeforces 817 C. Really Big Numbers】+ 构造
- codeforces 817C Really Big Numbers 二分
- codeforces 817C Really Big Numbers
- Codeforces 817 C. Really Big Numbers 二分
- CodeFroces 817C Really Big Numbers(二分)
- Educational Codeforces Round 23 C. Really Big Numbers
- Codeforces 817C Really Big Numbers【思维+二分】
- Educational Codeforces Round 23#C. Really Big Numbers
- Educational Codeforces Round 23 C. Really Big Numbers(二分 数学)
- Li Na really made a big upset,
- UVa10464 - Big Big Real Numbers
- C++设计模式一--StrategyPattern(策略模式)
- mysqldumpslow使用总结
- 全面解读Java NIO工作原理
- 79_游戏项目_动画的实现
- DIV 居中
- codeforces817c Really Big Numbers
- 机器学习会议总结1
- QT与Python混合编程经验记录
- 北大新方正,路在何方?
- nginx 配置高并发
- Jenkins Maven checkstyle findbugs pmd静态代码检查
- springboot分布式session同步
- COJ1229-压缩文件
- 学习淘淘商城第一百零六课(Mycat简单入门)