Light oj 1140 - How Many Zeroes? 数位dp
2016-04-29 15:41
435 查看
1140 - How Many Zeroes?
Jimmy writes down the decimal representations of all natural numbers between and including m and n, (m ≤ n). How many zeroes will he write down?
Each case contains two unsigned 32-bit integers m and n, (m ≤ n).
SPECIAL THANKS: JANE ALAM JAN (DESCRIPTION, SOLUTION, DATASET)
求任意区间的数中0的出现次数
注意判断是否是前导0 //用lld
ACcdoe
#include <cstdio>
#include <cstring>
#define ll long long
ll dp[22][22];
int data[22];
int cnt=1;
ll dfs(int len,int sum,int is_0,int limit){
if(!len)return is_0?1:sum;
if(!limit&&dp[len][sum]!=-1&&!is_0)return dp[len][sum];
int ed=limit?data[len]:9;
ll ans=0;
for(int i=0;i<=ed;++i)
if(is_0)ans+=dfs(len-1,0,i==0,limit&&i==ed);
else ans+=dfs(len-1,sum+(i==0),0,limit&&i==ed);
return (is_0||limit)?ans:dp[len][sum]=ans;
}
ll fun(ll n){
int len=0;
while(n){
data[++len]=n%10;
n/=10;
}
return dfs(len,0,1,1);
}
void doit(){
ll x,y;
scanf("%lld%lld",&x,&y);
printf("Case %d: %lld\n",cnt++,fun(y)-fun(x-1));
}
int main(){
int loop;
scanf("%d",&loop);
memset(dp,-1,sizeof(dp));
while(loop--)doit();
}
![]() ![]() | PDF (English) | Statistics | Forum |
Time Limit: 2 second(s) | Memory Limit: 32 MB |
Input
Input starts with an integer T (≤ 11000), denoting the number of test cases.Each case contains two unsigned 32-bit integers m and n, (m ≤ n).
Output
For each case, print the case number and the number of zeroes written down by Jimmy.Sample Input | Output for Sample Input |
5 10 11 100 200 0 500 1234567890 2345678901 0 4294967295 | Case 1: 1 Case 2: 22 Case 3: 92 Case 4: 987654304 Case 5: 3825876150 |
SPECIAL THANKS: JANE ALAM JAN (DESCRIPTION, SOLUTION, DATASET)
求任意区间的数中0的出现次数
注意判断是否是前导0 //用lld
ACcdoe
#include <cstdio>
#include <cstring>
#define ll long long
ll dp[22][22];
int data[22];
int cnt=1;
ll dfs(int len,int sum,int is_0,int limit){
if(!len)return is_0?1:sum;
if(!limit&&dp[len][sum]!=-1&&!is_0)return dp[len][sum];
int ed=limit?data[len]:9;
ll ans=0;
for(int i=0;i<=ed;++i)
if(is_0)ans+=dfs(len-1,0,i==0,limit&&i==ed);
else ans+=dfs(len-1,sum+(i==0),0,limit&&i==ed);
return (is_0||limit)?ans:dp[len][sum]=ans;
}
ll fun(ll n){
int len=0;
while(n){
data[++len]=n%10;
n/=10;
}
return dfs(len,0,1,1);
}
void doit(){
ll x,y;
scanf("%lld%lld",&x,&y);
printf("Case %d: %lld\n",cnt++,fun(y)-fun(x-1));
}
int main(){
int loop;
scanf("%d",&loop);
memset(dp,-1,sizeof(dp));
while(loop--)doit();
}
相关文章推荐
- Maven项目有工程报错,但是不影响运行
- 传统的错误处理方法
- java集合迭代器Iterator中的remove陷阱
- js 弹出确认 取消对话框
- Java多线程之synchronized和volatile
- nyoj_62 笨小熊
- Kafka Confluent 简介
- Ext.js添加子组件
- 丢掉幻想--《疯狂动物城》观后感
- 打包的文件不能被tomcat的读取illegalargumentexception
- eclipse中ant的安装
- C语言如何不使用“+”号进行加法运算!
- 利用redis + lua解决抢红包高并发的问题
- Bluemix的三台虚拟机做MongoDB Replica Set
- 递归
- Docker —— 用于统一开发和部署的轻量级 Linux 容器
- Material Design学习(三)
- js处理json数组
- js实现密码加密
- jQuery基础 - 改变CSS样式