Cod sursa(job #790578)

Utilizator visanrVisan Radu visanr Data 21 septembrie 2012 19:52:49
Problema Ratphu Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.7 kb
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;

char N[20];
int P, lg;
long long dp[1 << 18][20];

int main()
{
    freopen("ratphu.in", "r", stdin);
    freopen("ratphu.out", "w", stdout);
    int i, j, k;
    scanf("%s %i", N, &P);
    lg = strlen(N);
    for(i = 0; i < lg; i++)
          dp[1 << i][(N[i] - '0') % P] ++;
    for(i = 1; i < (1 << lg); i++)
          if(i & (i - 1))
               for(j = 0; j < lg; j++)
                     if(i & (1 << j))
                          for(k = 0; k < P; k++)
                                dp[i][(k * 10 + N[j] - '0') % P] += dp[i - (1 << j)][k];
    printf("%lld\n", dp[(1 << lg) - 1][0]);
    return 0;
}