V
来源:互联网 发布:时光机还原软件 编辑:程序博客网 时间:2024/04/27 11:03
V - How Many Zeroes?
题意:
求任意区间的数中0的出现次数
思路:
数位dp,注意判断是否是前导0 注意用长整型
代码:
#include <cstdio>#include <cstring>#include <cmath>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;int bit[20];ll dp[20][20];ll dfs(int pos,int sum,int zero,int limit){//zero前导0,sum是0的个数 if(pos<0) return zero?1:sum; if(!zero&&!limit&& dp[pos][sum] != -1) return dp[pos][sum]; int up=limit?bit[pos] : 9; ll ans = 0; for(int i=0; i<=up; i++){ if(zero&&!i) ans+=dfs(pos-1,0,1,limit&&up==i);//判断前导零 else if(!i) ans+=dfs(pos-1,sum+1,0,limit&&i ==up);//第二次访问加1 else ans+=dfs(pos-1,sum,0,limit&&i==up); } if(!zero&&!limit) dp[pos][sum] = ans; return ans;}ll cal(ll x){if(x<0)return 0; if(x== 0) return 1; memset(dp,-1,sizeof(dp)); int pos=0; while(x){ bit[pos++] = x%10; x/=10; } return dfs(pos-1,0,1,1);}int main(){ int T,ans=1; ll n,m; scanf("%d",&T); while(T--){ scanf("%lld %lld",&n,&m); printf("Case %d: %lld\n",ans++,cal(m) - cal(n-1)); } return 0;}
阅读全文
0 0
- V
- v
- v
- v
- v
- v
- V
- V
- V
- V
- V
- V
- v
- V
- V
- V
- v
- V
- 稳住,我们能赢!(给自己炖一锅鸡汤)
- tensorboard 可视化。。。开两个。。。。
- 专插本日志
- bzoj3611 大工程
- 11.7每日一练
- V
- tensorflow(1)-初始化
- angular总结4-表单处理
- 【量化小讲堂-Python&Pandas系列01】如何快速上手使用Python进行金融数据分析
- linux下搭建go环境很easy
- Linux进程的管理与调度(一)--- Linux进程描述符task_struct结构体详解
- 2钟方法求八皇后个数!
- JDK8新特性之接口的静态方法和默认方法
- windbg基本命令