Cod sursa(job #3276578)

Utilizator tudor_costinCostin Tudor tudor_costin Data 13 februarie 2025 21:02:03
Problema Ratphu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.99 kb
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2")
#include <bits/stdc++.h>

using namespace std;
ifstream fin("ratphu.in");
ofstream fout("ratphu.out");
#define int long long
const int Nmax=18;
int dp[(1<<Nmax)][20];
int pw[20];
signed main()
{
    int n,p;
    fin>>n>>p;
    vector<int> cifre;
    pw[0]=1;
    for(int i=1;i<=18;i++) pw[i]=pw[i-1]*10;
    while(n)
    {
        cifre.push_back(n%10);
        n=n/10;
    }
    n=cifre.size();
    dp[0][0]=1;
    for(int mask=1;mask<(1<<n);mask++)
    {
        int cnt=__builtin_popcount(mask);
        for(int rest=0;rest<p;rest++)
        {
            for(int bit=0;bit<n;bit++)
            {
                if((mask&(1<<bit))!=0)
                {
                    int nr=pw[cnt-1]*cifre[bit];
                    int need=(rest+p-(nr%p))%p;
                    dp[mask][rest]+=dp[mask^(1<<bit)][need];
                }
            }
        }
    }
    fout<<dp[(1<<n)-1][0]<<'\n';
    return 0;
}