年前每日Done。

1.14

SPOJ - STARSBC

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 
 5 int phi(int x)
 6 {
 7     int ret = 1;
 8     for(int i = 2; (long long) i * i <= x; i++)
 9     {
10         if(x % i == 0)
11         {
12             x /= i, ret *= i - 1;
13             while(x % i == 0) x /= i, ret *= i;
14         }
15     }
16     if(x > 1) ret *= x - 1;
17     return ret;
18 }
19 
20 int main(void)
21 {
22     int N;
23     while(~scanf("%d", &N)) printf("%d\n", (phi(N) + 1) / 2);
24     return 0;
25 }
Aguin

SPOJ - KAOS

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 typedef long long LL;
 7 
 8 // Trie
 9 const int maxnode = 1000000, sigma_size = 26;
10 struct Trie
11 {
12     int ch[maxnode][sigma_size];
13     int val[maxnode];
14     int sz;
15     Trie() {sz = 1; memset(ch[0], 0, sizeof(ch[0]));}
16     int idx(char c) {return c - 'a';}
17 
18     LL cal(string s)
19     {
20         LL ret = 0;
21         int u = 0, n = s.length();
22         for(int i = 0; i < n; i++)
23         {
24             int c = idx(s[i]);
25             if(!ch[u][c])
26             {
27                 memset(ch[sz], 0, sizeof(ch[sz]));
28                 val[sz] = 0;
29                 ch[u][c] = sz++;
30             }
31             for(int j = c + 1; j < sigma_size; j++) ret = ret + val[ch[u][j]];
32             u = ch[u][c];
33             val[u]++;
34         }
35         for(int j = 0; j < sigma_size; j++) ret = ret + val[ch[u][j]];
36         return ret;
37     }
38 } t;
39 
40 string str[1111111];
41 int main(void)
42 {
43     ios::sync_with_stdio(0);
44     int N;
45     cin >> N;
46     for(int i = 1; i <= N; i++) cin >> str[i];
47     sort(str + 1, str + N + 1);
48     LL ans = 0;
49     for(int i = 1; i <= N; i++)
50     {
51         reverse(str[i].begin(), str[i].end());
52         ans += t.cal(str[i]);
53     }
54     cout << ans << endl;
55     return 0;
56 }
Aguin

SPOJ - MINSUB

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 int st[1111], L[1111], R[1111];
 6 int G[1111][1111], M[1111][1111];
 7 int n, m, k;
 8 
 9 int cal(int x)
10 {
11     for(int i = 1; i <= n; i++)
12         for(int j = 1; j <= m; j++)
13             M[i][j] = G[i][j] >= x ? M[i][j-1] + 1 : 0;
14     int ret = 0;
15     for(int j = 1; j <= m; j++)
16     {
17         int p = 0;
18         for(int i = 1; i <= n; i++)
19         {
20             while(p && M[st[p]][j] >= M[i][j]) p--;
21             L[i] = p ? st[p] + 1 : 1;
22             st[++p] = i;
23         }
24         p = 0;
25         for(int i = n; i; i--)
26         {
27             while(p && M[st[p]][j] >= M[i][j]) p--;
28             R[i] = p ? st[p] - 1 : n;
29             st[++p] = i;
30         }
31         for(int i = 1; i <= n; i++)
32             ret = max(ret, (R[i] - L[i] + 1) * M[i][j]);
33     }
34     return ret;
35 }
36 
37 int main(void)
38 {
39     int T;
40     scanf("%d", &T);
41     while(T--)
42     {
43         scanf("%d %d %d", &n, &m, &k);
44         for(int i = 1; i <= n; i++)
45             for(int j = 1; j <= m; j++)
46                 scanf("%d", &G[i][j]);
47         int l = 0, r = 1e9, mid, ans = n * m;
48         while(l < r)
49         {
50             mid = r - (r - l) / 2;
51             int tmp = cal(mid);
52             if(tmp >= k) l = mid, ans = tmp;
53             else r = mid - 1;
54         }
55         printf("%d %d\n", l, ans);
56     }
57     return 0;
58 }
Aguin

SPOJ - INTSUB

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 typedef long long LL;
 5 const LL mod = 1e9 + 7;
 6 LL p[2222];
 7 
 8 int main(void)
 9 {
10     p[0] = 1;
11     for(int i = 1; i < 2222; i++) p[i] = p[i-1] * 2 % mod;
12     int T;
13     scanf("%d", &T);
14     for(int kase = 1; kase <= T; kase++)
15     {
16         int N;
17         scanf("%d", &N);
18         LL ans = 0;
19         for(int i = 1; i <= N + N; i++)
20         {
21             int cnt = 0;
22             for(int j = i + 1; j <= N + N; j++)
23                 if(j % i == 0) cnt++;
24             ans = (ans + (p[cnt] + mod - 1) * p[N+N-i-cnt]) % mod;
25         }
26         printf("Case %d: %lld\n", kase, ans);
27     }
28     return 0;
29 }
Aguin

1.16

SPOJ - STC02

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <map>
 6 using namespace std;
 7 typedef pair<int, int> pii;
 8 typedef long long LL;
 9 const int maxn = 1e6 + 10;
10 char s[maxn], r[maxn];
11 
12 struct Hash_map
13 {
14     const int seed = 131;
15     int mod[2] = {1000000007, 1000000009};
16     int P[2][maxn], sum[2][2][maxn];
17     map<pii, int> M;
18 
19     void init()
20     {
21         for(int t = 0; t <= 1; t++)
22         {
23             P[t][0] = 1;
24             for(int i = 1; i < maxn; i++)
25                 P[t][i] = (LL) P[t][i-1] * seed % mod[t];
26         }
27     }
28 
29     void pre(char * s, int x)
30     {
31         int len = strlen(s + 1);
32         for(int t = 0; t <= 1; t++)
33         {
34             sum[x][t][0] = 0;
35             for(int i = 1; i <= len; i++)
36                 sum[x][t][i] = ((LL) sum[x][t][i-1] * seed + s[i]) % mod[t];
37         }
38     }
39 
40     pii Hash(string s)
41     {
42         int ret[2] = {0};
43         int len = s.length();
44         for(int t = 0; t <= 1; t++)
45             for(int i = 0; i < len; i++)
46                 ret[t] = ((LL) ret[t] * seed + s[i]) % mod[t];
47         return pii(ret[0], ret[1]);
48     }
49 
50     pii Hash(char * s)
51     {
52         int ret[2] = {0};
53         int len = strlen(s);
54         for(int t = 0; t <= 1; t++)
55             for(int i = 0; i < len; i++)
56                 ret[t] = ((LL) ret[t] * seed + s[i]) % mod[t];
57         return pii(ret[0], ret[1]);
58     }
59 
60     pii Hash(int l, int r, int x)
61     {
62         int ret[2];
63         for(int t = 0; t <= 1; t++)
64             ret[t] = (sum[x][t][r] - (LL) sum[x][t][l-1] * P[t][r-l+1] % mod[t] + mod[t]) % mod[t];
65         return pii(ret[0], ret[1]);
66     }
67 } H;
68 
69 int main(void)
70 {
71     H.init();
72     int len;
73     scanf("%d %s", &len, s + 1);
74     for(int i = 1; i <= len; i++) r[i] = s[len+1-i] == '0' ? '1' : '0';
75     H.pre(s, 0), H.pre(r, 1);
76     LL ans = 0;
77     for(int i = 1; i < len; i++)
78     {
79         int l = 0, r = min(i, len - i), mid;
80         while(l < r)
81         {
82             mid = r - (r - l) / 2;
83             if(H.Hash(i - mid + 1, i, 0) == H.Hash(len - i - mid + 1, len - i, 1)) l = mid;
84             else r = mid - 1;
85         }
86         ans = ans + l;
87     }
88     printf("%lld\n", ans);
89     return 0;
90 }
Aguin

SPOJ - UCV2013E

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 typedef long long LL;
 5 const LL mod = 1e9 + 7;
 6 int x[55], y[55];
 7 LL C[555][555];
 8 
 9 int main(void)
10 {
11     for(int i = 0; i < 555; i++) C[i][0] = C[i][i] = 1;
12     for(int i = 2; i < 555; i++)
13         for(int j = 1; j < 555; j++)
14             C[i][j] = (C[i-1][j-1] + C[i-1][j]) % mod;
15     int N;
16     while(~scanf("%d", &N) && N)
17     {
18         int sum = 0;
19         for(int i = 1; i <= N; i++) scanf("%d", x + i), sum -= x[i];
20         for(int i = 1; i <= N; i++) scanf("%d", y + i), sum += y[i];
21         LL ans = 1;
22         for(int i = 1; i <= N; i++)
23             ans = ans * C[sum][y[i]-x[i]] % mod, sum -= y[i] - x[i];
24         printf("%lld\n", ans);
25     }
26     return 0;
27 }
Aguin

CodeForces - 414C

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <algorithm>
 5 using namespace std;
 6 typedef long long LL;
 7 const int maxn = 1111111;
 8 LL pos[22], inv[22];
 9 int a[maxn];
10 
11 void cal(int l, int r, int dep)
12 {
13     if(!dep) return;
14     cal(l, (l + r) / 2, dep - 1);
15     cal((l + r) / 2 + 1, r, dep - 1);
16     vector<int> v;
17     for(int i = l; i <= (l + r) / 2; i++) v.push_back(a[i]);
18     sort(v.begin(), v.end());
19     for(int i = (l + r) / 2 + 1; i <= r; i++)
20     {
21         pos[dep] = pos[dep] + (lower_bound(v.begin(), v.end(), a[i]) - v.begin());
22         inv[dep] = inv[dep] + (v.end() - upper_bound(v.begin(), v.end(), a[i]));
23     }
24 }
25 
26 int main(void)
27 {
28     int n, m;
29     scanf("%d", &n);
30     int tot = 1 << n;
31     for(int i = 1; i <= tot; i++) scanf("%d", a + i);
32     cal(1, tot, n);
33     for(int i = n - 1; i >= 0; i--)
34         for(int j = i + 1; j <= n; j++)
35             pos[j] += pos[i], inv[j] += inv[i];
36     scanf("%d", &m);
37     while(m--)
38     {
39         int q;
40         scanf("%d", &q);
41         for(int i = q + 1; i <= n; i++)
42             pos[i] += inv[q] - pos[q], inv[i] += pos[q] - inv[q];
43         for(int i = 0; i <= q; i++) swap(pos[i], inv[i]);
44         printf("%lld\n", inv[n]);
45     }
46     return 0;
47 }
Aguin

1.17

SPOJ - TBATTLE

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 const int maxn = 1e5 + 10;
 5 int a[maxn], p[33], num[33], cur[33];
 6 int d[maxn][33];
 7 
 8 bool ok(int cnt)
 9 {
10     for(int i = 1; i <= cnt; i++)
11         if(cur[i] < num[i]) return false;
12     return true;
13 }
14 
15 int main(void)
16 {
17     int N;
18     scanf("%d", &N);
19     for(int i = 1; i <= N; i++) scanf("%d", a + i);
20     int tmp = N, cnt = 0;
21     for(int i = 2; i <= tmp; i++)
22     {
23         if(tmp % i == 0)
24         {
25             p[++cnt] = i;
26             while(tmp % i == 0) tmp /= i, num[cnt]++;
27         }
28     }
29     int pos = 0, ans = maxn, l, r;
30     for(int i = 1; i <= N; i++)
31     {
32         while(!ok(cnt) && pos < N)
33         {
34             pos++;
35             for(int j = 1; j <= cnt; j++)
36                 while(a[pos] % p[j] == 0)
37                     a[pos] /= p[j], d[pos][j]++, cur[j]++;
38         }
39         if(!ok(cnt)) break;
40         if(pos - i + 1 < ans)
41         {
42             ans = pos - i + 1;
43             l = i, r = l + ans - 1;
44         }
45         for(int j = 1; j <= cnt; j++)
46             cur[j] -= d[i][j];
47     }
48     if(ans == maxn) puts("-1");
49     else printf("%d %d\n", l - 1, r - 1);
50     return 0;
51 }
Aguin

