Mai intai trebuie sa te autentifici.
Cod sursa(job #952506)
Utilizator | Data | 23 mai 2013 16:43:57 | |
---|---|---|---|
Problema | Invers modular | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.86 kb |
#include<fstream>
#define mod 1000003
#define LL long long
using namespace std;
ifstream f("mergesort.in");
ofstream g("mergesort.out");
int L,fac[mod],inv[mod],i,x,y;
void ec(int &x,int &y,int a,int 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;
}
int F(int L)
{
LL A,B;
B=L/2;
A=L-B;
if(L==1)
return 1;
LL T1;
T1=1LL*(1LL*fac[B]*F(A)+1LL*fac[A]*F(B))*fac[L];
T1%=mod;
T1*=1LL*inv[B]*inv[A];
T1+=fac[L]-2;
if(fac[L]<2)
T1+=mod;
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;
}