【HDU 5327】Olympiad(前缀和)

Olympiad


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] (a≤b)[a,b] (a≤b). Please be fast to get the gold medal!


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

For each test case, there are two numbers aa and bb, as described in the statement. It is guaranteed that 1≤a≤b≤1000001≤a≤b≤100000.
Output
For each testcase, print one line indicating the answer.


Sample Input
2
1 10
1 1000
Sample Output
10
738


题意:

求出a~b之间的beautiful数的个数!
beautiful数:每一位数字都不相同。

1≤a≤b≤100000。

思路:

数据范围很大,需要预处理用前缀和方法求出范围内(1~100000)每个数之前的beautiful数。
求所需范围a~b内的beautiful数只需要sum[b]-sum[a-1]即可。
【关于set】

代码示例:

#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
using namespace std;
#define MAX 100005
int sum[MAX];

bool check(int x)
{
    set<int> s;
    while(x)
    {
        int y=x%10;
        if(!s.count(y))
            s.insert(y);
        else return false;
        x/=10;    
    }
    return true;
}

int main()
{
    sum[0]=0;
    for(int i=1;i<MAX;i++)
        if(check(i)) sum[i]=1;
    for(int i=2;i<MAX;i++)
        sum[i]+=sum[i-1];

    int Case;
    int a,b;
    cin>>Case;
    while(Case--)
    {
        cin>>a>>b;
        cout<<sum[b]-sum[a-1]<<endl;
    }


    return 0;
 } 

猜你喜欢

转载自blog.csdn.net/chen_yuazzy/article/details/77074549