Cod sursa(job #91223)

Utilizator vicenzo_cnuStan Alexandru Dan vicenzo_cnu Data 11 octombrie 2007 21:30:15
Problema Pascal Scor 80
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.53 kb
#include<stdio.h>
long long r,t;
int d;
FILE *f,*g;

void procesare2(int n,int t1)
{long long x;
if(n-2>=r-n)
{x=n;
 while(x%2==0)
{t1++;x/=2;}
x=r-n+1;
while(x%2==0&&x>0)
{t1--;x/=2;}
if(t1>0&&n-2!=r-n)
t+=2;
else if(t1>0)
t++;
procesare2(n-1,t1);}}

void procesare3(int n,int t1)
{long long x;
if(n-2>=r-n)
{x=n;
while(x%3==0)
{t1++;x/=3;}
x=r-n+1;
while(x%3==0&&x>0)
{t1--;x/=3;}
if(t1>0&&n-2!=r-n)
t+=2;
else if(t1>0)
t++;
procesare3(n-1,t1);}}

void procesare4(int n,int t1)
{long long x;
if(n-2>=r-n)
{x=n;
while(x%2==0)
{t1++;x/=2;}
x=r-n+1;
while(x%2==0&&x>0)
{t1--;x/=2;}
if(t1>1&&n-2!=r-n)
t+=2;
else if(t1>1)
t++;
procesare4(n-1,t1);}}

void procesare5(int n,int t1)
{long long x;
if(n-2>=r-n)
{x=n;
while(x%5==0)
{t1++;x/=5;}
x=r-n+1;
while(x%5==0&&x>0)
{t1--;x/=5;}
if(t1>0&&n-2!=r-n)
t+=2;
else if(t1>0)
t++;
procesare5(n-1,t1);}}

void procesare6(int n,int t1,int t2)
{long long x;
if(n-2>=r-n)
{x=n;
while(x%2==0||x%3==0)
{if(x%2==0){t1++;x/=2;}
if(x%3==0)
{t2++;x/=3;}}
x=r-n+1;
while((x%2==0||x%3==0)  &&x>0)
{if(x%2==0)
{t1--;x/=2;}
if(x%3==0)
{t2--;x/=3;}}
if(t1>0&&t2>0&&n-2!=r-n)
t+=2;
else if(t1>0 &&t2>0)
t++;
procesare6(n-1,t1,t2) ;}}


int main()
{f=fopen("pascal.in","r");
g=fopen("pascal.out","w");
fscanf(f,"%lld %d",&r,&d);
if(d==2)
procesare2(r,0);
else if(d==3)
procesare3(r,0);
else if(d==4)
procesare4(r,0);
else if(d==5)
procesare5(r,0);
else if(d==6)
procesare6(r,0,0);
fprintf(g,"%lld",t);
fclose(f);
fclose(g);
return 0;}