Cod sursa(job #1721490)

Utilizator LucianTLucian Trepteanu LucianT Data 25 iunie 2016 19:01:33
Problema Ratphu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.72 kb
#include <bits/stdc++.h>
#define maxN 20
using namespace std;
int i,j,v[maxN],nrc,p,maxC,mask;
long long dp[1<<19][maxN],n;
char s[19];
inline int modu(int x)
{
    while(x>=p)
        x-=p;
    return x;
}
int main()
{
    ifstream f("ratphu.in");
    ofstream g("ratphu.out");
    f>>s>>p;
    n=strlen(s);
    dp[0][0]=1;
    for(mask=0;mask<(1<<n);mask++)
    {
        for(j=0;j<p;j++)
        {
            if(dp[mask][j]==0)
                continue;
            for(i=0;i<n;i++)
            {
                if((mask&(1<<i)))
                    continue;
                dp[mask+(1<<i)][modu(j*10+(s[i]-'0'))]+=dp[mask][j];
            }
        }
    }
    g<<dp[(1<<n)-1][0];
    return 0;
}