codechef ANUCBC(背包)

题目链接: https://www.codechef.com/problems/ANUCBC

按模数进行背包

取模不要直接取,分开写,不然会T

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<cmath>
using namespace std;
typedef long long ll;

const int maxn = 100010;
const int mod = 1e9+9;

int T,n,m,q;
int a[maxn],f[2][110];

ll read(){ ll s=0,f=1; char ch=getchar(); while(ch<'0' || ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0' && ch<='9'){ s=s*10+ch-'0'; ch=getchar(); } return s*f;}

int main(){
    T=read();
    while(T--){
        n=read(),q=read();
        for(int i=1;i<=n;i++) a[i]=read();
        
        while(q--){
            m=read();
            memset(f,0,sizeof(f));
            f[0][0]=1;
            int k=1;
            for(int i=1;i<=n;++i){
                int v=a[i]%m;
                v=(v+m)%m;
                for(int j=0;j<m;++j){
                    int tmp=j-v;
                    if(tmp<0) tmp=tmp+m;
                    f[k][j]=(f[!k][j]+f[!k][tmp])%mod;
                }
                k=!k;
            }
            printf("%d\n",f[!k][0]);
        }
    }

    return 0;
}
/*
2
5 1
1 2 -1 4 5
9
5 2
1 2 3 4 5
5
15
*/

 

posted @ 2019-02-26 10:17  Tartarus_li  阅读(145)  评论(0编辑  收藏  举报