Skip to content

Commit

Permalink
线性时间预处理[1,n]阶乘的逆元
Browse files Browse the repository at this point in the history
  • Loading branch information
Dev-XYS committed Feb 10, 2017
1 parent ee5d83d commit 10ba4fb
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions Modular-Multiplicative-Inverse-Sieve(Factorial,Linear).cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <cstdio>

#define MAX_N 10000000

typedef long long ll;

using namespace std;

// p must be a prime greater than n.
ll n, fac_inv[MAX_N], p;

ll qpow(ll x, ll y)
{
if (y == 1) return x;
ll t = qpow(x, y >> 1);
t = t * t % p;
if ((y & 1) == 0) return t;
else return t * x % p;
}

ll fac(ll x)
{
ll res = 1;
for (int i = 2; i <= x; i++)
{
res = res * i % p;
}
return res;
}

void fac_inv_sieve()
{
fac_inv[n] = qpow(fac(n), p - 2);
for (ll i = n - 1; i >= 1; i--)
{
fac_inv[i] = fac_inv[i + 1] * (i + 1) % p;
}
}

int main()
{
scanf("%lld%lld", &n, &p);
fac_inv_sieve();
for (int i = 1; i <= n; i++)
{
printf("inv(fac(%d)) mod %lld = %lld\n", i, p, fac_inv[i]);
}
return 0;
}

0 comments on commit 10ba4fb

Please sign in to comment.