Cod sursa(job #402841)

Utilizator andreea1coolBobu Andreea andreea1cool Data 24 februarie 2010 10:44:09
Problema Pascal Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.33 kb
#include<stdio.h>
#include<math.h>

using namespace std;

inline long functie(long int n,long int p)
{
	long int s=0;
	if(p==2)
	{
	    while(n)
	    {
	        s+=(n>>1);
	        n>>=1;
	    }
	    return s;
	}
	while(n)
	{
	    s+=n/p;
	    n=n/p;
	}
	return s;
}

int main()
{
    freopen("pascal.in","r",stdin);
    freopen("pascal.out","w",stdout);
    long r, j, n, k1=0, k2=0, k3=0, d, p, c=0, i, q ,s, t;
    scanf("%ld%ld",&r,&d);
    long k13, k12;
    if(d==2||d==3||d==5)
    {
        k1=functie(r,d);
    }
    else
    if(d==4)
    {
        k1=functie(r, 2);
    }
    else
    if(d==6)
    {
        k12=functie(r,2);
        k13=functie(r, 3);
    }

	for(j=0;j<=r;j++)
	{
		p=r-j;
		if(d==2||d==3||d==5)
		{
            k2=functie(p,d);
            k3=functie(j,d);

            if(k1-k2-k3>0)
                c++;
        }
        else
        if(d==4)
        {
            k2=functie(p,2);
            k3=functie(j,2);

            if(((k1-k2-k3)>>1)>0)
            {
                c++;
            }
        }

        else
        {
            long k22=functie(p, 2);
            long k23=functie(p, 3);
            long k32=functie(j, 2);
            long k33=functie(j, 3);

            if(k12-k22-k32>0 && k13-k23-k33>0)
                c++;
        }
        k2=0;
        k3=0;

    }
    printf("%ld\n",c);
    return 0;
}