hdu 5389 Zero Escape(记忆化搜索)


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 of65536is7, because6+5+5+3+6=25and2+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 beX.
For example, players{1,2,6}can get into the door9, but players{2,3,3}can't.

There is two doors, numberedAandB. MaybeA=B, but they are two different door.
And there isnplayers, everyone must get into one of these two doors. Some players will get into the doorA, and others will get into the doorB.
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 door9. Because there is no player to get into the door1, 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,mod258280327.

Input
The first line of the input contains a single numberT, the number of test cases.
For each test case, the first line contains three integersn,AandB.
Next line containsnintegersidi, 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 thesenplayers 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就可以轻松得到,只是一开始的时候要把A或B等于9改为0,这样就可以轻松求出数根了。
题目要求将每个数放入A或B,那么不妨将所有的方法都找出来,即每个数不是放入A就是放入B。
这道题用传统的dfs肯定会超时,所以用一个dp数组来记录状态,采用记忆化搜索来大大缩短运行时间,具体看代码
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 #pragma comment(linker, "/STACK:1024000000,1024000000")
 6 #define N 100006
 7 #define M 16
 8 #define MOD 258280327
 9 int n,A,B;
10 int dp[N][M];
11 int a[N];
12 int sum;
13 int dfs(int cur,int suma,int now)
14 {
15     if(dp[cur][suma]!=-1) return dp[cur][suma];
16     if(cur>n)
17     {
18         if(now==0)
19         {
20             return (sum-now)%9==B;
21         }
22         if(now==sum)
23         {
24             return suma == A;
25         }
26         return suma==A && (sum-now)%9==B;
27     }
28     
29     dp[cur][suma]=dfs(cur+1,(suma+a[cur])%9,now+a[cur])+dfs(cur+1,suma,now);
30     return dp[cur][suma]%=MOD;
31 }
32 int main()
33 {
34     int t;
35     scanf("%d",&t);
36     while(t--)
37     {
38         scanf("%d%d%d",&n,&A,&B);
39         if(A==9)
40          A=0;
41         if(B==9)
42           B=0;
43         sum=0;
44         for(int i=1;i<=n;i++)
45         {
46             scanf("%d",&a[i]);
47             sum+=a[i];
48         }  
49           
50         memset(dp,-1,sizeof(dp));
51         printf("%d\n",dfs(1,0,0));
52     }
53     return 0;
54 }
View Code

优质内容筛选与推荐>>
1、阿里云服务器下安装配置phpMyAdmin
2、POJ 2366 Sacrament of the sum
3、JDBC连接MySQL 方法 实例及资料收集
4、Redlock:Redis分布式锁最牛逼的实现
5、2.3.4删除空白


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号





    联系我们

    欢迎来到TinyMind。

    关于TinyMind的内容或商务合作、网站建议,举报不良信息等均可联系我们。

    TinyMind客服邮箱:support@tinymind.net.cn