Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define ll long long
- using namespace std;
- ll calcNcR(int n, int r) {
- ll p = 1, k = 1;
- if(n - r < r) r = n - r;
- if(r != 0)
- while(r) {
- p *= n; k *= r;
- long long m = __gcd(p, k);
- p /= m; k /= m;
- n--; r--;
- }
- else p = 1;
- return p;
- }
- void solve(int test) {
- int n, k; cin >> n >> k;
- ll ans = 0;
- if(n < k) cout << "Case " << test << ": " << ans << "\n";
- else {
- ans = calcNcR(n, k) * calcNcR(n, k);
- //k giai thua
- for(ll i = 1; i <= k; ++i)
- ans *= i;
- cout << "Case " << test << ": " << ans << "\n";
- }
- return;
- }
- int main() {
- //freopen("A-Rooks.inp", "r", stdin);
- //freopen("A-Rooks.out", "w", stdout);
- ios_base::sync_with_stdio(false);
- cin.tie(NULL); cout.tie(NULL);
- int t; cin >> t;
- for(int i = 1; i <= t; i++) {
- solve(i);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment