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.
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
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数组来记录状态,采用记忆化搜索来大大缩短运行时间,具体看代码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 #pragma comment(linker, "/STACK:1024000000,1024000000") 6 #define N 100006 7 #define M 16 8 #define MOD 258280327 9 int n,A,B; 10 int dp[N][M]; 11 int a[N]; 12 int sum; 13 int dfs(int cur,int suma,int now) 14 { 15 if(dp[cur][suma]!=-1) return dp[cur][suma]; 16 if(cur>n) 17 { 18 if(now==0) 19 { 20 return (sum-now)%9==B; 21 } 22 if(now==sum) 23 { 24 return suma == A; 25 } 26 return suma==A && (sum-now)%9==B; 27 } 28 29 dp[cur][suma]=dfs(cur+1,(suma+a[cur])%9,now+a[cur])+dfs(cur+1,suma,now); 30 return dp[cur][suma]%=MOD; 31 } 32 int main() 33 { 34 int t; 35 scanf("%d",&t); 36 while(t--) 37 { 38 scanf("%d%d%d",&n,&A,&B); 39 if(A==9) 40 A=0; 41 if(B==9) 42 B=0; 43 sum=0; 44 for(int i=1;i<=n;i++) 45 { 46 scanf("%d",&a[i]); 47 sum+=a[i]; 48 } 49 50 memset(dp,-1,sizeof(dp)); 51 printf("%d\n",dfs(1,0,0)); 52 } 53 return 0; 54 }