Cod sursa(job #1424072)

Utilizator alexpetrescuAlexandru Petrescu alexpetrescu Data 23 aprilie 2015 13:18:52
Problema Ratphu Scor 100
Compilator c Status done
Runda Arhiva de probleme Marime 0.86 kb
#include <stdio.h>
#define MAXN 18
#define MAXK 20
int v[MAXN], k;
long long d[1<<MAXN][MAXK];
inline int mod(int x){
    while(x>=k){
        x-=k;
    }
    return x;
}
int main(){
    int n, i, j, r;
    char ch;
    FILE *fin, *fout;
    fin=fopen("ratphu.in", "r");
    fout=fopen("ratphu.out", "w");
    ch=fgetc(fin);
    n=0;
    while(ch!=' '){
        v[n++]=ch-'0';
        ch=fgetc(fin);
    }
    fscanf(fin, "%d", &k);
    d[0][0]=1;
    for(i=0; i<(1<<n); i++){
        for(r=0; r<k; r++){
            if(d[i][r]!=0){
                for(j=0; j<n; j++){
                    if((i&(1<<j))==0){
                        d[i|(1<<j)][mod(r*10+v[j])]+=d[i][r];
                    }
                }
            }
        }
    }
    fprintf(fout, "%lld\n", d[(1<<n)-1][0]);
    fclose(fin);
    fclose(fout);
    return 0;
}