hdu 5389 Zero Escape(dp)

来源:互联网 发布:好的淘宝店铺推荐 编辑:程序博客网 时间:2024/05/20 18:55

给A、B还有n个数,问将这n个数分为两组,使得一组中所有数的和的数根为A,另一组为B,共有多少种方案。(其中一组中数的个数可以为0)


分析:

1、关于数根的计算:一个数x(x>0)的数根为:(x-1)%9+1

2、两个数的数根分别为x,y(x,y>9),则这两个数之和的数根为(x+y-1)%9+1

3、由于所有数都会被分到某一组,即如果一组数选定了,那么另一组的所有数的和的数根也相应地确定了。

4、根据以上几点,要判断是否能分为两组,只要判断A+B的树根是否等于这n个数之和的树根即可。如果相等,只需计算组成树根A(或者B)的方案数就可以了。

5、上面的计算结果并不包含将所有数都归为B组(或者A组)的方案数,还需要单独判断。



#include<iostream>#include<cstdio>#include<cmath>#include<map>#include<set>#include<algorithm>#include<queue>#include<stack>#include<cstdlib>#include<cstring>#include<vector>#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;typedef long long LL;typedef unsigned long long uLL;typedef __int64 LI;typedef unsigned __int64 uLI;typedef unsigned int uI;typedef double db;#define maxn 100005#define inf 0x3f3f3f3f#define mod 258280327int d[100005],dp[100001][10];int root(int x) {return (x-1)%9+1;}int main(){    int t,i,n,a,b,s,ans;    cin>>t;    while(t--)    {        scanf("%d%d%d",&n,&a,&b);        memset(dp,0,sizeof(dp));        dp[0][0]=1,s=ans=0;        for(i=1;i<=n;++i)        {            scanf("%d",&d[i]);            for(int j=0;j<10;++j)            {                dp[i][j]=(dp[i][j]+dp[i-1][j])%mod;                int x=root(j+d[i]);                dp[i][x]=(dp[i][x]+dp[i-1][j])%mod;            }            s=root(s+d[i]);        }        if(root(a+b)==s) ans+=dp[n][a];        else if(a==s) ++ans;        if(b==s) ++ans;        printf("%d\n",ans%mod);    }    return 0;}


0 0
原创粉丝点击