Mai intai trebuie sa te autentifici.
Cod sursa(job #1443653)
Utilizator | Data | 28 mai 2015 13:18:42 | |
---|---|---|---|
Problema | Ratphu | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva de probleme | Marime | 0.96 kb |
#include <iostream>
#include <fstream>
using namespace std;
const int PMax = 21;
int cif[20], P, ncif;
long long N;
long long dp[1 << 18][PMax];
void Read()
{
ifstream f ("ratphu.in");
f >> N >> P;
f.close();
}
void Solve()
{
ncif = -1;
while (N)
{
cif[++ ncif] = N % 10;
N /= 10;
}
dp[0][0] = 1;
for (int conf = 0; conf < (1 << (ncif + 1)); ++ conf)
{
for (int cifra = 0; cifra <= ncif; ++ cifra)
{
if ((conf & (1 << cifra)) == 0)
{
for (int rest = 0; rest < P; ++ rest)
{
dp[conf | (1 << cifra)][(rest * 10 + cif[cifra]) % P] += dp[conf][rest];
}
}
}
}
}
void Write()
{
ofstream g ("ratphu.out");
g << dp[(1 << (ncif + 1)) - 1][0] << "\n";
g.close();
}
int main()
{
Read();
Solve();
Write();
return 0;
}