码迷,mamicode.com
首页 > 编程语言 > 详细

2015多校.Zero Escape (dp减枝 && 滚动数组)

时间:2015-08-13 22:10:06      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

Zero Escape

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 289    Accepted Submission(s): 135


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
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
 题目本身就这样吧,归纳一下得到结论后就可以dp直接上了。
在比赛时没仔细看(有队友,hhhh),赛后补得时候莫名tle,23333
然后就知道dp是可以剪枝的,剪了跑的话,妥妥的。
技术分享
 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <algorithm>
 4 using namespace std;
 5 const int M = 1e5 + 10 , mod = 258280327 ;
 6 int n , A , B ;
 7 int a[M] ;
 8 int dp[2][10][10] ;
 9 int get (int x) {
10         return x/10 + x%10 ;
11 }
12 
13 //void solve () {
14 //        int cur = 0, nxt = 1;
15 //        dp[0][0][0] = 1 ;
16 //        for(int i = 1 ; i <= n ; i ++) {
17 //                 for (int j = 0 ; j < 10 ; j ++) {
18 //                         for (int k = 0 ; k < 10 ; k ++) if (dp[i - 1][j][k] > 0) {
19 //                                 dp[i][ get(j+a[i]) ][k] = (dp[i][ get(j+a[i]) ][k] + dp[i-1][j][k]) % mod ;
20 //                                 dp[i][j][ get(k+a[i]) ] = (dp[i][j][ get(k+a[i]) ] + dp[i-1][j][k]) % mod ;
21 //                         }
22 //                 }
23 //        }
24 //        //cout << dp[n][A][B] << " " << dp[n][A][0] << " " << dp[n][0][B] << endl ;
25 //        printf ("%d\n" , ((dp[n][A][B] + dp[n][A][0]) % mod + dp[n][0][B]) % mod) ;
26 //}
27 
28 void add (int &x , int y) {
29         x += y ;
30         if (x > mod) x -= mod ;
31 }
32 
33 void solve () {
34         int cur = 0 , nxt = 1 ;
35         memset (dp[cur] , 0 , sizeof(dp[cur])) ;
36         dp[cur][0][0] = 1 ;
37         for (int i = 1 ; i <= n ; i ++) {
38                 memset (dp[nxt] , 0 , sizeof(dp[nxt])) ;
39                 for (int j = 0 ; j < 10 ; j ++) {
40                         for (int k = 0 ; k < 10 ; k ++) {
41                                 if (dp[cur][j][k] == 0) continue ;
42                                 add (dp[nxt][ get(j+a[i]) ][k] , dp[cur][j][k])  ;
43                                 add (dp[nxt][j][ get(k+a[i]) ] , dp[cur][j][k])  ;
44                         }
45                 }
46                 swap (cur , nxt) ;
47         }
48         printf ("%d\n" , ((dp[cur][A][B] + dp[cur][A][0]) % mod + dp[cur][0][B]) % mod) ;
49 }
50 
51 int main () {
52         int T ;
53         scanf ("%d" , &T ) ;
54         while (T --) {
55                 scanf ("%d%d%d" , &n , &A , &B) ;
56                 for (int i = 1 ; i <= n ; i ++) {
57                         scanf ("%d" , &a[i]) ;
58                         //memset (dp[i] , 0 , sizeof(dp[i])) ;
59                 }
60                 a[0] = 0 ;
61                 solve () ;
62         }
63         return 0 ;
64 }
View Code

 

2015多校.Zero Escape (dp减枝 && 滚动数组)

标签:

原文地址:http://www.cnblogs.com/get-an-AC-everyday/p/4728446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!