hdu 5389 Zero Escape(dp)

给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 258280327
int 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值