Cod sursa(job #857433)

Utilizator dtoniucDaniel Toniuc dtoniuc Data 17 ianuarie 2013 20:27:26
Problema Ratphu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.61 kb
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;

char C[20];
long long a[1<<18][21];
int p,lmax;
int main()
{
    freopen ("ratphu.in","r",stdin);
    freopen ("ratphu.out","w",stdout);
    scanf("%s%i",C,&p);
    lmax=strlen(C);
    for(int i=0;i<lmax;i++)
        a[1<<i][(C[i]-'0')%p]++;
    for(int i=1;i<(1<<lmax);i++)
        if(i&(i-1))
            for(int j=0;j<lmax;j++)
                if(i&(1<<j))
                    for(int k=0;k<p;k++)
                        a[i][(k*10+C[j]-'0')%p]+=a[i-(1<<j)][k];
    printf("%lld",a[(1<<lmax)-1][0]);
    return 0;
}