HDU 5389 Zero Escape(dp解法详解) 已更新

来源:互联网 发布:gom引擎npc算法 编辑:程序博客网 时间:2024/05/19 23:00

题目链接: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 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 numberedX(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 doorA, 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 thesen 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
 
解题:

    比赛的时候是水过的,不得不吐槽下多校的数据真水,听说样例都没过也能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相同的情况。

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

代码:

//按给定规则转化为个位数int modify(int x){    int res=0;    while(x)    {        res+=(x%10);        x/=10;    }    if(res>9)        return modify(res);    else        return res;}int main(){    int t,n,a,b,tmp,ans;    int sum,xx;    scanf("%d",&t);    while(t--)    {        sum=0;//初始化        memset(dp,0,sizeof(dp));        scanf("%d%d%d",&n,&a,&b);//读入        for(int i=1;i<=n;i++)            scanf("%d",&arr[i]);        for(int i=1;i<=n;i++)        {            sum+=arr[i];        }        sum=modify(sum);        xx=a+b;        xx=modify(xx);//看是否相等        if(sum==xx)        {          dp[1][arr[1]]=1;          for(int i=2;i<=n;i++)          {  dp[i][0]=1;              for(int j=1;j<=9;j++)              {                  tmp=j-arr[i];                  if(tmp<0)  {tmp+=9;//当前位取,那么就由之前的对应推过来,当前位不取,那么就是之前的相同的    dp[i][j]=(dp[i-1][tmp]+dp[i-1][j])%mod;  }  //如果当前位刚好和原数组相同,可以是如下三种情况(理解重点)  //1.当前位不取,那么方案数为之前一位的对应值  //2.也可以是前面一位为9,然后取当前位的情况  //3.也可以是前面都不取,只取当前位,故为1  else if(tmp==0)  dp[i][j]=(dp[i-1][9]+dp[i-1][j]+1)%mod;  elsedp[i][j]=(dp[i-1][j]+dp[i-1][tmp])%mod;              }          }  //有可能a中都不取,只取b,b和sum相等(很容易遗漏)  if(b==sum)    printf("%d\n",(dp[n][a]+1)%mod);  else  printf("%d\n",(dp[n][a]));       }//不相等,只可能单独在一扇门中       else       {           ans=0;           if(sum==a)              ans++;           if(sum==b)               ans++;           printf("%d\n",ans);       }    }    return 0;}


如有疑问,欢迎交流微笑


0 0
原创粉丝点击