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 ...
随机推荐
- Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist
今天在用一键安装mysql的shell脚本安装mysql-5.1.73软件后发现mysql始终无法启动,多次执行后依旧报错,只能去查看error日志,发现了如下的2个错误: 错误一:Fatal err ...
- Java中的生产消费者问题
package day190109; import java.util.LinkedList; import java.util.Queue; import java.util.Random; pub ...
- 【audio】耳机插拔 线控按键识别流程【转】
耳机插拔/线控按键识别流程 耳机插拔/线控按键识别流程 1.文档概述 本文以msm8909平台,android N为例,介绍了通用情况下,耳机插拔的流程步骤,以及对耳机类型的识别逻辑.以方便在项目工作 ...
- 在Linux系统上利用Tomcat搭建测试环境
第一歩:查看Linux系统的IP地址. 输入命令:ifconfig 第二歩:WinSCP工具 1.下载WinSCP工具,便于文件直接从windows系统直接拖动到Linux系统中,图形化创建文件夹等. ...
- LeetCode算法题-Plus One(Java实现)
这是悦乐书的第156次更新,第158篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第15题(顺位题号是66).给定一个非空数字数组来表示一个非负整数,并给其加1.该数组已 ...
- Linux 小知识翻译 - 「服务器」
这次聊聊 「服务器」 这个词. 可能会觉得为什么「突然问这个?」.接下来请先考虑一下下面的题目. A) 「Web服务器是指提供网页数据的软件」 B) 「Web服务器是指运行上述软件的硬件」 那么,究竟 ...
- Java访问级别修饰符
用途 控制其他类可以访问的字段或方法 修饰符 public.protected.no modifier(未声明).private 访问级别 修饰符 当前类 包 子类 其他包 public √ √ √ ...
- mysql数据库显示 1164 table *** doesn't exist
问题出现场景: 以前mysql安装在C盘,后来重装系统,将mysql安装在了D盘,重装之前,将mysql的Data 文件夹备份了下来,mysql重新安装好之后,将原来的Data 文件夹内的数据库文件夹 ...
- 微信小程序跳转微信小程序新增配置项目 navigateToMiniProgramAppIdList
每个小程序可跳转的其他小程序数量限制为不超过 10 个 从 2.4.0 版本以及指定日期(具体待定)开始,开发者提交新版小程序代码时,如使用了跳转其他小程序功能,则需要在代码配置中声明将要跳转的小程序 ...
- [Java] SpringMVC工作原理之三:ViewResolver
一.ViewResolver 根据视图的名称将其解析为 View 类型的视图,如通过 ModelAndView 中的视图名称将其解析成 View,View 是用来渲染页面的,也就是将 Model 填入 ...