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!
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.
For each testcase, print one line indicating the answer.
2
1 10
1 1000
10738
思路:题目的要求就是求出[a,b]中所有满足该数中每个权位上的数都不相同的个数。我的思路就是先打表(感觉不打表会被TLE),在打表过程中将前i个数中满足题意的个数储存在sum数组中。要判断该数是不是beautiful数只需要将该数的每个权位上的数字提取出来存进一个A数组中,然后通过两个for循环进行遍历,通过flag进行标记提前结束不满足的循环。最后就是进行通过前缀和求差得到结果~
代码如下:
#include <cstdio> const int Max=1e5+;
int sum[Max],A[];
int T,a,b; void Sum(){
sum[]=;
for(int i=;i<=;i++){
int t=,k=i;
//接下来的循环用于将i的每个权位的数分离开来;
while(k>){
A[t++]=k%;
k/=;
}
if(t==){
sum[i]=sum[i-]+; //i<10时的所有数都满足题意;
}
else{
int flag=;
for(int j=;j<t;j++){
for(int q=;q<j;q++){
if(A[j]==A[q]){
flag++; //立个flag,当有两个数相等时退出当前循环,减少循环次数;
break;
}
}
if(flag) break;
}
if(flag){
sum[i]=sum[i-]; //当前数不满足要求时,前i个数中满足要求的数的个数等于前i-1时的个数;
}
else{
sum[i]=sum[i-]+; //当前数满足要求时,前i个数中满足要求的数等于前i-1时的个数加1;
}
}
}
} int main(){
Sum(); //打表;
while(~scanf("%d",&T)){
while(T--){
scanf("%d%d",&a,&b);
printf("%d\n",sum[b]-sum[a-]); //一维前缀和经典计算方法;
}
}
}
HDU - 5327 Olympiad(一维前缀和)的更多相关文章
- hdu 5327 Olympiad
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5327 Olympiad Description You are one of the competit ...
- HDU 5327 Olympiad (多校)
Olympiad Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...
- HDU 5327 Olympiad (水题)
题意:beautiful数字定义为该数字中的十进制形式每一位都不同,给一个区间[L,R],求该区间中有多少个beautiful数字. 思路:数字不大,直接暴力预处理,再统计区间[1,i]有多少个,用c ...
- HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化
HDU 2829 区间DP & 前缀和优化 & 四边形不等式优化 n个节点n-1条线性边,炸掉M条边也就是分为m+1个区间 问你各个区间的总策略值最少的炸法 就题目本身而言,中规中矩的 ...
- hdu 5317 RGCDQ(前缀和)
题目链接:hdu 5317 这题看数据量就知道需要先预处理,然后对每个询问都需要在 O(logn) 以下的复杂度求出,由数学规律可以推出 1 <= F(x) <= 7,所以对每组(L, R ...
- hdu 6609 区间条件前缀和 + 二分
题目传送门//res tp hdu 目的 在尾部逐步插入n个元素,求插入第i个元素时,[1,i)内删去多少个元素,可使前缀和[1,i]不大于m 多测Q [1,15] n [1,2e5] m [1,1e ...
- HDU 3303 Harmony Forever 前缀和+树状数组||线段树
Problem Description We believe that every inhabitant of this universe eventually will find a way to ...
- HDU 5776 sum (前缀和)
题意:给定 n 个数,和 m,问你是不是存在连续的数和是m的倍数. 析:考虑前缀和,如果有两个前缀和取模m相等,那么就是相等的,一定要注意,如果取模为0,就是真的,不要忘记了,我当时就没记得.... ...
- HDU 1251 字典树(前缀树)
题目大意 :Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).(单词互不相同) ...
随机推荐
- week1 四人小组项目
小组名称:nice! 项目组长:李权 组员:于淼 刘芳芳 杨柳 项目选题:东北师范大学论坛 作为东北师范大学同学间的信息交流平台,要满足的需求如下: 1.校内信息及公告 2.毕业生招聘信息 3.课程查 ...
- Linux下安装MySQL管理工具MySQL Administrator和MySQL Query Browser(转载)
文章来源:http://blog.csdn.net/sunrier/article/details/7572299 Linux下MySQL Administrator和MySQL Query Brow ...
- Winform 数据绑定
1.DataGridView数据绑定 namespace WindowsFormsApplication1 { public partial class Form1 : Form { private ...
- == 和equal的区别?-005
1,== 和equal的区别? ==比较两个值是否相等,equal比较对对象的引用是否一致 举例: int a = 2; int b = 2; System.err.println(a == b);/ ...
- perf中branch-filter到底是干嘛的?
./arch/x86/events/intel/core.c:2161: data.br_stack = &cpuc->lbr_stack;./arch/x86/e ...
- Redis架构演变与redis-cluster群集读写方案
导言 redis-cluster是近年来redis架构不断改进中的相对较好的redis高可用方案.本文涉及到近年来redis多实例架构的演变过程,包括普通主从架构(Master.slave可进行写读分 ...
- 7款很棒的 HTML5 视频播放器
做个连接:http://www.cnblogs.com/lhb25/archive/2011/06/27/7-great-html-5-video-player-scripts.html
- 在a标签的href用户#name 的可以实现页面 上下跳转
- [LOJ2540] [PKUWC2018] 随机算法
题目链接 LOJ:https://loj.ac/problem/2540 Solution 写的时候脑子不太清醒码了好长然后时间\(LOJ\)垫底... 反正随便状压\(dp\)一下就好了,设\(f[ ...
- Android ListView 中加入CheckBox/RadioButton 选择状态保持、全选、反选实现
最近在一个项目中,需要在ListView的item中加入CheckBox,但是遇到的一个问题是上下滑动的时候如果有选择了的CheckBox,就会出现选择项错误的问题,下面将个人的解决方法总结如下;先说 ...