Olympiad
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2015 Accepted Submission(s): 1262
Problem 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). Please be fast to get the gold medal!
Input
The first line of the input is a single integer T (T≤1000), 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 1≤a≤b≤100000.
Output
For each testcase, print one line indicating the answer.
Sample Input
2
1 10
1 1000
Sample Output
10
738
Author
XJZX
Source
2015 Multi-University Training Contest 4
Recommend
wange2014 | We have carefully selected several similar problems for you: 6253 6252 6251 6250 6249
题解: 求一个区间内的 具有(数字的每一位都不同)的这个特性的数字的个数
开出数组后,用judge() 函数判断是否符合要求,符合返回true,否则为false,在初始化的时候记录sum[i] (sum[i]代表i前符合特性的个数),有点类似打表的性质,然后就可以离线查询
一维前缀和:
这个优化 主要是用来 在O(1) 时间内求出一个序列中,a[i] +a[i-1]+……+a[j] 的和
具体原理: 用sum[i] 表示 (a[1]+a[2]+a[3]+……+a[i])其中sum[0]=0 ,则 (a[i]+a[i+1]+………+a[j]) 即等于sum[j] -sum[i-1]
#include<bits/stdc++.h>
using namespace std;
const int maxn=100000;
int sum[maxn],T,ql,qr,rs;
bool vis[10];
bool judge(int num)
{
memset(vis,false,sizeof(vis));
while(num)
{
if(vis[num%10]) {return false;}
vis[num%10]=true;
num/=10;
}
return true;
}
void init()
{
for(int i=0;i<maxn;i++)
{
sum[i]=judge(i)+sum[i-1];
}
}
int query(int n1,int n2)
{
return sum[n2]-sum[n1-1];
}
int main()
{
init();
cin>>T;
while(T--)
{
cin>>ql>>qr;
rs=query(ql,qr);
cout<<rs<<endl;
}
return 0;
}