Problem No: 1095 - Arrange the Numbers
This problem has two parts. This is a typical combinatorics problem.
This problem has two parts. This is a typical combinatorics problem.
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define mod 1000000007
- #define N 1000
- ll ncr[N + 2][N + 2];
- ll fact[N + 2];
- int main()
- {
- ncr[0][0] = 1;
- for (int i = 1; i <= N; i++) {
- for (int j = 0; j <= N; j++) {
- if (j > i) ncr[i][j] = 0;
- else if (j == 0 || i == j) ncr[i][j] = 1;
- else ncr[i][j] = (ncr[i - 1][j - 1] + ncr[i - 1][j]) % mod;
- }
- }
- fact[0] = 1;
- for (ll i = 1; i <= N; i++) {
- fact[i] = (i * fact[i - 1]) % mod;
- }
- int t, n, m, k, id = 0, nn, mm;
- ll ans, xx;
- scanf ("%d", &t);
- while (t--) {
- scanf ("%d %d %d", &n, &m, &k);
- ans = fact[n - k];
- nn = m - k;
- mm = n - k;
- for (int i = 1; i <= nn; i++) {
- xx = (ncr[nn][i] * fact[mm - i]) % mod;
- if (i & 1) ans = (ans - xx + mod) % mod;
- else ans = (ans + xx + mod) % mod;
- }
- ans = (ans * ncr[m][k]) % mod;
- printf ("Case %d: %lld\n", ++id, ans);
- }
- return 0;
- }
Comments
Post a Comment