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 题意: 作为史上最强的刷子之 ...
随机推荐
- 【iOS】UIImageView 点击事件
UIImageView 并不像 UIButton 那样点击鼠标就可以关联点击事件,也不像 Android 里有 onClickListener,这个时候就需要借助 UITapGestureRecogn ...
- X-Admin&ABP框架开发-系统日志
网站正常运行中有时出现异常在所难免,查看系统运行日志分析问题并能够根据错误信息快速解决问题尤为重要,ABP对于系统运行日志这块已经做了很好的处理,默认采用的Log4Net已经足够满足开发过程中的需要了 ...
- spring boot 学习笔记之前言----环境搭建(如何用Eclipse配置Maven和Spring Boot)
本篇文档来源:https://blog.csdn.net/a565649077/article/details/81042742 1.1 Eclipse准备 (1) 服务器上安装JDK和Mav ...
- RocketMQ中Broker的启动源码分析(一)
在RocketMQ中,使用BrokerStartup作为启动类,相较于NameServer的启动,Broker作为RocketMQ的核心可复杂得多 [RocketMQ中NameServer的启动源码分 ...
- 分布式ID系列(2)——UUID适合做分布式ID吗
UUID的生成策略: UUID的方式能生成一串唯一随机32位长度数据,它是无序的一串数据,按照开放软件基金会(OSF)制定的标准计算,UUID的生成用到了以太网卡地址.纳秒级时间.芯片ID码和许多可能 ...
- 微信小程序云开发报错解决: Setting data field "openid" to undefined is invalid.
最近在学习微信小程序云开发,刚一开始就遇到了问题. 点击获取openid的时候控制台开始报错: [云函数] [login] user openid: undefined VM97:1 Setting ...
- Redis集群与spring的整合
上一篇详细的赘述了Redis的curd操作及集群的搭建.下面我们开始将他整合到我们实际的项目中去.我的项目采用的是标准的ssm框架,ssm框架这里不说,直接开始整合. 首先在maven管理中将我们的j ...
- 大陆争霸[SDOI2010]带限制最短路
只要你有无限个自爆机器人,你就能为所欲为 斯普林·布拉泽 [题目描述] 略 一句话题意: 杰森国有 \(N\) 个城市,由 \(M\) 条单向道 路连接.杰森国的首都是城市 \(N\).你只需摧毁杰森 ...
- 项目构建分析和 webpack 优化实践
加入新公司一个月,最近接手在做一个 chrom 浏览器插件的项目,开发过程中发现项目打包的时间很长,足足有30多秒,这是让人很难接受的,而且构建的显示了几条包体积过大的提示信息: 可以看到,打包后有三 ...
- .NET World——gPRC概览
什么是gRPC 官方的定义: gRPC is a modern open source high performance RPC framework that can run in any envir ...