SPOJ - PUTNIK

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 const int INF = 1e9;
 6 int G[1505][1505], dp[1505][1505];
 7 
 8 int main(void)
 9 {
10     int N;
11     scanf("%d", &N);
12     for(int i = 1; i <= N; i++)
13         for(int j = 1; j <= N; j++)
14             scanf("%d", &G[i][j]);
15     for(int i = 0; i <= N + 1; i++)
16         for(int j = 0; j <= N + 1; j++)
17             dp[i][j] = INF;
18     dp[N+1][N] = dp[N][N+1] = 0;
19     for(int i = N - 1; i > 1; i--)
20         for(int j = i + 2; j <= N + 1; j++)
21         dp[i][i+1] = min(dp[i][i+1], dp[j][i+1] + G[i][j]),
22         dp[j][i] = min(dp[j][i], dp[j][i+1] + G[i][i+1]),
23         dp[i][j] = min(dp[i][j], dp[i+1][j] + G[i+1][i]),
24         dp[i+1][i] = min(dp[i+1][i], dp[i+1][j] + G[i][j]);
25     int ans = INF;
26     for(int i = 3; i <= N + 1; i++)
27         ans = min(ans, dp[i][2] + G[i][1] + G[1][2]),
28         ans = min(ans, dp[2][i] + G[2][1] + G[1][i]);
29     printf("%d\n", ans);
30     return 0;
31 }
Aguin

CodeForces - 509E

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int maxn = 1e6 + 10;
 6 double sum[maxn];
 7 char s[maxn];
 8 
 9 bool ok(char c)
10 {
11     return c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U' || c == 'Y';
12 }
13 
14 int main(void)
15 {
16     double tmp = sum[1] = 1.0;
17     for(int i = 2; i < maxn; i++)
18     {
19         tmp += 1.0 / i;
20         sum[i] = sum[i-1] + tmp;
21     }
22     scanf("%s", s + 1);
23     int len = strlen(s + 1);
24     double ans = 0.0;
25     for(int i = 1; i <= len; i++)
26     {
27         if(!ok(s[i])) continue;
28         ans += sum[len] - sum[i-1] - sum[len-i];
29     }
30     printf("%.8f\n", ans);
31     return 0;
32 }
Aguin

SPOJ - IITWPC4F

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 const int maxn = 1e5 + 10;
 6 int sum[2][maxn<<2], tag[2][maxn<<2];
 7 
 8 void gather(int id, int p)
 9 {
10     sum[id][p] = sum[id][p<<1] + sum[id][p<<1|1];
11 }
12 
13 void push(int id, int p, int m)
14 {
15     if(tag[id][p])
16     {
17         tag[id][p<<1] ^= 1;
18         tag[id][p<<1|1] ^= 1;
19         sum[id][p<<1] = (m - (m >> 1)) - sum[id][p<<1];
20         sum[id][p<<1|1] = (m >> 1) - sum[id][p<<1|1];
21         tag[id][p] = 0;
22     }
23 }
24 
25 void modify(int id, int p, int tl, int tr, int l, int r)
26 {
27     if(tr < l || r < tl) return;
28     if(l <= tl && tr <= r)
29     {
30         tag[id][p] ^= 1;
31         sum[id][p] = (tr - tl + 1) - sum[id][p];
32         return;
33     }
34     push(id, p, tr - tl + 1);
35     int mid = (tl + tr) >> 1;
36     modify(id, p<<1, tl, mid, l, r);
37     modify(id, p<<1|1, mid+1, tr, l, r);
38     gather(id, p);
39 }
40 
41 int query(int id, int p, int tl, int tr, int l, int r)
42 {
43     if(tr < l || r < tl) return 0;
44     if(l <= tl && tr <= r) return sum[id][p];
45     push(id, p, tr - tl + 1);
46     int mid = (tl + tr) >> 1;
47     int ret = query(id, p<<1, tl, mid, l, r);
48     ret += query(id, p<<1|1, mid+1, tr, l, r);
49     return ret;
50 }
51 
52 int main(void)
53 {
54     int q;
55     scanf("%d", &q);
56     while(q--)
57     {
58         char s[11];
59         int a, b, c, d;
60         scanf("%s", s);
61         if(s[0] == 'x')
62         {
63             scanf("%d %d", &a, &b);
64             modify(0, 1, 1, 100001, a + 1, b + 1);
65         }
66         else if(s[0] == 'y')
67         {
68             scanf("%d %d", &a, &b);
69             modify(1, 1, 1, 100001, a + 1, b + 1);
70         }
71         else if(s[0] == 'q')
72         {
73             scanf("%d %d %d %d", &a, &b, &c, &d);
74             long long e = query(0, 1, 1, 100001, a + 1, c + 1);
75             long long f = query(1, 1, 1, 100001, b + 1, d + 1);
76             printf("%lld\n", e * (d - b + 1) + f * (c - a + 1) - e * f * 2);
77         }
78     }
79     return 0;
80 }
Aguin

SPOJ - TLE

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 const int mod = 1e9;
 7 int f[1<<20], g[1<<20];
 8 int c[55];
 9 
10 int main(void)
11 {
12     int T;
13     scanf("%d", &T);
14     while(T--)
15     {
16         int N, M;
17         scanf("%d %d", &N, &M);
18         for(int i = 1; i <= N; i++) scanf("%d", c + i);
19         memset(f, 0, sizeof(f));
20         f[0] = 1;
21         for(int i = 1; i <= N; i++)
22         {
23             memcpy(g, f, sizeof(g));
24             for(int k = 0; k < M; k++)
25                 for(int j = 0; j < 1 << M; j++)
26                     if(((1<<k)&j)==0) g[j|(1<<k)] = (g[j|(1<<k)] + g[j]) % mod;
27             memset(f, 0, sizeof(f));
28             for(int j = 0; j < 1 << M; j++)
29                 if(j % c[i] != 0) f[j] = g[(~j)&((1<<M)-1)];
30         }
31         int ans = 0;
32         for(int i = 0; i < 1 << M; i++) ans = (ans + f[i]) % mod;
33         printf("%d\n", ans);
34     }
35     return 0;
36 }
Aguin

51nod - 1677

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 typedef long long LL;
 5 const LL mod = 1e9 + 7;
 6 const int maxn = 2e5 + 10;
 7 int n, k, cnt, h[maxn];
 8 LL ans, fac[maxn];
 9 
10 LL qpow(LL a, LL b)
11 {
12     LL ret = 1LL;
13     while(b)
14     {
15         if(b & 1) ret = ret * a % mod;
16         a = a * a % mod;
17         b >>= 1;
18     }
19     return ret;
20 }
21 
22 LL inv(LL x)
23 {
24     return qpow(x, mod - 2);
25 }
26 
27 LL C(int a, int b)
28 {
29     if(b > a) return 0;
30     return fac[a] * inv(fac[b]) % mod * inv(fac[a-b]) % mod;
31 }
32 
33 struct edge
34 {
35     int to, pre;
36 } e[maxn<<1];
37 
38 void add(int from, int to)
39 {
40     cnt++;
41     e[cnt].pre = h[from];
42     e[cnt].to = to;
43     h[from] = cnt;
44 }
45 
46 int sz[maxn];
47 void dfs(int x, int fa)
48 {
49     sz[x] = 1;
50     for(int i = h[x]; i; i = e[i].pre)
51     {
52         int to = e[i].to;
53         if(to == fa) continue;
54         dfs(to, x);
55         sz[x] += sz[to];
56     }
57     ans = (ans + C(n, k) - C(sz[x], k) - C(n - sz[x], k) + mod + mod) % mod;
58 }
59 
60 int main(void)
61 {
62     fac[0] = 1;
63     for(int i = 1; i < maxn; i++) fac[i] = fac[i-1] * i % mod;
64     scanf("%d %d", &n, &k);
65     for(int i = 1; i < n; i++)
66     {
67         int a, b;
68         scanf("%d %d", &a, &b);
69         add(a, b), add(b, a);
70     }
71     dfs(1, 0);
72     printf("%lld\n", ans);
73     return 0;
74 }
Aguin

CodeForces - 484D

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstdlib>
 5 #include <cmath>
 6 using namespace std;
 7 typedef long long LL;
 8 int a[1111111];
 9 
10 int main(void)
11 {
12     int n;
13     scanf("%d", &n);
14     for(int i = 1; i <= n; i++) scanf("%d", a + i);
15     LL dp1 = 0, dp2 = 0;
16     int lst = 0;
17     for(int i = 2; i < n; i++)
18     {
19         if(a[i] >= a[i-1] && a[i] >= a[i+1] || a[i] <= a[i-1] && a[i] <= a[i+1])
20         {
21             LL cpy1, cpy2;
22             if(!lst) cpy1 = abs(a[i-1] - a[1]), cpy2 = abs(a[i] - a[1]);
23             else cpy1 = max(dp1 + abs(a[lst] - a[i-1]), dp2 + (lst == i - 1 ? 0 : abs(a[lst+1] - a[i-1]))),
24                  cpy2 = max(dp1 + abs(a[lst] - a[i]), dp2 + abs(a[lst+1] - a[i]));
25             dp1 = cpy1, dp2 = cpy2, lst = i;
26         }
27     }
28     LL ans;
29     if(!lst) ans = abs(a[1] - a[n]);
30     else ans = max(dp1 + abs(a[n] - a[lst]), dp2 + abs(a[n] - a[lst+1]));
31     printf("%lld\n", ans);
32     return 0;
33 }
Aguin

51nod - 1189

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 typedef long long LL;
 6 const LL mod = 1e9 + 7;
 7 const int maxn = 1e6 + 10;
 8 int pr[maxn];
 9 
10 void GetPrime()
11 {
12     memset(pr, 0, sizeof(pr));
13     for(int i = 2; i < maxn; i++)
14     {
15         if(!pr[i]) pr[++pr[0]] = i;
16         for(int j = 1; j <= pr[0] && pr[j] * i < maxn; j++)
17         {
18             pr[i*pr[j]] = 1;
19             if(i % pr[j] == 0) break;
20         }
21     }
22 }
23 
24 int main(void)
25 {
26     GetPrime();
27     int N;
28     scanf("%d", &N);
29     LL ans = 1;
30     for(int i = 1; pr[i] <= N; i++)
31     {
32         int cnt = 0, tmp = N;
33         while(tmp) cnt += tmp / pr[i], tmp /= pr[i];
34         ans = ans * (cnt + cnt + 1) % mod;
35     }
36     ans = (ans + 1) * 500000004 % mod;
37     printf("%lld\n", ans);
38     return 0;
39 }
Aguin

CodeForces - 607B

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 int c[505], dp[505][505];
 6 
 7 int main(void)
 8 {
 9     int n;
10     scanf("%d", &n);
11     for(int i = 1; i <= n; i++) scanf("%d", c + i), dp[i][i] = 1;
12     for(int l = 2; l <= n; l++)
13     for(int s = 1; s + l - 1 <= n; s++)
14     {
15         int e = s + l - 1;
16         dp[s][e] = l;
17         if(c[s] == c[e])
18             if(l == 2) dp[s][e] = 1;
19             else dp[s][e] = min(dp[s][e], dp[s+1][e-1]);
20         for(int m = s; m < e; m++)
21             dp[s][e] = min(dp[s][e], dp[s][m] + dp[m+1][e]);
22     }
23     printf("%d\n", dp[1][n]);
24     return 0;
25 }
Aguin

LightOJ - 1265

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 
 5 int main(void)
 6 {
 7     int T;
 8     scanf("%d", &T);
 9     for(int kase = 1; kase <= T; kase++)
10     {
11         int t, d;
12         scanf("%d %d", &t, &d);
13         printf("Case %d: %.8f\n", kase, t % 2 ? 0 : 1.0 / (t + 1));
14     }
15     return 0;
16 }
Aguin

SPOJ - UOFTCG

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int maxn = 2e5 + 10;
 6 int ans, cnt, h[maxn], vis[maxn];
 7 
 8 struct edge
 9 {
10     int to, pre;
11 } e[maxn<<1];
12 
13 void add(int from, int to)
14 {
15     cnt++;
16     e[cnt].pre = h[from];
17     e[cnt].to = to;
18     h[from] = cnt;
19 }
20 
21 int tag[maxn];
22 void dfs(int x)
23 {
24     vis[x] = 1;
25     tag[x] = 0;
26     for(int i = h[x]; i; i = e[i].pre)
27     {
28         int to = e[i].to;
29         if(vis[to]) continue;
30         dfs(to);
31         tag[x] += tag[to];
32     }
33     if(tag[x] > 1) ans += tag[x] - 1, tag[x] = 0;
34     else tag[x] = 1;
35 }
36 
37 int main(void)
38 {
39     int T;
40     scanf("%d", &T);
41     while(T--)
42     {
43         cnt = 0;
44         memset(h, 0, sizeof(h));
45         memset(vis, 0, sizeof(vis));
46 
47         int n, m;
48         scanf("%d %d", &n, &m);
49         for(int i = 1; i <= m; i++)
50         {
51             int a, b;
52             scanf("%d %d", &a, &b);
53             add(a, b), add(b,a);
54         }
55 
56         ans = 0;
57         for(int i = 1; i <= n; i++)
58         {
59             if(vis[i]) continue;
60             dfs(i);
61             ans += tag[i];
62         }
63         printf("%d\n", ans + n - 1);
64     }
65     return 0;
66 }
Aguin

