Cod sursa(job #2254055)

Utilizator AndreiDeltaBalanici Andrei Daniel AndreiDelta Data 4 octombrie 2018 18:50:58
Problema Ratphu Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <bits/stdc++.h>
#define Dim 20
using namespace std;
ifstream f("ratphu.in");
ofstream g("ratphu.out");
unsigned long long N,P,val,big,lit,pr=1,ans;
unsigned long long fact,F[20];
int C[Dim],NR[12],V[12],cnt,pred;
bool Cifre[12];

int main()
{
    f>>N>>P;
    val=N;
    F[0]=1;
    for(int i=1;i<=18;i++) F[i]=F[i-1]*i;
    while(val)
    {
        C[++cnt]=val%10;
        Cifre[C[cnt]]=1;
        NR[C[cnt]]++;
        val/=10;
    }
    sort(C+1,C+cnt+1);
    for(int i=1;i<=cnt;i++)
    {
        big+=C[i]*pr;
        lit+=C[cnt-i+1]*pr;
        pr*=10;
    }
    for(unsigned long long i=lit;i<=big;i++)
    {
        if(i%P==0)
    {
       val=i;
       bool stop=1,ok=1;
       while(val!=0&&stop==1)
       {
           int c=val%10;
           val/=10;
           V[c]++;
           if(Cifre[c]==0||V[c]>NR[c]) stop=0;
           pred=c;
       }
       for(int j=0;j<=10;j++)
       {
           if(stop==1&&NR[j]>1) ans+=F[NR[j]],ok=0;
           V[j]=0;
       }
       if(stop==1&&ok==1) ans++;
    }

    }//cout<<i<<" "<<j<<" "<<V[1]<<" "<<V[2]<<" "<<V[3]<<" "<<V[4]<<'\n';
    g<<ans;
    return 0;
}