LINK

题意:给出两个数n, s,要求问1~n中\(x-bit(x)>=s\)的数有多少个。其中bit(x)指x的各位数之和

思路:首先观察能够发现,对于一个数如果满足了条件,由于x-bit(x)总是随x变大而变大的,所以只要求得最小的x能够满足条件,二分即可

/** @Date    : 2017-07-02 11:17:55
* @FileName: 817C 二分.cpp
* @Platform: Windows
* @Author : Lweleth (SoungEarlf@gmail.com)
* @Link : https://github.com/
* @Version : $Id$
*/
#include <bits/stdc++.h>
#define LL long long
#define PII pair
#define MP(x, y) make_pair((x),(y))
#define fi first
#define se second
#define PB(x) push_back((x))
#define MMG(x) memset((x), -1,sizeof(x))
#define MMF(x) memset((x),0,sizeof(x))
#define MMI(x) memset((x), INF, sizeof(x))
using namespace std; const int INF = 0x3f3f3f3f;
const int N = 1e5+20;
const double eps = 1e-8; LL n, s; LL trans(LL x)
{
LL res = 0;
while(x)
{
res += x % 10;
x /= 10;
}
return res;
}
int main()
{
while(cin >> n >> s)
{
LL l = 1, r = 1e18 + 20;//注意范围
if(s >= n)//注意s可能大于n
{
printf("0\n");
continue;
}
while(l < r)
{
LL mid = (l + r) >> 1;
if(mid - trans(mid) >= s)
r = mid;
else l = mid + 1; //cout << l << '~' << r << endl;
//cout << mid << "~" << trans(mid) << endl;
}
if(l > n)
printf("0\n");
else
printf("%lld\n", n - l + 1);
}
return 0;
}

817C. Really Big Numbers 二分的更多相关文章

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

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

  2. Codeforces 817C Really Big Numbers - 二分法 - 数论

    Ivan likes to learn different things about numbers, but he is especially interested in really big nu ...

  3. Codeforces 359D Pair of Numbers | 二分+ST表+gcd

    题面: 给一个序列,求最长的合法区间,合法被定义为这个序列的gcd=区间最小值 输出最长合法区间个数,r-l长度 接下来输出每个合法区间的左端点 题解: 由于区间gcd满足单调性,所以我们可以二分区间 ...

  4. Codeforces 817+818(A~C)

    (点击题目即可查看原题) 817A Treasure Hunt 题意:给出起点和终点,每次移动只能从 (a,b)移动至(a+x,b+y) , (a+x,b-y) , (a-x,b+y) , (a-x, ...

  5. AC自动机-算法详解

    What's Aho-Corasick automaton? 一种多模式串匹配算法,该算法在1975年产生于贝尔实验室,是著名的多模式匹配算法之一. 简单的说,KMP用来在一篇文章中匹配一个模式串:但 ...

  6. Binary Search

    Binary Search                              [原文见:http://www.topcoder.com/tc?module=Static&d1=tuto ...

  7. Python实例手册

    在电脑中突然发现一个这么好的资料,雪松大神制作,不敢独享,特与大家共享.连他的广告也一并复制了吧! python实例手册 #encoding:utf8 # 设定编码-支持中文 0说明 手册制作: 雪松 ...

  8. CF1036C Classy Numbers dfs+二分

    Classy Numbers time limit per test 3 seconds memory limit per test 256 megabytes input standard inpu ...

  9. UVa 10539 (筛素数、二分查找) Almost Prime Numbers

    题意: 求正整数L和U之间有多少个整数x满足形如x=pk 这种形式,其中p为素数,k>1 分析: 首先筛出1e6内的素数,枚举每个素数求出1e12内所有满足条件的数,然后排序. 对于L和U,二分 ...

随机推荐

  1. JAVA里面json和java对象之间的相互转换

    1. 把java 对象列表转换为json对象数组,并转为字符串 JSONArray array = JSONArray.fromObject(list);    String jsonstr = ar ...

  2. 一些有趣的erlang项目

    这里会收集一些erlang项目,有需可以转. erlang-bookmarks Scaling Erlang High Performance Erlang - Finding Bottlenecks ...

  3. Win10 版本情况 201810

  4. linux安装py3.6

    随手记录: https://www.python.org/ftp/python/3.6.8/Python-3.6.8rc1.tgz 所有linux版本: https://www.python.org/ ...

  5. xheditor在线编辑器在.netMVC4中的使用

    在线编辑器xheditor,测试感觉不错,特把使用方法记录如下 : 先看看基本使用方法,然后用实例来操作 1.xheditor 地址 http://xheditor.com/ 2.下载最新编辑器源码 ...

  6. 【ASP.NET Core 】ASP.NET Core 源码学习之 Logging[1]:Introduction

    在ASP.NET 4.X中,我们通常使用 log4net, NLog 等来记录日志,但是当我们引用的一些第三方类库使用不同的日志框架时,就比较混乱了.而在 ASP.Net Core 中内置了日志系统, ...

  7. appium手机操作

    1.按键操作 pressKeyCode(key, metastate) key为按键事件,metastate为辅助功能键 举例: pressKeyCode(AndroidKeyCode.HOME)   ...

  8. CSS预处理语言-less 的使用

    Less 是一门 CSS 预处理语言,它扩展了 CSS 语言,增加了变量.Mixin.函数等特性,使 CSS 更易维护和扩展. Less 可以运行在 Node 或浏览器端. Less的编译处理 作为一 ...

  9. BZOJ4919 大根堆(动态规划+treap+启发式合并)

    一个显然的dp是设f[i][j]为i子树内权值<=j时的答案,则f[i][j]=Σf[son][j],f[i][a[i]]++,f[i][a[i]+1~n]对其取max.这样是可以线段树合并的, ...

  10. Debugging QML Applications

    Debugging QML Applications Console API Log console.log, console.debug, console.info, console.warn an ...