P2602 [ZJOI2010]数字计数

题解

DFS 恶心的数位DP

对于这道题,我们可以一个数字一个数字的求

也就是分别统计区间 [ L , R ] 内部数字 i 出现的次数 (0<=i<=9)

也就是DFS只需要记录 :

当前填到第几位 pos

k一共出现多少次 sum

目标数字 k

是否顶上界 limit

是否全是前导零 qdl

dp[pos][sum]:

>不顶上界,没有前导零,

当前填到第pos位,目标数字一共出现sum次的时候(前pos位中一共有sum个目标数字)

对答案产生的贡献

>由于sum最多会取到和pos一样的个数,所以数组大小开的和pos一样就好了

这里 sum 记录的时候分两种情况:

(1)k!=0     直接看看 所填数字是否目标数字 就好了

(2)k=0      <1> 前面全是前导零,但是所填数字不是0

<2> 填到最后数字是0,也就是0000000,此时0要算出现一次

<3> 其余情况就不记录0出现的次数了

代码

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<queue> using namespace std; typedef long long ll; inline ll read()
{
ll ans=;
char last=' ',ch=getchar();
while(ch<''||ch>'') last=ch,ch=getchar();
while(ch>=''&&ch<='') ans=ans*+ch-'',ch=getchar();
if(last=='-') ans=-ans;
return ans;
} ll a,b;
ll c[],len;
ll dp[][]; ll dfs(ll pos,ll sum,ll k,bool limit,bool qdl)
{
if(pos<=) return sum;
if(!limit&&!qdl&&dp[pos][sum]!=-) return dp[pos][sum];
ll ans=;
ll up=limit?c[pos]:;
for(int i=;i<=up;i++)
ans+=dfs(pos-,sum+(k==?(!qdl&&i==)||(qdl&&i==&&pos==):(i==k)),k,limit&&(i==up),qdl&&(i==));
if(!limit&&!qdl) dp[pos][sum]=ans;
return ans;
} ll sum(ll x,ll k)
{
memset(c,,sizeof(c));len=;
memset(dp,-,sizeof(dp));
while(x)
{
c[++len]=x%;
x/=;
}
return dfs(len,,k,,);
} int main()
{
a=read();b=read();
for(int i=;i<=;i++)
printf("%lld ",sum(b,i)-sum(a-,i)); return ;
}

双倍经验(比第一个简单)

P1239 计数器

题解

也就是只需要一个 a 就够够的了

代码

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<queue> using namespace std; typedef long long ll; inline ll read()
{
ll ans=;
char last=' ',ch=getchar();
while(ch<''||ch>'') last=ch,ch=getchar();
while(ch>=''&&ch<='') ans=ans*+ch-'',ch=getchar();
if(last=='-') ans=-ans;
return ans;
} const ll mod=1e9+;
ll a,b,T,ans=;
ll c[],len;
ll dp[][]; ll dfs(ll pos,ll sum,ll k,bool limit,bool qdl)
{
if(pos<=) return sum;
if(!limit&&!qdl&&dp[pos][sum]!=-) return dp[pos][sum];
ll ans=;
ll up=limit?c[pos]:;
for(int i=;i<=up;i++)
ans+=dfs(pos-,sum+(k==?(!qdl&&i==)||(qdl&&i==&&pos==):(i==k)),k,limit&&(i==up),qdl&&(i==));
if(!limit&&!qdl) dp[pos][sum]=ans;
return ans;
} ll sum(ll x,ll k)
{
memset(c,,sizeof(c));len=;
memset(dp,-,sizeof(dp));
while(x)
{
c[++len]=x%;
x/=;
}
return dfs(len,,k,,);
} int main()
{
a=read();
for(int i=;i<=;i++)
printf("%lld\n",sum(a,i));
return ;
}

三倍经验

P4999 烦人的数学作业

题解

拿题一看:我要AC辣!!!

现实是 90pt

为哈!!!!!!

取模的锅,多取几次

我还是太天真

代码

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<queue> using namespace std; typedef long long ll; inline ll read()
{
ll ans=;
char last=' ',ch=getchar();
while(ch<''||ch>'') last=ch,ch=getchar();
while(ch>=''&&ch<='') ans=ans*+ch-'',ch=getchar();
if(last=='-') ans=-ans;
return ans;
} const ll mod=1e9+;
ll a,b,T,ans=;
ll c[],len;
ll dp[][]; ll dfs(ll pos,ll sum,ll k,bool limit,bool qdl)
{
if(pos<=) return sum;
if(!limit&&!qdl&&dp[pos][sum]!=-) return dp[pos][sum];
ll ans=;
ll up=limit?c[pos]:;
for(int i=;i<=up;i++)
ans+=dfs(pos-,sum+(k==?(!qdl&&i==)||(qdl&&i==&&pos==):(i==k)),k,limit&&(i==up),qdl&&(i==));
if(!limit&&!qdl) dp[pos][sum]=ans;
return ans;
} ll sum(ll x,ll k)
{
memset(c,,sizeof(c));len=;
memset(dp,-,sizeof(dp));
while(x)
{
c[++len]=x%;
x/=;
}
return dfs(len,,k,,);
} int main()
{
T=read();
while(T--)
{
ans=;
a=read();b=read();
for(int i=;i<=;i++)
{
ll tmp=((sum(b,i)-sum(a-1,i))%mod+mod)%mod; ans=((ans+i*tmp%mod+mod)%mod+mod)%mod;
} printf("%lld\n",ans%mod);
} return ;
}

最新文章

  1. Javascript:JSON总结
  2. centos 7 安装mysql
  3. 一个最小化的SpringBoot项目
  4. mysql在线改表结构 pt-online-schema-change
  5. poj 1753 Flip Game
  6. Tomcat启动报错 Failed to start component [StandardServer[8005]]解决
  7. Android RelativeLayout
  8. Hibernate的fetch (转)
  9. event.preventDefault和恢复元素默认事件
  10. 《TCP/IP作品详细解释2:达到》注意事项--ARP:地址解析协议
  11. Linux Kernel系列 - 黄牛X内核代码凝视
  12. js 弹层 提示
  13. PhotoShop常用的功能汇总
  14. MySql:SELECT 语句(六) CONCAT() 函数的使用
  15. 在Java中使用Socket模拟客户端和服务端(多线程)
  16. Python 同步IO/异步IO了解
  17. ScheduledExecutorService--目前最理想的定时任务实现方式
  18. DOM操作——JavaScript怎样添加、移除、移动、复制、创建和查找节点
  19. UIImagePickerController按钮的中文问题
  20. 移动开发UI库

热门文章

  1. mybatis generator代码生成器的使用
  2. TVM图优化(以Op Fusion为例)
  3. 使用私有api统计ios app运行时间及次数
  4. MySQL cmd操作
  5. VM安装vmtools后centos7无法上网
  6. bitcoind搭建
  7. idea将普通目录转换为模块maven module。
  8. ubuntu16.04中不能连接无线网络
  9. Vue入门(三)——模拟网络请求加载本地数据
  10. JavaScript教程——数据类型概述