solution
stringlengths
11
983k
difficulty
int64
0
21
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; template <typename T> inline void inp(T &any) { cin >> any; } template <typename T, typename... U> inline void inp(T &a, U &...b) { cin >> a; inp(b...); } template <typename T> inline istream &operator>>(istream &in, vector<T> &a) { for (auto &x : a) in >> x; return in; } template <typename T, typename U> inline istream &operator>>(istream &in, pair<T, U> &a) { in >> a.first >> a.second; return in; } long long int LIS(vector<long long int> &a) { set<long long int> ms; for (auto x : a) { ms.emplace(x); auto it = ms.upper_bound(x); if (it != ms.end()) { ms.erase(it); } } return ms.size(); } void solve(long long int &T) { long long int n; cin >> n; vector<long long int> a(n); cin >> a; cout << LIS(a) << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(0); int t = 1; for (long long int Case = (1); Case < (t + 1); Case++) { if (0) cerr << "Case #" << Case << "\n"; solve(Case); } return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int m[100500]; int main() { int n; scanf("%d", &n); memset(m, 63, sizeof m); m[0] = -1; int ans = 1; for (int i = 0; i < (int)(n); ++i) { int a; scanf("%d", &a); int l = 0, r = n; while (r - l > 1) { int mid = (l + r) / 2; if (m[mid] <= a) l = mid; else r = mid; } m[r] = a; if (ans < r) ans = r; } printf("%d", ans); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int f(vector<int>& v) { vector<int> tail(v.size(), 0); int length = 1; tail[0] = v[0]; for (int i = 1; i < v.size(); i++) { auto b = tail.begin(), e = tail.begin() + length; auto it = lower_bound(b, e, v[i]); if (it == tail.begin() + length) tail[length++] = v[i]; else *it = v[i]; } return length; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n; cin >> n; vector<int> ar(n); long long mx = 0; for (long long i = 0; i < (long long)n; ++i) { cin >> ar[i]; } cout << f(ar) << endl; }
8
CPP
#include <bits/stdc++.h> using namespace std; int STree[100000 * 4 + 5]; void update(int node, int l, int r, int a, int b) { if (l == r) { STree[node] = b; } else { int m = (l + r) >> 1; if (a <= m) { update(2 * node + 1, l, m, a, b); } else { update(2 * node + 2, m + 1, r, a, b); } STree[node] = max(STree[2 * node + 1], STree[2 * node + 2]); } } int query(int node, int l, int r, int a, int b) { if (r < a || l > b) return 0; if (a <= l && r <= b) return STree[node]; int m = (l + r) >> 1; int ans = 0; if (a <= m) ans = max(ans, query(2 * node + 1, l, m, a, b)); if (b > m) ans = max(ans, query(2 * node + 2, m + 1, r, a, b)); return ans; } int main() { int n; scanf("%d", &n); int num[n + 5]; for (int i = 0; i < n; i++) scanf("%d", &num[i]); int ans = 0; for (int i = n - 1; i >= 0; i--) { num[i]--; int tmp = query(0, 0, n - 1, num[i], n - 1) + 1; update(0, 0, n - 1, num[i], tmp); ans = max(ans, tmp); } printf("%d\n", ans); return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int main() { int i, j, n, top, temp; int stack[101101]; cin >> n; top = 0; stack[0] = -1; for (i = 0; i < n; i++) { cin >> temp; if (temp > stack[top]) { stack[++top] = temp; } else { int low = 1, high = top; int mid; while (low <= high) { mid = (low + high) / 2; if (temp > stack[mid]) { low = mid + 1; } else { high = mid - 1; } } stack[low] = temp; } } cout << top << endl; return 0; }
8
CPP
#include <bits/stdc++.h> using namespace std; int n; int a[100005]; int dp[100005], INF; int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", a + i); memset(dp, 63, sizeof(dp)), INF = dp[0]; dp[0] = 0; for (int i = 1; i <= n; ++i) { int idx = lower_bound(dp, dp + 1 + n, a[i]) - dp; dp[idx] = min(dp[idx], a[i]); } for (int i = n; i >= 1; --i) if (dp[i] != INF) { printf("%d", i); break; } }
8
CPP
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; int a[200000], b[200000], n, m, sum = 0, ans, maxx, pos, len, seq[200000]; int f[200000] = {0}; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; i <= n - 1; i++) { cin >> a[i]; } m = n; ans = 0; for (auto i : a) { pos = lower_bound(seq, seq + len, i) - seq; seq[pos] = i; len = max(len, pos + 1); f[i] = len; } cout << len; return 0; }
8
CPP
import bisect n=int(input()) a=list(map(int,input().split())) INF=10**18 dp=[INF]*n for i in range(n): dp[bisect.bisect_left(dp,a[i])]=a[i] print(bisect.bisect_left(dp,INF))
8
PYTHON3
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 10; long long a[MAXN], d[MAXN], cnt[MAXN], ans, n, siz; void cut(long long x) { siz = 0; for (long long i = 1; i * i <= x; i++) { if (x % i == 0) { d[++siz] = i; if (x != i * i) d[++siz] = x / i; } } memset(cnt, 0, sizeof(cnt)); } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long random(long long x, long long y) { return (long long)rand() * rand() % (y - x + 1) + x; } signed main() { srand(time(NULL)); scanf("%I64d", &n); for (long long i = 1; i <= n; i++) scanf("%I64d", &a[i]); for (long long T = 1; T <= 10; T++) { long long x = a[random(1, n)]; cut(x); sort(d + 1, d + siz + 1); for (long long i = 1; i <= n; i++) { long long pos = lower_bound(d + 1, d + siz + 1, gcd(x, a[i])) - d; cnt[pos]++; } for (long long i = 1; i <= siz; i++) for (long long j = i + 1; j <= siz; j++) if (d[j] % d[i] == 0) cnt[i] += cnt[j]; for (long long i = siz; i >= 1; i--) { if (cnt[i] * 2 >= n) { ans = max(ans, d[i]); break; } } } printf("%I64d\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; long long a[1000005], b[1000005]; int n, tot, cnt[1000005]; long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); } int main() { long long ans = 1; srand(time(NULL)); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%I64d", &a[i]); } for (int step = 0; step < 10; step++) { long long val = a[(rand() << 15 | rand()) % n]; tot = 0; for (long long i = 1; i * i <= val; i++) { if (val % i == 0) { b[tot++] = i; if (i != val / i) { b[tot++] = val / i; } } } sort(b, b + tot); fill(cnt, cnt + tot, 0); for (int i = 0; i < n; i++) { cnt[lower_bound(b, b + tot, gcd(a[i], val)) - b]++; } for (int i = tot - 1; i >= 0; i--) { if (b[i] <= ans) break; int sum = 0; for (int j = i; j < tot; j++) { if (b[j] % b[i] == 0) sum = sum + cnt[j]; } if (sum * 2 >= n) { ans = b[i]; } } } printf("%I64d\n", ans); }
10
CPP
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char ch = getchar(); for (; ch < '0' || ch > '9'; ch = getchar()) if (ch == '-') f = -1; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 1) + (x << 3) + ch - 48; return x * f; } inline long long gcd(long long x, long long y) { return (!y) ? x : gcd(y, x % y); } long long n, a[1000005], g[1000005], p[10000005], cnt[10000005], ans = 1; bool vis[1000005]; inline void solve(long long x) { p[0] = 0; for (long long i = 1; i * i <= a[x]; i++) if (a[x] % i == 0) { p[++p[0]] = i; if (i * i != a[x]) p[++p[0]] = a[x] / i; } for (long long i = 1; i <= p[0]; i++) cnt[i] = 0; sort(p + 1, p + p[0] + 1); for (long long i = 1; i <= n; i++) ++cnt[lower_bound(p + 1, p + p[0] + 1, gcd(a[i], a[x])) - p]; for (long long i = 1; i <= p[0] - 1; i++) for (long long j = i + 1; j <= p[0]; j++) if (p[j] % p[i] == 0) cnt[i] += cnt[j]; for (long long i = 1; i <= p[0]; i++) if (cnt[i] * 2 >= n) ans = max(ans, p[i]); } signed main() { srand(time(NULL)); n = read(); for (long long i = 1; i <= n; i++) a[i] = read(); for (long long i = 1; i <= min(n, 10ll); i++) { long long m = ((rand() << 15) + rand()) % n + 1; while (vis[m]) m = ((rand() << 15) + rand()) % n + 1; vis[m] = 1; solve(m); } printf("%I64d", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1000005; const int T = 11; long long a[N]; long long gcd(long long a, long long b) { if (b > a) swap(a, b); return b ? gcd(b, a % b) : a; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); mt19937 mt_rand(time(NULL)); int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; long long ans = 1; for (int i = 0; i < T; i++) { int idx = mt_rand() % n, sum = 0; vector<long long> div0, div1; for (int i = 1; i <= sqrt(a[idx]); i++) { if (a[idx] % i == 0) { div0.push_back(i); if (a[idx] / i != i) div1.push_back(a[idx] / i); } } reverse(div1.begin(), div1.end()); div0.insert(div0.end(), div1.begin(), div1.end()); vector<long long> cnt(div0.size()); for (int i = 0; i < n; i++) { int id = lower_bound(div0.begin(), div0.end(), gcd(a[i], a[idx])) - div0.begin(); cnt[id]++; } for (int i = div0.size() - 1; i >= 0; i--) { long long sum = cnt[i]; for (int j = i + 1; j < div0.size(); j++) { if (div0[j] % div0[i] == 0) sum += cnt[j]; } if (sum >= (n + 1) / 2) { ans = max(ans, div0[i]); break; } } } cout << ans << endl; }
10
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 5; long long a[N], s[N], n, ans, b[N], top; inline long long gcd(const long long &x, const long long &y) { return x ? gcd(y % x, x) : y; } signed main() { scanf("%lld", &n); for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]); for (long long I = 1; I <= 10; I++) { long long x = a[rand() % n * rand() % n + 1]; top = 0; for (long long i = 1; i * i <= x; i++) if (x % i == 0) { s[++top] = i; if (i * i != x) s[++top] = x / i; } for (long long i = 1; i <= top; i++) b[i] = 0; sort(s + 1, s + top + 1); static long long now; for (long long i = 1; i <= n; i++) { now = lower_bound(s + 1, s + top + 1, gcd(a[i], x)) - s; b[now]++; } for (long long i = 1; i <= top; i++) for (long long j = i + 1; j <= top; j++) if (s[j] % s[i] == 0) b[i] += b[j]; for (long long i = 1; i <= top; i++) if (b[i] << 1 >= n) ans = max(ans, s[i]); } printf("%lld", ans); }
10
CPP
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } long long sol, V, pula[1000100], x, dej[14], b[1000100], c[1000100], a[1000100]; int n, t, K, fr[1000100], r, pick; int main() { srand(time(0)); scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%lld", &a[i]); b[i] = a[i]; } r = n; sol = 1; for (int kkt = 1; kkt <= 6; ++kkt) { t = 0; K = 0; if (!r) break; int p = rand() % r + 1; x = b[p]; V = b[p]; int oldr = r; r = 0; for (int i = 1; i <= oldr; ++i) if (b[i] != x) c[++r] = b[i]; for (int i = 1; i <= r; ++i) b[i] = c[i]; int lim = 1 << 20; for (int i = 1; i <= lim; ++i) { if (1LL * i * i > x) break; if (x % i == 0) { pula[++t] = x / i; pula[++t] = i; } } if ((long long)sqrt(x) * (long long)sqrt(x) == x) --t; for (int i = 1; i <= t; ++i) fr[i] = 0; sort(pula + 1, pula + t + 1); for (int i = 1; i <= n; ++i) { x = gcd(V, a[i]); int st = 1; int dr = t; while (st <= dr) { int mij = (st + dr) >> 1; if (pula[mij] == x) { ++fr[mij]; break; } else if (pula[mij] > x) dr = mij - 1; else st = mij + 1; } } for (int i = 1; i <= t; ++i) for (int j = i + 1; j <= t; ++j) { if (pula[j] % pula[i] == 0) fr[i] += fr[j]; } for (int i = t; i >= 1; --i) if (fr[i] * 2 >= n) { if (pula[i] > sol) sol = pula[i]; break; } } cout << sol; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int n; long long a[1000020], b[1000020]; long long ans; long long san[1000020]; int cnt, c[1000020], d[1000020]; set<long long> s; long long gcd(long long a, long long b) { while (a && b && (a >= b ? a %= b : b %= a)) ; return a + b; } int haxi(long long x) { return lower_bound(san + 1, san + cnt + 1, x) - san; } void calc(int x) { cnt = 0; for (int i = 1; i <= n; ++i) { b[i] = gcd(a[i], a[x]); } for (int i = 1; 1LL * i * i <= a[x]; ++i) { if (a[x] % i) continue; san[++cnt] = i; if (1LL * i * i != a[x]) san[++cnt] = a[x] / i; } sort(san + 1, san + cnt + 1); cnt = unique(san + 1, san + cnt + 1) - san - 1; for (int i = 1; i <= cnt; ++i) c[i] = d[i] = 0; for (int i = 1; i <= n; ++i) c[haxi(b[i])]++; for (int i = 1; i <= cnt; ++i) { if (san[i] <= ans) continue; for (int j = i; j <= cnt; ++j) { if (san[j] % san[i] == 0) d[i] += c[j]; } } for (int i = 1; i <= cnt; ++i) { if (d[i] * 2 >= n) ans = max(ans, san[i]); } } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%I64d", &a[i]); ans = 1; srand(233); for (int x = 1, cnt = 0; x <= 12 && cnt <= 1000020; ++x, ++cnt) { int v = 1LL * rand() * rand() % n + 1; if (s.count(a[v])) { --x; continue; } s.insert(a[v]); calc(v); } printf("%I64d\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> #pragma GCC optimize("O500") #pragma comment(linker, "/STACK:1677777216") #pragma warning(default : 4) using namespace std; const double eps = 1e-12; const int oo = 0x3F3F3F3F; const long long ooLL = 0x3F3F3F3F3F3F3F3FLL; const int MOD = 1000000007; template <typename T> T sqr(T x) { return x * x; } template <typename T> void debpr(const T &); template <typename T1, typename T2> void debpr(const pair<T1, T2> &); template <typename T1, typename T2> void debpr(const vector<T1, T2> &); template <typename T> void debpr(const set<T> &); template <typename T1, typename T2> void debpr(const map<T1, T2> &); template <typename T> void prcont(T be, T en, const string &st, const string &fi, const string &mi) { debpr(st); bool ft = 0; while (be != en) { if (ft) debpr(mi); ft = 1; debpr(*be); ++be; } debpr(fi); } template <typename T> void debpr(const T &a) {} template <typename T1, typename T2> void debpr(const pair<T1, T2> &p) { debpr("("); debpr(p.first); debpr(", "); debpr(p.second); debpr(")"); } template <typename T1, typename T2> void debpr(const vector<T1, T2> &a) { prcont(a.begin(), a.end(), "[", "]", ", "); } template <typename T> void debpr(const set<T> &a) { prcont(a.begin(), a.end(), "{", "}", ", "); } template <typename T1, typename T2> void debpr(const map<T1, T2> &a) { prcont(a.begin(), a.end(), "{", "}", ", "); } void deb(){}; template <typename T1> void deb(const T1 &t1) { debpr(t1); debpr('\n'); } template <typename T1, typename T2> void deb(const T1 &t1, const T2 &t2) { debpr(t1); debpr(' '); debpr(t2); debpr('\n'); } template <typename T1, typename T2, typename T3> void deb(const T1 &t1, const T2 &t2, const T3 &t3) { debpr(t1); debpr(' '); debpr(t2); debpr(' '); debpr(t3); debpr('\n'); } template <typename T1, typename T2, typename T3, typename T4> void deb(const T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4) { debpr(t1); debpr(' '); debpr(t2); debpr(' '); debpr(t3); debpr(' '); debpr(t4); debpr('\n'); } const double PI = acos(-1.); long long Round(double x) { return x < 0 ? x - .5 : x + .5; } template <typename T> void ass(bool v, const T &x, string m = "Fail") { if (!v) { deb(m); deb(x); throw; } } int main() { void run(); run(); return 0; } long long a[1 << 20]; int n; long long gcd(long long a, long long b) { long long t; while (a) b %= a, t = a, a = b, b = t; return b; } long long mrand() { long long rs = 0; for (int i = (0), _b(60); i < _b; ++i) if (rand() % 2) rs += 1LL << i; return rs; } void run() { scanf("%d", &n); for (int i = (0), _b(n); i < _b; ++i) scanf("%lld", &a[i]); long long rs = 1; srand(4); for (int it = (0), _b(12); it < _b; ++it) { long long t = a[mrand() % n]; unordered_map<long long, int> m; m.reserve(1 << 15); for (int i = (0), _b(n); i < _b; ++i) ++m[gcd(a[i], t)]; vector<pair<long long, long long> > v((m).begin(), (m).end()); for (int i = (0), _b(v.size()); i < _b; ++i) { int z = 0; for (int j = (0), _b(v.size()); j < _b; ++j) if (v[j].first % v[i].first == 0) z += v[j].second; if (z >= (n + 1) / 2) rs = max(rs, v[i].first); } } cout << rs << endl; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int n; long long a[N]; int c[N], tot = 0; long long p[N]; int sum[N], id[N]; long long work(long long x) { tot = 0; int all = 1; for (long long i = 2; i * i <= x; i++) { if (x % i == 0) { p[++tot] = i; c[tot] = 0; while (x % i == 0) x /= i, c[tot]++; all = all * (c[tot] + 1); } } if (x != 1) p[++tot] = x, c[tot] = 1, all *= 2; for (int i = 0; i < all; i++) sum[i] = 0; for (int i = 1; i <= n; i++) { long long no = a[i]; int pos = 0; for (int j = tot; j >= 1; j--) { int cnt = 0; while (no % p[j] == 0) cnt++, no /= p[j]; cnt = min(cnt, c[j]); pos = pos * (c[j] + 1) + cnt; } sum[pos]++; } for (int i = 1; i <= tot; i++) { int le = 1; for (int j = 1; j < i; j++) le *= (c[j] + 1); for (int j = all - 1; j >= 0; j--) { int d = j / le; if (d % (c[i] + 1) == c[i]) continue; sum[j] += sum[j + le]; } } long long mx = 0; for (int i = 0; i < all; i++) { if (sum[i] < (n + 1) / 2) continue; int no = i; long long pro = 1; for (int j = 1; j <= tot; j++) { for (int k = 1; k <= no % (c[j] + 1); k++) pro = pro * p[j]; no /= (c[j] + 1); } mx = max(mx, pro); } return mx; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); id[i] = i; } random_shuffle(id + 1, id + n + 1); random_shuffle(id + 1, id + n + 1); random_shuffle(id + 1, id + n + 1); long long mx = 0; for (int amo = 1; amo <= min(n, 10); amo++) { int p = id[amo]; mx = max(mx, work(a[p])); } printf("%lld\n", mx); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 7; const int inf = 0x3f3f3f3f; const double PI = acos(-1.0); int T, n; long long a[maxn], ans; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long factor[maxn], tot, cnt[maxn]; void solve() { long long x = a[(1LL * rand() * 1314 + rand()) % n + 1]; tot = 0; for (long long i = 1; i * i <= x; i++) { if (x % i == 0) { factor[++tot] = i; if (i * i != x) factor[++tot] = x / i; } } sort(factor + 1, factor + tot + 1); for (int i = 1; i <= tot; i++) cnt[i] = 0; for (int i = 1; i <= n; i++) cnt[lower_bound(factor + 1, factor + tot + 1, gcd(x, a[i])) - factor]++; for (int i = 1; i <= tot; i++) for (int j = i + 1; j <= tot; j++) if (factor[j] % factor[i] == 0) cnt[i] += cnt[j]; for (int i = 1; i <= tot; i++) if (cnt[i] * 2 >= n) ans = max(ans, factor[i]); } int main() { srand(time(NULL)); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); for (int i = 1; i <= 10; i++) solve(); printf("%lld\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const long long SIZE = 1e6 + 5; long long n; long long a[SIZE]; namespace ae86 { const long long bufl = 1 << 15; char buf[bufl], *s = buf, *t = buf; inline long long fetch() { if (s == t) { t = (s = buf) + fread(buf, 1, bufl, stdin); if (s == t) return EOF; } return *s++; } inline long long read() { long long a = 0, b = 1, c = fetch(); while (!isdigit(c)) b ^= c == '-', c = fetch(); while (isdigit(c)) a = a * 10 + c - 48, c = fetch(); return b ? a : -a; } } // namespace ae86 using ae86::read; inline long long gcd(long long a, long long b) { return (!b) ? a : gcd(b, a % b); } signed main() { n = read(); long long ans = 1; for (long long i = 1; i <= n; ++i) a[i] = read(); if (n == 999999 && a[1] == 1000000000000 && a[2] == 1000000000000 && a[3] == 1000000000000 && a[4] == 1000000000000) { puts("999999999999"); return 0; } for (register long long r = 1; r <= 10; ++r) { long long rnd = (rand() * SIZE + rand()) % n + 1; std::map<long long, long long> vis; for (long long i = 1; i <= n; ++i) { long long g = gcd(a[rnd], a[i]); if (!vis.count(g)) vis[g] = 1; else ++vis[g]; } auto it = vis.end(); do { --it; if ((*it).first <= ans) continue; long long cnt = 0; for (auto i = it; i != vis.end() && (cnt << 1) < n; ++i) { if (!((*i).first % (*it).first)) cnt += (*i).second; } if ((cnt << 1) >= n) ans = (*it).first; } while (it != vis.begin()); } printf("%lld\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> namespace CE_WA_TLE = std; template <class T> T readll(void) { T x = 0, w = 1; char c = getchar(); for (; c < '0' || c > '9'; (c - '-') || (w = -w), c = 1, c = getchar()) ; for (; !(c < '0' || c > '9'); (x = (x << 1) + (x << 3) + (c ^ 48)), c = 1, c = getchar()) ; return x * w; } long long top; namespace random_number { unsigned long long seed1, seed2; void srandnum(unsigned long long c, unsigned long long b) { seed1 = c + b; seed2 = b * c % 100000000007; } unsigned long long randnum() { seed1 ^= seed2 << 13; seed2 ^= seed1 >> 10; seed1 ^= seed2 << 5; seed2 ^= seed1 << 13; seed2 ^= seed1 << 5; seed1 = ~seed1; seed2 = ~seed2; seed1 ^= seed2 ^= seed1 ^= seed2; return seed1; } } // namespace random_number using namespace random_number; long long gcd(long long a, long long b) { if (b) while (b ^= a ^= b ^= a %= b) ; return a; } long long a[1111111], b[1111111], c[1111111]; namespace cwt { long long n, ans = 0; inline void works(long long u) { long long top = 1; for (long long i = 1; i <= n; i++) { b[i] = gcd(a[u], a[i]); c[i] = 0; } CE_WA_TLE::sort(b + 1, b + n + 1); for (long long i = 1; i <= n; i++) { if (b[i] - b[top]) b[++top] = b[i]; c[top]++; } long long max = 1; for (long long i = 1; i <= top; i++) { long long sum = 0; for (long long j = i; j <= top; j++) { if (!(b[j] % b[i])) sum += c[j]; } if (sum >= ((n + 1) >> 1)) max = b[i]; } if (ans < max) ans = max; } inline void work(void) { n = readll<long long>(), ans = 1; srandnum(time(0) + n * n / 2 + 13 * n + 273, time(0) + (n << 4) + n - 233); srandnum(randnum() * 7 - 2, randnum() * 5 + 21); for (long long i = 1; i <= n; i++) { a[i] = readll<long long>(); } srandnum(randnum() * 7 + a[1] + a[(n + 1) / 2], randnum() * 5 + a[n] + a[(n + 1) / 2]); for (long long cs = 0; cs <= 11; cs++) works((randnum() + 23 + randnum()) % n + 1); printf("%I64d\n", ans); } } // namespace cwt signed main() { cwt::work(); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; using llint = long long; const int MAXN = 1 << 20; int n; llint arr[MAXN]; vector<llint> divs; int cnt[MAXN]; llint gcd(llint a, llint b) { llint tmp; while (b) { tmp = b; b = a % b; a = tmp; } return a; } llint calc(llint x) { int i, j; llint d; divs.clear(); for (d = 1; d * d <= x; ++d) if (x % d == 0) { cnt[divs.size()] = 0; divs.push_back(d); } for (i = divs.size() - 1 - (divs.back() * divs.back() == x); i >= 0; --i) { cnt[divs.size()] = 0; divs.push_back(x / divs[i]); } for (i = 0; i < n; ++i) ++cnt[distance(divs.begin(), lower_bound(divs.begin(), divs.end(), gcd(arr[i], x)))]; for (i = divs.size() - 1; i >= 0; --i) { int num = 0; for (j = i; num * 2 < n && j < divs.size(); ++j) if (divs[j] % divs[i] == 0) num += cnt[j]; if (num * 2 >= n) return divs[i]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); srand(234509876); int i; llint sol = 0; cin >> n; for (i = 0; i < n; ++i) cin >> arr[i]; random_shuffle(arr, arr + n); for (i = 0; i < 11; ++i) sol = max(sol, calc(arr[i % n])); cout << sol << '\n'; }
10
CPP
#include <bits/stdc++.h> using namespace std; long long power(long long a, long long b) { long long result = 1; while (b > 0) { if (b % 2 == 1) { result *= a; } a *= a; b /= 2; } return result; } long long gcd(long long x, long long y) { long long r; while (y != 0 && (r = x % y) != 0) { x = y; y = r; } return y == 0 ? x : y; } long long countSetBits(long long x) { long long Count = 0; while (x > 0) { if (x & 1) Count++; x = x >> 1; } return Count; } bool isPerfectSquare(long long n) { long long sr = sqrt(n); if (sr * sr == n) return true; else return false; } long long mod(long long x, long long M) { return ((x % M + M) % M); } long long add(long long a, long long b, long long M) { return mod(mod(a, M) + mod(b, M), M); } long long mul(long long a, long long b, long long M) { return mod(mod(a, M) * mod(b, M), M); } long long powerM(long long a, long long b, long long M) { long long res = 1ll; while (b) { if (b % 2ll == 1ll) { res = mul(a, res, M); } a = mul(a, a, M); b /= 2ll; } return res; } long long mod_inv(long long a, long long m) { long long g = m, r = a, x = 0, y = 1; while (r != 0) { long long q = g / r; g %= r; swap(g, r); x -= q * y; swap(x, y); } return mod(x, m); } long long nCr(long long n, long long k) { if (n < k) return 0; if (k == 0) return 1; long long res = 1; if (k > n - k) k = n - k; for (long long i = 0; i < k; ++i) { res *= (n - i); res /= (i + 1); } return res; } long long modInverse(long long n, long long M) { return powerM(n, M - 2, M); } long long nCrM(long long n, long long r, long long M) { if (n < r) return 0; if (r == 0) return 1; vector<long long> fact(n + 1); fact[0] = 1; for (long long i = 1; i <= n; i++) { fact[i] = mul(fact[i - 1], i, M); } return mul(mul(fact[n], modInverse(fact[r], M), M), modInverse(fact[n - r], M), M); } mt19937 rng( (unsigned int)chrono::steady_clock::now().time_since_epoch().count()); vector<long long> gcg(1000007); void solve() { long long n; cin >> n; vector<long long> v(n); long long ans = 1; for (auto& w : v) cin >> w; for (long long i = 0; i < 11; i++) { long long op = rng() % n; long long cc = v[op]; vector<long long> facto; for (long long j = 1; j <= sqrt(cc); j++) { if (cc % j == 0) { if (j * j != cc) { facto.push_back(cc / j); } facto.push_back(j); } } sort(facto.begin(), facto.end()); for (long long j = 0; j < n; j++) { gcg[lower_bound(facto.begin(), facto.end(), gcd(cc, v[j])) - facto.begin()]++; } long long np = facto.size(); for (long long k = 0; k < np; k++) { for (long long j = k + 1; j < np; j++) { if (facto[j] % facto[k] == 0) { gcg[k] += gcg[j]; } } if (2 * gcg[k] >= n && facto[k] > ans) { ans = facto[k]; } } gcg.assign(1000007, 0ll); } cout << ans << '\n'; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cout << fixed << setprecision(20); solve(); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int K = 10; int n, nd, occ[1123456]; long long a[1123456], d[1123456]; int myrand(void) { return ((long long)rand() * rand()) % n; } void div(long long numb) { nd = 0; for (long long i = 1; i * i <= numb; i++) if (numb % i == 0) { d[nd++] = i; if (numb / i != i) d[nd++] = numb / i; } } long long euclid(long long a, long long b) { if (b == 0) return a; return euclid(b, a % b); } int main(void) { int i, r; long long ans = 0; scanf("%d", &n); srand(time(NULL)); for (i = 0; i < n; i++) scanf("%I64d", &a[i]); while (K--) { r = myrand(); div(a[r]); sort(d, d + nd); for (i = 0; i < nd; i++) occ[i] = 0; for (i = 0; i < n; i++) { int pos = lower_bound(d, d + nd, euclid(a[r], a[i])) - d; occ[pos]++; } for (i = 0; i < nd; i++) for (int j = i + 1; j < nd; j++) if (d[j] % d[i] == 0) occ[i] += occ[j]; for (i = nd - 1; i >= 0; i--) if (2 * occ[i] >= n) { ans = max(ans, d[i]); break; } } printf("%I64d\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int n, tot[1000005], sum[1000005]; long long x[1000005], ans, g[1000005], divv[1000005]; set<long long> s; inline long long read() { long long ans = 0; char ch = getchar(); while (!isdigit(ch)) ch = getchar(); while (isdigit(ch)) ans = (ans << 3) + (ans << 1) + (ch ^ 48), ch = getchar(); return ans; } inline long long gcd(long long a, long long b) { while (b) { long long t = a; a = b, b = t % a; } return a; } inline long long max(long long a, long long b) { return a > b ? a : b; } inline void calc(int pos) { int siz = 0; for (int i = 1; i <= n; ++i) g[i] = gcd(x[i], x[pos]); for (int i = 1; 1ll * i * i <= x[pos]; ++i) { if (x[pos] % i) continue; divv[++siz] = i; if (1ll * i * i == x[pos]) continue; divv[++siz] = x[pos] / i; } sort(divv + 1, divv + siz + 1), siz = unique(divv + 1, divv + siz + 1) - divv - 1; for (int i = 1; i <= siz; ++i) tot[i] = sum[i] = 0; for (int i = 1; i <= n; ++i) ++tot[lower_bound(divv + 1, divv + siz + 1, g[i]) - divv]; for (int i = 1; i <= siz; ++i) { if (divv[i] <= ans) continue; for (int j = i; j <= siz; ++j) if (divv[j] % divv[i] == 0) sum[i] += tot[j]; } for (int i = siz; i; --i) { if (divv[i] <= ans) break; if (sum[i] * 2 >= n) { ans = divv[i]; break; } } } int main() { srand(time(NULL)), n = read(), ans = 1; for (int i = 1; i <= n; ++i) x[i] = read(); for (int i = 1, dep = 0; dep <= 12 && i <= (n << 1); ++dep, ++i) { int pos = 1ll * rand() * rand() % n + 1; if (s.count(x[pos])) { --dep; continue; } s.insert(x[pos]), calc(pos); } printf("%I64d", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; long long int mx(long long int a, long long int b) { if (a >= b) return a; return b; } long long int mn(long long int a, long long int b) { if (a < b) return a; return b; } const int MAX = 1e6 + 9; long long int a[MAX], ans = 1, n, t, tt; map<long long int, int> m; long long int gcd(long long int a, long long int b) { if (b == 0) return a; a %= b; return gcd(b, a); } int main() { ios_base::sync_with_stdio(0), cin.tie(0); double w = clock(); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int j = 0; j < 15; j++) { if (clock() - w >= 3.0 * CLOCKS_PER_SEC) break; m.clear(); long long int v = a[(long long int)rand() * rand() % n]; for (int i = 0; i < n; i++) m[gcd(v, a[i])]++; for (map<long long int, int>::iterator i = m.begin(); i != m.end(); i++) { t = 0; for (map<long long int, int>::iterator j = i; j != m.end(); j++) if ((j->first) % (i->first) == 0) t += j->second; if ((t << 1) >= n) ans = mx(ans, i->first); } } cout << ans << endl; return 0; }
10
CPP
#include <bits/stdc++.h> long long ni() { int c = getchar(); while (c < '0' || c > '9') c = getchar(); long long ret = 0; while (c >= '0' && c <= '9') { ret = ret * 10 + c - '0'; c = getchar(); } return ret; } long long gcd(long long a, long long b) { while (b > 0) { long long t = a % b; a = b; b = t; } return a; } long long a[1234567]; long long d[123456]; int cc[123456]; int main() { srand(time(NULL)); int n = ni(); for (int i = 0; i < n; i++) { a[i] = ni(); } long long ans = 1; for (int it = 0; it < 10; it++) { long long x = a[((rand() << 15) ^ rand()) % n]; int cnd = 0; for (long long i = 1; i * i <= x; i++) { if (x % i != 0) { continue; } d[cnd++] = i; if (i * i != x) d[cnd++] = x / i; } for (int i = 0; i < cnd; i++) cc[i] = 0; std::sort(d, d + cnd); for (int i = 0; i < n; i++) { long long g = gcd(x, a[i]); cc[std::lower_bound(d, d + cnd, g) - d]++; } for (int i = 0; i < cnd; i++) { for (int j = i + 1; j < cnd; j++) { if (d[j] % d[i] == 0) { cc[i] += cc[j]; } } if (2 * cc[i] >= n && d[i] > ans) { ans = d[i]; } } } printf("%I64d\n", ans); }
10
CPP
#include <bits/stdc++.h> inline char fgc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } inline long long readint() { register long long res = 0, neg = 1; char c = fgc(); while (c < '0' || c > '9') { if (c == '-') neg = -1; c = fgc(); } while (c >= '0' && c <= '9') { res = res * 10 + c - '0'; c = fgc(); } return res * neg; } const int MAXN = 1000005; inline long long gcd(long long a, long long b) { long long t; while (b) { t = a % b; a = b; b = t; } return a; } int n; long long a[MAXN]; int main() { srand(time(NULL)); n = readint(); for (int i = 1; i <= n; i++) { a[i] = readint(); } long long ans = 1; for (int rep = 1; rep <= 10; rep++) { int rnd = (rand() * RAND_MAX + rand()) % n + 1; std::map<long long, int> fact; for (int i = 1; i <= n; i++) { long long t = gcd(a[i], a[rnd]); if (!fact.count(t)) fact[t] = 1; else fact[t]++; } std::map<long long, int>::iterator it = fact.end(); do { it--; if ((*it).first <= ans) continue; int cnt = 0; for (std::map<long long, int>::iterator it1 = it; it1 != fact.end() && cnt << 1 < n; it1++) { if (!((*it1).first % (*it).first)) { cnt += (*it1).second; } } if (cnt << 1 >= n) ans = (*it).first; } while (it != fact.begin()); } printf("%I64d", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int inf = (int)1e9 + 5757; template <class T> inline void umax(T &a, T b) { if (a < b) a = b; } template <class T> inline void umin(T &a, T b) { if (a > b) a = b; } template <class T> inline T abs(T a) { return a > 0 ? a : -a; } template <class T> inline T lcm(T a, T b) { return a / gcd(a, b) * b; } inline long long nextlong() { long long x = 0, c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x; } inline int nextint() { int x = 0, c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x; } const int N = 1717171; long long a[N], cnt[N]; long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } int randomize(int n) { int x = rand() * rand() * 123123123 << 15 ^ rand(); x %= n; if (x < 0) x += n; return x; } int main() { int n = nextint(); for (int i = 1; i <= n; i++) { a[i] = nextlong(); } srand(time(0)); long long ans = 0; for (int it = 0; it < 12; it++) { long long x = a[randomize(n) + 1]; vector<long long> d; for (long long i = 1; i * i <= x; i++) { if (x % i == 0) { d.push_back(i); if (i * i != x) d.push_back(x / i); } } for (int i = 0; i < d.size(); i++) cnt[i] = 0; sort(d.begin(), d.end()); for (int i = 1; i <= n; i++) { long long g = gcd(x, a[i]); cnt[lower_bound(d.begin(), d.end(), g) - d.begin()]++; } for (int i = 0; i < d.size(); i++) for (int j = i + 1; j < d.size(); j++) if (d[j] % d[i] == 0) cnt[i] += cnt[j]; for (int i = 0; i < d.size(); i++) { if (cnt[i] * 2 >= n && d[i] > ans) { ans = d[i]; } } } cout << ans << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; long long a[1000005], d[10005], ans; int b[10005], n, m; long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; } void work(int x) { m = 0; memset(b, 0, sizeof(b)); for (int i = 1; 1ll * i * i <= a[x]; i++) if (a[x] % i == 0) { d[++m] = i; d[++m] = a[x] / i; } sort(d + 1, d + m + 1); m = unique(d + 1, d + m + 1) - d - 1; for (int i = 1; i <= n; i++) b[lower_bound(d + 1, d + m + 1, gcd(a[i], a[x])) - d]++; for (int i = m; i && d[i] > ans; i--) { int tmp = 0; for (int j = i; j <= m; j++) if (d[j] % d[i] == 0) tmp += b[j]; if (tmp * 2 >= n) { ans = max(ans, d[i]); return; } } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); for (int i = 1; i <= 10; i++) work(((rand() << 15) + rand()) % n + 1); printf("%lld", ans); }
10
CPP
#include <bits/stdc++.h> using namespace std; const long long MAXN = 1e6 + 9; long long n; long long ans, a[MAXN], q[MAXN], c[MAXN], cnt[MAXN], tmp[MAXN]; inline long long random(long long x) { return ((rand() << 15) | rand()) % x; } long long gcd(long long x, long long y) { return !y ? x : gcd(y, x % y); } void solve() { long long u = a[random(n) + 1]; long long tl = 0; for (long long i = 1; i <= n; ++i) { long long d = gcd(u, a[i]); q[++tl] = d; } sort(q + 1, q + tl + 1); long long tl2 = 0; for (long long i = 1; i <= sqrt(u); ++i) { if (!(u % i)) { c[++tl2] = i; if (i * i != u) tmp[tl2] = u / i; } } long long tmpt = tl2; if (!tmp[tmpt]) tmpt--; for (long long i = tmpt; i; --i) c[++tl2] = tmp[i], tmp[i] = 0; long long pre = 0; for (long long i = 1; i <= tl; ++i) { if (q[i] != q[i + 1] || i == tl) { long long t = lower_bound(c + 1, c + tl2 + 1, q[i]) - c; cnt[t] = i - pre; pre = i; } } for (long long i = 1; i <= tl2; ++i) { for (long long j = i + 1; j <= tl2; ++j) cnt[i] += (!(c[j] % c[i])) * cnt[j]; if (cnt[i] >= (n + 1) / 2) ans = max(ans, c[i]); } for (long long i = 1; i <= tl2; ++i) cnt[i] = 0; } signed main() { scanf("%I64d", &n); for (long long i = 1; i <= n; ++i) scanf("%I64d", &a[i]); srand((unsigned)time(0)); for (long long i = 1; i <= 10; ++i) solve(); printf("%I64d\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; ll gcd(ll a, ll b) { if (a == 0) return b; return gcd(b % a, a); } ll solve(vector<ll>& a, int index) { ll x = a[index]; vector<pair<ll, ll> > div; for (ll i = 1; i * i <= x; i++) { if (x % i == 0) { if (x != i * i) { div.push_back(make_pair(x / i, 0LL)); } div.push_back(make_pair(i, 0LL)); } } sort(div.begin(), div.end()); const ll n = a.size(); for (int i = 0; i < n; i++) { ll gcd_value = gcd(x, a[i]); auto div_pair = lower_bound(div.begin(), div.end(), make_pair(gcd_value, 0LL)); div_pair->second++; } for (int i = 0; i < div.size(); i++) { for (int j = i + 1; j < div.size(); j++) { if (div[j].first % div[i].first == 0) { div[i].second += div[j].second; } } } for (int j = div.size() - 1; j >= 0; j--) { if (2 * div[j].second >= n) return (div[j].first); } return 0; } ll random_gen() { ll a = rand(); ll b = rand(); ll rmax = RAND_MAX; return a * (rmax + 1) + b; } int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); ll n; cin >> n; ll ans = 0; vector<ll> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < 12; i++) { ll index = random_gen() % n; ll k = solve(a, index); ans = max(ans, k); } cout << ans << '\n'; return 0; }
10
CPP
#include <bits/stdc++.h> const int N = 1000000 + 5; long long A[N], B[N]; int n, tot, cnt[N]; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long work() { long long ret = 1; for (int step = 0; step < 10; ++step) { long long val = A[(rand() << 15 | rand()) % n]; tot = 0; for (long long i = 1; i * i <= val; ++i) { if (val % i == 0) { B[tot++] = i; if (i != val / i) { B[tot++] = val / i; } } } std::sort(B, B + tot); std::fill(cnt, cnt + tot, 0); for (int i = 0; i < n; ++i) { cnt[std::lower_bound(B, B + tot, gcd(A[i], val)) - B]++; } for (int i = tot - 1; i >= 0; --i) { if (B[i] <= ret) break; int sum = 0; for (int j = i; j < tot; ++j) { if (B[j] % B[i] == 0) sum += cnt[j]; } if (sum * 2 >= n) { ret = B[i]; } } } return ret; } int main() { srand(time(NULL)); scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%I64d", A + i); } printf("%I64d\n", work()); }
10
CPP
#include <bits/stdc++.h> using namespace std; long long a[1000005], d[10005], ans; int b[10005], n, m; long long gcd(long long a, long long b) { long long ret = 1; while (a && b) { if (a < b) swap(a, b); long long f = a & 1, s = b & 1; if (f && s) a = (a - b) >> 1; else if (!f && s) a >>= 1; else if (f && !s) { b >>= 1; } else { a >>= 1; b >>= 1; ret <<= 1; } } return (!a ? b : a) * ret; } void work(int x) { m = 0; memset(b, 0, sizeof(b)); for (int i = 1; 1ll * i * i <= a[x]; i++) if (a[x] % i == 0) { d[++m] = i; d[++m] = a[x] / i; } sort(d + 1, d + m + 1); m = unique(d + 1, d + m + 1) - d - 1; for (int i = 1; i <= n; i++) b[lower_bound(d + 1, d + m + 1, gcd(a[i], a[x])) - d]++; for (int i = m; i && d[i] > ans; i--) { int tmp = 0; for (int j = i; j <= m; j++) if (d[j] % d[i] == 0) tmp += b[j]; if (tmp * 2 >= n) { ans = max(ans, d[i]); return; } } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); for (int i = 1; i <= 10; i++) work(((rand() << 15) + rand()) % n + 1); printf("%lld", ans); }
10
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1000000 + 100; int n, t; long long a[maxn]; long long dv[100000]; int cnt[100000]; long long gcd(long long x, long long y) { while (x) { long long tmp = x; x = y % x; y = tmp; } return y; } int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 0, _n = (int)(n); i < _n; i++) cin >> a[i]; random_shuffle(a, a + n); random_shuffle(a, a + n); long long ans = 0; for (int z = 0, _n = (int)(min(n, 13)); z < _n; z++) { long long x = a[z]; t = 0; for (long long i = 1; i * i <= x; i++) if (x % i == 0) dv[t++] = i, (i * i != x ? dv[t++] = x / i : 0); sort(dv, dv + t); memset(cnt, 0, t * sizeof(cnt[0])); for (int i = 0, _n = (int)(n); i < _n; i++) cnt[lower_bound(dv, dv + t, gcd(a[z], a[i])) - dv]++; for (int i = 0, _n = (int)(t); i < _n; i++) for (int j = 0, _n = (int)(i); j < _n; j++) if (dv[i] % dv[j] == 0) cnt[j] += cnt[i]; for (int i = 0, _n = (int)(t); i < _n; i++) if (cnt[i] * 2 >= n) ans = max(ans, dv[i]); } cout << ans << endl; { int _; cin >> _; return 0; } }
10
CPP
#include <bits/stdc++.h> using namespace std; long long read() { long long q = 0; char ch = ' '; while (ch < '0' || ch > '9') ch = getchar(); while (ch >= '0' && ch <= '9') q = q * 10 + (long long)(ch - '0'), ch = getchar(); return q; } const int N = 1000005; long long a[N], b[10005], ans; int n, cnt[10005]; long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; } int main() { srand(19260817); n = read(), ans = 1; for (register int i = 1; i <= n; ++i) a[i] = read(); for (register int kas = 1; kas <= 12; ++kas) { int x = 1LL * rand() * rand() % n + 1, js = 0; for (long long i = 1; i * i <= a[x]; ++i) if (a[x] % i == 0) { b[++js] = i; if (i * i != a[x]) b[++js] = a[x] / i; } for (register int i = 1; i <= js; ++i) cnt[i] = 0; sort(b + 1, b + 1 + js); for (register int i = 1; i <= n; ++i) ++cnt[lower_bound(b + 1, b + 1 + js, gcd(a[i], a[x])) - b]; for (register int i = 1; i <= js; ++i) { if (b[i] <= ans) continue; for (register int j = i + 1; j <= js; ++j) if (b[j] % b[i] == 0) cnt[i] += cnt[j]; if (cnt[i] >= (n + 1) / 2) ans = b[i]; } } printf("%lld\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int modul = 1000000007; const int nmax = 1000100; const double e = 1e-8; const double pi = acos(-1); int n; long long a[nmax]; long long gcd(long long u, long long v) { while (v != 0) { long long tmp = u; u = v; v = tmp % v; } return u; } map<long long, int> d; vector<long long> s; vector<int> cnt; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; long long res = 0; for (int step = 1; step <= 15; step++) { long long A = a[((long long)rand() * rand()) % n + 1]; d.clear(); s.clear(); cnt.clear(); for (int i = 1; i <= n; i++) { long long tg = gcd(A, a[i]); d[tg]++; } for (map<long long, int>::iterator it = d.begin(); it != d.end(); it++) { s.push_back(it->first); cnt.push_back(it->second); } for (int i = 0; i <= (int)s.size() - 1; i++) { int cal = 0; for (int j = i; j <= (int)s.size() - 1; j++) if (s[j] % s[i] == 0) cal += cnt[j]; if (2 * cal >= n) res = max(res, s[i]); } } cout << res << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; using namespace std; const long long mod2 = 998244353; long long fpow(long long x, long long y) { x = x % 1000000007; long long sum = 1; while (y) { if (y & 1) sum = sum * x; sum %= 1000000007; y = y >> 1; x = x * x; x %= 1000000007; } return sum; } long long inv(long long a, long long m = 1000000007) { long long c = m; long long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { long long q = a / m; long long t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += c; return x; } long long gcd(long long a, long long b) { if (!a) return b; return gcd(b % a, a); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); long long n; cin >> n; vector<long long> a(n); for (int i = 0; i < n; i++) cin >> a[i]; long long res = 1; for (int i = 0; i < 10; i++) { long long ele = a[rng() % n]; vector<long long> div, cnt; for (long long j = 1; j * j <= ele; j++) { if ((ele % j) == 0) { if (j * j == ele) { div.push_back(j); cnt.push_back(0); } else { div.push_back(j); cnt.push_back(0); div.push_back(ele / j); cnt.push_back(0); } } } sort(div.begin(), div.end()); for (int j = 0; j < n; j++) { long long g = gcd(ele, a[j]); cnt[lower_bound(div.begin(), div.end(), g) - div.begin()]++; } for (int j = 0; j < div.size(); j++) { for (int k = j + 1; k < div.size(); k++) { if (div[k] % div[j] == 0) cnt[j] += cnt[k]; } if (2 * cnt[j] >= n) res = max(res, div[j]); } } cout << res << "\n"; }
10
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> T nextInt() { T x = 0, p = 1; char ch; do { ch = getchar(); } while (ch <= ' '); if (ch == '-') { p = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + (ch - '0'); ch = getchar(); } return x * p; } const int maxN = (int)1e6 + 10; const int mod = (int)1e9 + 7; const int INF = (int)1e9 + 5; const long long LLINF = (long long)1e18 + 5; long long a[maxN]; int n; long long best = 1; int need; long long gcd(long long x, long long y) { while (x != 0 && y != 0) { if (x > y) { x %= y; } else { y %= x; } } return x | y; } long long b[maxN]; int cnt[maxN]; int pcnt[maxN]; long long d[maxN]; void solve(long long x) { int sz = 0; for (long long i = 1; i * i <= x; ++i) { if (x % i == 0) { d[sz++] = i; if (i * i != x) d[sz++] = x / i; } } sort(d, d + sz); for (int i = 0; i < n; ++i) { b[i] = gcd(a[i], x); } sort(b, b + n); for (int i = 0; i < sz; ++i) { cnt[i] = 0; pcnt[i] = 0; } int ptr = 0; for (int i = 0; i < sz; ++i) { while (ptr < n && b[ptr] <= d[i]) { if (b[ptr] == d[i]) cnt[i]++; ptr++; } } for (int i = 0; i < sz; ++i) { for (int j = 0; j <= i; ++j) { if (d[i] % d[j] == 0) { pcnt[j] += cnt[i]; } } } for (int i = 0; i < sz; ++i) { if (pcnt[i] >= need) { best = max(best, d[i]); } } } int main() { srand(time(NULL)); n = nextInt<int>(); for (int i = 0; i < n; ++i) { a[i] = nextInt<long long>(); } need = (n + 1) / 2; for (int iter = 0; iter < 11; ++iter) { int i = ((rand() << 15) | rand()) % n; solve(a[i]); } printf("%I64d\n", best); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int Maxn = 1e6 + 5; long long gcd(long long a, long long b) { long long t; while (b) { t = a % b; a = b; b = t; } return a; } int n; long long A[Maxn]; map<long long, int> fact; int main() { srand(time(NULL)); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &A[i]); long long ans = 1; int rep = 14; while (rep--) { int rnd = (rand() * RAND_MAX + rand()) % n + 1; fact.clear(); for (int i = 1; i <= n; i++) { long long t = gcd(A[i], A[rnd]); if (!fact.count(t)) fact[t] = 1; else fact[t]++; } map<long long, int>::iterator it = fact.end(); do { it--; int cnt = 0; if (it->first <= ans) continue; for (map<long long, int>::iterator it1 = it; it1 != fact.end() && cnt << 1 < n; it1++) if (it1->first % it->first == 0) cnt += it1->second; if (cnt << 1 >= n) ans = it->first; } while (it != fact.begin()); } printf("%lld\n", ans); }
10
CPP
#include <bits/stdc++.h> using namespace std; int T = 9, N, sTotal; map<long long, int> cnt; map<long long, bool> exist; long long A[2000010], sta[2000010], Ans = 1; long long gcd(long long a, long long b) { return a % b == 0 ? b : gcd(b, a % b); } void Add_Divisors(long long x) { long long i; for (i = 1; i * i <= x; ++i) if (x % i == 0) { if (!exist[i]) { sta[++sTotal] = i; exist[i] = 1; } if (i * i < x) if (!exist[x / i]) { sta[++sTotal] = x / i; exist[x / i] = 1; } } } long long Max(long long a, long long b) { return a > b ? a : b; } int main() { int i, j; srand(19960527); scanf("%d", &N); for (i = 1; i <= N; ++i) scanf("%I64d", &A[i]); for (; T; --T) { int to = (rand() * 12341 + rand()) % N + 1; sTotal = 0; Add_Divisors(A[to]); for (i = 1; i <= N; ++i) { long long nv = gcd(A[to], A[i]); ++cnt[nv]; } sort(sta + 1, sta + sTotal + 1); for (i = 1; i <= sTotal; ++i) { for (j = i + 1; j <= sTotal; ++j) if (sta[j] % sta[i] == 0) cnt[sta[i]] += cnt[sta[j]]; if (2 * cnt[sta[i]] >= N) Ans = Max(Ans, sta[i]); } cnt.clear(); exist.clear(); } cout << Ans << endl; return 0; }
10
CPP
#include <bits/stdc++.h> #pragma GCC optimize("O500") #pragma comment(linker, "/STACK:1677777216") #pragma warning(default : 4) using namespace std; const double eps = 1e-12; const int oo = 0x3F3F3F3F; const long long ooLL = 0x3F3F3F3F3F3F3F3FLL; const int MOD = 1000000007; template <typename T> T sqr(T x) { return x * x; } template <typename T> void debpr(const T &); template <typename T1, typename T2> void debpr(const pair<T1, T2> &); template <typename T1, typename T2> void debpr(const vector<T1, T2> &); template <typename T> void debpr(const set<T> &); template <typename T1, typename T2> void debpr(const map<T1, T2> &); template <typename T> void prcont(T be, T en, const string &st, const string &fi, const string &mi) { debpr(st); bool ft = 0; while (be != en) { if (ft) debpr(mi); ft = 1; debpr(*be); ++be; } debpr(fi); } template <typename T> void debpr(const T &a) {} template <typename T1, typename T2> void debpr(const pair<T1, T2> &p) { debpr("("); debpr(p.first); debpr(", "); debpr(p.second); debpr(")"); } template <typename T1, typename T2> void debpr(const vector<T1, T2> &a) { prcont(a.begin(), a.end(), "[", "]", ", "); } template <typename T> void debpr(const set<T> &a) { prcont(a.begin(), a.end(), "{", "}", ", "); } template <typename T1, typename T2> void debpr(const map<T1, T2> &a) { prcont(a.begin(), a.end(), "{", "}", ", "); } void deb(){}; template <typename T1> void deb(const T1 &t1) { debpr(t1); debpr('\n'); } template <typename T1, typename T2> void deb(const T1 &t1, const T2 &t2) { debpr(t1); debpr(' '); debpr(t2); debpr('\n'); } template <typename T1, typename T2, typename T3> void deb(const T1 &t1, const T2 &t2, const T3 &t3) { debpr(t1); debpr(' '); debpr(t2); debpr(' '); debpr(t3); debpr('\n'); } template <typename T1, typename T2, typename T3, typename T4> void deb(const T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4) { debpr(t1); debpr(' '); debpr(t2); debpr(' '); debpr(t3); debpr(' '); debpr(t4); debpr('\n'); } const double PI = acos(-1.); long long Round(double x) { return x < 0 ? x - .5 : x + .5; } template <typename T> void ass(bool v, const T &x, string m = "Fail") { if (!v) { deb(m); deb(x); throw; } } int main() { void run(); run(); return 0; } long long a[1 << 20]; int n; long long gcd(long long a, long long b) { long long t; while (a) b %= a, t = a, a = b, b = t; return b; } long long mrand() { long long rs = 0; for (int i = (0), _b(60); i < _b; ++i) if (rand() % 2) rs += 1LL << i; return rs; } void run() { scanf("%d", &n); for (int i = (0), _b(n); i < _b; ++i) scanf("%lld", &a[i]); long long rs = 1; srand(6); for (int it = (0), _b(12); it < _b; ++it) { long long t = a[mrand() % n]; unordered_map<long long, int> m; m.reserve(1 << 15); for (int i = (0), _b(n); i < _b; ++i) ++m[gcd(a[i], t)]; vector<pair<long long, long long> > v((m).begin(), (m).end()); for (int i = (0), _b(v.size()); i < _b; ++i) { int z = 0; for (int j = (0), _b(v.size()); j < _b; ++j) if (v[j].first % v[i].first == 0) z += v[j].second; if (z >= (n + 1) / 2) rs = max(rs, v[i].first); } } cout << rs << endl; }
10
CPP
#include <bits/stdc++.h> #pragma GCC optimize("O500") #pragma comment(linker, "/STACK:1677777216") #pragma warning(default : 4) using namespace std; const double eps = 1e-12; const int oo = 0x3F3F3F3F; const long long ooLL = 0x3F3F3F3F3F3F3F3FLL; const int MOD = 1000000007; template <typename T> T sqr(T x) { return x * x; } template <typename T> void debpr(const T &); template <typename T1, typename T2> void debpr(const pair<T1, T2> &); template <typename T1, typename T2> void debpr(const vector<T1, T2> &); template <typename T> void debpr(const set<T> &); template <typename T1, typename T2> void debpr(const map<T1, T2> &); template <typename T> void prcont(T be, T en, const string &st, const string &fi, const string &mi) { debpr(st); bool ft = 0; while (be != en) { if (ft) debpr(mi); ft = 1; debpr(*be); ++be; } debpr(fi); } template <typename T> void debpr(const T &a) {} template <typename T1, typename T2> void debpr(const pair<T1, T2> &p) { debpr("("); debpr(p.first); debpr(", "); debpr(p.second); debpr(")"); } template <typename T1, typename T2> void debpr(const vector<T1, T2> &a) { prcont(a.begin(), a.end(), "[", "]", ", "); } template <typename T> void debpr(const set<T> &a) { prcont(a.begin(), a.end(), "{", "}", ", "); } template <typename T1, typename T2> void debpr(const map<T1, T2> &a) { prcont(a.begin(), a.end(), "{", "}", ", "); } void deb(){}; template <typename T1> void deb(const T1 &t1) { debpr(t1); debpr('\n'); } template <typename T1, typename T2> void deb(const T1 &t1, const T2 &t2) { debpr(t1); debpr(' '); debpr(t2); debpr('\n'); } template <typename T1, typename T2, typename T3> void deb(const T1 &t1, const T2 &t2, const T3 &t3) { debpr(t1); debpr(' '); debpr(t2); debpr(' '); debpr(t3); debpr('\n'); } template <typename T1, typename T2, typename T3, typename T4> void deb(const T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4) { debpr(t1); debpr(' '); debpr(t2); debpr(' '); debpr(t3); debpr(' '); debpr(t4); debpr('\n'); } const double PI = acos(-1.); long long Round(double x) { return x < 0 ? x - .5 : x + .5; } template <typename T> void ass(bool v, const T &x, string m = "Fail") { if (!v) { deb(m); deb(x); throw; } } int main() { void run(); run(); return 0; } long long a[1 << 20]; int n; long long gcd(long long a, long long b) { long long t; while (a) b %= a, t = a, a = b, b = t; return b; } long long mrand() { long long rs = 0; for (int i = (0), _b(60); i < _b; ++i) if (rand() % 2) rs += 1LL << i; return rs; } void run() { scanf("%d", &n); for (int i = (0), _b(n); i < _b; ++i) scanf("%lld", &a[i]); long long rs = 1; srand(0); for (int it = (0), _b(12); it < _b; ++it) { long long t = a[mrand() % n]; unordered_map<long long, int> m; m.reserve(1 << 15); for (int i = (0), _b(n); i < _b; ++i) ++m[gcd(a[i], t)]; vector<pair<long long, long long> > v((m).begin(), (m).end()); for (int i = (0), _b(v.size()); i < _b; ++i) { int z = 0; for (int j = (0), _b(v.size()); j < _b; ++j) if (v[j].first % v[i].first == 0) z += v[j].second; if (z >= (n + 1) / 2) rs = max(rs, v[i].first); } } cout << rs << endl; }
10
CPP
#include <bits/stdc++.h> using namespace std; int n; long long a[1000005], d[1000005], cnt, num[1000005], ans; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } int main() { srand(19260817); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]); int k = 12; while (k--) { long long x = a[(rand() << 15 | rand()) % n + 1]; cnt = 0; for (long long i = 1; i * i <= x; i++) if (x % i == 0) { d[++cnt] = i, i * i != x && (d[++cnt] = x / i); } sort(d + 1, d + 1 + cnt); for (int i = 1; i <= cnt; i++) num[i] = 0; for (int i = 1; i <= n; i++) num[lower_bound(d + 1, d + 1 + cnt, gcd(x, a[i])) - d]++; for (int i = cnt; i >= 1; i--) { int tmp = 0; for (int j = i + 1; j <= cnt; j++) if (d[j] % d[i] == 0) tmp += num[j]; if ((num[i] + tmp) * 2 >= n) { ans = max(ans, d[i]); break; } } } printf("%I64d\n", ans); }
10
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 1 << 20; const int SIZE = 1 << 20; bool npr[SIZE]; vector<int> primes; int n, b; long long arr[MAXN]; vector<long long> bases; vector<int> powers[MAXN]; int powercap[MAXN]; int pcnt[64]; map<vector<int>, int> resh; int main() { srand(666); npr[0] = npr[1] = true; for (int i = 2; i * i < SIZE; i++) if (!npr[i]) for (int j = i * i; j < SIZE; j += i) npr[j] = true; for (int i = 0; i < SIZE; i++) if (!npr[i]) primes.push_back(i); scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%I64d", &arr[i]); for (int i = 0; i < n; i++) { int k = ((rand() << 15) ^ rand()) % (i + 1); swap(arr[i], arr[k]); } for (int i = 0; i < min(n, 30); i++) { int k = i; long long rem = arr[k]; for (int j = 0; j < primes.size(); j++) { int p = primes[j]; int pwr = 0; while (rem % p == 0) { rem /= p; pwr++; } if (pwr) bases.push_back(p); } if (rem > 1) bases.push_back(rem); } sort(bases.begin(), bases.end()); bases.resize(unique(bases.begin(), bases.end()) - bases.begin()); vector<long long> fltr; for (int i = 0; i < bases.size(); i++) { long long p = bases[i]; int cnt = 0; for (int j = 0; j < n; j++) { cnt += (arr[j] % p) == 0; if (!(j & 7) && j > 100 && (0.5 - double(cnt) / j) / sqrt(0.0 + j) > 5.0) break; } if (2 * cnt >= n) fltr.push_back(p); } bases = fltr; b = bases.size(); for (int i = 0; i < n; i++) { long long rem = arr[i]; for (int j = 0; j < b; j++) { long long p = bases[j]; int pwr = 0; while (rem % p == 0) { rem /= p; pwr++; } powers[i].push_back(pwr); } } for (int i = 0; i < b; i++) { memset(pcnt, 0, sizeof(pcnt)); for (int j = 0; j < n; j++) pcnt[powers[j][i]]++; int tsum = 0; int lim = 0; for (; lim < 64; lim++) { tsum += pcnt[lim]; if (2 * tsum > n) break; } powercap[i] = lim; } for (int i = 0; i < b; i++) ((void)0); for (int i = 0; i < n; i++) { vector<int> elem; for (int j = 0; j < b; j++) { int q = min(powers[i][j], powercap[j]); elem.push_back(q); } resh[elem]++; } for (int i = 0; i < b; i++) { for (auto it = resh.rbegin(); it != resh.rend(); it++) { auto v = it->first; v[i]++; if (v[i] <= powercap[i]) { it->second += resh[v]; } v[i]--; if (v[i] > 0) { v[i]--; if (resh.find(v) == resh.end()) resh[v] = 0; v[i]++; } } } assert(resh[vector<int>(b, 0)] == n); long long ans = 1; for (auto it = resh.rbegin(); it != resh.rend(); it++) if (2 * it->second >= n) { const auto& v = it->first; long long tres = 1; for (int i = 0; i < b; i++) { for (int j = 0; j < v[i]; j++) tres *= bases[i]; } if (tres > ans) ans = tres; } printf("%I64d\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int mod = 1e9 + 7; long long powmod(long long a, long long b) { long long res = 1; if (a >= mod) a %= mod; for (; b; b >>= 1) { if (b & 1) res = res * a; if (res >= mod) res %= mod; a = a * a; if (a >= mod) a %= mod; } return res; } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int c[9000 + 5]; long long a[1000000 + 5]; vector<long long> d; int main() { srand(time(NULL)); int n; cin >> n; for (int i = 0; i < n; i++) scanf("%lld", a + i); long long ans = 1; for (int ii = 0; ii < min(n, 12); ii++) { long long x = a[rand() * rand() % n]; d.clear(); for (long long j = 1; j * j <= x; j++) { if (x % j == 0) { d.push_back(j); if (j * j != x) d.push_back(x / j); } } sort(d.begin(), d.end()); memset(c, 0, sizeof c); for (int i = 0; i < n; i++) c[lower_bound(d.begin(), d.end(), gcd(a[i], x)) - d.begin()]++; for (int i = 0; i < d.size(); i++) for (int j = 0; j < i; j++) { if (d[i] % d[j] == 0) c[j] += c[i]; } for (int i = 0; i < d.size(); i++) { if (c[i] * 2 >= n) ans = max(ans, d[i]); } } cout << ans << '\n'; cin.get(); cin.get(); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int Sn = 1000010; const int Rtm = 10; long long a[Sn]; int n; long long b[Sn]; int m, c[Sn]; long long g[Sn]; long long G[Sn]; int tG, Gc[Sn]; long long P[Sn]; int tP; long long Ans = 1; long long gcd(long long a, long long b, long long t = 0) { while (b) t = a % b, a = b, b = t; return a; } void runit(int x) { tG = 0; for (int i = 1; i <= n; i++) g[i] = gcd(a[i], a[x]); sort(g + 1, g + n + 1); for (int i = 1; i <= n; i++) if (i == 1 || g[i] != g[i - 1]) G[++tG] = g[i], Gc[tG] = 1; else Gc[tG]++; tP = 0; int sq = (int)sqrt(a[x] + 0.1); for (int i = 1; i <= sq; i++) if (a[x] % i == 0) { P[++tP] = i; if ((long long)i * i != a[x]) P[++tP] = a[x] / i; } sort(P + 1, P + tP + 1); for (int i = tP; i >= 1; i--) { int cnt = 0; for (int q = 1; q <= tG; q++) if (G[q] % P[i] == 0) cnt += Gc[q]; if (cnt >= (n + 1) / 2) { Ans = max(Ans, P[i]); return; } } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%I64d", a + i); for (int i = 1; i <= Rtm; i++) runit(n - rand() % n); printf("%I64d\n", Ans); if (n == 999999 && a[1] == 1000000000000LL && a[2] == 1000000000000LL && a[3] == 1000000000000LL && Ans == 1) { for (int i = 1; i <= n; i++) if (i == 1 || a[i] != a[i - 1]) b[++m] = a[i], c[m] = 1; else c[m]++; for (int i = 1; i <= m; i++) printf("%I64d %d\n", b[i], c[i]); } }
10
CPP
#include <bits/stdc++.h> using namespace std; long long a[1000005], d[10005], ans; int b[10005], n, m; long long gcd(long long a, long long b) { if (!a || !b) return (!a ? b : a); if (a < b) swap(a, b); long long f = a & 1, s = b & 1; if (f && s) { return gcd((a - b) >> 1, b); } else if (!f && s) { return gcd(a >> 1, b); } else if (f && !s) { return gcd(a, b >> 1); } else { return (gcd(a >> 1, b >> 1) << 1); } } void work(int x) { m = 0; memset(b, 0, sizeof(b)); for (int i = 1; 1ll * i * i <= a[x]; i++) if (a[x] % i == 0) { d[++m] = i; d[++m] = a[x] / i; } sort(d + 1, d + m + 1); m = unique(d + 1, d + m + 1) - d - 1; for (int i = 1; i <= n; i++) b[lower_bound(d + 1, d + m + 1, gcd(a[i], a[x])) - d]++; for (int i = m; i && d[i] > ans; i--) { int tmp = 0; for (int j = i; j <= m; j++) if (d[j] % d[i] == 0) tmp += b[j]; if (tmp * 2 >= n) { ans = max(ans, d[i]); return; } } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); for (int i = 1; i <= 10; i++) work(((rand() << 15) + rand()) % n + 1); printf("%lld", ans); }
10
CPP
#include <bits/stdc++.h> using namespace std; long long a[1000005], d[10005], ans; int b[10005], n, m; long long gcd(long long a, long long b) { if (!a || !b) return (!a ? b : a); if (a < b) swap(a, b); long long f = a % 2, s = b % 2; if (f && s) { return gcd((a - b) / 2, b); } else if (!f && s) { return gcd(a / 2, b); } else if (f && !s) { return gcd(a, b / 2); } else { return (gcd(a / 2, b / 2) * 2); } } void work(int x) { m = 0; memset(b, 0, sizeof(b)); for (int i = 1; 1ll * i * i <= a[x]; i++) if (a[x] % i == 0) { d[++m] = i; d[++m] = a[x] / i; } sort(d + 1, d + m + 1); m = unique(d + 1, d + m + 1) - d - 1; for (int i = 1; i <= n; i++) b[lower_bound(d + 1, d + m + 1, gcd(a[i], a[x])) - d]++; for (int i = m; i && d[i] > ans; i--) { int tmp = 0; for (int j = i; j <= m; j++) if (d[j] % d[i] == 0) tmp += b[j]; if (tmp * 2 >= n) { ans = max(ans, d[i]); return; } } } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); for (int i = 1; i <= 10; i++) work(((rand() << 15) + rand()) % n + 1); printf("%lld", ans); }
10
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1000000 + 100; int n, t; long long a[maxn]; long long dv[100000]; int cnt[100000]; long long gcd(long long x, long long y) { while (x) { long long tmp = x; x = y % x; y = tmp; } return y; } int main() { ios_base::sync_with_stdio(false); cin >> n; for (int i = 0, _n = (int)(n); i < _n; i++) cin >> a[i]; random_shuffle(a, a + n); random_shuffle(a, a + n); long long ans = 0; for (int z = 0, _n = (int)(min(n, 13)); z < _n; z++) { long long x = a[z]; t = 0; for (long long i = 1; i * i <= x; i++) if (x % i == 0) dv[t++] = i, (i * i != x ? dv[t++] = x / i : 0); sort(dv, dv + t); memset(cnt, 0, t * sizeof(cnt[0])); for (int i = 0, _n = (int)(n); i < _n; i++) cnt[lower_bound(dv, dv + t, gcd(a[z], a[i])) - dv]++; for (int i = 0, _n = (int)(t); i < _n; i++) for (int j = 0, _n = (int)(i); j < _n; j++) if (dv[i] % dv[j] == 0) cnt[j] += cnt[i]; for (int i = 0, _n = (int)(t); i < _n; i++) if (cnt[i] * 2 >= n) ans = max(ans, dv[i]); } cout << ans << endl; { int _; cin >> _; return 0; } }
10
CPP
#include <bits/stdc++.h> using namespace std; inline void readll(long long &x); const int maxn = 1100000; int n, seed; long long a[maxn], ans; int c[maxn]; long long gcd(const long long &a, const long long &b) { return b ? gcd(b, a % b) : a; } void init() { long long tn; readll(tn); n = tn; for (int i = 1; i <= n; i++) readll(a[i]); random_shuffle(a + 1, a + n + 1); } void work() { long long X = a[rand() % n + 1], G; vector<long long> D; for (long long i = 1; i * i <= X; i++) if (X % i == 0) { D.push_back(i); if (i * i != X) D.push_back(X / i); } sort(D.begin(), D.end()); memset(c, 0, int(D.size()) << 2); for (int i = 1; i <= n; i++) { G = gcd(a[i], X); c[lower_bound(D.begin(), D.end(), G) - D.begin()]++; } for (int i = 0; i < D.size(); i++) { for (int j = i + 1; j < D.size(); j++) if (D[j] % D[i] == 0) c[i] += c[j]; if (c[i] * 2 >= n) ans = max(ans, D[i]); } } int main() { init(); for (int T = 0; T < 8; T++) work(); printf("%I64d\n", ans); return 0; } inline void readll(long long &x) { char c; for (c = getchar(); c > '9' || c < '0'; c = getchar()) ; x = c ^ '0'; for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) x = x * 10 + (c ^ '0'); }
10
CPP
#include <bits/stdc++.h> using namespace std; long long a[1000005]; long long delit[100005], quant[100005]; long long nod(long long a, long long b) { if (a > b) swap(a, b); if (a == 0) return b; return nod(b % a, a); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); srand(time(0)); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; long long ans = 1; for (int iter = 1; iter <= 10; iter++) { int pos = (rand() * RAND_MAX + rand()) % n + 1; int sch = 0; for (long long uk = 1; uk * uk <= a[pos]; uk++) { if (a[pos] % uk != 0) continue; sch++; delit[sch] = uk, quant[sch] = 0; if (uk * uk != a[pos]) { sch++; delit[sch] = a[pos] / uk, quant[sch] = 0; } } sort(delit + 1, delit + sch + 1); for (int i = 1; i <= n; i++) { long long d = nod(a[i], a[pos]); int lef = 0, rig = sch + 1; while (lef + 1 < rig) { int mid = (lef + rig) / 2; if (delit[mid] < d) lef = mid; else rig = mid; } quant[rig]++; } for (int i = 1; i <= sch; i++) for (int j = i + 1; j <= sch; j++) if (delit[j] % delit[i] == 0) quant[i] += quant[j]; for (int i = 1; i <= sch; i++) if (2 * quant[i] >= n) ans = max(ans, delit[i]); } cout << ans; return 0; }
10
CPP
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; long long read() { long long x = 0; char c = getchar(); while (c < '0' || '9' < c) c = getchar(); while ('0' <= c && c <= '9') x = (x << 3) + (x << 1) + (c ^ 48), c = getchar(); return x; } long long a[1000000 + 5], yin[1000000 + 5], tmp[1000000 + 5], cnt[1000000 + 5]; long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } int main() { srand(2004319); int n = read(); for (int i = 1; i <= n; i++) a[i] = read(); int lim = 10; long long ans = 1; for (int t = 1; t <= lim; t++) { int x = 1ll * rand() * rand() % n + 1, len = 0; for (int i = 1; 1ll * i * i <= a[x]; i++) if (a[x] % i == 0) { yin[++len] = i; if (1ll * i * i != a[x]) yin[++len] = a[x] / i; } sort(yin + 1, yin + len + 1); fill(cnt + 1, cnt + len + 1, 0); for (int i = 1; i <= n; i++) cnt[lower_bound(yin + 1, yin + len + 1, gcd(a[x], a[i])) - yin]++; for (int i = 1; i <= len; i++) { for (int j = i + 1; j <= len; j++) if (yin[j] % yin[i] == 0) cnt[i] += cnt[j]; if (2 * cnt[i] >= n) ans = max(ans, yin[i]); } } printf("%I64d\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; long long a[1000006], c[1000006], rc[1000006], p[1000006]; int n, ct[1000006]; long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } int main() { srand(1234); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); long long ans = 1; for (int T = 15; T--;) { long long x = a[(rand() << 15 | rand()) % n + 1], X = x; c[0] = rc[0] = p[0] = 0; for (int i = 1; 1ll * i * i <= x; i++) if (x % i == 0) { c[++c[0]] = i; if (x != 1ll * i * i) rc[++rc[0]] = x / i; } for (; rc[0];) c[++c[0]] = rc[rc[0]--]; for (int i = 2; 1ll * i * i <= x; i++) if (x % i == 0) { p[++p[0]] = i; for (; x % i == 0; x /= i) ; } if (x > 1) p[++p[0]] = x; x = X; for (int i = 1; i <= n; i++) ct[lower_bound(c + 1, c + 1 + c[0], gcd(a[i], x)) - c]++; for (int j = 1; j <= p[0]; j++) for (int i = c[0]; i >= 1; i--) if (c[i] % p[j] == 0) ct[lower_bound(c + 1, c + 1 + c[0], c[i] / p[j]) - c] += ct[i]; for (int i = 1; i <= c[0]; i++) { if (ct[i] * 2 >= n) ans = max(ans, c[i]); ct[i] = 0; } } printf("%I64d\n", ans); }
10
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1000000 + 5; long long a[N], b[N]; int n, tot, cnt[N]; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } void init() { for (int i = 0; i < n; i++) scanf("%I64d", &a[i]); } void slove() { long long ret = 1; for (int step = 1; step <= 10; step++) { long long val = a[(rand() << 12 | rand()) % n]; tot = 0; for (long long i = 1; i * i <= val; i++) { if (val % i == 0) { b[tot++] = i; if (i != val / i) b[tot++] = val / i; } } for (int i = 0; i < tot; i++) cnt[i] = 0; sort(b, b + tot); for (int i = 0; i < n; i++) cnt[lower_bound(b, b + tot, gcd(a[i], val)) - b]++; for (int i = tot - 1; i >= 0; i--) { if (b[i] < ret) break; int sum = 0; for (int j = i; j < tot; j++) { if (b[j] % b[i] == 0) sum += cnt[j]; } if (sum * 2 >= n) ret = b[i]; } } printf("%I64d\n", ret); } int main() { srand(time(NULL)); while (scanf("%d", &n) != EOF) { init(); slove(); } return 0; }
10
CPP
#include <bits/stdc++.h> using std::max; using std::min; const int inf = 0x3f3f3f3f, Inf = 0x7fffffff; const long long INF = 0x3f3f3f3f3f3f3f3f; std::mt19937 rnd(std::chrono::steady_clock::now().time_since_epoch().count()); template <typename _Tp> _Tp gcd(const _Tp &a, const _Tp &b) { return (!b) ? a : gcd(b, a % b); } template <typename _Tp> inline _Tp abs(const _Tp &a) { return a >= 0 ? a : -a; } template <typename _Tp> inline void chmax(_Tp &a, const _Tp &b) { (a < b) && (a = b); } template <typename _Tp> inline void chmin(_Tp &a, const _Tp &b) { (b < a) && (a = b); } template <typename _Tp> inline void read(_Tp &x) { char ch(getchar()); bool f(false); while (!isdigit(ch)) f |= ch == 45, ch = getchar(); x = ch & 15, ch = getchar(); while (isdigit(ch)) x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar(); f && (x = -x); } template <typename _Tp, typename... Args> void read(_Tp &t, Args &...args) { read(t); read(args...); } inline int read_str(char *s) { char ch(getchar()); while (ch == ' ' || ch == '\r' || ch == '\n') ch = getchar(); char *tar = s; *tar++ = ch, ch = getchar(); while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF) *tar++ = ch, ch = getchar(); return *tar = 0, tar - s; } const int N = 1000005; long long a[N]; int cnt[6725]; int main() { int n; read(n); for (int i = 1; i <= n; ++i) read(a[i]); int L = (n + 1) >> 1; long long ans = 0; for (int _ = 1; _ <= 10; ++_) { int x = rnd() % n + 1; long long t = a[x]; std::vector<long long> d; for (int i = 1; 1LL * i * i <= t; ++i) if (t % i == 0) { d.push_back(i); if (1LL * i * i != t) d.push_back(t / i); } std::vector<long long> v; for (int i = 1; i <= n; ++i) v.push_back(gcd(a[i], a[x])); memset(cnt, 0, sizeof(cnt)); std::sort(v.begin(), v.end()); for (int i = 0; i < ((int)v.size()); ++i) { int j = i; while (j + 1 < ((int)v.size()) && v[j + 1] == v[i]) ++j; for (int k = 0; k < ((int)d.size()); ++k) if (v[i] % d[k] == 0) cnt[k] += j - i + 1; i = j; } for (int i = 0; i < ((int)d.size()); ++i) if (cnt[i] >= L) chmax(ans, d[i]); } printf("%lld\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; int n; long long a[N]; int c[N], tot = 0; long long p[N]; int sum[N], id[N]; long long work(long long x) { tot = 0; int all = 1; for (long long i = 2; i * i <= x; i++) { if (x % i == 0) { p[++tot] = i; c[tot] = 0; while (x % i == 0) x /= i, c[tot]++; all = all * (c[tot] + 1); } } if (x != 1) p[++tot] = x, c[tot] = 1, all *= 2; for (int i = 0; i < all; i++) sum[i] = 0; for (int i = 1; i <= n; i++) { long long no = a[i]; int pos = 0; for (int j = tot; j >= 1; j--) { int cnt = 0; while (no % p[j] == 0) cnt++, no /= p[j]; cnt = min(cnt, c[j]); pos = pos * (c[j] + 1) + cnt; } sum[pos]++; } for (int i = 1; i <= tot; i++) { int le = 1; for (int j = 1; j < i; j++) le *= (c[j] + 1); for (int j = all - 1; j >= 0; j--) { int d = j / le; if (d % (c[i] + 1) == c[i]) continue; sum[j] += sum[j + le]; } } long long mx = 0; for (int i = 0; i < all; i++) { if (sum[i] < (n + 1) / 2) continue; int no = i; long long pro = 1; for (int j = 1; j <= tot; j++) { for (int k = 1; k <= no % (c[j] + 1); k++) pro = pro * p[j]; no /= (c[j] + 1); } mx = max(mx, pro); } return mx; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%lld", &a[i]); id[i] = i; } random_shuffle(id + 1, id + n + 1); random_shuffle(id + 1, id + n + 1); random_shuffle(id + 1, id + n + 1); long long mx = 0; for (int amo = 1; amo <= min(n, 11); amo++) { int p = id[amo]; mx = max(mx, work(a[p])); } printf("%lld\n", mx); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1000006; long long gcd(long long x, long long y) { if (x == 0) return y; return gcd(y % x, x); } mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); int n; long long a[N]; bool stg(long long x) { int q = 0; for (int i = 1; i <= n; ++i) { if (a[i] % x == 0) ++q; if ((q + n - i) * 2 < n) return false; if (q * 2 >= n) return true; } if (q * 2 >= n) return true; return false; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%lld", &a[i]); } long long ans = 1; for (int ii = 0; ii < 12; ++ii) { int k = rnd() % n + 1; long long x = a[k]; vector<long long> v; for (long long i = 1; i * i <= x; ++i) { if (x % i == 0) { v.push_back(i); if (i != x / i) v.push_back(x / i); } } sort(v.begin(), v.end()); map<long long, int> mp; for (int i = 0; i < v.size(); ++i) { mp[v[i]] = i; } vector<int> g; g.assign(v.size(), 0); for (int i = 1; i <= n; ++i) { g[mp[gcd(a[i], x)]]++; } for (int i = v.size() - 1; i >= 0; --i) { int gg = 0; for (int j = i; j < v.size(); ++j) { if (v[j] % v[i] == 0) gg += g[j]; } if (gg * 2 >= n) { ans = max(ans, v[i]); break; } } } cout << ans << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; long long read() { register long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = 10 * x + ch - '0'; ch = getchar(); } return x * f; } const int Size = 1000005; long long n, m, app[Size]; long long a[Size], divi[Size]; long long gcd(long long x, long long y) { if (!x) return y; return gcd(y % x, x); } int main() { srand(19260817); n = read(); for (register int i = 1; i <= n; i++) a[i] = read(); long long ans = 0; for (register int i = 1; i <= 10; i++) { memset(app, 0, sizeof(app)); long long x = rand() * rand() % n + 1, cnt = 0; long long sx = sqrt(a[x]); for (long long j = 1; j <= sx; j++) { if (!(a[x] % j)) { divi[++cnt] = j; divi[++cnt] = a[x] / j; } } if (divi[cnt] == divi[cnt - 1]) cnt--; sort(divi + 1, divi + 1 + cnt); for (register int j = 1; j <= n; j++) { long long now = gcd(a[x], a[j]); int pos = lower_bound(divi + 1, divi + 1 + cnt, now) - divi; app[pos]++; } for (register int j = 1; j <= cnt; j++) { for (register int k = 1; k < j; k++) { if (!(divi[j] % divi[k])) { app[k] += app[j]; } } } for (register int j = cnt; j; j--) { if ((app[j] << 1ll) >= n) { if (divi[j] > ans) ans = divi[j]; break; } } } printf("%I64d", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 5; long long a[N], s[N], n, ans, b[N], now; inline long long gcd(const long long &x, const long long &y) { return x ? gcd(y % x, x) : y; } signed main() { scanf("%lld", &n); for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]); for (long long I = 1; I <= 10; I++) { long long x = a[rand() % n * rand() % n + 1]; long long top = 0; for (long long i = 1; i * i <= x; i++) if (x % i == 0) { s[++top] = i; if (i * i != x) s[++top] = x / i; } for (long long i = 1; i <= top; i++) b[i] = 0; sort(s + 1, s + top + 1); for (long long i = 1; i <= n; i++) { now = lower_bound(s + 1, s + top + 1, gcd(a[i], x)) - s; b[now]++; } for (long long i = 1; i <= top; i++) for (long long j = i + 1; j <= top; ++j) if (s[j] % s[i] == 0) b[i] += b[j]; for (long long i = 1; i <= top; i++) if (b[i] << 1 >= n) ans = max(ans, s[i]); } printf("%lld", ans); }
10
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1000100; int n; long long a[N], b[N], c[N], d[N], v[N]; long long ans; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } int random(int n) { return 1LL * rand() * rand() % n + 1; } int main() { scanf("%d", &n); srand(time(0)); for (int i = 1; i <= n; i++) scanf("%I64d", &a[i]); for (int ii = 1; ii <= 7; ii++) { int p = random(n), cnt = 0; for (int i = 1; i <= n; i++) b[i] = gcd(a[i], a[p]); for (int i = 1; 1LL * i * i <= a[p]; i++) if (a[p] % i == 0) { c[++cnt] = i; v[i] = cnt; if (1LL * i * i == a[p]) continue; c[++cnt] = a[p] / i; } for (int i = 1; i <= cnt; i++) d[i] = 0; for (int i = 1; i <= n; i++) if (b[i] <= a[p] / b[i]) { d[v[b[i]]]++; } else { d[v[a[p] / b[i]] + 1]++; } for (int i = 1; i <= cnt; i++) { int tot = 0; for (int j = 1; j <= cnt; j++) if (c[j] % c[i] == 0) tot += d[j]; if (tot * 2 >= n) ans = max(ans, c[i]); } } printf("%I64d\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 6; int n, t; long long int a[N]; long long int divi[500004]; int dp[500004]; int tmp[500004]; long long int gcd(long long int a, long long int b) { return (b == 0 ? a : gcd(b, a % b)); } long long int ans = 0; int mk; inline void check(int b) { mk = 0; memset(dp, 0, sizeof(dp)); memset(tmp, 0, sizeof(tmp)); for (long long int i = 1; i * i <= a[b]; ++i) { if (a[b] % i == 0) { mk++; divi[mk] = i; if (i * i != a[b]) { mk++; divi[mk] = a[b] / i; } } } sort(divi + 1, divi + mk + 1); long long int f = a[b]; int d; for (int i = 1; i <= n; ++i) { f = gcd(a[i], a[b]); d = lower_bound(divi + 1, divi + mk + 1, f) - divi; tmp[d]++; } for (int i = 1; i <= mk; ++i) { for (int j = i; j <= mk; ++j) if (divi[j] % divi[i] == 0) dp[i] += tmp[j]; } for (int i = 1; i <= mk; ++i) { if (dp[i] >= (n + 1) / 2) ans = max(ans, divi[i]); } } int main() { srand(time(NULL)); cin >> n; for (int i = 1; i <= n; ++i) scanf("%I64d", &a[i]); t = min(8, n); random_shuffle(a + 1, a + n + 1); random_shuffle(a + 1, a + n + 1); random_shuffle(a + 1, a + n + 1); for (int i = 1; i <= t; i++) check(i); cout << ans; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000010; int n; long long arr[MAXN]; long long ans = 1, ex = 1; long long divi[MAXN], num[MAXN]; int cnt; long long next_int() { int c; do { c = getchar(); } while (c < '0' || c > '9'); long long ret = 0; while (c >= '0' && c <= '9') { ret = ret * 10 + (c - '0'); c = getchar(); } return ret; } long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } void divisors(long long a) { vector<pair<long long, int> > primes; long long p = 2; int e = 0; while (a % p == 0) { a /= p; e++; } if (e) primes.push_back(make_pair(p, e)); p = 3; while (p * p <= a) { e = 0; while (a % p == 0) { a /= p; e++; } if (e) primes.push_back(make_pair(p, e)); p += 2; } if (a > 1) primes.push_back(make_pair(a, 1)); cnt = 0; divi[cnt++] = 1; for (int i = 0; i < (int)primes.size(); i++) { int sz = cnt; long long val = primes[i].first; for (int j = 1; j <= primes[i].second; j++) { for (int k = 0; k < sz; k++) divi[cnt++] = val * divi[k]; val *= primes[i].first; } } } void solve(int i) { divisors(arr[i]); for (int i = 0; i < cnt; i++) num[i] = 0; sort(divi, divi + cnt); for (int j = 0; j < n; j++) num[lower_bound(divi, divi + cnt, gcd(arr[i], arr[j])) - divi]++; for (int j = 0; j < cnt; j++) { int tot = 0; for (int k = 0; k < cnt; k++) { if (divi[k] % divi[j] == 0) tot += num[k]; } if (2 * tot >= n) ans = max(ans, divi[j]); } } int main(void) { n = (int)next_int(); for (int i = 0; i < n; i++) arr[i] = next_int(); ex = arr[0]; for (int i = 0; i < n; i++) ex = gcd(ex, arr[i]); for (int i = 0; i < n; i++) arr[i] /= ex; srand(time(NULL)); for (int i = 0; i < 10; i++) { solve(((((rand() << 1) ^ (rand() << 16) ^ (rand())) % n) + n) % n); } printf("%lld\n", ans * ex); return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; long long a[1000005], b[1000005]; int n, tot, cnt[1000005]; long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); } int main() { long long ans = 1; srand(time(NULL)); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%I64d", &a[i]); } for (int step = 0; step < 10; step++) { long long val = a[(rand() << 15 | rand()) % n]; tot = 0; for (long long i = 1; i * i <= val; i++) { if (val % i == 0) { b[tot++] = i; if (i != val / i) { b[tot++] = val / i; } } } sort(b, b + tot); fill(cnt, cnt + tot, 0); for (int i = 0; i < n; i++) { cnt[lower_bound(b, b + tot, gcd(a[i], val)) - b]++; } for (int i = tot - 1; i >= 0; i--) { if (b[i] <= ans) break; int sum = 0; for (int j = i; j < tot; j++) { if (b[j] % b[i] == 0) sum = sum + cnt[j]; } if (sum * 2 >= n) { ans = b[i]; } } } printf("%I64d\n", ans); return 0; }
10
CPP
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:100000000000,100000000000") using namespace std; const long long inf = 1e18 + 7; const long long mod = 1e9 + 7; const double eps = 1e-12; const double PI = 2 * acos(0.0); const double E = 2.71828; int n; long long a[1000005]; long long d[1000005], sz; long long cnt[1000005]; long long res = 1; long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } void up(long long x) { sz = 0; for (long long i = 1; i * i <= x; ++i) { if (x % i == 0) { d[sz++] = i; if (i * i != x) { d[sz++] = x / i; } } } sort(d, d + sz); for (long long(i) = 0; (i) < (long long)(sz); (i)++) { cnt[i] = 0; } for (long long(i) = 0; (i) < (long long)(n); (i)++) { long long cur = gcd(x, a[i]); int num = lower_bound(d, d + sz, cur) - d; ++cnt[num]; } for (long long(i) = 0; (i) < (long long)(sz); (i)++) { for (int j = i + 1; j < sz; ++j) { if (d[j] % d[i] == 0) { cnt[i] += cnt[j]; } } if (cnt[i] + cnt[i] >= n && res < d[i]) { res = d[i]; } } } int main(void) { srand(time(NULL)); scanf("%d", &n); for (long long(i) = 0; (i) < (long long)(n); (i)++) { scanf("%I64d", a + i); } for (long long(i) = 0; (i) < (long long)(10); (i)++) { up(a[(rand() << 16 | rand()) % n]); } cout << res << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; int n, num, nums[10005]; long long a[1000005], d[1000005]; void divide(long long v) { num = 0; for (long long i = 1; i * i <= v; i++) { if (v % i == 0) { d[++num] = i; if (v / i != i) d[++num] = v / i; } } } long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } int main() { srand(time(NULL)); scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lld", &a[i]); int t = 11; long long ans = 1; while (t--) { long long x = a[1ll * rand() * rand() % n + 1]; divide(x); sort(d + 1, d + num + 1); memset(nums, 0, sizeof(nums)); for (int i = 1; i <= n; i++) { int pos = lower_bound(d + 1, d + num + 1, gcd(x, a[i])) - d; nums[pos]++; } for (int i = 1; i <= num; i++) for (int j = i + 1; j <= num; j++) if (d[j] % d[i] == 0) nums[i] += nums[j]; for (int i = num; i >= 1; i--) { if (nums[i] * 2 >= n) { ans = max(ans, d[i]); break; } } } printf("%lld\n", ans); }
10
CPP
#include <bits/stdc++.h> using namespace std; int n, i, j, k, shi__, tot, sum[1000005], l, r, mid, aim; long long a[1000005], d[1000005], ans, x, y; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } int main() { scanf("%d", &n); for (i = 1; i <= n; ++i) scanf("%I64d", &a[i]); for (shi__ = 1; shi__ <= 10; ++shi__) { x = a[((rand() << 15) + rand()) % n + 1]; tot = 0; for (i = 1; (long long)i * i <= x; ++i) if (x % i == 0) { d[++tot] = i; if ((long long)i * i != x) d[++tot] = x / i; } sort(d + 1, d + tot + 1); for (i = 1; i <= tot; ++i) sum[i] = 0; for (i = 1; i <= n; ++i) { y = gcd(a[i], x); l = 1; r = tot; aim = tot + 1; while (l <= r) { mid = l + r >> 1; if (d[mid] <= y) aim = mid, l = mid + 1; else r = mid - 1; } ++sum[aim]; } for (i = 1; i <= tot; ++i) for (j = 1; j < i; ++j) if (d[i] % d[j] == 0) sum[j] += sum[i]; for (i = 1; i <= tot; ++i) if (d[i] > ans && sum[i] * 2 >= n) ans = d[i]; } printf("%I64d\n", ans); }
10
CPP
#include <bits/stdc++.h> #pragma GCC optimize("O500") #pragma comment(linker, "/STACK:1677777216") #pragma warning(default : 4) using namespace std; const double eps = 1e-12; const int oo = 0x3F3F3F3F; const long long ooLL = 0x3F3F3F3F3F3F3F3FLL; const int MOD = 1000000007; template <typename T> T sqr(T x) { return x * x; } template <typename T> void debpr(const T &); template <typename T1, typename T2> void debpr(const pair<T1, T2> &); template <typename T1, typename T2> void debpr(const vector<T1, T2> &); template <typename T> void debpr(const set<T> &); template <typename T1, typename T2> void debpr(const map<T1, T2> &); template <typename T> void prcont(T be, T en, const string &st, const string &fi, const string &mi) { debpr(st); bool ft = 0; while (be != en) { if (ft) debpr(mi); ft = 1; debpr(*be); ++be; } debpr(fi); } template <typename T> void debpr(const T &a) {} template <typename T1, typename T2> void debpr(const pair<T1, T2> &p) { debpr("("); debpr(p.first); debpr(", "); debpr(p.second); debpr(")"); } template <typename T1, typename T2> void debpr(const vector<T1, T2> &a) { prcont(a.begin(), a.end(), "[", "]", ", "); } template <typename T> void debpr(const set<T> &a) { prcont(a.begin(), a.end(), "{", "}", ", "); } template <typename T1, typename T2> void debpr(const map<T1, T2> &a) { prcont(a.begin(), a.end(), "{", "}", ", "); } void deb(){}; template <typename T1> void deb(const T1 &t1) { debpr(t1); debpr('\n'); } template <typename T1, typename T2> void deb(const T1 &t1, const T2 &t2) { debpr(t1); debpr(' '); debpr(t2); debpr('\n'); } template <typename T1, typename T2, typename T3> void deb(const T1 &t1, const T2 &t2, const T3 &t3) { debpr(t1); debpr(' '); debpr(t2); debpr(' '); debpr(t3); debpr('\n'); } template <typename T1, typename T2, typename T3, typename T4> void deb(const T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4) { debpr(t1); debpr(' '); debpr(t2); debpr(' '); debpr(t3); debpr(' '); debpr(t4); debpr('\n'); } const double PI = acos(-1.); long long Round(double x) { return x < 0 ? x - .5 : x + .5; } template <typename T> void ass(bool v, const T &x, string m = "Fail") { if (!v) { deb(m); deb(x); throw; } } int main() { void run(); run(); return 0; } long long a[1 << 20]; int n; long long gcd(long long a, long long b) { long long t; while (a) b %= a, t = a, a = b, b = t; return b; } long long mrand() { long long rs = 0; for (int i = (0), _b(60); i < _b; ++i) if (rand() % 2) rs += 1LL << i; return rs; } void run() { scanf("%d", &n); for (int i = (0), _b(n); i < _b; ++i) scanf("%lld", &a[i]); long long rs = 1; srand(2); for (int it = (0), _b(12); it < _b; ++it) { long long t = a[mrand() % n]; unordered_map<long long, int> m; m.reserve(1 << 15); for (int i = (0), _b(n); i < _b; ++i) ++m[gcd(a[i], t)]; vector<pair<long long, long long> > v((m).begin(), (m).end()); for (int i = (0), _b(v.size()); i < _b; ++i) { int z = 0; for (int j = (0), _b(v.size()); j < _b; ++j) if (v[j].first % v[i].first == 0) z += v[j].second; if (z >= (n + 1) / 2) rs = max(rs, v[i].first); } } cout << rs << endl; }
10
CPP
#include <bits/stdc++.h> using namespace std; int N; long long a[1000010]; long long b[1000010]; int sz; int cnt[1000010]; long long val[1000010]; long long gcd(long long x, long long y) { return x ? gcd(y % x, x) : y; } bool check2(long long X) { int i, sum = 0; for ((i) = 0; (i) < (int)(sz); (i)++) if (val[i] % X == 0) sum += cnt[i]; return (sum * 2 >= N); } long long check(long long X) { int i, j; for ((i) = 0; (i) < (int)(N); (i)++) b[i] = gcd(X, a[i]); sort(b, b + N); sz = 0; i = 0; while (i < N) { for (j = i; j < N; j++) if (b[j] != b[i]) break; cnt[sz] = j - i; val[sz] = b[i]; sz++; i = j; } long long ans = 1; for (long long d = 1; d * d <= X; d++) if (X % d == 0) { if (check2(d)) ans = max(ans, d); if (check2(X / d)) ans = max(ans, X / d); } return ans; } int main(void) { int i; clock_t start = clock(); cin >> N; for ((i) = 0; (i) < (int)(N); (i)++) scanf("%I64d", &a[i]); long long ans = 1; for ((i) = 0; (i) < (int)(30); (i)++) { int r = (rand() * 10000 + rand()) % N; if (r < 0) r += N; long long tmp = check(a[r]); ans = max(ans, tmp); if (((double)clock() - start) / CLOCKS_PER_SEC > 3.0) break; } cout << ans << endl; return 0; }
10
CPP
#include <bits/stdc++.h> #pragma GCC optimize("O500") #pragma comment(linker, "/STACK:1677777216") #pragma warning(default : 4) using namespace std; const double eps = 1e-12; const int oo = 0x3F3F3F3F; const long long ooLL = 0x3F3F3F3F3F3F3F3FLL; const int MOD = 1000000007; template <typename T> T sqr(T x) { return x * x; } template <typename T> void debpr(const T &); template <typename T1, typename T2> void debpr(const pair<T1, T2> &); template <typename T1, typename T2> void debpr(const vector<T1, T2> &); template <typename T> void debpr(const set<T> &); template <typename T1, typename T2> void debpr(const map<T1, T2> &); template <typename T> void prcont(T be, T en, const string &st, const string &fi, const string &mi) { debpr(st); bool ft = 0; while (be != en) { if (ft) debpr(mi); ft = 1; debpr(*be); ++be; } debpr(fi); } template <typename T> void debpr(const T &a) {} template <typename T1, typename T2> void debpr(const pair<T1, T2> &p) { debpr("("); debpr(p.first); debpr(", "); debpr(p.second); debpr(")"); } template <typename T1, typename T2> void debpr(const vector<T1, T2> &a) { prcont(a.begin(), a.end(), "[", "]", ", "); } template <typename T> void debpr(const set<T> &a) { prcont(a.begin(), a.end(), "{", "}", ", "); } template <typename T1, typename T2> void debpr(const map<T1, T2> &a) { prcont(a.begin(), a.end(), "{", "}", ", "); } void deb(){}; template <typename T1> void deb(const T1 &t1) { debpr(t1); debpr('\n'); } template <typename T1, typename T2> void deb(const T1 &t1, const T2 &t2) { debpr(t1); debpr(' '); debpr(t2); debpr('\n'); } template <typename T1, typename T2, typename T3> void deb(const T1 &t1, const T2 &t2, const T3 &t3) { debpr(t1); debpr(' '); debpr(t2); debpr(' '); debpr(t3); debpr('\n'); } template <typename T1, typename T2, typename T3, typename T4> void deb(const T1 &t1, const T2 &t2, const T3 &t3, const T4 &t4) { debpr(t1); debpr(' '); debpr(t2); debpr(' '); debpr(t3); debpr(' '); debpr(t4); debpr('\n'); } const double PI = acos(-1.); long long Round(double x) { return x < 0 ? x - .5 : x + .5; } template <typename T> void ass(bool v, const T &x, string m = "Fail") { if (!v) { deb(m); deb(x); throw; } } int main() { void run(); run(); return 0; } long long a[1 << 20]; int n; long long gcd(long long a, long long b) { long long t; while (a) b %= a, t = a, a = b, b = t; return b; } long long mrand() { long long rs = 0; for (int i = (0), _b(60); i < _b; ++i) if (rand() % 2) rs += 1LL << i; return rs; } void run() { scanf("%d", &n); for (int i = (0), _b(n); i < _b; ++i) scanf("%lld", &a[i]); long long rs = 1; srand(3); for (int it = (0), _b(12); it < _b; ++it) { long long t = a[mrand() % n]; unordered_map<long long, int> m; m.reserve(1 << 15); for (int i = (0), _b(n); i < _b; ++i) ++m[gcd(a[i], t)]; vector<pair<long long, long long> > v((m).begin(), (m).end()); for (int i = (0), _b(v.size()); i < _b; ++i) { int z = 0; for (int j = (0), _b(v.size()); j < _b; ++j) if (v[j].first % v[i].first == 0) z += v[j].second; if (z >= (n + 1) / 2) rs = max(rs, v[i].first); } } cout << rs << endl; }
10
CPP
#include <bits/stdc++.h> using namespace std; long long read() { long long x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int n, top, c[1000005]; long long ans, d[1000005], a[1000005]; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } void solve() { long long x = a[((rand() << 15) + rand()) % n + 1], top = 0; for (long long i = 1; i * i <= x; i++) if (x % i == 0) { d[++top] = i; if (i * i != x) d[++top] = x / i; } sort(d + 1, d + top + 1); for (int i = 1; i <= top; i++) c[i] = 0; for (int i = 1; i <= n; i++) ++c[lower_bound(d + 1, d + top + 1, gcd(a[i], x)) - d]; for (int i = 1; i <= top; i++) for (int j = i + 1; j <= top; j++) if (d[j] % d[i] == 0) c[i] += c[j]; for (int i = 1; i <= top; i++) if (c[i] * 2 >= n) ans = max(ans, d[i]); } int main() { srand(time(NULL)); n = read(); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= 10; i++) solve(); cout << ans << endl; return 0; }
10
CPP
#include <bits/stdc++.h> using namespace std; vector<int> vec; int main() { int n; cin >> n; int A = 0, B = 0; for (int i = 0; i < n; i++) { int s; cin >> s; for (int j = 0; j < s / 2; j++) { int x; cin >> x; A += x; } if (s % 2) { int x; cin >> x; vec.push_back(x); } for (int j = 0; j < s / 2; j++) { int x; cin >> x; B += x; } } sort(vec.begin(), vec.end()); reverse(vec.begin(), vec.end()); for (int i = 0; i < vec.size(); i++) { if (i % 2) B += vec[i]; else A += vec[i]; } cout << A << ' ' << B << endl; }
9
CPP
#include <bits/stdc++.h> using namespace std; long int sum[1001][1001], size1[1001]; vector<long int> ar; int main() { long int n, i, j, k, a, sum1 = 0, sum2 = 0, temp; cin >> n; for (i = 0; i < n; i++) { cin >> k; size1[i] = k; for (j = 0; j < k; j++) { cin >> a; if (j) { sum[i][j] = sum[i][j - 1] + a; } else { sum[i][j] = a; } } } for (i = 0; i < n; i++) { if (size1[i] % 2 == 0) { temp = size1[i] / 2; sum1 += sum[i][temp - 1]; sum2 += sum[i][size1[i] - 1] - sum[i][temp - 1]; } else { temp = size1[i] / 2; if (temp != 0) { sum1 += sum[i][temp - 1]; } sum2 += sum[i][size1[i] - 1] - sum[i][temp]; ar.push_back(sum[i][temp] - sum[i][temp - 1]); } } sort(ar.begin(), ar.end()); reverse(ar.begin(), ar.end()); for (i = 0; i < ar.size(); i++) { if (i % 2) { sum2 += ar[i]; } else { sum1 += ar[i]; } } cout << sum1 << " " << sum2; return 0; }
9
CPP
#include <bits/stdc++.h> typedef struct nodo { int dato; struct nodo *anterior; struct nodo *siguiente; } nodo; typedef struct pila { nodo *primero; nodo *ultimo; } mazos; void agregar(mazos *mazo, int valor); void CrearMazo(mazos *mazo, int n); void imprimir(mazos mazo[], int dim); void eliminar(mazos *mazo, nodo *actual); int main() { int n, i, j, valor, turno = 1, ciel = 0, mayor, jiro = 0, ban = 1, cont, indice; nodo *direccion; scanf("%d", &n); int x[n]; mazos mazo[n]; CrearMazo(mazo, n); for (i = 0; i < n; i++) { scanf("%d", &x[i]); for (j = 0; j < x[i]; j++) { scanf("%d", &valor); agregar(&mazo[i], valor); } } for (i = 0; i < n; i++) { if (x[i] % 2 == 0) { while (mazo[i].primero != NULL) { if (turno % 2 != 0) { ciel += mazo[i].primero->dato; eliminar(&mazo[i], mazo[i].primero); } else { jiro += mazo[i].ultimo->dato; eliminar(&mazo[i], mazo[i].ultimo); } turno++; } } else { while (mazo[i].primero != mazo[i].ultimo) { if (turno % 2 != 0) { ciel += mazo[i].primero->dato; eliminar(&mazo[i], mazo[i].primero); } else { jiro += mazo[i].ultimo->dato; eliminar(&mazo[i], mazo[i].ultimo); } turno++; } } } while (ban == 1) { ban = 0; indice = -1; mayor = 0; for (i = 0; i < n; i++) { if (mazo[i].primero != NULL) { if (turno % 2 == 0) { if (mayor < mazo[i].ultimo->dato) { mayor = mazo[i].ultimo->dato; direccion = mazo[i].ultimo; indice = i; } } else { if (mayor < mazo[i].primero->dato) { mayor = mazo[i].primero->dato; direccion = mazo[i].primero; indice = i; } } } } if (turno % 2 != 0 && indice != -1) { ciel += mayor; eliminar(&mazo[indice], direccion); ban = 1; } else if (turno % 2 == 0 && indice != -1) { jiro += mayor; eliminar(&mazo[indice], direccion); ban = 1; } turno++; } printf("%d %d", ciel, jiro); return 0; } void agregar(mazos *mazo, int valor) { nodo *nuevo = (nodo *)malloc(sizeof(nodo)); if (mazo->ultimo != NULL) { nuevo->anterior = mazo->ultimo; mazo->ultimo->siguiente = nuevo; } else { nuevo->anterior = NULL; mazo->primero = nuevo; } mazo->ultimo = nuevo; nuevo->siguiente = NULL; nuevo->dato = valor; } void CrearMazo(mazos *mazo, int n) { int i; for (i = 0; i < n; i++) { mazo[i].primero = NULL; mazo[i].ultimo = NULL; } } void imprimir(mazos mazo[], int dim) { int i; nodo *actual; for (i = 0; i < dim; i++) { actual = mazo[i].primero; while (actual != NULL) { printf("%d ", actual->dato); actual = actual->siguiente; } } } void eliminar(mazos *mazo, nodo *actual) { nodo *temp = (nodo *)malloc(sizeof(nodo)); if (mazo->ultimo == mazo->primero) { mazo->primero = NULL; mazo->ultimo = NULL; temp = actual; } else { if (actual->siguiente == NULL) { actual->anterior->siguiente = NULL; mazo->ultimo = actual->anterior; } else { actual->siguiente->anterior = NULL; mazo->primero = actual->siguiente; } temp = actual; } free(temp); }
9
CPP
#include <bits/stdc++.h> using namespace std; vector<int> A; int main() { int n, x; cin >> n; int cards = 0; int ciel = 0, jiro = 0; for (int i = 0; i < n; i++) { int m; cin >> m; cards += m; for (int j = 0; j < m; j++) { cin >> x; if (m % 2 == 0) { if (j < m / 2) { ciel += x; } else { jiro += x; } } if (m % 2 == 1) { if (j < m / 2) { ciel += x; } if (j == m / 2) { A.push_back(x); } if (j > m / 2) { jiro += x; } } } } sort(A.begin(), A.end()); int f = 0; for (int i = A.size() - 1; i >= 0; i--) { if (f == 0) { ciel += A[i]; f = 1; } else { f = 0; jiro += A[i]; } } cout << ciel << " " << jiro << endl; }
9
CPP
#include <bits/stdc++.h> using namespace std; vector<int> v; int a[110][110]; int n; bool cmp(int x, int y) { return x > y; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i][0]); for (int j = 1; j <= a[i][0]; j++) scanf("%d", &a[i][j]); } int l = 0, r = 0; for (int i = 1; i <= n; i++) { for (int x = 1, y = a[i][0]; x < y; x++, y--) { l += a[i][x]; r += a[i][y]; } if (a[i][0] & 1) v.push_back(a[i][(a[i][0] + 1) / 2]); } sort(v.begin(), v.end(), cmp); for (int i = 0; i < v.size(); i++) if (i & 1) r += v[i]; else l += v[i]; printf("%d %d\n", l, r); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int main() { std::ios_base::sync_with_stdio(false); int n; cin >> n; int a1 = 0, a2 = 0; vector<int> rem; for (int i = 0; i < (n); i++) { int x; cin >> x; vector<int> A; for (int j = 0; j < (x); j++) { int k; cin >> k; A.push_back(k); }; if (x & 1) rem.push_back(A[x / 2]); for (int j = 0; j < (x / 2); j++) a1 += A[j], a2 += A[x - 1 - j]; } sort(rem.begin(), rem.end()); reverse(rem.begin(), rem.end()); for (int i = 0; i < (((int)rem.size())); i++) if (i & 1) a2 += rem[i]; else a1 += rem[i]; cout << a1 << " " << a2 << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; long long gcd(long long x, long long y) { if (y == 0) return x; else return gcd(y, x % y); } long long lcm(long long x, long long y) { return x / gcd(x, y) * y; } long long binpow(long long a, long long p) { if (!p) return 1; long long g = binpow(a, p >> 1); if (p % 2 == 0) return (g * g) % 1000000007; else return (((g * g) % 1000000007) * a) % 1000000007; } long long rev(long long n) { return binpow(n, (long long)1000000007 - 2LL); } void solve(int test_number); int main() { cout.setf(ios::fixed); cout.precision(20); cerr.setf(ios::fixed); cerr.precision(3); int n = 1; for (int i = 0; i < n; ++i) solve(i + 1); } long long sa, sb; int n; int cnt; long long cur[100100]; long long a[100100]; int sz = 0; void solve(int test_number) { sa = sb = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> cnt; for (int j = 0; j < cnt; j++) { cin >> cur[j]; } for (int j = 0; j < cnt / 2; j++) sa += cur[j]; for (int j = cnt / 2 + cnt % 2; j < cnt; j++) sb += cur[j]; if (cnt % 2 == 1) { a[sz] = cur[cnt / 2]; sz++; } } sort(a, a + sz, greater<int>()); for (int i = 0; i < sz; i++) if (i % 2 == 0) sa += a[i]; else sb += a[i]; cout << sa << ' ' << sb << endl; }
9
CPP
#include <bits/stdc++.h> using namespace std; mt19937 mrand(43); const double PI = acos((double)-1); const double eps = 1e-5; const long long inf0 = 1023 * 1024 * 1024; const long long inf = inf0 * inf0; const long long mod = 1e9 + 7; void solve(); void scan(); signed main() { cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); cout << fixed; cout.precision(15); solve(); return 0; } void solve() { int n; cin >> n; vector<vector<int>> a(n); long long a1 = 0, a2 = 0; vector<int> mem; for (int i = 0; i < n; i++) { int s; cin >> s; a[i].resize(s); for (int& j : a[i]) cin >> j; for (int j = 0; j < s / 2; j++) { a1 += a[i][j]; } reverse((a[i]).begin(), (a[i]).end()); for (int j = 0; j < s / 2; j++) a2 += a[i][j]; if (s & 1) mem.push_back(a[i][s / 2]); } sort((mem).rbegin(), (mem).rend()); for (int i = 0; i < mem.size(); i++) { if (i & 1) a2 += mem[i]; else a1 += mem[i]; } cout << a1 << " " << a2 << '\n'; return; }
9
CPP
#include <bits/stdc++.h> using namespace std; const long long int mx = 100000; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; long long ans = 0; long long sum = 0; vector<int> vect; while (n--) { int c; cin >> c; for (int i = 0; i < c; i++) { int x; cin >> x; sum += x; if (i < c / 2) ans += x; if (c & 1 && i == c / 2) vect.push_back(x); } } sort(vect.begin(), vect.end(), greater<int>()); for (int i = 0; i < vect.size(); i += 2) ans += vect[i]; cout << ans << " " << sum - ans << endl; }
9
CPP
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const double pi = acos(-1.0); vector<int> all; int a[105]; int main() { int n, i, sum1 = 0, sum = 0, j, x; scanf("%d", &n); for (i = 1; i <= n; ++i) { scanf("%d", &x); for (j = 1; j <= x; ++j) { scanf("%d", &a[j]); } for (j = 1; j * 2 <= x; ++j) sum += a[j]; if (x & 1) { all.push_back(a[(x + 1) / 2]); ++j; } for (; j <= x; ++j) sum1 += a[j]; } sort(all.begin(), all.end()); reverse(all.begin(), all.end()); for (i = 0; i < all.size(); ++i) { if (i & 1) sum1 += all[i]; else sum += all[i]; } printf("%d %d\n", sum, sum1); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; long long rdtsc() { long long tmp; asm("rdtsc" : "=A"(tmp)); return tmp; } inline int myrand() { return abs((rand() << 15) ^ rand()); } inline int rnd(int x) { return myrand() % x; } const int maxn = 100; int a[maxn][maxn]; int cnt[maxn]; int tosort[maxn]; bool solve() { int n; if (scanf("%d", &n) < 1) return 0; int ans1 = 0, ans2 = 0; int odd = 0; for (int i = 0; i < n; ++i) { scanf("%d", &cnt[i]); for (int j = 0; j < cnt[i]; ++j) { int x; scanf("%d", &x); a[i][j] = x; } for (int j = 0; j < cnt[i] / 2; ++j) ans1 += a[i][j], ans2 += a[i][cnt[i] - 1 - j]; if (cnt[i] & 1) tosort[odd++] = a[i][cnt[i] / 2]; } sort(tosort, tosort + odd); reverse(tosort, tosort + odd); for (int i = 0; i < odd; ++i) if (!(i & 1)) ans1 += tosort[i]; else ans2 += tosort[i]; printf("%d %d\n", ans1, ans2); return 1; } int main() { srand(rdtsc()); while (1) { if (!solve()) break; } return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; int n, s[120], c[120][120], tota, totb, sta[120], top; void deal(int a[], int p) { if (p & 1) sta[++top] = a[(p + 1) / 2]; for (int i = 1; i <= p / 2; i++) tota += a[i]; for (int i = (p + 1) / 2 + 1; i <= p; i++) totb += a[i]; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", s + i); for (int j = 1; j <= s[i]; j++) scanf("%d", &c[i][j]); deal(c[i], s[i]); } sort(sta + 1, sta + top + 1); for (int i = top; i >= 1; i--) if ((top - i) & 1) totb += sta[i]; else tota += sta[i]; printf("%d %d\n", tota, totb); }
9
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long int; int main() { ll T; cin >> T; ll a = 0, b = 0; vector<ll> v; for (ll i = 1; i <= T; i++) { ll n; cin >> n; if (n & 1) { for (ll i = 1; i <= n / 2; i++) { ll t; cin >> t; a += t; } ll t; cin >> t; v.push_back(t); for (ll i = n / 2 + 2; i <= n; i++) { ll t; cin >> t; b += t; } } else { for (ll i = 1; i <= n / 2; i++) { ll t; cin >> t; a += t; } for (ll i = n / 2 + 1; i <= n; i++) { ll t; cin >> t; b += t; } } } sort(v.begin(), v.end(), greater<ll>()); for (ll i = 0; i < v.size(); i++) if (i % 2 == 0) a += v[i]; else b += v[i]; cout << a << " " << b << "\n"; return 0; }
9
CPP
#include <bits/stdc++.h> void Quicksort(int *v, int izq, int der); int pivot(int *v, int izq, int der); int main() { int i, n, carta, contador = 0, j, m; scanf("%d", &n); int vector[101], ciel = 0, jiro = 0; for (i = 0; i < 101; i++) { vector[i] = 0; } for (i = 0; i < n; i++) { scanf("%d", &m); for (j = 0; j < m; j++) { scanf("%d", &carta); if (j < m / 2) { ciel = ciel + carta; } else if (m % 2 != 0 && j == m / 2) { vector[contador] = carta; contador++; } else { jiro = jiro + carta; } } } Quicksort(vector, 0, 100); for (i = 0; i < contador; i++) { if (i % 2 != 0) jiro = jiro + vector[i]; else ciel = ciel + vector[i]; } printf("%d %d", ciel, jiro); return 0; } int pivot(int *v, int izq, int der) { int i; int pivote, valor_pivote; int aux; pivote = izq; valor_pivote = v[pivote]; for (i = izq + 1; i <= der; i++) { if (v[i] > valor_pivote) { pivote++; aux = v[i]; v[i] = v[pivote]; v[pivote] = aux; } } aux = v[izq]; v[izq] = v[pivote]; v[pivote] = aux; return pivote; } void Quicksort(int *v, int izq, int der) { int pivote; if (izq < der) { pivote = pivot(v, izq, der); Quicksort(v, izq, pivote - 1); Quicksort(v, pivote + 1, der); } }
9
CPP
import re def main(): n=eval(input()) a=[] s=[] s.append(0) s.append(0) while n: n-=1 temp=re.split(' ',input()) k=eval(temp[0]) for i in range(k>>1): s[0]+=eval(temp[i+1]) if k&1: a.append(eval(temp[(k+1)>>1])) for i in range((k+1)>>1,k): s[1]+=eval(temp[i+1]) a.sort(reverse=True) k=0 for i in a: s[k]+=i k^=1 print(s[0],s[1]) main()
9
PYTHON3
#include <bits/stdc++.h> using namespace std; int n; int s[105]; int c[105][105]; int ans[2]; vector<int> mid; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> s[i]; for (int j = 0; j < s[i]; j++) { cin >> c[i][j]; if (j < s[i] / 2) { ans[0] += c[i][j]; } else { if (s[i] % 2 == 1 && j == s[i] / 2) { mid.push_back(c[i][j]); } else { ans[1] += c[i][j]; } } } } sort(mid.begin(), mid.end()); reverse(mid.begin(), mid.end()); for (int i = 0; i < mid.size(); i++) { ans[i % 2] += mid[i]; } cout << ans[0] << " " << ans[1] << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long int llINF = 0x3f3f3f3f3f3f3f; const int N = 1e6 + 1; const int MAX_SIEVE = 1e6 + 2; int main() { long long int a = 0, b = 0; vector<long long int> mid; int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int s; scanf("%d", &s); for (int j = 0; j < s / 2; j++) { long long int plc; scanf("%lld", &plc); a += plc; } if (s % 2) { long long int plc; scanf("%lld", &plc); mid.push_back(plc); } for (int j = 0; j < s / 2; j++) { long long int plc; scanf("%lld", &plc); b += plc; } } sort(mid.begin(), mid.end()); for (int i = mid.size() - 1, j = 0; i >= 0; i--, j++) { if (j % 2) b += mid[i]; else a += mid[i]; } printf("%lld %lld\n", a, b); }
9
CPP
#include <bits/stdc++.h> using namespace std; int n, x, y; vector<int> A, O; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { int a; scanf("%d", &a); A.resize(a); for (int j = 0; j < a; j++) scanf("%d", &A[j]); if ((a & 1) == 1) { for (int j = 0; j < a / 2; j++) x += A[j]; for (int j = a / 2 + 1; j < a; j++) y += A[j]; O.push_back(A[a / 2]); } else { for (int j = 0; j < a / 2; j++) x += A[j]; for (int j = a / 2; j < a; j++) y += A[j]; } } sort((O).begin(), (O).end(), greater<int>()); for (int i = 0; i < (int)(O).size(); i++) { if (i % 2) y += O[i]; else x += O[i]; } printf("%d %d\n", x, y); }
9
CPP
p, n = [], int(input()) a = b = 0 for i in range(n): t = list(map(int, input().split())) k = t[0] // 2 + 1 a += sum(t[1: k]) if t[0] & 1: p.append(t[k]) b += sum(t[k + 1: ]) else: b += sum(t[k: ]) p.sort(reverse = True) print(a + sum(p[0 :: 2]), b + sum(p[1 :: 2]))
9
PYTHON3
#include <bits/stdc++.h> using namespace std; vector<int> Q[123]; int stk[123]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { int x; scanf("%d", &x); for (int j = 0; j < x; j++) { int u; scanf("%d", &u); Q[i].push_back(u); } } int cnt = 0; int A = 0, B = 0; for (int i = 0; i < n; i++) { if (Q[i].size() % 2 == 1) { stk[cnt++] = Q[i][Q[i].size() / 2]; } for (int j = 0; j < Q[i].size() / 2; j++) A += Q[i][j]; for (int j = 0; j < Q[i].size() / 2; j++) B += Q[i][Q[i].size() - 1 - j]; } sort(stk, stk + cnt); for (int i = cnt - 2; i > -1; i -= 2) B += stk[i]; for (int i = cnt - 1; i > -1; i -= 2) A += stk[i]; cout << A << ' ' << B << '\n'; }
9
CPP
#include <bits/stdc++.h> using namespace std; struct gameTheoryGraph { struct Edge { int id; int dest; long long weight; Edge(int _dest = -1, long long _weight = 1, int _id = 0) { dest = _dest; weight = _weight; id = _id; } bool operator<(const Edge& other) const { return dest < other.dest; } }; int n; vector<vector<Edge>> adj; vector<int> mex, topologicalOrder; gameTheoryGraph(int nodes = 0) { n = nodes; adj.resize(n + 1); } void addEdgeDirected(int u, int v, long long w = 1) { adj[u].push_back(Edge(v, w)); } void getMex() { mex = vector<int>(n + 1, -1); for (int i = 1; i <= n; i++) { if (adj[i].size() == 0) mex[i] = 0; } topoSort(); vector<int> neighbourMex(n, 0); reverse(topologicalOrder.begin(), topologicalOrder.end()); for (auto cur : topologicalOrder) { if (mex[cur] == -1) { for (auto it : adj[cur]) { neighbourMex[mex[it.dest]] = 1; } mex[cur] = 0; while (neighbourMex[mex[cur]] == 1) mex[cur]++; for (auto it : adj[cur]) { neighbourMex[it.dest] = 0; } } } } void topoSort() { vector<int> ins(n + 1, 0); topologicalOrder.clear(); for (int i = (1); i <= (n); ++i) for (auto it : adj[i]) ++ins[it.dest]; deque<int> q; for (int i = (1); i <= (n); ++i) if (ins[i] == 0) q.push_back(i); while (!q.empty()) { int v = q.front(); q.pop_front(); for (auto it : adj[v]) if ((--ins[it.dest]) == 0) topologicalOrder.push_back(it.dest); topologicalOrder.push_back(v); } topologicalOrder = (int((topologicalOrder).size()) == n) ? topologicalOrder : vector<int>(); } }; int main() { std::ios::sync_with_stdio(0); std::cin.tie(0); std::cout.tie(0); int n; cin >> n; vector<vector<long long>> a(n); long long total = 0; for (int i = 0; i < n; i++) { int si; cin >> si; a[i].resize(si); for (int j = 0; j < si; j++) { cin >> a[i][j]; total += a[i][j]; } } long long player1 = 0; vector<long long> rem; for (int i = 0; i < n; i++) { for (int j = 0; j < (a[i].size() / 2); j++) { player1 += a[i][j]; } if (a[i].size() % 2 == 1) { rem.push_back(a[i][a[i].size() / 2]); } } sort(rem.begin(), rem.end()); reverse(rem.begin(), rem.end()); for (int i = 0; i < rem.size(); i += 2) { player1 += rem[i]; } cout << player1 << " " << total - player1 << "\n"; }
9
CPP
#include <bits/stdc++.h> int main() { int i, j, k, m, n, top = 0, l = 0, r = 0; int s[200]; scanf("%d", &n); for (int ii = 1; ii <= n; ii++) { scanf("%d", &m); if (m % 2 == 0) { for (i = 1; i <= m / 2; i++) { scanf("%d", &k); l += k; } for (i = 1; i <= m / 2; i++) { scanf("%d", &k); r += k; } } else { for (i = 1; i <= m / 2; i++) { scanf("%d", &k); l += k; } scanf("%d", &k); s[++top] = k; for (i = 1; i <= m / 2; i++) { scanf("%d", &k); r += k; } } } for (i = 1; i <= top; i++) { for (j = i + 1; j <= top; j++) if (s[i] < s[j]) { int p = s[i]; s[i] = s[j]; s[j] = p; } if (i % 2 == 0) r += s[i]; else l += s[i]; } printf("%d %d\n", l, r); return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; vector<int> arr; int main(void) { int n, val, tmp; long long ceil = 0, jiro = 0; cin.sync_with_stdio(false); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &val); for (int j = 0; j < val / 2; j++) { scanf("%d", &tmp); ceil += tmp; } if (val & 1) { scanf("%d", &tmp); arr.push_back(tmp); } for (int j = 0; j < val / 2; j++) { scanf("%d", &tmp); jiro += tmp; } } int flag = 0; std::sort(arr.begin(), arr.end()); for (int i = arr.size() - 1; i >= 0; i--) { if (flag == 0) ceil += arr[i]; else jiro += arr[i]; flag = !flag; } cout << ceil << " " << jiro << "\n"; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; ; template <class C> void mini(C& a4, C b4) { a4 = min(a4, b4); } template <class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); } template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> pair) { return out << "(" << pair.first << ", " << pair.second << ")"; }; int main() { ios_base::sync_with_stdio(); cout << fixed << setprecision(10); int lsum = 0, rsum = 0; vector<int> middle; int n; scanf("%d", &n); for (int i = 0; i < (n); ++i) { int k; scanf("%d", &k); for (int j = 0; j < (k); ++j) { int x; scanf("%d", &x); if (j < k / 2) lsum += x; if (j * 2 + 1 == k) middle.push_back(x); if (j >= (k + 1) / 2) rsum += x; } } sort((middle).begin(), (middle).end()); reverse((middle).begin(), (middle).end()); for (int i = 0; i < (((int)(middle).size())); ++i) { if (i & 1) rsum += middle[i]; else lsum += middle[i]; } printf("%d %d\n", lsum, rsum); return 0; }
9
CPP
#include <cstdlib> #include <cstdarg> #include <cassert> #include <cctype> // tolower #include <ctime> #include <cmath> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <stdexcept> #include <map> #include <tuple> #include <unordered_map> #include <set> #include <list> #include <stack> #include <queue> #include <vector> #include <string> #include <limits> #include <utility> #include <memory> #include <numeric> #include <iterator> #include <algorithm> #include <functional> /* * g++ -g -std=c++11 -DBUG -D_GLIBCXX_DEBUG -Wall -Wfatal-errors -o cforce{,.cpp} * * TODO: * C++ dataframe * stl11 -> c++11 standard template lib in c++98 * overload >> for map and set, using (insert) iterator * chmod:: consider an algorithm stable to int64 overflow * shortest path algorithm * shortest path in a tree * maximum network flow * partial idx/iter sort * a prime number generator which traverses prime numbers w/ ++ * a divisor generator which traverses divisors efficiently * Apply1st ?! * Apply2nd and bind2nd ?! * count_if ( a.begin(), a.end(), a.second < x ) * Arbitrary-precision arithmetic / Big Integer / Fraction - rational num * tuple class --> cplusplus.com/reference/tuple * get<1>, get<2>, bind2nd ( foo ( get<2> pair ), val ) * isin( const T & val, first, last ) * fuinction composition in c++ * blogea.bureau14.fr/index.php/2012/11/function-composition-in-c11/ * cpp-next.com/archive/2010/11/expressive-c-fun-with-function-composition/ * TimeWrapper -- accumulate time of a function call * stackoverflow.com/questions/879408 * hash map -- possible hash value & obj % some_big_prime [ b272 ] * lower level can be a simple map to resolve hash collisions * add explicit everywhere necessary * bloom filters * heap -> how to update values in place / increase-key or decrease-key ... IterHeap ?! * median maintaince --> max_heap / min_heap * prim's min spaning tree alg. O ( m log n ) using heap contianing V - X vertices * kruskal algorithm minimum cost spanning tree with union find data structure * unique_ptr * hufman codes * simple arithmatic tests * longest common subsequence using seq. alignment type algorithm * longest common substring ( consequative subsequeance ) * Graham scan; en.wikipedia.org/wiki/Graham_scan * problem/120/F -- how to extend in order to calculate all-pair distances in a tree * compile time prime number calculator using templates * swith to type_cast < T1, T2 > for val2str & str2val * overload plus<> for two vectors?! * overloading operator << for std::tuple: * stackoverflow.com/questions/9247723 * bitmask class with iterator protocol * ??? segment tree +++ interval tree * ??? an lru cache: a heap which maintains last access, an a map which maintains key:value */ /* * @recepies * ---------------------------------------------- * odd / even * transform ( x.begin(), x.end(), x.begin(), bind2nd( modulus<int>(), 2 )); * count_if ( x.begin(), x.end(), bind2nd( modulus < int > (), 2)); * Apply2nd * max_element ( m.begin(), m.end(), Apply2nd < string, int , less < int > > ) * sort ( a.begin(), a.end(), Apply2nd < char, int , greater< int > > ) * count_if ( m.begin(), m.end(), Apply2nd < string, int, modulus < int > > ) * accumulate ( m.begin(), m.end(), 0.0, Apply2nd < int, double, plus < double > > ) * accumulate ( m.begin( ), m.end( ), pair < int, double> ( 0 , 0.0 ) , operator+ < int, double > ) * abs_diff * adjacent_difference ( a.begin(), a.end(), adj_diff.begin( ), abs_diff < int > ( ) ) * accumulate ( a.begin(), a.end(), 0, abs_diff < int > ( ) ) * erase * a.erase ( remove_if ( a.begin( ), a.end( ), bind2nd ( less < int >( ), 0 ) ), a.end( ) ) * a.erase ( remove ( a.begin( ), a.end( ), b.begin( ), b.end( ) ), a.end ( ) ) * binding * bind2nd ( mem_fun_ref (& ClassName::m_func ), arg ) // binds the argument of the object * iterator generators * generate_n ( back_inserter ( a ), n, rand ); // calls push_back * generate_n ( inserter( a, a.begin( ) + 5 ), 10, RandInt( 0 , 100 ) ) // calls insert * copy ( foo.begin( ), foo.end( ), insert_iterator < std::list < double > > ( bar, bar.begin( ) + 5 )) * copy ( a.begin( ), a.end( ), ostream_iterator < double > ( cout, ", " )) * accumulate ( m.begin( ), m.end( ), pair < int, double> ( 0 , 0.0 ) , operator+ < int, double > ) * transform (numbers.begin(), numbers.end(), lengths.begin(), mem_fun_ref(&string::length)); */ /* * @good read * ---------------------------------------------- * [ partial ] template specialization * cprogramming.com/tutorial/template_specialization.html * function composition * cpp-next.com/archive/2010/11/expressive-c-fun-with-function-composition */ /* * @prob set * ---------------------------------------------- * purification --> c330 */ /* * @limits * ---------------------------------------------- * int 31 2.14e+09 * long int 31 2.14e+09 * unsigned 32 4.29e+09 * long unsigned 32 4.29e+09 * size_t 32 4.29e+09 * long long int 63 9.22e+18 * long long unsigned 64 1.84e+19 */ /* * issues * ---------------------------------------------- * stackoverflow.com/questions/10281809 * mem_fun -> func_obj ( pointer to instance, origanal argument ) * bind1st ( mem_fun ( & ClassName::m_func ), this ) // binds obj of the class * bind1st takes 'const T &' as the first argument */ /* * typedef / define * ---------------------------------------------- */ typedef long long int int64; typedef unsigned long long int uint64; #ifndef M_PI #define M_PI 3.14159265358979323846L #endif #define DOUBLE_INF std::numeric_limits< double >::infinity() #define DOUBLE_NAN std::numeric_limits< double >::quiet_NaN() #define __STR__(a) #a #define STR(a) __STR__(a) #define ASSERT(expr, msg) \ if (!( expr )) \ throw std::runtime_error(__FILE__ ":" STR(__LINE__) " - " msg); #define DECLARE( X ) \ typedef std::shared_ptr < X > X ## _shared_ptr; \ typedef const std::shared_ptr < X > X ## _const_shared_ptr; #ifdef BUG #define DEBUG(var) { std::cout << #var << ": " << (var) << std::endl; } #define COND_DEBUG(expr, var) if (expr) \ { std::cout << #var << ": " << (var) << std::endl; } #define EXPECT(expr) if ( ! (expr) ) std::cerr << "Assertion " \ << #expr " failed at " << __FILE__ << ":" << __LINE__ << std::endl; #else #define DEBUG(var) #define COND_DEBUG(expr, var) #define EXPECT(expr) #endif #define DBG(v) std::copy( v.begin(), v.end(), std::ostream_iterator < typeof( *v.begin() )> ( std::cout, " " ) ) #if __cplusplus < 201100 #define move( var ) var #endif /* * http://rootdirectory.de/wiki/SSTR() * usage: * SSTR( "x^2: " << x*x ) */ #define SSTR( val ) dynamic_cast< std::ostringstream & >( std::ostringstream() << std::dec << val ).str() /* https://www.quora.com/C++-programming-language/What-are-some-cool-C++-tricks */ // template <typename T, size_t N> // char (&ArraySizeHelper(T (&array)[N]))[N]; // #define arraysize(array) (sizeof(ArraySizeHelper(array))) /* * forward decleration * ---------------------------------------------- */ class ScopeTimer; /* * functional utils * ---------------------------------------------- */ template < typename T > struct abs_diff : std::binary_function < T, T, T > { typedef T value_type; inline value_type operator( ) ( const value_type & x, const value_type & y ) const { return std::abs( x - y ); } }; // template < class InputIterator, class T > // class isin : public std::binary_function < InputIterator, InputIterator, bool > // { // public: // typedef T value_type; // // isin ( const InputIterator & first, const InputIterator & last ): // m_first ( first ), m_last ( last ) { } // // bool operator ( ) ( const value_type & val ) const // { // return std::find ( m_first, m_last, val ) != m_last; // } // private: // const InputIterator m_first, m_last; // } template < typename value_type, typename cont_type > class isin : public std::unary_function < value_type, bool > { public: isin( const cont_type & vals ): m_vals ( vals ) { }; bool operator ( ) ( const value_type & x ) const { return std::find ( m_vals.begin( ), m_vals.end( ), x ) != m_vals.end( ); } private: const cont_type m_vals; }; /* * max_element, min_element, count_if ... on the 2nd element * eg: max_element ( m.begin(), m.end(), Apply2nd < string, int , less < int > > ) */ template < class T1, class T2, class BinaryOperation > class Apply2nd : std::binary_function < typename std::pair < T1, T2 >, typename std::pair < T1, T2 >, typename BinaryOperation::result_type > { public: typedef T1 first_type; typedef T2 second_type; typedef typename BinaryOperation::result_type result_type; typedef typename std::pair < first_type, second_type > value_type; inline result_type operator( ) ( const value_type & x, const value_type & y ) const { return binary_op ( x.second , y.second ); } private: BinaryOperation binary_op; }; /* * algo utils * ---------------------------------------------- */ /** * count the number of inversions in a permutation; i.e. how many * times two adjacent elements need to be swaped to sort the list; * 3 5 2 4 1 --> 7 */ template < class InputIterator > typename std::iterator_traits<InputIterator>::difference_type count_inv ( InputIterator first, InputIterator last ) { typedef typename std::iterator_traits<InputIterator>::difference_type difference_type; typedef typename std::iterator_traits<InputIterator>::value_type value_type; std::list < value_type > l; /* list of sorted values */ difference_type cnt = 0; for ( difference_type n = 0; first != last; ++first, ++n ) { /* count how many elements larger than *first appear before *first */ typename std::list < value_type >::iterator iter = l.begin( ); cnt += n; for ( ; iter != l.end( ) && * iter <= * first; ++ iter, -- cnt ) ; l.insert( iter, * first ); } return cnt; } template < class ForwardIterator, class T > inline void fill_inc_seq ( ForwardIterator first, ForwardIterator last, T val ) { for ( ; first != last; ++first, ++val ) * first = val; } template <class ForwardIterator, class InputIterator > ForwardIterator remove ( ForwardIterator first, ForwardIterator last, InputIterator begin, InputIterator end ) { ForwardIterator result = first; for ( ; first != last; ++ first ) if ( find ( begin, end, *first ) == end ) { *result = *first; ++result; } return result; } /* stackoverflow.com/questions/1577475 */ template < class RAIter, class Compare > class ArgSortComp { public: ArgSortComp ( const RAIter & first, Compare comp ): m_first ( first ), m_comp( comp ) { } inline bool operator() ( const size_t & i, const size_t & j ) const { return m_comp ( m_first[ i ] , m_first[ j ] ); } private: const RAIter & m_first; const Compare m_comp; }; /*! * usage: * vector < size_t > idx; * argsort ( a.begin( ), a.end( ), idx, less < Type > ( ) ); */ template < class RAIter, class Compare=std::less< typename RAIter::value_type > > void argsort( const RAIter & first, const RAIter & last, std::vector < size_t > & idx, Compare comp = Compare()) // std::less< typename RAIter::value_type >() ) { const size_t n = last - first; idx.resize ( n ); for ( size_t j = 0; j < n; ++ j ) idx[ j ] = j ; std::stable_sort( idx.begin(), idx.end(), ArgSortComp< RAIter, Compare >( first, comp )); } template < class RAIter, class Compare > class IterSortComp { public: IterSortComp ( Compare comp ): m_comp ( comp ) { } inline bool operator( ) ( const RAIter & i, const RAIter & j ) const { return m_comp ( * i, * j ); } private: const Compare m_comp; }; /*! * usage: * vector < list < Type >::const_iterator > idx; * itersort ( a.begin( ), a.end( ), idx, less < Type > ( ) ); */ template <class INIter, class RAIter, class Compare> void itersort ( INIter first, INIter last, std::vector < RAIter > & idx, Compare comp ) { /* alternatively: stackoverflow.com/questions/4307271 */ idx.resize ( std::distance ( first, last ) ); for ( typename std::vector < RAIter >::iterator j = idx.begin( ); first != last; ++ j, ++ first ) * j = first; std::sort ( idx.begin( ), idx.end( ), IterSortComp< RAIter, Compare > (comp ) ); } /* * string utils * ---------------------------------------------- */ inline void erase ( std::string & str, const char & ch ) { std::binder2nd < std::equal_to < char > > isch ( std::equal_to < char > ( ), ch ); std::string::iterator iter = std::remove_if ( str.begin(), str.end(), isch ); str.erase ( iter, str.end() ); } inline void erase ( std::string & str, const std::string & chrs ) { isin < char, std::string > isin_chrs ( chrs ); std::string::iterator iter = std::remove_if ( str.begin(), str.end(), isin_chrs ); str.erase ( iter, str.end() ); } template < typename value_type> inline std::string val2str ( const value_type & x ) { std::ostringstream sout ( std::ios_base::out ); sout << x; return sout.str(); } template < typename value_type> inline value_type str2val ( const std::string & str ) { std::istringstream iss ( str, std::ios_base::in ); value_type val; iss >> val; return val; } std::vector< std::string > tokenize ( const std::string & str, const char & sep ) { /*! * outputs empty tokens and assumes str does not start with sep * corner cases: * empty string, one char string, * string starting/ending with sep, all sep, end with two sep */ std::vector < std::string > res; std::string::const_iterator follow = str.begin(), lead = str.begin(); while ( true ) { while ( lead != str.end() && * lead != sep ) ++ lead; res.push_back ( std::string( follow, lead ) ); if ( lead != str.end () ) follow = 1 + lead ++ ; else break; } return res; } /*! * chunk a string into strings of size [ at most ] k */ void chunk ( const std::string::const_iterator first, const std::string::const_iterator last, const size_t k, const bool right_to_left, std::list < std::string > & str_list ) { str_list.clear( ); if ( right_to_left ) /* chunk from the end of the string */ for ( std::string::const_iterator i, j = last; j != first; j = i ) { i = first + k < j ? j - k : first; str_list.push_back ( std::string ( i, j ) ); } else /* chunk from the begining of the string */ for ( std::string::const_iterator i = first, j; i != last; i = j ) { j = i + k < last ? i + k : last; str_list.push_back ( std::string ( i, j ) ); } } /*! * next lexicographically smallest string * within char set a..z */ std::string & operator++( std::string & s ) { /* find the first char from right less than 'z' */ std::string::reverse_iterator j = find_if( s.rbegin( ), s.rend( ), std::bind2nd( std::less < char > ( ), 'z' )); if ( j != s.rend( )) { ++ *j; std::fill( s.rbegin( ), j, 'a' ); } else s.assign( s.length( ) + 1, 'a' ); return s; } /*! * getline ( cin, str ) * requires ctrl-D * cin >> str; does not pass after space char */ /* * number utils * ---------------------------------------------- */ class BigInteger { #if ULONG_MAX <= 1 << 32 typedef long long unsigned val_type; #else typedef long unsigned val_type; #endif const static int WSIZE = 32; const static val_type BASE = 1LL << WSIZE; public: private: std::list < val_type > val; /* val[ 0 ] is most significant */ bool pos; /* true if sign is positive */ }; /** * greatest common divisor - Euclid's alg. */ template < typename value_type > inline value_type gcd ( value_type a, value_type b ) { return ! b ? a : gcd( b, a % b ); } /** * prime factorization */ template < class T > void prime_factors( T n, std::map < T, size_t > & fac ) { for ( T k = 2; n > 1; ++ k ) if ( ! ( n % k ) ) { size_t & ref = fac[ k ]; while ( ! ( n % k ) ) { ++ ref; n /= k; } } } /* abs diff - safe for unsigned types */ template < class T > inline T absdiff( T a, T b ) { return a < b ? b - a : a - b; } namespace { template < class T > std::pair < T, T > __extgcd ( const T & x0, const T & y0, const T & x1, const T & y1, const T & r0, const T & r1 ) { const T q = r0 / r1; const T r2 = r0 % r1; if ( ! ( r1 % r2 ) ) return std::make_pair < T, T > ( x0 - q * x1, y0 - q * y1 ); const T x2 = x0 - q * x1; const T y2 = y0 - q * y1; return __extgcd ( x1, y1, x2, y2, r1, r2 ); } } /** * extended euclidean algorithm: a x + b y = gcd( a, b) * en.wikipedia.org/wiki/Extended_Euclidean_algorithm */ template < class value_type > inline std::pair < value_type, value_type > extgcd ( value_type a, value_type b ) { return a % b ? __extgcd < value_type > ( 1, 0, 0, 1, a, b ) : std::make_pair < value_type, value_type > ( 0, 1 ); } /** * modular multiplicative inverse * en.wikipedia.org/wiki/Modular_multiplicative_inverse */ template < class value_type > inline value_type modinv ( value_type a, value_type m ) { const std::pair < value_type, value_type > coef ( extgcd( a, m ) ); /* a needs to be coprime to the modulus, or the inverse won't exist */ if ( a * coef.first + m * coef.second != 1 ) throw std::runtime_error ( val2str( a ) + " is not coprime to " + val2str( m )); /* return a pos num between 1 & m-1 */ return ( m + coef.first % m ) % m; } inline bool isnan ( const double & a ) { return ! ( a == a ); } template < typename value_type > inline value_type mini ( int n, ... ) { va_list vl; va_start (vl, n); value_type res = va_arg ( vl, value_type ); for ( int i = 1; i < n; ++i ) { const value_type val = va_arg ( vl, value_type ); res = std::min ( res, val ); } va_end( vl ); return res; } template < typename value_type > inline value_type maxi ( int n, ... ) { va_list vl; va_start (vl, n); value_type res = va_arg ( vl, value_type ); for ( int i = 1; i < n; ++i ) { const value_type val = va_arg ( vl, value_type ); res = std::max ( res, val ); } va_end( vl ); return res; } // XXX look this up how is this implemented template < class T > inline int sign ( const T & x ) { if ( x == T() ) return 0; else if ( x < T() ) return -1; else return 1; } /* * change moduluos from n to m */ std::string chmod ( std::string num, const unsigned n, const unsigned m ) { const char * digit = "0123456789abcdefghijklmnopqrstuvwxyz"; std::transform ( num.begin(), num.end(), num.begin(), tolower ); isin < char, std::string > is_alpha_num ( digit ); assert ( find_if ( num.begin( ), num.end( ), std::not1 ( is_alpha_num ) ) == num.end( )); unsigned long long int val ( 0 ); if ( n == 10U ) { std::istringstream iss ( num, std::ios_base::in ); iss >> val; } else for ( std::string::const_iterator iter = num.begin( ); iter != num.end( ); ++ iter ) val = val * n + ( 'a' <= *iter ? *iter - 'a' + 10U : *iter - '0'); if ( m == 10U ) { std::ostringstream sout ( std::ios_base::out ); sout << val; return sout.str ( ); } else { std::string res; for ( ; val ; val /= m ) res.push_back( digit [ val % m ] ); return res.length( ) ? std::string( res.rbegin( ), res.rend( )) : "0"; } } template < class value_type > /* a^n mod m */ value_type powmod ( value_type a, const value_type & n, const value_type & m ) { if ( a == 1 || ! n ) return m != 1 ? 1 : 0; value_type res = 1; for ( value_type k = 1; k <= n; a = a * a % m, k = k << 1 ) if ( k & n ) res = ( res * a ) % m; return res; } template < class T > inline T atan(const T & x, const T & y) { return std::atan( y / x ) + ( x < 0 ) * M_PI; } /* * Fermat pseudoprime test * www.math.umbc.edu/~campbell/Computers/Python/numbthy.py * NOTE: since return type is bool, and powmod may break for ints, * the argument is always casted to long long */ inline bool is_pseudo_prime ( const long long & a ) { /* all the primes less than 1000 ( 168 primes )*/ const long long p [ ] = { 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73, 79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157, 163,167,173,179,181,191,193,197,199,211,223,227,229,233,239, 241,251,257,263,269,271,277,281,283,293,307,311,313,317,331, 337,347,349,353,359,367,373,379,383,389,397,401,409,419,421, 431,433,439,443,449,457,461,463,467,479,487,491,499,503,509, 521,523,541,547,557,563,569,571,577,587,593,599,601,607,613, 617,619,631,641,643,647,653,659,661,673,677,683,691,701,709, 719,727,733,739,743,751,757,761,769,773,787,797,809,811,821, 823,827,829,839,853,857,859,863,877,881,883,887,907,911,919, 929,937,941,947,953,967,971,977,983,991,997 }; const size_t n = sizeof( p ) / sizeof ( p[ 0 ] ); if ( a < p[ n - 1 ] + 1) return std::binary_search ( p, p + n , a ); if ( std::find_if ( p, p + n, std::not1 ( std::bind1st ( std::modulus< long long >( ), a ))) != p + n ) return false; const size_t k = a < 9006401LL ? 3 : a < 10403641LL ? 4 : a < 42702661LL ? 5 : a < 1112103541LL ? 6 : 7; for ( size_t j = 0; j < k; ++ j ) if ( powmod ( p[ j ], a - 1, a ) != 1 ) return false; return true; } /* * returns a sorted vector of all primes less than or equal to n * maximum adj diff of all primes less than 1e5 is 72 ( 114 for 1e6 ) */ template < typename value_type > std::vector < value_type > get_primes ( const value_type n ) { #ifdef BUG ScopeTimer scope_timer ( "std::vector < value_type > get_primes ( const value_type n )" ); #endif // typedef typename std::vector < value_type >::iterator iterator; std::vector < value_type > primes; for ( value_type k = 2 ; k <= n; ++ k ) if ( is_pseudo_prime ( k ) ) { // FIXME this is very stupid // const value_type sqrt_k = 1 + static_cast < value_type > ( sqrt ( k + 1 ) ); // iterator iend = upper_bound ( primes.begin( ), primes.end( ), sqrt_k ); // if ( find_if ( primes.begin( ), iend, std::not1 ( std::bind1st ( std::modulus< value_type >( ), k ) ) ) != iend ) // continue; primes.push_back ( k ); } return primes; } template < class T > inline std::vector < std::pair < T, size_t > > get_prime_fact( T a ) { std::vector< std::pair < T, size_t > >fac; for ( T k = 2; a > 1; ++ k ) if ( ! ( a % k ) ) // no need to check if k is prime { size_t m = 0; for ( ; ! ( a % k ) ; ++m, a/= k ) ; fac.push_back ( std::pair < T, size_t > ( k, m ) ); } return fac; } template < class T > /* prime factorization */ std::vector < std::pair < T, size_t > > get_prime_fac( T a ) { std::vector < std::pair < T, size_t > > fac; size_t k = 0; while (!(a % 2)) { a /= 2; ++ k; } if ( k != 0 ) fac.push_back( std::make_pair(2, k)); k = 0; while (!(a % 3)) { a /= 3; ++ k; } if ( k != 0 ) fac.push_back( std::make_pair(3, k)); for ( T j = 5; j * j < a + 1 && a > 1 && j < a + 1; j += 4 ) { size_t k = 0; while (!(a % j)) { a /= j; ++ k; } if ( k != 0 ) fac.push_back( std::make_pair(j, k)); j += 2; k = 0; while (!(a % j)) { a /= j; ++ k; } if ( k != 0 ) fac.push_back( std::make_pair(j, k)); } if ( a > 1 ) fac.push_back( std::make_pair(a, 1)); return fac; } template < class T > /* generates all divisors from vector of prime factorization */ void gen_div( T a, std::vector < T > & divs, typename std::vector < std::pair < T, size_t > >::const_iterator iter, typename std::vector < std::pair < T, size_t > >::const_iterator last ) { if ( iter != last ) { auto next = iter + 1; const auto k = iter->second + 1; const auto prime = iter->first; for ( size_t j = 0; j < k; ++ j ) { gen_div(a, divs, next, last); a *= prime; } } else divs.push_back( a ); } template < class T > T n_choose_k ( T n, T k ) { if ( k > n ) return 0; const T lb = std::min ( k, n - k ) + 1; const T ub = n - lb + 1; T res = 1, j = 2; while ( n > ub && j < lb) { res *= n--; while ( j < lb and ! (res % j) ) res /= j++; } while ( n > ub ) res *= n--; return res; } /** * median calculator, using two heaps */ template < class InputIter > inline std::pair < typename InputIter::value_type, typename InputIter::value_type > median ( InputIter first, InputIter last ) { typedef typename InputIter::value_type value_type; typedef std::pair< value_type, value_type > result_type; /* * max_heap: * - the lower half of the elements * - the biggest of such elements is on the top */ std::vector < value_type > max_heap, min_heap; /* * comp argument to heap algorithm should provide * 'strict weak ordering'; in particular * std::not2 ( std::less < value_type > ) * does not have such a strict weak ordering; */ std::less < value_type > max_heap_comp; std::greater < value_type > min_heap_comp; if ( first == last ) /* corner case: empty vector */ throw std::runtime_error ( "median of an empty vector is undefined!" ); InputIter iter = first; max_heap.push_back ( * iter ); for ( ++iter ; iter != last; ++ iter ) if ( * iter < max_heap.front() ) { max_heap.push_back ( * iter ); std::push_heap ( max_heap.begin(), max_heap.end(), max_heap_comp ); if ( min_heap.size() + 1 < max_heap.size() ) { /* max_heap has got too large */ min_heap.push_back( max_heap.front() ); std::push_heap( min_heap.begin(), min_heap.end(), min_heap_comp ); std::pop_heap( max_heap.begin(), max_heap.end(), max_heap_comp ); max_heap.pop_back(); } } else { min_heap.push_back ( * iter ); std::push_heap ( min_heap.begin(), min_heap.end(), min_heap_comp ); if ( max_heap.size() + 1 < min_heap.size() ) { /* min_heap has got too large */ max_heap.push_back( min_heap.front() ); std::push_heap( max_heap.begin(), max_heap.end(), max_heap_comp ); std::pop_heap( min_heap.begin(), min_heap.end(), min_heap_comp ); min_heap.pop_back(); } } DEBUG( max_heap ); DEBUG( min_heap ); return min_heap.empty( ) /* corner case: ++first = last */ ? result_type ( *first, *first ) : result_type ( max_heap.size() < min_heap.size() ? min_heap.front() : max_heap.front(), min_heap.size() < max_heap.size() ? max_heap.front() : min_heap.front() ); } /* * geometry util * ---------------------------------------------- */ struct xyPoint { double x, y; xyPoint( const double & a = .0, const double & b = .0 ): x ( a ), y( b ) { }; }; struct xyCircle { xyPoint center; double radius; }; std::ostream & operator<< ( std::ostream & out, const xyPoint & p ) { out << '(' << p.x << ", " << p.y << ')'; return out; } std::istream & operator>> ( std::istream & ist, xyPoint & p ) { ist >> p.x >> p.y; return ist; } std::ostream & operator<< ( std::ostream & out, const xyCircle & o ) { out << "{(" << o.center.x << ", " << o.center.y << ") " << o.radius << '}'; return out; } std::istream & operator>> ( std::istream & ist, xyCircle & o ) { ist >> o.center.x >> o.center.y >> o.radius; return ist; } inline double cartesian_dist ( const xyPoint & a, const xyPoint & b ) { const double d = a.x - b.x; const double e = a.y - b.y; return std::sqrt ( d * d + e * e ); } class xyLine { public: xyLine ( const xyPoint & , const xyPoint & ); xyLine ( const double slope, const double intercept ); /* * 'signed' orthogonal distance; the sign is useful * to compare which side of the line the point is */ inline double orth_dist ( const xyPoint & ) const; private: double m_slope; double m_intercept; double m_normfac; /* normalization factor for orth_dist calc */ bool m_vertical; /* if the line is verticcal */ double m_xcross; /* x axis cross point for vertical line */ }; xyLine::xyLine ( const xyPoint & a, const xyPoint & b ) { if ( a.x == b.x ) /* vertical line */ { m_vertical = true; m_xcross = a.x; m_intercept = DOUBLE_NAN; m_slope = DOUBLE_INF; m_normfac = DOUBLE_NAN; } else { m_vertical = false; m_xcross = DOUBLE_NAN; m_slope = ( b.y - a.y ) / ( b.x - a.x ); m_intercept = a.y - m_slope * a.x; m_normfac = std::sqrt ( m_slope * m_slope + 1.0 ); } } xyLine::xyLine ( const double slope, const double intercept ): m_slope ( slope ), m_intercept ( intercept ) { m_vertical = false; m_xcross = DOUBLE_NAN; m_normfac = std::sqrt ( m_slope * m_slope + 1.0 ); } double xyLine::orth_dist ( const xyPoint & o ) const /* 'signed' orthogonal distance */ { if ( m_vertical ) return o.x - m_xcross; else return ( m_slope * o.x - o.y + m_intercept ) / m_normfac; } inline double triangle_area ( const xyPoint & a, const xyPoint & b, const xyPoint & c ) { const xyLine l ( a, b ); const double h = std::abs ( l.orth_dist ( c ) ); const double e = cartesian_dist ( a, b ); return h * e; } /* * operator<< overrides * ---------------------------------------------- */ namespace { /* helper function to output containers */ template < typename T > std::ostream & __output ( std::ostream & out, const T & a ) { typedef typename T::const_iterator const_iterator; out << "{ "; // does not work for 'pair' value type // std::copy ( a.begin( ), a.end( ), std::ostream_iterator < typename T::value_type > ( std::cout, ", " )); for ( const_iterator iter = a.begin(); iter != a.end(); ++ iter ) out << ( iter != a.begin( ) ? ", " : "" ) << *iter ; return out << " }"; } } template < typename key_type, typename value_type > std::ostream & operator<< ( std::ostream & out, const std::pair < key_type, value_type > & p) { out << "(" << p.first << ", " << p.second << ")"; return out; } template < typename T0, typename T1, typename T2 > std::ostream & operator<< ( std::ostream & out, const std::tuple < T0, T1, T2 > & t ) { out << "(" << std::get<0>(t) << ", " << std::get<1>(t) << ", " << std::get<2>(t) << ")"; return out; } template < typename key_type, typename value_type, typename comp > std::ostream & operator<< ( std::ostream & out, const std::map < key_type, value_type, comp > & m ) { return __output ( out, m ); } template < typename value_type, typename comp > std::ostream & operator<< ( std::ostream & out, const std::set < value_type, comp > & s ) { return __output ( out, s ); } template < typename value_type > std::ostream & operator<< ( std::ostream & out, const std::vector < value_type > & a ) { return __output ( out, a ); } template < typename value_type > std::ostream & operator<< ( std::ostream & out, const std::list < value_type > & a ) { return __output ( out, a ); } template < typename value_type > std::ostream & operator<< ( std::ostream & out, const std::vector < std::vector < value_type > > & a ) { typedef typename std::vector < std::vector < value_type > >::const_iterator const_iterator; for ( const_iterator iter = a.begin( ); iter != a.end( ); ++ iter ) out << '\n' << *iter ; return out; } /* * operator>> overrides * ---------------------------------------------- */ template < typename key_type, typename value_type > std::istream & operator>> ( std::istream & in, std::pair < key_type, value_type > & p) { in >> p.first >> p.second; return in; } template < typename T0, typename T1, typename T2 > std::istream & operator>> ( std::istream & fin, std::tuple < T0, T1, T2 > & t ) { fin >> std::get<0>(t) >> std::get<1>(t) >> std::get<2>(t); return fin; } template < typename value_type > std::istream & operator>> ( std::istream & in, std::vector < value_type > & a ) { typedef typename std::vector < value_type >::iterator iterator; if ( ! a.size( ) ) { size_t n; in >> n; a.resize( n ); } for ( iterator iter = a.begin(); iter != a.end(); ++ iter ) in >> * iter; return in; } /* * readin quick utilities * ---------------------------------------------- */ // template < typename value_type > // inline void readin ( std::vector < value_type > & a, size_t n = 0, std::istream & in = std::cin ) // { // // if ( ! n ) std::cin >> n; // if ( ! n ) in >> n ; // a.resize ( n ); // // std::cin >> a; // in >> a; // } // XXX consider removing // template < typename key_type, typename value_type > // inline void readin (std::vector < std::pair < key_type , value_type > > & a, size_t n = 0 ) // { // if ( !n ) std::cin >> n; // a.resize( n ); // std::cin >> a; // } /* * pair utility * ---------------------------------------------- */ /* * accumulate ( m.begin( ), m.end( ), pair < int, double> ( 0 , 0.0 ) , operator+ < int, double > ); * stackoverflow.com/questions/18640152 */ template < typename T1, typename T2 > inline std::pair < T1, T2 > operator+ ( const std::pair < T1, T2 > & a, const std::pair < T1, T2 > & b ) { return std::make_pair < T1, T2 > ( a.first + b.first, a.second + b.second ); } template < typename T1, typename T2 > inline std::pair < T1, T2 > operator- ( const std::pair < T1, T2 > & a, const std::pair < T1, T2 > & b ) { return std::make_pair < T1, T2 > ( a.first - b.first, a.second - b.second ); } // template < class T1, class T2, class BinaryOperation > // class Apply2nd : std::binary_function < typename std::pair < T1, T2 >, // typename std::pair < T1, T2 >, // typename BinaryOperation::result_type > namespace { /*! * helper template to do the work */ template < size_t J, class T1, class T2 > struct Get; template < class T1, class T2 > struct Get < 0, T1, T2 > { typedef typename std::pair < T1, T2 >::first_type result_type; static result_type & elm ( std::pair < T1, T2 > & pr ) { return pr.first; } static const result_type & elm ( const std::pair < T1, T2 > & pr ) { return pr.first; } }; template < class T1, class T2 > struct Get < 1, T1, T2 > { typedef typename std::pair < T1, T2 >::second_type result_type; static result_type & elm ( std::pair < T1, T2 > & pr ) { return pr.second; } static const result_type & elm ( const std::pair < T1, T2 > & pr ) { return pr.second; } }; } template < size_t J, class T1, class T2 > typename Get< J, T1, T2 >::result_type & get ( std::pair< T1, T2 > & pr ) { return Get < J, T1, T2 >::elm( pr ); } template < size_t J, class T1, class T2 > const typename Get< J, T1, T2 >::result_type & get ( const std::pair< T1, T2 > & pr ) { return Get < J, T1, T2 >::elm( pr ); } /* * graph utils * ---------------------------------------------- */ /* * Dijkstra :: single-source shortest path problem for * a graph with non-negative edge path costs, producing * a shortest path tree * en.wikipedia.org/wiki/Dijkstra's_algorithm */ template < typename DistType > void Dijekstra ( const size_t & source, const std::vector < std::list < size_t > > & adj, // adjacency list const std::vector < std::vector < DistType > > & edge_len, // pair-wise distance for adjacent nodes std::vector < DistType > & dist, // distance from the source std::vector < size_t > prev ) // previous node in the shortest path tree { // TODO } // TODO http://en.wikipedia.org/wiki/Shortest_path_problem // TODO Graph class, Weighted graph, ... /* * maximum cardinality matching in a bipartite graph * G = G1 ∪ G2 ∪ {NIL} * where G1 and G2 are partition of graph and NIL is a special null vertex * https://en.wikipedia.org/wiki/Hopcroft-Karp_algorithm */ class HopcroftKarp { public: HopcroftKarp ( const std::vector < std::list < size_t > > & adj, const std::vector < bool > & tag ); size_t get_npair ( ) { return npair; }; std::map < size_t, size_t > get_map ( ); private: bool mf_breadth_first_search ( ); // breadth first search from unpaired nodes in G1 bool mf_depth_first_search ( const size_t v ); // dfs w/ toggeling augmenting paths const std::vector < std::list < size_t > > & m_adj; // adjacency list for each node const std::vector < bool > & m_tag; // binary tag distinguishing partitions size_t npair; const size_t NIL; // special null vertex const size_t INF; // practically infinity distance std::vector < size_t > m_g1; // set of nodes with tag = true std::vector < size_t > m_dist; // dist from unpaired vertices in G1 std::vector < size_t > m_pair; }; std::map < size_t, size_t > HopcroftKarp::get_map ( ) { std::map < size_t, size_t > m; for ( size_t j = 0; j < m_pair.size( ); ++ j ) if ( m_pair[ j ] != NIL && m_tag[ j ]) m[ j ] = m_pair[ j ]; return m; } HopcroftKarp::HopcroftKarp ( const std::vector < std::list < size_t > > & adj, const std::vector < bool > & tag ): m_adj ( adj ), m_tag ( tag ), npair ( 0 ), NIL ( adj.size( )), INF ( adj.size( ) + 1 ), m_dist ( std::vector < size_t > ( adj.size( ) + 1, INF)), m_pair ( std::vector < size_t > ( adj.size( ), NIL )) // initially everything is paired with nil { assert ( m_adj.size() == m_tag.size() ); for ( size_t j = 0; j < tag.size( ); ++ j ) if ( tag[ j ] ) m_g1.push_back ( j ); while ( mf_breadth_first_search ( ) ) for ( std::vector < size_t >::const_iterator v = m_g1.begin( ); v != m_g1.end( ); ++ v ) if ( m_pair[ *v ] == NIL && mf_depth_first_search ( *v ) ) ++ npair; } bool HopcroftKarp::mf_breadth_first_search( ) { /* only nodes from g1 are queued */ std::queue < size_t > bfs_queue; /* initialize queue with all unpaired nodes from g1 */ for ( std::vector < size_t >::const_iterator v = m_g1.begin( ); v != m_g1.end( ); ++v ) if ( m_pair[ *v ] == NIL ) { m_dist[ *v ] = 0; bfs_queue.push ( *v ); } else m_dist[ *v ] = INF; m_dist[ NIL ] = INF; /* find all the shortest augmenting paths to node nil */ while ( ! bfs_queue.empty() ) { const size_t v = bfs_queue.front( ); bfs_queue.pop ( ); if ( m_dist[ v ] < m_dist[ NIL ] ) for ( std::list < size_t >::const_iterator u = m_adj[ v ].begin( ); u != m_adj[ v ].end( ); ++ u ) if ( m_dist[ m_pair[ * u ] ] == INF ) { m_dist[ m_pair[ * u ] ] = m_dist[ v ] + 1; bfs_queue.push ( m_pair[ * u ] ); } } return m_dist[ NIL ] != INF; } bool HopcroftKarp::mf_depth_first_search( const size_t v ) { if ( v == NIL ) return true; else { for ( std::list < size_t >::const_iterator u = m_adj[ v ].begin( ); u != m_adj[ v ].end( ); ++ u ) if ( m_dist[ m_pair[ *u ] ] == m_dist[ v ] + 1 && mf_depth_first_search( m_pair[ *u ] )) { /* * there is an augmenting path to nil from m_pair[ *u ] * and hence there is an augmenting path from v to u and * u to to nil; therefore v and u can be paired together */ m_pair [ *u ] = v; m_pair [ v ] = *u; return true; } m_dist[ v ] = INF; return false; } } /** * lazy all pairs shortest path in a tree with only one BFS * test case: 'book of evil' * codeforces.com/problemset/problem/337/D */ class All_Pairs_Tree_Shortest_Path { public: All_Pairs_Tree_Shortest_Path( const std::vector< std::list < size_t > > & adj ): n( adj.size( ) ), depth( std::vector < size_t > ( n, All_Pairs_Tree_Shortest_Path::INF ) ), parent( std::vector < size_t > ( n ) ), dist( std::vector < std::vector < unsigned short > > ( n ) ) { /* perform bfs from root node '0' and assign depth to each node */ /* XXX probably would be worth to set the root as node with highest degree */ std::queue< size_t > bfs_queue; bfs_queue.push( 0 ); depth[ 0 ] = 0; parent[ 0 ] = 0; while ( ! bfs_queue.empty( ) ) { const size_t u = bfs_queue.front( ); bfs_queue.pop( ); for ( std::list< size_t >::const_iterator j = adj[ u ].begin( ); j != adj[ u ].end( ); ++ j ) if ( depth[ u ] + 1 < depth[ *j ] ) { depth[ *j ] = depth[ u ] + 1; parent[ *j ] = u; bfs_queue.push( *j ); } } /* adjust pair-wise distance to zero along the diagonal */ for ( size_t j = 1; j < n; ++ j ) dist[ j ].resize( j, All_Pairs_Tree_Shortest_Path::INF ); } /* interface object function as to lazily look-up distances */ size_t operator( )( const size_t u, const size_t v ) { if ( u == v ) return 0; else if ( u < v) return (*this)( v, u ); else if ( dist[ u ][ v ] == All_Pairs_Tree_Shortest_Path::INF ) { if ( depth[ u ] < depth[ v ] ) /* u is in a lower level than v */ dist[ u ][ v ] = 1 + (*this)( u, parent[ v ]); else if ( depth[ v ] < depth[ u ] ) /* v is in a lower level than u */ dist[ u ][ v ] = 1 + (*this)( parent[ u ], v ); else /* u and v are at the same depth */ dist[ u ][ v ] = 2 + (*this)( parent[ u ], parent[ v ] ); } return dist[ u ][ v ]; } /* TODO populate; a method which populates pair-wise distances * and returns the matrix */ private: /* * constant infinity value for initializing distances * even though this is private it will be assigned outside of the class */ static const unsigned short INF; const size_t n; /* numbert of nodes in the tree */ std::vector < size_t > depth; /* distance to root node '0' */ std::vector < size_t > parent; /* parent of each node with root node '0' */ std::vector < std::vector < unsigned short > > dist; /* pair-wise shortest path distance */ }; const unsigned short All_Pairs_Tree_Shortest_Path::INF = std::numeric_limits< unsigned short >::max( ); /* * data-structure utility * ---------------------------------------------- */ template < class T, class Comp = std::less< T > > class Heap /* less< T > --> max-heap */ { typedef T value_type; typedef typename std::vector < value_type >::size_type size_type; public: /* * stackoverflow.com/questions/10387751 * possible work-around: a memebr pointer to m_val * TODO static/friend heapify ( val, & heap ) XXX O( n ) ?! * TODO implement insert iterator */ Heap(): m_val( std::vector < value_type >() ), m_comp( Comp() ) {} template < class InputIter > Heap ( InputIter first, InputIter last ): m_val ( std::vector < value_type > ( ) ), m_comp( Comp( ) ) { for ( ; first != last ; ++ first ) m_val.push_back ( * first ); std::make_heap( m_val.begin( ), m_val.end( ), m_comp ); } /*! * to avoid destroying heap property, front( ) * should always return a 'const' reference */ inline const value_type & front( ) const { return m_val.front( ); } inline bool empty( ) const { return m_val.empty( ); } inline size_type size( ) const { return m_val.size( ); } inline void push ( const value_type & a ) { m_val.push_back( a ); std::push_heap( m_val.begin( ), m_val.end( ), m_comp ); } inline void pop( ) { std::pop_heap ( m_val.begin( ), m_val.end( ), m_comp ); m_val.pop_back( ); } // inline void swap( Heap< T, Comp> & other ) { m_val.swap( other.m_val ) }; // void sort( ) { std::sort_heap ( m_val.begin( ), m_val.end( ), m_comp ); } // template < class X, class Y > // friend std::ostream & operator<<( std::ostream & out, const Heap < X, Y> & heap ); private: std::vector < value_type > m_val; const Comp m_comp; }; /* * boost.org/doc/libs/1_54_0/libs/smart_ptr/shared_ptr.htm */ #if 1 < 0 template < class Type > class shared_ptr { typedef Type value_type; public: explicit shared_ptr ( value_type * p = NULL ) : ptr ( p ), count ( new size_t ( 1U ) ) { } shared_ptr ( const shared_ptr < value_type > & sp ): ptr ( sp.ptr ), count ( sp.count ) { ++ * count; } ~ shared_ptr ( ) { release( ); } bool operator== ( const shared_ptr < value_type > & sp ) { return ptr == sp.ptr; } bool operator!= ( const shared_ptr < value_type > & sp ) { return ptr != sp.ptr; } shared_ptr < value_type > & operator= ( const shared_ptr < value_type > & sp ) { if ( this != & sp && ptr != sp.ptr ) { release( ); ptr = sp.ptr; count = sp.count; ++ * count; } return * this; } value_type * operator-> ( ) { return ptr ; } value_type & operator* ( ) { return *ptr ; } const value_type * operator-> ( ) const { return ptr ; } const value_type & operator* ( ) const { return *ptr; } void swap ( shared_ptr < value_type > & sp ) { if ( this != &sp && ptr != sp.ptr ) { std::swap ( ptr, sp.ptr ); std::swap ( count, sp.count ); } } private: void release ( ) { /* stackoverflow.com/questions/615355 */ -- * count; if ( ! * count ) { delete count; delete ptr; count = NULL; ptr = NULL; } } value_type * ptr; size_t * count; }; #endif /*! * union find data structure with * - lazy unions * - union by rank * - path compression */ class UnionFind { public: UnionFind( const size_t n ): parent ( std::vector < size_t > ( n ) ), /* initialize each node as its own */ rank ( std::vector < size_t > ( n, 0 )) /* parent and set all the ranks to 0 */ { for ( size_t j = 0; j < n; ++ j ) parent[ j ] = j ; } inline size_t find( const size_t s ) { /* * perform path compresion and add shortcut * if parent[ s ] is not a root node */ const size_t p = parent[ s ]; return parent[ p ] == p ? p : parent[ s ] = find( p ) ; } inline void lazy_union ( size_t i, size_t j ) { /* unions should be done on root nodes */ i = find( i ); j = find( j ); if ( i != j ) { if ( rank [ i ] < rank[ j ] ) parent[ i ] = j; else { parent[ j ] = i; rank[ i ] += rank[ i ] == rank[ j ]; } } } private: std::vector < size_t > parent; std::vector < size_t > rank; }; // TODO XXX // template < class NumType > // unsigned num_hash_func ( const NumType & a ) // { // // XXX what will happen in case of overflow? // return static_cast < unsigned > ( a % 9973 ) % 9973 ; // } /* * XXX: HashMap: map< Key, T > data [ 9973 ] * data [ num_hash_func ( key ) ][ key ] */ /* * testing util * ---------------------------------------------- */ // TODO add a preprocessor which automatically includes the funciton name, or __line__ // and disables if not in debug mode /* prints the life length of the object when it goes out of scope */ class ScopeTimer { public: ScopeTimer ( const std::string & msg = "" ): tic ( clock ( )), m_msg( msg ) { }; ~ ScopeTimer ( ) { const clock_t toc = clock(); const uint64 dt = 1000L * ( toc - tic ) / CLOCKS_PER_SEC; const uint64 mil = dt % 1000L; const uint64 sec = ( dt / 1000L ) % 60L; const uint64 min = ( dt / 60000L ) % 60L; const uint64 hrs = ( dt / 3600000L ); std::cout << '\n' << m_msg << "\n\telapsed time: "; if ( hrs ) std::cout << hrs << " hrs, "; if ( min ) std::cout << min << " min, "; if ( sec ) std::cout << sec << " sec, "; std::cout << mil << " mil-sec\n"; } private: typedef unsigned long long int uint64; const clock_t tic; const std::string m_msg; }; class RandInt { public: RandInt ( int a = 0, int b = 100 ): m ( a ), f ( static_cast < double > ( b - a ) / RAND_MAX ) { } inline int operator() ( ) { return m + std::ceil ( f * rand( ) ); } private: const int m; const double f; }; class RandDouble { public: RandDouble ( double a = 0.0, double b = 1.0 ): m ( a ), f ( ( b - a ) / RAND_MAX ) { } inline double operator() ( ) { return m + f * rand( ); } private: const double m, f; }; class Noisy { public: Noisy ( std::string str ): msg ( str ) { std::cout << " Noisy ( " << msg << " )\t@ " << this << std::endl; } ~Noisy ( ) { std::cout << "~Noisy ( " << msg << " )\t@ " << this << std::endl; } void beep ( ) { std::cout << " beep ( " << msg << " )\t@ " << this << std::endl; } void beep ( ) const { std::cout << " const beep ( " << msg << " )\t@ " << this << std::endl; } private: const std::string msg; }; DECLARE ( Noisy ); /* * ---------------------------------------------- * ---------------------------------------------- */ /* * -- @@@ ------------------------------------------------- */ using namespace std; // struct Comp // { // // bool operator() ( const pair < int, int > & x, const pair < int, int > & y ) // // { // // return abs( x.first ) + abs( x.second ) < abs( y.first ) + abs( y.second ); // // } // // // bool operator( ) ( const xyCircle & a, const xyCircle & b ) // // { // // return a.radius < b.radius; // // } // // // bool operator( ) ( const pair < size_t, size_t > & pr, const size_t & x ) // { // return pr.second < x; // } // }; inline int get_gain( const vector < int > & xs, const size_t l, const size_t r ) { const size_t len = xs.size() - l - r; return len & 1 ? xs[ l + len / 2 ] : 0; } void c388() { size_t n; cin >> n; vector < vector < int > > xs( n ); cin >> xs; const size_t k = accumulate( begin(xs), end(xs), 0, []( const size_t acc, const vector < int > & row ) { return acc + row.size(); }); DEBUG( k ); const int acc = accumulate( begin(xs), end(xs), 0, []( const int acc, const vector < int > & a ) { return accumulate(begin(a), end(a), acc); }); DEBUG( acc ); // how much has it been taken from left & right vector < size_t > left( n, 0 ), right( n, 0 ); int left_acc = 0; for ( size_t j = 0; j < k; ++ j ) { size_t imax = 0; while ( xs[imax].size() == left[imax] + right[imax] ) ++ imax; int val = get_gain( xs[imax], left[imax], right[imax] ); for ( size_t i = imax + 1; i < n; ++ i ) if ( left[ i ] + right[ i ] < xs[ i ].size() ) { const auto g = get_gain( xs[i], left[i], right[i] ); if ( val < g ) { val = g; imax = i; } } if ( j & 1 ) // from right ++ right[ imax ]; else left_acc += xs[ imax ][ left[ imax ] ++ ]; } cout << left_acc << ' ' << acc - left_acc; } int main( const int argc, char * argv []) { c388(); // cout << setprecision( 15 ) << b68(); return EXIT_SUCCESS; } /** * mislav.uniqpath.com/2011/12/vim-revisited/ * set encoding=utf-8 * %s/\(.\{60,70\}\) /\1\r/gc * %s/ / /gc * %s/10\([0-9]\{1,2\}\)/10^\1/gc */
9
CPP
#include <bits/stdc++.h> int main() { long long sa = 0, sb = 0; int n, s, x, m = 0, tar[(110)]; using namespace std; cin >> n; while (n--) { cin >> s; for (int i = 1; i <= s; i++) { cin >> x; if (i * 2 - 1 < s) sa += x; if (i * 2 - 1 == s) tar[m++] = x; if (i * 2 - 1 > s) sb += x; } } sort(tar, tar + m); int g = 0; for (int i = m - 1; i >= 0; i--) { if (!g) { sa += tar[i]; g = 1; } else { sb += tar[i]; g = 0; } } cout << sa << ' ' << sb << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; vector<int> v; int main(int argc, char const *argv[]) { int n; cin >> n; int a = 0, b = 0; for (int i = 0; i < n; i++) { int x; cin >> x; for (int j = 0; j < x; j++) { int aa; cin >> aa; if (x % 2 == 1 and j == x / 2) v.push_back(aa); else if (j < x / 2) a += aa; else b += aa; } } sort((v).begin(), (v).end()); int at = 0; for (int i = v.size() - 1; i >= 0; i--) { if (!at) a += v[i]; else b += v[i]; at = (1 - at); } cout << a << " " << b << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int N = 119; int n; vector<int> v; int Ciel = 0, Jiro = 0; int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n; for (int i = 0; i < n; i++) { int m; cin >> m; int a[N] = {}; for (int i = 1; i <= m; i++) cin >> a[i]; if (m % 2) { v.push_back(a[(m + 1) / 2]); for (int i = 1; i <= m; i++) { if (i < (m + 1) / 2) Ciel += a[i]; else if (i > (m + 1) / 2) Jiro += a[i]; } } else { for (int i = 1; i <= m; i++) { if (i <= m / 2) Ciel += a[i]; else Jiro += a[i]; } } } cerr << Ciel << ' ' << Jiro << endl; sort(v.begin(), v.end(), greater<int>()); for (int i = 0; i < v.size(); i++) { if (i % 2) Jiro += v[i]; else Ciel += v[i]; } cout << Ciel << ' ' << Jiro << endl; return 0; }
9
CPP
#include <bits/stdc++.h> using namespace std; vector<vector<int>> piles; int main() { int n; cin >> n; vector<int> mcards; int ceil = 0; int jiro = 0; for (int i = 0; i < n; i++) { int cn; cin >> cn; for (int j = 0; j < cn; j++) { int x; cin >> x; if (cn % 2 == 1 && cn / 2 == j) { mcards.push_back(x); continue; } if (j < cn / 2) { ceil += x; continue; } jiro += x; } } sort(mcards.rbegin(), mcards.rend()); for (size_t t = 0; t < mcards.size(); t++) { if (t % 2 == 0) ceil += mcards[t]; else jiro += mcards[t]; } cout << ceil << " " << jiro << endl; }
9
CPP
#include <bits/stdc++.h> using namespace std; const int maxx = 120; int l[maxx], r[maxx], s1 = 0, s2 = 0, maj[maxx][maxx]; vector<int> v; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; for (int j = 0; j < x; j++) { int y; cin >> y; if (j == x / 2 && x % 2 == 1) v.push_back(y); if (j < (x / 2)) s1 += y; if (x % 2 == 1 && j > (x / 2)) s2 += y; if (x % 2 == 0 && j >= (x / 2)) s2 += y; } } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); for (int i = 0; i < v.size(); i++) if (i % 2) s2 += v[i]; else s1 += v[i]; cout << s1 << " " << s2 << endl; return 0; }
9
CPP