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】UILabel 常用属性设置
UILabel 的一些常用属性,示例代码如下: // 字体大小 label.font = [UIFont systemFontOfSize:14.0]; label.font = [UIFont fo ...
- 用mongodb 固定集合实现只保留固定数量的记录,自动淘汰老旧数据
在一个保存report记录的场景中,我们使用MongoDB进行数据存储 example: db: report Collection: daily_report 创建db: use report; ...
- Python学习系列(三)Python 入门语法规则1
一.注释 ''' 多行注释 ''' #单行注释 ''' #example1.1 测试程序 时间:4/17/2017 i1=input("请输入用户名:") i2=input ...
- 聊聊目标管理之 OKR
这篇文章我们不谈技术,聊点轻松的,那聊什么呢?聊一下最近很火的目标管理 OKR.不知道小伙伴你们的公司什么情况,我的公司今年开始推行 OKR,用了大半年的时间,感觉效果还不错,上周六又参加了一天的复盘 ...
- java并发编程(二十)----(JUC集合)CopyOnWriteArrayList介绍
这一节开始我们正式来介绍JUC集合类.我们按照List.Set.Map.Queue的顺序来进行介绍.这一节我们来看一下CopyOnWriteArrayList. CopyOnWriteArrayLis ...
- 分享我的GD32F450的IAP过程
最近一个项目使用GD32F450VI+ESP8266需要做远程升级,基本参考正点原子IAP的那一章节,但是在GD32F450上却遇到了问题,无法跳转,然后使用正点原子的开发板stm32f429,以及s ...
- React Native-路由跳转
搭建完RN开发环境后(搭建方式可查看https://www.cnblogs.com/luoyihao/p/11178377.html),要实现多个页面之间的跳转. 1.这时需要安装react-navi ...
- map的实现和柯里化(Currying)
版权申明:本文为博主窗户(Colin Cai)原创,欢迎转帖.如要转贴,必须注明原文网址 http://www.cnblogs.com/Colin-Cai/p/11329874.html 作者:窗户 ...
- ABAP 查看采购订单行项目已开票金额和已清金额
FUNCTION zmm_fm_po_invence. *"----------------------------------------------------------------- ...
- 三步理解--门控循环单元(GRU),TensorFlow实现
1. 什么是GRU 在循环神经⽹络中的梯度计算⽅法中,我们发现,当时间步数较⼤或者时间步较小时,循环神经⽹络的梯度较容易出现衰减或爆炸.虽然裁剪梯度可以应对梯度爆炸,但⽆法解决梯度衰减的问题.通常由于 ...