求(nk)(mod232)
- (nk)=n!k!(n?k)!
- 根据上式,只需要枚举各质数的指数即可。即得到如下形式:
2a1×3a2×5a3…2b1×3b2×5b3?×2c1×3c2×5c3…=2a1?b1?c1×3a2?b2?c2×5a3?b3?c3…
- 先筛出质数,然后枚举各质数,对每个质数,算出各指数即可
/* **********************************************
File Name: 4111.cpp
Auther: [email protected]
Created Time: 2015年08月17日 星期一 19时46分13秒
*********************************************** */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll MOD = 1LL << 32;
const int MAX = 1000007;
bool is_prime[MAX];
int prime[MAX], tot;
int times[MAX];
int get_prime() {
memset(is_prime, true, sizeof(is_prime));
for (int i = 2; i <= MAX / i; ++i) {
for (int j = i * i; j < MAX; j += i) {
is_prime[j] = false;
}
}
tot = 0;
for (int i = 2; i < MAX; ++i) {
if (is_prime[i]) {
prime[tot++] = i;
}
}
return tot;
}
int gao(int p, int n) {
//printf("gao %d, %d: ", p, n);
int sum = 0;
for (ll i = p; i <= n; i *= p) {
sum += n / i;
}
//printf("%d\n", sum);
return sum;
}
ui fast_pow(ui a, int n) {
ui res = 1;
while (n) {
if (n & 1) res *= a;
a *= a;
n >>= 1;
}
return res;
}
int main() {
ios::sync_with_stdio(false);
get_prime();
int T;
cin >> T;
while (T--) {
int n, k;
cin >> n >> k;
if (k == 0) {
cout << 1 << endl;
continue;
}
memset(times, 0, sizeof(times));
for (int i = 0; prime[i] <= n; ++i) {
times[i] += gao(prime[i], n);
times[i] -= gao(prime[i], k);
times[i] -= gao(prime[i], n - k);
}
ui res = 1;
for (int i = 0; prime[i] <= n; ++i) {
//printf("pair %d^%d\n", prime[i], times[i]);
res *= fast_pow(prime[i], times[i]);
res %= MOD;
}
cout << res << endl;
}
return 0;
}
版权声明:那泥烤去看鸭o o
时间: 2024-10-06 15:06:46