Cod sursa(job #1946717)

Utilizator tziplea_stefanTiplea Stefan tziplea_stefan Data 30 martie 2017 13:02:40
Problema Dirichlet Scor 96
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>
#define MOD 9999991
#define LL long long

using namespace std;

ifstream fin("dirichlet.in");
ofstream fout("dirichlet.out");

int N;

void Modular_Reverse(LL &x, LL &y, LL A, LL B)
{
    if (B==0)
      x=1, y=0;
    else
    {
        Modular_Reverse(x, y, B, A % B);
        LL aux=x;
        x=y;
        y=aux-y*(A / B);
    }
}

LL Get_Catalan(int N)
{
    int i;
    LL ANS=1, fact=1;
    LL x, y;
    for (i=1; i<=2*N; i++)
    {
        ANS*=i;
        ANS%=MOD;
        if (i<=N)
        {
            fact*=i;
            fact%=MOD;
        }
    }
    x=y=0;
    Modular_Reverse(x, y, fact, MOD);
    if (x<0)
      x=MOD+(x % MOD);
    ANS*=x;
    ANS%=MOD;
    fact*=N+1;
    fact%=MOD;
    x=y=0;
    Modular_Reverse(x, y, fact, MOD);
    if (x<0)
      x=MOD+(x % MOD);
    ANS*=x;
    ANS%=MOD;
    return ANS;
}

int main()
{
    fin >> N;
    fout << Get_Catalan(N) << '\n';
    fin.close();
    fout.close();
    return 0;
}