Cod sursa(job #2755482)

Utilizator PredescuSebastianIonPredescu Sebastian Ion PredescuSebastianIon Data 27 mai 2021 14:27:24
Problema Ratphu Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.67 kb
#include <bits/stdc++.h>

using namespace std;
ifstream f("ratphu.in");
ofstream g("ratphu.out");
int p,k=0,cifre[21],rest[301];
long long n,dp[(1<<19)][21];
int main()
{
	f>>n>>p;
	dp[0][0]=1;
	while(n)
	{
		cifre[++k]=n%10;
		n/=10;
	}
	for(int i=1;i<=300;i++)
    {
        rest[i]=i%p;
	}
	for(int masca=0;masca<=(1<<k)-1;masca++)
	{
		for(int i=0;i<p;i++)
		{
			if(dp[masca][i]>0)
			{
				for(int bit=0;bit<k;bit++)
				{
					if((masca&(1<<bit))==0)
                    {
                        dp[(masca^(1<<bit))][rest[i*10+cifre[bit+1]]]+=dp[masca][i];
                    }
				}
			}
		}
	}
	g<<dp[(1<<k)-1][0]<<"\n";
	return 0;
}