demo/old/pc.c

56 lines
1.2 KiB
C

#include <stdio.h>
long long mod_rev(long long x, long long mod)
{
long long i = 0;
long long a = x;
long long b = mod;
long long pa[3] = {0, 1, 0};
long long pb[3] = {0, 0, 1};
for (;;) {
long long q = a / b;
long long r = a % b;
pa[i] = pa[(i + 1) % 3] - q * pa[(i + 2) % 3];
pb[i] = pb[(i + 1) % 3] - q * pb[(i + 2) % 3];
if (!r) {
long long rev = pa[(i + 2) % 3];
if (0 > rev) {
rev += mod;
}
return rev;
}
a = b;
b = r;
i = (i + 1) % 3;
}
}
long long calc(long long m, long long n)
{
const long long mod = 1000000007;
long long x;
long long total = 1;
long long d = n - m;
long long p = d < m ? d : m;
long long q = d < m ? m : d;
for (x = 2; x <= n; x++) {
total = (total * x) % mod;
}
for (x = 2; x <= p; x++) {
long long rev = mod_rev(x, mod);
total = (total * rev) % mod;
total = (total * rev) % mod;
}
for (; x <= q; x++) {
long long rev = mod_rev(x, mod);
total = (total * rev) % mod;
}
return total;
}