码迷,mamicode.com
首页 > 其他好文 > 详细

暑假集训刷题记录

时间:2015-07-23 21:26:09      阅读:108      评论:0      收藏:0      [点我收藏+]

标签:

    向上跳,再向上跳,也许再努力一点我就能够着菊苣们的膝盖了。

                                          ——题记

 

7.23

    CodeForces 559C 组合数 + DP

技术分享
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <map>
 5 #define MP make_pair
 6 #define F first
 7 #define S second
 8 using namespace std;
 9 
10 typedef long long LL;
11 typedef pair<LL, LL> Point;
12 
13 const int maxn = 22000 + 10;
14 const LL MOD = 1000000007LL;
15 
16 inline LL mul_mod(LL a, LL b)
17 {
18     a %= MOD; b %= MOD;
19     return (a * b) % MOD;
20 }
21 
22 inline LL sub_mod(LL a, LL b)
23 {
24     return (((a - b) % MOD) + MOD) % MOD;
25 }
26 
27 LL pow_mod(LL a, LL n)
28 {
29     LL ans = 1;
30     while(n)
31     {
32         if(n & 1) ans = mul_mod(ans, a);
33         a = mul_mod(a, a);
34         n >>= 1;
35     }
36     return ans;
37 }
38 
39 LL Inverse(LL a)
40 { return pow_mod(a, MOD - 2); }
41 
42 const int maxh = 200000 + 10;
43 
44 LL fac[maxh], invfac[maxh];
45 
46 void init()
47 {
48     fac[0] = fac[1] = 1;
49     for(int i = 2; i < maxh; i++) fac[i] = mul_mod(fac[i-1], i);
50     invfac[maxh-1] = Inverse(fac[maxh-1]);
51     for(int i = maxh-2; i >= 0; i--) invfac[i] = mul_mod(invfac[i+1], i+1);
52 }
53 
54 LL C(LL n, LL m)
55 {
56     if(m > n) { puts("shakalaka"); return 0; }
57     return mul_mod(mul_mod(fac[n], invfac[m]), invfac[n-m]);
58 }
59 
60 int h, w, n;
61 
62 Point a[maxn];
63 
64 LL methods(int i, int j)
65 {
66     LL x = a[j].first - a[i].first;
67     LL y = a[j].second - a[i].second;
68     return C(x + y, x);
69 }
70 
71 LL dp[maxn];
72 
73 int main()
74 {
75     //freopen("in.txt", "r", stdin);
76 
77     init();
78     scanf("%d%d%d", &h, &w, &n);
79     for(int i = 1; i <= n; i++)
80     {
81         LL x, y; scanf("%I64d%I64d", &x, &y);
82         a[i] = MP(x, y);
83     }
84     sort(a + 1, a + n + 1);
85     a[0] = MP(1, 1);
86     a[n+1] = MP(h, w);
87     dp[1] = methods(0, 1);
88 
89     for(int i = 2; i <= n + 1; i++)
90     {
91         dp[i] = methods(0, i);
92         for(int j = 1; j < i; j++) if(a[j].F <= a[i].F && a[j].S <= a[i].S)
93             dp[i] = sub_mod(dp[i], mul_mod(methods(j, i), dp[j]));
94     }
95 
96     printf("%I64d\n", dp[n + 1]);
97 
98     return 0;
99 }
代码君

    

    CodeForces 219D 树形DP

技术分享
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <vector>
 5 using namespace std;
 6 
 7 const int maxn = 200000 + 10;
 8 int n;
 9 
10 vector<int> G[maxn];
11 vector<bool> f[maxn];
12 
13 int L[maxn];
14 int fa[maxn];
15 int up[maxn];
16 int rev[maxn];
17 int totup;
18 
19 void dfs(int u, int father, int d)
20 {
21     L[u] = d;
22     fa[u] = father;
23     for(int i = 0; i < G[u].size(); i++)
24     {
25         int v = G[u][i];
26         if(v == father) continue;
27         if(!f[u][i]) { totup++; up[v] = up[u] + 1; }
28         else up[v] = up[u];
29         dfs(v, u, d + 1);
30     }
31 }
32 
33 int main()
34 {
35     //freopen("in.txt", "r", stdin);
36     scanf("%d", &n);
37     for(int i = 0; i < n-1; i++)
38     {
39         int u, v; scanf("%d%d", &u, &v);
40         G[u].push_back(v); f[u].push_back(true);
41         G[v].push_back(u); f[v].push_back(false);
42     }
43 
44     dfs(1, 0, 0);
45 
46     for(int v = 1; v <= n; v++)
47         rev[v] = totup - up[v] + L[v] - up[v];
48 
49     int ans = n - 1;
50     for(int i = 1; i <= n; i++) ans = min(ans, rev[i]);
51 
52     printf("%d\n", ans);
53 
54     vector<int> hehe;
55     for(int i = 1; i <= n; i++) if(rev[i] == ans) hehe.push_back(i);
56 
57     printf("%d", hehe[0]);
58     for(int i = 1; i < hehe.size(); i++) printf(" %d", hehe[i]);
59     puts("");
60 
61     return 0;
62 }
代码君

 

    

暑假集训刷题记录

标签:

原文地址:http://www.cnblogs.com/AOQNRMGYXLMV/p/4671534.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!