1.18

SPOJ - INS14G

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 typedef long long LL;
 5 const LL mod = 1e9 + 7;
 6 const int maxn = 1e6 + 10;
 7 LL fac[maxn];
 8 
 9 LL qpow(LL a, LL b)
10 {
11     LL ret = 1LL;
12     while(b)
13     {
14         if(b & 1) ret = ret * a % mod;
15         a = a * a % mod;
16         b >>= 1;
17     }
18     return ret;
19 }
20 
21 LL inv(LL x)
22 {
23     return qpow(x, mod - 2);
24 }
25 
26 LL C(int n, int m)
27 {
28     return fac[n] * inv(fac[m]) % mod * inv(fac[n-m]) % mod;
29 }
30 
31 int read() {
32     char ch;
33     for (ch = getchar(); ch < '0' || ch > '9'; ch = getchar());
34     int cnt = 0;
35     for (; ch >= '0' && ch <= '9'; ch = getchar())
36         cnt = cnt * 10 + ch - '0';
37     return cnt;
38 }
39 
40 int main(void)
41 {
42     fac[0] = 1;
43     for(int i = 1; i < maxn; i++) fac[i] = fac[i-1] * i % mod;
44     int T = read();
45     while(T--)
46     {
47         int n = read();
48         printf("%lld\n", C(n - 1, n / 2));
49     }
50     return 0;
51 }
Aguin

AtCoder - 2272

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 typedef long long LL;
 5 const LL mod = 1e9 + 7;
 6 int d[66];
 7 
 8 LL dp[66][2][2];
 9 LL cal(int bit)
10 {
11     dp[0][0][0] = 1;
12     for(int i = 1; i <= bit; i++)
13     for(int p = 0; p <= 1; p++)
14     for(int q = 0; q <= 1; q++)
15     for(int a = 0; a <= 1; a++)
16     for(int b = a; b <= 1; b++)
17     {
18         if(!dp[i-1][p][q]) continue;
19         int nq = a + b + q > 1 ? 1 : 0;
20         int now = (a + b + q) % 2;
21         int np = (now > d[i] || p && now == d[i]) ? 1 : 0;
22         dp[i][np][nq] = (dp[i][np][nq] + dp[i-1][p][q]) % mod;
23     }
24     return dp[bit][0][0];
25 }
26 
27 int main(void)
28 {
29     LL N, tmp;
30     scanf("%lld", &N), tmp = N;
31     int bit = 0;
32     while(tmp) d[++bit] = tmp % 2, tmp /= 2;
33     printf("%lld\n", cal(bit));
34     return 0;
35 }
Aguin

HDU - 4858

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstring>
 5 using namespace std;
 6 const int maxn = 2e5 + 10;
 7 int cnt, h[maxn];
 8 
 9 struct edge
10 {
11     int to, pre;
12 } e[maxn<<1];
13 
14 void add(int from, int to)
15 {
16     cnt++;
17     e[cnt].pre = h[from];
18     e[cnt].to = to;
19     h[from] = cnt;
20 }
21 
22 int u[maxn], v[maxn];
23 int deg[maxn], tag[maxn], s1[maxn], s2[maxn];
24 int main(void)
25 {
26     int T;
27     scanf("%d", &T);
28     while(T--)
29     {
30         cnt = 0;
31         memset(h, 0, sizeof(h));
32         memset(s1, 0, sizeof(s1));
33         memset(s2, 0, sizeof(s2));
34         memset(deg, 0, sizeof(deg));
35 
36         int n, m;
37         scanf("%d %d", &n, &m);
38         for(int i = 1; i <= m; i++)
39         {
40             scanf("%d %d", u + i, v + i);
41             deg[u[i]]++, deg[v[i]]++;
42         }
43         int lim = sqrt(n);
44         for(int i = 1; i <= n; i++)
45             tag[i] = deg[i] > lim ? 1 : 0;
46         for(int i = 1; i <= m; i++)
47         {
48             if(tag[u[i]] == tag[v[i]]) add(u[i], v[i]), add(v[i], u[i]);
49             else if(tag[u[i]]) add(v[i], u[i]);
50             else add(u[i], v[i]);
51         }
52         int q;
53         scanf("%d", &q);
54         while(q--)
55         {
56             int op, x, y;
57             scanf("%d", &op);
58             if(!op)
59             {
60                 scanf("%d %d", &x, &y);
61                 if(tag[x]) s2[x] += y;
62                 else for(int i = h[x]; i; i = e[i].pre) s1[e[i].to] += y;
63             }
64             else
65             {
66                 scanf("%d", &x);
67                 int ans = s1[x];
68                 for(int i = h[x]; i; i = e[i].pre) ans += s2[e[i].to];
69                 printf("%d\n", ans);
70             }
71         }
72     }
73     return 0;
74 }
Aguin

SPOJ - IGAME

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 typedef long long LL;
 6 
 7 int d[22];
 8 LL dp[22][16][2];
 9 LL cal(LL x)
10 {
11     int bit = 0;
12     LL tmp = x;
13     while(tmp) d[++bit] = tmp % 10, tmp /= 10;
14 
15     memset(dp, 0, sizeof(dp));
16     dp[bit+1][0][1] = 1;
17     for(int i = bit; i; i--)
18     for(int p = 0; p < 16; p++)
19     for(int q = 0; q <= 1; q++)
20     for(int j = 0; j <= 9; j++)
21     {
22         if(!dp[i+1][p][q]) continue;
23         if(q && j > d[i]) continue;
24         int np = p ^ j;
25         int nq = q && j == d[i] ? 1 : 0;
26         dp[i][np][nq] = dp[i][np][nq] + dp[i+1][p][q];
27     }
28     return dp[1][0][0] + dp[1][0][1];
29 }
30 
31 int main(void)
32 {
33     int T;
34     scanf("%d", &T);
35     while(T--)
36     {
37         LL A, B;
38         scanf("%lld %lld", &A, &B);
39         LL ans2 = cal(B) - cal(A-1);
40         LL ans1 = B - A + 1 - ans2;
41         printf("%lld %lld\n", ans1, ans2);
42     }
43     return 0;
44 }
Aguin

CodeForces - 712E

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 const int maxn = 1e5 + 10;
 6 int a[maxn], b[maxn];
 7 
 8 struct node
 9 {
10     double l, r;
11     node(double L = -1, double R = -1): l(L), r(R) {}
12     friend node operator + (node A, node B)
13     {
14         if(A.l < 0) return B;
15         if(B.l < 0) return A;
16         node ret;
17         ret.l = A.l * B.l / (1 - (1 - B.l) * (1 - A.r));
18         ret.r = A.r * B.r / (1 - (1 - B.l) * (1 - A.r));
19         return ret;
20     }
21 } R[maxn<<2];
22 
23 void gather(int p)
24 {
25     R[p] = R[p<<1] + R[p<<1|1];
26 }
27 
28 void build(int p, int l, int r)
29 {
30     if(l < r)
31     {
32         int mid = (l + r) >> 1;
33         build(p<<1, l, mid);
34         build(p<<1|1, mid + 1, r);
35         gather(p);
36     }
37     else R[p].l = 1.0 * a[l] / b[l], R[p].r = 1 - R[p].l;
38 }
39 
40 void modify(int p, int tl, int tr, int l, int r, double v)
41 {
42     if(tr < l || r < tl) return;
43     if(l <= tl && tr <= r)
44     {
45         R[p].l = v;
46         R[p].r = 1 - v;
47         return;
48     }
49     int mid = (tl + tr) >> 1;
50     modify(p<<1, tl, mid, l, r, v);
51     modify(p<<1|1, mid+1, tr, l, r, v);
52     gather(p);
53 }
54 
55 node query(int p, int tl, int tr, int l, int r)
56 {
57     if(tr < l || r < tl) return node();
58     if(l <= tl && tr <= r) return R[p];
59     int mid = (tl + tr) >> 1;
60     node ret = query(p<<1, tl, mid, l, r);
61     ret = ret + query(p<<1|1, mid+1, tr, l, r);
62     return ret;
63 }
64 
65 int main(void)
66 {
67     int n, q;
68     scanf("%d %d", &n, &q);
69     for(int i = 1; i <= n; i++) scanf("%d %d", a + i, b + i);
70     build(1, 1, n);
71     while(q--)
72     {
73         int op, aa, bb, ll, rr;
74         scanf("%d", &op);
75         if(op == 1)
76         {
77             scanf("%d %d %d", &ll, &aa, &bb);
78             modify(1, 1, n, ll, ll, 1.0 * aa / bb);
79         }
80         else
81         {
82             scanf("%d %d", &aa, &bb);
83             printf("%.10f\n", query(1, 1, n, aa, bb).l);
84         }
85     }
86     return 0;
87 }
Aguin

CodeForces - 566D

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 const int maxn = 2e5 + 10;
 5 int fa[2][maxn];
 6 
 7 int Find(int id, int x)
 8 {
 9     return fa[id][x] == x ? x : fa[id][x] = Find(id, fa[id][x]);
10 }
11 
12 void Union(int id, int x, int y)
13 {
14     x = Find(id, x);
15     y = Find(id, y);
16     if(x != y) fa[id][y] = x;
17 }
18 
19 int main(void)
20 {
21     int n, q;
22     scanf("%d %d", &n, &q);
23     for(int i = 1; i <= n; i++) fa[0][i] = fa[1][i] = i;
24     while(q--)
25     {
26         int op, x, y;
27         scanf("%d %d %d", &op, &x, &y);
28         if(op == 1) Union(1, x, y);
29         else if(op == 2)
30             while(Find(0, x) != Find(0, y))
31                 Union(1, Find(0, x), Find(0, x) + 1), fa[0][Find(0, x)] = Find(0, x) + 1;
32         else puts(Find(1, x) == Find(1, y) ? "YES" : "NO");
33     }
34     return 0;
35 }
Aguin

CodeForces - 351B

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 int p[3333];
 5 
 6 int main(void)
 7 {
 8     int n;
 9     scanf("%d", &n);
10     for(int i = 1; i <= n; i++) scanf("%d", p + i);
11     int cnt = 0;
12     for(int i = 1; i <= n; i++)
13         for(int j = i + 1; j <= n; j++)
14             if(p[j] < p[i]) cnt++;
15     printf("%d\n", cnt + cnt - cnt % 2);
16     return 0;
17 }
Aguin

1.22

PAT - Letter-moving Game

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 char s[1111], t[1111];
 7 
 8 int main(void)
 9 {
10     scanf("%s %s", s + 1, t + 1);
11     int len = strlen(s + 1), ans = 0;
12     for(int i = 1; i <= len; i++)
13     {
14         int p = 0;
15         for(int j = 1; j <= len; j++)
16             if(i + p <= len && t[i+p] == s[j]) p++;
17         ans = max(ans, p);
18     }
19     printf("%d\n", len - ans);
20     return 0;
21 }
Aguin

LightOJ - 1318

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 typedef long long LL;
 6 const int maxn = 111111;
 7 int pr[maxn];
 8 
 9 void GetPrime()
10 {
11     memset(pr, 0, sizeof(pr));
12     for(int i = 2; i < maxn; i++)
13     {
14         if(!pr[i]) pr[++pr[0]] = i;
15         for(int j = 1; j <= pr[0] && pr[j] * i < maxn; j++)
16         {
17             pr[i*pr[j]] = 1;
18             if(i % pr[j] == 0) break;
19         }
20     }
21 }
22 
23 LL qpow(LL a, LL b, LL mod)
24 {
25     LL ret = 1LL;
26     while(b)
27     {
28         if(b & 1) ret = ret * a % mod;
29         a = a * a % mod;
30         b >>= 1;
31     }
32     return ret;
33 }
34 
35 LL get(LL n, LL p)
36 {
37     int ret = 0;
38     while(n) ret += n / p, n /= p;
39     return ret;
40 }
41 
42 LL C(LL n, LL m, LL mod)
43 {
44     LL ret = 1;
45     for(int i = 1; pr[i] <= n; i++)
46     {
47         LL c = get(n, pr[i]) - get(m, pr[i]) - get(n - m, pr[i]);
48         ret = ret * qpow(pr[i], c, mod) % mod;
49     }
50     return ret;
51 }
52 
53 int main(void)
54 {
55     GetPrime();
56     int T;
57     scanf("%d", &T);
58     for(int kase = 1; kase <= T; kase++)
59     {
60         int N, K, L, M;
61         scanf("%d %d %d %d", &N, &K, &L, &M);
62         printf("Case %d: ", kase);
63         if(!M) printf("%lld\n", qpow(K, L, N) + 1);
64         else
65         {
66             LL tmp = qpow(K, L - 1, N) * qpow(K - 1, M - 1, N) % N * ((LL) K * (K - 1) / 2 % N) % N;
67             LL ans = tmp * C(L, M, N) % N;
68             printf("%lld\n", ans + 1);
69         }
70     }
71     return 0;
72 }
Aguin

