Cod sursa(job #1796265)

Utilizator alittlezzCazaciuc Valentin alittlezz Data 3 noiembrie 2016 11:43:28
Problema Ratphu Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.81 kb
#include <iostream>
#include <fstream>
using namespace std;

int p,c[22],lung;
long long n,a[1<<20][20];
int main()
{
    ifstream in("ratphu.in");
    ofstream out("ratphu.out");

    in>>n>>p;
    long long d=n;
    while(d)
    {
        c[lung++]=d%10;
        d/=10;
    }

    for(int i=0;i<lung;++i)
        ++a[1<<i][c[i]%p];

    for(int i=1;i<(1<<lung);++i)
    {
        for(int r=0;r<p;++r) // restul
        {
            if(a[i][r])
            {
                for(int j=0;j<lung;++j) // bitul nou
                {
                    if(!(i&(1<<j)))
                    {
                        a[i|(1<<j)][(r*10+c[j])%p]+=a[i][r];
                    }
                }
            }
        }
    }

    out<<a[(1<<lung)-1][0]<<'\n';

    out.close();
    return 0;
}