UVa 11361 - Investigating Div-Sum Property
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2346
数位DP
代码:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue> #define ll long long
using namespace std; int dp[11][90][10002];
int a[11];
int c[11];
int M;
int dfs(int x,int k1,int k2,int flag)
{
if(x==-1)
{
if(k1==0&&k2==0)
return 1;
else
return 0;
}
if(!flag&&dp[x][k1][k2]!=-1)
return dp[x][k1][k2];
int up=(flag)?a[x]:9;
int sum=0;
for(int i=0;i<=up;++i)
{
//sum+=dfs(x-1,(k1+i)%M,(k2*10+i)%M,flag&&(i==up)); if(((k1-i)%M+M)%M<90)//两种递归,代表两种不同的含义,都是正确的
sum+=dfs(x-1,((k1-i)%M+M)%M,((k2-i*c[x])%M+M)%M,flag&&(i==up));
}
if(!flag)
dp[x][k1][k2]=sum; return sum;
}
int solve(int k)
{
int len=0;
while(k)
{
a[len++]=k%10;
k=k/10;
}
return dfs(len-1,0,0,1);
}
int main()
{
//freopen("data.in","r",stdin);
int T;
cin>>T;
while(T--)
{
memset(dp,-1,sizeof(dp));
int a,b;
cin>>a>>b>>M;
c[0]=1;
for(int i=1;i<=10;++i)
c[i]=(c[i-1]*10)%M;
//cout<<b<<" "<<solve(b)<<endl;
cout<<solve(b)-solve(a-1)<<endl;
}
return 0;
}
UVa 11361 - Investigating Div-Sum Property的更多相关文章
- 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】UVA - 11361 - Investigating Div-Sum Property
经典数位dp!而且这好像是数位dp的套路板子……不需要讨论原来我很头疼的一些边界. 改天用这个板子重做一下原来的一些数位dp题目. http://blog.csdn.net/the_useless/a ...
- 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 ...
- UVa 11361 (计数 递推) Investigating Div-Sum Property
题意: 统计[a, b]中有多少个数字满足:自身是k的倍数,而且各个数字之和也是k的倍数. 分析: 详细分析见<训练之南>吧,=_=|| 书上提出了一个模板的概念,有了模板我们就可以分块计 ...
- [题解]UVa 10891 Game of Sum
在游戏的任何时刻剩余的都是1 - n中的一个连续子序列.所以可以用dp[i][j]表示在第i个数到第j个数中取数,先手的玩家得到的最大的分值.因为两个人都很聪明,所以等于自己和自己下.基本上每次就都是 ...
- 09_Sum游戏(UVa 10891 Game of Sum)
问题来源:刘汝佳<算法竞赛入门经典--训练指南> P67 例题28: 问题描述:有一个长度为n的整数序列,两个游戏者A和B轮流取数,A先取,每次可以从左端或者右端取一个或多个数,但不能两端 ...
- UVa 10891 - Game of Sum 动态规划,博弈 难度: 0
题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- UVa 10891 Game of Sum - 动态规划
因为数的总和一定,所以用一个人得分越高,那么另一个人的得分越低. 用$dp[i][j]$表示从$[i, j]$开始游戏,先手能够取得的最高分. 转移通过枚举取的数的个数$k$来转移.因为你希望先手得分 ...
- UVA 10891 Game of Sum(区间DP(记忆化搜索))
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
随机推荐
- [css]【转载】CSS样式分离之再分离
原文链接:http://www.zhangxinxu.com/wordpress/2010/07/css%E6%A0%B7%E5%BC%8F%E5%88%86%E7%A6%BB%E4%B9%8B%E5 ...
- about shell
摘抄 cat 等 cat主要有三大功能:1.一次显示整个文件.$ cat filename2.从键盘创建一个文件.$ cat > filename只能创建新文件,不能编辑已有文件.3.将 ...
- Illegal mix of collations (big5_chinese_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation 'like'
解释: 非法的混合排序规则(big5_chinese_ci)和(utf8_general_ci)操作“like”. 原本是单个字段查询数据的,现在是把所有的字段用一个搜索框来查询. 主要出问题是下列这 ...
- java中的堆内存和栈内存
Java把内存分成两种: 一种叫做栈内存 一种叫做堆内存 栈内存 : 在函数中定义的一些基本类型的变量和对象的引用变量都是在函数的栈内存中分配.当在一段代码块中定义一个变量时,java就在栈中为这个变 ...
- Sqlserver_In、exists使用
in是把外表和内表作hash 连接,而exists 是对外表作loop 循环,每次loop 循环再对内表进行查询. 一直以来认为exists 比in 效率高的说法是不准确的.如果查询的两个表大小相当, ...
- HTTP Status 500 - An exception occurred processing at line 35
HTTP Status 500 - An exception occurred processing JSP page /manage/addCategory.jsp at line 35 type ...
- Linux环境下JDK/Eclipse一键安装脚本
-------------------------------------------------------------------- author:jiangxin Email:jiangxinn ...
- 复杂TableView在iOS上的性能优化
声明:本文翻译自<iOS performance optimization>,原文作者 Khang Vo.翻译本文纯属为了技术交流的目的,并不具有任何的商业性质,也不得利用本文内容进行商业 ...
- jsonp跨域原理
Jsonp原理: 首先在客户端注册一个callback (如:'jsoncallback'), 然后把callback的名字(如:jsonp1236827957501)传给服务器.注意:服务端得到ca ...
- js键盘操作事件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...