Cod sursa(job #2859930)

Utilizator DauCuDalta43Diaconu Razvan DauCuDalta43 Data 2 martie 2022 10:15:23
Problema Ratphu Scor 30
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <bits/stdc++.h>
using namespace std;

int cnt,n,p;
int st[25];
int fr[15];

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

void Verif()
{
    int i,x;
    x=0;
    for(i=1;i<=n;i++)
        x=x*10+st[i];
    if(x%p==0)cnt++;
}

void Back(int top)
{
    if(top>n)
    {
        Verif();
        return;
    }
    for(int i=0;i<=9;i++)
        if(fr[i]!=0)
        {
            fr[i]--;
            st[top]=i;
            Back(top+1);
            fr[i]++;
        }
}

int main()
{
    int x;
    fin>>x>>p;
    while(x)
    {
        fr[x%10]++;
        x/=10;
        n++;
    }
    Back(1);
    for(int i=0;i<=9;i++)
        while(fr[i]>0)
        {
            cnt*=fr[i];
            fr[i]--;
        }
    fout<<cnt;
    return 0;
}