Cod sursa(job #952495)

Utilizator predatorGigi Valoare predator Data 23 mai 2013 16:22:44
Problema Algoritmul lui Euclid extins Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.8 kb
#include<fstream>
#define mod 1000003
#define LL long long
using namespace std;
ifstream f("mergesort.in");
ofstream g("mergesort.out");
LL L,fac[mod],inv[mod],i,x,y;
void ec(LL &x,LL &y,LL a,LL b)
{
    if(!b) { x=1,y=0; return; }
    LL x0,y0;
    ec(x,y,b,a%b);
    x0=x; y0=y;
    x=y;
    y=x0-(a/b)*y0;
}
LL F(LL L)
{
    LL A,B;
    B=L/2;
    A=L-B;
    if(L==1)
        return 1;
	LL T1;
	T1=fac[A]*F(A)+fac[B]*F(B);
	T1%=mod;
	T1*=fac[L]*inv[A];
	T1%=mod;
	T1*=inv[B];
	T1+=fac[L]-2;
	T1%=mod;
    return T1;
}
int main ()
{
    f>>L;
    fac[0]=1;
    for(i=1;i<=L;++i)
        fac[i]=fac[i-1]*i%mod;
    for(i=1;i<=L;++i)
    {
        x=1;
        ec(x,y,fac[i],mod);
        while(x<=0)
            x+=mod;
        inv[i]=x;
    }
    g<<F(L);
    return 0;
}