2.8
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 typedef long long LL; 6 7 // template 8 struct Point 9 { 10 LL x, y; 11 Point(LL x = 0, LL y = 0): x(x), y(y){} 12 }; 13 typedef Point Vector; 14 Vector operator - (Vector A, Vector B){return Vector(A.x - B.x, A.y - B.y);} 15 Vector operator + (Vector A, Vector B){return Vector(A.x + B.x, A.y + B.y);} 16 bool operator < (const Point& a, const Point& b){return a.x < b.x || (a.x == b.x && a.y < b.y);} 17 LL Cross(Vector A, Vector B){return A.x * B.y - A.y * B.x;} 18 19 LL PolygonArea(Point* P, int n) 20 { 21 LL ans = 0; 22 for(int i = 1; i < n - 1; i++) ans += Cross(P[i] - P[0], P[i+1] - P[0]); 23 return ans; 24 } 25 26 int ConvexHull(Point* p, int n, Point* ch) 27 { 28 sort(p, p + n); 29 int m = 0; 30 for(int i = 0; i < n; i++) 31 { 32 while(m > 1 && Cross(ch[m-1] - ch[m-2], p[i] - ch[m-2]) <= 0) m--; 33 ch[m++] = p[i]; 34 } 35 int k = m; 36 for(int i = n - 2; i >= 0; i--) 37 { 38 while(m > k && Cross(ch[m-1] - ch[m-2], p[i] - ch[m-2]) <= 0) m--; 39 ch[m++] = p[i]; 40 } 41 if(n > 1) m--; 42 return m; 43 } 44 45 Point p[5555], ch[5555]; 46 bool judge(int i, int j, int pos, int cnt) 47 { 48 p[0] = ch[i], p[1] = ch[j]; 49 p[2] = ch[pos]; 50 LL s1 = PolygonArea(p, 3); 51 p[2] = ch[(pos+1)%cnt]; 52 LL s2 = PolygonArea(p, 3); 53 return s2 > s1; 54 } 55 56 int main(void) 57 { 58 int n; 59 LL S; 60 scanf("%d %I64d", &n, &S); 61 for(int i = 0; i < n; i++) 62 { 63 int a, b; 64 scanf("%d %d", &a, &b); 65 p[i].x = (LL) a, p[i].y = (LL) b; 66 } 67 int cnt = ConvexHull(p, n, ch); 68 int a, b, c, pos; 69 LL s = 0; 70 for(int i = 0; i < cnt; i++) 71 { 72 pos = (i + 2) % cnt; 73 for(int j = (i + 1) % cnt; (j + 1) % cnt != i; j = (j + 1) % cnt) 74 { 75 if(pos == j) pos = (pos + 1) % cnt; 76 while((pos + 1) % cnt != i && judge(i, j, pos, cnt)) pos = (pos + 1) % cnt; 77 p[0] = ch[i], p[1] = ch[j], p[2] = ch[pos]; 78 LL tmp = PolygonArea(p, 3); 79 if(tmp > s) s = tmp, a = i, b = j, c = pos; 80 } 81 } 82 Point x = ch[a] + ch[b] - ch[c], y = ch[c] + ch[a] - ch[b], z = ch[b] + ch[c] - ch[a]; 83 printf("%I64d %I64d ", x.x, x.y); 84 printf("%I64d %I64d ", y.x, y.y); 85 printf("%I64d %I64d ", z.x, z.y); 86 return 0; 87 }
2.9
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 typedef long long LL; 7 typedef pair<LL, LL> pii; 8 const int maxn = 5e4 + 10; 9 LL K, mod, s[maxn]; 10 pii pr[maxn]; 11 int N, MM; 12 13 // Matrix 14 struct Matrix 15 { 16 LL m[2][2]; 17 Matrix(){memset(m, 0, sizeof(m));} 18 void E(){memset(m, 0, sizeof(m)); for(int i = 0; i < 2; i++) m[i][i] = 1;} 19 }; 20 Matrix M_mul(Matrix a, Matrix b) 21 { 22 Matrix ret; 23 for(int i = 0; i < 2; i++) 24 for(int j = 0; j < 2; j++) 25 for(int k = 0; k < 2; k++) 26 ret.m[i][j] = ( ret.m[i][j] + (a.m[i][k] * b.m[k][j]) % mod ) % mod; 27 return ret; 28 } 29 Matrix M_qpow(Matrix P, LL n) 30 { 31 Matrix ret; 32 ret.E(); 33 while(n) 34 { 35 if(n & 1LL) ret = M_mul(ret, P); 36 n >>= 1LL; 37 P = M_mul(P, P); 38 } 39 return ret; 40 } 41 42 // seg 43 Matrix a[maxn], mul[maxn<<2]; 44 void gather(int p) 45 { 46 mul[p] = M_mul(mul[p<<1|1], mul[p<<1]); 47 } 48 void build(int p, int l, int r) 49 { 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); 56 } 57 else mul[p] = a[l]; 58 } 59 Matrix query(int p, int tl, int tr, int l, int r) 60 { 61 Matrix ret; ret.E(); 62 if(tr < l || r < tl) return ret; 63 if(l <= tl && tr <= r) return mul[p]; 64 int mid = (tl + tr) >> 1; 65 ret = query(p<<1, tl, mid, l, r); 66 ret = M_mul(query(p<<1|1, mid+1, tr, l, r), ret); 67 return ret; 68 } 69 70 Matrix query(LL l, LL r) 71 { 72 LL len = r - l + 1; 73 int pl = (l - 1) % N + 1, pr = (r - 1) % N + 1; 74 if(pl <= pr && len <= N) return query(1, 1, N, pl, pr); 75 len -= N - pl + 1 + pr; 76 LL rd = len / N; 77 Matrix ret = M_mul(M_mul(query(1, 1, N, 1, pr), M_qpow(query(1, 1, N, 1, N), rd)), query(1, 1, N, pl, N)); 78 return ret; 79 } 80 81 int main(void) 82 { 83 scanf("%I64d %I64d", &K, &mod); 84 scanf("%d", &N); 85 for(int i = 0; i < N; i++) scanf("%I64d", s + i); 86 scanf("%d", &MM); 87 for(int i = 1; i <= MM; i++) scanf("%I64d %I64d", &pr[i].first, &pr[i].second); 88 if(!K) return puts("0"); 89 sort(pr + 1, pr + 1 + MM); 90 s[N] = s[0]; 91 for(int i = 1; i <= N; i++) a[i].m[0][0] = s[i], a[i].m[0][1] = s[i-1], 92 a[i].m[1][0] = 1, a[i].m[1][1] = 0; 93 build(1, 1, N); 94 Matrix M, P; 95 M.E(); 96 P.m[1][0] = 1, P.m[1][1] = 0; 97 LL st = 1; 98 for(int i = 1; i <= MM; i++) 99 { 100 LL p = pr[i].first, v = pr[i].second; 101 if(i > 1) 102 { 103 if(pr[i-1].first == p - 1) 104 { 105 P.m[0][0] = v, P.m[0][1] = pr[i-1].second; 106 M = M_mul(P, M); 107 st++; 108 if(st > K) break; 109 continue; 110 } 111 P.m[0][0] = s[st%N], P.m[0][1] = pr[i-1].second; 112 M = M_mul(P, M); 113 st++; 114 if(st > K) break; 115 } 116 if(p > K) {M = M_mul(query(st, K), M); st = K + 1; break;} 117 if(st < p) M = M_mul(query(st, p - 1), M), st = p; 118 P.m[0][0] = v, P.m[0][1] = s[(p-1)%N]; 119 M = M_mul(P, M); 120 st++; 121 if(st > K) break; 122 } 123 if(st <= K) 124 { 125 P.m[0][0] = s[st%N], P.m[0][1] = pr[MM].second; 126 M = M_mul(P, M); 127 st++; 128 if(st <= K) M = M_mul(query(st, K), M); 129 } 130 printf("%I64d ", M.m[1][0]); 131 return 0; 132 }
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 const int maxn = 1e5 + 10; 5 typedef long long LL; 6 LL a[maxn], b[maxn]; 7 8 int main(void) 9 { 10 LL N; 11 scanf("%lld", &N); 12 LL sum = 0; 13 for(int i = 1; i <= N; i++) scanf("%lld", a + i), sum = sum + a[i]; 14 LL div = (1 + N) * (LL) N / 2; 15 if(sum % div) {puts("NO"); return 0;} 16 LL rd = sum / div; 17 for(int i = 1; i < N; i++) b[i] = a[i+1] - a[i]; 18 b[N] = a[1] - a[N]; 19 LL ok = 1, cnt = 0; 20 for(int i = 1; i <= N; i++) 21 { 22 b[i] -= rd; 23 if(b[i] > 0 || b[i] % N) {ok = 0; break;} 24 cnt -= b[i] / N; 25 } 26 if(cnt != rd) ok = 0; 27 puts(ok ? "YES" : "NO"); 28 return 0; 29 }
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 typedef long long LL; 5 const int maxn = 1e5 + 10; 6 LL v[maxn]; 7 8 // edge 9 int cnt, h[maxn]; 10 struct edge 11 { 12 int to, pre; 13 } e[maxn<<1]; 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 deg[maxn], ok; 23 LL dfs(int x, int f) 24 { 25 if(deg[x] == 1) return v[x]; 26 LL ret = v[x] + v[x]; 27 for(int i = h[x]; i; i = e[i].pre) 28 { 29 int to = e[i].to; 30 if(to == f) continue; 31 LL tmp = dfs(to, x); 32 if(tmp > v[x]) ok = 0; 33 if(tmp > ret) ok = 0; 34 ret -= tmp; 35 } 36 if(ret > v[x]) ok = 0; 37 return ret; 38 } 39 40 int main(void) 41 { 42 int N; 43 scanf("%d", &N); 44 for(int i = 1; i <= N; i++) scanf("%lld", v + i); 45 for(int i = 1; i < N; i++) 46 { 47 int a, b; 48 scanf("%d %d", &a, &b); 49 add(a, b); add(b, a); 50 deg[a]++; deg[b]++; 51 } 52 if(N == 2) 53 { 54 if(v[1] == v[2]) puts("YES"); 55 else puts("NO"); 56 return 0; 57 } 58 int rt = 0; 59 for(int i = 1; i <= N; i++) 60 if(deg[i] != 1) {rt = i; break;} 61 ok = 1; 62 if(dfs(rt, 0)) ok = 0; 63 puts(ok ? "YES" : "NO"); 64 return 0; 65 }
2.10
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 typedef pair<int, int> pii; 6 int a[3333], sg[3333]; 7 8 // edge 9 int cnt, h[3333]; 10 struct edge 11 { 12 int to, pre; 13 } e[3333]; 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 pii pr[3333]; 23 int main(void) 24 { 25 int N; 26 scanf("%d", &N); 27 for(int i = 1; i <= N; i++) scanf("%d", a + i), pr[i] = pii(a[i], i); 28 for(int i = 1; i < N; i++) 29 { 30 int u, v; 31 scanf("%d %d", &u, &v); 32 if(a[u] > a[v]) add(v, u); 33 if(a[v] > a[u]) add(u, v); 34 } 35 sort(pr + 1, pr + 1 + N); 36 for(int i = 1; i <= N; i++) 37 { 38 int x = pr[i].second; 39 if(!sg[x]) 40 for(int j = h[x]; j; j = e[j].pre) sg[e[j].to] = 1; 41 } 42 for(int i = 1; i <= N; i++) if(sg[i]) printf("%d ", i); 43 puts(""); 44 return 0; 45 }
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 typedef long long LL; 5 double dp[205][2][250][1<<8]; 6 7 int main(void) 8 { 9 int x, K, p; 10 scanf("%d %d %d", &x, &K, &p); 11 if(x & (1 << 8)) 12 { 13 int p = 0; 14 while(x & (1LL << (9 + p))) p++; 15 dp[0][1][p][x&((1<<8)-1)] = 1; 16 } 17 else 18 { 19 if((1 << 9) > x) dp[0][0][0][x&((1<<8)-1)] = 1; 20 else 21 { 22 int p = 0; 23 while(!(x & (1LL << (9 + p)))) p++; 24 dp[0][0][p+1][x&((1<<8)-1)] = 1; 25 } 26 } 27 double p1 = 1.0 * p / 100, p2 = 1 - p1; 28 for(int i = 0; i < K; i++) 29 for(int o = 0; o <= 1; o++) 30 for(int j = 0; j < 250; j++) 31 for(int k = 0; k < (1 << 8); k++) 32 for(int op = 0; op <= 1; op++) 33 { 34 if(op == 0) 35 { 36 int b8 = k & (1 << 7); 37 int nk = (k << 1) & ((1 << 8) - 1); 38 if(b8) 39 { 40 if(o) dp[i+1][1][j+1][nk] += dp[i][o][j][k] * p1; 41 else dp[i+1][1][0][nk] += dp[i][o][j][k] * p1; 42 } 43 else 44 { 45 if(o) dp[i+1][0][1][nk] += dp[i][o][j][k] * p1; 46 else if(!j) dp[i+1][0][0][nk] += dp[i][o][j][k] * p1; 47 else dp[i+1][0][j+1][nk] += dp[i][o][j][k] * p1; 48 } 49 } 50 else 51 { 52 int nk = (k + 1) & ((1 << 8) - 1); 53 int b8 = k & (1 << 7), nb8 = nk & (1 << 7); 54 if(b8 && !nb8) 55 { 56 if(o) dp[i+1][0][j+1][nk] += dp[i][o][j][k] * p2; 57 else dp[i+1][1][0][nk] += dp[i][o][j][k] * p2; 58 } 59 else dp[i+1][o][j][nk] += dp[i][o][j][k] * p2; 60 } 61 } 62 double ans = 0; 63 for(int o = 0; o <= 1; o++) 64 for(int j = 0; j < 250; j++) 65 for(int k = 0; k < (1 << 8); k++) 66 { 67 if(!k) 68 { 69 if(!o) ans += dp[K][o][j][k] * (8 + j); 70 else ans += dp[K][o][j][k] * 8; 71 } 72 else 73 { 74 int cnt = 0; 75 while(!(k & (1 << cnt))) cnt++; 76 ans += dp[K][o][j][k] * cnt; 77 } 78 } 79 printf("%.10f ", ans); 80 return 0; 81 }
2.11
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 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 int main(void) 22 { 23 int n, m; 24 scanf("%d %d", &n, &m); 25 dp[0] = 1; 26 for(int i = 1; i <= n; i++) dp[i] = (dp[i-1] * (2 * m - 1) + qpow(m, i - 1)) % mod; 27 printf("%I64d ", dp[n]); 28 return 0; 29 }
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 const int maxn = 222222; 5 int dp[6][maxn]; 6 7 int main(void) 8 { 9 dp[0][0] = 1; 10 for(int k = 1; k * k * k < maxn; k++) 11 for(int i = 0; i + k * k * k < maxn; i++) 12 for(int j = 0; j < 5; j++) 13 dp[j+1][i+k*k*k] += dp[j][i]; 14 int N; 15 scanf("%d", &N); 16 printf("%d ", dp[5][N] + dp[4][N] + dp[3][N] + dp[2][N] + dp[1][N]); 17 return 0; 18 }
1 #include <iostream> 2 #include <cstdio> 3 #include <map> 4 using namespace std; 5 typedef long long LL; 6 const LL mod = 1e9 + 7; 7 map<LL, LL> M; 8 map<LL, LL> :: iterator it; 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 int main(void) 23 { 24 int m; 25 scanf("%d", &m); 26 for(int i = 1; i <= m; i++) 27 { 28 LL x; 29 scanf("%I64d", &x); 30 M[x]++; 31 } 32 LL mul = 1, ans = 1; 33 for(it = M.begin(); it != M.end(); it++) mul = (mul * (1 + (*it).second)) % (mod + mod - 2); 34 for(it = M.begin(); it != M.end(); it++) ans = (ans * qpow((*it).first, mul * (*it).second % (mod + mod - 2) / 2)) % mod; 35 printf("%I64d ", ans); 36 return 0; 37 }
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int maxn = 1e6 + 10; 6 char s[maxn]; 7 int f[26], l[26]; 8 9 int main(void) 10 { 11 scanf("%s", s + 1); 12 int len = strlen(s + 1); 13 for(int i = 1; i <= len; i++) l[s[i]-'A'] = i; 14 for(int i = len; i >= 1; i--) f[s[i]-'A'] = i; 15 int N; 16 scanf("%d", &N); 17 while(N--) 18 { 19 char s1[22], s2[22]; 20 scanf("%s %s", s1, s2); 21 if(f[s1[0]-'A'] && f[s1[0]-'A'] < l[s2[0]-'A']) puts("YA"); 22 else puts("TIDAK"); 23 } 24 return 0; 25 }
2.13
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 typedef long long LL; 5 const LL mod = 10000007; 6 const int maxn = 1e4 + 10; 7 LL a[maxn]; 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; 16 scanf("%d", &N); 17 for(int i = 1; i <= N; i++) scanf("%lld", a + i); 18 LL ans = 0; 19 for(int i = 0; i <= 32; i++) 20 { 21 int z = 0, o = 0; 22 LL k = 1LL << i; 23 for(int j = 1; j <= N; j++) 24 { 25 if(a[j] & k) o++; 26 else z++; 27 } 28 ans = (ans + (LL) o * z * 2) % mod; 29 } 30 printf("Case %d: %lld ", kase, ans); 31 } 32 return 0; 33 }
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 const int maxn = 1e5 + 10; 6 int a[maxn]; 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", a + i); 17 sort(a + 1, a + 1 + N); 18 int p = 1, ans = N; 19 for(int i = 1; i <= N; i++) 20 if(a[p] + a[p] <= a[i]) p++, ans--; 21 printf("Case %d: %d ", kase, ans); 22 } 23 return 0; 24 }
2.14
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, c; 14 scanf("%lld %lld %lld", &a, &b, &c); 15 LL ans = (a + b + c) / 3; 16 ans = min(ans, a + b); 17 ans = min(ans, a + c); 18 ans = min(ans, c + b); 19 printf("%lld ", ans); 20 } 21 return 0; 22 }
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 using namespace std; 5 const double eps = 1e-9; 6 7 int main(void) 8 { 9 int T; 10 scanf("%d", &T); 11 while(T--) 12 { 13 int x, y, r; 14 scanf("%d %d %d", &x, &y, &r); 15 double L = 0, R = x; 16 while(R - L > eps) 17 { 18 double mid = (L + R) / 2; 19 double t = sqrt(x * x + y * y - r * r - mid * mid); 20 if((mid - x) * (mid - x) + (t - y) * (t - y) > r * r) L = mid; 21 else R = mid; 22 } 23 printf("%.6f %.6f ", L, sqrt(x * x + y * y - r * r - L * L)); 24 } 25 return 0; 26 }
2.16
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <vector> 5 using namespace std; 6 const int maxn = 1e5 + 10; 7 int X[maxn], Y[maxn], L[maxn], R[maxn]; 8 vector<int> v; 9 10 // ST 11 int a[maxn<<2], rmq[maxn<<2][20]; 12 void RMQ_init(int n) 13 { 14 for(int i = 1; i <= n; i++) rmq[i][0] = a[i]; 15 for(int j = 1; (1 << j) <= n; j++) 16 for(int i = 1; i + ( 1 << j ) - 1 <= n; i++) 17 rmq[i][j] = max(rmq[i][j-1] , rmq[i+(1<<j-1)][j-1]); 18 } 19 int RMQ_query(int l, int r) 20 { 21 int k = 0; 22 while( ( 1 << (k + 1) ) <= r - l + 1 ) k++; 23 return max(rmq[l][k], rmq[r-(1<<k)+1][k]); 24 } 25 26 27 int main(void) 28 { 29 int N, Q; 30 scanf("%d", &N); 31 for(int i = 1; i <= N; i++) scanf("%d %d", X + i, Y + i), v.push_back(X[i]), v.push_back(Y[i] + 1); 32 scanf("%d", &Q); 33 for(int i = 1; i <= Q; i++) scanf("%d %d", L + i, R + i), v.push_back(L[i]), v.push_back(R[i]); 34 sort(v.begin(), v.end()); 35 v.erase(unique(v.begin(), v.end()), v.end()); 36 for(int i = 1; i <= N; i++) 37 { 38 int x = lower_bound(v.begin(), v.end(), X[i]) - v.begin() + 1; 39 a[x]++; 40 int y = lower_bound(v.begin(), v.end(), Y[i] + 1) - v.begin() + 1; 41 a[y]--; 42 } 43 for(int i = 1; i <= v.size(); i++) a[i] += a[i-1]; 44 RMQ_init(v.size()); 45 for(int i = 1; i <= Q; i++) 46 { 47 int l = lower_bound(v.begin(), v.end(), L[i]) - v.begin() + 1; 48 int r = lower_bound(v.begin(), v.end(), R[i]) - v.begin() + 1; 49 printf("%d ", RMQ_query(l, r)); 50 } 51 return 0; 52 }
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 typedef long long LL; 6 const LL mod = 1e9 + 7; 7 const int maxn = 1e6 + 10; 8 LL sum[maxn]; 9 10 int main(void) 11 { 12 for(int i = 1; i < maxn; i++) sum[i] = (sum[i-1] + (LL) i * (i - 1) / 2) % mod; 13 int T; 14 scanf("%d", &T); 15 while(T--) 16 { 17 int x, y; 18 scanf("%d %d", &x, &y); 19 printf("%lld ", (sum[y-1] - sum[max(0,x-2)] + mod) % mod); 20 } 21 return 0; 22 }
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 typedef long long LL; 5 const int maxn = 1e5 + 10; 6 LL a[maxn], L[maxn], R[maxn]; 7 8 LL gcd(LL a, LL b) 9 { 10 return a % b ? gcd(b, a % b) : b; 11 } 12 13 int main(void) 14 { 15 int n; 16 scanf("%d", &n); 17 for(int i = 1; i <= n; i++) scanf("%lld", a + i); 18 L[1] = a[1]; 19 for(int i = 2; i <= n; i++) L[i] = gcd(L[i-1], a[i]); 20 R[n] = a[n]; 21 for(int i = n - 1; i; i--) R[i] = gcd(R[i+1], a[i]); 22 if(a[1] % R[2]) {printf("%lld ", R[2]); return 0;} 23 else for(int i = 2; i < n; i++) 24 { 25 LL g = gcd(L[i-1], R[i+1]); 26 if(a[i] % g) {printf("%lld ", g); return 0;} 27 } 28 printf("%lld ", L[n-1]); 29 return 0; 30 }
2.17
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 typedef long long LL; 5 const int maxn = 2e5 + 10; 6 int fa[maxn]; 7 LL r[maxn]; 8 9 int Find(int x) 10 { 11 if(fa[x] == x) return x; 12 int y = Find(fa[x]); 13 r[x] += r[fa[x]]; 14 return fa[x] = y; 15 } 16 17 int main(void) 18 { 19 int N, M; 20 while(~scanf("%d %d", &N, &M)) 21 { 22 int ans = 0; 23 for(int i = 0; i <= N; i++) fa[i] = i, r[i] = 0; 24 while(M--) 25 { 26 int A, B, S; 27 scanf("%d %d %d", &A, &B, &S); 28 A--; 29 if(Find(A) != Find(B)) 30 { 31 int FA = Find(A), FB = Find(B); 32 fa[FA] = FB, r[FA] = r[B] - r[A] + S; 33 } 34 else if(r[A] - r[B] != S) ans++; 35 } 36 printf("%d ", ans); 37 } 38 return 0; 39 }
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 typedef long long LL; 6 const int maxn = 1e5 + 10; 7 8 // edge 9 int cnt, h[maxn]; 10 struct edge 11 { 12 int to, pre; 13 } e[maxn<<1]; 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 // LCA 23 int dep[maxn]; 24 int anc[maxn][33]; 25 void dfs(int x, int fa) 26 { 27 for(int i = h[x]; i; i = e[i].pre) 28 { 29 int to = e[i].to; 30 if(to == fa) continue; 31 dep[to] = dep[x] + 1; 32 anc[to][0] = x; 33 dfs(to, x); 34 } 35 } 36 void LCA_init(int n) 37 { 38 for(int j = 1; (1 << j) < n; j++) 39 for(int i = 1; i <= n; i++) if(anc[i][j-1]) 40 anc[i][j] = anc[anc[i][j-1]][j-1]; 41 } 42 int LCA(int u, int v) 43 { 44 int log; 45 if(dep[u] < dep[v]) swap(u, v); 46 for(log = 0; (1 << log) < dep[u]; log++); 47 for(int i = log; i >= 0; i--) 48 if(dep[u] - (1<<i) >= dep[v]) u = anc[u][i]; 49 if(u == v) return u; 50 for(int i = log; i >= 0; i--) 51 if(anc[u][i] && anc[u][i] != anc[v][i]) 52 u = anc[u][i], v = anc[v][i]; 53 return anc[u][0]; 54 } 55 56 LL X[maxn], Y[maxn]; 57 int st[maxn]; 58 int main(void) 59 { 60 int n, m; 61 scanf("%d", &n); 62 for(int i = 1; i <= n; i++) scanf("%lld %lld", X + i, Y + i); 63 st[0] = n; 64 int p = 0; 65 for(int i = n - 1; i; i--) 66 { 67 while(p && (Y[st[p-1]] - Y[st[p]]) * (X[st[p-1]] - X[i]) > (Y[st[p-1]] - Y[i]) * (X[st[p-1]] - X[st[p]])) p--; 68 add(st[p], i), add(i, st[p]); 69 st[++p] = i; 70 } 71 dfs(n, 0); 72 LCA_init(n); 73 scanf("%d", &m); 74 while(m--) 75 { 76 int a, b; 77 scanf("%d %d", &a, &b); 78 printf("%d ", LCA(a, b)); 79 } 80 return 0; 81 }
2.18
1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <algorithm> 5 #include <cmath> 6 using namespace std; 7 typedef long long LL; 8 const LL mod = 1e9 + 7; 9 const double eps = 1e-9; 10 double x[1111], y[1111]; 11 12 struct node 13 { 14 double x, y, k; 15 node(double x = 0, double y = 0, double k = 0): x(x), y(y), k(k) {} 16 friend bool operator < (node A, node B) 17 { 18 if(fabs(A.x - B.x) > eps) return A.x < B.x; 19 if(fabs(A.y - B.y) > eps) return A.y < B.y; 20 return A.k < B.k; 21 } 22 }; 23 vector<node> v; 24 25 int main(void) 26 { 27 int n; 28 scanf("%d", &n); 29 for(int i = 1; i <= n; i++) 30 { 31 double a, b, c, d; 32 scanf("%lf %lf %lf %lf", &a, &b, &c, &d); 33 x[i] = a / b, y[i] = c / d; 34 double t = x[i] * x[i] + y[i] * y[i]; 35 x[i] /= t, y[i] /= t; 36 } 37 for(int i = 1; i <= n; i++) 38 for(int j = i + 1; j <= n; j++) 39 { 40 double xx = (x[i] + x[j]) / 2; 41 double yy = (y[i] + y[j]) / 2; 42 double kk; 43 if(fabs(x[i] - x[j]) < eps) kk = 1e9; 44 else kk = (y[j] - y[i]) / (x[j] - x[i]); 45 v.push_back(node(xx, yy, kk)); 46 } 47 sort(v.begin(), v.end()); 48 LL ans = 0; 49 for(int i = 0; i < v.size(); i++) 50 { 51 LL cnt = 2, tmp = 1; 52 for(int j = i + 1; j <= v.size(); j++) 53 { 54 if(j == v.size() || fabs(v[j].x - v[i].x) > eps || fabs(v[j].y - v[i].y) > eps) 55 { 56 tmp = (tmp * cnt) % mod; 57 ans = (ans + tmp + mod - 1) % mod; 58 i = j - 1; break; 59 } 60 if(fabs(v[j].k - v[j-1].k) < eps) cnt++; 61 else tmp = (tmp * cnt) % mod, cnt = 2; 62 } 63 } 64 printf("%I64d ", (ans - v.size() + mod) % mod); 65 return 0; 66 }
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 #include <algorithm> 6 using namespace std; 7 typedef long long LL; 8 const int maxn = 222; 9 int a[222]; 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) 19 { 20 Matrix ret; 21 memset(ret.m, -1, sizeof(ret.m)); 22 for(int i = 0; i < maxn; i++) 23 for(int j = 0; j < maxn; j++) 24 for(int k = 0; k < maxn; k++) 25 if(a.m[i][k] != -1 && b.m[k][j] != -1) 26 ret.m[i][j] = max(ret.m[i][j], a.m[i][k] + b.m[k][j]); 27 return ret; 28 } 29 30 Matrix M_qpow(Matrix P, LL n) 31 { 32 Matrix ret = P; 33 while(n) 34 { 35 if(n & 1LL) ret = M_mul(ret, P); 36 n >>= 1LL; 37 P = M_mul(P, P); 38 } 39 return ret; 40 } 41 42 const int maxnode = 222, sigma_size = 26; 43 struct Ac_auto 44 { 45 int Next[maxnode][sigma_size]; 46 int fail[maxnode]; 47 LL val[maxnode]; 48 int sz; 49 50 Ac_auto(){sz = 1; memset(Next[0], 0, sizeof(Next[0]));} 51 void init(){sz = 1; memset(Next[0], 0, sizeof(Next[0]));} 52 53 int idx(char c){return c - 'a';} 54 55 void insert(char * s, int v) 56 { 57 int u = 0, n = strlen(s); 58 for(int i = 0; i < n; i++) 59 { 60 int c = idx(s[i]); 61 if(!Next[u][c]) 62 { 63 memset(Next[sz], 0, sizeof(Next[sz])); 64 Next[u][c] = sz++; 65 val[sz] = 0; 66 } 67 u = Next[u][c]; 68 } 69 val[u] = val[u] + v; 70 } 71 72 void build() 73 { 74 queue<int> q; 75 fail[0] = 0; 76 for(int i = 0; i < sigma_size; i++) if(Next[0][i]) 77 { 78 fail[Next[0][i]] = 0; 79 q.push(Next[0][i]); 80 } 81 while(!q.empty()) 82 { 83 int pos = q.front(); q.pop(); 84 for(int i = 0; i < sigma_size; i++) 85 { 86 if(!Next[pos][i]) Next[pos][i] = Next[fail[pos]][i]; 87 else 88 { 89 fail[Next[pos][i]] = Next[fail[pos]][i]; 90 q.push(Next[pos][i]); 91 } 92 } 93 } 94 } 95 96 Matrix solve() 97 { 98 Matrix ret; 99 memset(ret.m, -1, sizeof(ret.m)); 100 for(int i = 0; i < sz; i++) 101 for(int j = 0; j < sigma_size; j++) 102 { 103 int u = Next[i][j], v = u; 104 if(!u) continue; 105 LL tmp = 0; 106 while(v) 107 { 108 tmp = tmp + val[v]; 109 v = fail[v]; 110 } 111 ret.m[i][u] = tmp; 112 } 113 return ret; 114 } 115 116 } ACA; 117 118 int main(void) 119 { 120 int n; 121 LL L; 122 scanf("%d %I64d", &n, &L); 123 for(int i = 1; i <= n; i++) scanf("%d", a + i); 124 char s[222]; 125 for(int i = 1; i <= n; i++) scanf("%s", s), ACA.insert(s, a[i]); 126 ACA.build(); 127 Matrix O = ACA.solve(); 128 O = M_qpow(O, L - 1); 129 LL ans = 0; 130 for(int i = 0; i < ACA.sz; i++) ans = max(ans, O.m[0][i]); 131 printf("%I64d ", ans); 132 return 0; 133 }
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 const int maxn = 5e5 + 10; 5 char s[maxn], op[maxn]; 6 7 struct List 8 { 9 int x, y, pre, nxt; 10 List(){} 11 } L[maxn]; 12 13 int st[maxn]; 14 int main(void) 15 { 16 int n, m, p; 17 scanf("%d %d %d", &n, &m, &p); 18 scanf("%s", s + 1); 19 scanf("%s", op + 1); 20 21 int tp = 0; 22 for(int i = 1; i <= n; i++) 23 { 24 L[i].pre = i - 1; 25 L[i].nxt = i + 1; 26 if(s[i] == '(') L[i].x = 1, st[++tp] = i; 27 else 28 { 29 L[i].x = -1; 30 L[i].y = st[tp]; 31 L[st[tp]].y = i; 32 tp--; 33 } 34 } 35 L[0].nxt = 1, L[n+1].pre = n; 36 37 for(int i = 1; i <= m; i++) 38 { 39 if(op[i] == 'L') p = L[p].pre; 40 else if(op[i] == 'R') p = L[p].nxt; 41 else 42 { 43 int x = p; 44 int y = L[p].y; 45 if(x > y) swap(x, y); 46 p = L[y].nxt; 47 L[L[x].pre].nxt = L[y].nxt; 48 L[L[y].nxt].pre = L[x].pre; 49 if(p == n + 1) p = L[p].pre; 50 } 51 } 52 53 p = L[0].nxt; 54 while(p != n + 1) 55 { 56 putchar(L[p].x == 1 ? '(' : ')'); 57 p = L[p].nxt; 58 } 59 return 0; 60 }
2.21
1 #include <iostream> 2 #include <cstdio> 3 #include <queue> 4 using namespace std; 5 typedef long long LL; 6 const int maxn = 1e5 + 10; 7 int a1[maxn], b1[maxn], a2[maxn], b2[maxn]; 8 9 struct node 10 { 11 int i, j; 12 LL x; 13 node(int i = 0, int j = 0, LL x = 0): i(i), j(j), x(x) {} 14 friend bool operator < (node A, node B) 15 { 16 return A.x < B.x; 17 } 18 }; 19 priority_queue<node> pq; 20 21 int main(void) 22 { 23 int n; 24 scanf("%d", &n); 25 for(int i = 1; i <= n; i++) scanf("%d %d %d %d", a1 + i, b1 + i, a2 + i, b2 + i); 26 LL ans = 0; 27 for(int i = 1; i <= n; i++) 28 { 29 if(a1[i] <= b2[i] && b1[i] <= a2[i]) continue; 30 if(a1[i] + b1[i] <= a2[i] + b2[i]) 31 { 32 if(a1[i] > b2[i]) ans = ans + a1[i] - b2[i]; 33 else ans = ans + a2[i] - b1[i]; 34 } 35 else pq.push(node(i, 1, a1[i] + b1[i])); 36 } 37 int o = 0; 38 while(!pq.empty()) 39 { 40 o ^= 1; 41 node tmp = pq.top(); pq.pop(); 42 if(tmp.j == 1) 43 { 44 if(o) ans += a1[tmp.i]; 45 else ans -= b1[tmp.i]; 46 pq.push(node(tmp.i, 2, a2[tmp.i] + b2[tmp.i])); 47 } 48 else 49 { 50 if(o) ans += a2[tmp.i]; 51 else ans -= b2[tmp.i]; 52 } 53 } 54 printf("%I64d ", ans); 55 return 0; 56 }
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; typedef long long LL; const int maxn = 1e5 + 10; struct gem { LL a, b; gem(LL a = 0, LL b = 0): a(a), b(b) {} friend bool operator < (gem A, gem B) { if(A.a != B.a) return A.a < B.a; return A.b < B.b; } } g[maxn]; LL dp[maxn]; int main(void) { int n, r, w, h; scanf("%d %d %d %d", &n, &r, &w, &h); for(int i = 1; i <= n; i++) { int x, y; scanf("%d %d", &x, &y); g[i] = gem(y + (LL) r * x, y + (LL) r * (w - x)); } sort(g + 1, g + 1 + n); for(int i = 1; i <= n; i++) dp[i] = 1e18; int ans = 0; for(int i = 1; i <= n; i++) { int x = upper_bound(dp + 1, dp + n, g[i].b) - dp; dp[x] = g[i].b; ans = max(ans, x); } printf("%d ", ans); return 0; }
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 using namespace std; 5 const int maxn = 4e6 + 10; 6 double sum[maxn]; 7 8 int main(void) 9 { 10 for(int i = 1; i < maxn; i++) sum[i] = sum[i-1] + log(i); 11 for(int i = 1; i < maxn; i++) sum[i] /= i; 12 int T; 13 scanf("%d", &T); 14 while(T--) 15 { 16 int a; 17 scanf("%d", &a); 18 printf("%d ", lower_bound(sum + 1, sum + maxn, log(a)) - sum); 19 } 20 return 0; 21 }
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 const int maxn = 1e5 + 10; 5 char s[maxn]; 6 int a[maxn]; 7 8 int main(void) 9 { 10 int N; 11 scanf("%d %s", &N, s + 1); 12 for(int p = 0; p <= 1; p++) 13 for(int q = 0; q <= 1; q++) 14 { 15 a[1] = p, a[2] = q; 16 for(int i = 3; i <= N; i++) a[i] = a[i-2] ^ a[i-1] ^ (s[i-1] == 'x'); 17 if(a[1] != a[N-1] ^ a[N] ^ (s[N] == 'x')) continue; 18 if(a[2] != a[N] ^ a[1] ^ (s[1] == 'x')) continue; 19 for(int i = 1; i <= N; i++) putchar(a[i] ? 'W' : 'S'); 20 puts(""); return 0; 21 } 22 puts("-1"); 23 return 0; 24 }
1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <algorithm> 5 using namespace std; 6 typedef long long LL; 7 vector<LL> v[11111]; 8 9 int main(void) 10 { 11 for(int i = 1; i <= 1e5; i++) 12 { 13 int x = i, tot = 1; 14 for(int j = 2; j * j <= x; j++) 15 { 16 if(x % j) continue; 17 int cnt = 0; 18 while(x % j == 0) x /= j, cnt++; 19 tot *= cnt * 2 + 1; 20 } 21 if(x != 1) tot *= 3; 22 if(tot <= 10000) v[tot].push_back((LL) i * i); 23 } 24 int T; 25 scanf("%d", &T); 26 while(T--) 27 { 28 int k; 29 LL l, r; 30 scanf("%d %lld %lld", &k, &l, &r); 31 printf("%d ", upper_bound(v[k].begin(), v[k].end(), r) - lower_bound(v[k].begin(), v[k].end(), l)); 32 } 33 return 0; 34 }
#include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; typedef long long LL; typedef pair<LL, LL> pii; const int maxn = 5e4 + 10; char s[maxn]; priority_queue<pii> pq; int main(void) { scanf("%s", s + 1); int len = strlen(s + 1), a, b, cnt = 0, ok = 1; LL ans = 0; for(int i = 1; i <= len; i++) { if(s[i] == '(') cnt++; else if(s[i] == ')') cnt--; else { scanf("%d %d", &a, &b); ans += b; s[i] = ')'; pq.push(pii(b - a, i)); cnt--; } if(cnt < 0) { if(pq.empty()) {ok = 0; break;} cnt += 2; pii tmp = pq.top(); pq.pop(); ans -= tmp.first; s[tmp.second] = '('; } } if(cnt) ok = 0; if(!ok) puts("-1"); else printf("%I64d %s ", ans, s + 1); return 0; }
2.22
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 const int maxn = 1e6 + 10; 5 int a[maxn<<1]; 6 7 void change(int s, int d, int e) 8 { 9 if(s + d < e) change(s + d, d, e), a[s+d] = a[s]; 10 else a[e] = a[s]; 11 } 12 13 int main(void) 14 { 15 int n; 16 scanf("%d", &n); 17 for(int i = 1; i <= n; i++) a[i] = i; 18 for(int i = 2; i <= n; i++) change(i - 1, i, i + n - 1); 19 for(int i = n; i < n + n; i++) printf("%d ", a[i]); 20 return 0; 21 }
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 const int INF = 1e9; 6 int G[22][22], deg[22], dp[1<<16]; 7 8 int main(void) 9 { 10 int n, m; 11 scanf("%d %d", &n, &m); 12 for(int i = 1; i <= n; i++) 13 for(int j = 1; j <= n; j++) 14 G[i][j] = INF; 15 int ans = 0; 16 for(int i = 1; i <= m; i++) 17 { 18 int a, b, w; 19 scanf("%d %d %d", &a, &b, &w); 20 G[b][a] = G[a][b] = min(G[a][b], w); 21 ans += w; deg[a]++; deg[b]++; 22 } 23 for(int k = 1; k <= n; k++) 24 for(int i = 1; i <= n; i++) 25 for(int j = 1; j <= n; j++) 26 G[i][j] = min(G[i][j], G[i][k] + G[k][j]); 27 for(int i = 2; i <= n; i++) if(deg[i] && G[1][i] == INF) return puts("-1"); 28 for(int i = 1; i < (1 << n); i++) dp[i] = INF; 29 for(int i = 0; i < (1 << n); i++) 30 { 31 if(dp[i] == INF) continue; 32 int x = 0; 33 for(int j = 1; j <= n; j++) 34 if(deg[j] % 2 && !(i & (1 << (j - 1)))) {x = j; break;} 35 if(!x) continue; 36 for(int j = x + 1; j <= n; j++) 37 if(deg[j] % 2 && !(i & (1 << (j - 1)))) dp[i^(1<<(j-1))^(1<<(x-1))] = min(dp[i^(1<<(j-1))^(1<<(x-1))], dp[i] + G[x][j]); 38 } 39 int msk = 0; 40 for(int i = 1; i <= n; i++) 41 if(deg[i] % 2) msk |= 1 << (i - 1); 42 printf("%d ", ans + dp[msk]); 43 return 0; 44 }
2.23
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <vector> 5 using namespace std; 6 const int maxn = 1e5 + 10; 7 vector<int> vec[maxn]; 8 int d[maxn], a[maxn]; 9 int n, m; 10 11 struct node 12 { 13 int id, t; 14 friend bool operator < (node A, node B) 15 { 16 return A.t < B.t; 17 } 18 } s[maxn]; 19 20 bool check(int day) 21 { 22 for(int i = 1; i <= m; i++) 23 { 24 if(!vec[i].size()) return false; 25 if(vec[i][0] > day) return false; 26 int tmp = upper_bound(vec[i].begin(), vec[i].end(), day) - vec[i].begin() - 1; 27 s[i].id = i, s[i].t = vec[i][tmp]; 28 } 29 sort(s + 1, s + 1 + m); 30 long long sum = 0; 31 for(int i = 1; i <= m; i++) 32 if(s[i].t - 1 - sum < a[s[i].id]) return false; 33 else sum = sum + a[s[i].id] + 1; 34 return true; 35 } 36 37 int main(void) 38 { 39 scanf("%d %d", &n, &m); 40 for(int i = 1; i <= n; i++) 41 { 42 scanf("%d", d + i); 43 if(d[i]) vec[d[i]].push_back(i); 44 } 45 for(int i = 1; i <= m; i++) scanf("%d", a + i); 46 int l = 0, r = n; 47 if(!check(n)) return puts("-1"); 48 while(l < r) 49 { 50 int mid = l + (r - l) / 2; 51 if(check(mid)) r = mid; 52 else l = mid + 1; 53 } 54 printf("%d ", r); 55 return 0; 56 }
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 typedef long long LL; 5 6 bool prime(LL x) 7 { 8 for(int i = 2; i * i <= x; i++) 9 if(x % i == 0) return false; 10 return true; 11 } 12 13 LL gcd(LL a, LL b) 14 { 15 return a % b ? gcd(b, a % b) : b; 16 } 17 18 int main(void) 19 { 20 int T; 21 scanf("%d", &T); 22 while(T--) 23 { 24 LL n; 25 scanf("%I64d", &n); 26 LL pk = n, pkk = n + 1; 27 while(!prime(pk)) pk--; 28 while(!prime(pkk)) pkk++; 29 LL a = pk * pkk - 2 * pkk + 2 * (n - pk + 1); 30 LL b = 2 * pk * pkk; 31 LL g = gcd(a, b); 32 printf("%I64d/%I64d ", a / g, b / g); 33 } 34 return 0; 35 }
2.24
1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 #include <algorithm> 5 #include <cstring> 6 using namespace std; 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 vis[maxn]; 25 int main(void) 26 { 27 GetPrime(); 28 int n; 29 scanf("%d", &n); 30 vector<int> a, b; 31 int ans = 0, two = 0, x = 2; 32 while(x <= n) b.push_back(x), x *= 2, two++; 33 for(int i = 2; pr[i] * 2 <= n; i++) 34 { 35 int cnt = 0; 36 for(int j = 1; j * pr[i] <= n; j++) 37 if(!vis[pr[i] * j]) {vis[pr[i] * j] = 1; cnt++;} 38 if(cnt % 2) two++; 39 ans += cnt / 2; 40 } 41 printf("%d ", ans + two / 2); 42 memset(vis, 0, sizeof(vis)); 43 for(int i = 2; pr[i] * 2 <= n; i++) 44 { 45 for(int j = 1; j * pr[i] <= n; j++) 46 if(!vis[pr[i] * j]) 47 { 48 vis[pr[i] * j] = 1; 49 if(j != 2) a.push_back(pr[i] * j); 50 else b.push_back(pr[i] * j); 51 } 52 if(a.size() % 2) 53 { 54 for(int j = 0; j + 1 < a.size(); j += 2) printf("%d %d ", a[j], a[j+1]); 55 printf("%d %d ", a[a.size()-1], b[b.size()-1]); 56 b.pop_back(); 57 a.clear(); 58 } 59 else 60 { 61 for(int j = 0; j + 1 < a.size(); j += 2) printf("%d %d ", a[j], a[j+1]); 62 a.clear(); 63 } 64 } 65 for(int j = 0; j + 1 < b.size(); j += 2) printf("%d %d ", b[j], b[j+1]); 66 return 0; 67 }
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 typedef long long LL; 5 const LL mod = 1e6 + 3; 6 7 LL qpow(LL a, LL b) 8 { 9 LL ret = 1LL; 10 while(b) 11 { 12 if(b & 1) ret = ret * a % mod; 13 a = a * a % mod; 14 b >>= 1; 15 } 16 return ret; 17 } 18 19 LL inv(LL x) 20 { 21 return qpow(x, mod - 2); 22 } 23 24 int main(void) 25 { 26 LL n, k; 27 scanf("%I64d %I64d", &n, &k); 28 if(n <= 60 && (1LL << n) < k) return puts("1 1"); 29 LL e = (n % (mod - 1)) * ((k - 1) % (mod - 1)) % (mod - 1), d = 0, tmp = k - 1; 30 while(tmp) d = (d + (tmp /= 2)) % (mod - 1); 31 if(k - 1 >= mod) return 0 * printf("%I64d %I64d ", qpow(2, (e - d + mod - 1) % (mod - 1)), qpow(2, (e - d + mod - 1) % (mod - 1))); 32 LL m = 1; 33 for(int i = 1; i < k; i++) m = m * (qpow(2, n % (mod - 1)) - i + mod) % mod; 34 printf("%I64d %I64d ", (qpow(2, (e - d + mod - 1) % (mod - 1)) - m * inv(qpow(2, d)) % mod + mod) % mod, qpow(2, (e - d + mod - 1) % (mod - 1))); 35 return 0; 36 }
2.27
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 const int maxn = 5005; 6 int cnt, h[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 son[maxn], dp[maxn][maxn][2], cpy[maxn][maxn][2]; 22 void dfs(int x, int f) 23 { 24 for(int j = 0; j < maxn; j++) dp[x][j][0] = dp[x][j][1] = maxn; 25 for(int i = h[x]; i; i = e[i].pre) 26 { 27 int to = e[i].to; 28 if(to == f) continue; 29 dfs(to, x); 30 memcpy(cpy[x], dp[x], sizeof(cpy[x])); 31 for(int j = 0; j < maxn; j++) dp[x][j][0] = dp[x][j][1] = maxn; 32 if(!son[x]) 33 for(int k = 0; k <= son[to]; k++) 34 dp[x][k][0] = min(dp[x][k][0], dp[to][k][0]), 35 dp[x][k][0] = min(dp[x][k][0], dp[to][k][1] + 1), 36 dp[x][k][1] = min(dp[x][k][1], dp[to][k][0] + 1), 37 dp[x][k][1] = min(dp[x][k][1], dp[to][k][1]); 38 else 39 for(int j = son[x]; j >= 0; j--) 40 for(int k = 0; k <= son[to]; k++) 41 dp[x][j+k][0] = min(dp[x][j+k][0], cpy[x][j][0] + dp[to][k][0]), 42 dp[x][j+k][0] = min(dp[x][j+k][0], cpy[x][j][0] + dp[to][k][1] + 1), 43 dp[x][j+k][1] = min(dp[x][j+k][1], cpy[x][j][1] + dp[to][k][0] + 1), 44 dp[x][j+k][1] = min(dp[x][j+k][1], cpy[x][j][1] + dp[to][k][1]); 45 son[x] += son[to]; 46 } 47 if(!son[x]) dp[x][1][1] = dp[x][0][0] = 0, son[x] = 1; 48 } 49 50 int deg[maxn]; 51 int main(void) 52 { 53 int n; 54 scanf("%d", &n); 55 for(int i = 1; i < n; i++) 56 { 57 int u, v; 58 scanf("%d %d", &u, &v); 59 add(u, v), add(v, u); 60 deg[u]++, deg[v]++; 61 } 62 if(n == 2) return puts("1"); 63 int rt = 1, lf = 0; 64 for(int i = 1; i <= n; i++) 65 if(deg[i] != 1) rt = i; 66 else lf++; 67 dfs(rt, 0); 68 printf("%d ", min(dp[rt][lf/2][0], dp[rt][lf/2][1])); 69 return 0; 70 }
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 = 1010; 10 int lv[maxn], it[maxn]; 11 int cnt, h[maxn]; 12 13 struct edge 14 { 15 int to, pre, cap; 16 } e[5000000]; 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 vector<int> vec; 95 int u[250000], v[250000]; 96 int main(void) 97 { 98 int T; 99 scanf("%d", &T); 100 while(T--) 101 { 102 init(); 103 int n, m, k; 104 scanf("%d %d %d", &n, &m, &k); 105 vec.clear(); 106 for(int i = 1; i <= k; i++) 107 { 108 int x; 109 scanf("%d", &x); 110 vec.push_back(x); 111 } 112 for(int i = 1; i <= m; i++) scanf("%d %d", u + i, v + i); 113 int S = n + 1, T = S + 1; 114 int l = 1, r = k, mid; 115 while(l < r) 116 { 117 init(); 118 int mid = l + (r - l) / 2; 119 ad(S, 1, INF); 120 for(int i = 0; i < vec.size(); i++) ad(vec[i], T, 1); 121 for(int i = 1; i <= m; i++) add(u[i], v[i], mid), add(v[i], u[i], mid); 122 if(Dinic(S, T) == k) r = mid; 123 else l = mid + 1; 124 } 125 printf("%d ", r); 126 } 127 return 0; 128 }
2.28
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 int main(void) 6 { 7 int n; 8 while(~scanf("%d", &n) && n) printf("%d ", n / 2); 9 return 0; 10 }
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 #include <algorithm> 5 using namespace std; 6 const int maxn = 1e6 + 10; 7 int block, ca[maxn], cb[maxn], ans[maxn]; 8 int a[maxn], b[maxn]; 9 10 struct query 11 { 12 int id, l, r; 13 }Q[maxn]; 14 15 bool cmp(query A, query B) 16 { 17 if(A.l / block != B.l / block) return A.l / block < B.l / block; 18 return A.r < B.r; 19 } 20 21 int main(void) 22 { 23 int N, M, K; 24 scanf("%d %d %d", &N, &M, &K); 25 for(int i = 1; i <= N; i++) scanf("%d", a + i); 26 for(int i = 1; i <= N; i++) scanf("%d", b + i); 27 for(int i = 0; i < M; i++) 28 scanf("%d %d", &Q[i].l, &Q[i].r), Q[i].l++, Q[i].r++, Q[i].id = i; 29 block = sqrt(N), sort(Q, Q + M, cmp); 30 int l = 1, r = 0, tmp = 0; 31 for(int i = 0; i < M; i++) 32 { 33 while(r < Q[i].r) 34 { 35 r++; 36 if(cb[a[r]]) cb[a[r]]--, tmp++; 37 else ca[a[r]]++; 38 if(ca[b[r]]) ca[b[r]]--, tmp++; 39 else cb[b[r]]++; 40 } 41 while(r > Q[i].r) 42 { 43 if(ca[a[r]]) ca[a[r]]--; 44 else cb[a[r]]++, tmp--; 45 if(cb[b[r]]) cb[b[r]]--; 46 else ca[b[r]]++, tmp--; 47 r--; 48 } 49 while(l < Q[i].l) 50 { 51 if(ca[a[l]]) ca[a[l]]--; 52 else cb[a[l]]++, tmp--; 53 if(cb[b[l]]) cb[b[l]]--; 54 else ca[b[l]]++, tmp--; 55 l++; 56 } 57 while(l > Q[i].l) 58 { 59 l--; 60 if(cb[a[l]]) cb[a[l]]--, tmp++; 61 else ca[a[l]]++; 62 if(ca[b[l]]) ca[b[l]]--, tmp++; 63 else cb[b[l]]++; 64 } 65 ans[Q[i].id] = tmp; 66 } 67 for(int i = 0; i < M; i++) printf("%d ", ans[i]); 68 return 0; 69 }
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 using namespace std; 5 typedef long long LL; 6 LL c[1111]; 7 8 int main(void) 9 { 10 LL t, a, b, ans = 0, cnt = 0, tmp; 11 scanf("%I64d %I64d %I64d", &t, &a, &b); 12 if(t == 1 && a == 1 && b == 1) return puts("inf"); 13 if(t == 1 && a == 1) return puts("0"); 14 if(a == 1 && b == 1) return puts("1"); 15 if(a == 1) return puts("0"); 16 for(tmp = a; tmp <= b; tmp *= a) 17 { 18 if(tmp == b) ans++; 19 if(log(tmp) + log(a) > log(b) + 1e-9) break; 20 } 21 while(b) c[++cnt] = b % a, b /= a; 22 tmp = 0; 23 for(int i = cnt; i; i--) tmp = tmp * t + c[i]; 24 if(tmp == a && cnt > 1) ans++; 25 printf("%d ", ans); 26 return 0; 27 }
3.1
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 = 2e5 + 10; 8 int cnt, h[maxn], f[maxn]; 9 10 struct edge 11 { 12 int to, pre; 13 } e[maxn<<1]; 14 15 void add(int from, int to) 16 { 17 cnt++; 18 e[cnt].pre = h[from]; 19 e[cnt].to = to; 20 h[from] = cnt; 21 } 22 23 int tot = 0; 24 int in[maxn], out[maxn]; 25 void dfs(int x) 26 { 27 in[x] = ++tot; 28 for(int i = h[x]; i; i = e[i].pre) dfs(e[i].to); 29 out[x] = ++tot; 30 } 31 32 vector<pii> v; 33 int main(void) 34 { 35 int N, Q; 36 scanf("%d", &N); 37 for(int i = 1; i <= N; i++) 38 { 39 scanf("%d", f + i); 40 if(f[i]) add(f[i], i); 41 } 42 for(int i = 1; i <= N; i++) 43 if(!f[i]) dfs(i); 44 scanf("%d", &Q); 45 while(Q--) 46 { 47 v.clear(); 48 int M, x; 49 scanf("%d", &M); 50 for(int i = 1; i <= M; i++) 51 { 52 scanf("%d", &x); 53 v.push_back(pii(in[x], out[x])); 54 } 55 sort(v.begin(), v.end()); 56 int ans = (v[0].second - v[0].first + 1) / 2; 57 for(int i = 1; i < M; i++) 58 if(v[i].second > v[i-1].second) ans += (v[i].second - v[i].first + 1) / 2; 59 else v[i] = v[i-1]; 60 printf("%d ", ans); 61 } 62 return 0; 63 }
3.2
1 #include <iostream> 2 #include <cstdio> 3 #include <vector> 4 using namespace std; 5 typedef long long LL; 6 const int maxn = 1e5 + 10; 7 int cnt[maxn], tot; 8 9 vector<int> v[maxn]; 10 void dfs(int dep, int x) 11 { 12 if(cnt[dep+1] > x + x) dfs(dep + 1, x + x); 13 v[dep].push_back(++tot); 14 if(cnt[dep+1] > x + x + 1) dfs(dep + 1, x + x + 1); 15 } 16 17 void ans_print(int dep, int x) 18 { 19 printf("%d ", v[dep][x]); 20 if(cnt[dep+1] > x + x) ans_print(dep + 1, x + x); 21 if(cnt[dep+1] > x + x + 1) ans_print(dep + 1, x + x + 1); 22 } 23 24 int main(void) 25 { 26 int q; 27 scanf("%d", &q); 28 while(q--) 29 { 30 LL c; 31 int n, p = 1; 32 scanf("%d %lld", &n, &c); 33 c = -c; 34 for(int i = 0; i < n; i++) cnt[i] = 1, c += i, v[i].clear(); 35 cnt[n] = 0; 36 if(c < 0) {puts("-1"); continue;} 37 for(int i = n - 1; i > p; i--) 38 { 39 int d = min(c, (LL) (i - p)); 40 if(!d) break; 41 c -= d, cnt[i-d]++, cnt[i] = 0; 42 if(cnt[p] == cnt[p-1] + cnt[p-1]) p++; 43 } 44 if(c) {puts("-1"); continue;} 45 tot = 0; 46 dfs(0, 0), ans_print(0, 0), puts(""); 47 } 48 return 0; 49 }
3.3
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 typedef long long LL; 5 6 int main(void) 7 { 8 LL a, tmp = 1e17, x = 0; 9 scanf("%I64d", &a); 10 for(int i = 1; i <= 810; i++) x = (x + tmp) % a; 11 printf("%I64d %I64d ", a - x, a - x + 10 * tmp - 1); 12 return 0; 13 }
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <vector> 5 using namespace std; 6 const int maxn = 3e5 + 10; 7 int a[maxn], m[maxn][20], g[maxn][20]; 8 vector<int> v; 9 10 int gcd(int a, int b) 11 { 12 return a % b ? gcd(b, a % b) : b; 13 } 14 15 void RMQ_init(int n) 16 { 17 for(int i = 1; i <= n; i++) m[i][0] = g[i][0] = a[i]; 18 for(int j = 1; (1 << j) <= n; j++) 19 for(int i = 1; i + ( 1 << j ) - 1 <= n; i++) 20 m[i][j] = min(m[i][j-1] , m[i+(1<<j-1)][j-1]), 21 g[i][j] = gcd(g[i][j-1] , g[i+(1<<j-1)][j-1]); 22 } 23 24 int mq(int l, int r) 25 { 26 int k = 0; 27 while( ( 1 << (k + 1) ) <= r - l + 1 ) k++; 28 return min(m[l][k], m[r-(1<<k)+1][k]); 29 } 30 31 int gq(int l, int r) 32 { 33 int k = 0; 34 while( ( 1 << (k + 1) ) <= r - l + 1 ) k++; 35 return gcd(g[l][k], g[r-(1<<k)+1][k]); 36 } 37 38 bool ok(int mid, int n) 39 { 40 for(int i = 1; i + mid - 1 <= n; i++) 41 if(mq(i, i + mid - 1) == gq(i, i + mid - 1)) 42 return true; 43 return false; 44 } 45 46 int main(void) 47 { 48 int n; 49 scanf("%d", &n); 50 for(int i = 1; i <= n; i++) scanf("%d", a + i); 51 RMQ_init(n); 52 int l = 1, r = n, mid; 53 while(l < r) 54 { 55 mid = r - (r - l) / 2; 56 if(ok(mid, n)) l = mid; 57 else r = mid - 1; 58 } 59 for(int i = 1; i + l - 1 <= n; i++) 60 if(mq(i, i + l - 1) == gq(i, i + l - 1)) 61 v.push_back(i); 62 printf("%d %d ", v.size(), l - 1); 63 for(int i = 0; i < v.size(); i++) printf("%d ", v[i]); 64 return 0; 65 }
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 using namespace std; 5 const int maxn = 5e5 + 10; 6 typedef long long LL; 7 int c[maxn]; 8 9 int main(void) 10 { 11 int n, k, M = 0, m = 2e9; 12 LL sum = 0; 13 scanf("%d %d", &n, &k); 14 for(int i = 1; i <= n; i++) scanf("%d", c + i), M = max(M, c[i]), m = min(m, c[i]), sum += c[i]; 15 int l = (sum + n - 1) / n, r = M; 16 while(l < r) 17 { 18 LL cnt = 0; 19 int mid = (l + r) / 2; 20 for(int i = 1; i <= n; i++) 21 if(c[i] > mid) cnt += c[i] - mid; 22 if(cnt <= k) r = mid; 23 else l = mid + 1; 24 } 25 int u = r; 26 l = m, r = sum / n; 27 while(l < r) 28 { 29 LL cnt = 0; 30 int mid = r - (r - l) / 2; 31 for(int i = 1; i <= n; i++) 32 if(c[i] < mid) cnt += mid - c[i]; 33 if(cnt <= k) l = mid; 34 else r = mid - 1; 35 } 36 int d = l; 37 printf("%d ", u - d); 38 return 0; 39 }
3.4
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 = 2e5 + 10; 8 int a[maxn], p[maxn], g[maxn]; 9 vector<pii> v; 10 11 int main(void) 12 { 13 int n; 14 scanf("%d", &n); 15 for(int i = 1; i <= n; i++) scanf("%d", a + i); 16 int P = 0, G = 0; 17 for(int i = 1; i <= n; i++) 18 if(a[i] == 1) p[++P] = i; 19 else g[++G] = i; 20 for(int i = n; i; i--) 21 { 22 int wp = 0, wg = 0; 23 P = G = 0; 24 while(1) 25 { 26 if(!p[P+i] && !g[G+i]) break; 27 if(!p[P+i]) G += i, P = g[G] - G, wg++; 28 else if(!g[G+i]) P += i, G = p[P] - P, wp++; 29 else if(p[P+i] < g[G+i]) P += i, G = p[P] - P, wp++; 30 else G += i, P = g[G] - G, wg++; 31 if(p[P] == n && wp > wg) {v.push_back(pii(wp, i)); break;} 32 if(g[G] == n && wg > wp) {v.push_back(pii(wg, i)); break;} 33 } 34 } 35 sort(v.begin(), v.end()); 36 printf("%d ", v.size()); 37 for(int i = 0; i < v.size(); i++) printf("%d %d ", v[i].first, v[i].second); 38 return 0; 39 }
1 #include <iostream> 2 #include <cstdio> 3 #include <set> 4 using namespace std; 5 typedef long long LL; 6 set<LL> s[22]; 7 LL a[5555]; 8 9 int main(void) 10 { 11 int n, k, q; 12 scanf("%d %d", &n, &k); 13 for(int i = 1; i <= n; i++) scanf("%lld", a + i); 14 for(int i = 1; i <= n; i++) 15 for(int j = 0; j <= k; j++) 16 s[j].insert(a[i] * j); 17 scanf("%d", &q); 18 while(q--) 19 { 20 LL x; 21 scanf("%lld", &x); 22 int ans = k + 1; 23 for(int i = 1; i <= n; i++) 24 for(int j = 0; j <= k && a[i] * j <= x; j++) 25 for(int p = 0; p + j <= k; p++) 26 if(s[p].find(x - a[i] * j) != s[p].end()) 27 ans = min(ans, j + p); 28 printf("%d ", ans == k + 1 ? -1 : ans); 29 } 30 return 0; 31 }