Cod sursa(job #960241)

Utilizator xSliveSergiu xSlive Data 9 iunie 2013 22:52:32
Problema Kperm Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <fstream>
using namespace std;
int s[5001],ok,k,n,p,c=0;
int valid(int k)
{
    for(int i=1;i<k;i++)
        if(s[i]==s[k])
            return 0;
    if(k>=p)
        if( (s[k] + s[k-1] + s[k-3]) % p != 0)
            return 0;
    return 1;
}


int main()
{
    ifstream f("kperm.in");
    ofstream g("kperm.out");
    f>>n>>p;
    k=1;
    s[k]=0;
    while(k>0)
    {
        ok=0;
        while(s[k]<n&&!ok)
        {
            s[k]++;
            ok=valid(k);
        }
        if(ok)
            if(k==n)
                c++;
            else
            {
                k++;
                s[k]=0;
            }
        else k--;
    }
    g<<c;
    return 0;
}