HDU 5327 Olympiad

分析

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

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

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

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

思路

如何判断beatiful number

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

代码

#include <cstdio>
#define MAX_N 100005
int 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值