【数位dp】UVA - 11361 - Investigating Div-Sum Property
经典数位dp!而且这好像是数位dp的套路板子……不需要讨论原来我很头疼的一些边界。
改天用这个板子重做一下原来的一些数位dp题目。
http://blog.csdn.net/the_useless/article/details/53674906
题目大意:
给定a,b,k三个正整数,统计在[a,b]之间的整数n中,有多少n自身是k的倍数,且n的各个数字(十进制)之和也是k的倍数.(1⩽a⩽b⩽231)
题目分析:
这是一道典型的数位DP题.
n非常大,若是直接枚举的话会超时,考虑利用加法原理计算方案数.
将数拆分开来,拆成一位一位的,从前往后枚举.那么就会出现形如”32**”这样枚举了部分,还有部分未枚举.可以用三维状态来表示:f(d,m1,m2)表示当前还有d个数未枚举,m1表示前缀各数之和%k,m2表示组成数%k.如之前的数”32**”就应该对应为f(2,5%k,3200%k).
对应的转移方程则有
f(d,m1,m2)=∑f(d−1,(m1+i)%k,m2+i∗10d−1%k|0⩽i⩽9)
所以dp数组需要开多大.10∗10000∗10000≈109?开不下!
但是其实各个位数之和最大为1+9∗9=82,所以当k>82时,直接输出0.

#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
int a,b,MOD,T;
int dp[15][90][90],pow_10[15];
int f(int d,int m1,int m2){
if(dp[d][m1][m2]!=-1){
return dp[d][m1][m2];
}
dp[d][m1][m2]=0;
for(int i=0;i<10;i++){
dp[d][m1][m2]+=f(d-1,(m1+i)%MOD,(m2+i*pow_10[d-1])%MOD);
}
return dp[d][m1][m2];
}
int calc(int x)
{
int len=0;
if(!x){
len=1;
}
int t=x;
while(t){
++len;
t/=10;
}
int res=0,LeftSide=0,SumDigits=0;//LeftSideÊǵ±Ç°×ó±ß½ç£¬SumDigitsÊǵ±Ç°ËùÓÐÊýλ֮ºÍ
for(int i=1;i<=len;i++) {
while((ll)LeftSide+(ll)pow_10[len-i]-1ll<=(ll)x){
//ÅжÏÄÜ·ñ´ÓÕâÀï¼ÌÐøÍùÏÂÇó£¬ÒªÊDz»Äܵϰ¾ÍÒªÍùºóÍÆÒ»Î»
//±ÈÈç3212£¬Äã¾Í²»ÄÜ´Ó3200ÔÙÍù3299Ç󣬶øÓ¦¸ÃÍùºóÍÆµ½Íù3209Çó
res+=f(len-i,SumDigits%MOD,LeftSide%MOD);
LeftSide+=pow_10[len-i];
++SumDigits;
}
}
return res;
}
int main(){
// freopen("uvaLive4123.in","r",stdin);
scanf("%d",&T);
pow_10[0]=1;
for(int i=1;i<=9;++i){
pow_10[i]=pow_10[i-1]*10;
}
for(;T;--T){
scanf("%d%d%d",&a,&b,&MOD);
if(MOD>82){
puts("0");
continue;
}
memset(dp,-1,sizeof(dp));
for(int i=0;i<MOD;++i){
for(int j=0;j<MOD;++j){
dp[0][i][j]=0;
}
}
dp[0][0][0]=1;
printf("%d\n",calc(b)-calc(a-1));
}
return 0;
}
【数位dp】UVA - 11361 - Investigating Div-Sum Property的更多相关文章
- UVa 11361 - Investigating Div-Sum Property
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- 数位DP:SPOJ KPSUM - The Sum
KPSUM - The Sum One of your friends wrote numbers 1, 2, 3, ..., N on the sheet of paper. After that ...
- UVA 11361 - Investigating Div-Sum Property 数位DP
An integer is divisible by 3 if the sum of its digits is also divisible by 3. For example, 3702 is d ...
- 数位dp总结 之 从入门到模板
转发自WUST_WenHao巨巨的博客 基础篇 数位dp是一种计数用的dp,一般就是要统计一个区间[le,ri]内满足一些条件数的个数.所谓数位dp,字面意思就是在数位上进行dp咯.数位还算是比较好听 ...
- 数位dp相关
经典的数位Dp是要求统计符合限制的数字的个数. 一般的形式是:求区间[n,m]满足限制f(1). f(2). f(3)等等的数字的数量是多少. 条件 f(i) 一般与数的大小无关,而与数的组成有关. ...
- Investigating Div-Sum Property UVA - 11361
An integer is divisible by 3 if the sum of its digits is also divisible by 3. For example, 3702 is d ...
- UVA11361 Investigating Div-Sum Property(数位dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 题目意思:问在区间[A,B]有多少个数不仅满足自身是k的倍数,而且其各个位数上的和 ...
- Educational Codeforces Round 53 (Rated for Div. 2) E. Segment Sum (数位dp求和)
题目链接:https://codeforces.com/contest/1073/problem/E 题目大意:给定一个区间[l,r],需要求出区间[l,r]内符合数位上的不同数字个数不超过k个的数的 ...
- UVa 1009 Sharing Chocolate (数位dp)
题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_proble ...
随机推荐
- webpack自动生成项目的html
1 自动生成多个html页面 设置webpack.config.js中的plugins属性,多次调用plugin插件(new htmlWebpackPlugin()),同时设置对应数量的.js入口文件 ...
- perl HTML::LinkExtor模块(2)
use LWP::Simple; use HTML::LinkExtor; $html_code = get("https://tieba.baidu.com/p/4929234512&qu ...
- 【Android开发日记】之入门篇(十五)——ViewPager+自定义无限ViewPager
ViewPager 在 Android 控件中,ViewPager 一直算是使用率比较高的控件,包括首页的banner,tab页的切换都能见到ViewPager的身影. viewpager 来源自 v ...
- node中--save跟--save--dev
--save参数表示将该模块写入dependencies属性, --save-dev表示将该模块写入devDependencies属性. dependencies字段指定了项目运行所依赖的模, d ...
- LeetCode解题报告——Convert Sorted List to Binary Search Tree & Populating Next Right Pointers in Each Node & Word Ladder
1. Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in ...
- LeetCode解题报告—— Rotate List & Set Matrix Zeroes & Sort Colors
1. Rotate List Given a list, rotate the list to the right by k places, where k is non-negative. Exam ...
- Python中的PIL
转自:http://blog.csdn.net/yockie/article/details/8498301 介绍 把Python的基础知识学习后,尝试一下如何安装.加载.使用非标准库,选择了图像处理 ...
- Android点击图标重新启动问题
原文:http://blog.csdn.net/jianiuqi/article/details/54091181 项目中的小问题:发现应用打包安装后按home键切换到后台后,点击应用图标又重新打开了 ...
- 寻找与网页内容相关的图片(三)网易新闻与qq空间的做法
寻找与网页相关的图片现在看来无非有两种方式,第一种是网页自己指定,第二种是通过算法推断. 对于网站的内容提供者来说,他自己知道相关的图片在哪,正如前文所述可以在HTML的头部加上META标签,也可以像 ...
- SQL Error: 1064, SQLState: 42000
这个错误是因为mysql有些关键字被我们用了,需要更改关键字成其他名字 ADD ALL ALTER ANALYZE AND AS ASC ASENSITIVE BEFORE BETWEEN BIGIN ...