3.5
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 typedef long long LL; 7 deque<int> dq[444]; 8 LL sum[444][5]; 9 10 int main(void) 11 { 12 int n; 13 scanf("%d", &n); 14 while(n--) 15 { 16 int x; 17 char op[22]; 18 scanf("%s", op); 19 if(op[0] == 'a') 20 { 21 scanf("%d", &x); 22 int p = 0; 23 while(1) 24 { 25 int sz = dq[p].size(); 26 if(sz < 300 || dq[p][299] > x) 27 { 28 deque<int> tmp; 29 for(int i = 0; i < sz; i++) if(dq[p][i] < x) tmp.push_back(dq[p][i]); 30 tmp.push_back(x); 31 for(int i = 0; i < sz; i++) if(dq[p][i] > x) tmp.push_back(dq[p][i]); 32 dq[p] = tmp; 33 sz++; 34 memset(sum[p], 0, sizeof(sum[p])); 35 for(int i = 0; i < sz; i++) sum[p][i%5] += dq[p][i]; 36 while(dq[p].size() == 301) 37 { 38 int y = dq[p][300]; 39 dq[p].pop_back(); 40 sum[p][0] -= y; 41 p++; 42 dq[p].push_front(y); 43 for(int i = 4; i; i--) swap(sum[p][i], sum[p][i-1]); 44 sum[p][0] += y; 45 } 46 break; 47 } 48 p++; 49 } 50 } 51 else if(op[0] == 'd') 52 { 53 scanf("%d", &x); 54 int p = 0; 55 while(1) 56 { 57 int sz = dq[p].size(); 58 if(sz < 300 || dq[p][299] >= x) 59 { 60 deque<int> tmp; 61 for(int i = 0; i < sz; i++) if(dq[p][i] < x) tmp.push_back(dq[p][i]); 62 for(int i = 0; i < sz; i++) if(dq[p][i] > x) tmp.push_back(dq[p][i]); 63 dq[p] = tmp; 64 sz--; 65 memset(sum[p], 0, sizeof(sum[p])); 66 for(int i = 0; i < sz; i++) sum[p][i%5] += dq[p][i]; 67 while(dq[p+1].size()) 68 { 69 int y = dq[p+1][0]; 70 dq[p+1].pop_front(); 71 sum[p+1][0] -= y; 72 for(int i = 0; i < 4; i++) swap(sum[p+1][i], sum[p+1][i+1]); 73 dq[p].push_back(y); 74 sum[p][4] += y; 75 p++; 76 } 77 break; 78 } 79 p++; 80 } 81 } 82 else 83 { 84 int p = 0; 85 LL ans = 0; 86 while(dq[p].size()) ans += sum[p][2], p++; 87 printf("%I64d ", ans); 88 } 89 } 90 return 0; 91 }
1 #include <cstdio> 2 #include <cstdlib> 3 #include <climits> 4 #include <iostream> 5 #include <algorithm> 6 #include <cstring> 7 #include <string> 8 #include <queue> 9 #include <map> 10 #include <vector> 11 #include <bitset> 12 #include <cmath> 13 #include <set> 14 #include <utility> 15 #include <ctime> 16 #define sqr(x) ((x)*(x)) 17 using namespace std; 18 19 const int N = 1010; 20 const double eps = 1e-8; 21 const double pi = acos(-1.0); 22 double area[N]; 23 int n; 24 25 int dcmp(double x) { 26 if (x < -eps) return -1; else return x > eps; 27 } 28 29 struct cp { 30 double x, y, r, angle; 31 int d; 32 cp(){} 33 cp(double xx, double yy, double ang = 0, int t = 0) { 34 x = xx; y = yy; angle = ang; d = t; 35 } 36 void get() { 37 scanf("%lf%lf%lf", &x, &y, &r); 38 d = 1; 39 } 40 }cir[N], tp[N * 2]; 41 42 double dis(cp a, cp b) { 43 return sqrt(sqr(a.x - b.x) + sqr(a.y - b.y)); 44 } 45 46 double cross(cp p0, cp p1, cp p2) { 47 return (p1.x - p0.x) * (p2.y - p0.y) - (p1.y - p0.y) * (p2.x - p0.x); 48 } 49 50 int CirCrossCir(cp p1, double r1, cp p2, double r2, cp &cp1, cp &cp2) { 51 double mx = p2.x - p1.x, sx = p2.x + p1.x, mx2 = mx * mx; 52 double my = p2.y - p1.y, sy = p2.y + p1.y, my2 = my * my; 53 double sq = mx2 + my2, d = -(sq - sqr(r1 - r2)) * (sq - sqr(r1 + r2)); 54 if (d + eps < 0) return 0; if (d < eps) d = 0; else d = sqrt(d); 55 double x = mx * ((r1 + r2) * (r1 - r2) + mx * sx) + sx * my2; 56 double y = my * ((r1 + r2) * (r1 - r2) + my * sy) + sy * mx2; 57 double dx = mx * d, dy = my * d; sq *= 2; 58 cp1.x = (x - dy) / sq; cp1.y = (y + dx) / sq; 59 cp2.x = (x + dy) / sq; cp2.y = (y - dx) / sq; 60 if (d > eps) return 2; else return 1; 61 } 62 63 bool circmp(const cp& u, const cp& v) { 64 return dcmp(u.r - v.r) < 0; 65 } 66 67 bool cmp(const cp& u, const cp& v) { 68 if (dcmp(u.angle - v.angle)) return u.angle < v.angle; 69 return u.d > v.d; 70 } 71 72 double calc(cp cir, cp cp1, cp cp2) { 73 double ans = (cp2.angle - cp1.angle) * sqr(cir.r) 74 - cross(cir, cp1, cp2) + cross(cp(0, 0), cp1, cp2); 75 return ans / 2; 76 } 77 78 void CirUnion(cp cir[], int n) { 79 cp cp1, cp2; 80 sort(cir, cir + n, circmp); 81 for (int i = 0; i < n; ++i) 82 for (int j = i + 1; j < n; ++j) 83 if (dcmp(dis(cir[i], cir[j]) + cir[i].r - cir[j].r) <= 0) 84 cir[i].d++; 85 for (int i = 0; i < n; ++i) { 86 int tn = 0, cnt = 0; 87 for (int j = 0; j < n; ++j) { 88 if (i == j) continue; 89 if (CirCrossCir(cir[i], cir[i].r, cir[j], cir[j].r, 90 cp2, cp1) < 2) continue; 91 cp1.angle = atan2(cp1.y - cir[i].y, cp1.x - cir[i].x); 92 cp2.angle = atan2(cp2.y - cir[i].y, cp2.x - cir[i].x); 93 cp1.d = 1; tp[tn++] = cp1; 94 cp2.d = -1; tp[tn++] = cp2; 95 if (dcmp(cp1.angle - cp2.angle) > 0) cnt++; 96 } 97 tp[tn++] = cp(cir[i].x - cir[i].r, cir[i].y, pi, -cnt); 98 tp[tn++] = cp(cir[i].x - cir[i].r, cir[i].y, -pi, cnt); 99 sort(tp, tp + tn, cmp); 100 int p, s = cir[i].d + tp[0].d; 101 for (int j = 1; j < tn; ++j) { 102 p = s; s += tp[j].d; 103 area[p] += calc(cir[i], tp[j - 1], tp[j]); 104 } 105 } 106 } 107 108 int main() { 109 int n; 110 scanf("%d", &n); 111 for(int i = 0; i < n; i++) cir[i].get(); 112 CirUnion(cir, n); 113 printf("%.3lf ", area[1]); 114 return 0; 115 }
3.8
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 typedef long long LL; 7 const int maxn = 1e5 + 10; 8 int n, k, SA[maxn], r[maxn], tmp[maxn], h[maxn]; 9 char s[maxn]; 10 11 bool cmp(int i, int j) 12 { 13 if(r[i] != r[j]) return r[i] < r[j]; 14 return ( i + k <= n ? r[i+k] : -1 ) < ( j + k <= n ? r[j+k] : -1 ); 15 } 16 17 void get_SA() 18 { 19 for(int i = 0; i <= n; i++) 20 { 21 SA[i] = i; 22 r[i] = i < n ? s[i] : -1; 23 } 24 for(k = 1; k <= n; k <<= 1) 25 { 26 sort(SA, SA + n + 1, cmp); 27 tmp[SA[0]] = 0; 28 for(int i = 1; i <= n; i++) tmp[SA[i]] = tmp[SA[i-1]] + cmp(SA[i-1], SA[i]); 29 memcpy(r, tmp, sizeof(r)); 30 } 31 return; 32 } 33 34 void get_height() 35 { 36 for(int i = 0; i <= n; i++) r[SA[i]] = i; 37 int k = 0; 38 for(int i = 0; i < n; i++) 39 { 40 if(k) k--; 41 int j = SA[r[i]-1]; 42 while(s[i+k] == s[j+k]) k++; 43 h[r[i]] = k; 44 } 45 return; 46 } 47 48 int st[maxn]; 49 int main(void) 50 { 51 int k; 52 scanf("%s %d", s, &k); 53 n = strlen(s); 54 if((LL) n * (n + 1) / 2 < k) return puts("No such line."); 55 get_SA(); 56 get_height(); 57 for(int i = 1; i <= n; i++) 58 { 59 for(int j = st[i]; j < h[i+1]; j++) 60 { 61 for(int p = i; p <= n && (p == i || j < h[p]); p++) 62 { 63 if(k == 1) 64 { 65 for(int q = SA[i]; q <= SA[i] + j; q++) printf("%c", s[q]); 66 return puts(""); 67 } 68 k--; st[p]++; 69 } 70 } 71 for(int j = st[i]; j + SA[i] < n; j++) 72 { 73 if(k == 1) 74 { 75 for(int q = SA[i]; q <= SA[i] + j; q++) printf("%c", s[q]); 76 return puts(""); 77 } 78 k--; 79 } 80 } 81 return 0; 82 }
3.11
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <algorithm> 5 using namespace std; 6 int n, k, a[2222], f[2222], g[2222]; 7 8 bool check(int x) 9 { 10 for(int i = 1; i <= n; i++) f[i] = i - 1, g[i] = n - i; 11 for(int i = 1; i <= n; i++) 12 for(int j = i + 1; j <= n; j++) 13 if(abs(a[i] - a[j]) <= (j - i) * (long long) x) 14 f[j] = min(f[j], f[i] + j - i - 1); 15 for(int i = n; i; i--) 16 for(int j = i - 1; j; j--) 17 if(abs(a[i] - a[j]) <= (i - j) * (long long) x) 18 g[j] = min(g[j], g[i] - j + i - 1); 19 for(int i = 1; i <= n; i++) 20 if(f[i] + g[i] <= k) return true; 21 return false; 22 } 23 24 int main(void) 25 { 26 scanf("%d %d", &n, &k); 27 for(int i = 1; i <= n; i++) scanf("%d", a + i); 28 int l = 0, r = 2e9; 29 while(l < r) 30 { 31 int mid = l + (r - l) / 2; 32 if(check(mid)) r = mid; 33 else l = mid + 1; 34 } 35 printf("%d ", r); 36 return 0; 37 }
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <vector> 5 #include <cstring> 6 using namespace std; 7 typedef pair<int, int> pii; 8 typedef long long LL; 9 const int sz = 333; 10 const int maxn = 1e5 + 10; 11 LL a[maxn], tag[maxn], sum[maxn]; 12 int big[maxn], vis[maxn]; 13 vector<int> v[maxn]; 14 vector<pii> l[maxn]; 15 16 int main(void) 17 { 18 int n, m, q; 19 scanf("%d %d %d", &n, &m, &q); 20 for(int i = 1; i <= n; i++) scanf("%I64d", a + i); 21 for(int i = 1; i <= m; i++) 22 { 23 int k, x; 24 scanf("%d", &k); 25 if(k >= sz) big[i] = 1; 26 for(int j = 1; j <= k; j++) 27 { 28 scanf("%d", &x); 29 v[i].push_back(x); 30 if(big[i]) sum[i] += a[x]; 31 } 32 } 33 for(int i = 1; i <= m; i++) 34 { 35 if(!big[i]) continue; 36 memset(vis, 0, sizeof(vis)); 37 for(int j = 0; j < v[i].size(); j++) vis[v[i][j]] = 1; 38 for(int j = 1; j <= m; j++) 39 { 40 int cnt = 0; 41 for(int k = 0; k < v[j].size(); k++) cnt += vis[v[j][k]]; 42 if(cnt) l[j].push_back(pii(i, cnt)); 43 } 44 } 45 while(q--) 46 { 47 LL x; 48 int k; 49 char s[11]; 50 scanf("%s", s); 51 if(s[0] == '?') 52 { 53 scanf("%d", &k); 54 LL ans = sum[k]; 55 for(int i = 0; i < l[k].size(); i++) ans += tag[l[k][i].first] * l[k][i].second; 56 if(!big[k]) for(int i = 0; i < v[k].size(); i++) ans += a[v[k][i]]; 57 printf("%I64d ", ans); 58 } 59 else 60 { 61 scanf("%d %I64d", &k, &x); 62 if(big[k]) tag[k] += x; 63 else 64 { 65 for(int i = 0; i < v[k].size(); i++) a[v[k][i]] += x; 66 for(int i = 0; i < l[k].size(); i++) sum[l[k][i].first] += x * l[k][i].second; 67 } 68 } 69 } 70 return 0; 71 }
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 typedef long long LL; 6 typedef pair<LL, LL> pll; 7 const int maxn = 1e5 + 10; 8 9 // Hash 10 const int seed = 131; 11 int mod[2] = {1000000007, 1000000009}; 12 LL P[2][maxn], S[2][maxn]; 13 void init() 14 { 15 for(int t = 0; t <= 1; t++) 16 { 17 P[t][0] = S[t][0] = 1; 18 for(int i = 1; i < maxn; i++) 19 P[t][i] = P[t][i-1] * seed % mod[t], 20 S[t][i] = (S[t][i-1] + P[t][i]) % mod[t]; 21 } 22 } 23 24 // seg 25 char s[maxn]; 26 int tag[maxn<<2]; 27 LL sum[2][maxn<<2]; 28 void gather(int p, int m) 29 { 30 sum[0][p] = (sum[0][p<<1] * P[0][m>>1] + sum[0][p<<1|1]) % mod[0]; 31 sum[1][p] = (sum[1][p<<1] * P[1][m>>1] + sum[1][p<<1|1]) % mod[1]; 32 } 33 34 void push(int p, int m) 35 { 36 if(tag[p] != -1) 37 { 38 tag[p<<1] = tag[p<<1|1] = tag[p]; 39 sum[0][p<<1] = tag[p] * S[0][(m-(m>>1))-1] % mod[0]; 40 sum[1][p<<1] = tag[p] * S[1][(m-(m>>1))-1] % mod[1]; 41 sum[0][p<<1|1] = tag[p] * S[0][(m>>1)-1] % mod[0]; 42 sum[1][p<<1|1] = tag[p] * S[1][(m>>1)-1] % mod[1]; 43 tag[p] = -1; 44 } 45 } 46 47 void build(int p, int l, int r) 48 { 49 tag[p] = -1; 50 if(l < r) 51 { 52 int mid = (l + r) >> 1; 53 build(p<<1, l, mid); 54 build(p<<1|1, mid + 1, r); 55 gather(p, r - l + 1); 56 } 57 else sum[0][p] = sum[1][p] = s[l] - '0'; 58 } 59 60 void modify(int p, int tl, int tr, int l, int r, int v) 61 { 62 if(tr < l || r < tl) return; 63 if(l <= tl && tr <= r) 64 { 65 tag[p] = v; 66 sum[0][p] = v * S[0][tr-tl] % mod[0]; 67 sum[1][p] = v * S[1][tr-tl] % mod[1]; 68 return; 69 } 70 push(p, tr - tl + 1); 71 int mid = (tl + tr) >> 1; 72 modify(p<<1, tl, mid, l, r, v); 73 modify(p<<1|1, mid+1, tr, l, r, v); 74 gather(p, tr - tl + 1); 75 } 76 77 pll query(int p, int tl, int tr, int l, int r) 78 { 79 if(tr < l || r < tl) return pll(0, 0); 80 if(l <= tl && tr <= r) return pll(sum[0][p], sum[1][p]); 81 push(p, tr - tl + 1); 82 int mid = (tl + tr) >> 1; 83 if(r <= mid) return query(p<<1, tl, mid, l, r); 84 if(l > mid) return query(p<<1|1, mid+1, tr, l, r); 85 pll L = query(p<<1, tl, mid, l, r); 86 pll R = query(p<<1|1, mid+1, tr, l, r); 87 R.first = (R.first + L.first * P[0][min(tr,r)-mid]) % mod[0]; 88 R.second = (R.second + L.second * P[1][min(tr,r)-mid]) % mod[1]; 89 return R; 90 } 91 92 int main(void) 93 { 94 init(); 95 int n, m, k; 96 scanf("%d %d %d %s", &n, &m, &k, s + 1); 97 build(1, 1, n); 98 int q = m + k; 99 while(q--) 100 { 101 int op, l, r, x; 102 scanf("%d %d %d %d", &op, &l, &r, &x); 103 if(op == 1) modify(1, 1, n, l, r, x); 104 else puts(query(1, 1, n, l, r - x) == query(1, 1, n, l + x, r) ? "YES" : "NO"); 105 } 106 return 0; 107 }
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 const int maxn = 111111; 7 int n, k, SA[maxn], r[maxn], tmp[maxn], h[maxn]; 8 char s[maxn]; 9 10 bool cmp(int i, int j) 11 { 12 if(r[i] != r[j]) return r[i] < r[j]; 13 return ( i + k <= n ? r[i+k] : -1 ) < ( j + k <= n ? r[j+k] : -1 ); 14 } 15 16 void get_SA() 17 { 18 for(int i = 0; i <= n; i++) 19 { 20 SA[i] = i; 21 r[i] = i < n ? s[i] : -1; 22 } 23 for(k = 1; k <= n; k <<= 1) 24 { 25 sort(SA, SA + n + 1, cmp); 26 tmp[SA[0]] = 0; 27 for(int i = 1; i <= n; i++) tmp[SA[i]] = tmp[SA[i-1]] + cmp(SA[i-1], SA[i]); 28 memcpy(r, tmp, sizeof(r)); 29 } 30 return; 31 } 32 33 void get_height() 34 { 35 for(int i = 0; i <= n; i++) r[SA[i]] = i; 36 int k = 0; 37 for(int i = 0; i < n; i++) 38 { 39 if(k) k--; 40 int j = SA[r[i]-1]; 41 while(s[i+k] == s[j+k]) k++; 42 h[r[i]] = k; 43 } 44 return; 45 } 46 47 char s1[5555], s2[5555]; 48 int main(void) 49 { 50 scanf("%s %s", s1, s2); 51 int l1 = strlen(s1), l2 = strlen(s2); 52 for(int i = 0; i < l1; i++) s[i] = s1[i]; 53 s[l1] = '#'; 54 for(int i = 0; i < l2; i++) s[l1+i+1] = s2[i]; 55 s[l1+l2+1] = 0; 56 n = strlen(s); 57 get_SA(); 58 get_height(); 59 int ans = maxn; 60 for(int i = 2; i <= n; i++) 61 { 62 if(h[i-1] >= h[i] || h[i+1] >= h[i]) continue; 63 if(SA[i-1] < l1 && SA[i] > l1 || SA[i-1] > l1 && SA[i] < l1) ans = min(ans, max(h[i-1], h[i+1]) + 1); 64 } 65 if(ans == maxn) puts("-1"); 66 else printf("%d ", ans); 67 return 0; 68 }
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 int x[55], y[55]; 5 6 void print(int * x, int n) 7 { 8 for(int i = 1; i < n; i++) putchar('('); 9 for(int i = 1; i <= n; i++) 10 { 11 if(i != 1) putchar('+'); 12 printf("(%d*((1-abs((t-%d)))+abs((abs((t-%d))-1))))", x[i] / 2, i, i); 13 if(i != 1) putchar(')'); 14 } 15 puts(""); 16 } 17 18 int main(void) 19 { 20 int n, k; 21 scanf("%d", &n); 22 for(int i = 1; i <= n; i++) scanf("%d %d %d", x + i, y + i, &k); 23 print(x, n), print(y, n); 24 return 0; 25 }
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 dp[maxn]; 8 9 int main(void) 10 { 11 LL p = 1; 12 for(int i = 1; i < maxn; i++) dp[i] = (dp[i-1] * 3 % mod + p * (p - 1 + mod) % mod) % mod, p = p * 2 % mod; 13 int t, x; 14 scanf("%d", &t); 15 while(t--) scanf("%d", &x), printf("%lld ", dp[x]); 16 return 0; 17 }
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 typedef long long LL; 6 LL a[19], dp[1<<19][19], G[19][19]; 7 8 int main(void) 9 { 10 int n, m, k, x, y, c; 11 scanf("%d %d %d", &n, &m, &k); 12 for(int i = 1; i <= n; i++) scanf("%I64d", a + i); 13 for(int i = 1; i <= k; i++) scanf("%d %d %d", &x, &y, &c), G[x][y] += c; 14 memset(dp, -1, sizeof(dp)); 15 LL ans = dp[0][0] = 0; 16 for(int i = 0; i < (1 << n); i++) 17 for(int j = 0; j <= n; j++) 18 { 19 if(dp[i][j] == -1) continue; 20 ans = max(ans, dp[i][j]); 21 int cnt = 0; 22 for(int k = 0; k < n; k++) 23 if((1 << k) & i) cnt++; 24 if(cnt == m) continue; 25 for(int k = 0; k < n; k++) 26 { 27 if((1 << k) & i) continue; 28 dp[i|(1<<k)][k+1] = max(dp[i|(1<<k)][k+1], dp[i][j] + G[j][k+1] + a[k+1]); 29 } 30 } 31 printf("%I64d ", ans); 32 return 0; 33 }
3.12
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <stack> 5 using namespace std; 6 typedef long long LL; 7 const LL mod = 1e9 + 7; 8 const int maxn = 7777; 9 stack<int> st; 10 char s[777]; 11 12 // edge 13 int cnt, h[maxn]; 14 struct edge 15 { 16 int to, pre; 17 } e[maxn<<1]; 18 void add(int from, int to) 19 { 20 cnt++; 21 e[cnt].pre = h[from]; 22 e[cnt].to = to; 23 h[from] = cnt; 24 } 25 26 LL dp[777][3][3], tmp[777][3][3], cpy[777][3][3]; 27 void dfs(int x) 28 { 29 int fst = 1; 30 for(int i = h[x]; i; i = e[i].pre) 31 { 32 int to = e[i].to; 33 dfs(to); 34 if(fst) memcpy(cpy[x], dp[to], sizeof(cpy[x])), fst = 0; 35 else 36 { 37 memcpy(tmp[x], cpy[x], sizeof(tmp[x])); 38 memset(cpy[x], 0, sizeof(cpy[x])); 39 for(int l1 = 0; l1 < 3; l1++) 40 for(int r1 = 0; r1 < 3; r1++) 41 for(int l2 = 0; l2 < 3; l2++) 42 for(int r2 = 0; r2 < 3; r2++) 43 if(r1 && r1 == l2) continue; 44 else cpy[x][l1][r2] = (cpy[x][l1][r2] + tmp[x][l1][r1] * dp[to][l2][r2]) % mod; 45 } 46 } 47 if(!x) {memcpy(dp[0], cpy[x], sizeof(cpy[x])); return;} 48 if(!fst) 49 { 50 for(int l1 = 0; l1 < 3; l1++) 51 for(int r1 = 0; r1 < 3; r1++) 52 for(int l2 = 0; l2 < 3; l2++) 53 for(int r2 = 0; r2 < 3; r2++) 54 if(l1 && r1 || !l1 && !r1) continue; 55 else if(l1 && l1 == l2) continue; 56 else if(r1 && r1 == r2) continue; 57 else dp[x][l1][r1] = (dp[x][l1][r1] + cpy[x][l2][r2]) % mod; 58 } 59 else 60 { 61 dp[x][0][1] = dp[x][0][2] = 1; 62 dp[x][1][0] = dp[x][2][0] = 1; 63 } 64 } 65 66 int main(void) 67 { 68 scanf("%s", s); 69 int l = strlen(s); 70 int tot = 0; 71 st.push(0); 72 for(int i = 0; i < l; i++) 73 { 74 if(s[i] == '(') add(st.top(), ++tot), st.push(tot); 75 else st.pop(); 76 } 77 dfs(0); 78 LL ans = 0; 79 for(int l1 = 0; l1 < 3; l1++) 80 for(int r1 = 0; r1 < 3; r1++) 81 ans = (ans + dp[0][l1][r1]) % mod; 82 printf("%I64d ", ans); 83 return 0; 84 }
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 typedef long long LL; 5 LL c[33][33]; 6 7 int main(void) 8 { 9 for(int i = 0; i <= 30; i++) c[i][i] = c[i][0] = 1; 10 for(int i = 2; i <= 30; i++) 11 for(int j = 1; j <= i; j++) 12 c[i][j] = c[i-1][j] + c[i-1][j-1]; 13 int t, n, m; 14 scanf("%d", &t); 15 while(t--) scanf("%d %d", &n, &m), printf("%lld ", c[n][m-1]); 16 return 0; 17 }
1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 #include <algorithm> 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 x[maxn], y[maxn]; 23 bool cmp1(int A, int B) 24 { 25 if(x[A] != x[B]) return x[A] < x[B]; 26 return y[A] < y[B]; 27 } 28 double k[maxn]; 29 bool cmp(int A, int B) 30 { 31 return k[A] < k[B]; 32 } 33 34 int sz[maxn]; 35 void dfs1(int x, int f) 36 { 37 sz[x] = 1; 38 for(int i = h[x]; i; i = e[i].pre) 39 { 40 int to = e[i].to; 41 if(to == f) continue; 42 dfs1(to, x); 43 sz[x] += sz[to]; 44 } 45 } 46 47 int ans[maxn]; 48 void dfs2(int p, int f, deque<int> dq) 49 { 50 sort(dq.begin(), dq.end(), cmp1); 51 int tmp = dq.front(); dq.pop_front(); 52 ans[tmp] = p; 53 for(int i = 0; i < dq.size(); i++) 54 { 55 if(x[dq[i]] == x[tmp]) 56 if(y[dq[i]] > y[tmp]) k[dq[i]] = 1e10; 57 else k[dq[i]] = -1e10; 58 else k[dq[i]] = 1.0 * (y[tmp] - y[dq[i]]) / (x[tmp] - x[dq[i]]); 59 } 60 sort(dq.begin(), dq.end(), cmp); 61 for(int i = h[p]; i; i = e[i].pre) 62 { 63 int to = e[i].to; 64 if(to == f) continue; 65 deque<int> dqq; 66 while(sz[to]) dqq.push_back(dq.front()), dq.pop_front(), sz[to]--; 67 dfs2(to, p, dqq); 68 } 69 } 70 71 int main(void) 72 { 73 int n; 74 scanf("%d", &n); 75 for(int i = 1; i < n; i++) 76 { 77 int u, v; 78 scanf("%d %d", &u, &v); 79 add(u, v), add(v, u); 80 } 81 for(int i = 1; i <= n; i++) scanf("%d %d", x + i, y + i); 82 dfs1(1, 0); 83 deque<int> dq; 84 for(int i = 1; i <= n; i++) dq.push_back(i); 85 dfs2(1, 0, dq); 86 for(int i = 1; i <= n; i++) printf("%d ", ans[i]); 87 return 0; 88 }
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 typedef long long LL; 6 const LL INF = 1e18; 7 const int maxn = 1e5 + 10; 8 LL a[maxn], M[maxn<<2], tag[maxn<<2], hM[maxn<<2], htag[maxn<<2]; 9 10 void gather(int p) 11 { 12 M[p] = max(M[p<<1], M[p<<1|1]); 13 hM[p] = max(hM[p<<1], hM[p<<1|1]); 14 } 15 16 void push(int p) 17 { 18 if(htag[p]) 19 { 20 htag[p<<1] = max(htag[p<<1], tag[p<<1] + htag[p]); 21 hM[p<<1] = max(hM[p<<1], M[p<<1] + htag[p]); 22 htag[p<<1|1] = max(htag[p<<1|1], tag[p<<1|1] + htag[p]); 23 hM[p<<1|1] = max(hM[p<<1|1], M[p<<1|1] + htag[p]); 24 htag[p] = 0; 25 } 26 if(tag[p]) 27 { 28 tag[p<<1] += tag[p]; 29 tag[p<<1|1] += tag[p]; 30 M[p<<1] += tag[p]; 31 M[p<<1|1] += tag[p]; 32 tag[p] = 0; 33 } 34 } 35 36 void modify(int p, int tl, int tr, int l, int r, LL v) 37 { 38 if(tr < l || r < tl) return; 39 if(l <= tl && tr <= r) 40 { 41 tag[p] += v; 42 htag[p] = max(htag[p], tag[p]); 43 M[p] += v; 44 hM[p] = max(hM[p], M[p]); 45 return; 46 } 47 push(p); 48 int mid = (tl + tr) >> 1; 49 modify(p<<1, tl, mid, l, r, v); 50 modify(p<<1|1, mid+1, tr, l, r, v); 51 gather(p); 52 } 53 54 LL query(int p, int tl, int tr, int l, int r) 55 { 56 if(tr < l || r < tl) return -INF; 57 if(l <= tl && tr <= r) return hM[p]; 58 push(p); 59 int mid = (tl + tr) >> 1; 60 LL ret = query(p<<1, tl, mid, l, r); 61 ret = max(ret, query(p<<1|1, mid+1, tr, l, r)); 62 return ret; 63 } 64 65 struct query 66 { 67 int id, l, r; 68 query(int id = 0, int l = 0, int r = 0): id(id), l(l), r(r) {} 69 friend bool operator < (query A, query B) 70 { 71 return A.r < B.r; 72 } 73 } q[maxn]; 74 75 LL ans[maxn]; 76 int pre[maxn<<1]; 77 int main(void) 78 { 79 int n, m; 80 scanf("%d", &n); 81 for(int i = 1; i <= n; i++) scanf("%lld", a + i); 82 scanf("%d", &m); 83 for(int i = 1; i <= m; i++) scanf("%d %d", &q[i].l, &q[i].r), q[i].id = i; 84 sort(q + 1, q + m + 1); 85 int p = 0; 86 for(int i = 1; i <= m; i++) 87 { 88 while(p < q[i].r) 89 { 90 p++; 91 modify(1, 1, n, pre[a[p]+100000] + 1, p, a[p]); 92 pre[a[p]+100000] = p; 93 } 94 ans[q[i].id] = query(1, 1, n, q[i].l, q[i].r); 95 } 96 for(int i = 1; i <= m; i++) printf("%lld ", ans[i]); 97 return 0; 98 }
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 const int maxn = 1e5 + 10; 5 typedef long long LL; 6 7 int sg[30] = { 8 0, 1, 2, 1, 4, 3, 2, 1, 5, 6, 9 2, 1, 8, 7, 5, 9, 8, 7, 3, 4, 10 7, 4, 2, 1, 10, 9, 3, 6, 11, 12 11 }; 12 13 int vis[maxn]; 14 int main(void) 15 { 16 int n; 17 scanf("%d", &n); 18 int SG = sg[1], cnt = 0; 19 if(n > 100000) cnt = n - 100000; 20 for(int i = 2; i <= min(100000, n); i++) 21 { 22 if(vis[i]) continue; 23 int tot = 0; 24 LL k = i; 25 while(k <= n) 26 { 27 tot++; 28 if(k <= 100000) vis[k] = 1; 29 else cnt--; 30 k *= i; 31 } 32 SG ^= sg[tot]; 33 } 34 if(cnt % 2) SG ^= sg[1]; 35 puts(SG ? "Vasya" : "Petya"); 36 return 0; 37 } 38 /* 39 #include <iostream> 40 #include <cstdio> 41 #include <map> 42 using namespace std; 43 typedef long long LL; 44 map<LL, int> M; 45 46 int dfs(LL x) 47 { 48 if(!x) return 0; 49 if(M.find(x) != M.end()) return M[x]; 50 int vis[30] = {0}; 51 for(int i = 1; i <= 29; i++) 52 { 53 if((1LL << (i - 1)) & x) 54 { 55 LL y = x; 56 int t = i; 57 while(t <= 29) y = (y | (1LL << (t - 1))) ^ (1LL << (t - 1)), t += i; 58 vis[dfs(y)] = 1; 59 } 60 } 61 for(int i = 0; i <= 29; i++) 62 if(!vis[i]) return M[x] = i; 63 } 64 65 int main(void) 66 { 67 LL x = 0; 68 for(int i = 1; i <= 29; i++) 69 x |= 1LL << (i - 1), printf("%d %d ", i, dfs(x)); 70 return 0; 71 } 72 */
6.24
很久很久很久没写题。中间博客园还炸了一会。
1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <algorithm> 5 using namespace std; 6 typedef pair<int, int> pii; 7 const int maxn = 5e4 + 10; 8 vector<pii> q[maxn]; 9 vector<int> v[maxn]; 10 int vis[maxn], ans[maxn]; 11 int cnt, h[maxn]; 12 int fa[maxn]; 13 14 int uu[maxn], vv[maxn]; 15 int w[maxn], M[maxn], m[maxn], up[maxn], dn[maxn]; 16 17 struct edge 18 { 19 int to, pre; 20 } e[maxn<<1]; 21 22 void add(int from, int to) 23 { 24 cnt++; 25 e[cnt].pre = h[from]; 26 e[cnt].to = to; 27 h[from] = cnt; 28 } 29 30 int Find(int x) 31 { 32 if(fa[x] == x) return x; 33 int f = Find(fa[x]); 34 up[x] = max(max(up[x], up[fa[x]]), M[fa[x]] - m[x]); 35 dn[x] = max(max(dn[x], dn[fa[x]]), M[x] - m[fa[x]]); 36 m[x] = min(m[fa[x]], m[x]); 37 M[x] = max(M[fa[x]], M[x]); 38 return fa[x] = f; 39 } 40 41 void LCA(int p, int f) 42 { 43 m[p] = M[p] = w[p]; 44 fa[p] = p; 45 for(int i = h[p]; i; i = e[i].pre) 46 { 47 int to = e[i].to; 48 if(to == f) continue; 49 LCA(to, p); 50 fa[to] = p; 51 } 52 vis[p] = 1; 53 for(int i = 0; i < q[p].size(); i++) 54 { 55 int to = q[p][i].first, id = q[p][i].second; 56 if(!vis[to]) continue; 57 v[Find(to)].push_back(id); 58 } 59 for(int i = 0; i < v[p].size(); i++) 60 { 61 int id = v[p][i]; 62 Find(uu[id]), Find(vv[id]); 63 ans[id] = max(M[vv[id]] - m[uu[id]], max(up[uu[id]], dn[vv[id]])); 64 } 65 return; 66 } 67 68 int main(void) 69 { 70 int N, Q; 71 scanf("%d", &N); 72 for(int i = 1; i <= N; i++) scanf("%d", w + i); 73 for(int i = 1; i < N; i++) 74 { 75 int u, v; 76 scanf("%d %d", &u, &v); 77 add(u, v), add(v, u); 78 } 79 scanf("%d", &Q); 80 for(int i = 1; i <= Q; i++) 81 { 82 scanf("%d %d", uu + i, vv + i); 83 q[uu[i]].push_back(pii(vv[i], i)); 84 q[vv[i]].push_back(pii(uu[i], i)); 85 } 86 LCA(1, 0); 87 for(int i = 1; i <= Q; i++) printf("%d ", ans[i]); 88 return 0; 89 }
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 typedef long long LL; 6 int h[22]; 7 8 // Matrix 9 const int maxn = 16; 10 const LL mod = 1e9 + 7; 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) 19 { 20 Matrix ret; 21 for(int i = 0; i < maxn; i++) 22 for(int j = 0; j < maxn; j++) 23 for(int k = 0; k < maxn; k++) 24 ret.m[i][j] = ( ret.m[i][j] + (a.m[i][k] * b.m[k][j]) % mod ) % mod; 25 return ret; 26 } 27 28 Matrix M_qpow(Matrix P, LL n) 29 { 30 Matrix ret; 31 ret.E(); 32 while(n) 33 { 34 if(n & 1LL) ret = M_mul(ret, P); 35 n >>= 1LL; 36 P = M_mul(P, P); 37 } 38 return ret; 39 } 40 41 int K; 42 int dp[22]; 43 void dfs(int x) 44 { 45 if(x > 15) return; 46 dp[x]++; 47 for(int i = 1; i <= K; i++) dfs(x + h[i]); 48 } 49 50 int main(void) 51 { 52 LL N; 53 scanf("%lld %d", &N, &K); 54 for(int i = 1; i <= K; i++) scanf("%d", h + i); 55 dfs(0); 56 Matrix A; 57 for(int i = 1; i <= 14; i++) A.m[i][i+1] = 1; 58 for(int i = 1; i <= K; i++) A.m[15][15-h[i]+1] = 1; 59 A = M_qpow(A, N - 1); 60 LL ans = 0; 61 for(int i = 1; i <= 15; i++) ans = (ans + A.m[1][i] * dp[i]) % mod; 62 printf("%lld ", ans * 2 % mod); 63 return 0; 64 }
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 int d[111], sum[111]; 6 int dp[111][111]; 7 8 int main(void) 9 { 10 int T; 11 scanf("%d", &T); 12 for(int kase = 1; kase <= T; kase++) 13 { 14 int n; 15 scanf("%d", &n); 16 for(int i = 1; i <= n; i++) scanf("%d", d + i), sum[i] = sum[i-1] + d[i]; 17 for(int l = 2; l <= n; l++) 18 for(int s = 1; s + l - 1 <= n; s++) 19 { 20 int e = s + l - 1; 21 dp[s][e] = min(dp[s][e-1] + d[e] * (e - s), dp[s+1][e] + d[s] * (e - s)); 22 for(int m = s + 1; m < e; m++) 23 dp[s][e] = min(dp[s][e], dp[s][m-1] + dp[m+1][e] + (sum[e] - sum[m]) * (m - s) + d[m] * (e - s)); 24 } 25 printf("Case #%d: %d ", kase, dp[1][n]); 26 } 27 return 0; 28 }
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 char a[111], c[111]; 6 int t[111], p[111]; 7 8 int main(void) 9 { 10 int b, d; 11 scanf("%d %d %s %s", &b, &d, a + 1, c + 1); 12 int la = strlen(a + 1), lc = strlen(c + 1); 13 for(int i = 1; i <= lc; i++) 14 { 15 t[i] = 0, p[i] = i; 16 for(int j = 1; j <= la; j++) 17 if(a[j] == c[p[i]]) 18 { 19 p[i]++; 20 if(p[i] > lc) p[i] = 1, t[i]++; 21 } 22 } 23 int ans = 0, cur = 1; 24 for(int i = 1; i <= b; i++) 25 ans += t[cur], cur = p[cur]; 26 printf("%d ", ans / d); 27 return 0; 28 }
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 typedef long long LL; 6 int a[2222222]; 7 8 int main(void) 9 { 10 int n; 11 scanf("%d", &n); 12 for(int i = 1; i <= n; i++) scanf("%d", a + i); 13 sort(a + 1, a + 1 + n); 14 int now = n, t = 1; 15 LL ans = 0; 16 for(int i = 1; i <= n; i++) 17 { 18 if(n - i + 1 == now / 4) now /= 4, t++; 19 ans = ans + (LL) t * a[i]; 20 } 21 printf("%I64d ", ans); 22 return 0; 23 }
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 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(LL a, LL b) 27 { 28 return fac[a] * inv(fac[b]) % mod * inv(fac[a-b]) % mod; 29 } 30 31 int main(void) 32 { 33 fac[0] = 1; 34 for(int i = 1; i < maxn; i++) fac[i] = fac[i-1] * i % mod; 35 int A, B, C, D; 36 scanf("%d %d %d %d", &A, &B, &C, &D); 37 if(B == D + 1) printf("%lld ", c(A + B - 1, B - 1) * c(C + B - 1, B - 1) % mod); 38 else if(D == B + 1) printf("%lld ", c(A + D - 1, D - 1) * c(C + D - 1, D - 1) % mod); 39 else if(B == D) 40 { 41 LL a, b; 42 if(!A && !B) a = c(C + B, B); 43 else a = c(A + B - 1, B - 1) * c(C + B, B); 44 if(!C && !D) b = c(A + B, B); 45 else b = c(A + B, B) * c(C + B - 1, B - 1); 46 printf("%lld ", (a + b) % mod); 47 } 48 else puts("0"); 49 return 0; 50 }
HackerRank - manasa-and-combinatorics
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 typedef long long LL; 5 const int maxn = 1e5 + 10; 6 LL fac[maxn]; 7 8 LL qpow(LL a, LL b, LL mod) 9 { 10 LL ret = 1LL; 11 while(b) 12 { 13 if(b & 1) ret = ret * a % mod; 14 a = a * a % mod; 15 b >>= 1; 16 } 17 return ret; 18 } 19 20 LL inv(LL x, LL mod) 21 { 22 return qpow(x, mod - 2, mod); 23 } 24 25 LL Lucas(LL n, LL m, LL mod) 26 { 27 LL ret = 1LL; 28 while(n && m) 29 { 30 LL a = n % mod, b = m % mod; 31 if(a < b) return 0; 32 ret = ret * fac[a] % mod * inv(fac[b] * fac[a-b] % mod, mod) % mod; 33 n /= mod, m /= mod; 34 } 35 return ret; 36 } 37 38 int main(void) 39 { 40 LL p = 99991; 41 fac[0] = 1LL; 42 for(int i = 1; i <= p; i++) fac[i] = fac[i-1] * i % p; 43 int T; 44 scanf("%d", &T); 45 while(T--) 46 { 47 LL n; 48 scanf("%lld", &n); 49 LL ans = Lucas(n + n + n, n, p) - Lucas(n + n + n, n - 1, p) - Lucas(n + n + n, n - 1, p) + Lucas(n + n + n, n - 2, p); 50 printf("%lld ", (ans % p + p) % p); 51 } 52 return 0; 53 }
8.30
穿越了半年哈哈哈哈。
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 typedef long long LL; 5 const int maxn = 1e7 + 10; 6 LL cnt[maxn]; 7 8 int pr[maxn]; 9 void GetPrime() 10 { 11 for(int i = 2; i < maxn; i++) 12 { 13 if(!pr[i]) pr[++pr[0]] = i; 14 for(int j = 1; j <= pr[0] && pr[j] * i < maxn; j++) 15 { 16 pr[i*pr[j]] = 1; 17 if(i % pr[j] == 0) break; 18 } 19 } 20 } 21 22 23 24 bool check(LL x) 25 { 26 for(int i = 1; i <= pr[0]; i++) 27 { 28 if(!cnt[pr[i]]) continue; 29 LL y = x, tmp = 0; 30 while(y) tmp += y / pr[i], y /= pr[i]; 31 if(tmp < cnt[pr[i]]) return false; 32 } 33 return true; 34 } 35 36 int main(void) 37 { 38 GetPrime(); 39 int k; 40 scanf("%d", &k); 41 for(int i = 1; i <= k; i++) 42 { 43 int x; 44 scanf("%d", &x); 45 cnt[2]++; 46 cnt[x+1]--; 47 } 48 for(int i = 2; i <= 10000000; i++) cnt[i] += cnt[i-1]; 49 for(int i = 10000000; i > 1; i--) 50 { 51 if(!cnt[i]) continue; 52 for(int j = 1; pr[j] * pr[j] <= i; j++) 53 if(i % pr[j] == 0) 54 { 55 cnt[pr[j]] += cnt[i]; cnt[i/pr[j]] += cnt[i]; 56 cnt[i] = 0; break; 57 } 58 } 59 LL l = 1, r = 1e18; 60 while(l < r) 61 { 62 LL mid = (l + r) / 2; 63 if(check(mid)) r = mid; 64 else l = mid + 1; 65 } 66 printf("%I64d ", r); 67 return 0; 68 }
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 int main(void) 6 { 7 int n; 8 scanf("%d", &n); 9 if(n % 2) 10 { 11 for(int i = 0; i < n; i++) printf("%d ", i); puts(""); 12 for(int i = 0; i < n; i++) printf("%d ", i); puts(""); 13 for(int i = 0; i < n; i++) printf("%d ", (i + i) % n); puts(""); 14 } 15 else puts("-1"); 16 return 0; 17 }
1 #include <iostream> 2 #include <cstdio> 3 #include <set> 4 using namespace std; 5 typedef long long LL; 6 typedef pair<LL, LL> pll; 7 const int maxn = 1e5 + 10; 8 int f[maxn], son[maxn]; 9 LL t[maxn], sum[maxn]; 10 multiset<pll> ms[maxn], MS; 11 multiset<pll> :: iterator it; 12 13 void add(int x) 14 { 15 if(ms[x].empty()) return; 16 pll tmp = *(ms[x].begin()); 17 tmp.first += t[x] / (son[x] + 2); 18 MS.insert(tmp); 19 tmp = *(ms[x].rbegin()); 20 tmp.first += t[x] / (son[x] + 2); 21 MS.insert(tmp); 22 } 23 24 void del(int x) 25 { 26 if(ms[x].empty()) return; 27 pll tmp = *(ms[x].begin()); 28 tmp.first += t[x] / (son[x] + 2); 29 it = MS.find(tmp); 30 MS.erase(it); 31 tmp = *(ms[x].rbegin()); 32 tmp.first += t[x] / (son[x] + 2); 33 it = MS.find(tmp); 34 MS.erase(it); 35 } 36 37 int main(void) 38 { 39 int n, q; 40 scanf("%d %d", &n, &q); 41 for(int i = 1; i <= n; i++) scanf("%lld", t + i); 42 for(int i = 1; i <= n; i++) scanf("%d", f + i), son[f[i]]++; 43 for(int i = 1; i <= n; i++) sum[f[i]] += t[i] / (son[i] + 2); 44 for(int i = 1; i <= n; i++) ms[f[i]].insert(pll(sum[i] + (t[i] - t[i] / (son[i] + 2) * (son[i] + 1)), i)); 45 for(int i = 1; i <= n; i++) add(i); 46 while(q--) 47 { 48 int type, x, y; 49 scanf("%d", &type); 50 if(type == 1) 51 { 52 scanf("%d %d", &x, &y); 53 54 // f[x] -> y 55 56 // sum: fx ffx y fy 57 // son: fx y 58 // f: x 59 // son[f]: 60 61 set<int> S; 62 S.insert(x); 63 S.insert(f[x]); 64 S.insert(f[f[x]]); 65 S.insert(f[f[f[x]]]); 66 S.insert(y); 67 S.insert(f[y]); 68 S.insert(f[f[y]]); 69 for(set<int> :: iterator it = S.begin(); it != S.end(); it++) del(*it); 70 71 ms[f[f[f[x]]]].erase(pll(sum[f[f[x]]] + (t[f[f[x]]] - t[f[f[x]]] / (son[f[f[x]]] + 2) * (son[f[f[x]]] + 1)), f[f[x]])); 72 ms[f[f[x]]].erase(pll(sum[f[x]] + (t[f[x]] - t[f[x]] / (son[f[x]] + 2) * (son[f[x]] + 1)), f[x])); 73 ms[f[x]].erase(pll(sum[x] + (t[x] - t[x] / (son[x] + 2) * (son[x] + 1)), x)); 74 75 sum[f[x]] -= t[x] / (son[x] + 2); 76 sum[f[f[x]]] -= t[f[x]] / (son[f[x]] + 2); 77 son[f[x]]--; 78 sum[f[f[x]]] += t[f[x]] / (son[f[x]] + 2); 79 80 ms[f[f[f[x]]]].insert(pll(sum[f[f[x]]] + (t[f[f[x]]] - t[f[f[x]]] / (son[f[f[x]]] + 2) * (son[f[f[x]]] + 1)), f[f[x]])); 81 ms[f[f[x]]].insert(pll(sum[f[x]] + (t[f[x]] - t[f[x]] / (son[f[x]] + 2) * (son[f[x]] + 1)), f[x])); 82 83 ms[f[f[y]]].erase(pll(sum[f[y]] + (t[f[y]] - t[f[y]] / (son[f[y]] + 2) * (son[f[y]] + 1)), f[y])); 84 ms[f[y]].erase(pll(sum[y] + (t[y] - t[y] / (son[y] + 2) * (son[y] + 1)), y)); 85 86 sum[y] += t[x] / (son[x] + 2); 87 sum[f[y]] -= t[y] / (son[y] + 2); 88 son[y]++; 89 sum[f[y]] += t[y] / (son[y] + 2); 90 91 ms[f[f[y]]].insert(pll(sum[f[y]] + (t[f[y]] - t[f[y]] / (son[f[y]] + 2) * (son[f[y]] + 1)), f[y])); 92 ms[f[y]].insert(pll(sum[y] + (t[y] - t[y] / (son[y] + 2) * (son[y] + 1)), y)); 93 ms[y].insert(pll(sum[x] + (t[x] - t[x] / (son[x] + 2) * (son[x] + 1)), x)); 94 95 f[x] = y; 96 97 for(set<int> :: iterator it = S.begin(); it != S.end(); it++) add(*it); 98 } 99 else if(type == 2) 100 { 101 scanf("%d", &x); 102 LL tmp = sum[x] + (t[x] - t[x] / (son[x] + 2) * (son[x] + 1)) + t[f[x]] / (son[f[x]] + 2); 103 printf("%lld ", tmp); 104 } 105 else 106 { 107 it = MS.begin(); 108 printf("%lld ", (*it).first); 109 it = MS.end(); it--; 110 printf("%lld ", (*it).first); 111 } 112 } 113 return 0; 114 }
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <stack> 5 #include <queue> 6 #include <vector> 7 #include <algorithm> 8 #include <cmath> 9 using namespace std; 10 const int maxn = 2222; 11 12 // edge 13 int cnt, h[maxn<<1]; 14 void init() 15 { 16 cnt = 0; 17 memset(h, 0, sizeof(h)); 18 } 19 struct edge 20 { 21 int to, pre; 22 } e[1111111]; 23 void add(int from, int to) 24 { 25 cnt++; 26 e[cnt].pre = h[from]; 27 e[cnt].to = to; 28 h[from] = cnt; 29 } 30 31 // SCC 32 stack<int> S; 33 int dfn[maxn], low[maxn], sccno[maxn]; 34 int dfs_clock, scc_cnt; 35 36 void dfs(int u) 37 { 38 low[u] = dfn[u] = ++dfs_clock; 39 S.push(u); 40 41 for(int i = h[u]; i; i = e[i].pre) 42 { 43 int v = e[i].to; 44 if(!dfn[v]) 45 { 46 dfs(v); 47 low[u] = min(low[u], low[v]); 48 } 49 else if(!sccno[v]) low[u] = min(low[u], dfn[v]); 50 } 51 52 if(dfn[u] == low[u]) 53 { 54 scc_cnt++; 55 while(1) 56 { 57 int x = S.top(); S.pop(); 58 sccno[x] = scc_cnt; 59 if(x == u) break; 60 } 61 } 62 } 63 64 void find_scc(int n) 65 { 66 for(int i = 1; i <= n + n; i++) dfn[i] = sccno[i] = 0; 67 dfs_clock = scc_cnt = 0; 68 for(int i = 1; i <= n + n; i++) if(!dfn[i]) dfs(i); 69 } 70 71 bool judge(int n) 72 { 73 find_scc(n); 74 for(int i = 1; i <= n; i++) 75 { 76 if(sccno[i] == sccno[n+i]) 77 return false; 78 } 79 return true; 80 } 81 82 int x[555], y[555]; 83 int ax[1111], ay[1111]; 84 int bx[1111], by[1111]; 85 int N, A, B; 86 int sx1, sy1, sx2, sy2; 87 88 bool check(int mid) 89 { 90 init(); 91 for(int i = 1; i <= N; i++) 92 if(abs(x[i] - sx1) + abs(y[i] - sy1) > mid) add(i, i + N); 93 for(int i = 1; i <= N; i++) 94 if(abs(x[i] - sx2) + abs(y[i] - sy2) > mid) add(i + N, i); 95 for(int i = 1; i <= N; i++) 96 for(int j = i + 1; j <= N; j++) 97 { 98 if(abs(x[i] - sx1) + abs(y[i] - sy1) + abs(x[j] - sx1) + abs(y[j] - sy1) > mid) add(i, j + N), add(j, i + N); 99 if(abs(x[i] - sx2) + abs(y[i] - sy2) + abs(x[j] - sx2) + abs(y[j] - sy2) > mid) add(i + N, j), add(j + N, i); 100 if(abs(x[i] - sx1) + abs(y[i] - sy1) + abs(x[j] - sx2) + abs(y[j] - sy2) + abs(sx1 - sx2) + abs(sy1 - sy2) > mid) add(i, j), add(j + N, i + N); 101 if(abs(x[i] - sx2) + abs(y[i] - sy2) + abs(x[j] - sx1) + abs(y[j] - sy1) + abs(sx1 - sx2) + abs(sy1 - sy2) > mid) add(i + N, j + N), add(j, i); 102 } 103 for(int i = 1; i <= A; i++) add(ax[i], ay[i] + N), add(ax[i] + N, ay[i]), add(ay[i], ax[i] + N), add(ay[i] + N, ax[i]); 104 for(int i = 1; i <= B; i++) add(bx[i], by[i]), add(bx[i] + N, by[i] + N), add(by[i], bx[i]), add(by[i] + N, bx[i] + N); 105 return judge(N); 106 } 107 108 int main(void) 109 { 110 while(~scanf("%d %d %d", &N, &A, &B)) 111 { 112 scanf("%d %d %d %d", &sx1, &sy1, &sx2, &sy2); 113 for(int i = 1; i <= N; i++) scanf("%d %d", x + i, y + i); 114 for(int i = 1; i <= A; i++) scanf("%d %d", ax + i, ay + i); 115 for(int i = 1; i <= B; i++) scanf("%d %d", bx + i, by + i); 116 int l = 0, r = 12000000, mid; 117 if(!check(r)) {puts("-1"); continue;} 118 while(l < r) 119 { 120 mid = l + (r - l) / 2; 121 if(check(mid)) r = mid; 122 else l = mid + 1; 123 } 124 printf("%d ", r); 125 } 126 return 0; 127 }
9.3
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 T; 10 scanf("%d", &T); 11 while(T--) 12 { 13 LL A, B, X, Y; 14 scanf("%lld %lld %lld %lld", &A, &B, &X, &Y); 15 LL k = A / (B - A); 16 LL x = (X + B - 1) / B; 17 LL y = Y / A; 18 if(x == y) printf("%lld ", min(Y, x * B) - max(X, x * A) + 1); 19 else if(y < k) printf("%lld ", (y - x - 1) * (B - A + 1) + min(x * B, Y) - max(X, x * A) + 1 + min(y * B, Y) - max(y * A, X) + 1); 20 else if(k > x) printf("%lld ", (k - 1 - x) * (B - A + 1) + min(x * B, Y) - max(X, x * A) + 1 + Y - max(x * B, (k - 1) * B)); 21 else printf("%lld ", x * B - X + 1 + Y - max(x * B, (k - 1) * B)); 22 } 23 return 0; 24 }
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <bitset> 5 using namespace std; 6 typedef long long LL; 7 const LL mod = 1e9 + 7; 8 int a[1111][303]; 9 LL po[1111]; 10 bitset<303> bs[1111]; 11 12 int main(void) 13 { 14 po[0] = 1; 15 for(int i = 1; i < 1111; i++) po[i] = po[i-1] * 2 % mod; 16 int T; 17 scanf("%d", &T); 18 for(int kase = 1; kase <= T; kase++) 19 { 20 int n; 21 scanf("%d", &n); 22 for(int i = 1; i <= n; i++) 23 { 24 LL x; 25 scanf("%lld", &x); 26 bs[i].reset(); 27 for(int j = 2; j <= 300; j++) 28 while(x % j == 0) x /= j, bs[i].flip(j); 29 if(x > 1) bs[i].flip(x); 30 } 31 int cnt = 0; 32 for(int i = 1; i <= n; i++) 33 { 34 int pos = 0; 35 for(int j = 1; j <= 300; j++) 36 if(bs[i][j]) {pos = j; break;} 37 if(!pos) continue; 38 cnt++; 39 for(int j = 1; j <= n; j++) 40 { 41 if(j == i) continue; 42 if(!bs[j][pos]) continue; 43 bs[j] ^= bs[i]; 44 } 45 } 46 printf("Case %d: %lld ", kase, (po[n-cnt] + mod - 1) % mod); 47 } 48 return 0; 49 }
9.5
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 typedef long long LL; 6 const int maxn = 1e6 + 10; 7 int a[maxn], M1[maxn<<2], M2[maxn<<2], cnt[maxn<<2], tag[maxn<<2]; 8 LL sum[maxn<<2]; 9 10 void gather(int p) 11 { 12 sum[p] = sum[p<<1] + sum[p<<1|1]; 13 if(M1[p<<1] == M1[p<<1|1]) M1[p] = M1[p<<1], cnt[p] = cnt[p<<1] + cnt[p<<1|1], M2[p] = max(M2[p<<1], M2[p<<1|1]); 14 else if(M1[p<<1] > M1[p<<1|1]) M1[p] = M1[p<<1], cnt[p] = cnt[p<<1], M2[p] = max(M2[p<<1], M1[p<<1|1]); 15 else M1[p] = M1[p<<1|1], cnt[p] = cnt[p<<1|1], M2[p] = max(M2[p<<1|1], M1[p<<1]); 16 } 17 18 void push(int p) 19 { 20 if(tag[p] != -1) 21 { 22 if(tag[p] < M1[p<<1]) 23 { 24 sum[p<<1] -= (LL) (M1[p<<1] - tag[p]) * cnt[p<<1]; 25 tag[p<<1] = M1[p<<1] = tag[p]; 26 } 27 if(tag[p] < M1[p<<1|1]) 28 { 29 sum[p<<1|1] -= (LL) (M1[p<<1|1] - tag[p]) * cnt[p<<1|1]; 30 tag[p<<1|1] = M1[p<<1|1] = tag[p]; 31 } 32 tag[p] = -1; 33 } 34 } 35 36 void build(int p, int l, int r) 37 { 38 tag[p] = -1; 39 if(l < r) 40 { 41 int mid = (l + r) >> 1; 42 build(p<<1, l, mid); 43 build(p<<1|1, mid + 1, r); 44 gather(p); 45 } 46 else sum[p] = M1[p] = a[l], M2[p] = -1, cnt[p] = 1; 47 } 48 49 void modify(int p, int tl, int tr, int l, int r, int v) 50 { 51 if(tr < l || r < tl) return; 52 if(l <= tl && tr <= r) 53 { 54 if(v >= M1[p]) return; 55 if(v < M1[p] && v > M2[p]) 56 { 57 sum[p] -= (LL) (M1[p] - v) * cnt[p]; 58 tag[p] = M1[p] = v; 59 return; 60 } 61 } 62 push(p); 63 int mid = (tl + tr) >> 1; 64 modify(p<<1, tl, mid, l, r, v); 65 modify(p<<1|1, mid+1, tr, l, r, v); 66 gather(p); 67 } 68 69 int Mquery(int p, int tl, int tr, int l, int r) 70 { 71 if(tr < l || r < tl) return -1; 72 if(l <= tl && tr <= r) return M1[p]; 73 push(p); 74 int mid = (tl + tr) >> 1; 75 return max(Mquery(p<<1, tl, mid, l, r), Mquery(p<<1|1, mid+1, tr, l, r)); 76 } 77 78 LL query(int p, int tl, int tr, int l, int r) 79 { 80 if(tr < l || r < tl) return 0LL; 81 if(l <= tl && tr <= r) return sum[p]; 82 push(p); 83 int mid = (tl + tr) >> 1; 84 LL ret = query(p<<1, tl, mid, l, r); 85 ret += query(p<<1|1, mid+1, tr, l, r); 86 return ret; 87 } 88 89 int main(void) 90 { 91 int T; 92 scanf("%d", &T); 93 while(T--) 94 { 95 int n, m; 96 scanf("%d %d", &n, &m); 97 for(int i = 1; i <= n; i++) scanf("%d", a + i); 98 build(1, 1, n); 99 while(m--) 100 { 101 int op, x, y, t; 102 scanf("%d", &op); 103 if(op == 0) 104 { 105 scanf("%d %d %d", &x, &y, &t); 106 modify(1, 1, n, x, y, t); 107 } 108 else if(op == 1) 109 { 110 scanf("%d %d", &x, &y); 111 printf("%d ", Mquery(1, 1, n, x, y)); 112 } 113 else 114 { 115 scanf("%d %d", &x, &y); 116 printf("%lld ", query(1, 1, n, x, y)); 117 } 118 } 119 } 120 return 0; 121 }
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 using namespace std; 5 6 int main(void) 7 { 8 int m, n; 9 scanf("%d %d", &m, &n); 10 double ans = 0.0, sum = 0.0; 11 for(int i = 1; i <= m; i++) 12 { 13 double p = pow(1.0 * i / m, n) - sum; 14 ans += i * p; 15 sum += p; 16 } 17 printf("%.9f ", ans); 18 return 0; 19 }
9.6
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cmath> 5 using namespace std; 6 typedef long long LL; 7 const int maxn = 1e5 + 10; 8 LL a[maxn], sum[maxn<<2], m[maxn<<2], M[maxn<<2], tag[maxn<<2]; 9 10 void gather(int p) 11 { 12 sum[p] = sum[p<<1] + sum[p<<1|1]; 13 M[p] = max(M[p<<1], M[p<<1|1]); 14 m[p] = min(m[p<<1], m[p<<1|1]); 15 } 16 17 void push(int p, int l) 18 { 19 if(tag[p]) 20 { 21 tag[p<<1] += tag[p]; 22 tag[p<<1|1] += tag[p]; 23 sum[p<<1] += tag[p] * (l - (l >> 1)); 24 sum[p<<1|1] += tag[p] * (l >> 1); 25 m[p<<1] += tag[p]; 26 m[p<<1|1] += tag[p]; 27 M[p<<1] += tag[p]; 28 M[p<<1|1] += tag[p]; 29 tag[p] = 0; 30 } 31 } 32 33 void build(int p, int l, int r) 34 { 35 tag[p] = 0; 36 if(l < r) 37 { 38 int mid = (l + r) >> 1; 39 build(p<<1, l, mid); 40 build(p<<1|1, mid + 1, r); 41 gather(p); 42 } 43 else sum[p] = M[p] = m[p] = a[l]; 44 } 45 46 void Add(int p, int tl, int tr, int l, int r, LL v) 47 { 48 if(tr < l || r < tl) return; 49 if(l <= tl && tr <= r) 50 { 51 m[p] += v; 52 M[p] += v; 53 tag[p] += v; 54 sum[p] += v * (tr - tl + 1); 55 return; 56 } 57 push(p, tr - tl + 1); 58 int mid = (tl + tr) >> 1; 59 Add(p<<1, tl, mid, l, r, v); 60 Add(p<<1|1, mid+1, tr, l, r, v); 61 gather(p); 62 } 63 64 void Sqrt(int p, int tl, int tr, int l, int r) 65 { 66 if(tr < l || r < tl) return; 67 LL u = sqrt(M[p] + 0.5), v = sqrt(m[p] + 0.5); 68 if(l <= tl && tr <= r && M[p] - m[p] == u - v) 69 { 70 v -= m[p]; 71 m[p] += v; 72 M[p] += v; 73 tag[p] += v; 74 sum[p] += v * (tr - tl + 1); 75 return; 76 } 77 push(p, tr - tl + 1); 78 int mid = (tl + tr) >> 1; 79 Sqrt(p<<1, tl, mid, l, r); 80 Sqrt(p<<1|1, mid+1, tr, l, r); 81 gather(p); 82 } 83 84 LL query(int p, int tl, int tr, int l, int r) 85 { 86 if(tr < l || r < tl) return 0LL; 87 if(l <= tl && tr <= r) return sum[p]; 88 push(p, tr - tl + 1); 89 int mid = (tl + tr) >> 1; 90 LL ret = query(p<<1, tl, mid, l, r); 91 ret += query(p<<1|1, mid+1, tr, l, r); 92 return ret; 93 } 94 95 int main(void) 96 { 97 int t; 98 scanf("%d", &t); 99 while(t--) 100 { 101 int N, M; 102 scanf("%d %d", &N, &M); 103 for(int i = 1; i <= N; i++) scanf("%lld", a + i); 104 build(1, 1, N); 105 while(M--) 106 { 107 int op, l, r; 108 LL x; 109 scanf("%d %d %d", &op, &l, &r); 110 if(op == 1) 111 { 112 scanf("%lld", &x); 113 Add(1, 1, N, l, r, x); 114 } 115 else if(op == 2) Sqrt(1, 1, N, l, r); 116 else printf("%lld ", query(1, 1, N, l, r)); 117 } 118 } 119 return 0; 120 }
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 typedef long long LL; 5 const LL mod = 1e9 + 61; 6 const int maxn = 1e5 + 10; 7 int a[maxn], sum[maxn][33]; 8 int st[maxn], L[maxn], R[maxn]; 9 10 int main(void) 11 { 12 int T; 13 scanf("%d", &T); 14 while(T--) 15 { 16 int n; 17 scanf("%d", &n); 18 for(int i = 1; i <= n; i++) 19 { 20 scanf("%d", a + i); 21 for(int j = 0; j <= 30; j++) 22 sum[i][j] = sum[i-1][j] ^ ((a[i] >> j & 1) ? 1 : 0); 23 } 24 25 for(int i = 1; i <= n; i++) 26 for(int j = 0; j <= 30; j++) 27 sum[i][j] += sum[i-1][j]; 28 29 int p = 0; 30 for(int i = 1; i <= n; i++) 31 { 32 while(p && a[st[p]] < a[i]) p--; 33 L[i] = p ? st[p] + 1 : 1; 34 st[++p] = i; 35 } 36 37 p = 0; 38 for(int i = n; i >= 1; i--) 39 { 40 while(p && a[st[p]] <= a[i]) p--; 41 R[i] = p ? st[p] - 1 : n; 42 st[++p] = i; 43 } 44 45 LL ans = 0; 46 for(int i = 1; i <= n; i++) 47 { 48 for(int j = 0; j <= 30; j++) 49 { 50 int lo = sum[i-1][j] - (L[i] > 1 ? sum[L[i]-2][j] : 0), lz = i - L[i] + 1 - lo; 51 int ro = sum[R[i]][j] - sum[i-1][j], rz = R[i] - i + 1 - ro; 52 ans = (ans + ((LL) lz * ro + (LL) lo * rz) * a[i] % mod * (1LL << j)) % mod; 53 } 54 } 55 printf("%lld ", ans); 56 } 57 return 0; 58 }
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 int p[3333], vis[3333]; 6 7 int main(void) 8 { 9 int n, m, sz = 0; 10 scanf("%d", &n); 11 for(int i = 1; i <= n; i++) scanf("%d", p + i); 12 scanf("%d", &m); 13 for(int i = 1; i <= n; i++) 14 { 15 if(vis[i]) continue; 16 sz++; 17 int now = i; 18 while(!vis[now]) 19 { 20 vis[now] = 1; 21 now = p[now]; 22 } 23 } 24 if(m + sz == n) puts("0"); 25 else if(m + sz > n) 26 { 27 int d = m + sz - n, cnt = 0; 28 printf("%d ", d); 29 for(int i = 1; i <= d; i++) 30 { 31 for(int j = 1; j <= n; j++) vis[j] = 0; 32 int now = 1; 33 while(!vis[now]) vis[now] = 1, now = p[now]; 34 for(int j = 1; j <= n; j++) 35 if(!vis[j]) {printf("1 %d ", j); swap(p[1], p[j]); break;} 36 } 37 } 38 else 39 { 40 int d = n - m - sz, cnt = 0; 41 printf("%d ", d); 42 for(int i = 1; i <= d; i++) 43 { 44 for(int j = 1; j <= n; j++) vis[j] = 0; 45 for(int j = 1; j <= n; j++) 46 { 47 if(p[j] != j) 48 { 49 int m = n, now = j; 50 while(!vis[now]) 51 { 52 vis[now] = 1, now = p[now]; 53 if(now != j) m = min(m, now); 54 } 55 printf("%d %d ", j, m), swap(p[j], p[m]); 56 break; 57 } 58 } 59 } 60 } 61 return 0; 62 }
9.7
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 typedef long long LL; 5 int u[22], v[22], a[111], b[111], c[111]; 6 LL dp[22][22222]; 7 int n, m, q; 8 9 LL cal(int rt, int msk) 10 { 11 if(msk == (1 << (rt - 1))) return 1LL; 12 if(dp[rt][msk] != -1) return dp[rt][msk]; 13 14 int mi = 0; 15 for(int i = 1; i <= n; i++) 16 { 17 if(i == rt) continue; 18 if((1 << (i - 1)) & msk) {mi = i; break;} 19 } 20 21 dp[rt][msk] = 0; 22 for(int i = (msk - 1) & msk; i; i = (i - 1) & msk) 23 { 24 if((1 << (rt - 1)) & i) continue; 25 if((1 << (mi - 1)) & i) 26 { 27 for(int j = 1; j <= n; j++) 28 { 29 if((1 << (j - 1)) & i) 30 { 31 int ok = 1; 32 for(int k = 1; k <= m; k++) 33 { 34 if(u[k] == rt && v[k] == j) continue; 35 if(v[k] == rt && u[k] == j) continue; 36 if(((1 << (u[k] - 1)) & (msk ^ i)) && ((1 << (v[k] - 1)) & (i))) ok = 0; 37 if(((1 << (v[k] - 1)) & (msk ^ i)) && ((1 << (u[k] - 1)) & (i))) ok = 0; 38 } 39 for(int k = 1; k <= q; k++) 40 { 41 if(((1 << (a[k] - 1)) & (msk ^ i)) && ((1 << (b[k] - 1)) & (i)) && c[k] != rt) ok = 0; 42 if(((1 << (b[k] - 1)) & (msk ^ i)) && ((1 << (a[k] - 1)) & (i)) && c[k] != rt) ok = 0; 43 if(a[k] == b[k] && a[k] != c[k]) ok = 0; 44 } 45 if(ok) dp[rt][msk] += cal(rt, msk ^ i) * cal(j, i); 46 } 47 } 48 } 49 } 50 return dp[rt][msk]; 51 } 52 53 int main(void) 54 { 55 scanf("%d %d %d", &n, &m, &q); 56 for(int i = 1; i <= m; i++) scanf("%d %d", u + i, v + i); 57 for(int i = 1; i <= q; i++) scanf("%d %d %d", a + i, b + i, c + i); 58 for(int i = 0; i <= n; i++) 59 for(int j = 0; j <= (1 << n); j++) 60 dp[i][j] = -1; 61 printf("%I64d ", cal(1, (1<<n)-1)); 62 return 0; 63 }
网上复制的
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 using namespace std; 5 6 double sa , sb , sc , sd , v ; 7 double s , r , x , y , z ; 8 9 struct point 10 { 11 double x , y , z ; 12 point( double x = 0 , double y = 0 , double z = 0 ):x(x),y(y),z(z) { } 13 int read() { return scanf( "%lf%lf%lf" , &x , &y , &z ) ; } 14 double length() { return sqrt(x*x+y*y+z*z) ; } 15 } A , B , C , D ; 16 17 point operator - ( point A , point B ) 18 { 19 return point( A.x-B.x , A.y-B.y , A.z-B.z ) ; 20 } 21 22 double dot( point A , point B ) 23 { 24 return A.x*B.x + A.y*B.y + A.z*B.z ; 25 } 26 27 point cross( point A , point B ) 28 { 29 return point( A.y*B.z-A.z*B.y , A.z*B.x-A.x*B.z , A.x*B.y-A.y*B.x ) ; 30 } 31 32 double area( point A , point B , point C ) 33 { 34 return cross(B-A,C-A).length()/2 ; 35 } 36 37 double volume( point A , point B , point C , point D ) 38 { 39 return fabs(dot(D-A,cross(B-A,C-A)))/6 ; 40 } 41 42 int main(void) 43 { 44 while(~A.read()) 45 { 46 B.read() ; 47 C.read() ; 48 D.read() ; 49 sa = area(B,C,D) ; 50 sb = area(A,C,D) ; 51 sc = area(A,B,D) ; 52 sd = area(A,B,C) ; 53 v = volume(A,B,C,D) ; 54 if( v < 1e-8 ) 55 { 56 puts("O O O O") ; 57 continue ; 58 } 59 else 60 { 61 s = sa+sb+sc+sd ; 62 r = v*3/s ; 63 x = (A.x*sa+B.x*sb+C.x*sc+D.x*sd)/s ; 64 y = (A.y*sa+B.y*sb+C.y*sc+D.y*sd)/s ; 65 z = (A.z*sa+B.z*sb+C.z*sc+D.z*sd)/s ; 66 printf( "%.4f %.4f %.4f %.4f " , x , y , z , r ) ; 67 } 68 } 69 return 0 ; 70 }
10.4
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <unordered_map> 5 #include <algorithm> 6 using namespace std; 7 int ans, num; 8 9 // Trie 10 const int maxn = 3e5 + 10; 11 const int maxnode = 6e6 + 10, sigma_size = 26; 12 char s[maxn]; 13 struct Trie 14 { 15 unordered_map<int, int> ch[maxnode]; 16 int cnt, rt[maxn], sz[maxn]; 17 Trie(){cnt = 0;} 18 19 int idx(char c) {return c - 'a';} 20 21 void dfs(int u, int pu, int pv) 22 { 23 for(int i = 0; i < sigma_size; i++) 24 { 25 if(ch[pv].find(i) == ch[pv].end()) continue; 26 if(ch[pu].find(i) == ch[pu].end()) 27 { 28 ch[cnt].clear(); 29 ch[pu][i] = cnt++; 30 sz[u]++; 31 } 32 dfs(u, ch[pu][i], ch[pv][i]); 33 } 34 } 35 36 int merge(int u, int v) 37 { 38 if(!u) return v; 39 if(!v) return u; 40 if(sz[u] < sz[v]) swap(u, v); 41 dfs(u, rt[u], rt[v]); 42 return u; 43 } 44 45 int link(int u, int v) 46 { 47 ch[cnt].clear(); 48 rt[u] = cnt++; 49 if(!v) 50 { 51 ch[cnt].clear(); 52 ch[rt[u]][idx(s[u])] = cnt++; 53 sz[u] = 2; 54 } 55 else 56 { 57 ch[rt[u]][idx(s[u])] = rt[v]; 58 sz[u] = sz[v] + 1; 59 } 60 } 61 62 } tr; 63 64 // edge 65 int cnt, h[maxn]; 66 struct edge 67 { 68 int to, pre; 69 } e[maxn<<1]; 70 void add(int from, int to) 71 { 72 cnt++; 73 e[cnt].pre = h[from]; 74 e[cnt].to = to; 75 h[from] = cnt; 76 } 77 78 int c[maxn]; 79 80 void dfs(int x, int f) 81 { 82 int son = 0; 83 for(int i = h[x]; i; i = e[i].pre) 84 { 85 int to = e[i].to; 86 if(to == f) continue; 87 dfs(to, x); 88 son = tr.merge(to, son); 89 } 90 tr.link(x, son); 91 if(ans == c[x] + tr.sz[x]) num++; 92 if(ans < c[x] + tr.sz[x]) ans = c[x] + tr.sz[x], num = 1; 93 } 94 95 int main(void) 96 { 97 int n; 98 scanf("%d", &n); 99 for(int i = 1; i <= n; i++) scanf("%d", c + i); 100 scanf("%s", s + 1); 101 for(int i = 1; i < n; i++) 102 { 103 int u, v; 104 scanf("%d %d", &u, &v); 105 add(u, v), add(v, u); 106 } 107 ans = -1; 108 dfs(1, 0); 109 printf("%d %d ", ans - 1, num); 110 return 0; 111 }
10.14
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 typedef long long LL; 6 char s[111]; 7 8 int main(void) 9 { 10 int n; 11 scanf("%d", &n); 12 for(int i = 1; i <= n; i++) 13 { 14 scanf(" IAO'%s", s); 15 int l = strlen(s); 16 LL y = 1988, x = 0; 17 for(LL j = 10, cnt = 1; cnt <= l; j *= 10, cnt++) 18 { 19 x += j / 10 * (s[l-cnt] - '0'); 20 LL dy = y % j; 21 y = y - dy + x; 22 if(dy >= x) y += j; 23 } 24 printf("%I64d ", y); 25 } 26 return 0; 27 }
10.17
1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <cstring> 5 using namespace std; 6 typedef long long LL; 7 const LL mod = 1e9 + 9; 8 LL dp[15][1<<15], pw[444]; 9 int G[22][22], edge[1<<15]; 10 int vis[22]; 11 12 int main(void) 13 { 14 pw[0] = 1; 15 for(int i = 1; i < 444; i++) pw[i] = pw[i-1] * 2 % mod; 16 int T; 17 scanf("%d", &T); 18 for(int kase = 1; kase <= T; kase++) 19 { 20 int N, M, K; 21 scanf("%d %d %d", &N, &M, &K); 22 for(int i = 1; i <= N; i++) 23 for(int j = 1; j <= N; j++) 24 G[i][j] = 0; 25 for(int i = 1; i <= M; i++) 26 { 27 int u, v; 28 scanf("%d %d", &u, &v); 29 G[u][v] = G[v][u] = 1; 30 } 31 for(int i = 0; i < (1 << N); i++) 32 { 33 edge[i] = 0; 34 for(int j = 1; j <= N; j++) 35 vis[j] = (i & (1 << (j - 1))) ? 1 : 0; 36 for(int j = 1; j <= N; j++) 37 for(int k = j; k <= N; k++) 38 if(vis[j] && vis[k] && G[j][k]) 39 edge[i]++; 40 } 41 memset(dp, 0, sizeof(dp)); 42 for(int i = 1; i < (1 << N); i++) 43 { 44 dp[1][i] = pw[edge[i]]; 45 int bit = 0; 46 for(int j = 1; j <= N; j++) 47 if(i & (1 << (j - 1))) {bit = j - 1; break;} 48 for(int j = (i - 1) & i; j > 0; j = (j - 1) & i) 49 if(j & (1 << bit)) 50 dp[1][i] = (dp[1][i] - dp[1][j] * pw[edge[i^j]] % mod + mod) % mod; 51 } 52 for(int k = 1; k < K; k++) 53 { 54 for(int i = 1; i < (1 << N); i++) 55 { 56 int bit = 0; 57 for(int j = 1; j <= N; j++) 58 if(i & (1 << (j - 1))) {bit = j - 1; break;} 59 for(int j = (i - 1) & i; j > 0; j = (j - 1) & i) 60 if(j & (1 << bit)) 61 dp[k+1][i] = (dp[k+1][i] + dp[k][i^j] * dp[1][j]) % mod; 62 } 63 } 64 printf("Case #%d: %lld ", kase, dp[K][(1<<N)-1]); 65 } 66 return 0; 67 }