HDU 4249 A Famous Equation(数位DP)
题目链接:点击打开链接
思路:用d[i][a][b][c][is]表示当前到了第i位, 三个数的i位各自是a,b,c, 是否有进位 , 的方法数。
细节參见代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
typedef long double ld;
const ld eps = 1e-9, PI = 3.1415926535897932384626433832795;
const int mod = 1000000000 + 7;
const int INF = 0x3f3f3f3f;
// & 0x7FFFFFFF
const int seed = 131;
const ll INF64 = ll(1e18);
const int maxn = 15;
int T,n,m,len,vis[maxn][maxn][maxn][maxn][2],len1,len2,len3,kase = 0;
char a[maxn],b[maxn],c[maxn],s[100];
ll d[maxn][maxn][maxn][maxn][2];
ll dp(int pos, int bb, int cc, int dd, int is) {
ll& ans = d[pos][bb][cc][dd][is];
if(pos > len) return is == 0;
if(vis[pos][bb][cc][dd][is] == kase) return ans;
vis[pos][bb][cc][dd][is] = kase;
ans = 0;
if(a[pos] == '?' && b[pos] == '?') {
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 10; j++) {
if(pos == len1 && i == 0 && len1 != 1) continue;
if(pos == len2 && j == 0&& len2 != 1) continue;
int cur = i + j + is;
int res = 0;
if(cur >= 10) {
cur -= 10; ++res;
}
if(c[pos] == '? ' && !(pos == len3 && cur == 0 && len3 != 1)) ans += dp(pos+1,i, j,cur, res);
else if(c[pos]-'0' == cur) ans += dp(pos+1, i,j,cur, res);
}
}
}
else if(a[pos] == '?') {
for(int i = 0; i < 10; i++) {
if(pos == len1 && i == 0&& len1 != 1) continue;
int cur = i + b[pos]-'0' + is;
int res = 0;
if(cur >= 10) {
cur -= 10; ++res;
}
if(c[pos] == '? ' && !(pos == len3 && cur == 0 && len3 != 1)) ans += dp(pos+1,i,b[pos]-'0',cur, res);
else if(c[pos]-'0' == cur) ans += dp(pos+1,i,b[pos]-'0',cur, res);
}
}
else if(b[pos] == '?') {
for(int i = 0; i < 10; i++) {
if(pos == len2 && i == 0&& len2 != 1) continue;
int cur = i + a[pos]-'0' + is;
int res = 0;
if(cur >= 10) {
cur -= 10; ++res;
}
if(c[pos] == '?' && !(pos == len3 && cur == 0 && len3 != 1)) ans += dp(pos+1,a[pos]-'0',i,cur, res);
else if(c[pos]-'0' == cur) ans += dp(pos+1,a[pos]-'0',i,cur, res);
}
}
else {
int cur = a[pos]-'0' + b[pos]-'0' + is;
int res = 0;
if(cur >= 10) {
cur -= 10; ++res;
}
if(c[pos] == '?' && !(pos == len3 && cur == 0 && len3 != 1)) ans += dp(pos+1, a[pos]-'0',b[pos]-'0',cur, res);
else if(c[pos]-'0' == cur) ans += dp(pos+1, a[pos]-'0',b[pos]-'0',cur, res);
}
return ans;
}
int main() {
while(~scanf("%s",s+1)) {
len = strlen(s+1);
len1 = 0; len2 = 0; len3 = 0;
int id = 0;
for(int i = len; i >= 1; i--) {
if(s[i] == '=' || s[i] == '+') { id++; continue; }
if(id == 0) {
c[++len3] = s[i];
}
else if(id == 1) {
b[++len2] = s[i];
}
else {
a[++len1] = s[i];
}
}
len = max(len1, max(len2, len3));//补全不足的。 降低代码量
for(int i = len1+1; i <= len; i++) a[i] = '0';
for(int i = len2+1; i <= len; i++) b[i] = '0';
for(int i = len3+1; i <= len; i++) c[i] = '0';
++kase;
ll ans = dp(1, 0 , 0, 0, 0);
printf("Case %d: %I64d\n",kase,ans);
}
return 0;
}
HDU 4249 A Famous Equation(数位DP)的更多相关文章
- HDU 4294 A Famous Equation(DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4249 题目大意:给一个a+b=c的表达式,但是a.b.c中部分位的数字丢失,并用?代替,问有多少种方案 ...
- HDOJ 4249 A Famous Equation DP
DP: DP[len][k][i][j] 再第len位,第一个数len位为i,第二个数len位为j,和的第len位为k 每一位能够从后面一位转移过来,能够进位也能够不进位 A Famous Equat ...
- HDU 4507 (鬼畜级别的数位DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4507 题目大意:求指定范围内与7不沾边的所有数的平方和.结果要mod 10^9+7(鬼畜の元凶) 解题 ...
- HDU 5787 K-wolf Number (数位DP)
K-wolf Number 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5787 Description Alice thinks an integ ...
- 【HDU 3652】 B-number (数位DP)
B-number Problem Description A wqb-number, or B-number for short, is a non-negative integer whose de ...
- HDU 5787 K-wolf Number(数位DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5787 [题目大意] 求区间[L,R]内十进制数相邻k位之间不相同的数字的个数. [题解] 很显然的 ...
- 2017"百度之星"程序设计大赛 - 复赛1005&&HDU 6148 Valley Numer【数位dp】
Valley Numer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 4352 XHXJ's LIS 数位dp lis
目录 题目链接 题解 代码 题目链接 HDU 4352 XHXJ's LIS 题解 对于lis求的过程 对一个数列,都可以用nlogn的方法来的到它的一个可行lis 对这个logn的方法求解lis时用 ...
- HDU 2089 不要62(数位dp模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求区间内不包含4和连续62的数的个数. 思路: 简单的数位dp模板题.给大家推荐一个好的讲解博客.h ...
随机推荐
- dubbo之服务容器
服务容器是一个standalone的启动程序,因为后台服务不需要Tomcat或JBoss等Web容器的功能,如果硬要用Web容器去加载服务提供方,增加复杂性,也浪费资源. 服务容器只是一个简单的Mai ...
- 有关bash,我希望我能知晓的十件事
简介 我之前的一篇文章比我预想的更受欢迎,因此我想再写一篇文章来介绍一些不太知名的bash功能 正如之前所言,由于我觉得bash是一种要经常使用(且需理解)的技术,所以我在研究bash时写了一本书.虽 ...
- wpf 自定义Button按钮
创建ButtonEx类 public class ButtonEx : Button { static ButtonEx() { DefaultStyleKeyProperty.OverrideMet ...
- 查询条件中,不进sql语句 也不进后台bug
前端代码:本来代码中少写了value="1",后来加上value值之后,可以正常进方法 <div class="row"> <label cl ...
- 带返回值的线程Callable
- mybatis中resultMap引发的吐血bug
简单的讲: 问题背景:如果在写mybatis中的resultMap时,不下心将resultMapde id写成映射接口的名字,会发生什么? 结论:单元测试进度条卡住但不报错, Tomcat运行不报错, ...
- LINUX-光盘
cdrecord -v gracetime=2 dev=/dev/cdrom -eject blank=fast -force 清空一个可复写的光盘内容 mkisofs /dev/cdrom > ...
- 阻塞套接字返回EAGAIN
今天用NDK写了一个通信程序,发现阻塞SOKCET 读写的时候返回了EAGAIN.NDK下PERROR输出为Try Again.查了半天头文件 在网上找到了原因.在此纪录.网址为http://blog ...
- 自己写一个HashMap
package cn.aresoft; /** * HashMap原理 * * @author develp * HashMap是一种以键值对存储数据的数据结构,简单的来说是这样.内部怎么实现的呢?实 ...
- 设置NODE_ENV=test环境变量
之前开发时因为有内网测试环境和外网测试环境,再部署打包时总是切换两个域名,比较麻烦,所以最好能设置一个环境变量,来控制两个域名,于是做了如下配置: "scripts": { &qu ...