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 ...
随机推荐
- Java设置全局热键——第三方包jintellitype实现
Java原生API并不支持为应用程序设置全局热键.要实现全局热键,需要用JNI方式实现,这就涉及到编写C/C++代码,这对于大多数不熟悉C/C++的javaer来说,有点困难.不过幸好,国外有人已经实 ...
- Android yuv转Bitmap
YuvImage image = new YuvImage(data, ImageFormat.NV21, size.width, size.height, null); if(image!=nu ...
- 字符流-缓冲区-自定义myBufferedReader
public class myBufferedReaderDemo { public static void main(String[] arg) throws IOException{ FileRe ...
- 三维卷积:全景图像Spherical CNNs(Code)
卷积神经网络(CNN)可以很好的处理二维平面图像的问题.然而,对球面图像进行处理需求日益增加.例如,对无人机.机器人.自动驾驶汽车.分子回归问题.全球天气和气候模型的全方位视觉处理问题. 将球形信号的 ...
- HDU_2955_Robberies_01背包
A - Robberies Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submi ...
- swift Hashable Equatable
/// You can use any type that conforms to the `Hashable` protocol in a set or /// as a dictionary ke ...
- 从整体上理解进程创建、可执行文件的加载和进程执行进程切换,重点理解分析fork、execve和进程切换
学号后三位<168> 原创作品转载请注明出处https://github.com/mengning/linuxkernel/ 1.分析fork函数对应的内核处理过程sys_clone,理解 ...
- HDU多校Round 6
Solved:2 rank:452 I. Werewolf 没有铁人 找铁狼 如果一个环中只有一条狼人边那个人就是铁狼 说铁狼是好人的人也是铁狼 #include <bits/stdc++.h& ...
- valgrind检查代码内存泄漏,5种内存泄漏情况
摘要: valgrind是linux下用于调试程序和查找内存泄露的常用工具.valgrind会报告5种内存泄露,"definitely lost", "indirectl ...
- xmpp 消息和好友上下线(3)
原始地址:XMPPFrameWork IOS 开发(四) 消息 //收到消息 - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XM ...