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最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).(单词互不相同) ...
随机推荐
- java的参数传递
1按值传递:传递的是原始值的副本,而不是原始值的内存地址 基本数据类型是传原始值的副本 class Test02 { public static void main(String[] args) { ...
- Window Classes in Win32
探索Win32系统之窗口类(Window Classes in Win32) Kyle MarshMicrosoft Developer Network Technology GroupMSDN技术组 ...
- OSPF学习中的问题
OSPF对接两方,对设置的要求,哪些参数必须相同 (HELLO &dead interval, area ID, authentation, 末节区域(option中的E位), network ...
- 【Redis】- 主从复制
Redis跟MySQL一样,拥有非常强大的主从复制功能,而且还支持一个master可以拥有多个slave,而一个slave又可以拥有多个slave,从而形成强大的多级服务器集群架构. redis的主从 ...
- 扩展SplitContainer控件
效果图: 自定义控件实现代码: using System; using System.ComponentModel; using System.Drawing; using System.Window ...
- 【bzoj1441】Min 扩展裴蜀定理
题目描述 给出n个数(A1...An)现求一组整数序列(X1...Xn)使得S=A1*X1+...An*Xn>0,且S的值最小 输入 第一行给出数字N,代表有N个数 下面一行给出N个数 输出 S ...
- 【bzoj2502】清理雪道 有上下界最小流
题目描述 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降的方向. 你的团队负责每周定时清理雪道.你们拥有一架直升飞 ...
- hdu 3015 Disharmony Trees (离散化+树状数组)
Disharmony Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Python使用requests模块下载图片
MySQL中事先保存好爬取到的图片链接地址. 然后使用多线程把图片下载到本地. # coding: utf-8 import MySQLdb import requests import os imp ...
- POJ1149:PIGS——题解
http://poj.org/problem?id=1149 题目大意: Mirko有M个猪圈和N个客户,猪圈里有特定数量的猪,每个客户按照顺序来买猪,他们只能打开他们能打开的猪圈,然后取走一些猪(上 ...