【链接】 我是链接,点我呀:)

【题意】

让你求出只由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());
}
}
}

最新文章

  1. 【ASP.NET】VS编译成功后自动生成Nuget包
  2. BZOJ1097: [POI2007]旅游景点atr
  3. 用php获取本周,上周,本月,上月,本季度日期的代码
  4. vs2013中的“任务列表”菜单
  5. lua中string.find()函数作用于汉字字符串
  6. 浏览器js console对象
  7. Sphinx学习之sphinx的安装篇
  8. MySQL 全文搜索支持
  9. 静态Web开发 DOM
  10. Android实用代码块
  11. python多进程,以及进程池并发
  12. 关于DreamWeaver CS6.0 + PhoneGap 之移动开发环境搭建
  13. AnsiString和String的区别、使用
  14. iOS - Bluetooth 蓝牙
  15. SpringMVC 教程 - Handler Method
  16. 11 OptionsMenu 菜单
  17. UPUPW配置
  18. (转)Web.config配置文件详解
  19. 3.复杂的viewpager
  20. node基础 npm、module、exports、require

热门文章

  1. bootstrap的modal弹窗,在多层窗口关闭时只会关闭自窗口,不再关闭父窗口
  2. poj 3613 Cow Relays【矩阵快速幂+Floyd】
  3. python实现对excel数据进行修改/添加
  4. 基于Hexo且在GitHub上搭建博客
  5. linux Java环境变了配置
  6. [C和指针] 4-语句、5-操作符和表达式
  7. [Usaco2013 Nov]No Change
  8. Redis基础---链接管理
  9. 设置Echarts鼠标悬浮样式
  10. Objective-C设计模式——外观Faced(接口适配)