#9:自闭的味道——4

BZOJ1005,给了度就有prufer序,排列组合。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 struct BigInt {
 5     int a[10005], len;
 6 
 7     BigInt() {
 8         memset(a, 0, sizeof(a));
 9         len = 1;
10     }
11 
12     BigInt operator * (const int p) const {
13         BigInt ret = *this;
14         for (int i = 1; i <= ret.len; i++) {
15             ret.a[i] *= p;
16         }
17         for (int i = 1; i <= ret.len; i++) {
18             ret.a[i+1] += ret.a[i] / 10;
19             ret.a[i] %= 10;
20         }
21         while (ret.a[ret.len+1]) {
22             ret.len++;
23             ret.a[ret.len+1] += ret.a[ret.len] / 10;
24             ret.a[ret.len] %= 10;
25         }
26         return ret;
27     }
28 
29     BigInt operator / (const int p) const {
30         BigInt ret = *this;
31         for (int i = ret.len; i; i--) {
32             ret.a[i-1] += ret.a[i] % p * 10;
33             ret.a[i] /= p;
34         }
35         while (!ret.a[ret.len] && ret.len > 1)
36             ret.len--;
37         return ret;
38     }
39 };
40 
41 int n, d[1001];
42 int sum, cnt;
43 bool none;
44 BigInt ans;
45 
46 void cal() {
47     ans.a[1] = 1;
48     for (int i = n-1-sum; i <= n-2; i++)
49         ans = ans * i;
50     for (int i = 1; i <= n; i++)
51         for (int j = 2; j < d[i]; j++)
52             ans = ans / j;
53     for (int i = 1; i <= n-2-sum; i++)
54         ans = ans * (n-cnt);
55 }
56 
57 int main() {
58     cin >> n;
59     for (int i = 1; i <= n; i++) {
60         cin >> d[i];
61         if (!d[i])    none = true;
62         if (~d[i])    cnt++, sum += d[i]-1;
63     }
64     if (sum > n-2)    none = true;
65     if (n == 1) {
66         printf("%d
", d[1] > 0 ? 0 : 1);
67         return 0;
68     }
69     if (none) {
70         puts("0");
71         return 0;
72     }
73 
74     cal();
75     for (int i = ans.len; i; i--)
76         printf("%d", ans.a[i]);
77     puts("");
78 
79     return 0;
80 }
View Code

BZOJ1430,顺手prufer水题一道。

 1 #include <cstdio>
 2 #define ll long long
 3 #define mod 9999991
 4 
 5 int n, ans = 1;
 6 
 7 int main() {
 8     scanf("%d", &n);
 9     for (int i = 0; i < n-2; i++)
10         ans = (ll)ans * n % mod;
11     for (int i = 1; i < n; i++)
12         ans = (ll)ans * i % mod;
13     printf("%d
", ans);
14     return 0;
15 }
View Code

BZOJ1008,基础的组合数学。

 1 #include <cstdio>
 2 #define ll long long
 3 #define mod 100003
 4 
 5 ll m, n;
 6 
 7 ll ksm(ll a, ll b) {
 8     ll res = 1ll;
 9     for (; b; b >>= 1) {
10         if (b & 1)    res = res * a % mod;
11         a = a * a % mod;
12     }
13     return res;
14 }
15 
16 int main() {
17     scanf("%lld%lld", &m, &n);
18     printf("%d
", (ksm(m, n) - m*ksm(m-1, n-1)%mod + mod) % mod);
19     return 0;
20 }
View Code

然后就开始被各种DP题卡死……看不懂题解qwq

今日末开启单调队列优化的学习,POJ1821,列完式子k和j关系不大所以可以预处理一下的意思?

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 int n, m;
 7 int f[110][16010];
 8 int q[16010];
 9 struct worker {
10     int L, P, S;
11 
12     bool operator < (const worker b) const {
13         return S < b.S;
14     }
15 }a[110];
16 
17 inline int cal(int i, int k) {
18     return f[i-1][k] - a[i].P * k;
19 }
20 
21 int main() {
22     scanf("%d%d", &n, &m);
23     for (int i = 1; i <= m; i++)
24         scanf("%d%d%d", &a[i].L, &a[i].P, &a[i].S);
25     sort(a+1, a+1+m);
26 
27     for (int i = 1; i <= m; i++) {
28         int l = 1, r = 0;
29         for (int k = max(0, a[i].S - a[i].L); k < a[i].S; k++) {
30             while (l <= r && cal(i, q[r]) <= cal(i, k))
31                 r--;
32             q[++r] = k;
33         }
34 
35         for (int j = 1; j <= n; j++) {
36             f[i][j] = max(f[i-1][j], f[i][j-1]);
37             if (j >= a[i].S) {
38                 while (l <= r && q[l] < j - a[i].L)    l++;
39                 if (l <= r)
40                     f[i][j] = max(f[i][j], cal(i, q[l]) + a[i].P * j);
41             }
42         }
43     }
44 
45     printf("%d
", f[m][n]);
46     return 0;
47 }
View Code
原文地址:https://www.cnblogs.com/AlphaWA/p/10357040.html