POJ - 3498

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <queue>
  6 #include <vector>
  7 using namespace std;
  8 const int INF = 1e9;
  9 const int maxn = 2e5 + 10;
 10 int lv[maxn], it[maxn];
 11 int cnt, h[222];
 12 
 13 struct edge
 14 {
 15     int to, pre, cap;
 16 } e[maxn<<1];
 17 
 18 void init()
 19 {
 20     memset(h, -1, sizeof(h));
 21     cnt = 0;
 22 }
 23 
 24 void add(int from, int to, int cap)
 25 {
 26     e[cnt].pre = h[from];
 27     e[cnt].to = to;
 28     e[cnt].cap = cap;
 29     h[from] = cnt;
 30     cnt++;
 31 }
 32 
 33 void ad(int from, int to, int cap)
 34 {
 35     add(from, to, cap);
 36     add(to, from, 0);
 37 }
 38 
 39 void bfs(int s)
 40 {
 41     memset(lv, -1, sizeof(lv));
 42     queue<int> q;
 43     lv[s] = 0;
 44     q.push(s);
 45     while(!q.empty())
 46     {
 47         int v = q.front(); q.pop();
 48         for(int i = h[v]; i >= 0; i = e[i].pre)
 49         {
 50             int cap = e[i].cap, to = e[i].to;
 51             if(cap > 0 && lv[to] < 0)
 52             {
 53                 lv[to] = lv[v] + 1;
 54                 q.push(to);
 55             }
 56         }
 57     }
 58 }
 59 
 60 int dfs(int v, int t, int f)
 61 {
 62     if(v == t) return f;
 63     for(int &i = it[v]; i >= 0; i = e[i].pre)
 64     {
 65         int &cap = e[i].cap, to = e[i].to;
 66         if(cap > 0 && lv[v] < lv[to])
 67         {
 68             int d = dfs(to, t, min(f, cap));
 69             if(d > 0)
 70             {
 71                 cap -= d;
 72                 e[i^1].cap += d;
 73                 return d;
 74             }
 75         }
 76     }
 77     return 0;
 78 }
 79 
 80 int Dinic(int s, int t)
 81 {
 82     int flow = 0;
 83     while(1)
 84     {
 85         bfs(s);
 86         if(lv[t] < 0) return flow;
 87         memcpy(it, h, sizeof(it));
 88         int f;
 89         while((f = dfs(s, t, INF)) > 0) flow += f;
 90     }
 91 }
 92 
 93 
 94 
 95 int N, sum;
 96 double D;
 97 int x[222], y[222], n[222], m[222];
 98 int dist2(int i, int j)
 99 {
100     return (x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]);
101 }
102 bool ok(int k)
103 {
104     init();
105     int S = N + N + 1, T = S + 1;
106     for(int i = 1; i <= N; i++)
107         ad(S, i, n[i]), ad(i, N + i, m[i]);
108     for(int i = 1; i <= N; i++)
109         for(int j = 1; j <= N; j++)
110             if(dist2(i, j) <= D * D) ad(N + i, j, INF);
111     ad(k, T, INF);
112     return Dinic(S, T) >= sum;
113 }
114 
115 vector<int> v;
116 int main(void)
117 {
118     int T;
119     scanf("%d", &T);
120     while(T--)
121     {
122         v.clear();
123         scanf("%d %lf", &N, &D);
124         sum = 0;
125         for(int i = 1; i <= N; i++) scanf("%d %d %d %d", x + i, y + i, n + i, m + i), sum += n[i];
126         for(int i = 1; i <= N; i++)
127             if(ok(i)) v.push_back(i);
128         if(!v.size()) puts("-1");
129         else
130         {
131             for(int i = 0; i < v.size(); i++) printf("%d ", v[i] - 1);
132             puts("");
133         }
134     }
135     return 0;
136 }
Aguin

1.25

LightOJ - 1352

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 typedef long long LL;
 5 
 6 int bit(LL x)
 7 {
 8     int ret = 0;
 9     while(x) x /= 2, ret++;
10     return ret;
11 }
12 
13 LL cal(LL x)
14 {
15     if(x <= 1 || x == 3) return x;
16     if(x == 2) return 0;
17     LL b = 2, sum = 1;
18     for(int i = 1; ; i++)
19     {
20         if(sum + b < x) sum += b, b <<= 1;
21         else
22         {
23             LL ret = x % 2;
24             LL r = x - sum;
25             int k = bit(x), t = 0;
26             for(int j = 1; j < k; j++)
27             {
28                 int o = 0;
29                 b >>= 1;
30                 if(r > b)
31                 {
32                     r -= b, o = (r % 2) ^ t;
33                     if(j == k - 2) t = 1;
34                 }
35                 else o = t;
36                 ret = (ret << 1) + o ^ (j == 1);
37             }
38             return ret;
39         }
40     }
41 }
42 
43 int main(void)
44 {
45     int T;
46     scanf("%d", &T);
47     for(int kase = 1; kase <= T; kase++)
48     {
49         LL p, q;
50         scanf("%lld %lld", &p, &q);
51         int b = bit(q) - bit(p - 1);
52         printf("Case %d: %lld\n", kase, cal(q) ^ (cal(p - 1) << b));
53     }
54     return 0;
55 }
Aguin

1.26

CodeForces - 438C

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <algorithm>
 5 using namespace std;
 6 typedef long long LL;
 7 const LL mod = 1000000007;
 8 
 9 // Geo
10 const double EPS = 1e-10;
11 struct Point
12 {
13     double x, y;
14     Point(double x = 0, double y = 0): x(x), y(y) {}
15 };
16 typedef Point Vector;
17 Vector operator - (Vector A, Vector B)
18 {
19     return Vector(A.x - B.x, A.y - B.y);
20 }
21 int dcmp(double x)
22 {
23     if(fabs(x) < EPS) return 0;
24     else return x < 0 ? -1 : 1;
25 }
26 double Cross(Vector A, Vector B)
27 {
28     return A.x * B.y - A.y * B.x;
29 }
30 bool judge(Point a1, Point a2, Point b1, Point b2)
31 {
32     double c1 = Cross(a2 - a1, b1 - a1), c2 = Cross(a2 - a1, b2 - a1);
33     double c3 = Cross(b2 - b1, a1 - b1), c4 = Cross(b2 - b1, a2 - b1);
34     return dcmp(c1) * dcmp(c2) < 0 && dcmp(c3) * dcmp(c4) < 0;
35 }
36 Point p[222];
37 LL dp[222][222];
38 int main(void)
39 {
40     int n;
41     scanf("%d", &n);
42     for(int i = 0; i < n; i++)
43     {
44         double x, y;
45         scanf("%lf %lf", &x, &y);
46         p[i] = Point(x, y);
47     }
48 
49     double s = 0;
50     for(int i = 0; i < n; i++)
51         s += Cross(p[i], p[(i+1)%n]);
52     if(s < 0) reverse(p, p + n);
53 
54     for(int i = 0; i < n - 1; i++) dp[i][i+1] = 1;
55 
56     for(int l = 2; l < n; l++)
57     for(int s = 0; s + l < n; s++)
58     {
59         int e = s + l;
60         for(int m = s + 1; m < e; m++)
61             if(dcmp(Cross(p[m] - p[s], p[s] - p[e])) < 0)
62                 dp[s][e] = (dp[s][e] + dp[s][m] * dp[m][e]) % mod;
63     }
64     printf("%I64d\n", dp[0][n-1]);
65     return 0;
66 }
Aguin

CodeForces - 321C

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 const int maxn = 1e5 + 10;
 7 
 8 int cnt, h[maxn];
 9 struct edge
10 {
11     int to, pre;
12 } e[maxn<<1];
13 void add(int from, int to)
14 {
15     cnt++;
16     e[cnt].pre = h[from];
17     e[cnt].to = to;
18     h[from] = cnt;
19 }
20 
21 int sz[maxn], vis[maxn];
22 void Size(int x, int f)
23 {
24     sz[x] = 1;
25     int tmp = 0;
26     for(int i = h[x]; i; i = e[i].pre)
27     {
28         int to = e[i].to;
29         if(to == f || vis[to] != -1) continue;
30         Size(to, x);
31         sz[x] += sz[to];
32     }
33 }
34 
35 int rt, M;
36 void Root(int r, int x, int f)
37 {
38     int tmp = 0;
39     for(int i = h[x]; i; i = e[i].pre)
40     {
41         int to = e[i].to;
42         if(to == f || vis[to] != -1) continue;
43         Root(r, to, x);
44         tmp = max(tmp, sz[to]);
45     }
46     tmp = max(tmp, sz[r] - sz[x]);
47     if(tmp < M) M = tmp, rt = x;
48 }
49 
50 void solve(int x, int id)
51 {
52     Size(x, 0);
53     M = 1e9;
54     Root(x, x, 0);
55     vis[rt] = id;
56     for(int i = h[rt]; i; i = e[i].pre)
57     {
58         int to = e[i].to;
59         if(vis[to] == -1) solve(to, id + 1);
60     }
61 }
62 
63 int main(void)
64 {
65     int n;
66     scanf("%d", &n);
67     for(int i = 1; i < n; i++)
68     {
69         int u, v;
70         scanf("%d %d", &u, &v);
71         add(u, v), add(v, u);
72     }
73     memset(vis, -1, sizeof(vis));
74     solve(1, 0);
75     for(int i = 1; i <= n; i++) printf("%c ", 'A' + vis[i]);
76     return 0;
77 }
Aguin

CodeForces - 727E

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <map>
  6 #include <set>
  7 #include <string>
  8 #include <vector>
  9 using namespace std;
 10 typedef pair<int, int> pii;
 11 typedef long long LL;
 12 const int maxn = 2e6 + 10;
 13 char s[maxn], ss[maxn];
 14 map<pii, int> s1;
 15 set<pii> s2;
 16 vector<int> v;
 17 
 18 struct Hash_map
 19 {
 20     const int seed = 131;
 21     int mod[2] = {1000000007, 1000000009};
 22     int P[2][maxn], sum[2][maxn];
 23     map<pii, int> M;
 24 
 25     void init()
 26     {
 27         for(int t = 0; t <= 1; t++)
 28         {
 29             P[t][0] = 1;
 30             for(int i = 1; i < maxn; i++)
 31                 P[t][i] = (LL) P[t][i-1] * seed % mod[t];
 32         }
 33     }
 34 
 35     void pre(char * s)
 36     {
 37         int len = strlen(s + 1);
 38         for(int t = 0; t <= 1; t++)
 39         {
 40             sum[t][0] = 0;
 41             for(int i = 1; i <= len; i++)
 42                 sum[t][i] = ((LL) sum[t][i-1] * seed + s[i]) % mod[t];
 43         }
 44     }
 45 
 46     pii Hash(string s)
 47     {
 48         int ret[2] = {0};
 49         int len = s.length();
 50         for(int t = 0; t <= 1; t++)
 51             for(int i = 0; i < len; i++)
 52                 ret[t] = ((LL) ret[t] * seed + s[i]) % mod[t];
 53         return pii(ret[0], ret[1]);
 54     }
 55 
 56     pii Hash(char * s)
 57     {
 58         int ret[2] = {0};
 59         int len = strlen(s);
 60         for(int t = 0; t <= 1; t++)
 61             for(int i = 0; i < len; i++)
 62                 ret[t] = ((LL) ret[t] * seed + s[i]) % mod[t];
 63         return pii(ret[0], ret[1]);
 64     }
 65 
 66     pii Hash(int l, int r)
 67     {
 68         int ret[2];
 69         for(int t = 0; t <= 1; t++)
 70             ret[t] = (sum[t][r] - (LL) sum[t][l-1] * P[t][r-l+1] % mod[t] + mod[t]) % mod[t];
 71         return pii(ret[0], ret[1]);
 72     }
 73 } H;
 74 
 75 int main(void)
 76 {
 77     H.init();
 78     int n, k, g;
 79     scanf("%d %d %s %d", &n, &k, s + 1, &g);
 80     for(int i = 1; i <= g; i++)
 81     {
 82         scanf("%s", ss);
 83         s1[H.Hash(ss)] = i;
 84     }
 85     H.pre(s);
 86     int flag = 0;
 87     for(int i = 1; i <= k; i++)
 88     {
 89         int ok = 1;
 90         s2.clear(); v.clear();
 91         for(int j = 0; j < n - 1; j++)
 92         {
 93             pii tmp = H.Hash(i + j * k, i + j * k + k - 1);
 94             if(s1.find(tmp) == s1.end() || s2.find(tmp) != s2.end()) {ok = 0; break;}
 95             s2.insert(tmp);
 96             v.push_back(s1[tmp]);
 97         }
 98         pii t1 = H.Hash(i + (n - 1) * k, n * k);
 99         pii t2 = H.Hash(1, i - 1);
100         t1.first = ((LL) t1.first * H.P[0][i-1] % H.mod[0] + t2.first) % H.mod[0];
101         t1.second = ((LL) t1.second * H.P[1][i-1] % H.mod[1] + t2.second) % H.mod[1];
102         if(s1.find(t1) == s1.end() || s2.find(t1) != s2.end()) ok = 0;
103         else s2.insert(t1), v.push_back(s1[t1]);
104         if(ok)
105         {
106             flag = 1;
107             puts("YES");
108             for(int j = 0; j < n; j++) printf("%d ", v[j]);
109             puts("");
110             break;
111         }
112     }
113     if(!flag) puts("NO");
114     return 0;
115 }
Aguin

