kuangbin 数学训练二 Combinations
题目链接:传送门#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define ll long longusing namespace std;const ll N = 1e6 + 10;const ll p = 1000003;ll t, n, x, y,
·
题目链接:
传送门
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const ll N = 1e6 + 10;
const ll p = 1000003;
ll t, n, x, y, ca;
ll fact[N], infact[N];
//快速幂模板
ll qmul(ll a, ll k) {
ll res = 1;
while (k) {
if (k & 1) res = (ll)res * a % p;
a = (ll) a * a % p;
k >>= 1;
}
return res;
}
int main() {
fact[0] = infact[0] = 1;
//打表求出所有数的阶乘及其逆元
for (ll i = 1; i < N; i ++ ) {
fact[i] = (ll)fact[i - 1] * i % p;
infact[i] = (ll)infact[i - 1] * qmul(i, p - 2) % p;
}
scanf("%lld", &t);
while(t--) {
scanf("%lld %lld", &x, &y);
//套公式计算
ll ans = fact[x] * infact[y] % p * infact[x - y] % p;
printf("Case %lld: %lld\n", ++ca, ans);
}
}
这道题算是很明显的组合数问题,套组合数的公式,打个表即可。注意数据范围。数组不要开太大或太小。
更多推荐
所有评论(0)