Codeforces 507D (Round #287(div.2))D. The Maths Lecture【数位DP】
来源:互联网 发布:淘宝充值卡代理 编辑:程序博客网 时间:2024/06/01 10:25
题目地址:http://codeforces.com/contest/507/problem/D
题意:
给出n,k,m,问满足一下条件的数字有多少:
1.这个数刚好有n位;
2.这个数存在某非空的后缀Si,满足Si%k=0;
3.这个数大于0;
解法:
这个题是个数位DP,我们考虑从数的右边往左边构造的方法。
定义这样的DP数组, dp[i][j][0]:表示有i位数位,mod k=j,且不含mod k=0的后缀的数有多少个
dp[i][j][1]:表示有i位数位,mod k=j,且含有mod k=0的后缀的数有多少个
于是最后的结果就是ans+=dp[n][i][1](i:0~k-1)
代码:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define N 1001int n,k,m;int dp[N][111][2];int tenk[N];void getTenk(){ tenk[0]=1; for(int i=1;i<=n;i++) tenk[i]=(tenk[i-1]*10)%k;}int main(){ while(~scanf("%d%d%d",&n,&k,&m)) { memset(dp,0,sizeof(dp)); getTenk(); dp[0][0][0]=1; for(int i=0;i<n;i++) for(int j=0;j<k;j++) for(int l=(i==n-1);l<10;l++) { int now=(j+l*tenk[i])%k; if(now==0 && l)//如果l=0的话,其实这个数并不是i+1位数位的,所以不能加到dp[i+1][now][1]中; dp[i+1][now][1]=(dp[i+1][now][1]+dp[i][j][0])%m; else dp[i+1][now][0]=(dp[i+1][now][0]+dp[i][j][0])%m; dp[i+1][now][1]=(dp[i+1][now][1]+dp[i][j][1])%m;//原本含有后缀的数,在最前面加上一个数得到的数肯定也是含有后缀的 } int ans=0; for(int i=0;i<k;i++) ans=(ans+dp[n][i][1])%m; printf("%d\n",ans); } return 0;}
0 0
- Codeforces 507D (Round #287(div.2))D. The Maths Lecture【数位DP】
- Codeforces Round #287 (Div. 2) D. The Maths Lecture (数位dp)
- Codeforces Round #287 (Div. 2) D. The Maths Lecture 数位dp
- Codeforces Round #287 (Div. 2) D. The Maths Lecture(数位dp)
- Codeforces Round #287 (Div. 2)-D. The Maths Lecture(数位dp)
- codeforces--507D--The Maths Lecture(数位dp)
- The Maths Lecture - CodeForces 507 D dp
- CF #287 div2 D The Maths Lecture 数位DP
- 507D The Maths Lecture
- Codeforences #287 div2 D. The Maths Lecture
- Codeforces507D 数位dp The Maths Lecture
- Codeforces Round #228 Div.1 D 数位dp+数学
- Codeforces Round #273 (Div. 2)D dp
- Codeforces Round #358 (Div. 2) D dp
- Codeforces Round #235 (Div. 2) / 410D Roman and Numbers (带有整除性质的数位DP)
- Codeforces Round #235 (Div. 2) D. Roman and Numbers (数位dp、状态压缩)
- Codeforces Round #157 (Div. 2)D(数位DP+组合数)
- CodeForces 507D The Maths Lecture 构造恰好n位数且存在一个后缀能被k整除的方法数 dp
- Codeforces Problem - 13E Holes
- Enterprise User's Meeting 2014 slide
- Tomcat启动时,自动访问本地servlet
- delete this--对象请求自杀
- OpenGL ES入门详解
- Codeforces 507D (Round #287(div.2))D. The Maths Lecture【数位DP】
- const char b
- 位运算符的应用的总结
- 关于在console中查看opencv Mat 矩阵元素的问题
- 反射获取class对象的三种方式
- 友元函数
- vs2010中看不见类视图和资源视图的解决方法
- 引用
- angular服务