HDU 5327 Olympiad

来源:互联网 发布:个人怎样做网络销售 编辑:程序博客网 时间:2024/05/18 07:15

分析

所以和奥运会并没有什么关系

beatiful number被定义为一个各位数字均不重复的数字。给定一闭区间,问该区间有多少beatiful number

因为本题的区间长度是不固定的,所以考虑规律是比较不合理的。

那么需要确实地计算该区间的每一个数,为了防止RE,可以采取预处理,处理输入前先处理好输入范围内的数据,即到给定这位之前有多少个beatiful number(递推关系)。这样,得到输入数据时可以直接打表

思路

如何判断beatiful number

给定一数字,需要将其逐位提取,这里可以从个位数开始提取,方法是%10,将值设立一个数组做标记,如果该值存在标记说明出现过了那么不是beatiful number。之后取下一位则/10%10。需要注意的是跳出循环的条件,考虑一位数可以采取do {…} while(…);或预先把一位数直接写好,从两位数开始。

代码

#include <cstdio>#define MAX_N 100005int f[MAX_N];int is_beautiful(int i){    int a[10] = {0};    do {        if (a[i%10]) return 0;        else a[i%10] = 1;    } while (i/=10);    return 1;}int main(){    for (int i = 1; i < MAX_N; i++)        f[i] = f[i-1] + is_beautiful(i);    int T, a, b;    scanf("%d", &T);    while (T--) {        scanf("%d%d", &a, &b);        printf("%d\n", f[b]-f[a-1]);    }    return 0;}

题目

Description

You are one of the competitors of the Olympiad in numbers. The problem of this year relates to beatiful numbers. One integer is called beautiful if and only if all of its digitals are different (i.e. 12345 is beautiful, 11 is not beautiful and 100 is not beautiful). Every time you are asked to count how many beautiful numbers there are in the interval [a,b] (ab). Please be fast to get the gold medal!

Input

The first line of the input is a single integer T (T1000), indicating the number of testcases.

For each test case, there are two numbers a and b, as described in the statement. It is guaranteed that 1ab100000.

Output

For each testcase, print one line indicating the answer.

Sample Input

2
1 10
1 1000

Sample Output

10
738

0 0
原创粉丝点击