Thursday, 31 December 2015

UVa 10616 - Divisible Group Sums

//\\__ hr1212 __//\\

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef map<int,int> mi;

#define si(a) scanf("%lld",&a)
#define sii(a,b) scanf("%lld %lld",&a,&b)
#define siii(a,b,c) scanf("%lld %lld %lld",&a,&b,&c)
#define pi(a) printf("%lld\n",a)
#define nl printf("\n");
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(),(c).end()
#define f(i,a,b) for(i=a;i<b;i++)
#define rf(i,a,b) for(i=a;i>=b;i--)
#define clr(x,a) memset(x,a,sizeof(x))
#define MAX 1000100
#define MOD 1000000007

ll d,n,m,dp[300][50][30],a[MAX];

ll solve(ll i,ll m,ll sum){
    if(m==0 && sum==0)
        return dp[i][m][sum]=1;
    if(m==0 && sum!=0)
        return dp[i][m][sum]=0;
    if(i>=n || m<0)
        return 0;
    if(dp[i][m][sum]!=-1)
        return dp[i][m][sum];
    return dp[i][m][sum]=solve(i+1,m,sum)+solve(i+1,m-1,((sum+a[i])%d+d)%d);
}

int main(){
    ll r,k,i,c=0,x=0,y=0,j,t,l,z,x1=0,y1=0,q;
    ll ans=0;string p;

    t=0;
    while(1){
        sii(n,q);
        if(n==0 && q==0)
            break;
        t++;
        printf("SET %lld:\n",t);

        f(i,0,n)
            si(a[i]);
        f(i,1,q+1){
            clr(dp,-1);
            sii(d,m);
            printf("QUERY %lld: %lld\n",i,solve(0,m,0));
        }
    }

    return 0;
}

No comments:

Post a Comment