传送门1
传送门2
Dear Guo
I never forget the moment I met with you.You carefully asked me: “I have a very difficult problem. Can you teach me?”.I replied with a smile, “of course”.”I have n items, their weight was a[i]”,you said,”Let’s define f(i,j,k,l,m) to be the number of the subset of the weight of n items was m in total and has No.i and No.j items without No.k and No.l items.”“And then,” I asked.You said:”I want to know
Sincerely yours,
Liao
The first line of input contains an integer T(T≤15) indicating the number of test cases.
Each case contains 2 integers n,s(4≤n≤1000,1≤s≤1000) . The next line contains n numbers: a1,a2,…,an(1≤ai≤1000).
Each case print the only number — the number of her would modulo 109+7 (both Liao and Guo like the number).
2
4 4
1 2 3 4
4 4
1 2 3 4
8
8
给定 n 个数,其中选定若干数,这若干数的权值和为m,且这些数中没有下标为 i,j 的数,有下标为 k,l 的数的集合个数。
定义 dp[i][j][ii][jj] 表示前i个物品,总权值为j,已有ii个必选,jj必不选的方案数。
显然 i<=n,j<=s,0<=ii,jj<=2
当然对于当前一个状态,它有四种转移状态:
1. 选中当前的, 增加权值, 增加必 选个数。
2. 选中当前的, 增加权值,不增加必 选个数。
3. 不选中当前的,不增加权值, 增加必不选个数。
4. 不选中当前的,不增加权值,不增加必不选个数。
在 f(i,j,k,l,m) 中,因为 i,j 可以互换, l,k 也可以互换,故而最后方案数乘以4即为所求。
参考
#include
#include
#define mod 1000000007
#define N 1005
#define FOR(i,a,b) for(int i=(a),i##_END_=(b);i<=i##_END_;i++)
int dp[N][N][3][3],a[N];
int main() {
int T,n,s,tmp;
int ans;
scanf("%d",&T);
while(T--) {
ans=0;
scanf("%d%d",&n,&s);
FOR(i,1,n)scanf("%d",&a[i]);
memset(dp,0,sizeof dp);
dp[1][a[1]][0][0]=1;
dp[1][a[1]][1][0]=1;
dp[1][0][0][0]=1;
dp[1][0][0][1]=1;
FOR(i,2,n)FOR(j,0,s) {
tmp=j+a[i];
if(tmp<=s) { //卡常大法好
dp[i][tmp][0][0]=(dp[i][tmp][0][0]+dp[i-1][j][0][0])%mod;
dp[i][tmp][1][0]=(dp[i][tmp][1][0]+dp[i-1][j][0][0])%mod;
dp[i][tmp][1][0]=(dp[i][tmp][1][0]+dp[i-1][j][1][0])%mod;
dp[i][tmp][2][0]=(dp[i][tmp][2][0]+dp[i-1][j][1][0])%mod;
dp[i][tmp][2][0]=(dp[i][tmp][2][0]+dp[i-1][j][2][0])%mod;
dp[i][tmp][0][1]=(dp[i][tmp][0][1]+dp[i-1][j][0][1])%mod;
dp[i][tmp][1][1]=(dp[i][tmp][1][1]+dp[i-1][j][0][1])%mod;
dp[i][tmp][1][1]=(dp[i][tmp][1][1]+dp[i-1][j][1][1])%mod;
dp[i][tmp][2][1]=(dp[i][tmp][2][1]+dp[i-1][j][1][1])%mod;
dp[i][tmp][2][1]=(dp[i][tmp][2][1]+dp[i-1][j][2][1])%mod;
dp[i][tmp][0][2]=(dp[i][tmp][0][2]+dp[i-1][j][0][2])%mod;
dp[i][tmp][1][2]=(dp[i][tmp][1][2]+dp[i-1][j][0][2])%mod;
dp[i][tmp][1][2]=(dp[i][tmp][1][2]+dp[i-1][j][1][2])%mod;
dp[i][tmp][2][2]=(dp[i][tmp][2][2]+dp[i-1][j][1][2])%mod;
dp[i][tmp][2][2]=(dp[i][tmp][2][2]+dp[i-1][j][2][2])%mod;
}
dp[i][j][0][0]=(dp[i][j][0][0]+dp[i-1][j][0][0])%mod;
dp[i][j][0][1]=(dp[i][j][0][1]+dp[i-1][j][0][0])%mod;
dp[i][j][0][1]=(dp[i][j][0][1]+dp[i-1][j][0][1])%mod;
dp[i][j][0][2]=(dp[i][j][0][2]+dp[i-1][j][0][1])%mod;
dp[i][j][0][2]=(dp[i][j][0][2]+dp[i-1][j][0][2])%mod;
dp[i][j][1][0]=(dp[i][j][1][0]+dp[i-1][j][1][0])%mod;
dp[i][j][1][1]=(dp[i][j][1][1]+dp[i-1][j][1][0])%mod;
dp[i][j][1][1]=(dp[i][j][1][1]+dp[i-1][j][1][1])%mod;
dp[i][j][1][2]=(dp[i][j][1][2]+dp[i-1][j][1][1])%mod;
dp[i][j][1][2]=(dp[i][j][1][2]+dp[i-1][j][1][2])%mod;
dp[i][j][2][0]=(dp[i][j][2][0]+dp[i-1][j][2][0])%mod;
dp[i][j][2][1]=(dp[i][j][2][1]+dp[i-1][j][2][0])%mod;
dp[i][j][2][1]=(dp[i][j][2][1]+dp[i-1][j][2][1])%mod;
dp[i][j][2][2]=(dp[i][j][2][2]+dp[i-1][j][2][1])%mod;
dp[i][j][2][2]=(dp[i][j][2][2]+dp[i-1][j][2][2])%mod;
}
FOR(m,1,s)ans=(ans+dp[n][m][2][2])%mod;
printf("%d\n",(4LL*ans)%mod);
}
return 0;
}
当然你不想看卡常的话可以看下面的代码
#include
#include
#define mod 1000000007
#define N 1005
#define FOR(i,a,b) for(int i=(a),i##_END_=(b);i<=i##_END_;i++)
int dp[N][N][3][3],a[N];
int main() {
int T,n,s,tmp;
int ans;
scanf("%d",&T);
while(T--) {
ans=0;
scanf("%d%d",&n,&s);
FOR(i,1,n)scanf("%d",&a[i]);
memset(dp,0,sizeof dp);
dp[1][a[1]][0][0]=1;
dp[1][a[1]][1][0]=1;
dp[1][0][0][0]=1;
dp[1][0][0][1]=1;
FOR(i,2,n)FOR(j,0,s) {
tmp=j+a[i];
if(tmp<=s)FOR(jj,0,2) {
dp[i][tmp][0][jj]=(dp[i][tmp][0][jj]+dp[i-1][j][0][jj])%mod;
dp[i][tmp][1][jj]=(dp[i][tmp][1][jj]+dp[i-1][j][0][jj])%mod;
dp[i][tmp][1][jj]=(dp[i][tmp][1][jj]+dp[i-1][j][1][jj])%mod;
dp[i][tmp][2][jj]=(dp[i][tmp][2][jj]+dp[i-1][j][1][jj])%mod;
dp[i][tmp][2][jj]=(dp[i][tmp][2][jj]+dp[i-1][j][2][jj])%mod;
}
FOR(ii,0,2) {
dp[i][j][ii][0]=(dp[i][j][ii][0]+dp[i-1][j][ii][0])%mod;
dp[i][j][ii][1]=(dp[i][j][ii][1]+dp[i-1][j][ii][0])%mod;
dp[i][j][ii][1]=(dp[i][j][ii][1]+dp[i-1][j][ii][1])%mod;
dp[i][j][ii][2]=(dp[i][j][ii][2]+dp[i-1][j][ii][1])%mod;
dp[i][j][ii][2]=(dp[i][j][ii][2]+dp[i-1][j][ii][2])%mod;
}
}
FOR(m,1,s)ans=(ans+dp[n][m][2][2])%mod;
printf("%d\n",(4LL*ans)%mod);
}
return 0;
}