【Codeforces 1036C】Classy Numbers
【链接】 我是链接,点我呀:)
【题意】
让你求出只由3个非0数字组成的数字在[li,ri]这个区间里面有多少个.
【题解】
只由3个非0数字组成的数字在1~10^18中只有60W个
dfs处理出来之后排序做个二分查找一下区间里有多少个就好。
【代码】
import java.io.*;
import java.util.*;
public class Main {
static InputReader in;
static PrintWriter out;
public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
}
static int N = 700000;
static class Task{
TreeSet<Long> myset = new TreeSet<Long>();
long a[] = new long[N+10];
int T,n;
long l,r;
void dfs(long cur,int num,int dep) {
if (dep==18) {
if (cur>0) myset.add(cur);
return;
}
if (num<3) {
for (int j = 1;j <= 9;j++)
dfs(cur*10+j,num+1,dep+1);
}
dfs(cur*10,num,dep+1);
}
int get_least_gore(long x) {
int l = 1,r = n,temp = 1;
while (l<=r) {
int mid = (l+r)/2;
if (a[mid]>=x) {
temp = mid;
r = mid - 1;
}else l = mid + 1;
}
return temp;
}
int get_last_lore(long x) {
int l = 1,r = n,temp = 1;
while (l<=r) {
int mid = (l+r)/2;
if (a[mid]<=x) {
temp = mid;
l = mid + 1;
}else r = mid - 1;
}
return temp;
}
public void solve(InputReader in,PrintWriter out) {
myset.add((long)1e18);
dfs(0,0,0);
n = myset.size();
Iterator<Long> it = myset.iterator();
int j = 1;
while (it.hasNext()) {
a[j] = it.next();
j++;
}
T = in.nextInt();
for (int ii = 1;ii <= T;ii++) {
l = in.nextLong();r = in.nextLong();
int idx1 = get_least_gore(l);
int idx2 = get_last_lore(r);
out.println(idx2-idx1+1);
}
}
}
static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer;
public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
}
public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
【Codeforces 1036C】Classy Numbers的更多相关文章
- 【Codeforces 300C】Beautiful Numbers
[链接] 我是链接,点我呀:) [题意] 让你找到长度为n的数字 这个数字只由a或者b组成 且这n个数码的和也是由a或者b组成的 求出满足这样要求的数字的个数 [题解] 枚举答案数字中b的个数为y,那 ...
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【66.47%】【codeforces 556B】Case of Fake Numbers
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 746E】Numbers Exchange
[题目链接]:http://codeforces.com/problemset/problem/746/E [题意] 你有n张卡片,上面写着不同的数字; 然后另外一个人有m张上面写着不同的数字的卡片: ...
- 【codeforces 367C】Sereja and the Arrangement of Numbers
[题目链接]:http://codeforces.com/problemset/problem/367/C [题意] 我们称一个数列a[N]美丽; 当且仅当,数列中出现的每一对数字都有相邻的. 给你n ...
- 【19.77%】【codeforces 570D】Tree Requests
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【35.37%】【codeforces 556C】Case of Matryoshkas
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【34.57%】【codeforces 557D】Vitaly and Cycle
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- 【55.70%】【codeforces 557A】Ilya and Diplomas
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
随机推荐
- springmvc 用fasterxml.jackson返回son数据
一,引入fasterxm.jackson包 <dependency> <groupId>com.fasterxml.jackson.core</groupId> & ...
- 字符类型C++(ascll码表)
ascll码: 序号 字符 序号 字符 序号 字符 序号 字符 序号 字符 序号 字符 32 空格 48 0 64 @ 80 P 96 ` 112 p 33 ! 49 1 65 A 81 Q 97 a ...
- [转]深入C语言内存区域分配(进程的各个段)详解
一般情况下,一个可执行二进制程序(更确切的说,在Linux操作系统下为一个进程单元,在UC/OSII中被称为任务)在存储(没有调入到内存运行)时拥有3个部分,分别是代码段(text).数据段(data ...
- Javascript对象基础讲解
1.Object对象详解 javascript 里最基本的数据类型是对象. avaScript里的对象其实是一个无序的属性集合,属性又是一个个的名-值对. 除了字符串,数字,true,false,nu ...
- 392 Is Subsequence 判断子序列
给定字符串 s 和 t ,判断 s 是否为 t 的子序列.你可以认为 s 和 t 中仅包含英文小写字母.字符串 t 可能会很长(长度 ~= 500,000),而 s 是个短字符串(长度 <=10 ...
- JOptionPane.showMessageDialog出现在浏览器下面的解决方法
将JOptionPane.showMessageDialog(null, result, "发布公告:", JOptionPane.INFORMATION_MESSAGE);中的参 ...
- LN : leetcode 207 Course Schedule
lc 207 Course Schedule 207 Course Schedule There are a total of n courses you have to take, labeled ...
- 【转载】testlink 1.8.5 安装错误的解决方法
TestLink所需环境为PHP+MYSQL (支持MS SQL等),系统推荐使用PHP5.2,安装成功以后,如果运行时出错,主要两种错: [1].HP Warning: strtotime(): I ...
- js技巧(三)
1.检测浏览器,search的用法 if(window.navigator.userAgent.search(/firefox/i)!=-1){ alert('ff'); } else if(wind ...
- 关于vue构建项目的一些指令
第一步: brew install nodejs(MAC机子下) Windows直接官网下载对应版本node.js 第二步: 获取nodejs模块安装目录访问权限(Windows系统忽略)sudo ...