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 ...
随机推荐
- NTT【51nod】1514 美妙的序列
题意:1~n 的全排列中,有多少个排列满足任意从中间切成两段后,左边段的最大值大于右边段的最小值? 例如:n为3时有3种 2 3 1 3 1 2 3 2 1 解释:比如 2 3 1 (2) (3 1) ...
- java实现将资源文件转化成sql语句导入数据库
文档结构
- hdu4570Multi-bit Trie
链接 13年长沙邀请赛的题,神题意~ 题意:摘自http://blog.csdn.net/libin56842/article/details/9703457 这题题意确实有点难懂,起码对于我这个英语 ...
- Java后端开发
Java后端开发 名称 内容 基本框架 Spring.Mybatis Linux服务器 数据库优化 消息服务 rabbitMQ.activeMq rocketMq 缓存服务 memcached ...
- JavaScript 开发进阶:理解 JavaScript 作用域和作用域链(转载 学习中。。。)
作用域是JavaScript最重要的概念之一,想要学好JavaScript就需要理解JavaScript作用域和作用域链的工作原理.今天这篇文章对JavaScript作用域和作用域链作简单的介绍,希望 ...
- 有关使用Maven常见问题总结(Eclipse中使用Maven、Maven项目部署到tomcat等问题)
http://blog.csdn.net/sunitjy/article/details/42709311 ********************************************** ...
- yum的使用及配置
yum的使用及配置 文章来源:http://www.ilanni.com/?p=9032 最近由于服务器需求,需要在公司内网搭建内网yum源. 搭建内网yum源需要分以下几个步骤,如下: 1. yum ...
- SqlServer扩展存储过程
1. 扩展存储过程xp_cmdshell用法: --1.启用 SP_CONFIGURE RECONFIGURE GO SP_CONFIGURE RECONFIGURE GO --2.用法 master ...
- ManyToMany关联方式----
http://blog.csdn.net/sinat_18882775/article/details/51171427 好的博客 系列文章总结
- ios CoreBluetooth 警告 is being dealloc'ed while pending connection
ios CoreBluetooth 警告 is being dealloc'ed while pending connection CoreBluetooth[WARNING] <CBPerip ...