您的位置:首页 > 其它

HDU 5654 Clarke and problem(动态规划)

2015-09-24 20:50 239 查看

题目链接:

  戳我

题目大意:

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(1≤T≤10),表示数据的组数。
每组数据第一行是两个正整数n, p(1 \le n, p \le 1000)n,p(1≤n,p≤1000)。
接下来的一行有nn个整数a_i(|a_i| \le 10^9)a​i​​(∣a​i​​∣≤10​9​​),表示第ii个数。

输出描述
对于每组数据,输出一个整数,表示问题的方案数,由于答案很大,所以求出对10^9+710​9​​+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
[0]);
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: