Cod sursa(job #784168)

Utilizator vendettaSalajan Razvan vendetta Data 5 septembrie 2012 00:02:05
Problema Tricouri Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.45 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

ifstream f("tricouri.in");
ofstream g("tricouri.out");

#define nmax 300005
#define Kmax 6
#define Pmax 21

int n, m, a[nmax], dp[2][Kmax][Pmax];

void citeste(){

    f >> n >> m;
    for(int i=1; i<=n; i++) f >> a[i];

    sort(a+1, a+n+1);

}

void rezolva(){

    for(int i=1; i<=m; i++){
        int x, y;
        f >> x >> y;
        //un subsir de suma maxima avand x numare si suma lor sa fie divizibila cu y
        for(int k=0; k<Kmax; k++) for(int rest=0; rest<Pmax; rest++) dp[0][k][rest] = 0, dp[1][k][rest] = 0;
        int linie = 1;
        int ok = 1;
        for(int j=n; j>=1; j--){
            if( dp[1-linie][1][ a[j]%y ] == 0 ) dp[linie][1][a[j]%y] = a[j];
            for(int k=2; k<=x; k++){
                for(int rest=0; rest<y; rest++){
                    if (dp[1-linie][k][ (rest*10+a[j])%y ] == 0 && dp[1-linie][k-1][ rest ] != 0){
                        dp[linie][k][ (rest*10+a[j])%y ] = dp[1-linie][k-1][rest] + a[j];
                    }
                }
            }
            if (dp[linie][x][0] != 0){
                g << dp[linie][x][0] << "\n";
                ok = 0;
                break;
            }
            linie = 1-linie;
            //for(int k=0; )
        }
        if (ok == 1) g << -1 << "\n";

    }

}

int main(){

    citeste();
    rezolva();

    f.close();
    g.close();

    return 0;

}