Cod sursa(job #465676)

Utilizator andrei.dAndrei Diaconeasa andrei.d Data 25 iunie 2010 11:45:58
Problema Ratphu Scor 40
Compilator cpp Status done
Runda Stelele Informaticii 2010, gimnaziu si clasa a IX-a, Ziua 1 Marime 1.24 kb
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <vector>

using namespace std;

#define file_in "ratphu.in"
#define file_out "ratphu.out"

#define nmax 100

int v[nmax];
int p[nmax];
int viz[nmax];
long long sol;
int P;
int nr;
char ch;



void citire()
{
    int i,l;
    freopen(file_in,"r",stdin);
    freopen(file_out,"w",stdout);

    nr=0;
    while(!feof(stdin))
    {
        scanf("%c", &ch);
        if (ch==' ')
          break;
        v[++nr]=ch-'0';
    }

    scanf("%d", &P);


}

void back(int k)
{
    int i,j;
    long long x;
    if (k==nr+1)
    {
       if (P==1) sol++;
       else
       if (P==2 && v[p[nr]]%2==0) sol++;
       else
       {

       x=0;
       i=0;
       while(v[p[i]]==0) i++;
       for (j=i;j<=nr;++j)
            x=x*10+v[p[j]];
       if (x%P==0) sol++;
       }
    }

    for (i=1;i<=nr;++i)
        if (!viz[i])
        {
            viz[i]=1;
            p[k]=i;
            back(k+1);
            viz[i]=0;
        }
}

void solve()
{
     sol=0;
     back(1);

     printf("%lld\n", sol);
}

int main()
{
    citire();
    solve();

    fclose(stdin);
    fclose(stdout);

    return 0;
}