Cod sursa(job #1446591)
| Utilizator | Data | 2 iunie 2015 12:25:29 | |
|---|---|---|---|
| Problema | Ratphu | Scor | 100 |
| Compilator | cpp | Status | done |
| Runda | Arhiva de probleme | Marime | 0.67 kb |
#include <fstream>
using namespace std;
ifstream in("ratphu.in");
ofstream out("ratphu.out");
long long dp[1<<18][20];
int v[19];
int main()
{
long long n;
int i=0,p,nrcif=0,k,j;
in>>n>>p;
while(n)
{
v[i]=n%10;
n=n/10;
i++;
nrcif++;
}
for(i=0;i<nrcif;++i)
dp[1<<i][v[i]%p]=1;
for(i=1;i<=(1<<nrcif)-1;++i)
for(j=0;j<nrcif;++j)
{
if((i & (1<<j)) == 0)
{
for(k=0;k<p;++k)
{
dp[i | (1<<j)][(k*10 + v[j])%p]+=dp[i][k];
}
}
}
out<<dp[(1<<nrcif)-1][0];
}