1.27

CodeForces - 449A

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 typedef long long LL;
 6 
 7 int main(void)
 8 {
 9     int n, m, k;
10     scanf("%d %d %d", &n, &m, &k);
11     if(k > n + m - 2) return puts("-1");
12     LL ans = 0;
13     for(int i = max(0, k - m + 1); i < min(n, k + 1); )
14     {
15         ans = max(ans, (LL) (n / (i + 1)) * (m / (k - i + 1)));
16         if(n / (i + 1) == 0) break;
17         i = n / (n / (i + 1));
18     }
19     for(int i = max(0, k - n + 1); i < min(m, k + 1); )
20     {
21         ans = max(ans, (LL) (m / (i + 1)) * (n / (k - i + 1)));
22         if(m / (i + 1) == 0) break;
23         i = m / (m / (i + 1));
24     }
25     printf("%I64d\n", ans);
26     return 0;
27 }
Aguin

CodeForces - 449D

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 typedef long long LL;
 5 const LL mod = 1000000007;
 6 const int maxn = 1111111;
 7 int a[maxn], cnt[maxn];
 8 LL p[maxn], g[maxn];
 9 
10 int main(void)
11 {
12     p[0] = 1;
13     for(int i = 1; i < maxn; i++) p[i] = p[i-1] * 2 % mod;
14     int n;
15     scanf("%d", &n);
16     for(int i = 1; i <= n; i++) scanf("%d", a + i);
17     for(int i = 1; i <= n; i++) cnt[a[i]]++;
18     for(int i = 0; i <= 20; i++)
19         for(int j = 0; j <= 1000000; j++)
20             if(j & (1 << i)) cnt[j^(1 << i)] += cnt[j];
21     for(int i = 0; i <= 1000000; i++) g[i] = (p[cnt[i]] + mod - 1) % mod;
22     for(int i = 0; i <= 20; i++)
23         for(int j = 0; j <= 1000000; j++)
24             if(j & (1 << i)) g[j^(1 << i)] = (g[j^(1 << i)] - g[j] + mod) % mod;
25     printf("%I64d\n", g[0]);
26     return 0;
27 }
Aguin

1.28

HYSBZ - 2118

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <queue>
 6 using namespace std;
 7 typedef long long LL;
 8 typedef pair<int, int> pii;
 9 const int maxn = 1e6 + 10;
10 const LL INF = 1e15;
11 int N, a[22];
12 
13 LL dist[maxn];
14 void dijkstra()
15 {
16     priority_queue<pii, vector<pii>, greater<pii> > pq;
17     fill(dist, dist + maxn, INF);
18     dist[0] = 0;
19     pq.push(pii(0, 0));
20     while(!pq.empty())
21     {
22         pii tmp = pq.top(); pq.pop();
23         int v = tmp.second;
24         if(dist[v] < tmp.first) continue;
25         for(int i = 1; i < N; i++)
26         {
27             int to = (v + a[i]) % a[N];
28             if(dist[to] > dist[v] + a[i])
29             {
30                 dist[to] = dist[v] + a[i];
31                 pq.push(pii(dist[to], to));
32             }
33         }
34     }
35 }
36 
37 LL cal(LL x)
38 {
39     LL ret = 0;
40     for(int i = 0; i < a[N]; i++)
41         if(dist[i] != INF && x >= dist[i]) ret += (x - dist[i]) / a[N] + 1;
42     return ret;
43 }
44 
45 int main(void)
46 {
47     LL l, r;
48     scanf("%d %lld %lld", &N, &l, &r);
49     for(int i = 1; i <= N; i++) scanf("%d", a + i);
50     sort(a + 1, a + 1 + N);
51     if(a[N] == 0) puts("0");
52     else
53     {
54         dijkstra();
55         LL ans = cal(r) - cal(l - 1);
56         printf("%lld\n", ans);
57     }
58     return 0;
59 }
Aguin

CodeForces - 611D

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 typedef long long LL;
 6 const int mod = 1e9 + 7;
 7 int dp[5005][5005], sum[5005][5005];
 8 char s[5005];
 9 int lcp[5005][5005];
10 
11 int main(void)
12 {
13     int n;
14     scanf("%d", &n);
15     scanf("%s", s + 1);
16     for(int i = n; i >= 1; i--)
17     for(int j = n; j >= i; j--)
18         lcp[i][j] = s[i] == s[j] ? lcp[i+1][j+1] + 1 : 0;
19     for(int i = 1; i <= n; i++)
20     {
21         dp[i][1] = 1;
22         for(int j = 2; j <= i; j++)
23         {
24             if(s[j] == '0') continue;
25             int len = i - j + 1, k = max(1, j - len + 1);
26             if(k < j) dp[i][j] = (dp[i][j] + sum[j-1][j-1] - sum[j-1][k-1] + mod) % mod;
27             if(j > len && lcp[j-len][j] < len && s[j+lcp[j-len][j]] > s[j-len+lcp[j-len][j]]) dp[i][j] = (dp[i][j] + dp[j-1][j-len]) % mod;
28         }
29         sum[i][1] = 1;
30         for(int j = 2; j <= i; j++) sum[i][j] = (sum[i][j-1] + dp[i][j]) % mod;
31     }
32     LL ans = 0;
33     for(int i = 1; i <= n; i++) ans = (ans + dp[n][i]) % mod;
34     printf("%I64d\n", ans);
35     return 0;
36 }
Aguin

1.29

CodeForces - 714E

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cmath>
 5 using namespace std;
 6 typedef long long LL;
 7 int a[3333], b[3333];
 8 LL dp[3333][3333], m[3333][3333];
 9 
10 int main(void)
11 {
12     int n;
13     scanf("%d", &n);
14     for(int i = 1; i <= n; i++) scanf("%d", a + i), a[i] -= i, b[i] = a[i];
15     sort(b + 1, b + 1 + n);
16     int cnt = unique(b + 1, b + 1 + n) - b - 1;
17     for(int i = 1; i <= n; i++)
18     {
19         for(int j = 1; j <= cnt; j++)
20             dp[i][j] = m[i-1][j] + abs(a[i] - b[j]);
21         m[i][1] = dp[i][1];
22         for(int j = 2; j <= cnt; j++)
23             m[i][j] = min(m[i][j-1], dp[i][j]);
24     }
25     LL ans = 1e18;
26     for(int i = 1; i <= cnt; i++) ans = min(ans, dp[n][i]);
27     printf("%I64d\n", ans);
28     return 0;
29 }
Aguin

CodeForces - 488C

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 int main(void)
 7 {
 8     int hy, ay, dy, hm, am, dm, h, a, d;
 9     scanf("%d %d %d %d %d %d %d %d %d", &hy, &ay, &dy, &hm, &am, &dm, &h, &a, &d);
10     int ans = 1e9;
11     for(int i = 0; i <= 200; i++)
12     for(int j = 0; j <= 100; j++)
13     {
14         int x = max(0, ay + i - dm);
15         int y = max(0, am - dy - j);
16         if(!x) continue;
17         int tx = (hm + x - 1) / x;
18         int hp = tx * y + 1;
19         ans = min(ans, i * a + j * d + h * max(0, hp - hy));
20     }
21     printf("%d\n", ans);
22     return 0;
23 }
Aguin

SPOJ - LEXSTR

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <set>
 5 using namespace std;
 6 const int maxn = 1e5 + 10;
 7 char s[maxn];
 8 multiset<int> S[maxn];
 9 
10 int fa[maxn];
11 int Find(int x)
12 {
13     return fa[x] == x ? x : fa[x] = Find(fa[x]);
14 }
15 void Union(int x, int y)
16 {
17     x = Find(x);
18     y = Find(y);
19     if(x != y) fa[y] = x;
20 }
21 
22 int main(void)
23 {
24     int T;
25     scanf("%d", &T);
26     while(T--)
27     {
28         scanf("%s", s);
29         int len = strlen(s), m;
30         for(int i = 0; i < len; i++) fa[i] = i;
31         scanf("%d", &m);
32         while(m--)
33         {
34             int a, b;
35             scanf("%d %d", &a, &b);
36             Union(a, b);
37         }
38         for(int i = 0; i < len; i++) S[Find(i)].insert(s[i] - 'a');
39         for(int i = 0; i < len; i++)
40         {
41             s[i] = (*S[Find(i)].begin()) + 'a';
42             S[Find(i)].erase(S[Find(i)].begin());
43         }
44         puts(s);
45     }
46     return 0;
47 }
Aguin

1.30

SPOJ - MOON1

 1 // sboj卡常卡不过
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cstring>
 5 using namespace std;
 6 typedef long long LL;
 7 const LL mod = 1e9 + 7;
 8 const int maxn = 446;
 9 LL C[maxn][maxn];
10 
11 struct Matrix
12 {
13     LL m[maxn][maxn];
14     Matrix(){memset(m, 0, sizeof(m));}
15     void E(){memset(m, 0, sizeof(m)); for(int i = 0; i < maxn; i++) m[i][i] = 1;}
16 };
17 
18 Matrix M_mul(Matrix a, Matrix b, int r)
19 {
20     Matrix ret;
21     for(int i = 0; i <= r; i++)
22         for(int k = 0; k <= r; k++)
23             if(a.m[i][k])
24                 for(int j = 0; j <= r; j++)
25                     if(b.m[k][j])
26                         ret.m[i][j] = ( ret.m[i][j] + a.m[i][k] * b.m[k][j]) % mod;
27     return ret;
28 }
29 
30 Matrix M_qpow(Matrix P, LL n, int r)
31 {
32     Matrix ret;
33     ret.E();
34     while(n)
35     {
36         if(n & 1LL) ret = M_mul(ret, P, r);
37         n >>= 1LL;
38         P = M_mul(P, P, r);
39     }
40     return ret;
41 }
42 
43 int main(void)
44 {
45     for(int i = 0; i < maxn; i++) C[i][0] = C[i][i] = 1;
46     for(int i = 2; i < maxn; i++)
47         for(int j = 1; j <= i; j++)
48             C[i][j] = (C[i-1][j-1] + C[i-1][j]) % mod;
49     int T;
50     scanf("%d", &T);
51     while(T--)
52     {
53         int N, a, r;
54         scanf("%d %d %d", &N, &a, &r);
55         Matrix M;
56         for(int i = 0; i <= r; i++)
57             for(int j = 0; j <= i; j++)
58                 M.m[i][j] = C[i][j] * a % mod;
59         M.m[r+1][r] = M.m[r+1][r+1] = 1;
60         M = M_qpow(M, N, r + 1);
61         LL ans = 0LL;
62         for(int i = 0; i <= r; i++) ans = (ans + M.m[r+1][i] * a) % mod;
63         printf("%lld\n", ans);
64     }
65     return 0;
66 }
Aguin

CodeForces - 276D

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 typedef long long LL;
 5 
 6 int main(void)
 7 {
 8     LL l, r;
 9     scanf("%I64d %I64d", &l, &r);
10     l ^= r;
11     int cnt = 0;
12     while(l) l >>= 1, cnt++;
13     printf("%I64d\n", (1LL << cnt) - 1);
14     return 0;
15 }
Aguin

