题目链接:点击打开链接

思路:用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)的更多相关文章

  1. HDU 4294 A Famous Equation(DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4249 题目大意:给一个a+b=c的表达式,但是a.b.c中部分位的数字丢失,并用?代替,问有多少种方案 ...

  2. HDOJ 4249 A Famous Equation DP

    DP: DP[len][k][i][j] 再第len位,第一个数len位为i,第二个数len位为j,和的第len位为k 每一位能够从后面一位转移过来,能够进位也能够不进位 A Famous Equat ...

  3. HDU 4507 (鬼畜级别的数位DP)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4507 题目大意:求指定范围内与7不沾边的所有数的平方和.结果要mod 10^9+7(鬼畜の元凶) 解题 ...

  4. HDU 5787 K-wolf Number (数位DP)

    K-wolf Number 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5787 Description Alice thinks an integ ...

  5. 【HDU 3652】 B-number (数位DP)

    B-number Problem Description A wqb-number, or B-number for short, is a non-negative integer whose de ...

  6. HDU 5787 K-wolf Number(数位DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5787 [题目大意] 求区间[L,R]内十进制数相邻k位之间不相同的数字的个数. [题解] 很显然的 ...

  7. 2017"百度之星"程序设计大赛 - 复赛1005&&HDU 6148 Valley Numer【数位dp】

    Valley Numer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  8. HDU 4352 XHXJ's LIS 数位dp lis

    目录 题目链接 题解 代码 题目链接 HDU 4352 XHXJ's LIS 题解 对于lis求的过程 对一个数列,都可以用nlogn的方法来的到它的一个可行lis 对这个logn的方法求解lis时用 ...

  9. HDU 2089 不要62(数位dp模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=2089 题意:求区间内不包含4和连续62的数的个数. 思路: 简单的数位dp模板题.给大家推荐一个好的讲解博客.h ...

随机推荐

  1. android视频播放器系列(二)——VideoView

    最近在学习视频相关的知识,现在也是在按部就班的一步步的来,如果有同样需求的同学可以跟着大家一起促进学习. 上一节说到了可以使用系统播放器以及浏览器播放本地以及网络视频,但是这在很大程度上并不能满足我们 ...

  2. 提取header头进行模块化处理

    在进行爬取网上东西的时候一般网站都做了UA的过滤,解决办法就是在代码中加入. 所以才有了本篇提取header头信息单独写成一个模块或者说是函数/类的想法,直接上示例 1.把UA头信息在浏览器中复制出来 ...

  3. SQL 语句在存储过程执行和在SSMS中执行的差异

    SQL 语句在存储过程执行和在SSMS中执行的差异 SSMS是SQlSerever management studio.本文所述情形在SQLServer2008中测试. 有时发现同样几条语句,在SSM ...

  4. js中时钟表盘

    1.js时钟表盘 代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  5. MySQL——基本安装与使用

    基本安装 下载地址:https://dev.mysql.com/downloads/mysql/ 选择解压版本:mysql-5.7.21-winx64.zip 以管理员身份打开cmd(除了安装服务不要 ...

  6. 安装好Pycharm后如何配置Python解释器简易教程

    呃呃,遇到坑了...... 安装完Python,没有去配置好Python解释器,直接打开Python项目包,去运行程序,程序输出结果只是显示 Process finished with exit co ...

  7. java jvm eclipse 性能调优

    低配配置 -Dfile.encoding=UTF-8-Xms960m-Xmx960m-Xmn384m-Xverify:none-Xss256k-XX:MaxTenuringThreshold=2-XX ...

  8. 十二届 - CSU 1803 :2016(同余定理)

    题目地址:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1803 Knowledge Point: 同余定理:两个整数a.b,若它们除以整数m所 ...

  9. 洛谷——P2590 [ZJOI2008]树的统计(树链剖分模板练手)

    P2590 [ZJOI2008]树的统计 I. CHANGE u t : 把结点u的权值改为t II. QMAX u v: 询问从点u到点v的路径上的节点的最大权值 III. QSUM u v: 询问 ...

  10. poj - 3254 - Corn Fields (状态压缩)

    poj - 3254 - Corn Fields (状态压缩)超详细 参考了 @外出散步 的博客,在此基础上增加了说明 题意: 农夫有一块地,被划分为m行n列大小相等的格子,其中一些格子是可以放牧的( ...