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 ...
随机推荐
- 基于SOC方案的嵌入式开发-远程定时设备
Soc方案实现简单的定时开关灯 http://club.gizwits.com/forum.php?mod=viewthread&tid=7787&highlight=%E5%AE%9 ...
- HTML5 页面调用微信接口
参考微信微信官方文档JS_SDK:https://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html#JSSDK.E4.BD.B ...
- MySQL性能优化必备25条
1. 为查询缓存优化你的查询 大多数的MySQL服务器都开启了查询缓存.这是提高性最有效的方法之一,而且这是被MySQL的数据库引擎处理的.当有很多相同的查询被执行了多次的时候,这些查询结果会被放到一 ...
- Git——github基本操作
基本概念 上一篇文章写到git共享仓库,但是有个局限性,就是这个仓库存在于本地,其他人无法从我们这个仓库拿到共享的内容 但是我们可以将这个共享仓库放入一个远程的服务器上,然后设置一些登录权限就能完美的 ...
- Exception Information
https://developer.apple.com/library/content/technotes/tn2004/tn2123.html Exception Information The t ...
- Leetcode加一 (java、python3)
加一 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. Given ...
- Ubuntu终端常用的快捷键(转载)
本文转自:https://www.cnblogs.com/nucdy/p/5251659.html 侵删 Ubuntu中的许多操作在终端(Terminal)中十分的快捷,记住一些快捷键的操作更得心应 ...
- <MyBatis>入门五 查询的返回值处理
select : 返回对象: <select id = " " resultType= "对象的全类名" /> List: <sele ...
- 修改虚拟机中Linux的IP
联网:ifup eth0 查看ip:ifconfig 点击编辑,选择NAT,子网ip修改第三字段为25,确定,重启linux后,会自动分配字段为25的ip 或者,也可以修改为自己想要的ip,如图:进入 ...
- Mac 当xampp里mysql无法启动的解决办法
sudo /Applications/XAMPP/xamppfiles/bin/mysql.server start