HDU - 5269

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 typedef long long LL;
 6 const LL mod = 998244353;
 7 
 8 const int maxnode = 2000000, sigma_size = 2;
 9 struct Trie
10 {
11     int ch[maxnode][sigma_size];
12     int val[maxnode];
13     int sz;
14     Trie() {sz = 1; memset(ch[0], 0, sizeof(ch[0]));}
15 
16     void insert(char *s)
17     {
18         int u = 0, n = strlen(s);
19         for(int i = 0; i < n; i++)
20         {
21             int c = s[i] - '0';
22             if(!ch[u][c])
23             {
24                 memset(ch[sz], 0, sizeof(ch[sz]));
25                 val[sz] = 0;
26                 ch[u][c] = sz++;
27             }
28             u = ch[u][c];
29             val[u]++;
30         }
31     }
32 
33     LL solve(int x, int dep)
34     {
35         LL ret = 0;
36         if(ch[x][0] && ch[x][1]) ret = (ret + (1LL << dep) % mod * val[ch[x][0]] % mod * val[ch[x][1]] % mod) % mod;
37         if(ch[x][0]) ret = (ret + solve(ch[x][0], dep + 1)) % mod;
38         if(ch[x][1]) ret = (ret + solve(ch[x][1], dep + 1)) % mod;
39         return ret;
40     }
41 
42 } _;
43 
44 int main(void)
45 {
46     int T;
47     scanf("%d", &T);
48     for(int kase = 1; kase <= T; kase++)
49     {
50         int n;
51         scanf("%d", &n);
52         _ = Trie();
53         for(int i = 1; i <= n; i++)
54         {
55             LL x;
56             char s[33];
57             int cnt = 0;
58             scanf("%I64d", &x);
59             while(x) s[cnt++] = (x % 2) + '0', x /= 2;
60             for(int j = cnt; j < 30; j++) s[j] = '0'; s[30] = 0;
61             _.insert(s);
62         }
63         printf("Case #%d: %I64d\n", kase, (_.solve(0, 0) << 1) % mod);
64     }
65     return 0;
66 }
Aguin

CodeForces - 382B

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 typedef long long LL;
 6 LL vis[1111], aa[1111];
 7 
 8 int main(void)
 9 {
10     LL a, b, w, x, c;
11     scanf("%I64d %I64d %I64d %I64d %I64d", &a, &b, &w, &x, &c);
12     if(c <= a) return puts("0");
13     LL da = 0, dc = 0;
14     for(LL i = 1; i <= w + w; i++)
15     {
16         dc++;
17         if(b >= x) b = b - x;
18         else da++, b = w - (x - b);
19         aa[i] = a - da;
20         if(c - dc <= a - da) {printf("%I64d\n", i); break;}
21         if(vis[b])
22         {
23             LL vb = vis[b];
24             LL d = i - vb + aa[i] - aa[vb];
25             LL nd = c - dc - a + da;
26             LL t = max(0LL, nd / d - 1);
27             dc += t * d;
28             if(c - dc <= a - da) {printf("%I64d\n", i + t * (i - vb)); break;}
29             for(LL j = 1; ; j++)
30             {
31                 dc++;
32                 if(b >= x) b = b - x;
33                 else da++, b = w - (x - b);
34                 if(c - dc <= a - da) {printf("%I64d\n", i + t * (i - vb) + j); break;}
35             }
36             break;
37         }
38         vis[b] = i;
39     }
40     return 0;
41 }
Aguin

CodeForces - 385D

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <cmath>
 5 using namespace std;
 6 const double pi = acos(-1);
 7 const double eps = 1e-8;
 8 int x[1111], y[1111], a[1111];
 9 double dp[1<<20];
10 
11 int main(void)
12 {
13     int n, l, r;
14     scanf("%d %d %d", &n, &l, &r);
15     for(int i = 1; i <= n; i++) scanf("%d %d %d", x + i, y + i, a + i);
16     for(int i = 0; i < (1 << n); i++) dp[i] = l;
17     for(int i = 0; i < (1 << n); i++)
18     {
19         for(int j = 1; j <= n; j++)
20         {
21             if(i & (1 << (j - 1))) continue;
22             double alpha = a[j] * pi / 180;
23             double beta = acos((x[j] - dp[i]) / sqrt((x[j] - dp[i]) * (x[j] - dp[i]) + y[j] * y[j]));
24             double gamma = pi - alpha - beta;
25             if(gamma < eps) dp[i ^ (1 << (j - 1))] = r;
26             else
27             {
28                 double d = sqrt((x[j] - dp[i]) * (x[j] - dp[i]) + y[j] * y[j]) / sin(gamma) * sin(alpha);
29                 dp[i ^ (1 << (j - 1))] = max(dp[i ^ (1 << (j - 1))], dp[i] + d);
30             }
31         }
32     }
33     printf("%.9f\n", min(1.0 * r, dp[(1<<n)-1]) - l);
34     return 0;
35 }
Aguin

1.31

CodeForces - 478E

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <unordered_map>
  4 using namespace std;
  5 typedef long long LL;
  6 unordered_map<LL, int> S[2][10];
  7 int d[22], dd[22];
  8 
  9 int main(void)
 10 {
 11     LL n, k;
 12     scanf("%I64d %I64d", &n, &k);
 13     for(int i = 1; i < 1e7; i++)
 14     {
 15         int cnt = 0, tmp = i;
 16         while(tmp) d[++cnt] = tmp % 10, tmp /= 10;
 17         if(cnt == 1) {if(i % n == 0) k--;}
 18         else if(cnt == 2) {if(i % n == 0 && d[1] != d[2]) k--;}
 19         else
 20         {
 21             int ok = 1;
 22             for(int j = 1; j < cnt - 1; j++)
 23                 if(d[j] < d[j+1] && d[j+1] > d[j+2] || d[j] > d[j+1] && d[j+1] < d[j+2]) continue;
 24                 else ok = 0;
 25             if(ok)
 26             {
 27                 if(i % n == 0)
 28                     k--;
 29                 if(cnt == 7)
 30                     if(d[cnt] > d[cnt-1]) S[0][d[cnt]][i%n]++;
 31                     else S[1][d[cnt]][i%n]++;
 32                 if(cnt == 6 && d[cnt] > d[cnt-1]) S[1][0][i%n]++;
 33             }
 34         }
 35         if(!k) {printf("%d\n", i); return 0;}
 36     }
 37 
 38     for(int i = 1; i < 1e7; i++)
 39     {
 40         int cnt = 0, tmp = i;
 41         LL m = (n - (long long) i * 10000000 % n) % n;
 42         while(tmp) d[++cnt] = tmp % 10, tmp /= 10;
 43         if(cnt == 1)
 44         {
 45             for(int j = 0; j < d[1]; j++)
 46             {
 47                 if(S[1][j].find(m) == S[1][j].end()) continue;
 48                 if(k > S[1][j][m]) k -= S[1][j][m];
 49                 else
 50                 {
 51                     printf("%d", i);
 52                     for(int p = 1e5; p < 1e7; p++)
 53                     {
 54                         if(p % n != m) continue;
 55                         int ccnt = 0, tmpp = p;
 56                         while(tmpp) dd[++ccnt] = tmpp % 10, tmpp /= 10;
 57                         if(p < 1e6) dd[++ccnt] = 0;
 58                         if(dd[ccnt] != j) continue;
 59                         int okk = 1;
 60                         for(int jj = 1; jj < ccnt - 1; jj++)
 61                             if(dd[jj] < dd[jj+1] && dd[jj+1] > dd[jj+2] || dd[jj] > dd[jj+1] && dd[jj+1] < dd[jj+2]) continue;
 62                             else okk = 0;
 63                         if(okk)
 64                         {
 65                             if(dd[ccnt] > dd[ccnt-1]) continue;
 66                             else k--;
 67                         }
 68                         if(!k) {printf("%07d\n", p); return 0;}
 69                     }
 70                 }
 71             }
 72             for(int j = d[1] + 1; j < 10; j++)
 73             {
 74                 if(S[0][j].find(m) == S[0][j].end()) continue;
 75                 if(k > S[0][j][m]) k -= S[0][j][m];
 76                 else
 77                 {
 78                     printf("%d", i);
 79                     for(int p = 1e5; p < 1e7; p++)
 80                     {
 81                         if(p % n != m) continue;
 82                         int ccnt = 0, tmpp = p;
 83                         while(tmpp) dd[++ccnt] = tmpp % 10, tmpp /= 10;
 84                         if(p < 1e6) dd[++ccnt] = 0;
 85                         if(dd[ccnt] != j) continue;
 86                         int okk = 1;
 87                         for(int jj = 1; jj < ccnt - 1; jj++)
 88                             if(dd[jj] < dd[jj+1] && dd[jj+1] > dd[jj+2] || dd[jj] > dd[jj+1] && dd[jj+1] < dd[jj+2]) continue;
 89                             else okk = 0;
 90                         if(okk)
 91                         {
 92                             if(dd[ccnt] < dd[ccnt-1]) continue; // S[0][d[cnt]][i%n]++;
 93                             else k--;
 94                         }
 95                         if(!k) {printf("%07d\n", p); return 0;}
 96                     }
 97                 }
 98             }
 99         }
100         else
101         {
102             int ok = 1;
103             for(int j = 1; j < cnt - 1; j++)
104                 if(d[j] < d[j+1] && d[j+1] > d[j+2] || d[j] > d[j+1] && d[j+1] < d[j+2]) continue;
105                 else ok = 0;
106             if(ok)
107             {
108                 if(d[1] < d[2])
109                 {
110                     for(int j = d[1] + 1; j < 10; j++)
111                     {
112                         if(S[0][j].find(m) == S[0][j].end()) continue;
113                         if(k > S[0][j][m]) k -= S[0][j][m];
114                         else
115                         {
116                             printf("%d", i);
117                             for(int p = 1e5; p < 1e7; p++)
118                             {
119                                 if(p % n != m) continue;
120                                 int ccnt = 0, tmpp = p;
121                                 while(tmpp) dd[++ccnt] = tmpp % 10, tmpp /= 10;
122                                 if(p < 1e6) dd[++ccnt] = 0;
123                                 if(dd[ccnt] != j) continue;
124                                 int okk = 1;
125                                 for(int jj = 1; jj < ccnt - 1; jj++)
126                                     if(dd[jj] < dd[jj+1] && dd[jj+1] > dd[jj+2] || dd[jj] > dd[jj+1] && dd[jj+1] < dd[jj+2]) continue;
127                                     else okk = 0;
128                                 if(okk)
129                                 {
130                                     if(dd[ccnt] < dd[ccnt-1]) continue; // S[0][d[cnt]][i%n]++;
131                                     else k--;
132                                 }
133                                 if(!k) {printf("%07d\n", p); return 0;}
134                             }
135                         }
136                     }
137                 }
138                 else if(d[2] < d[1])
139                 {
140                     for(int j = 0; j < d[1]; j++)
141                     {
142                         if(S[1][j].find(m) == S[1][j].end()) continue;
143                         if(k > S[1][j][m]) k -= S[1][j][m];
144                         else
145                         {
146                             printf("%d", i);
147                             for(int p = 1e5; p < 1e7; p++)
148                             {
149                                 if(p % n != m) continue;
150                                 int ccnt = 0, tmpp = p;
151                                 while(tmpp) dd[++ccnt] = tmpp % 10, tmpp /= 10;
152                                 if(p < 1e6) dd[++ccnt] = 0;
153                                 if(dd[ccnt] != j) continue;
154                                 int okk = 1;
155                                 for(int jj = 1; jj < ccnt - 1; jj++)
156                                     if(dd[jj] < dd[jj+1] && dd[jj+1] > dd[jj+2] || dd[jj] > dd[jj+1] && dd[jj+1] < dd[jj+2]) continue;
157                                     else okk = 0;
158                                 if(okk)
159                                 {
160                                     if(dd[ccnt] > dd[ccnt-1]) continue; // S[0][d[cnt]][i%n]++;
161                                     else k--;
162                                 }
163                                 if(!k) {printf("%07d\n", p); return 0;}
164                             }
165                         }
166                     }
167                 }
168             }
169         }
170     }
171     puts("-1");
172     return 0;
173 }
Aguin

