Cod sursa(job #2859830)

Utilizator robertanechita1Roberta Nechita robertanechita1 Data 1 martie 2022 23:40:56
Problema Ratphu Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.75 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin("ratphu.in");
ofstream fout("ratphu.out");

char s[25];
int dp[270000][25], p, a[25], n, rest[25][25];

int main()
{
    fin >> (s + 1);
    for(int i = 1; s[i]; i++)
        a[n++] = s[i] - '0';
    fin >> p;
    long long P = (1 << n);
    for(int i = 0; i <= 19; i++)
        for(int j = 0; j < n; j++)
            rest[i][j] = (i * 10 + a[j]) % p;
    dp[0][0] = 1;
    for(int stare = 0; stare < P; stare++)
        for(int i = 0; i < n; i++)
            if((stare & (1 << i)) == 0)
                for(int r = 0; r < p; r++)
                    dp[stare | (1 << i)][rest[r][a[i]]] += dp[stare][r];
    fout << dp[P - 1][0] << "\n";
    fout.close();
    return 0;
}