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 ;
}

最新文章

  1. asp.net core项目发布网站时的选项
  2. JavaScript中的splice方法
  3. 在VS2013中使用Log4net
  4. 《BI那点儿事》Microsoft 神经网络算法
  5. SQL集合操作符样例(UNION,INTERSECT,EXCEPT)
  6. 组合数学 - 母函数的运用 --- hdu 1709 :The Balance
  7. Android一 流
  8. ODAC的安装以及Entity Framework for Oracle 基本配置
  9. oralce dubugs
  10. php不同版本特性记录
  11. &lt;转载&gt;如何解决子级用float浮动父级div高度不能自适应的问题
  12. Playrix Codescapes Cup (Codeforces Round #413, rated, Div. 1 + Div. 2)(A.暴力,B.优先队列,C.dp乱搞)
  13. Flash Media Live Encoder 使用帮助
  14. JetBrain系列IDE提示Filesystem Case-Sensitivity Mismatch的解决
  15. [Oracle维护工程师手记]为什么flashback 的时候既需要 flashback log ,又需要 archive log?
  16. Python 游戏之旅(Pygame)
  17. Office Web Apps 2013 修改Excel在线查看文件大小限制
  18. 用jQuery修改右键菜单
  19. Email移动的原理
  20. linux 守护进程 daemon

热门文章

  1. Android 交错 GridView
  2. i利用图片按钮 和 input type=&quot;image&quot; 为背景提交表单
  3. Volley 百财帮封装
  4. FineUI初学手册
  5. 获取android手机联系人信息
  6. MySQL MyISAM/InnoDB高并发优化经验
  7. Android 5.1 Camera 架构学习之Camera初始化
  8. DHCP租约时间工作原理
  9. 动态IP无法获取默认网关,显示0.0.0.0的解决办法
  10. Java反射 - 3(动态代理)