CodeForces - 717D

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int maxn = 266;
 6 
 7 struct Matrix
 8 {
 9     double m[maxn][maxn];
10     Matrix(){memset(m, 0, sizeof(m));}
11     void E(){memset(m, 0, sizeof(m)); for(int i = 0; i < maxn; i++) m[i][i] = 1;}
12 };
13 
14 Matrix M_mul(Matrix a, Matrix b)
15 {
16     Matrix ret;
17     for(int i = 0; i < maxn; i++)
18         for(int j = 0; j < maxn; j++)
19             for(int k = 0; k < maxn; k++)
20                 ret.m[i][j] +=  a.m[i][k] * b.m[k][j];
21     return ret;
22 }
23 
24 Matrix M_qpow(Matrix P, int n)
25 {
26     Matrix ret;
27     ret.E();
28     while(n)
29     {
30         if(n & 1) ret = M_mul(ret, P);
31         n >>= 1;
32         P = M_mul(P, P);
33     }
34     return ret;
35 }
36 
37 double p[111];
38 int main(void)
39 {
40     int n, x;
41     scanf("%d %d", &n, &x);
42     for(int i = 0; i <= x; i++) scanf("%lf", p + i);
43     Matrix M;
44     for(int i = 0; i <= 255; i++)
45         for(int j = 0; j <= x; j++)
46             M.m[i][i^j] += p[j];
47     M = M_qpow(M, n - 1);
48     double ans = 0;
49     for(int i = 1; i <= 255; i++)
50         for(int j = 0; j <= x; j++)
51             ans += M.m[i][j] * p[j];
52     printf("%.9f\n", ans);
53     return 0;
54 }
Aguin

CodeForces - 371E

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 typedef long long LL;
 6 typedef pair<int, int> pii;
 7 const int maxn = 3e5 + 10;
 8 pii p[maxn];
 9 
10 int main(void)
11 {
12     int n, k;
13     scanf("%d", &n);
14     for(int i = 1; i <= n; i++) scanf("%d", &p[i].first), p[i].second = i;
15     scanf("%d", &k);
16     sort(p + 1, p + 1 + n);
17     LL sum = 0, tmp = 0;
18     for(int i = 1; i <= k; i++) sum += p[i].first, tmp += (LL) p[i].first * i - sum;
19     LL ans = tmp, st = 1;
20     for(int i = k + 1; i <= n; i++)
21     {
22         tmp -= sum - (LL) p[i-k].first * k;
23         sum -= p[i-k].first;
24         sum += p[i].first;
25         tmp += (LL) p[i].first * k - sum;
26         if(tmp < ans) ans = tmp, st = i - k + 1;
27     }
28     for(int i = st; i < st + k; i++) printf("%d ", p[i].second);
29     return 0;
30 }
Aguin

2.2

CodeForces - 448E

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <vector>
 4 #include <algorithm>
 5 using namespace std;
 6 typedef long long LL;
 7 const int maxn = 1e5;
 8 int st[maxn], ed[maxn];
 9 vector<LL> Y, Y1;
10 
11 vector<LL> solve(vector<LL> a, vector<LL> b)
12 {
13     int sa = a.size(), sb = b.size(), sz = Y.size();
14     for(int j = st[0] = 0; j < sb; j++) if(j == maxn - 1 || b[j] == Y[0]) {ed[0] = j; break;}
15     for(int i = 1; i < sz; i++)
16     {
17         if(ed[i-1] == maxn - 1)
18             break;
19         for(int j = st[i] = ed[i-1] + 1; j < sb; j++)
20             if(j == maxn - 1|| b[j] == Y[i]) {ed[i] = j; break;}
21     }
22     vector<LL> ret;
23     for(int i = 0; i < sa; i++)
24     {
25         int id = lower_bound(Y.begin(), Y.end(), a[i]) - Y.begin();
26         for(int j = st[id]; j <= ed[id]; j++)
27         {
28             ret.push_back(b[j]);
29             if(ret.size() == maxn) break;
30         }
31         if(ret.size() == maxn) break;
32     }
33     return ret;
34 }
35 
36 int main(void)
37 {
38     LL X, k;
39     scanf("%I64d %I64d", &X, &k);
40     if(!k) {printf("%I64d\n", X); return 0;}
41     for(LL i = 1; i * i <= X; i++)
42     {
43         if(X % i == 0)
44         {
45             Y.push_back(i);
46             if(i * i != X) Y.push_back(X / i);
47         }
48     }
49     sort(Y.begin(), Y.end());
50     for(int i = 0; i < Y.size(); i++)
51     {
52         for(int j = 0; j < Y.size(); j++)
53         {
54             if(Y[i] % Y[j] == 0) Y1.push_back(Y[j]);
55             if(Y1.size() == maxn) break;
56         }
57         if(Y1.size() == maxn) break;
58     }
59     k--;
60     vector<LL> ans = Y, a = Y1;
61     while(k)
62     {
63         if(k & 1) ans = solve(ans, a);
64         a = solve(a, a);
65         k >>= 1;
66     }
67     for(int i = 0; i < ans.size(); i++) printf("%I64d ", ans[i]);
68     return 0;
69 }
Aguin

CodeForces - 451D

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int maxn = 1e5 + 10;
 6 typedef long long LL;
 7 char s[maxn];
 8 int cnt[2][2];
 9 
10 int main(void)
11 {
12     scanf("%s", s + 1);
13     int len = strlen(s + 1);
14     LL ans1 = 0, ans2 = 0;
15     for(int i = 1; i <= len; i++)
16     {
17         if(s[i] == 'a')
18         {
19             if(i % 2) cnt[1][0]++, ans1 += cnt[0][0], ans2 += cnt[1][0];
20             else cnt[0][0]++, ans1 += cnt[1][0], ans2 += cnt[0][0];
21         }
22         else
23         {
24             if(i % 2) cnt[1][1]++, ans1 += cnt[0][1], ans2 += cnt[1][1];
25             else cnt[0][1]++, ans1 += cnt[1][1], ans2 += cnt[0][1];
26         }
27     }
28     printf("%I64d %I64d\n", ans1, ans2);
29     return 0;
30 }
Aguin

2.5

CodeForces - 662A

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 typedef long long LL;
 6 const int maxn = 5e5 + 10;
 7 LL a[maxn], b[maxn], c[maxn];
 8 int vis[66];
 9 
10 int main(void)
11 {
12     int n;
13     scanf("%d", &n);
14     LL s = 0;
15     for(int i = 1; i <= n; i++) scanf("%I64d %I64d", a + i, b + i);
16     for(int i = 1; i <= n; i++) c[i] = a[i] ^ b[i], s ^= a[i];
17     int fr = n, ok = 1;
18     for(int i = 1; i <= 60; i++)
19     {
20         LL tmp = 1LL << (i - 1);
21         int x = 0;
22         for(int j = i; j <= n; j++)
23         if(tmp & c[j]) {x = j; break;}
24         if(!x) continue;
25         vis[i] = 1;
26         for(int j = 1; j <= n; j++)
27         if(j != x && (tmp & c[j])) c[j] ^= c[x];
28         swap(c[i], c[x]);
29     }
30     for(int i = 1; i <= 60; i++)
31     {
32         LL tmp = 1LL << (i - 1);
33         if(s & tmp)
34         {
35             if(vis[i]) s ^= c[i], fr--;
36             else {ok = 0; break;}
37         }
38         else if(vis[i]) fr--;
39     }
40     if(!ok) puts("1/1");
41     else printf("%I64d/%I64d\n", (1LL << (n - fr)) - 1, 1LL << (n - fr));
42     return 0;
43 }
Aguin

CodeForces - 448C

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 int a[5555];
 6 
 7 int solve(int l, int r, int h)
 8 {
 9     int ret = 0;
10     int m = a[l];
11     for(int i = l; i <= r; i++) m = min(m, a[i]);
12     for(int i = l; i <= r; i++)
13     {
14         if(a[i] == m) continue;
15         for(int j = i; j <= r; j++)
16         {
17             if(j == r || a[j+1] == m)
18             {
19                 ret += solve(i, j, m);
20                 i = j;
21                 break;
22             }
23         }
24     }
25     ret = min(ret + m - h, r - l + 1);
26     return ret;
27 }
28 
29 int main(void)
30 {
31     int n;
32     scanf("%d", &n);
33     for(int i = 1; i <= n; i++) scanf("%d", a + i);
34     printf("%d\n", solve(1, n, 0));
35     return 0;
36 }
Aguin

HDU - 5396

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 typedef long long LL;
 6 const LL mod = 1e9 + 7;
 7 LL a[222], dp[222][222];
 8 char s[222];
 9 
10 LL c(LL a, LL b, char op)
11 {
12     if(op == '+') return (a + b) % mod;
13     if(op == '-') return (a - b + mod) % mod;
14     if(op == '*') return a * b % mod;
15 }
16 
17 LL qpow(LL a, LL b)
18 {
19     LL ret = 1LL;
20     while(b)
21     {
22         if(b & 1) ret = ret * a % mod;
23         a = a * a % mod;
24         b >>= 1;
25     }
26     return ret;
27 }
28 
29 LL inv(LL x)
30 {
31     return qpow(x, mod - 2);
32 }
33 
34 int main(void)
35 {
36     int n;
37     while(~scanf("%d", &n))
38     {
39         for(int i = 1; i <= n; i++) scanf("%I64d", a + i);
40         scanf("%s", s + 1);
41         memset(dp, 0, sizeof(dp));
42         for(int i = 1; i <= n; i++) dp[i][i] = a[i];
43         for(int l = 2; l <= n; l++)
44         for(int st = 1; st + l - 1 <= n; st++)
45         {
46             for(int k = st; k < st + l - 1; k++)
47             dp[st][st+l-1] = (dp[st][st+l-1] + c(dp[st][k], dp[k+1][st+l-1], s[k])) % mod;
48             dp[st][st+l-1] = dp[st][st+l-1] * inv(l - 1) % mod;
49         }
50         LL ans = dp[1][n];
51         for(int i = 1; i < n; i++) ans = ans * i % mod;
52         printf("%I64d\n", ans);
53     }
54     return 0;
55 }
Aguin

CodeForces - 715B

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <algorithm>
  5 #include <queue>
  6 #include <vector>
  7 using namespace std;
  8 const int maxn = 1111;
  9 const int INF = 1e9 + 1;
 10 typedef pair<int, int> pii;
 11 int cnt, h[maxn];
 12 int dist[maxn];
 13 
 14 struct edge
 15 {
 16     int to, pre, cost;
 17 } e[2222222];
 18 
 19 void init()
 20 {
 21     cnt = 0;
 22     memset(h, 0, sizeof(h));
 23 }
 24 
 25 void add(int from, int to, int cost)
 26 {
 27     cnt++;
 28     e[cnt].pre = h[from];
 29     e[cnt].to = to;
 30     e[cnt].cost = cost;
 31     h[from] = cnt;
 32 }
 33 
 34 void dijkstra(int s)
 35 {
 36     priority_queue<pii, vector<pii>, greater<pii> > pq;
 37     fill(dist, dist + maxn, INF);
 38     dist[s] = 0;
 39     pq.push(pii(0, s));
 40     while(!pq.empty())
 41     {
 42         pii tmp = pq.top(); pq.pop();
 43         int v = tmp.second;
 44         if(dist[v] < tmp.first) continue;
 45         for(int i = h[v]; i; i = e[i].pre)
 46         {
 47             int to = e[i].to, cost = e[i].cost;
 48             if(dist[to] > dist[v] + cost)
 49             {
 50                 dist[to] = dist[v] + cost;
 51                 pq.push(pii(dist[to], to));
 52             }
 53         }
 54     }
 55 }
 56 
 57 vector<int> vec;
 58 int main(void)
 59 {
 60     init();
 61     int N, M, L, S, T;
 62     scanf("%d %d %d %d %d", &N, &M, &L, &S, &T);
 63     for(int i = 1; i <= M; i++)
 64     {
 65         int u, v, w;
 66         scanf("%d %d %d", &u, &v, &w);
 67         if(!w) vec.push_back(cnt + 1), w = INF;
 68         add(u, v, w), add(v, u, w);
 69     }
 70     dijkstra(S);
 71     if(dist[T] < L) return puts("NO");
 72     for(int i = 0; i < vec.size(); i++) e[vec[i]].cost = 1, e[vec[i]+1].cost = 1;
 73     dijkstra(S);
 74     if(dist[T] > L) return puts("NO");
 75     puts("YES");
 76     int l = 0, r = vec.size();
 77     while(l < r)
 78     {
 79         int mid = l + (r - l) / 2;
 80         for(int i = 0; i < mid; i++) e[vec[i]].cost = 1, e[vec[i]+1].cost = 1;
 81         for(int i = mid; i < vec.size(); i++) e[vec[i]].cost = INF, e[vec[i]+1].cost = INF;
 82         dijkstra(S);
 83         if(dist[T] <= L) r = mid;
 84         else l = mid + 1;
 85     }
 86     for(int i = 0; i < r; i++) e[vec[i]].cost = 1, e[vec[i]+1].cost = 1;
 87     for(int i = r; i < vec.size(); i++) e[vec[i]].cost = INF, e[vec[i]+1].cost = INF;
 88     if(!r)
 89     {
 90         for(int i = 1; i <= cnt; i += 2) printf("%d %d %d\n", e[i+1].to, e[i].to, e[i].cost);
 91         return 0;
 92     }
 93     int Le = 1, R = INF;
 94     while(Le < R)
 95     {
 96         int mid = R - (R - Le) / 2;
 97         e[vec[r-1]].cost = e[vec[r-1]+1].cost = mid;
 98         dijkstra(S);
 99         if(dist[T] > L) R = mid - 1;
100         else Le = mid;
101     }
102     e[vec[r-1]].cost = e[vec[r-1]+1].cost = Le;
103     for(int i = 1; i <= cnt; i += 2) printf("%d %d %d\n", e[i+1].to, e[i].to, e[i].cost);
104     return 0;
105 }
Aguin

