HDU 5389 Zero Escape
题意:有一些人,每人拿一个号码,有两个门,门的值分别为A和B,要求把人分成两堆(可以为空)一堆人手持号码之和的数字根若等于A或者B就可以进入A门或者B门,要求两堆人分别进入不同的门,求有几种分配方式,如果A和B的值相等则算两种。
解法:dp。比赛的时候并不知道一个数%9+1就是数字根……煞费苦心的算了半天……只得出结论a+b的数字根等于a的数字根+b的数字根的数字根……没打表……于是T了……然后打了个表才过orz
设dp[i][j]表示用前i个人获得数字根为j的和的方案数,打一个表vector<int> tab[i][j]表示和j相加的数字根等于i的数。则可以得到方程dp[i][j] = dp[i - 1][j] + ∑dp[i - 1][tab[j][a[i]][k]],dp[i - 1][j]表示不选第i个数,后面的和表示选第i个数时可以转移来的所有状态之和。
答案即为dp[n][A],但是当A和B相等时,多出一种A选0个人的方案,需要特判。
代码:
因为一开始没想到上面那个特判……想了好多乱七八糟的特判= =代码好乱……【无视无视
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <limits.h>
using namespace std;
typedef long long LL;
const LL mod = 258280327;
int t, n, A, B;
int get_num(int x)
{
int tmp = x;
while(tmp >= 10)
{
x = tmp;
tmp = 0;
while(x)
{
tmp += x % 10;
x /= 10;
}
}
return tmp;
}
vector <int> tab[10][10];
void init()
{
for(int i = 1; i <= 9; i++)
for(int j = 1; j <= 9; j++)
for(int k = 0; k <= 9; k++)
{
if(get_num(k + j) == i)
tab[i][j].push_back(k);
}
}
LL dp[100005][11];
LL a[100005];
int main()
{
init();
scanf("%d", &t);
while(t--)
{
scanf("%d%d%d", &n, &A, &B);
memset(dp, 0, sizeof dp);
LL sum = 0;
for(int i = 1; i <= n; i++)
{
scanf("%lld", &a[i]);
sum += a[i];
}
for(int i = 0; i <= n; i++)
dp[i][0] = 1;
LL ans = 0;
int tmp = get_num(sum);
if(tmp != get_num(A + B))
{
if(tmp == A && tmp == B)
{
printf("2\n");
continue;
}
else if(tmp == A)
{
printf("1\n");
continue;
}
else if(tmp == B)
{
printf("1Zn");
continue;
}
else
{
printf("0\n");
continue;
}
}
if(sum == A)
{
if(sum == B)
{
printf("2\n");
continue;
}
else
{
printf("1\n");
continue;
}
}
else if(sum == B)
{
printf("1\n");
continue;
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= 9; j++)
{
dp[i][j] += dp[i - 1][j];
int len = tab[j][a[i]].size();
for(int k = 0; k < len; k++)
{
dp[i][j] += dp[i - 1][tab[j][a[i]][k]];
if(dp[i][j] > mod)
dp[i][j] -= mod;
}
}
}
ans = dp[n][A];
if(get_num(sum) == B)
ans++;
printf("%lld\n", ans);
}
return 0;
}
HDU 5389 Zero Escape的更多相关文章
- HDU 5389 Zero Escape(dp啊 多校)
题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5389 Problem Description Zero Escape, is a visual no ...
- hdu 5389 Zero Escape (dp)
题目:http://acm.hdu.edu.cn/showproblem.php? pid=5389 题意:定义数根:①把每一位上的数字加起来得到一个新的数,②反复①直到得到的数仅仅有1位.给定n,A ...
- HDU 5389 Zero Escape(DP + 滚动数组)
Zero Escape Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) To ...
- hdu 5389 Zero Escape(记忆化搜索)
Problem Description Zero Escape, is a visual novel adventure video game directed by Kotaro Uchikoshi ...
- 2015 Multi-University Training Contest 8 hdu 5389 Zero Escape
Zero Escape Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- HDU 5389 Zero Escape (MUT#8 dp优化)
[题目链接]:pid=5389">click here~~ [题目大意]: 题意: 给出n个人的id,有两个门,每一个门有一个标号,我们记作a和b,如今我们要将n个人分成两组,进入两个 ...
- 递推DP HDOJ 5389 Zero Escape
题目传送门 /* 题意:把N个数分成两组,一组加起来是A,一组加起来是B,1<=A,B<=9,也可以全分到同一组.其中加是按照他给的规则加,就是一位一位加,超过一位数了再拆分成一位一位加. ...
- hdoj 5389 Zero Escape
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5389 大体题意是:有两个门A和B,还有一群人,每个人都有一个数字, 疯了一样的T..比赛的时候十连T也 ...
- HDU 3605:Escape(最大流+状态压缩)
http://acm.hdu.edu.cn/showproblem.php?pid=3605 题意:有n个人要去到m个星球上,这n个人每个人对m个星球有一个选择,即愿不愿意去,"Y" ...
随机推荐
- POJ 1417 True Liars(种类并查集+dp背包问题)
题目大意: 一共有p1+p2个人,分成两组,一组p1,一组p2.给出N个条件,格式如下: x y yes表示x和y分到同一组,即同是好人或者同是坏人. x y no表示x和y分到不同组,一个为好人,一 ...
- SDUT1500 Message Flood
以前做过的用的字典树,可是貌似现在再用超内存....求解释... 问了LYN用的map函数做的,又去小小的学了map函数.... http://wenku.baidu.com/view/0b08cec ...
- ubuntu安装google 输入法
12.04 LTS Precise sudo apt-get install ibus ibus-clutter ibus-gtk ibus-gtk3 ibus-qt4 sudo apt-get in ...
- 套题T2
数学(math.cpp) DXY的数学很差... 对于所有1<=i<=N求(2^i – i^2)能被7整除的个数.(N<=1000000) 样例输入: 3 样例输出: 1 你在代码中 ...
- stringgird中使用TClientDataSet排序的问题
function TfrmMain.createIIReport(cdsBody: TClientDataSet; silent: Boolean): String;var s,sText: ...
- python学习[一]
Vamei写了很好的python教程,感谢:http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html 摘录笔记 print命令行模式: ...
- 一个简单的将GUI程序的log信息输出到关联的Console窗口中(AllocConsole SetConsoleTitle WriteConsole 最后用ShowWindow(GetConsoleWindow)进行显示)
// .h 文件 #pragma once class CConsoleDump { public: explicit CConsoleDump(LPCTSTR lpszWindowTitle = N ...
- PHP文件下载原理
1.php下载原理图 2.文件下载源码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <?php $ ...
- jQuery 表格排序插件 Tablesorter 使用
jQuery 表格排序插件 Tablesorter 使用方式如下: 1.引入头文件(注意一定要把jQuery放在前面): <script src="lib/jquery-1.8.3.m ...
- 用paint 计算字符串的像素宽度
方法1: //直接返回参数字符串所占用的像素宽度 Paint paint = new Paint(); width = paint.measureText(str); 有一些view可以直接得到pai ...