当前位置: 代码迷 >> 综合 >> LightOJ 1125 Divisible Group Sums (简单DP)
  详细解决方案

LightOJ 1125 Divisible Group Sums (简单DP)

热度:36   发布时间:2023-11-15 12:46:46.0

题目链接:http://lightoj.com/volume_showproblem.php?problem=1125

题目大意:

给定n个数,要求选m个数使得
其和被k整除,问选法的个数。

题目分析: 

题目分析:由于范围很小,直径二暴力DP即可,
但还要注意序列中的数可能为负数的情况,取模的时候再注意下即可。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=1e3+1;
const int ub=1e6;
const double inf=5e-4;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
/*
题目大意:给定n个数,要求选m个数使得
其和被k整除,问选法的个数。题目分析:由于范围很小,直径二暴力DP即可,
但还要注意序列中的数可能为负数的情况,取模的时候再注意下即可。
*/
ll n,m,x,y,a[maxn];
ll dp[maxn][30][30];///dp数组
int Mod(ll p){p%=x;if(p<0) p+=x;return p;
}
int main(){int t;scanf("%d",&t);for(int ca=1;ca<=t;ca++){scanf("%lld%lld",&n,&m);rep(i,1,n+1) scanf("%lld",&a[i]);printf("Case %d:\n",ca);while(m--){scanf("%lld%lld",&x,&y);mst(dp,0),dp[0][0][0]=1;rep(i,0,n) rep(j,0,y) rep(k,0,x){dp[i+1][j+1][Mod(1LL*k+a[i+1])]+=dp[i][j][k];dp[i+1][j][k]+=dp[i][j][k];}ll ans=0;rep(i,1,n+1) ans+=1LL*dp[i][y][0];printf("%lld\n",ans);}}return 0;
}

 

  相关解决方案