Pagini recente » Cod sursa (job #473610) | Cod sursa (job #881821) | Cod sursa (job #1828325) | Cod sursa (job #1126643) | Cod sursa (job #1318525)
#include<bits/stdc++.h>
using namespace std;
int digit[30];
long long d[(1 << 21)][25]; // cate permutari dau restul j ormate din cifrele din config i
int main()
{
FILE *fin = fopen("ratphu.in", "r");
FILE *fout = fopen("ratphu.out", "w");
long long n, p;
fscanf(fin, "%lld %lld", &n, &p);
int nr = 0;
while( n != 0 ) {
digit[nr] = n % 10;
nr++;
n /= 10;
}
d[0][0]=1;
long long lim = ( 1 << nr );
for( int i = 0; i < lim; ++i)
for(int k = 0; k < p; ++k)
if(d[i][k])
for( int j = 0; j < nr; ++j)
if(( i & (1 << j)) == 0)
d[i | (1 << j)][(k * 10 + digit[j]) % p] += d[i][k];
fprintf(fout, "%lld\n", d[(1 << nr) - 1][0]);
}