Really Big Numbers CodeForces - 817C (数学规律+二分)
1 second
256 megabytes
standard input
standard output
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).
中文题意:
给你一个数N和k,让你找出小于等于N的数x的数量,x满足这样的条件:
x减去x的每一位的数字sum和的结果大于等于k,我们设这个过程叫F(x),即F(x)= x - sumdig(x)
思路:
先手写一部分数字看下他们的结果。
1-1=0
2-2=0
10-1=9
11-2=9
12-3=9
13-4=9
14-5=9
15-6=9
16-7=9
17-8=9
18-9=9
19-10=9
20-2=18
21-3=18
29-11=18
30-3=27
80-8=72
90-9=81
+9
99-18=81
100-1=99
110-2=108
120-3=117
发现没有明确的直接的数学公式可以得出满足条件最小的数num,
我们只所以找num,是因为得出num可以直接根据num和N的关系来算出结果,
num就是最小的满足条件的那个数。
但是通过上面的一些数字可以发现,F(x)的值是随着x单调递增的。
SPEAKING OF 单调递增,显然我们可以想到二分,
即二分出那个num,然后直接得出答案。
具体细节见我的code:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define db(x) cout<<"== [ "<<x<<" ] =="<<endl;
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
ll n;
ll k;
ll check(ll x)
{
ll cnt=0ll;
ll f=x;
while(x)
{
cnt+=(x%);
x/=;
}
return f-cnt;
}
int main()
{
cin>>n>>k;
ll l=0ll;
ll r=n;
ll mid;
ll num=1e18;
num++;
while(l<=r)
{
mid=(l+r)/2ll;
if(check(mid)>=k)
{
num=mid;
r=mid-;
}else
{
l=mid+;
}
}
// db(num);
ll ans=max(0ll,n-num+);
cout<<ans<<endl;
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}
Really Big Numbers CodeForces - 817C (数学规律+二分)的更多相关文章
- Codeforces 715A & 716C Plus and Square Root【数学规律】 (Codeforces Round #372 (Div. 2))
		
C. Plus and Square Root time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
 - PAT甲级——1104 Sum of Number Segments  (数学规律、自动转型)
		
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90486252 1104 Sum of Number Segmen ...
 - [wx]自然数学规律
		
有趣的数学规律 椭圆 双曲线 抛物线都叫圆锥曲线 它们跟圆锥有着怎样的关系? 他们都是圆锥与平面在不同姿势下交配的产物. 参考 椭圆 抛物线 小结 e: 离线率 P: 任意一点 F: 焦点 准线: 一 ...
 - 【BZOJ2876】【NOI2012】骑行川藏(数学,二分答案)
		
[BZOJ2876][NOI2012]骑行川藏(数学,二分答案) 题面 BZOJ 题解 我们有一个很有趣的思路. 首先我们给每条边随意的赋一个初值. 当然了,这个初值不会比这条边的风速小. 那么,我们 ...
 - hihoCoder 1584 Bounce 【数学规律】 (ACM-ICPC国际大学生程序设计竞赛北京赛区(2017)网络赛)
		
#1584 : Bounce 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 For Argo, it is very interesting watching a cir ...
 - Magic Numbers CodeForces - 628D
		
Magic Numbers CodeForces - 628D dp函数中:pos表示当前处理到从前向后的第i位(从1开始编号),remain表示处理到当前位为止共产生了除以m的余数remain. 不 ...
 - [Codeforces 1199C]MP3(离散化+二分答案)
		
[Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...
 - Codeforces 626C  Block Towers「贪心」「二分」「数学规律」
		
题意: 一堆人用方块盖塔,有n个人每次只能加两块方块,有m个人每次只能加三块方块.要求每个人盖的塔的高度都不一样,保证所用方块数最少,求最高的塔的高度. 0<=n+m 0<=n,m< ...
 - Codeforces  817C Really Big Numbers - 二分法 - 数论
		
Ivan likes to learn different things about numbers, but he is especially interested in really big nu ...
 
随机推荐
- 洗礼灵魂,修炼python(81)--全栈项目实战篇(9)—— 购物商城登录验证系统
			
都在线购物过吧?那么你应该体验过,当没有登录账户时,点开购物车,个人中心,收藏物品等的操作时,都会直接跳转到登录账户的界面,然后如果登录一次后就不用再登录,直到用户登出. 是的,本次项目就是做一个登录 ...
 - 确认是否是因为做了物理I/O而导致的性能不佳
			
要获取语句是否进行了I/O,需要打开set statistics on 和set statistics on.
 - Python对文件的解压和压缩
			
zipfile: 解压: import os, zipfile serverzip_path = 'D:\\server.zip' serverzip_target_path = 'd:\\2' f ...
 - Python:decorator  [转]
			
Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现了say_hello()和say_goodbye()两个函数. def sa ...
 - python——函数之生成器
			
1 生成器函数的含义 生成器是一个返回可以迭代对象的函数,它是一个特殊的迭代器,但迭代器的抽象层级更高且比较复杂需要实现很多方法.相较迭代器而言,生成器简单使用. 2 生成器的创建方式 2.1 ...
 - LeetCode算法题-Nim Game(Java实现)
			
这是悦乐书的第203次更新,第213篇原创 01 看题和准备 你和你的朋友正在玩下面的Nim游戏:桌子上有一堆石头,每次你轮流去除1到3块石头. 移除最后一块石头的人将成为赢家. 你是第一个取出石块的 ...
 - Servlet(二):初识Servlet
			
在手动写完一个Servlet小例子后,是不是有很多疑问,接下来会为大家详细介绍Servlet的知识. 1.什么是Servlet 是在服务器上运行的小程序.一个servlet就是一个Java类,并且可以 ...
 - 平方根的C语言实现(一) —— 浮点数的存储
			
版权申明:本文为博主窗户(Colin Cai)原创,欢迎转帖.如要转贴,必须注明原文网址 http://www.cnblogs.com/Colin-Cai/p/7203254.html 作者:窗户 Q ...
 - JavaScript在网页中使用以及注意事项
			
<script>标签的解析<script>xxx</script>这组标签,是用于在 html 页面中插入 js 的主要方法.它主要有以下几个属性:charset: ...
 - 5255 -- 【FJOI2016】神秘数
			
5255 -- [FJOI2016]神秘数 Description 一个可重复数字集合\(S\) 的神秘数定义为最小的不能被 \(S\) 的子集的和表示的正整数.例如: \(S = {1,1,1,4, ...