Problem No: 1326-Race
Both combinatorics and dynamic problem concept is used in this problem. We calculate how many ways the race can finish.
- #include<bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define N 1000
- #define mod 10056
- ll ncr[N + 1][N + 1];
- ll dp[N + 1];
- int main()
- {
- ll xx, yy;
- int i, j;
- ncr[0][0] = 1;
- for (i = 1; i <= N; i++) {
- for (j = 0; j <= N; j++) {
- if (j > i) ncr[i][j] = 0;
- else if (j == i || j == 0) ncr[i][j] = 1;
- else ncr[i][j] = (ncr[i - 1][j - 1] + ncr[i - 1][j]) % mod;
- }
- }
- dp[0] = 1;
- for (int i = 1; i <= N; i++) {
- xx = 0;
- for (int j = 1; j <= i; j++) {
- yy = (ncr[i][j] * dp[i - j]) % mod;
- xx = (xx + yy) % mod;
- }
- dp[i] = xx;
- }
- int n, t, id = 0;
- scanf ("%d", &t);
- while (t--) {
- scanf ("%d", &n);
- ll ans = dp[n];
- printf ("Case %d: %lld\n", ++id, ans);
- }
- return 0;
- }
Comments
Post a Comment