hdu 5389 Zero Escape

传送门

Zero Escape

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

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

Author

SXYZ

Source

2015 Multi-University Training Contest 8

16464431 2016-03-07 10:54:36 Accepted 5389 483MS 6260K 1168 B G++ czy

题解:

先转一发题解:http://blog.csdn.net/david_jett/article/details/47617099

解题:

比赛的时候是水过的,不得不吐槽下多校的数据真水,听说样例都没过也能A。之前发了错误的题解,实在是抱歉,现在改正了。之前大致思路是对的:

dp[i][j]=dp[i-1][j]+dp[i-1][tmp](tmp为j-arr[i],若小于等于0,需加9调整)

dp[i][j]含义为取前i个数字中的若干个,按给定规则,算出来的和为j的方案数

之前有两个细节没考虑到,

一是、当前位和我dp[i][j]中的j值相同,那么对应三种情况。

1.当前位不取,直接取前面dp[i-1][j]的值

2.当前位取,前面取的是dp[i-1][9]的值(因为加9,不变)

3.当前位取,前面都不取,此种情况特殊,直接加一即可。

其实2、3两种情况是一个数分别对应dp[i-1][0]和dp[i-1][9]的特殊情况。

二是、当a+b的和和sum相同时,最后输出结果的时候,按理说只要输出dp[n][a]就可以了,但是这样会遗漏a中都不取,全都放在b中,而b又刚好和sum相同的情况。

现在是没有错误了,请放心看

注意点:

开始想到dp了,不过傻傻的用了3维dp,结果超时了,没注意到一部分人进了A门,那么剩下的人肯定要进B门,所以第三维可以省掉。

转移方程:

    for(o = 1;o <= n;o++){
        for(i = 0;i <= 9;i++){
            t = (9+i-x[o]) % 9;
            dp[o][i] = (dp[o-1][t] + dp[o-1][i]) %mod;
        }
    }

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <stack>
 6 #include <cctype>
 7 #include <vector>
 8 #include <cmath>
 9 #include <map>
10 #include <queue>
11
12 #define ll long long
13 #define N 100005
14 #define mod 258280327
15
16 using namespace std;
17
18 int T;
19 int n,a,b;
20 int x[N];
21 int dp[N][11];
22 int ans;
23 int s;
24
25 int root(int x) {return (x-1)%9+1;}
26
27 void solve()
28 {
29     int i,o;
30     int t;
31     dp[0][0] = 1;
32     for(o = 1;o <= n;o++){
33         for(i = 0;i <= 9;i++){
34             t = (9+i-x[o]) % 9;
35             dp[o][i] = (dp[o-1][t] + dp[o-1][i]) %mod;
36         }
37     }
38 }
39
40 int main()
41 {
42     //freopen("in.txt","r",stdin);
43     scanf("%d",&T);
44     for(int ccnt=1;ccnt<=T;ccnt++){
45     //while(scanf("%d%s%s",&n,a,b)!=EOF){
46         scanf("%d%d%d",&n,&a,&b);
47         memset(dp,0,sizeof(dp));
48         int i;
49         s = 0;
50         for(i = 1;i <= n;i++){
51             scanf("%d",&x[i]);
52             s = root(s+x[i]);
53         }
54         solve();
55         ans = 0;
56         if(root(a+b)==s) ans+=dp[n][a];
57         else if(a==s) ++ans;
58         if(b==s) ++ans;
59         printf("%d\n",ans%mod);
60     }
61     return 0;
62 }
时间: 2024-10-05 15:19:53

hdu 5389 Zero Escape的相关文章

HDU 5389 Zero Escape(dp啊 多校啊 )

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5389 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

HDU 5389 Zero Escape(dp啊 多校)

题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=5389 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 o

HDU 5389 Zero Escape(dp解法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5389 题面: Zero Escape Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 164    Accepted Submission(s): 73 Problem Description Zero Escape, is a vis

HDU 5389 Zero Escape (类0/1背包)

Zero Escape Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 883    Accepted Submission(s): 452 Problem Description Zero Escape, is a visual novel adventure video game directed by Kotaro Uchik

hdu 5389 Zero Escape (dp)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5389 题意:定义数根:①把每一位上的数字加起来得到一个新的数,②重复①直到得到的数只有1位.给定n,A,B和n个一位数,求把这n个数分成两部分,使得这两部分的其中一部分的和的数根等于A另外一部分的和的数根等于B的方案数. 分析:一个数a的数根s=(a-1)%9+1,为了方便直接用s=a%9,其中0代表9.定义dp[i][j]表示前i个数中数根为j的方案数.对于第i个数可以选也可以不选,那么状态转移方程为

HDU 5389 Zero Escape(DP + 滚动数组)

Zero Escape Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 864    Accepted Submission(s): 438 Problem Description Zero Escape, is a visual novel adventure video game directed by Kotaro Uchik

hdu(5389)——Zero Escape(01背包变形)

啊,这是一道挺不错的动态规划的题目呢. 一开始我们并没有找出规律,后来队友突然喊了一句:"dp啊!" 顿时人生豁然开朗了... 于是她很神奇的推出了转移方程,然后再共同查错下,a出了这道题,真是佩服这人的高智商啊 题意: 首先n,代表的是n个人,然后是A,B,分别代表两个门的值. 然后第二行给出了n个人的价值,问你要使这n个人全部进入门中(可以是A,B,或是其中的一扇门),总共有几种方法. 但是要注意这里的价值并不是仅仅加起来就好了,这里是要把它加起来直到为一个个位数. 思路: 定义:

HDU 5389 Zero Escape (MUT#8 dp优化)

[题目链接]:click here~~ [题目大意]: 题意: 给出n个人的id,有两个门,每个门有一个标号,我们记作a和b,现在我们要将n个人分成两组,进入两个门中,使得两部分人的标号的和(迭代的求,直至变成一位数,我们姑且叫做求"和"操作~)分别等于a和b,问有多少种分法. [思路]:比赛的时候还是学弟递推的方程,当时是dp三维dp[i][j]k]:分别表示枚举到第i位,A门,B门的状态,但是一直没想到怎么进一步优化,卡在100n的复杂度了 赛后看了一下题解,(虽然高中生写的题解看

HDU 5389 Zero Escape(2015年多校联合第八场 动态规划)

题目连接:传送门 题意: 讲一个长度为n的序列分为两组,使得一组的和为A,一组的和为B. 注意这里的和定义为这些数的和的数根. 一个数的数根的计算公式为,root = (x-1)%9+1; 很明显一个正整数的数根是1~9的分析,如果这n个数的数根分成两组使得 一组的数根为A,一组的数根为B那么这两组的数的和的数根等于(A+B)的 数根.因此我们只需要考虑组成其中一个数的情况,然后再最后进行一个 判断即可我们设dp[i][j]表示前i个数组成的数根为j的数目. 注意其中任意一组可以为空. 代码如下