HDU 5654 Clarke and problem(动态规划)

题目链接:

  戳我

题目大意:

http://bestcoder.hdu.edu.cn/contests/contest_chineseproblem.php?cid=631&pid=1002

问题描述
克拉克是一名人格分裂患者。某一天,克拉克分裂成了一个学生,在做题。 
突然一道难题难到了克拉克,这道题是这样的:  
给你nn个数,要求选一些数(可以不选),把它们加起来,使得和恰好是pp的倍数(00也是pp的倍数),求方案数。 对于nn很小的时候,克拉克是能轻易找到的。然而对于nn很大的时候,克拉克没有办法了,所以来求助于你。 
输入描述
第一行一个整数T(1 \le T \le 10)T(1T10),表示数据的组数。 每组数据第一行是两个正整数n, p(1 \le n, p \le 1000)n,p(1n,p1000)。 接下来的一行有nn个整数a_i(|a_i| \le 10^9)ai​​(ai​​109​​),表示第ii个数。
输出描述
对于每组数据,输出一个整数,表示问题的方案数,由于答案很大,所以求出对10^9+7109​​+7的答案即可。 

样例解释:

解题思路:

 设 dp[i][j] 代表 前 i 个数 除模余 j 的 个数, 则 dp[0][0] = 1。

dp[i][j]  = dp[i-1][j] + dp[i-1][t], t必须满足(t + a[i]) % p == j

代码:

//Author LJH
//www.cnblogs.com/tenlee
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#define clc(a, b) memset(a, b, sizeof(a))
#define LL long long
using namespace std;

const int inf = 0x3f;
const int INF = 0x3f3f3f3f;
const int maxn = 1e3+5;
const int M = 1e9+7;
int dp[maxn][maxn], a[maxn], n, p;

int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d %d", &n, &p);
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
        }
        clc(dp, 0);
        dp[0][0] = 1;
        for(int i = 1; i <= n; i++)
        {
            int t = ((p - a[i]) % p + p) % p;
            for(int j = 0; j < p; j++)
            {
                dp[i][j] = (dp[i-1][j] + dp[i-1][t]) % M;
                t = (t + 1) % p;
            }
        }
        printf("%d\n", dp[n][0]);
    }
    return 0;
}

  

 

转载于:https://www.cnblogs.com/tenlee/p/4836495.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值