HDU 5464 Clarke and problem(DP 01背包)

本文探讨了多重个性障碍患者在阅读过程中遇到的问题,并提供了一种解决方案,通过选择序列中的数字并计算其和是否能被某个特定数字整除来找到有效组合。此方法适用于编程领域,特别适用于Java和其他通用语言的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Clarke and problem
Time Limit 20001000 MS (JavaOthers)    Memory Limit 6553665536 K (JavaOthers)
Total Submission(s) 400    Accepted Submission(s) 179




Problem Description
Clarke is a patient with multiple personality disorder. One day, Clarke turned into a student and read a book.
Suddenly, a difficult problem appears 
You are given a sequence of number a1,a2,...,an and a number p. Count the number of the way to choose some of number(choose none of them is also a solution) from the sequence that sum of the numbers is a multiple of p(0 is also count as a multiple of p). Since the answer is very large, you only need to output the answer modulo 109+7


 


Input
The first line contains one integer T(1≤T≤10) - the number of test cases. 
T test cases follow. 
The first line contains two positive integers n,p(1≤n,p≤1000) 
The second line contains n integers a1,a2,...an(ai≤109). 
 


Output
For each testcase print a integer, the answer.
 


Sample Input
1
2 3
1 2
 


Sample Output
2


Hint
2 choice choose none and choose all.
 


Source

BestCoder Round #56 (div.2) 


/*01背包DP*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <string.h>
using namespace std;
const int N=1000+10;
const int mod=1e9+7;
int num[N];
int p,n;
int dp[N][N];
int main(){
	int t,i,j;
	scanf("%d",&t);
	while(t--){
		memset(dp,0,sizeof(dp));
		dp[0][0]=1;
		scanf("%d%d",&n,&p);
		for(i=1;i<=n;i++){
			scanf("%d",&num[i]);
			num[i]%=p;
			//这里被刘大哥指明...  处理负数的  将他变成整数 
			num[i]=(num[i]+p)%p;   
		}		  
	    for(i=1;i<=n;i++){   //枚举每个数拿还是不拿 
	    	for(j=0;j<p;j++){   //枚举余数 
	    	 //不拿  就是余数为j加上i-1余数为j的数量 
	    	   dp[i][j]=(dp[i][j]+dp[i-1][j])%mod;   
			//拿就 是余数为拿后的加上前I-1余数为拿前的 
			dp[i][(j+num[i])%p]=(dp[i][(j+num[i])%p]+dp[i-1][j])%mod;
			}
		}
		printf("%d\n",dp[n][0]);
	}
    return 0;	
}





/*   暴搜超时 */
/*
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
using namespace std;
const int N=1000+10;
const int mod=1000000000+7;
int num[N];
int p,n;
int re;
void dfs(int now,int sum){
	int i;
	if(sum%p==0)
	  {
	  	re++;
	  	re%=mod;
	  	return;
	  }
	else if(sum>p||now>=n)
	 return;	
	dfs(now+1,sum+num[now]);
	dfs(now+1,sum);
}
int main(){
	int t,i;
	scanf("%d",&t);
	while(t--){
		re=1;
		scanf("%d%d",&n,&p);
		for(i=0;i<n;i++)
		  scanf("%d",&num[i]);
		sort(num,num+n);
		dfs(0,0);
		printf("%d\n",re);
	}
    return 0;	
}
*/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值