HDU 5389 Zero Escape

来源:互联网 发布:客户数据库 编辑:程序博客网 时间:2024/06/05 11:06

Zero Escape


Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)


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(1X9), 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=9B=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 nA and B.
Next line contains n integers idi, describing the identifier of every player.
T100n105n1061A,B,idi9
 

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
43 9 11 2 63 9 12 3 35 2 31 1 1 1 19 9 91 2 3 4 5 6 7 8 9
 

Sample Output
101060

思路:
 用动态规划,因为各位数总和是能是1~9,这里用dp[i]][j]表示前i个数总和为j的所有可能数(这个和是个位数)。
注意:
 (X+Y)% 9 = (X%9 + Y%9) % 9;当值为0时置为9。
  转移方程:

  dp[i][j] = dp[i-1][j] + dp[i-1][j-id[i]]  (j-id[i] >= 0)
  dp[i][j] = dp[i-1][j] + dp[i-1][j-id[i]+9]  (j-id[i] < 0)
注意最终如果A+B != SUM,则答案为0,否则只要考虑dp[n][A],因为剩下的都得放到B里面,还要判断一下A、B和SUM的关系。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <bits/stdc++.h>  
  2. #define endl "\n"  
  3. using namespace std;  
  4. const int MAXN = 1e5 + 7;  
  5. const int MOD = 258280327;  
  6. int id[MAXN];  
  7. int dp[MAXN][10];  
  8.   
  9. void Init(int n) {  
  10.     for(int i = 0; i < n; ++i) {  
  11.         for(int j = 0; j < 10; ++j) {  
  12.             dp[i][j] = 0;  
  13.         }  
  14.     }  
  15. }  
  16.   
  17. int main()  
  18. {  
  19.     ios::sync_with_stdio(false);  
  20.     int T, n, A, B;  
  21.     cin >> T;  
  22.     while(T--) {  
  23.         cin >> n >> A >> B;  
  24.         Init(n);  
  25.         int sum = 0;  
  26.         for(int i = 1; i <= n; ++i) {  
  27.             cin >> id[i];  
  28.             sum += id[i];  
  29.         }  
  30.         sum = sum % 9 ? sum % 9 : 9;  
  31.         int ret = (A+B) % 9 ? (A+B) % 9 : 9;  
  32.         int res = 0;  
  33.         if(sum == B) res++;  
  34.         dp[0][0] = 1;  
  35.         if(sum == ret) {  
  36.             for(int i = 1; i <= n; ++i) {  
  37.                 for(int j = 0; j < 10; ++j) {  
  38.                     if(j-id[i] >= 0) dp[i][j] = dp[i-1][j] + dp[i-1][j-id[i]];  
  39.                     else dp[i][j] = dp[i-1][j] + dp[i-1][j-id[i]+9];  
  40.                     dp[i][j] %= MOD;  
  41.                 }  
  42.             }  
  43.             cout << (dp[n][A] + res) % MOD << endl;  
  44.         }  
  45.         else {  
  46.             if(sum == A) ++res;  
  47.             cout << res << endl;  
  48.         }  
  49.     }  
  50.     return 0;  
  51. }  
0 0
原创粉丝点击