LightOJ - 1106

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 int dp[33][222], f[33], d[33], t[33];
 7 int pre[33][222];
 8 
 9 void ans_print(int x, int ti)
10 {
11     if(x != 1) ans_print(x - 1, pre[x][ti]), printf(", ");
12     printf("%d", 5 * (pre[x][ti] - ti - t[x]));
13 }
14 
15 int main(void)
16 {
17     int T;
18     scanf("%d", &T);
19     for(int kase = 1; kase <= T; kase++)
20     {
21         int n, h;
22         scanf("%d %d", &n, &h);
23         for(int i = 1; i <= n; i++) scanf("%d", f + i);
24         for(int i = 1; i <= n; i++) scanf("%d", d + i);
25         for(int i = 2; i <= n; i++) scanf("%d", t + i);
26         memset(dp, -1, sizeof(dp));
27         dp[0][h*12] = 0;
28         for(int i = 1; i <= n; i++)
29         for(int j = t[i]; j <= h * 12; j++)
30         {
31             if(dp[i-1][j] == -1) continue;
32             for(int k = 0; k <= j - t[i]; k++)
33             {
34                 int v;
35                 if(d[i])
36                 {
37                     int a = f[i] / d[i];
38                     int b = f[i] % d[i];
39                     if(k <= a) v = k * (f[i] + f[i] - (k - 1) * d[i]) / 2;
40                     else v = a * (f[i] + f[i] - (a - 1) * d[i]) / 2 + b;
41                 }
42                 else v = k * f[i];
43                 int ndp = dp[i-1][j] + v;
44                 if(j >= t[i] + k && ndp > dp[i][j-t[i]-k]) dp[i][j-t[i]-k] = ndp, pre[i][j-t[i]-k] = j;
45             }
46         }
47         printf("Case %d:\n", kase);
48         int ans = dp[n][0], x = n;
49         for(int i = n; i >= 1; i--) if(dp[i][0] >= ans) ans = dp[i][0], x = i;
50         ans_print(x, 0);
51         for(int i = x + 1; i <= n; i++) printf(", 0");
52         printf("\nNumber of fish expected: %d\n", dp[x][0]);
53     }
54     return 0;
55 }
Aguin

2.6

LightOJ - 1125

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 typedef long long LL;
 6 LL dp[22][22];
 7 int a[222];
 8 
 9 int main(void)
10 {
11     int T;
12     scanf("%d", &T);
13     for(int kase = 1; kase <= T; kase++)
14     {
15         int N, Q;
16         scanf("%d %d", &N, &Q);
17         for(int i = 1; i <= N; i++) scanf("%lld", a + i);
18         printf("Case %d:\n", kase);
19         while(Q--)
20         {
21             int D, M;
22             scanf("%d %d", &D, &M);
23             memset(dp, 0, sizeof(dp));
24             dp[0][0] = 1;
25             for(int i = 1; i <= N; i++)
26             for(int j = M - 1; j >= 0; j--)
27             for(int k = 0; k < D; k++)
28             {
29                 if(!dp[j][k]) continue;
30                 dp[j+1][(k+a[i]%D+D)%D] += dp[j][k];
31             }
32             printf("%lld\n", dp[M][0]);
33         }
34     }
35     return 0;
36 }
Aguin

SPOJ - DWARFLOG

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 typedef long long LL;
 6 const int INF = 1e9;
 7 const int maxn = 2e5 + 10;
 8 int p[maxn], m[maxn<<2], M[maxn<<2];
 9 
10 void gather(int p)
11 {
12     m[p] = min(m[p<<1], m[p<<1|1]);
13     M[p] = max(M[p<<1], M[p<<1|1]);
14 }
15 
16 void build(int p, int l, int r)
17 {
18     if(l < r)
19     {
20         int mid = (l + r) >> 1;
21         build(p<<1, l, mid);
22         build(p<<1|1, mid + 1, r);
23         gather(p);
24     }
25     else m[p] = M[p] = l;
26 }
27 
28 void modify(int p, int tl, int tr, int x, int v)
29 {
30     if(tr < x || x < tl) return;
31     if(tl == tr)
32     {
33         m[p] = M[p] = v;
34         return;
35     }
36     int mid = (tl + tr) >> 1;
37     modify(p<<1, tl, mid, x, v);
38     modify(p<<1|1, mid+1, tr, x, v);
39     gather(p);
40 }
41 
42 int query_m(int p, int tl, int tr, int l, int r)
43 {
44     if(tr < l || r < tl) return INF;
45     if(l <= tl && tr <= r) return m[p];
46     int mid = (tl + tr) >> 1;
47     int ret = min(query_m(p<<1, tl, mid, l, r), query_m(p<<1|1, mid+1, tr, l, r));
48     return ret;
49 }
50 
51 int query_M(int p, int tl, int tr, int l, int r)
52 {
53     if(tr < l || r < tl) return 0;
54     if(l <= tl && tr <= r) return M[p];
55     int mid = (tl + tr) >> 1;
56     int ret = max(query_M(p<<1, tl, mid, l, r), query_M(p<<1|1, mid+1, tr, l, r));
57     return ret;
58 }
59 
60 int main(void)
61 {
62     int N, M;
63     scanf("%d %d", &N, &M);
64     for(int i = 1; i <= N; i++) p[i] = i;
65     build(1, 1, N);
66     while(M--)
67     {
68         int op, X, Y;
69         scanf("%d %d %d", &op, &X, &Y);
70         if(op == 1)
71         {
72             modify(1, 1, N, p[X], Y);
73             modify(1, 1, N, p[Y], X);
74             swap(p[X], p[Y]);
75         }
76         else
77         {
78             if(p[Y] - p[X] != Y - X) {puts("NO"); continue;}
79             int m = query_m(1, 1, N, p[X], p[Y]);
80             int M = query_M(1, 1, N, p[X], p[Y]);
81             if(m != X || M != Y) {puts("NO"); continue;}
82             puts("YES");
83         }
84     }
85     return 0;
86 }
Aguin

SPOJ - DRUIDEOI

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 using namespace std;
 5 const int maxn = 1e5 + 10;
 6 const int INF = 1e9 + 1;
 7 int h[maxn], l[maxn], r[maxn], st[maxn];
 8 
 9 int main(void)
10 {
11     int T;
12     scanf("%d", &T);
13     while(T--)
14     {
15         int N;
16         scanf("%d", &N);
17         for(int i = 1; i <= N; i++) scanf("%d", h + i);
18         int p = 0;
19         for(int i = 1; i <= N; i++)
20         {
21             while(p && h[st[p]] <= h[i]) p--;
22             st[++p] = i;
23         }
24         for(int i = 1; i <= N; i++)
25         {
26             while(p && h[st[p]] <= h[i]) p--;
27             l[i] = p ? st[p] : -1;
28             st[++p] = i;
29         }
30         p = 0;
31         for(int i = N; i; i--)
32         {
33             while(p && h[st[p]] <= h[i]) p--;
34             st[++p] = i;
35         }
36         for(int i = N; i; i--)
37         {
38             while(p && h[st[p]] <= h[i]) p--;
39             r[i] = p ? st[p] : -1;
40             st[++p] = i;
41         }
42         for(int i = 1; i <= N; i++) printf("%d %d\n", l[i], r[i]);
43     }
44     return 0;
45 }
Aguin

CodeForces - 550D

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 
 5 int main(void)
 6 {
 7     int k;
 8     scanf("%d", &k);
 9     if(k % 2 == 0) puts("NO");
10     else
11     {
12         puts("YES");
13         printf("%d %d\n", k * k + 1, (k * k + 1) * k / 2);
14         for(int i = 1; i <= (k - 1) / 2; i++)
15         {
16             printf("%d %d\n%d %d\n", (i - 1) * (k + 1) + 1, k * k, (i - 1) * (k + 1) + 2, k * k);
17             for(int j = 3; j <= k + 1; j++) printf("%d %d\n", (i - 1) * (k + 1) + 1, (i - 1) * (k + 1) + j);
18             for(int o = 2; o <= k; o++)
19             for(int j = o + 1; j <= k + 1; j++) printf("%d %d\n", (i - 1) * (k + 1) + o, (i - 1) * (k + 1) + j);
20         }
21         for(int i = (k - 1) / 2 + 1; i <= k - 1; i++)
22         {
23             printf("%d %d\n%d %d\n", (i - 1) * (k + 1) + 1, k * k + 1, (i - 1) * (k + 1) + 2, k * k + 1);
24             for(int j = 3; j <= k + 1; j++) printf("%d %d\n", (i - 1) * (k + 1) + 1, (i - 1) * (k + 1) + j);
25             for(int o = 2; o <= k; o++)
26             for(int j = o + 1; j <= k + 1; j++) printf("%d %d\n", (i - 1) * (k + 1) + o, (i - 1) * (k + 1) + j);
27         }
28         printf("%d %d\n", k * k, k * k + 1);
29     }
30     return 0;
31 }
Aguin

SPOJ - TRNGL

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 typedef long long LL;
 5 const LL mod = 100007;
 6 LL dp[1111];
 7 
 8 int main(void)
 9 {
10     dp[3] = 1;
11     for(int i = 4; i <= 1000; i++)
12     {
13         dp[i] = (dp[i] + dp[i-1] + dp[i-1]) % mod;
14         for(int j = 3; j < i - 1; j++)
15         dp[i] = (dp[i] + dp[j] * dp[i-j+1]) % mod;
16     }
17     int T;
18     scanf("%d", &T);
19     while(T--)
20     {
21         int N;
22         scanf("%d", &N);
23         printf("%lld\n", dp[N]);
24     }
25     return 0;
26 }
Aguin

CodeForces - 487C

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 typedef long long LL;
 5 
 6 LL qpow(LL a, LL b, LL mod)
 7 {
 8     LL ret = 1LL;
 9     while(b)
10     {
11         if(b & 1) ret = ret * a % mod;
12         a = a * a % mod;
13         b >>= 1;
14     }
15     return ret;
16 }
17 
18 LL inv(LL x, LL mod)
19 {
20     return qpow(x, mod - 2, mod);
21 }
22 
23 int main(void)
24 {
25     int n;
26     scanf("%d", &n);
27     if(n == 1) puts("YES\n1");
28     else if(n == 4) puts("YES\n1\n3\n2\n4");
29     else
30     {
31         for(int i = 2; i * i <= n; i++)
32             if(n % i == 0) return puts("NO");
33         puts("YES\n1");
34         for(int i = 2; i < n; i++) printf("%I64d\n", i * inv(i - 1, n) % n);
35         printf("%d\n", n);
36     }
37     return 0;
38 }
Aguin

CodeForces - 229C

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 typedef long long LL;
 5 const int maxn = 1e6 + 10;
 6 int a[maxn];
 7 
 8 int main(void)
 9 {
10     int n, m;
11     scanf("%d %d", &n, &m);
12     for(int i = 1; i <= m; i++)
13     {
14         int u, v;
15         scanf("%d %d", &u, &v);
16         a[u]++, a[v]++;
17     }
18     LL ans = 0;
19     for(int i = 1; i <= n; i++) ans += (LL) a[i] * (n - a[i] - 1);
20     ans = (LL) n * (n - 1) * (n - 2) / 6 - ans / 2;
21     printf("%I64d\n", ans);
22     return 0;
23 }
Aguin

 

posted @ 2017-01-14 20:18  Aguin  阅读(231)  评论(0编辑  收藏  举报