HDU - 5327 Olympiad(一维前缀和)

 

Olympiad

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

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

思路:题目的要求就是求出[a,b]中所有满足该数中每个权位上的数都不相同的个数。我的思路就是先打表(感觉不打表会被TLE),在打表过程中将前i个数中满足题意的个数储存在sum数组中。要判断该数是不是beautiful数只需要将该数的每个权位上的数字提取出来存进一个A数组中,然后通过两个for循环进行遍历,通过flag进行标记提前结束不满足的循环。最后就是进行通过前缀和求差得到结果~

代码如下:

 1 #include <cstdio>
 2 
 3 const int Max=1e5+50;
 4 int sum[Max],A[10];
 5 int T,a,b;
 6 
 7 void Sum(){
 8   sum[0]=0;
 9   for(int i=1;i<=100000;i++){
10     int t=0,k=i;
11     //接下来的循环用于将i的每个权位的数分离开来;
12     while(k>0){
13       A[t++]=k%10;
14       k/=10;
15     }
16     if(t==1){
17       sum[i]=sum[i-1]+1;     //i<10时的所有数都满足题意;
18     }
19     else{
20       int flag=0;
21       for(int j=1;j<t;j++){
22         for(int q=0;q<j;q++){
23           if(A[j]==A[q]){
24             flag++;          //立个flag,当有两个数相等时退出当前循环,减少循环次数;
25             break;
26           }
27         }
28         if(flag) break;
29       }
30       if(flag){
31         sum[i]=sum[i-1];     //当前数不满足要求时,前i个数中满足要求的数的个数等于前i-1时的个数;
32       }
33       else{
34         sum[i]=sum[i-1]+1;   //当前数满足要求时,前i个数中满足要求的数等于前i-1时的个数加1;
35       }
36     }
37   }
38 }
39 
40 int main(){
41   Sum();                    //打表;
42   while(~scanf("%d",&T)){
43     while(T--){
44       scanf("%d%d",&a,&b);
45       printf("%d\n",sum[b]-sum[a-1]);      //一维前缀和经典计算方法;
46     }
47   }
48 }

 

 

转载于:https://www.cnblogs.com/Dillonh/p/8490008.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值