hdu 5389 Zero Escape 2015多校联合训练赛#8 动态规划

Zero Escape

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


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=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.
T100 n105 n106 1A,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
2015 Multi-University Training Contest 8

对于一个有根数:所有位置上的数字之和%9就是最后的有根数。=========因此一个数字无论是先加或者后加的,都没有什么影响


定义dp[i][j]为加入A集合的有根数得到的值为i,加入集合B的有根数得到的值为j

对于输入数字x转移为  dp[i+x][j] += dp[i][j], dp[i][j+x] += dp[i][j]

最后特殊处理全部加入A或者加入B集合中的情况;


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define ll int
ll mod = 258280327;

ll dp[2][10][10];
int mp[30];

int main(){
    for(int i = 0;i < 20; i++)
        mp[i] = i%10 + i/10;
    int t;
    scanf("%d",&t);
    while(t--){
        int p = 0, q = 1;
        memset(dp,0,sizeof(dp));
        dp[p][0][0] = 1;
        int a,b,n,u,v;
        scanf("%d%d%d",&n,&a,&b);
        int t = 0;
        while(n--){
            memset(dp[q],0,sizeof(dp[q]));
            scanf("%d",&u);
            t = mp[t+u];
            for(int i = 0;i < 10; i++){
                for(int j = 0;j < 10; j++){
                    v = mp[i+u];
                    dp[q][v][j] += dp[p][i][j];
                    if(dp[q][v][j] >= mod)
                        dp[q][v][j] -= mod;

                    v = mp[j+u];
                    dp[q][i][v] += dp[p][i][j];
                    if(dp[q][i][v] >= mod)
                        dp[q][i][v] -= mod;
                }
            }
            swap(p,q);
        }
        ll ans = dp[p][a][b];
        if(t == a) ans++;
        if(t == b) ans++;
        ans %= mod;
        printf("%I64d\n",ans);
    }
    return 0;
}





你可能感兴趣的:(动态规划,HDU,5389,2015多校联合训练赛#8)