hdu5389 Zero Escape(模拟+找规律+DP)

来源:互联网 发布:小说软件 编辑:程序博客网 时间:2024/05/09 21:24


Link: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): 553    Accepted Submission(s): 272


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
 

Source
2015 Multi-University Training Contest 8
 


编程思想:按题意模拟求数根。然后DP。

详细分析过程参考自: http://blog.csdn.net/david_jett/article/details/47617099 

状态转移方程为dp[i][j]=(dp[i-1][pre]+dp[i-1][j])%mod;其中pre为j-a[i],i是当前下标,j从1到9,若算出的pre小于等于0还需加上9。dp[i][j]的含义是到第i位为止,前i个数中的若干个加起来和为j的方案数,转移方程中的两项分别对应,当前i位取或不取。因为每个数字是由确定的两个数字组合加起来得到的,比如2,若其中一个为1,那么另外一个也一定为1。且存在这样的性质,若干个数,按给定的规则先加,或者分开加,再组合在一起,得到的结果是一样的。故可以先预先把所有的数加起来,若其和与A+B得到的结果相同,那么直接取dp[n][A]+dp[n][B]的值即可,若不同,那么只可以放在任意门中,分别检查一下即可。


AC code:

#include <algorithm>#include <iostream>#include <iomanip>#include <cstring>#include <climits>#include <complex>#include <fstream>#include <cassert>#include <cstdio>#include <bitset>#include <vector>#include <deque>#include <queue>#include <stack>#include <ctime>#include <set>#include <map>#include <cmath>#define LL long long#define MAXN 1000010 using namespace std;const int mod=258280327;LL dp[100010][10];int a[MAXN];LL sum,sum2,ans;int modify(LL x){int res=0;while(x){res+=x%10;x/=10;}while(res>=10){int temp=res;res=0;while(temp){res+=temp%10;temp/=10;}}return res;}int main(){int n,A,B,m,i,j,pre,t,cas;scanf("%d",&t);cas=0;while(t--){scanf("%d%d%d",&n,&A,&B);sum=0;ans=0;for(i=1;i<=n;i++){scanf("%d",&a[i]);sum+=a[i];}sum=modify(sum);sum2=modify(A+B);memset(dp,0,sizeof(dp));dp[1][a[1]]=1;for(i=2;i<=n;i++){for(j=1;j<=9;j++){pre=j-a[i];if(pre<=0)pre+=9;dp[i][j]=(dp[i-1][pre]+dp[i-1][j])%mod;}}if(sum==sum2){ans=(dp[n][A]+dp[n][B])%mod;printf("%I64d\n",ans);}else{if(sum==A)ans++;if(sum==B)ans++;printf("%I64d\n",ans);}}return 0;}


0 0
原创粉丝点击