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.

Input

The first (and the only) line contains two integers n and s (1 ≤ n, s ≤ 1018).

Output

Print one integer — the quantity of really big numbers that are not greater than n.

Examples
input
12 1
output
3
input
25 20
output
0
input
10 9
output
1
Note

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).


  题目大意 设,定义,求满足的X有 多少个。

  随便举几个数10, 20, 30, 100,然后发现对应的函数值的分别为9, 18, 27和99猜测它满足"单调性"。

  现在来证明一下,当A > B时,

  

  当最高的不相同的位数为k,则A - B的最小值为$10^{k - 1}$,后面的各位数字之差最小为 -9(k - 1)(个位为第1位),显然这两个数的和大于等于0。

  所以就可以二分出第一个满足要求的数,然后算一算就好了。

Code

 #include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <stack>
#ifndef WIN32
#define Auto "%lld"
#else
#define Auto "%I64d"
#endif
using namespace std;
typedef bool boolean;
const signed int inf = (signed)((1u << ) - );
const signed long long llf = (signed long long)((1ull << ) - );
const double eps = 1e-;
const int binary_limit = ;
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
template<typename T>
inline boolean readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) {
ungetc(x, stdin);
return false;
}
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u * ) + x - '');
ungetc(x, stdin);
u *= aFlag;
return true;
} #define LL long long LL n, s; inline void init() {
readInteger(n);
readInteger(s);
} boolean check(LL x) {
LL y = x, bitsum = ;
while(y) bitsum += y % , y /= ;
return x - bitsum >= s;
} inline void solve() {
LL l = , r = n;
while(l <= r) {
LL mid = (l + r) >> ;
if(check(mid)) r = mid - ;
else l = mid + ;
}
printf(Auto"\n", n - r);
} int main() {
init();
solve();
return ;
}

Codeforces 817C Really Big Numbers - 二分法 - 数论的更多相关文章

  1. [codeforces 55]D. Beautiful numbers

    [codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...

  2. Really Big Numbers CodeForces - 817C (数学规律+二分)

    C. Really Big Numbers time limit per test 1 second memory limit per test 256 megabytes input standar ...

  3. Codeforces Round #382 Div. 2【数论】

    C. Tennis Championship(递推,斐波那契) 题意:n个人比赛,淘汰制,要求进行比赛双方的胜场数之差小于等于1.问冠军最多能打多少场比赛.题解:因为n太大,感觉是个构造.写写小数据, ...

  4. Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)

    Codeforces Beta Round #17 题目链接:点击我打开题目链接 大概题意: 给你 \(b\),\(n\),\(c\). 让你求:\((b)^{n-1}*(b-1)\%c\). \(2 ...

  5. CodeForces - 1245A Good ol' Numbers Coloring (思维)

    Codeforces Round #597 (Div. 2 Consider the set of all nonnegative integers: 0,1,2,-. Given two integ ...

  6. CodeForces 682A Alyona and Numbers (水题)

    Alyona and Numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/121333#problem/A Description After fi ...

  7. Codeforces 449D Jzzhu and Numbers

    http://codeforces.com/problemset/problem/449/D 题意:给n个数,求and起来最后为0的集合方案数有多少 思路:考虑容斥,ans=(-1)^k*num(k) ...

  8. HDU1058 Humble Numbers 【数论】

    Humble Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) T ...

  9. Codeforces 396B On Sum of Fractions 数论

    题目链接:Codeforces 396B On Sum of Fractions 题解来自:http://blog.csdn.net/keshuai19940722/article/details/2 ...

随机推荐

  1. nodejs+react使用webpack打包时控制台报错

    一.错误:Uncaught ReferenceError: process is not defined 解决方法: new webpack.DefinePlugin({ 'process.env': ...

  2. Web界面进行用户管理

    Web界面进行用户管理 添加用户           Tags:表示账号的角色 Admin:超级管理员 No access :表示没有可以访问的virtual host虚拟机(相当于数据库)     ...

  3. python中的IO操作

    python中的基本IO操作: 1) 键盘输入函数:raw_input(string),不作处理的显示,与返回. input(string),可以接受一个python表达式作为返回,python内部得 ...

  4. tensorflow tensor 索引

    问题: self.q_eval4next: (100,2) ix=[0,1,0,1---0,1](100,1) 我想取q_eval4next[:,idx] #use_doubleQ 切片用!!!! s ...

  5. Sql Server参数化查询之where in和like实现详解 [转]

    文章导读 拼SQL实现where in查询 使用CHARINDEX或like实现where in 参数化 使用exec动态执行SQl实现where in 参数化 为每一个参数生成一个参数实现where ...

  6. WebAppInitializer类,代替web.xml

    package com.ssm.yjblogs.config; import javax.servlet.MultipartConfigElement; import javax.servlet.Se ...

  7. STL容器之vector

    [1]模板类vector 模板类vector可理解为广义数组.广义数组,即与类型无关的数组,具有与数组相同的所有操作. 那么,你或许要问:既然C++语言本身已提供了一个序列式容器array,为什么还要 ...

  8. spring boot: ConfigurationProperties

    读取配置信息 1.5 之前 @Component @ConfigurationProperties(prefix = "user", locations= {"class ...

  9. django之路由分析

    URL配置(URLconf)就像Django所支撑网站的目录.它的本质是URL与要为该URL调用的视图函数之间的映射表. URLconf配置 基本格式: from django.conf.urls i ...

  10. Qt 之 pro 配置详解

    原文地址:https://blog.csdn.net/liang19890820/article/details/51774724 简述 使用Qt的时候,我们经常会对pro进行一系列繁琐的配置,为方便 ...