Problem Description
Zero Escape, is a visual novel adventure video game directed by Kotaro Uchikoshi (you may hear about ever17?) and developed by Chunsoft.

Stilwell is enjoying the first chapter of this series, and in this chapter digital root is an important factor.

This is the definition of digital root on Wikipedia:
The digital root of a non-negative integer is the single digit value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached.
For example, the digital root of 65536 is 7, because 6+5+5+3+6=25 and 2+5=7.

In the game, every player has a special identifier. Maybe two players have the same identifier, but they are different players. If a group of players want to get into a door numbered X(1≤X≤9), the digital root of their identifier sum must be X.
For example, players {1,2,6} can get into the door 9, but players {2,3,3} can't.

There is two doors, numbered A and B. Maybe A=B, but they are two different door.
And there is n players, everyone must get into one of these two doors. Some players will get into the door A, and others will get into the door B.
For example: 
players are {1,2,6}, A=9, B=1
There is only one way to distribute the players: all players get into the door 9. Because there is no player to get into the door 1, the digital root limit of this door will be ignored.

Given the identifier of every player, please calculate how many kinds of methods are there, mod 258280327.

 
Input
The first line of the input contains a single number T, the number of test cases.
For each test case, the first line contains three integers n, A and B.
Next line contains n integers idi, describing the identifier of every player.
T≤100, n≤105, ∑n≤106, 1≤A,B,idi≤9
 
Output
For each test case, output a single integer in a single line, the number of ways that these n players can get into these two doors.
 
Sample Input
4
3 9 1
1 2 6
3 9 1
2 3 3
5 2 3
1 1 1 1 1
9 9 9
1 2 3 4 5 6 7 8 9
 
Sample Output
1
0
10
60
 
Source
 
首先注意到求数根的方法,其实将要求数根的这个数%9就可以轻松得到,只是一开始的时候要把A或B等于9改为0,这样就可以轻松求出数根了。
题目要求将每个数放入A或B,那么不妨将所有的方法都找出来,即每个数不是放入A就是放入B。
这道题用传统的dfs肯定会超时,所以用一个dp数组来记录状态,采用记忆化搜索来大大缩短运行时间,具体看代码
 
 
 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define N 100006
#define M 16
#define MOD 258280327
int n,A,B;
int dp[N][M];
int a[N];
int sum;
int dfs(int cur,int suma,int now)
{
if(dp[cur][suma]!=-) return dp[cur][suma];
if(cur>n)
{
if(now==)
{
return (sum-now)%==B;
}
if(now==sum)
{
return suma == A;
}
return suma==A && (sum-now)%==B;
} dp[cur][suma]=dfs(cur+,(suma+a[cur])%,now+a[cur])+dfs(cur+,suma,now);
return dp[cur][suma]%=MOD;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&n,&A,&B);
if(A==)
A=;
if(B==)
B=;
sum=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
} memset(dp,-,sizeof(dp));
printf("%d\n",dfs(,,));
}
return ;
}

hdu 5389 Zero Escape(记忆化搜索)的更多相关文章

  1. [HDU 1428]--漫步校园(记忆化搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1428 漫步校园 Time Limit: 2000/1000 MS (Java/Others)    M ...

  2. hdu 4597 Play Game(记忆化搜索)

    题目链接:hdu 4597 Play Game 题目大意:给出两堆牌,仅仅能从最上和最下取,然后两个人轮流取,都依照自己最优的策略.问说第一个人对多的分值. 解题思路:记忆化搜索,状态出来就很水,dp ...

  3. hdu 4856 Tunnels (记忆化搜索)

    Tunnels Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  4. HDU 4597 Play Game(记忆化搜索,深搜)

    题目 //传说中的记忆化搜索,好吧,就是用深搜//多做题吧,,这个解法是搜来的,蛮好理解的 //题目大意:给出两堆牌,只能从最上和最下取,然后两个人轮流取,都按照自己最优的策略,//问说第一个人对多的 ...

  5. HDU 5001 概率DP || 记忆化搜索

    2014 ACM/ICPC Asia Regional Anshan Online 给N个点,M条边组成的图,每一步能够从一个点走到相邻任一点,概率同样,问D步后没走到过每一个点的概率 概率DP  測 ...

  6. HDU 4597 Play Game (记忆化搜索博弈DP)

    题意 给出2*n个数,分两列放置,每列n个,现在alice和bob两个人依次从任意一列的对头或队尾哪一个数,alice先拿,且两个人都想拿最多,问alice最后能拿到数字总和的最大值是多少. 思路 4 ...

  7. HDU 3779 Railroad(记忆化搜索)

    Railroad Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Subm ...

  8. hdu 5535 Cake 构造+记忆化搜索

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=5355 题意:给定n与m,其中1<= n <= 1e5,2 <= m <= 10;问 ...

  9. HDU 1176 免费馅饼(记忆化搜索)

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

随机推荐

  1. 使用CButtonColumn自定义CGridiew里面的按钮

    参考 http://www.yiiwiki.com/post/8 使用yii做的项目,编辑的时候,使用到的是自带的 CButtonColumn , 现在需要在编辑的时候跳新页面,这时只需要这样做就可以 ...

  2. pen: Local Testing

    [踩点] * OLEViewer:查看 ActiveX 组件信息 [Fuzz] * Tools in This Article * COMRaider:ActiveX/ocx [utils] * Fi ...

  3. hdu 2203

    题意: 子串问题 水题,只要把母串*2,然后比较...... 感觉我好懒....没有自己写函数...... 反正我不是勤快的人......... AC代码: #include <iostream ...

  4. mockito学习笔记

    mockito http://mockito.github.io/mockito/docs/current/org/mockito/Mockito.html

  5. Linux 所有命令都用不了,只有cd exit能用

    原因: 在设置 java环境变量时,编辑profile文件没有写正确,导致在命令行下 ls等命令不能够识别.在命令行下打入下面这段就可以了export PATH=/usr/local/sbin:/us ...

  6. RTX管理器服务运行状态空白

    A)打开RTX管理器安装目录下的bin文件夹,运行convert.bat 右键计算机——管理——服务——找到以RTX开头的服务,按RTX_ConfigCenter.RTX_HTTPServer. RT ...

  7. php全局变量与局部变量中的使用

    <?php //error handler function $v1=10; function f1() { //这里是把局部变成全局变量,也就是创建一个和外部同名的局部变量并通过“引用”的方式 ...

  8. [转]《深度探索C++对象模型》读书笔记[一]

    前 言 Stanley B.Lippman1.   任何对象模型都需要的三种转换风味: ü        与编译器息息相关的转换 ü        语言语义转换 ü        程序代码和对象模型的 ...

  9. 你好,C++(24)好大一个箱子!5.1.1 函数的声明和定义

    第5章 用函数封装程序功能 在完成功能强大的工资程序V1.0之后,我们信心倍增,开始向C++世界的更深远处探索. 现在,我们可以用各种数据类型定义变量来表达问题中所涉及的各种数据:用操作符连接这些变量 ...

  10. 统计建模与R软件习题二答案

    # 习题2 # 2.1 x=c(1,2,3) y=c(4,5,6) e=c(rep(1,3)) z=2*x+y+e;z x%*%y # 若x,y如答案那样定义为矩阵,则不能用%*%,因为,维数不对应, ...