LightOJ 1068 Investigation (数位dp)
来源:互联网 发布:安卓windows桌面主题 编辑:程序博客网 时间:2024/06/10 19:48
http://www.lightoj.com/volume_showproblem.php?problem=1068
求出区间[A,B]内能被K整除且各位数字之和也能被K整除的数的个数。(1 ≤ A ≤ B < 231 and 0 < K < 10000)
算是最简单的数位dp了,k在这里是10000,三维数组都开不开。但是想想会发现A,B最多有10位,各位数字之和不会超过90,那么当 k >= 90时,就不用dp,因为个位数字之和对k取余不会等于0。所以数组只需开到dp[12][90][90]。
#include <stdio.h>#include <iostream>#include <map>#include <set>#include <list>#include <stack>#include <vector>#include <math.h>#include <string.h>#include <queue>#include <string>#include <stdlib.h>#include <algorithm>#define LL __int64#define eps 1e-12#define PI acos(-1.0)using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 4010;int dp[15][92][92];int a,b,k;int dig[15];int dfs(int len, int mod1, int mod2, int up){if(len == 0)return mod1 == 0 && mod2 == 0;if(!up && dp[len][mod1][mod2] != -1)return dp[len][mod1][mod2];int res = 0;int n = up ? dig[len] : 9;for(int i = 0; i <= n; i++){int tmod1 = (mod1*10 + i)%k;int tmod2 = (mod2 + i)%k;res += dfs(len-1,tmod1,tmod2,up&&i==n);}if(!up)dp[len][mod1][mod2] = res;return res;}int cal(int num){int len = 0;while(num){dig[++len] = num%10;num /= 10;}return dfs(len,0,0,1);}int main(){int test;scanf("%d",&test);for(int item = 1; item <= test; item++){scanf("%d %d %d",&a,&b,&k);if(k >= 90){printf("Case %d: 0\n",item);continue;}memset(dp,-1,sizeof(dp));printf("Case %d: %d\n",item,cal(b) - cal(a-1));}return 0;}
0 0
- lightoj 1068 Investigation 数位dp
- LightOJ 1068 Investigation (数位dp)
- lightoj 1068 - Investigation (数位DP)
- LightOJ - 1068 Investigation(数位DP)
- LightOJ 1068 - Investigation(数位DP)
- lightoj 1068 - Investigation 数位DP
- lightOJ 1068 - Investigation (数位dp)
- LightOJ 1068 Investigation (数位DP)
- lightoj - 1068 Investigation (数位dp)
- LightOJ 1068 - Investigation (数位dp)
- 【1】【状压DP】LightOJ 1068 Investigation
- Light oj 1068 - Investigation(数位dp)
- Light OJ 1068 Investigation (数位DP)
- Investigation (数位dp)
- lightoj 1068 数位DP
- LightOJ 1068数位dp
- Lightoj 1068 数位dp
- lightoj 1068(数位dp)
- 数组的指针特性
- hdu1394-Minimum Inversion Number-线段树和逆序数的爱情
- Linux学习笔记(八)
- POJ 1014 Dividing(多重背包)
- DSP-BIOS使用入门
- LightOJ 1068 Investigation (数位dp)
- 【Android UI】案例01Cover Flow3D效果的实现(Gallery+BaseAdapter)
- Qt Model/View 学习笔记 (二)
- hadoop学习工作总结(一)
- 使用Tcl脚本分配FPGA管脚
- 知道先序和中序,求后序以及层次遍历
- Markdown中插入数学公式的方法
- Qt Model/View 学习笔记 (三)
- C++学习笔记