CF1036C Classy Numbers dfs+二分
3 seconds
256 megabytes
standard input
standard output
Let's call some positive integer classy if its decimal representation contains no more than 33 non-zero digits. For example, numbers 44, 200000200000, 1020310203 are classy and numbers 42314231, 102306102306, 72774200007277420000 are not.
You are given a segment [L;R][L;R]. Count the number of classy integers xx such that L≤x≤RL≤x≤R.
Each testcase contains several segments, for each of them you are required to solve the problem separately.
The first line contains a single integer TT (1≤T≤1041≤T≤104) — the number of segments in a testcase.
Each of the next TT lines contains two integers LiLi and RiRi (1≤Li≤Ri≤10181≤Li≤Ri≤1018).
Print TT lines — the ii-th line should contain the number of classy integers on a segment [Li;Ri][Li;Ri].
4
1 1000
1024 1024
65536 65536
999999 1000001
1000
1
0
2
题意:classy数:一个数的不为0的位数不超过三位,问你le到ri范围内有多少个classy数?
分析:考虑le和ri的范围为10^18,这之间满足条件的classy数不超过一百万(自己暴搜计算一下),将这些数存进数组里,排下序找出le的位置和ri的位置,相减就可以得出中间classy数的多少
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e5+10;
const ll mod = 2e9+7;
const double pi = acos(-1.0);
const double eps = 1e-8;
vector<ll> v;
void dfs( ll dep, ll res, ll n ) { //dep表示现在这个数是第几位,res表示现在这个数的大小,n表示res中不为0的位数
v.push_back(res);
if( dep == 18 ) {
return ;
}
dfs(dep+1,res*10,n);
if( n < 3 ) {
for( ll i = 1; i <= 9; i ++ ) {
dfs(dep+1,res*10+i,n+1);
}
}
}
int main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
ll T, le, ri;
for( ll i = 1; i <= 9; i ++ ) {
dfs(1,i,1);
}
v.push_back(1e18);
sort(v.begin(),v.end()); //排序数组
//debug(v.size());
cin >> T;
while( T -- ) {
cin >> le >> ri;
cout << upper_bound(v.begin(),v.end(),ri)-lower_bound(v.begin(),v.end(),le) << endl; //二分查找
}
return 0;
}
CF1036C Classy Numbers dfs+二分的更多相关文章
- [CF1036C]Classy Numbers
题目大意:多个询问,每个询问问$[l,r](1\leqslant l\leqslant r\leqslant10^{18})$内有多少个数满足非零数位小于等于$3$. 题解:数位$DP$,$f_{i, ...
- uva 10004 Bicoloring(dfs二分染色,和hdu 4751代码差不多)
Description In the ``Four Color Map Theorem" was proven with the assistance of a computer. This ...
- Educational Codeforces Round 50 (Rated for Div. 2) C. Classy Numbers
C. Classy Numbers 题目链接:https://codeforces.com/contest/1036/problem/C 题意: 给出n个询问,每个询问给出Li,Ri,问在这个闭区间中 ...
- Java实现 LeetCode 655 输出二叉树(DFS+二分)
655. 输出二叉树 在一个 m*n 的二维字符串数组中输出二叉树,并遵守以下规则: 行数 m 应当等于给定二叉树的高度. 列数 n 应当总是奇数. 根节点的值(以字符串格式给出)应当放在可放置的第一 ...
- Codeforces 1036C Classy Numbers 【DFS】
<题目链接> 题目大意: 对于那些各个位数上的非0数小于等于3的数,我们称为 classy number ,现在给你一个闭区间 [L,R] (1≤L≤R≤1018).,问你这个区间内有多 ...
- 10324 Global Warming dfs + 二分
时间限制:1000MS 内存限制:65535K提交次数:0 通过次数:0 题型: 编程题 语言: G++;GCC Description Global warming is a big prob ...
- Codeforces Round #299 (Div. 2)A B C 水 dfs 二分
A. Tavas and Nafas time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- hdu5676 ztr loves lucky numbers(dfs)
链接 ztrloveslucky numbers 题意 定义幸运数为:只存在4和7且4和7数量相等的数,给出n,求比>=n的最小幸运数 做法 暴力搜出所有长度从2-18的幸运数,因为最多9个4, ...
- hdu 5188 dfs+二分
get了很多新技能 当时想到了用dfs,但是排序用的是限制时间排序,一直没搞出来. 正解: 二分用时,dfs判断,为了顺利进行做题,需要按照做题开始时间排序 还可以用dp 题意: 作为史上最强的刷子之 ...
随机推荐
- 【Android Studio】Gradle DSL method not found:'android()'
如图所示: 参考:http://www.jianshu.com/p/d370d41fb7da 又遇到了这个问题: 参考:http://stackoverflow.com/questions/24204 ...
- solr使用心得
/** * @author zhipeng * @date 创建时间:2015-10-10 下午12:15:35 * @parameter * @return */ publ ...
- RocketMQ中Broker的消息存储源码分析
Broker和前面分析过的NameServer类似,需要在Pipeline责任链上通过NettyServerHandler来处理消息 [RocketMQ中NameServer的启动源码分析] 实际上就 ...
- containerd与kubernetes集成
kubernetes集群三步安装 概念介绍 cri (Container runtime interface) cri is a containerd plugin implementation of ...
- HTML5 Device Access (设备访问)
camera api (含图片预览) 参考地址 主要为利用input type=file, accept="image/*" 进行处理 图片预览方式(两种) const file ...
- CentOS7.x 搭建 GitLab 教程
今天闲来无事,想起之前买了一个阿里云 ECS,一直闲置着没用,一时兴起就想搭个自己的 GitLab 玩玩,GitLab 官网也提供了安装教程,很简单,照着步骤一步步基本没什么问题,可能安装的过程中有一 ...
- git:将代码提交到远程仓库(码云)
初始化 进入一个任意的文件夹(如D:\aqin_test1\) git init # 初始化,让git将这个文件夹管理起来 git add . # 收集此文件夹下的所有文件 git config -- ...
- LeetCode_62_不同路径
/** * @author jianw.li * @date 2019/1/22 11:11 PM * @Description: 不同路径 * 一个机器人位于一个 m x n 网格的左上角 (起始点 ...
- VMware安装Centos7虚拟机
首先安装虚拟机很简单,所以呢,具体的安装过程就引用别人的博客,这篇文字很详细,引用之后会在后面加上一些遇到的问题: 原文:https://blog.csdn.net/babyxue/article/d ...
- 微信小程序商城系统怎样搭建?
微信是一种非常便捷的生活方式,微信小程序一直深受企业和商家的青睐,如美团.京东.拼多多.唯品会.小红书等知名公司都推出了自己的小程序.对于网上商城小程序的开发似乎是一件非常难的事情,用什么开发?如何开 ...