solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int main() { int n, m; scanf("%d", &n); long a; long long c[1000000]; long long b1[21]; long long b2[21]; long long b3[21]; long long t[21]; t[0] = 1; b1[0] = 0; for (int i = 1; i <= n; i++) t[i] = t[i - 1] << 1; vector<vector<long> > v; for (int i = 0, _n = t[n]; i < _n; i++) { scanf("%ld", &a); vector<long> v1; v1.push_back(a); v1.push_back(i); v.push_back(v1); } sort(v.begin(), v.end()); long long s = 0; for (int j = 1; j <= n; j++) { b2[j] = 0; for (int i = 0; i < t[n - j]; i++) { c[i] = t[j - 1]; } for (int i = 0; i < t[n]; i++) { if ((v[i][1] >> j) << 1 == v[i][1] >> (j - 1)) c[v[i][1] >> j] -= 1; else b2[j] += c[v[i][1] >> j]; } s = s + b2[j]; } for (int i = 0; i < v.size(); i++) v[i][1] = t[n] - 1 - v[i][1]; sort(v.begin(), v.end()); for (int j = 1; j <= n; j++) { b3[j] = 0; for (int i = 0; i < t[n - j]; i++) { c[i] = t[j - 1]; } for (int i = 0; i < t[n]; i++) { if ((v[i][1] >> j) << 1 == v[i][1] >> (j - 1)) c[v[i][1] >> j] -= 1; else b3[j] += c[v[i][1] >> j]; } b1[j] = b2[j] + b3[j]; } scanf("%d", &m); int q; for (int i = 0, _n = m; i < _n; i++) { scanf("%d", &q); for (int j = 1; j <= q; j++) { s = s + b1[j] - 2 * b2[j]; b2[j] = b1[j] - b2[j]; } printf("%I64d\n", s); } return 0; }
3
#include <bits/stdc++.h> using namespace std; char str[100005]; int main() { int n, k; scanf("%d%d", &n, &k); getchar(); scanf("%s", str + 1); int i, cnt; for (i = 1, cnt = 0; i <= n && cnt < k; i++) { if (i % 2) { if (str[i] == '4' && str[i + 1] == '4' && str[i + 2] == '7') break; if (str[i] == '4' && str[i + 1] == '7' && str[i + 2] == '7') break; } if (str[i] == '4' && str[i + 1] == '7') { if (i % 2 && str[i + 1] != '4') { cnt++; str[i + 1] = '4'; } else if (i % 2 == 0 && str[i] != '7') { cnt++; str[i] = '7'; i -= 2; } } } if ((k - cnt) % 2) { if (str[i + 1] == '4') str[i + 1] = '7'; else str[i + 1] = '4'; } printf("%s\n", str + 1); return 0; }
2
#include <bits/stdc++.h> using namespace std; template <class T> T sqr(T a) { return a * a; } template <class T> T abs(T x) { if (x < 0) return -x; return x; } const double eps = 1e-8; const double pi = acos(-1.0); char str[200010]; int main() { int n, i, f, a, b; while (1 == scanf("%d", &n)) { f = a = b = 0; scanf("%s", str); for (i = 0; i < n; i++) { if (str[i] == 'F') f++; else if (str[i] == 'A') a++; else b++; } if (b == 1) printf("%d\n", b); else if (b) printf("%d\n", 0); else printf("%d\n", a); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, m, q; int u[500005], v[500005], wt[500005], par[500005], waqt = 0, waqtForNodes[500005], parr[500005]; map<int, bool> M; vector<pair<int, int> > offlineStoredQueries[500005]; vector<int> eg[500005]; int find2(int x) { if (waqtForNodes[x] != waqt) { waqtForNodes[x] = waqt; parr[x] = par[x]; } if (parr[x] != x) parr[x] = find2(parr[x]); return parr[x]; } int find(int x) { if (par[x] != x) par[x] = find(par[x]); return par[x]; } void Union(int x, int y) { int xset = find(par[x]); int yset = find(par[y]); if (xset != yset) par[xset] = yset; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> u[i] >> v[i] >> wt[i]; eg[wt[i]].push_back(i); } for (int i = 1; i <= n; i++) par[i] = i; cin >> q; for (int i = 0; i < q; i++) { int numEdges; cin >> numEdges; for (int j = 0; j < numEdges; j++) { int edgeNo; cin >> edgeNo; int weight = wt[edgeNo]; offlineStoredQueries[weight].push_back(make_pair(edgeNo, i)); } } for (int weight = 1; weight < 500005; weight++) { for (int i = 0; i < offlineStoredQueries[weight].size(); i++) { if (i == 0 || offlineStoredQueries[weight][i - 1].second != offlineStoredQueries[weight][i].second) waqt++; int edgeNo = offlineStoredQueries[weight][i].first; int queryNo = offlineStoredQueries[weight][i].second; if (find2(u[edgeNo]) == find2(v[edgeNo])) M[queryNo]; parr[find2(u[edgeNo])] = parr[find2(v[edgeNo])]; } for (int i = 0; i < eg[weight].size(); i++) { Union(u[eg[weight][i]], v[eg[weight][i]]); } } for (int i = 0; i < q; i++) { if (M.count(i) != 0) cout << "NO" << endl; else cout << "YES" << endl; } }
3
#include <iostream> #include <cmath> #include <cstdio> #include <cstring> using namespace std; const int MAXN = 1010; const double INF = 1e10; struct circle { double x, y, r; }; circle c[MAXN]; double xs, ys, xt, yt, d[MAXN], G[MAXN][MAXN]; int n; bool used[MAXN]; double dist(circle a, circle b) { return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)) - a.r - b.r; } void dijkstra(int s) { fill(d+1, d+1+n, INF); memset(used, false, sizeof(used)); d[s] = 0; while (true) { int v = -1; for (int u=1; u<=n; u++) if (!used[u] && (v == -1 || d[u] < d[v])) v = u; if (v == -1) break; used[v] = true; for (int u=1; u<=n; u++) if (d[v] + G[v][u] < d[u]) d[u] = d[v] + G[v][u]; } } int main() { cin >> xs >> ys >> xt >> yt; c[1] = circle{xs, ys, 0}; c[2] = circle{xt, yt, 0}; cin >> n; for (int i=1; i<=n; i++) cin >> c[i+2].x >> c[i+2].y >> c[i+2].r; n += 2; for (int i=1; i<=n; i++) { for (int j=1; j<=n; j++) { if (i == j) continue; G[i][j] = dist(c[i], c[j]); if (G[i][j] < 0) G[i][j] = 0; } } dijkstra(1); printf("%.10f\n", d[2]); return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, a[500005], b[500005], dd[500005]; vector<int> order; struct IntervalTree { pair<int, int> t[500005 << 2]; void update(int l, int r, int id, int x, int val) { if (l == r) { t[id] = make_pair(val, x); return; } if (x <= (r + l) / 2) update(l, (r + l) / 2, id * 2, x, val); else update((r + l) / 2 + 1, r, id * 2 + 1, x, val); t[id] = max(t[id * 2], t[id * 2 + 1]); } pair<int, int> getMax(int l, int r, int id, int x, int y) { if (l > y || r < x) return make_pair(0, 0); if (l >= x && r <= y) return t[id]; pair<int, int> a = getMax(l, (r + l) / 2, id * 2, x, y); pair<int, int> b = getMax((r + l) / 2 + 1, r, id * 2 + 1, x, y); return max(a, b); } } t; void DFS(int u) { dd[u] = 1; t.update(1, n, 1, u, 0); if (b[u] != n + 1 && !dd[b[u]]) DFS(b[u]); while (1) { auto z = t.getMax(1, n, 1, 1, a[u] - 1); if (z.first > u) DFS(z.second); else break; } order.push_back(u); } int res[500005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); if (fopen("INP.TXT", "r")) { freopen("INP.TXT", "r", stdin); } cin >> n; for (int i = 1; i <= n; i++) b[i] = n + 1; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i] != -1) b[a[i]] = i; else a[i] = n + 1; } for (int i = 1; i <= n; i++) t.update(1, n, 1, i, b[i]); for (int i = 1; i <= n; i++) if (!dd[i]) DFS(i); int cnt = 0; for (auto u : order) res[u] = ++cnt; for (int i = 1; i <= n; i++) cout << res[i] << ' '; }
5
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; struct Widget { string name; int hor; int border; int spacing; long long first, second; bool calculated; vector<string> nxt; Widget() { border = 0; spacing = 0; first = second = 0; calculated = false; } }; bool operator<(const Widget& w1, const Widget& w2) { return w1.name < w2.name; } int n; char s[1000]; vector<Widget> a; set<string> used; Widget& getWidget(string name) { for (int i = 0, _n = ((int)((a).size())); i < _n; ++i) if (a[i].name == name) return a[i]; throw 1; return a[(int)((a).size())]; } void calculate(Widget& w) { if (w.calculated || !(int)((w.nxt).size())) return; long long X = 0, Y = 0; long long mX = 0, mY = 0; for (int i = 0, _n = ((int)((w.nxt).size())); i < _n; ++i) { Widget& c = getWidget(w.nxt[i]); calculate(c); X += c.first, Y += c.second; mX = max(mX, c.first); mY = max(mY, c.second); } if (w.hor == 1) { w.first = X + w.border * 2 + w.spacing * ((int)((w.nxt).size()) - 1); w.second = mY + w.border * 2; } else { w.first = mX + w.border * 2; w.second = Y + w.border * 2 + w.spacing * ((int)((w.nxt).size()) - 1); } w.calculated = true; } void solution() { scanf("%d\n", &n); for (int it = 0, _n = (n); it < _n; ++it) { gets(s); for (int i = 0, _n = (strlen(s)); i < _n; ++i) if (s[i] == '(' || s[i] == ')' || s[i] == '.') s[i] = ' '; if (s[0] == 'W') { char name[11]; int first, second; sscanf(s, "Widget %s %d,%d ", name, &first, &second); Widget w; w.name = string(name); w.hor = -1; w.first = first; w.second = second; a.push_back(w); } else if (s[0] == 'V') { char name[11]; sscanf(s, "VBox %s", name); Widget w; w.name = string(name); w.hor = 0; a.push_back(w); } else if (s[0] == 'H') { char name[11]; sscanf(s, "HBox %s", name); Widget w; w.name = string(name); w.hor = 1; a.push_back(w); } else { char name[11]; char op[12]; char param[11]; int first, second; sscanf(s, "%s %s %s", name, op, param); Widget& w = getWidget(string(name)); if (op[0] == 'p') w.nxt.push_back(string(param)); else if (op[4] == 's') w.spacing = atoi(param); else w.border = atoi(param); } } for (int i = 0, _n = ((int)((a).size())); i < _n; ++i) calculate(a[i]); sort((a).begin(), (a).end()); for (int i = 0, _n = ((int)((a).size())); i < _n; ++i) printf("%s %I64d %I64d\n", a[i].name.c_str(), a[i].first, a[i].second); } int main() { solution(); return 0; }
2
#include <bits/stdc++.h> const int N = 100141; using namespace std; int n, m, fn, value, cnt, c = 0; long long dp[N], f[N], mod = 1e9 + 7; vector<int> a; vector<int> lk; bool lucky(int x) { while (x > 0) { if (x % 10 != 7 && x % 10 != 4) return 0; x /= 10; } return 1; } long long binpow(long long a, long long b) { a %= mod; long long res = 1; while (b) { if (b % 2) res = res * a % mod; a = a * a % mod; b /= 2; } return res; } long long C(int kx, int nx) { if (nx == 0 || kx > nx || kx < 0) return 0; long long fq = binpow(f[kx] * f[nx - kx] % mod, mod - 2) % mod; fq *= f[nx] % mod; return fq % mod; } void fill_fact() { f[0] = 1; f[1] = 1; for (int i = 2; i <= N - 3; i++) { f[i] = f[i - 1] * i; f[i] %= mod; } } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); ; fill_fact(); cin >> n >> m; fn = n; a.resize(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort((a).begin(), (a).end()); for (int i = 0; i < n; i++) { value = a[i]; if (lucky(value)) { c = 1; while (a[i] == a[i + 1] && i + 1 < n) { c++; i++; } lk.push_back(c); fn -= c; } } for (int j = 0; j < (int)(lk).size(); j++) { cnt = lk[j]; for (int i = m; i >= 1; i--) { if (!dp[i]) continue; dp[i + 1] += (long long)(dp[i] * cnt) % mod; dp[i + 1] %= mod; } dp[1] += cnt; dp[1] %= mod; } long long ans = C(m, fn); for (int j = 1; j <= m; j++) { if (fn < m - j) continue; long long R = max((long long)1, C(m - j, fn)) % mod; R *= dp[j]; R %= mod; ans += R; ans %= mod; } cout << ans << "\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, x; cin >> n; set<long long> st; while (1) { if (st.find(n) != st.end()) break; st.insert(n); n++; while (n % 10 == 0) { n /= 10; } } cout << st.size() << "\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, k, i; cin >> n; char a[110]; cin >> a; for (i = 0; i < n; i++) { if (a[i] == '8') { k = i; break; } } if (n - i < 11) cout << "NO" << endl; else cout << "YES" << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1e4 + 7; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; int n; long long a[10], b[10]; bool vis[10]; int main() { long long sum = 0, ans = 0; scanf("%d", &n); for (int i = 0; i < n; ++i) scanf("%lld", &a[i]); sort(a, a + n); for (int i = 0; i < n; ++i) { long long res = a[i]; for (long long j = 2; j * j <= a[i]; ++j) while (res % j == 0) { b[i]++; res /= j; } if (res > 1) b[i]++; if (b[i] > 1) ans += b[i]; } int pos, cnt; for (int i = n - 1; i >= 0; --i) { if (!vis[i]) sum++; while (1) { cnt = 0; for (int j = i - 1; j >= 0; --j) { if (!vis[j] && a[i] % a[j] == 0 && b[j] > cnt) { cnt = b[j]; pos = j; } } if (cnt) { vis[pos] = 1; a[i] /= a[pos]; ans -= b[pos] - 1; } else break; } } ans += sum; if (sum > 1) ans++; printf("%lld\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; typedef pair<int, int> P; #define rep(i, n) for (int i = 0; i < n; i++) #define int long long signed main(signed argc, char* argv[]) { ios::sync_with_stdio(false); cin.tie(nullptr); int n,m; cin>>n>>m; cout<<n*m<<endl; }
0
#include <cstdio> int main(){ int X,Y,Z; scanf("%d %d %d",&X,&Y,&Z); printf("%d %d %d\n",Z,X,Y); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 5e5 + 7; const int MAXM = 1e7 + 9; const int P = 1e9 + 7; int isp[MAXM], pri[MAXM / 10]; int S[MAXM], _2[MAXN], a[MAXN], tot[MAXM], g[MAXM], cnt, mu[MAXM]; bool vis[MAXM]; inline void upd(int &a) { a += (a >> 31) & P; } int n, lim, ans; void sieve(const int n) { mu[1] = 1; for (int i = 2; i <= n; ++i) { if (!isp[i]) { pri[++cnt] = i; mu[i] = -1; } for (int j = 1; j <= cnt && i * pri[j] <= n; ++j) { isp[i * pri[j]] = 1; if (i % pri[j] == 0) { mu[i * pri[j]] = 0; break; } else mu[i * pri[j]] = -mu[i]; } } } int main() { for (int i = *_2 = 1; i <= 500000; ++i) upd(_2[i] = _2[i - 1] * 2 - P); scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), ++tot[a[i]]; lim = *max_element(a + 1, a + n + 1); sieve(lim); for (int i = 1; i <= cnt; ++i) { for (int j = lim / pri[i]; j; --j) { tot[j] += tot[j * pri[i]]; } } for (int i = 1; i <= lim; ++i) g[i] = tot[i] * mu[i]; for (int i = 1; i <= cnt; ++i) { for (int j = 1; j * pri[i] <= lim; ++j) { g[j * pri[i]] += g[j]; } } for (int i = 1; i <= lim; ++i) S[i] = _2[tot[i]] - 1; for (int i = cnt; i; --i) { for (int j = 1; j * pri[i] <= lim; ++j) { upd(S[j] -= S[j * pri[i]]); } } for (register int i = lim; i > 1; --i) { ans = (ans + 1ll * S[i] * g[i]) % P; } printf("%d\n", ans); return 0; }
5
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:64000000") using namespace std; void __never(int a) { printf("\nOPS %d", a); } char S[1024]; int i = 0, ans = 0; string read_token() { string re; while (S[i] >= 'A' && S[i] <= 'Z') re.push_back(S[i++]); return re; } map<string, int> parse() { map<string, int> re; string cur = read_token(); re[cur]++; if (S[i] == '.') { i++; return re; } { if (!(S[i] == ':')) { __never(44); cout.flush(); cerr.flush(); abort(); } }; i++; while (1) { map<string, int> tmp = parse(); if (tmp.find(cur) != tmp.end()) ans += tmp[cur]; map<string, int>::iterator it; for (it = tmp.begin(); it != tmp.end(); it++) re[it->first] += it->second; if (S[i] == ',') i++; else if (S[i] == '.') { i++; break; } else { if (!(false)) { __never(57); cout.flush(); cerr.flush(); abort(); } }; } return re; } void sol() { parse(); cout << ans; } int main() { cin >> S; sol(); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string str; cin >> str; map<string, int> mp; for (int i = 1; i < n; i++) { string tt = ""; tt += str[i - 1]; tt += str[i]; mp[tt]++; } int cnt = -1; string ans; for (auto x : mp) { if (cnt < x.second) { cnt = x.second; ans = x.first; } } cout << ans << endl; }
2
#include <bits/stdc++.h> using namespace std; int main() { double a, b; cin >> a >> b; double len = a + b; int k = (a + b) / (b + b); if (k < 1) puts("-1"); else printf("%.12lf\n", len / 2.0 / k); return 0; }
1
#include <bits/stdc++.h> using namespace std; void read(int &x) { x = 0; int w = 1; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') w = -1, ch = getchar(); while (ch >= '0' && ch <= '9') { x = (x << 1) + (x << 3) + ch - 48, ch = getchar(); } x *= w; } int mx(int a, int b) { return a > b ? a : b; } int mn(int a, int b) { return a < b ? a : b; } int n, k, l, c, d, p, nl, np; int main() { read(n); read(k); read(l); read(c); read(d); read(p); read(nl); read(np); int ans, tmp; tmp = mn(k * l / nl, p / np); ans = c * d; ans = mn(tmp, ans); printf("%d", ans / n); putchar('\n'); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long int n, b, d, s, i, ans = 0; cin >> b >> d >> s; n = max(b, max(d, s)); if (b < n - 1) ans += n - 1 - b; if (d < n - 1) ans += n - 1 - d; if (s < n - 1) ans += n - 1 - s; cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; unsigned long long d[1000002], vis1[1000002], add[1000002]; long long ans = 0; int main() { int num, x, y, i, j, k, num2, m, n, t; scanf("%d%d", &n, &m); d[0] = 1; for (i = 1; i <= n; i++) { d[i] = d[i - 1] * 101; } for (i = 1; i <= m; i++) { scanf("%d%d", &x, &y); vis1[x] += d[y], vis1[y] += d[x]; } for (i = 1; i <= n; i++) add[i] = vis1[i] + d[i]; sort(add + 1, add + n + 1); sort(vis1 + 1, vis1 + 1 + n); num = 0; num2 = 0; for (i = 2; i <= n; i++) { if (vis1[i] == vis1[i - 1]) { num++, ans += num; } else num = 0; if (add[i] == add[i - 1]) { num2++, ans += num2; } else num2 = 0; } cout << ans << endl; }
3
#include <bits/stdc++.h> using namespace std; int main() { float x, n, sum = 0; cin >> x; for (int i = 0; i < x; i++) { cin >> n; sum += n; } cout << sum / x; return 0; }
2
#define rep(i, n) for(int i = 0; i < (int)(n); i++) typedef long long ll; #include <bits/stdc++.h> using namespace std; int main() { ll n; std::cin >> n; if(n==1){ std::cout << 1 << std::endl; return 0; } set<ll> element; ll nown = n; for (int i = 2; i <= sqrt(n); i++) { if(nown%i==0){ element.insert(i); } while(nown%i==0){ nown/=i; } } if(nown!=1){ element.insert(nown); } // 素数のとき if(element.empty()){ element.insert(n); } ll result = n; for (auto e : element) { // std::cout << e << std::endl; result /= e; result *= (e-1); } std::cout << result << std::endl; }
0
#include <bits/stdc++.h> using namespace std; struct Node { int v, nxt; } e[300005 << 1]; int adj[300005], n, m, c, fa[300005], dep[300005]; inline void Add(int u, int v) { e[++c].v = v; e[c].nxt = adj[u]; adj[u] = c; } inline void GET(int &n) { char c; n = 0; do c = getchar(); while ('0' > c || c > '9'); while ('0' <= c && c <= 'n') n = n * 10 + c - '0', c = getchar(); } void predfs(int u) { for (int i = adj[u]; i; i = e[i].nxt) if (e[i].v != fa[u]) fa[e[i].v] = u, dep[e[i].v] = dep[u] + 1, predfs(e[i].v); } set<pair<int, int> > s[300005]; long long delta[300005], ans; inline void Merge(int u, int v) { if (s[u].size() > s[v].size()) swap(s[u], s[v]), swap(delta[u], delta[v]); int d = delta[u] - delta[v]; for (auto P : s[u]) s[v].insert(pair<int, int>(P.first + d, P.second)); s[u].clear(); } int dfs(int u) { int id = u, t; for (int i = adj[u]; i; i = e[i].nxt) if (e[i].v != fa[u]) { t = dfs(e[i].v); if (dep[t] < dep[id]) id = t; } if (1 == u) return 1; if (u == id) { while (!s[u].empty() && dep[s[u].begin()->second] >= dep[u]) s[u].erase(s[u].begin()); if (s[u].empty()) { puts("-1"); exit(0); } pair<int, int> cur = *s[u].begin(); s[u].erase(s[u].begin()); ans += delta[u] + cur.first; delta[u] = -cur.first; Merge(u, cur.second); id = cur.second; } else Merge(u, id); return id; } int main() { scanf("%d%d", &n, &m); int u, v, w; for (int i = 1; i < n; ++i) { GET(u); GET(v); Add(u, v); Add(v, u); } predfs(1); for (int i = 1; i <= m; ++i) { GET(u); GET(v); GET(w); s[u].insert(pair<int, int>(w, v)); } dfs(1); cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int lmt = 5e5 + 100; long long a[lmt], pre[lmt], suff[lmt], mx[4 * lmt], lazy[4 * lmt]; void build(int at, int L, int R) { if (L == R) { mx[at] = pre[L]; return; } int mid = (L + R) / 2; build(at * 2, L, mid); build(at * 2 + 1, mid + 1, R); mx[at] = max(mx[at * 2], mx[at * 2 + 1]); } void update(int at, int L, int R, int l, int r, long long u) { if (lazy[at] != 0) { mx[at] += lazy[at]; if (L != R) { lazy[at * 2] += lazy[at]; lazy[at * 2 + 1] += lazy[at]; } lazy[at] = 0; } if (r < L || R < l) return; if (l <= L && R <= r) { mx[at] += u; if (L != R) { lazy[at * 2] += u; lazy[at * 2 + 1] += u; } return; } int mid = (L + R) / 2; update(at * 2, L, mid, l, r, u); update(at * 2 + 1, mid + 1, R, l, r, u); mx[at] = max(mx[at * 2], mx[at * 2 + 1]); } long long query(int at, int L, int R, int l, int r) { if (lazy[at] != 0) { mx[at] += lazy[at]; if (L != R) { lazy[at * 2] += lazy[at]; lazy[at * 2 + 1] += lazy[at]; } lazy[at] = 0; } if (r < L || R < l) return 0LL; if (l <= L && R <= r) return mx[at]; int mid = (L + R) / 2; long long x = query(at * 2, L, mid, l, r), y = query(at * 2 + 1, mid + 1, R, l, r); return max(x, y); } int main() { ios_base::sync_with_stdio(false); int n; long long x; cin >> n >> x; for (int i = 1; i <= n; i++) { cin >> a[i]; } long long ans = 0; for (int i = 1; i <= n; i++) { pre[i] = max(pre[i], pre[i - 1] + a[i]); ans = max(ans, pre[i]); } for (int i = n; i > 0; i--) { suff[i] = max(suff[i], suff[i + 1] + a[i]); ans = max(ans, suff[i]); } if (x > 0) { ans = max(ans, ans * x); } for (int i = n; i > 0; i--) { pre[i] = pre[i - 1]; } for (int i = 1; i <= n; i++) { suff[i] = suff[i + 1]; } build(1, 1, n); for (int i = 1; i <= n; i++) { long long u = x * a[i]; update(1, 1, n, 1, i, u); long long res = query(1, 1, n, 1, i); res += suff[i]; ans = max(ans, res); } cout << ans << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; int trs_mar2[35][17][17]; int trs_mar[35][17][17]; int m = 14; void prepare() { trs_mar[0][1][2] = trs_mar[0][1][3] = trs_mar[0][1][4] = trs_mar[0][1][5] = 1; trs_mar[0][2][6] = trs_mar[0][2][7] = 1; trs_mar[0][3][8] = trs_mar[0][3][9] = 1; trs_mar[0][4][10] = trs_mar[0][4][11] = 1; trs_mar[0][5][12] = trs_mar[0][5][13] = 1; trs_mar[0][6][10] = trs_mar[0][6][11] = 1; trs_mar[0][7][12] = trs_mar[0][7][13] = 1; trs_mar[0][8][10] = trs_mar[0][8][11] = 1; trs_mar[0][9][12] = trs_mar[0][9][13] = 1; trs_mar[0][10][6] = 1; trs_mar[0][11][8] = trs_mar[0][11][9] = 1; trs_mar[0][12][7] = 1; trs_mar[0][13][8] = trs_mar[0][13][9] = 1; memcpy(trs_mar2, trs_mar, sizeof(trs_mar)); for (int i = 1; i < m; i++) for (int j = 1; j < m; j++) if (trs_mar[0][i][j] != 0) trs_mar[0][i][m]++; trs_mar[0][m][m] = 1; trs_mar2[0][1][m] = 8; trs_mar2[0][2][m] = 4; trs_mar2[0][3][m] = 4; trs_mar2[0][4][m] = 3; trs_mar2[0][5][m] = 3; trs_mar2[0][6][m] = 3; trs_mar2[0][7][m] = 3; trs_mar2[0][8][m] = 3; trs_mar2[0][9][m] = 3; trs_mar2[0][10][m] = 2; trs_mar2[0][11][m] = 4; trs_mar2[0][12][m] = 2; trs_mar2[0][13][m] = 4; trs_mar2[0][14][m] = 1; for (int t = 1; t <= 31; t++) { for (int i = 1; i <= m; i++) for (int j = 1; j <= m; j++) for (int k = 1; k <= m; k++) { trs_mar[t][i][k] = (trs_mar[t][i][k] + trs_mar[t - 1][i][j] * (long long)trs_mar[t - 1][j][k]) % mod; trs_mar2[t][i][k] = (trs_mar2[t][i][k] + trs_mar2[t - 1][i][j] * (long long)trs_mar2[t - 1][j][k]) % mod; } } } int ans[2][17]; int now; int work(int n) { if (n == 0) return -2; now = 0; memset(ans, 0, sizeof(ans)); ans[now][1] = 1; for (int t = 0; t <= 30; t++) if ((1 << t) & n) { now = 1 - now; for (int i = 1; i <= m; i++) ans[now][i] = 0; for (int i = 1; i <= m; i++) for (int j = 1; j <= m; j++) ans[now][j] = (ans[now][j] + ans[1 - now][i] * (long long)trs_mar[t][i][j]) % mod; } long long res = ans[now][m]; now = 0; memset(ans, 0, sizeof(ans)); ans[now][1] = 1; for (int t = 0; t <= 30; t++) if ((1 << t) & ((n - 1) / 2)) { now = 1 - now; for (int i = 1; i <= m; i++) ans[now][i] = 0; for (int i = 1; i <= m; i++) for (int j = 1; j <= m; j++) ans[now][j] = (ans[now][j] + ans[1 - now][i] * (long long)trs_mar2[t][i][j]) % mod; } res += ans[now][m]; if (res % 2 != 0) res += mod; res /= 2; return res % mod; } int main() { int l, r; cin >> l >> r; prepare(); cout << (work(r) - work(l - 1) + mod) % mod << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, k; bool dd[100009][2]; string s[2]; queue<tuple<int, int, int>> q; void xl() { cin >> n >> k; cin >> s[0] >> s[1]; s[0] = '#' + s[0]; s[1] = '#' + s[1]; q.push({1, 0, 0}); dd[1][0] = 1; while (q.size()) { auto p = q.front(); q.pop(); int x, y, z; tie(x, y, z) = p; if (x + k > n) { cout << "YES" << endl; return; } if (x > 1 && dd[x - 1][y] == 0 && s[y][x - 1] == '-') { if (x - 1 > z + 1) { dd[x - 1][y] = 1; q.push({x - 1, y, z + 1}); } } if (x < n && dd[x + 1][y] == 0 && s[y][x + 1] == '-') { dd[x + 1][y] = 1; q.push({x + 1, y, z + 1}); } if (x + k <= n && dd[x + k][1 - y] == 0 && s[1 - y][x + k] == '-') { dd[x + k][1 - y] = 1; q.push({x + k, 1 - y, z + 1}); } } cout << "NO" << endl; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); xl(); }
2
#include <bits/stdc++.h> int main() { int n, i; scanf("%d", &n); if (n & 1) { for (i = 0; i < n - 1; i++) printf("%d ", i); printf("%d\n", i); for (i = 0; i < n - 1; i++) printf("%d ", i + 1); printf("0\n"); for (i = 0; i < n - 1; i++) printf("%d ", (i + i + 1) % n); printf("%d\n", (i + i + 1) % n); } else printf("-1\n"); return 0; }
1
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using D = double; using uint = unsigned int; #ifdef WIN32 #define LLD "%I64d" #else #define LLD "%lld" #endif #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second const int maxn = 5005; const int MOD = 1000000007; int cnk[maxn][maxn]; int A, B, C; int n; ll get(ll a, ll b) { if (a == 0 && b > 0) return 0; if (a == 0 && b == 0) return 1; return cnk[a + b - 1][a - 1]; } ll calc(ll a, ll b, ll c, ll d) { // cout << "calc " << a << ' ' << b << ' ' << c << ' ' << d << ' ' << endl; if (d < 0) return 0; if (a + b + c + d > n) return 0; return (((ll)cnk[a + b][b] * cnk[a + b + c][c]) % MOD * cnk[a + b + c + d][d]) % MOD; } int main() { scanf("%d%d%d%d", &n, &A, &B, &C); if (B % 2 == 1) { cout << 0 << endl; return 0; } cnk[0][0] = 1; for (int i = 1; i <= n; i++) { cnk[i][0] = 1; for (int j = 1; j <= i; j++) cnk[i][j] = (cnk[i - 1][j - 1] + cnk[i - 1][j]) % MOD; } ll answer = 0; for (int x = 0; x <= C; x++) { for (int y = 0; 3 * y + x <= C; y++) { ll curans = calc(B / 2, y, C - 3 * y - x, A - (C - 3 * y - x)); // cout << x << ' ' << y << ' ' << curans << endl; curans = ((ll)curans * get(B / 2, x)) % MOD; // cout << curans << endl; answer = (answer + curans) % MOD; } } cout << answer << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int prime[1000000], ans[1000000] = {0}, sum[1000000] = {0}; bool not_prime[10000001] = {0}; inline int lowbit(const int& x) { return x & -x; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int prime_num = 0; for (int i = 2; i <= 10000000; ++i) { if (!not_prime[i]) prime[prime_num++] = i; for (int j = 0; j < prime_num && 1LL * i * prime[j] <= 10000000; ++j) { not_prime[i * prime[j]] = 1; if (!(i % prime[j])) break; } } prime[prime_num] = 10000001; int n, m, l, r, input; for (cin >> n; n; --n) { cin >> input; if (!not_prime[input]) ++ans[lower_bound(prime, prime + prime_num, input) - prime]; else for (int j = 0;; ++j) { if (!(input % prime[j])) { ++ans[j]; do input /= prime[j]; while (!(input % prime[j])); } if (prime[j] * prime[j] > input) { if (input != 1) ++ans[lower_bound(prime + (j + 1), prime + prime_num, input) - prime]; break; } } } ans[prime_num++] = 0; sum[0] = ans[0]; for (int i = 1; i < prime_num; ++i) sum[i] = sum[i - 1] + ans[i]; for (cin >> m; m; --m) { cin >> l >> r; r = min(r, 10000000); cout << (l <= r ? sum[upper_bound(prime, prime + prime_num, r) - prime - 1] - (l ? sum[lower_bound(prime, prime + prime_num, l) - prime - 1] : 0) : 0) << '\n'; } return 0; }
3
#include<iostream> using namespace std; int main (void) { string s; cin>>s; if(s[2]==s[3]&&s[4]==s[5]){cout<<"Yes";} else{cout<<"No";} }
0
#include <bits/stdc++.h> using namespace std; struct segtree { vector<vector<int>> t; vector<int> d; int h, n, sz; inline void build_node(const int& x) { for (int j = 0; j < 26; ++j) { t[x][j] = t[x << 1][j] + t[x << 1 | 1][j]; } } inline void build(int x) { for (x /= 2; x > 0; x /= 2) { build_node(x); } } segtree(const string& s) { sz = s.size(); h = sizeof(int) * 8 - __builtin_clz(sz); n = 1 << h; d = vector<int>(n << 1, 0); t = vector<vector<int>>(n << 1, vector<int>(26, 0)); for (int i = 0; i < sz; ++i) { ++t[i + n][s[i] - 'a']; } for (int i = n - 1; i > 0; --i) { build_node(i); } } void apply(int x, int v) { if (x < n) { d[x] = v; } } void fill_nodes(const vector<int>& ind, const vector<int>& char_count, int dir) { for (const int& x : ind) { for (int i = 0; i < 26; ++i) { t[x][i] = 0; } } int ch_type = (dir == 1 ? 0 : 25); int used_of_type = 0; for (int i = (dir == 1 ? 0 : 0); i < ind.size(); ++i) { int x = ind[i]; int my_sz = 1 << (h - (sizeof(int) * 8 - __builtin_clz(x)) + 1); int have = 0; for (; have < my_sz; (dir == 1 ? ++ch_type : --ch_type)) { int take = min(my_sz - have, char_count[ch_type] - used_of_type); t[x][ch_type] += take; have += take; used_of_type += take; if (have == my_sz) break; used_of_type = 0; } } } void sort_nodes(const vector<int>& ind, int dir) { vector<int> char_count(26, 0); for (const int& x : ind) { for (int i = 0; i < 26; ++i) { char_count[i] += t[x][i]; } } fill_nodes(ind, char_count, dir); } void push_node(int x) { if (d[x] != 0) { fill_nodes(vector<int>({x << 1, x << 1 | 1}), t[x], d[x]); apply(x << 1, d[x]); apply(x << 1 | 1, d[x]); d[x] = 0; } } void push(int x) { for (int s = h; s > 0; --s) { int i = x >> s; if (d[i] != 0) { push_node(i); } } } void set(int l, int r, int dir) { int l0 = l + n; int r0 = r + n; push(l0); push(r0 - 1); vector<int> front, back; for (l += n, r += n; l < r; l /= 2, r /= 2) { if (l % 2) { front.push_back(l); apply(l, dir); ++l; } if (r % 2) { --r; back.push_back(r); apply(r, dir); } } reverse(back.begin(), back.end()); front.insert(front.end(), back.begin(), back.end()); sort_nodes(front, dir); push(l0); push(r0 - 1); build(l0); build(r0 - 1); } string to_string() { for (int i = 1; i < n; ++i) { push_node(i); } static char cstr[100010]; for (int i = 0; i < sz; ++i) { for (int j = 0; j < 26; ++j) { if (t[i + n][j] != 0) { cstr[i] = j + 'a'; } } } cstr[sz] = 0; return string(cstr); } }; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int N, Q; cin >> N >> Q; string s; cin >> s; segtree st(s); for (int qq = 0; qq < Q; ++qq) { int i, j, k; cin >> i >> j >> k; st.set(i - 1, j, (k == 1 ? 1 : -1)); } cout << st.to_string() << "\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main(int argc, const char* argv[]) { long s, v1, v2, t1, t2; cin >> s >> v1 >> v2 >> t1 >> t2; long ans1 = s * v1 + 2 * t1; long ans2 = s * v2 + 2 * t2; if (ans1 > ans2) { cout << "Second" << endl; } else if (ans2 > ans1) { cout << "First" << endl; } else { cout << "Friendship" << endl; } }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, a = 0, b = 0, c = 0, d = 0; cin >> n; while (n--) { int s; cin >> s; if (s == 1) a++; if (s == 2) b++; if (s == 3) c++; if (s == 4) d++; } int ans = (d + c + (b * 2 + max(0, a - c) + 3) / 4); cout << ans << "\n"; return 0; }
2
// mp.second を li に #include<iostream> #include<map> using namespace std; #define nC2(n) ((n)*((n)-1)/2) #define rep(i,n) for(int i=0;i<(n);i++) #define df 0 typedef long int li; int main(){ int n,m; cin >>n >>m; li s=0; li ans=0; map<int,li> mp; mp[0]=1; rep(i,n){ int a; cin >>a; (s+=a)%=m; mp[s]++; } for(auto x: mp){ ans+=nC2(x.second); } cout << ans << "\n"; }
0
#include <bits/stdc++.h> void quicksort(int number[200000], int first, int last) { int i, j, pivot, temp; if (first < last) { pivot = first; i = first; j = last; while (i < j) { while (number[i] <= number[pivot] && i < last) i++; while (number[j] > number[pivot]) j--; if (i < j) { temp = number[i]; number[i] = number[j]; number[j] = temp; } } temp = number[pivot]; number[pivot] = number[j]; number[j] = temp; quicksort(number, first, j - 1); quicksort(number, j + 1, last); } } int main() { int i, count, number[200000], p, t, val; scanf("%d", &t); for (p = 1; p <= t; p++) { scanf("%d", &count); for (i = 0; i < count * 2; i++) scanf("%d", &number[i]); quicksort(number, 0, count * 2 - 1); val = number[count] - number[count - 1]; printf("%d\n", val); } return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 10; long long ans[N][2]; long long fa1[N], fa2[N], s1[N], s2[N]; long long find(long long fa[], long long x) { return x == fa[x] ? x : fa[x] = find(fa, fa[x]); } void merge(long long fa[], long long x, long long y) { long long fx = find(fa, x); long long fy = find(fa, y); fa[fx] = fy; } void init(long long fa[], long long n) { for (long long i = 1; i <= n; ++i) fa[i] = i; } void solve() { long long n, m1, m2; cin >> n >> m1 >> m2; init(fa1, n), init(fa2, n); for (long long i = 1; i <= m1; ++i) { long long u, v; cin >> u >> v; merge(fa1, u, v); } for (long long i = 1; i <= m2; ++i) { long long u, v; cin >> u >> v; merge(fa2, u, v); } long long cnt = 0; for (long long i = 2; i <= n; ++i) if (find(fa1, 1) != find(fa1, i) && find(fa2, 1) != find(fa2, i)) { ans[++cnt][0] = 1; ans[cnt][1] = i; merge(fa1, i, 1); merge(fa2, i, 1); } long long sz1 = 0, sz2 = 0; for (long long i = 2; i <= n; ++i) { if (fa1[i] == i && fa1[i] != find(fa1, 1)) s1[sz1++] = i; if (fa2[i] == i && fa2[i] != find(fa2, 1)) s2[sz2++] = i; } for (long long i = 0, j = 0; i < sz1 && j < sz2; ++i, ++j) ans[++cnt][0] = s1[i], ans[cnt][1] = s2[j]; cout << cnt << '\n'; for (long long i = 1; i <= cnt; ++i) cout << ans[i][0] << ' ' << ans[i][1] << '\n'; } signed main() { ios::sync_with_stdio(false); cin.tie(0); solve(); return 0; }
4
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:33554432") using namespace std; const int MOD = 1000 * 1000 * 1000 + 7; int n, k; long long fact[1 << 17], inv[1 << 17]; long long a[1 << 17]; long long dp[1 << 11][1 << 11]; map<int, int> M; long long Pow(long long x, int a) { if (a == 0) return 1; if (a == 1) return x; if (a & 1) return (x * (Pow((x * x) % MOD, a / 2)) % MOD) % MOD; return (Pow((x * x) % MOD, a / 2)) % MOD; } long long invert(long long x) { return Pow(x, MOD - 2); } long long C(int n, int k) { return (fact[n] * ((inv[n - k] * inv[k]) % MOD)) % MOD; } vector<int> L; int luck(int x) { int res = 1; while (x) { res &= (x % 10 == 7 || x % 10 == 4); x /= 10; } return res; } int main() { cin >> n >> k; int s = n; for (int i = 0; i < n; ++i) { cin >> a[i]; if (luck(a[i])) { M[a[i]]++; s--; } } for (map<int, int>::iterator it = M.begin(); it != M.end(); ++it) L.push_back(it->second); fact[0] = 1; for (int i = 1; i < 1 << 17; ++i) fact[i] = (fact[i - 1] * i) % MOD; for (int i = 0; i < 1 << 17; ++i) inv[i] = invert(fact[i]); long long res = 0; memset(dp, 0, sizeof(dp)); for (int i = 0; i <= L.size(); ++i) dp[i][0] = 1; for (int i = 1; i <= L.size(); ++i) for (int j = 1; j <= i; ++j) dp[i][j] = (dp[i - 1][j] + (dp[i - 1][j - 1] * L[i - 1]) % MOD) % MOD; for (int i = 0; i <= min(s, k); ++i) res = (res + (C(s, i) * dp[L.size()][k - i]) % MOD) % MOD; cout << res << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long MOD = (int)998244353; long double sqr(long double a) { return a * a; } void c(long long x, long long y) { cout << x << " " << y << "\n"; } int main() { long long x = 1, y = 1, cnt; cin >> cnt; c(x, y); --cnt; x++; while (cnt) { c(x, y); cnt--; if (cnt) c(x + 1, y); else break; cnt--; if (cnt) c(x, y + 3); else break; cnt--; x += 2; } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n, item, helper = 1; cin >> n; vector<int> myVec; for (int i = 2001; i < 2001 + n; i++) { cin >> item; if (item == helper) { myVec.push_back(i); helper++; } } cout << myVec.size() << endl; for (int item : myVec) cout << item << " "; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long spf[9]; long long fac[9]; void sieve() { spf[1] = 1; for (long long i = 2; i < 9; i++) spf[i] = i; for (long long i = 4; i < 9; i += 2) spf[i] = 2; for (long long i = 3; i * i < 9; i++) { if (spf[i] == i) { for (long long j = i * i; j < 9; j += i) if (spf[j] == j) spf[j] = i; } } } map<long long, long long> getfactor(long long a) { map<long long, long long> m; while (a > 1) { m[spf[a]]++; a /= spf[a]; } return m; } long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } long long inverse(long long a, long long p) { return power(a, p - 2, p); } long long ncr(long long n, long long r, long long p) { if (r == 0) return 1; return (fac[n] * inverse(fac[r], p) % p * inverse(fac[n - r], p) % p) % p; } long long hah[2000001] = {0}; long long pre[2000001] = {0}; void solve() { long long n, x, y; cin >> n >> x >> y; long long a[n]; for (long long i = 0; i < n; i++) { cin >> a[i]; hah[a[i]]++; } for (long long i = 1; i <= 2000000; i++) { pre[i] = hah[i] * i + pre[i - 1]; } for (long long i = 1; i <= 2000000; i++) hah[i] += hah[i - 1]; long long ans = 1e18; for (long long i = 2; i <= 1000000; i++) { long long temp = min(i, x / y); long long sum = 0; for (long long j = 0; j <= 1000000; j += i) { if (x > y) sum += (hah[max(j + i - i, j + i - temp - 1)] - hah[j + i - i]) * x + ((hah[j + i - 1] - hah[max(j + i - i, j + i - temp - 1)]) * (j + i) - (pre[j + i - 1] - pre[max(j + i - i, j + i - temp - 1)])) * y; else sum += (hah[j + i - 1] - hah[j + i - i]) * x; } ans = min(sum, ans); } cout << ans << '\n'; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t = 1; while (t--) { solve(); } }
2
#include <bits/stdc++.h> using namespace std; template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; } template <class T> void chmin(T &t, T f) { if (t > f) t = f; } template <class T> void chmax(T &t, T f) { if (t < f) t = f; } int in() { int x; scanf("%d", &x); return x; } const int INF = 1001001001; int N, K; int L[110]; string S[110]; int dp[110][110][110]; void solve(int a, int b, int x0) { int *DP = dp[a][b]; int x; int i, j; int k, kk; for (x = x0; x < L[a]; ++x) { for (i = a; i < b; ++i) if (!(x < L[i] && S[a][x] == S[i][x])) break; if (i < b) break; } for (k = 0; k <= b - a; ++k) { DP[k] = -INF; } DP[0] = 0; for (i = a; i < b; i = j) { for (j = i; j < b && S[i][x] == S[j][x]; ++j) ; if (i == a && j == b) { for (k = 0; k <= b - a; ++k) { DP[k] = 0; } break; } solve(i, j, x); for (k = b - a; k >= 0; --k) { for (kk = 0; kk <= j - i && kk <= k; ++kk) { chmax(DP[k], DP[k - kk] + dp[i][j][kk]); } } } for (k = 0; k <= b - a; ++k) { DP[k] += (x - x0) * (k * (k - 1) / 2); } } int main() { int i; char buf[510]; for (; ~scanf("%d%d", &N, &K);) { for (i = 0; i < N; ++i) { scanf("%s", buf); S[i] = buf; } sort(S, S + N); for (i = 0; i < N; ++i) { L[i] = S[i].size(); S[i] += "!"; } solve(0, N, 0); printf("%d\n", dp[0][N][K]); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z; cin >> t; while (t--) { cin >> n >> x; cout << x * 2 << "\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int g[5][5]; for (int i = int(0); i <= int(4); i++) { for (int j = int(0); j <= int(4); j++) { cin >> g[i][j]; } } long long happiness = -9999; vector<int> a = {0, 1, 2, 3, 4}; do { long long curr = (g[a[0]][a[1]] + g[a[1]][a[0]] + g[a[2]][a[3]] + g[a[3]][a[2]]) + (g[a[1]][a[2]] + g[a[2]][a[1]] + g[a[3]][a[4]] + g[a[4]][a[3]]) + (g[a[2]][a[3]] + g[a[3]][a[2]]) + (g[a[3]][a[4]] + g[a[4]][a[3]]); if (curr > happiness) happiness = curr; } while (next_permutation(a.begin(), a.end())); cout << happiness; return 0; }
2
#include <bits/stdc++.h> using namespace std; const long double PI = 3.141592653589793238L; double gcd(double x, double y) { while (fabs(x) > 1e-4 && fabs(y) > 1e-4) { if (x > y) x -= floor(x / y) * y; else y -= floor(y / x) * x; } return x + y; } double area(double a, double b, double c) { double s = 0; s = (a + b + c) / 2; return sqrt(s * (s - a) * (s - b) * (s - c)); } int main() { double x1, x2, x3, y1, y2, y3; double a = 0; double b = 0; double c = 0; double S = 0; double R = 0; double A = 0; double B = 0; double C = 0; double g = 0; double n = 0; while (cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3) { a = sqrt(((x2 - x3) * (x2 - x3)) + ((y2 - y3) * (y2 - y3))); b = sqrt(((x1 - x3) * (x1 - x3)) + ((y1 - y3) * (y1 - y3))); c = sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1))); S = area(a, b, c); R = (a * b * c) / (4 * S); A = acos((b * b + c * c - a * a) / (2 * b * c)); B = acos((a * a + c * c - b * b) / (2 * a * c)); C = acos((a * a + b * b - c * c) / (2 * a * b)); n = PI / gcd(A, gcd(B, C)); double result = (R * R * n * sin((2 * PI) / n)) / 2; cout << setprecision(10) << fixed << result << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int tree[700000] = {0}; int arr[300010] = {0}; int n; set<int> s; void update(int node, int start, int end, int l, int r, int win) { if (start > r || end < l) { return; } else if (l <= start && end <= r) { auto it = s.lower_bound(start); while (it != s.end() && *it <= end) { arr[*it] = win; auto it2 = it; it++; s.erase(it2); } s.insert(win); return; } else { int mid = (start + end) / 2; update(2 * node, start, mid, l, r, win); update(2 * node, mid + 1, end, l, r, win); } } int main() { cin >> n; for (int i = 1; i <= n; i++) { s.insert(i); } int m; cin >> m; while (m--) { int l, r, win; cin >> l >> r >> win; update(1, 1, n, l, r, win); } for (auto i : s) { arr[i] = 0; } for (int i = 1; i <= n; i++) { cout << arr[i] << " "; } cout << endl; }
1
#include <bits/stdc++.h> using namespace std; int n, m, deg[1000005]; int main() { scanf("%d", &n); scanf("%d", &m); for (int i = 0; i < m; i++) { int u, v; scanf("%d", &u); scanf("%d", &v); u--; v--; deg[u]++; deg[v]++; } long long tot = 0; for (int i = 0; i < n; i++) { long long d = deg[i]; tot += d * (d - 1) + (n - 1 - d) * (n - 2 - d) - d * (n - 1 - d); } printf("%I64d", tot / 6ll); return 0; }
3
#include <bits/stdc++.h> using namespace std; int a[111][111], n, m; int rr[111], cc[111]; int main(int argc, const char* argv[]) { cin >> n >> m; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { cin >> a[i][j]; } bool fl = true; for (; fl == true;) { fl = false; for (int i = 0; i < n; i++) { int sum = 0; for (int j = 0; j < m; j++) sum += a[i][j]; if (sum < 0) { fl = true; rr[i + 1] ^= 1; for (int j = 0; j < m; j++) a[i][j] = -a[i][j]; } } for (int j = 0; j < m; j++) { int sum = 0; for (int i = 0; i < n; i++) sum += a[i][j]; if (sum < 0) { fl = true; cc[j + 1] ^= 1; for (int i = 0; i < n; i++) a[i][j] = -a[i][j]; } } } int nn = 0; for (int i = 1; i <= n; i++) if (rr[i] == 1) nn++; cout << nn << " "; for (int i = 1; i <= n; i++) if (rr[i] == 1) cout << i << " "; cout << endl; nn = 0; for (int i = 1; i <= m; i++) if (cc[i] == 1) nn++; cout << nn << " "; for (int i = 1; i <= m; i++) if (cc[i] == 1) cout << i << " "; cout << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, k, cnt = 0; long long a[4000000], b[4000000]; int nx[4000000], pr[4000000]; struct Cmp { bool operator()(const int &lhs, const int &rhs) const { if (b[lhs] != b[rhs]) return b[lhs] < b[rhs]; return lhs < rhs; } }; set<int, Cmp> se; priority_queue<long long, vector<long long>, greater<long long> > q; const int MASK = (1 << 22) - 1; int main() { scanf("%d%d", &n, &k); for (int i = 0; i < (int)(n); ++i) { long long x; scanf("%I64d", &x); if ((cnt == 1 && x <= a[0]) || (cnt > 1 && (x == a[cnt - 1] || (x > a[cnt - 1] && a[cnt - 1] > a[cnt - 2]) || (x < a[cnt - 1] && a[cnt - 1] < a[cnt - 2])))) --cnt; a[cnt++] = x; } if (cnt & 1) --cnt; long long ans = 0; for (int i = 0; i < cnt; i += 2) ans += a[i + 1] - a[i]; for (int i = 0; i < (int)(cnt); ++i) { nx[i] = i + 1; pr[i] = i - 1; } nx[cnt - 1] = -1; pr[0] = -1; for (int i = 0; i < (int)(cnt - 1); ++i) b[i] = abs(a[i + 1] - a[i]); for (int i = 0; i < (int)(cnt - 1); ++i) q.push((b[i] << 22) | i); k = cnt / 2 - k; for (int kk = 0; kk < (int)(k); ++kk) { int ind = q.top() & MASK; long long val = q.top() >> 22; q.pop(); if (b[ind] != val) { ++k; continue; } ans -= val; b[ind] = b[nx[ind]] = -1; if (pr[ind] == -1) { if (nx[nx[ind]] != -1) { pr[nx[nx[ind]]] = -1; } } else { nx[pr[ind]] = nx[nx[ind]]; if (nx[nx[ind]] != -1) { pr[nx[nx[ind]]] = pr[ind]; b[pr[ind]] = abs(a[nx[nx[ind]]] - a[pr[ind]]); q.push((b[pr[ind]] << 22) | pr[ind]); } else { b[pr[ind]] = -1; } } } printf("%I64d\n", ans); return 0; }
6
#include<bits/stdc++.h> using namespace std; int main() { string a,b; cin>>a>>b; int c=0,i; a=a+b; for(int i=0;i<a.size();i++) c=c*10+a[i]-48; if(int(sqrt(c))==sqrt(c)) cout<<"Yes\n"; else cout<<" No\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, x, y; cin >> n >> x >> y; int a[n + 1]; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { int flag = 1; for (int j = i - 1; j >= 1 && j >= i - x; j--) { if (a[i] > a[j]) { flag = 0; break; } } for (int j = i + 1; j <= n && j <= i + y; j++) { if (a[i] > a[j]) { flag = 0; break; } } if (flag == 1) { cout << i << endl; return 0; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int N, M, x, foo; int a[10005]; int res = 2; multiset<int> pos; int cntZero = 0; set<pair<int, int> > used; int main() { scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d", a + i); if (a[i] == 0) cntZero++; } for (int i = 0; i < N; i++) { pos.insert(a[i]); } vector<int> toAdd; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { int temp = 2; if (i == j) continue; if (used.find(make_pair(a[i], a[j])) != used.end()) continue; used.insert(make_pair(a[i], a[j])); if (a[i] == 0 && a[j] == 0) { res = max(res, cntZero); continue; } int last = a[j]; int prevLast = a[i]; if (last >= 1e9 + 1 && prevLast >= 1e9 + 1) { continue; } if (last < -1e9 && prevLast < -1e9) { continue; } int sum = a[i] + a[j]; toAdd.push_back(a[i]); toAdd.push_back(a[j]); pos.erase(pos.find(a[i])); pos.erase(pos.find(a[j])); while (pos.find(sum) != pos.end()) { pos.erase(pos.find(sum)); toAdd.push_back(sum); temp++; swap(last, prevLast); last = sum; if (last >= 1e9 + 1 && prevLast >= 1e9 + 1) { break; } if (last < -1e9 && prevLast < -1e9) { break; } sum = last + prevLast; } for (int x = 0; x < toAdd.size(); x++) { pos.insert(toAdd[x]); } toAdd.clear(); res = max(res, temp); } } printf("%d\n", res); }
4
#include <bits/stdc++.h> using namespace std; long long ic[4] = {0, 2, 3, 1}; long long getb(long long a) { if (a == 1LL) { return 2LL; } return ic[a & 3] | (getb(a >> 2) << 2); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int tt; cin >> tt; while (tt--) { long long n; cin >> n; long long d = 1; while (n >= d) { d = (d << 2LL); } d = (d >> 2LL); long long diff = (n - d); long long a = d | (diff / 3LL); long long b = getb(a); long long c = a ^ b; if ((diff % 3) == 0) { cout << a << endl; } else if ((diff % 3) == 1) { cout << b << endl; } else { cout << c << endl; } } return 0; }
5
#include<bits/stdc++.h> #define int long long using namespace std; const int maxn=400; const int mod=1e9+7; int n,c; int a[maxn+5],b[maxn+5],sum[maxn+5][maxn+5]; struct mat { int a[maxn+5]; mat(){memset(a,0,sizeof(a));} }s[maxn+5],ans; mat mul(mat a,mat b) { mat res; for(int i=0;i<=c;i++) { for(int j=0;j<=c;j++) { if(i+j>c) continue; res.a[i+j]+=a.a[i]*b.a[j]; res.a[i+j]%=mod; } } return res; } int qpow(int a,int b) { int res=1%mod; for(;b;b>>=1,a=(a*a)%mod) if(b&1) res=(res*a)%mod; return res; } signed main() { scanf("%lld%lld",&n,&c); for(int i=1;i<=n;i++) scanf("%lld",&a[i]); for(int i=1;i<=n;i++) scanf("%lld",&b[i]); for(int i=1;i<=maxn;i++) for(int j=0;j<=maxn;j++) sum[i][j]=(sum[i-1][j]+qpow(i,j))%mod; for(int i=1;i<=n;i++) { for(int k=0;k<=c;k++) { s[i].a[k]=sum[b[i]][k]-sum[a[i]-1][k]; if(s[i].a[k]<0) s[i].a[k]+=mod; } } ans.a[0]=1; for(int i=1;i<=n;i++) ans=mul(ans,s[i]); printf("%lld\n",ans.a[c]); return 0; }
0
#include <cstdio> #include <vector> #include <queue> using namespace std; typedef long long i64; int main() { const int MAX = 1e5; vector<vector<int>> g(2 * MAX); int n; scanf("%d", &n); for(int i = 0; i < n; i++) { int x, y; scanf("%d%d", &x, &y); x -= 1; y -= 1; g[x].push_back(MAX + y); g[MAX + y].push_back(x); } i64 ans = 0; vector<bool> visited(2 * MAX); for(int s = 0; s < 2 * MAX; s++) { if(visited[s]) { continue; } queue<int> q; int edges = 0; int x_vertices = 0, y_vertices = 0; q.push(s); while(!q.empty()) { int v = q.front(); q.pop(); if(visited[v]) { continue; } visited[v] = true; if(v < MAX) { x_vertices += 1; } else { y_vertices += 1; } for(auto& w : g[v]) { if(!visited[w]) { edges += 1; } q.push(w); } } ans += x_vertices * (i64)y_vertices - edges; } printf("%lld\n", ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; long long dp[55][55][55][110]; long long C[110][110]; int n, h, k; long long ans; long long tmp; int x, y; void pre_C() { memset(C, 0, sizeof(C)); for (int i = 0; i <= 100; i++) C[i][0] = 1; for (int i = 1; i <= 100; i++) for (int j = 1; j <= i; j++) { C[i][j] = (C[i - 1][j] + C[i - 1][j - 1]) % (1000000007LL); } } int work(int n, int k, int x) { if (x > 0) k++; k--; n = n + k; if (min(n - k, k) >= 5) return -1; else return C[n][k]; } void pre_dp() { dp[0][0][0][1] = 1; int h, N, M, K, i; for (h = 0; h < 50; h++) for (N = 0; N < 50; N++) for (M = 0; M <= N; M++) for (K = 1; K <= 100; K++) { dp[h][N][M][K] %= (1000000007LL); if (dp[h][N][M][K]) { dp[h + 1][N][0][K] += dp[h][N][M][K]; for (i = 1; i <= 50 - N; i++) { tmp = K * work(i, M + 1, h); if (tmp < 0 || tmp > 100) break; dp[h + 1][N + i][i][tmp] += dp[h][N][M][K]; } } } } void solve() { int i, j; for (x = 1; x <= min(n / 2, h - 1); x++) for (y = 0; 2 * (x + y) <= n; y++) { for (i = 1; i <= k; i++) for (j = 0; j <= y; j++) ans += (long long)(h - x) * dp[x][y][j][i]; ans %= (1000000007LL); } } int main() { memset(dp, 0, sizeof(dp)); pre_C(); pre_dp(); while (~scanf("%d%d%d", &n, &h, &k)) { ans = 0; solve(); printf("%I64d\n", ans); } return 0; }
5
#include <bits/stdc++.h> using namespace std; template <class T> inline T SQR(T a) { return a * a; } template <class T> inline T gcd(T a, T b) { a = abs(a); b = abs(b); if (!b) return a; return gcd(b, a % b); } template <class T> inline T lcm(T a, T b) { a = abs(a); b = abs(b); return (a / _gcd(a, b)) * b; } template <typename T> T POW(T b, T p) { T r = 1; while (p) { if (p & 1) r = (r * b); b = (b * b); p >>= 1; } return r; } template <typename T> T BigMod(T b, T p, T m) { T r = 1; while (p) { if (p & 1) r = (r * b) % m; b = (b * b) % m; p >>= 1; } return r; } template <typename T> T ModInverse(T n, T m) { return BigMod(n, m - 2, m); } double DEG(double x) { return (180.0 * x) / (acos(-1.0)); } double RAD(double x) { return (x * (double)acos(-1.0)) / (180.0); } template <typename T> double DIS(T a, T b) { return sqrt((double)(SQR(a.first - b.first) + SQR(a.second - b.second))); } template <typename T> T ANGLE(T a, T b) { return atan(double(a.second - b.second) / double(a.first - b.first)); } template <typename T> int isLeft(T a, T b, T c) { return (c.first - a.first) * (b.second - a.second) - (b.first - a.first) * (c.second - a.second); } template <class T> void prnt(T v) { for (int i = 0; i < (int)v.size(); i++) { if (!i) cout << v[i]; else cout << " " << v[i]; } cout << endl; } template <class T> void BIN(T n) { if (!n) { printf("\n"); return; } BIN(n / 2); cout << (n % 2); } template <typename T> T in() { char ch; T n = 0; bool ng = false; while (1) { ch = getchar(); if (ch == '-') { ng = true; ch = getchar(); break; } if (ch >= '0' && ch <= '9') break; } while (1) { n = n * 10 + (ch - '0'); ch = getchar(); if (ch < '0' || ch > '9') break; } return (ng ? -n : n); } long long MD; const int ms = 2; struct mat { long long m[3][3]; mat(){}; void unify() { memset(m, 0, sizeof(m)); for (int i = 0; i < ms; i++) { for (int j = 0; j < ms; j++) { if (i == j) m[i][j] = 1; } } } void print() { for (int i = 0; i < ms; i++) { for (int j = 0; j < ms; j++) { cout << m[i][j] << " - "; } printf("\n"); } printf("\n"); } void nullify() { memset(m, 0, sizeof(m)); } }; mat b; mat operator*(mat a, mat b) { mat c; for (int i = 0; i < ms; i++) { for (int j = 0; j < ms; j++) { c.m[i][j] = 0; for (int k = 0; k < ms; k++) { c.m[i][j] += (a.m[i][k] * b.m[k][j]) % MD; c.m[i][j] %= MD; } } } return c; } long long Fib(long long n) { if (n == 1) return (2LL % MD); if (n == 2) return (3LL % MD); n -= 2LL; mat B = b; mat ret; ret.unify(); while (n) { if (n & 1) ret = (ret * B); B = (B * B); n >>= 1LL; } return ((ret.m[0][0] * 3LL) % MD + (ret.m[0][1] * 2LL) % MD) % MD; } long long TwoP(long long b, long long n) { long long ret = 1LL % MD; while (n) { if (n & 1) ret = (ret * b) % MD; b = (b * b) % MD; n >>= 1LL; } return ret; } int main() { long long i, j, k, l, n, m; ios_base::sync_with_stdio(0), cin.tie(0); cin >> n >> k >> l >> MD; if (MD == 1LL) { cout << 0 << "\n"; return 0; } b.nullify(); b.m[0][0] = b.m[0][1] = b.m[1][0] = 1LL; long long ans0 = Fib(n); long long ans1 = (TwoP(2LL, n) - ans0 + MD) % MD; long long ans = 1LL; for (long long i = 0; i < l; i++) { if (k & 1) ans = (ans * ans1) % MD; else ans = (ans * ans0) % MD; k >>= 1LL; ans += MD; ans %= MD; } if (k) ans = 0LL; cout << (ans + MD) % MD << "\n"; return 0; }
4
#include<bits/stdc++.h> using namespace std; double n,m,x,y; int main() { cin>>n>>m>>x>>y; if(x==n/2&&y==m/2) printf("%.12f 1",n*m/2.0); else printf("%.12f 0",n*m/2.0); return 0; }
0
#include <bits/stdc++.h> using namespace std; bool check(double nu, double nd, double nl, double nr, double mu, double md, double ml, double mr, double mx, double my) { if ((mx >= nl && mx <= nr && md >= nd && md <= nu) || (mx >= nl && mx <= nr && mu >= nd && mu <= nu) || (ml >= nl && ml <= nr && my >= nd && my <= nu) || (mr >= nl && mr <= nr && my >= nd && my <= nu)) return true; else if ((nu + nr >= ml + my && nu <= nr - mx + mu && nu >= nr - mx + md && nu + nr <= mr + my) || (nd + nr >= ml + my && nd <= nr - mx + mu && nd >= nr - mx + md && nd + nr <= mr + my)) return true; else if ((nu + nl >= ml + my && nu <= nl - mx + mu && nu >= nl - mx + md && nu + nl <= mr + my) || (nd + nl >= ml + my && nd <= nl - mx + mu && nd >= nl - mx + md && nd + nl <= mr + my)) return true; else if (mx >= nl && mx <= nr && my >= nd && my <= nu) return true; else return false; } int main() { double t1_up, t1_down, t1_left, t1_right; double t2_up, t2_down, t2_left, t2_right; double t2_cx, t2_cy; double x, y; t1_up = t1_right = t2_up = t2_right = -105; t1_down = t1_left = t2_down = t2_left = 105; for (int i = 0; i < 4; i++) { scanf("%lf%lf", &x, &y); t1_down = min(y, t1_down); t1_up = max(y, t1_up); t1_left = min(x, t1_left); t1_right = max(x, t1_right); } for (int i = 0; i < 4; i++) { scanf("%lf%lf", &x, &y); t2_down = min(y, t2_down); t2_up = max(y, t2_up); t2_left = min(x, t2_left); t2_right = max(x, t2_right); } t2_cx = (t2_left + t2_right) / 2; t2_cy = (t2_up + t2_down) / 2; if (check(t1_up, t1_down, t1_left, t1_right, t2_up, t2_down, t2_left, t2_right, t2_cx, t2_cy)) { printf("YES\n"); } else printf("NO\n"); return 0; }
3
#include <bits/stdc++.h> using namespace std; long long int dx[] = {1, -1, 0, 0}; long long int dy[] = {0, 0, -1, 1}; long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } long long int power_mod(long long int a, long long int n) { long long int res = 1; while (n > 0) { if (n & 1) res = ((res % 998244353) * (a % 998244353)) % 998244353, n--; else a = ((a % 998244353) * (a % 998244353)) % 998244353, n /= 2; } return res; } long long int nCr(long long int n, long long int r) { if (r > n) return 0; double res = 1; for (long long int i = 1; i <= r; i++) res = (res * (n - r + i)) / i; return (long long int)(res + 0.01); } long long int power(long long int a, long long int n) { long long int res = 1; for (long long int i = 0; i < n; i++) res = res * a; return res; } bool cmp(vector<long long int> a, vector<long long int> b) { return a[0] < b[0]; } long long int lcm(long long int a, long long int b) { return (a * b) / gcd(a, b); } void solve() { long long int l, r, x, y; cin >> l >> r >> x >> y; if (y % x != 0) { cout << 0 << endl; return; } long long int num = y / x; long long int ans = 0; for (long long int i = 1; i * i <= num; i++) { if (num % i == 0) { if (gcd(i, (num / i)) == 1) { long long int a = i * x; long long int b = (num / i) * x; if (a == b) { if (a >= l && b <= r) { ans++; } } else { long long int cnt = 0; if (a >= l && b <= r) { cnt++; } swap(a, b); if (a >= l && b <= r) { cnt++; } if (cnt == 2) ans += 2; } } } } cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t = 1; while (t--) { solve(); } return 0; }
2
#include <bits/stdc++.h> void swap(int *a, int *b) { int tmp = *a; *a = *b, *b = tmp; } struct edge_t { struct edge_t *next; int v; }; struct edge_t *e[300005], ep[300005 * 4], *ep_top = ep; int eu[300005], ev[300005]; void add_edge(int u, int v) { ep_top->v = v; ep_top->next = e[u]; e[u] = ep_top++; ep_top->v = u; ep_top->next = e[v]; e[v] = ep_top++; } int fa[300005][20], dep[300005]; void dfs0(int u, int f) { dep[u] = dep[f] + 1; fa[u][0] = f; for (int i = 0; i + 1 < 20; ++i) fa[u][i + 1] = fa[fa[u][i]][i]; for (struct edge_t *i = e[u]; i; i = i->next) if (i->v != f) dfs0(i->v, u); } int lca(int u, int v) { if (dep[u] < dep[v]) swap(&u, &v); int delta = dep[u] - dep[v]; for (int i = 0; i < 20; ++i) if (delta & (1 << i)) u = fa[u][i]; for (int i = 20 - 1; i >= 0; --i) if (fa[u][i] != fa[v][i]) u = fa[u][i], v = fa[v][i]; return u == v ? u : fa[u][0]; } int col[300005], col2lca[300005], bel[300005], ucol[300005]; int n, k; struct edge_t *e2[300005]; void add_edge2(int u, int v) { ; ep_top->v = v; ep_top->next = e2[u]; e2[u] = ep_top++; ep_top->v = u; ep_top->next = e2[v]; e2[v] = ep_top++; } int dp[300005][2]; void dfs2(int u, int f) { dp[u][0] = !ucol[u]; dp[u][1] = !!ucol[u]; for (struct edge_t *i = e2[u]; i; i = i->next) { int v = i->v; if (v == f) continue; dfs2(v, u); int ori[] = {dp[u][0], dp[u][1]}; if (ucol[u]) dp[u][1] = 1ll * ori[1] * (dp[v][0] + dp[v][1]) % 998244353; else { dp[u][0] = 1ll * ori[0] * (dp[v][0] + dp[v][1]) % 998244353; dp[u][1] = (1ll * ori[1] * (dp[v][0] + dp[v][1]) + 1ll * ori[0] * dp[v][1]) % 998244353; } }; } int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; ++i) scanf("%d", &col[i]); for (int i = 1; i < n; ++i) { int u, v; scanf("%d%d", &u, &v); eu[i] = u, ev[i] = v; add_edge(u, v); } dfs0(1, 0); memset(col2lca + 1, -1, sizeof(*col2lca) * (unsigned)n); for (int i = 1; i <= n; ++i) if (col[i]) { if (col2lca[col[i]] == -1) col2lca[col[i]] = i; else col2lca[col[i]] = lca(col2lca[col[i]], i); } for (int i = 1; i <= n; ++i) if (col[i]) { int u = i, c = col[i], b = col2lca[col[i]]; ; while (dep[u] >= dep[b]) { if (ucol[u]) { if (ucol[u] != c) { puts("0"); return 0; } else break; } else { bel[u] = b; ucol[u] = c; u = fa[u][0]; } } } for (int i = 1; i <= n; ++i) if (!bel[i]) bel[i] = i; for (int i = 1; i <= n; ++i) ; for (int i = 1; i < n; ++i) if (bel[eu[i]] != bel[ev[i]]) add_edge2(bel[eu[i]], bel[ev[i]]); dfs2(1, 0); printf("%d\n", dp[1][1] % 998244353); return 0; }
6
#include <cstdio> #include <cstring> #include <queue> #define MAXN 510 using namespace std; int n,m; queue<int> a[MAXN]; int cnt[MAXN]; bool removed[MAXN]; int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ int t; scanf("%d",&t); a[i].push(t); } } for(int i=1;i<=n;i++) cnt[a[i].front()]++; int ans=n; for(int i=1;i<=m;i++){ int res=0; int maxj; for(int j=1;j<=m;j++) if(!removed[j] && cnt[j]>res) res=cnt[j],maxj=j; if(res<ans) ans=res; removed[maxj]=1; for(int i=1;i<=n;i++) while(removed[a[i].front()]){ a[i].pop(); cnt[a[i].front()]++; } } printf("%d\n",ans); }
0
#include <bits/stdc++.h> using namespace std; bool isVowel(char c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'y') { return true; } return false; } int main() { ios::sync_with_stdio(false); cin.tie(0); string s; getline(cin, s); for (int i = s.length() - 2; i >= 0; i--) { if (s[i] != ' ' && s[i] != '?') { s[i] = tolower(s[i]); if (isVowel(s[i])) { cout << "YES\n"; } else { cout << "NO\n"; } break; } } return 0; }
1
#include <bits/stdc++.h> #pragma comment(linker, "/stack:247474112") #pragma GCC optimize("Ofast") using namespace std; const int INF = 0x3f3f3f3f; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<int> p(n, 0); for (long long i = 0; i < n; ++i) { cin >> p[i]; --p[i]; } set<int> fw[n]; for (long long i = 0; i < m; ++i) { int u, v; cin >> u >> v; fw[--v].insert(--u); } int id = p[n - 1]; int ans = 0; for (int i = n - 1; i > 0; --i) { if (fw[p[i]].find(p[i - 1]) != fw[p[i]].end()) { fw[p[i]].erase(p[i - 1]); swap(p[i], p[i - 1]); if (p[i - 1] == id) { ++ans; } else { i += 2; } } } cout << ans << endl; }
4
// // main.cpp // Problem Solving // // Created by Murad Al Wajed on 11/2/21. // #include<cstdio> #include<iostream> #include<vector> #include<map> #include<stack> #include<queue> #include<bitset> #include<list> #include<iomanip> #include<string> #include<climits> #include<sstream> #include<fstream> #include<cctype> #include<time.h> #include<assert.h> #include<set> #include<numeric> #include<functional> #include<cstring> #include<cmath> #include<iterator> #include<memory.h> #include<utility> #include<ctime> #include<algorithm> #define ll long long #define dbl double #define vl vector<ll> #define sf(zz) scanf("%I64d",&zz) #define sf2(zz,zzz) scanf("%I64d %I64d",&zz,&zzz) #define sf3(zz,zzz,zzzz) scanf("%I64d %I64d %I64d",&zz,&zzz,&zzzz) #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); #define read freopen("in.txt","r",stdin) #define write freopen("out.txt","w",stdout) #define reset(a,d) memset(a,d,sizeof(a)) #define pb(a,b) a.push_back(b) #define min3(a,b,c) min(a,min(b,c)) #define max3(a,b,c) max(a,max(b,c)) #define min4(a,b,c,d) min(min(a,b),min(c,d)) #define max4(a,b,c,d) max(max(a,b),max(c,d)) #define DIST(x1,y1, x2, y2) (((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2))) #define DIST3D(x1,x2, y1, y2, z1, z2) (((x1-x2)*(x1-x2))+((y1-y2)*(y1-y2)) + ((z1-z2)*(z1-z2))) #define ALL(x) (x).begin(),(x).end() #define LLA(x) x.rbegin(), x.rend() #define SORT(v) sort(ALL(v)) #define inf 1e15 #define PI acos(-1.0) std::string sss="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; //-----------------------------------------------------------// using namespace std; ll arr[100+10], dep[100+10]; void dp(ll l, ll r, ll cnt){ if(l>r)return; ll mx=0,mxind=l; for(int i=l;i<=r;i++){ if (arr[i] > mx){ mx = arr[i]; mxind = i; } } dep[mxind] = cnt; dp(l, mxind-1, cnt+1); dp(mxind+1,r, cnt+1); } void CASE(){ // cout<<"hm\n"; ll n,i,j; cin>>n; reset(arr,0); reset(dep,0); for(i=0;i<n;i++){ cin>>arr[i]; } dp(0,n-1,0); for(i=0;i<n;i++){ cout<<dep[i]<<" "; } cout<<"\n"; } int main(){ int t = 1; cin>>t; for(int i=0;i<t;i++)CASE(); }
4
#include <bits/stdc++.h> using namespace std; using ll = long long; const int MAXN = 222222; const int INF = 0x3f3f3f3f; const ll LINF = 0x3f3f3f3f3f3f3f3fLL; template <typename T> T gcd(T a, T b) { while (b) { a %= b; swap(a, b); } return a; } class Rational { public: using TBase = long long; static const Rational ZERO; static const Rational ONE; static const Rational TWO; static const double EPS; TBase a, b; Rational() : a(0), b(1) {} Rational(TBase _a) : a(_a), b(1) {} Rational(TBase _a, TBase _b) : a(_a), b(_b) { norm(); } operator bool() const { return (a != 0); } operator long long() const { return a / b; } operator long double() const { return (a + 0.) / b; } bool operator!() const { return !a; } bool operator&&(const bool x) const { return (a > 0 && x); } Rational operator-() const { return Rational(-a, b); } Rational operator+() const { return Rational(a, b); } Rational& operator++() { a += b; return *this; } Rational operator++(int) { a += b; return Rational(a - b, b); } Rational& operator--() { a -= b; return *this; } Rational operator--(int) { a -= b; return Rational(a + b, b); } Rational operator+(const Rational& x) const { return Rational(a * x.b + x.a * b, b * x.b); } Rational operator-(const Rational& x) const { return Rational(a * x.b - x.a * b, b * x.b); } Rational operator*(const Rational& x) const { return Rational(a * x.a, b * x.b); } Rational operator/(const Rational& x) const { return Rational(a * x.b, b * x.a); } Rational& operator+=(const Rational& x) { a = a * x.b + x.a * b; b *= x.b; norm(); return *this; } Rational& operator-=(const Rational& x) { a = a * x.b - x.a * b; b *= x.b; norm(); return *this; } Rational& operator*=(const Rational& x) { a *= x.a; b *= x.b; norm(); return *this; } Rational& operator/=(const Rational& x) { a *= x.b; b *= x.a; norm(); return *this; } Rational operator+(const int x) const { return Rational(a + static_cast<TBase>(x) * b, b); } Rational operator-(const int x) const { return Rational(a - static_cast<TBase>(x) * b, b); } Rational operator*(const int x) const { return Rational(a * static_cast<TBase>(x), b); } Rational operator/(const int x) const { return Rational(a, b * static_cast<TBase>(x)); } Rational& operator+=(const int x) { a += b * static_cast<TBase>(x); return *this; } Rational& operator-=(const int x) { a -= b * static_cast<TBase>(x); return *this; } Rational& operator*=(const int x) { a *= static_cast<TBase>(x); norm(); return *this; } Rational& operator/=(const int x) { b *= static_cast<TBase>(x); norm(); return *this; } Rational operator+(const ll x) const { return Rational(a + static_cast<TBase>(x) * b, b); } Rational operator-(const ll x) const { return Rational(a - static_cast<TBase>(x) * b, b); } Rational operator*(const ll x) const { return Rational(a * static_cast<TBase>(x), b); } Rational operator/(const ll x) const { return Rational(a, b * static_cast<TBase>(x)); } Rational& operator+=(const ll x) { a += b * static_cast<TBase>(x); return *this; } Rational& operator-=(const ll x) { a -= b * static_cast<TBase>(x); return *this; } Rational& operator*=(const ll x) { a *= static_cast<TBase>(x); norm(); return *this; } Rational& operator/=(const ll x) { b *= static_cast<TBase>(x); norm(); return *this; } double operator+(const double x) const { return (a + 0.) / b + x; } double operator-(const double x) const { return (a + 0.) / b - x; } double operator*(const double x) const { return (a + 0.) / b * x; } double operator/(const double x) const { return (a + 0.) / b / x; } bool operator==(const Rational& x) const { return (a == x.a && b == x.b); } bool operator!=(const Rational& x) const { return (a != x.a || b != x.b); } bool operator<(const Rational& x) const { return (a * x.b < x.a * b); } bool operator>(const Rational& x) const { return (a * x.b > x.a * b); } bool operator<=(const Rational& x) const { return (a * x.b <= x.a * b); } bool operator>=(const Rational& x) const { return (a * x.b >= x.a * b); } bool operator==(const int x) const { return (a == static_cast<TBase>(x) * b); } bool operator!=(const int x) const { return (a != static_cast<TBase>(x) * b); } bool operator<(const int x) const { return (a < static_cast<TBase>(x) * b); } bool operator>(const int x) const { return (a > static_cast<TBase>(x) * b); } bool operator<=(const int x) const { return (a <= static_cast<TBase>(x) * b); } bool operator>=(const int x) const { return (a >= static_cast<TBase>(x) * b); } bool operator==(const ll x) const { return (a == static_cast<TBase>(x) * b); } bool operator!=(const ll x) const { return (a != static_cast<TBase>(x) * b); } bool operator<(const ll x) const { return (a < static_cast<TBase>(x) * b); } bool operator>(const ll x) const { return (a > static_cast<TBase>(x) * b); } bool operator<=(const ll x) const { return (a <= static_cast<TBase>(x) * b); } bool operator>=(const ll x) const { return (a >= static_cast<TBase>(x) * b); } bool operator==(const double x) const { return (fabs((a + 0.) / b - x) < EPS); } bool operator!=(const double x) const { return (fabs((a + 0.) / b - x) > EPS); } bool operator<(const double x) const { return ((a + 0.) / b + EPS < x); } bool operator>(const double x) const { return ((a + 0.) / b > x + EPS); } bool operator<=(const double x) const { return ((a + 0.) / b + EPS <= x); } bool operator>=(const double x) const { return ((a + 0.) / b >= x + EPS); } void norm() { TBase g = gcd(a, b); a /= g; b /= g; if (b < 0) { a = -a; b = -b; } } Rational negate() const { return Rational(-a, b); } Rational abs() const { return (a < 0 ? Rational(-a, b) : Rational(a, b)); } Rational inverse() const { return Rational(b, a); } int sign() const { return (a < 0 ? -1 : (a > 0)); } double doubleValue() const { return (a + 0.) / b; } TBase Floor() const { return a / b; } TBase Ceil() const { return static_cast<TBase>(ceil((a + 0.) / b - EPS)); } TBase Round() const { double cur = (a + 0.) / b; return static_cast<TBase>( (cur < 0 ? ceil(cur - .5 - EPS) : floor(cur + .5 + EPS))); } double frac() const { return (a + 0.) / b - a / b; } ll hashCode() const { return a * 877117 + b; } friend ostream& operator<<(ostream& out, Rational& x) { return out << x.a << "/" << x.b; } }; const Rational Rational::ZERO = Rational(0); const Rational Rational::ONE = Rational(1); const Rational Rational::TWO = Rational(2); const double Rational::EPS = EPS; int n; ll pref[MAXN]; vector<ll> a; Rational check(int offset, int idx) { return std::move( Rational(pref[idx] - pref[idx - offset - 1] + pref[n] - pref[n - offset], offset * 2 + 1) - Rational(a[idx])); } template <typename T> bool umax(T& value, const T& rhs) { if (value < rhs) { value = std::move(rhs); return true; } return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); while (cin >> n) { a.resize(n + 1); a[0] = 0; for (int i = 1; i <= n; ++i) { cin >> a[i]; } sort(a.begin(), a.end()); pref[0] = 0; for (int i = 1; i <= n; ++i) { pref[i] = pref[i - 1] + a[i]; } Rational total_best = -INF; int ax = 0, ay = 0; for (int i = 1; i <= n; ++i) { int L = 0, R = min(i - 1, n - i); while (R - L > 2) { int diff = (R - L) / 3; int m1 = L + diff, m2 = R - diff; if (check(m1, i) < check(m2, i)) { L = m1; } else { R = m2; } } Rational val = std::move(check(L, i)); int best = L; for (int j = L + 1; j <= R; ++j) { if (umax(val, std::move(check(j, i)))) { best = j; } } if (umax(total_best, val)) { ay = best; ax = i; } } cout << (ay * 2 + 1) << endl; for (int i = ax - ay; i <= ax; ++i) { cout << a[i] << " "; } for (int i = n - ay + 1; i <= n; ++i) { cout << a[i] << " "; } cout << endl; } return 0; }
5
#include <bits/stdc++.h> int last[1000100], next[10000100]; long long val[10000100]; int cnt[10000100]; int c[2010]; int prime[2010], top, pp; inline void add(long long &s, int &ss) { val[++top] = s; cnt[top] = ss; next[top] = last[s % 1000007]; last[s % 1000007] = top; } inline int find(long long &x) { for (int i = last[x % 1000007]; i; i = next[i]) if (x == val[i]) return cnt[i]; return -1; } int f(int x, int y, int z, int p) { if (p == 0) return 1; long long s = ((long long)(x * 2001 + y) * 2001 + z) * (pp + 1) + p; if (~find(s)) return find(s); int a, b, c, aa, bb, cc, ret = 0; for (a = 0, aa = 1; aa <= x; ++a, aa *= prime[p]) for (b = 0, bb = 1; bb <= y; ++b, bb *= prime[p]) for (c = 0, cc = 1; cc <= z; ++c, cc *= prime[p]) ret = ((long long)(a + b + c + 1) * f(x / aa, y / bb, z / cc, p - 1) + ret) & 1073741823; add(s, ret); return ret; } int main() { int i, j, x, y, z; for (i = 2; i <= 2000; ++i) if (c[i] == 0) { prime[++top] = i; for (j = i * i; j <= 2000; j += i) c[j] = 1; } pp = top; top = 0; scanf("%d%d%d", &x, &y, &z); printf("%d\n", f(x, y, z, pp)); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; long long left_weight = 0; long long right_weight = 0; long long l_torque = 0; long long r_torque = 0; int lever; for (int i = 0; i < s.length(); i++) { if (s[i] == '^') { lever = i; } } int j; for (int i = lever - 1, j = 1; i >= 0; i--, j++) { if (s[i] == '=') continue; int digit = s[i] - '0'; left_weight += digit; l_torque += digit * j; } for (int i = lever + 1, j = 1; i < s.length(); i++, j++) { if (s[i] == '=') continue; int digit = s[i] - '0'; right_weight += digit; r_torque += digit * j; } if (l_torque > r_torque) { cout << "left"; } else if (l_torque < r_torque) { cout << "right"; } else { cout << "balance"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 6e5 + 51; int n, r, pos, c; long double res; int x[MAXN]; inline int read() { register int num = 0, neg = 1; register char ch = getchar(); while (!isdigit(ch) && ch != '-') { ch = getchar(); } if (ch == '-') { neg = -1; ch = getchar(); } while (isdigit(ch)) { num = (num << 3) + (num << 1) + (ch - '0'); ch = getchar(); } return num * neg; } int main() { n = 1 << read(), r = read(); for (register int i = 0; i < n; i++) { x[i] = read(), res += x[i]; } cout << fixed << setprecision(8) << res / n << endl; for (register int i = 1; i <= r; i++) { pos = read(), c = read(), res -= x[pos], res += (x[pos] = c); cout << fixed << setprecision(8) << res / n << endl; } }
6
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; char s[maxn], ss[maxn]; int mp[maxn][27], la[27], len; bool check(int mid) { int sum = 0; for (int i = 0; i < 26; i++) if (mp[mid][i] >= la[i]) sum++; if (sum == 26) return 1; return 0; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int n; cin >> n; cin >> s + 1; memset(mp, 0, sizeof(mp)); for (int i = 1; i <= n; i++) { mp[i][s[i] - 'a']++; for (int j = 0; j < 26; j++) mp[i][j] += mp[i - 1][j]; } int m; cin >> m; while (m--) { memset(la, 0, sizeof(la)); cin >> ss + 1; len = strlen(ss + 1); for (int i = 1; i <= len; i++) la[ss[i] - 'a']++; int l = 1, r = n, ans = l + r >> 1; while (l <= r) { int mid = l + r >> 1; if (check(mid)) r = mid - 1, ans = mid; else l = mid + 1; } cout << ans << "\n"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; typedef complex<double> P; string l(string s){ if( s[0] <= 'Z' ) s[0] = s[0] - 'Z' + 'z'; return s; } int main(){ map<string,int> d; string s; int mx = 0; string v = ""; while( cin >> s ){ mx = max(mx,++d[l(s)]); if( v.size() < s.size() ) v = l(s); } for( auto i : d) if( mx == i.second) cout << i.first << " "; cout << v << endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long ans = 0; for (int i = (0); i < (n); ++i) { long long t, T, x, cost; cin >> t >> T >> x >> cost; if (t >= T) { ans += cost + m * x; continue; } long long aux1 = cost; if (m > (T - t)) aux1 += m * x; long long aux2 = (long long)ceil((double)(m - (T - t)) / (T - t)) + 1; aux2 *= cost; ans += min(aux1, aux2); } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long GCD(long long a, long long b) { return (a) ? GCD(b % a, a) : b; } long long LCM(long long a, long long b) { return a * b / GCD(a, b); } long long fastpow(long long b, long long p) { if (!p) return 1; long long ret = fastpow(b, p >> 1); ret *= ret; if (p & 1) ret *= b; return ret; } string alpha = "abcdefghijklmnopqrstuvwxyz"; int divisor(int number) { int i; for (i = 2; i <= sqrt(number); i++) { if (number % i == 0) { return number / i; } } return 1; } int myXOR(int x, int y) { int res = 0; for (int i = 31; i >= 0; i--) { bool b1 = x & (1 << i); bool b2 = y & (1 << i); bool xoredBit = (b1 & b2) ? 0 : (b1 | b2); res <<= 1; res |= xoredBit; } return res; } void printDivisors(int n, vector<int>& v) { for (int i = 1; i <= sqrt(n); i++) { if (n % i == 0) { if (n / i == i && i > 1) v.push_back(i); else { if (i > 1) v.push_back(i); if (n / i > 1) v.push_back(n / i); } } } } int bin(vector<int> vec, int val) { int l = 0, r = vec.size() - 1, mid = r / 2; while (l <= r) { mid = (l + r) / 2; if (vec[mid] < val && vec[mid + 1] > val) { if (vec[mid + 1] == val) return mid + 1; return mid; } else if (vec[mid] > val) { r = mid - 1; } else if (vec[mid] < val) { l = mid + 1; } else if (vec[mid] == val) return mid; } return -1; } void clear(vector<bool> v) { for (int i = 0; i < v.size(); i++) v[i] = 0; } bool comp(const string& s1, const string& s2) { return s2 + s1 < s1 + s2; } vector<string> split(const string& s, char delim) { vector<string> result; stringstream ss(s); string item; while (getline(ss, item, delim)) { result.push_back(item); } return result; } int countWords(string str) { stringstream s(str); string word; int count = 0; while (s >> word) count++; return count; } long long power(long long x, long long y, long long p, int& c) { int res = 1; if (x >= p) c++; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) { if (res * x >= p) c++; res = (res * x) % p; } y = y >> 1; if (x * x >= p) c++; x = (x * x) % p; } return res; } bool isPalindrome(string str) { int l = 0; int h = str.length() - 1; while (h > l) { if (str[l++] != str[h--]) { return 0; } } return 1; } bool checkMuns(vector<int> vec) { for (int i = 0; i < (int)(vec.size()); ++i) { if (vec[i] < 0) return 0; } return 1; } void fast() { ios_base::sync_with_stdio(false); cin.tie(NULL); } long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; if (x == 0) return 0; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long ceiLl(long long x, long long y) { return (x + y - 1) / y; } vector<long long> divisors(long long n) { vector<long long> ans; long long i = 1; for (; i * i < n; i++) { if (n % i == 0) { ans.push_back(i); ans.push_back(n / i); } } if (i * i == n) ans.push_back(i); return ans; } vector<long long> findDivisors(long long n) { if (n == 1) return vector<long long>(1, 0); vector<long long> numFactors(n + 1); for (int i = 1; i <= n; i++) { for (int j = 1; j * i <= n; j++) numFactors[i * j]++; } return numFactors; } vector<long long> factorization(long long n) { vector<long long> primes; for (long long i = 2; i * i <= n; ++i) { if (n % i == 0) { primes.push_back(i); while (n % i == 0) { n /= i; } } } if (n > 1) primes.push_back(n); return primes; } bool isSubSeq(string a, string b) { int idx = 0; for (int i = 0; i < a.length(); i++) { if (a[i] == b[idx]) idx++; } if (idx == b.length()) return 1; else return 0; } bool isSubStr(string a, string b) { for (int i = 0; i < a.length(); i++) { if (a.substr(i, b.length()) == b) return 1; } return 0; } bool sortbysec(const pair<int, int>& a, const pair<int, int>& b) { return (a.second < b.second); } bool isEqual(string a, string b) { if (a.size() != b.size()) return 0; sort(((a).begin()), ((a).end())); sort(((b).begin()), ((b).end())); if (a == b) return 1; return 0; } string bin(unsigned n) { string ans = ""; unsigned i; for (i = 1 << 14; i > 0; i = i / 2) (n & i) ? ans += '1' : ans += '0'; return ans; } string leftrotate(string a, int d) { string s = a; reverse(s.begin(), s.begin() + d); reverse(s.begin() + d, s.end()); reverse(s.begin(), s.end()); return s; } string rightrotate(string a, int d) { string s = a; return leftrotate(s, s.length() - d); } int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } long long findlcm(vector<long long> arr, int n) { long long ans = arr[0]; for (int i = 1; i < n; i++) ans = (((arr[i] * ans)) / (gcd(arr[i], ans))); return ans; } long long findGCD(vector<long long> arr) { int n = arr.size(); long long result = arr[0]; for (int i = 1; i < n; i++) { result = gcd(arr[i], result); if (result == 1) { return 1; } } return result; } long long countDivisors(long long n) { long long cnt = 0; for (int i = 1; i <= sqrt(n); i++) { if (n % i == 0) { if (n / i == i) cnt++; else cnt = cnt + 2; } } return cnt; } int main() { fast(); int t; t = 1; map<long long, int> valid; int i = 0; while (1) { long long x = pow(2, i); if (x > 1000000000) break; valid[x] = 1; i++; } i = 0; while (1) { long long x = pow(3, i); if (x > 1000000000) break; valid[x] = 1; i++; } while (t--) { int n; cin >> n; vector<long long> vec(n); long long ma = 0; for (int i = 0; i < (int)(n); ++i) { cin >> vec[i]; ma = max(ma, vec[i]); } for (int i = 0; i < (int)(n); ++i) { while (vec[i] % 2 == 0) { vec[i] /= 2; } while (vec[i] % 3 == 0) { vec[i] /= 3; } } set<int> s; for (int i = 0; i < (int)(n); ++i) s.insert(vec[i]); if (s.size() == 1) cout << "YES"; else cout << "NO"; cout << "\n"; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int n; int a[2005]; int dp1[2005][2005]; int dp2[2005][2005]; int temp[2005]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) { int len = 0; memset(temp, 0, sizeof(temp)); for (int j = i; j <= n; j++) { if (a[j] >= temp[len]) temp[++len] = a[j]; else { int pos = upper_bound(temp + 1, temp + len + 1, a[j]) - temp; temp[pos] = a[j]; } dp1[i][j] = len; } } for (int i = n; i >= 1; i--) { int len = 0; memset(temp, 0, sizeof(temp)); for (int j = i; j >= 1; j--) { if (a[j] >= temp[len]) temp[++len] = a[j]; else { int pos = upper_bound(temp + 1, temp + len + 1, a[j]) - temp; temp[pos] = a[j]; } dp2[j][i] = len; } } int result = 0; for (int i = 1; i <= n; i++) for (int j = i; j <= n; j++) result = max(result, dp1[1][n] - dp1[i][j] + dp2[i][j]); cout << result << endl; return 0; }
3
#include<iostream> #include<stack> using namespace std; //union_find begin #define MAX 5000000 struct node{ int parent,size; }; node tree[MAX]; void init(){ for(int i=0;i<MAX;i++){ tree[i].parent=i; tree[i].size=1; } } /* int find(int x){ if(tree[x].parent==x)return x; tree[x].parent = find( tree[x].parent ); return tree[x].parent; } */ int st[MAX],cnt; int find(int x){ cnt=0; while(tree[x].parent!=x){ st[cnt++]=x; x=tree[x].parent; } while(cnt>0){ cnt--; tree[st[cnt]].parent=x; } return x; } int count(int x){ x=find(x); return tree[x].size; } bool same(int x,int y){ return ( find(x) == find(y) ); } void unite(int x,int y){ x=find(x); y=find(y); if(x==y)return; if(count(x)>count(y))swap(x,y); tree[x].parent=y; tree[y].size+=tree[x].size; } // union find end int dy[]={-1,0,1,0}; int dx[]={0,1,0,-1}; int W,H,N,target; char t[1005][1005]; int change(int i,int j){ return i*W+j; } int solve(){ cin>>N; for(int ans=1;ans<=N;ans++){ int i,j; cin>>j>>i; if(t[i][j]=='.')continue; t[i][j]='.'; for(int k=0;k<4;k++){ int ni=i+dy[k]; int nj=j+dx[k]; if(ni<0 || nj<0 || H<=ni || W<=nj)continue; if(t[ni][nj]=='#')continue; unite( change(i,j) , change(ni,nj) ); } if(same(target,0))return ans; } return -1; } int main(){ init(); cin>>W>>H; for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ cin>>t[i][j]; if(t[i][j]=='t')target=change(i,j); } } for(int i=0;i<H;i++){ for(int j=0;j<W;j++){ if(t[i][j]=='#')continue; for(int k=0;k<4;k++){ int ni=i+dy[k]; int nj=j+dx[k]; if(ni<0 || nj<0 || H<=ni || W<=nj)continue; if(t[ni][nj]=='#')continue; unite( change(i,j) , change(ni,nj) ); } } } if( same( target , 0 ) ){ cout<<"0"<<endl; }else{ cout<<solve()<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long int head[100005]; vector<long long int> track; int n, mm; inline bool check(long long int m) { int x = 0; for (int i = 0; i < n; i++) { long long int tem = m; if (track[x] <= head[i] && head[i] - track[x] <= tem) { long long int piche = head[i] - track[x]; tem -= piche; tem = max(tem / 2, tem - piche); while (track[x] <= head[i] + tem) { x++; if (x == mm) return true; } } else if (track[x] > head[i]) { while (track[x] <= head[i] + m) { x++; if (x == mm) return true; } } } return false; } int main() { scanf("%d %d", &n, &mm); for (int i = 0; i < n; i++) scanf("%I64d", &head[i]); for (int i = 0; i < mm; i++) { long long int x; scanf("%I64d", &x); track.push_back(x); } long long int s = 0ll; long long int e = 20000000005ll; long long int ans = e; while (s <= e) { long long int m = (s + e) / 2ll; if (check(m)) ans = m, e = m - 1ll; else s = m + 1ll; } printf("%I64d\n", ans); ; return 0; }
3
#include <bits/stdc++.h> using namespace std; multimap<int, int> chk; int N, M, T, R, A[20002]; int main() { scanf("%d %d %d", &N, &M, &T); bool good = 0; for (int i = 0; i < N; i++) { int h, m, s; scanf("%d:%d:%d", &h, &m, &s); int p = h * 3600 + m * 60 + s; while (!chk.empty() && chk.begin()->first + T <= p) chk.erase(chk.begin()); if (chk.size() == M) { int last = chk.end().operator--()->second; chk.erase(chk.end().operator--()); A[i] = last; chk.insert(make_pair(p, last)); } else { A[i] = ++R; chk.insert(make_pair(p, R)); } if (chk.size() == M) good = 1; } if (good) { printf("%d\n", R); for (int i = 0; i < N; i++) printf("%d\n", A[i]); } else puts("No solution"); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 100000 + 10; const int MOD = 1000000000 + 7; const int INF = 1000000000 + 10; long long n, m, ret, p; long long mu[N], notP[N]; long long calc(long long lim) { long long ret = 0; for (long long d = 1; d <= n; ++d) { for (long long x = d; x <= n; x += d) { long long l = 0, r = m + 10, mid; for (; l + 1 < r;) { mid = (l + r) / 2; if (d * mid > m) { r = mid; continue; } if ((mid * d) * (mid * d) + x * x < lim) l = mid; else r = mid; } if (l == 0) break; ret += 2 * mu[d] * (n + 1 - x) % p * ((l * (m + 1) - (l * (l + 1) / 2) % p * d) % p + p) % p; ret = (ret + p) % p; } } if (lim > 1) { ret = (ret + n * (m + 1) + m * (n + 1)) % p; } return ret; } void solve() { for (int i = 1; i < N; ++i) { mu[i] = 1; } for (long long i = 2; i < N; ++i) { if (notP[i]) continue; for (long long j = i; j < N; j += i) { if (j % (i * i) == 0) mu[j] = 0; else { mu[j] = -mu[j]; } notP[j] = true; } } long long l, r; cin >> n >> m; if (n > m) swap(n, m); cin >> l >> r; cin >> p; cout << (calc(r * r + 1) - calc(l * l) + p) % p << endl; } int main() { solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; int t, n, a[10000]; int main() { int t = 1; cin >> t; while (t--) { cin >> n; int ans = 1000000; for (int i = 1; i <= n; i++) cin >> a[i]; for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) ans = min(ans, abs(a[i] - a[j])); cout << ans << "\n"; } }
2
#include <bits/stdc++.h> using namespace std; const bool DEBUG = true; using ll = long long; using ii = pair<int, int>; using vi = vector<int>; using vii = vector<ii>; const double PI = acos(-1); const double EPS = 1e-9; int main() { ios::sync_with_stdio(0); int t; cin >> t; while (t--) { int n, x; cin >> n >> x; priority_queue<int> q; int db = 0; for (int i = 0; i < n; ++i) { int a, b; cin >> a >> b; if (a > b) q.push(a - b); db = max(db, a); } int ans = 1; x -= db; if (x > 0 and q.empty()) { cout << -1 << endl; continue; } else if (x > 0) { ans += ceil(double(x) / double(q.top())); } cout << ans << endl; } }
2
#include <bits/stdc++.h> using namespace std; int n, x, y, z; int init; long long int dp[3][105], arr[300005], period; long long int getGrundy(int p, int s) { if (s == 0) return dp[p][s] = 0; if (dp[p][s] != -1) return dp[p][s]; set<long long int> st; st.insert(-1); if (p == 0) { st.insert(getGrundy(0, max(0, s - x))); st.insert(getGrundy(1, max(0, s - y))); st.insert(getGrundy(2, max(0, s - z))); } else if (p == 1) { st.insert(getGrundy(0, max(0, s - x))); st.insert(getGrundy(2, max(0, s - z))); } else if (p == 2) { st.insert(getGrundy(0, max(0, s - x))); st.insert(getGrundy(1, max(0, s - y))); } vector<long long int> ans; for (long long int e : st) ans.push_back(e); for (int i = 0, colchin = ans.size(); i < colchin; ++i) if (i == ans.size() - 1 || ans[i] + 1 != ans[i + 1]) return dp[p][s] = ans[i] + 1; } long long int gg(int p, long long int s) { if (s < 100) return dp[p][(int)s]; long long int k = (s - 100) / period; k++; int news = s - k * period; assert(news >= init); return dp[p][news]; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int T; cin >> T; while (T--) { cin >> n >> x >> y >> z; memset(dp, -1, sizeof dp); for (int i = 0, colchin = 100; i < colchin; ++i) getGrundy(0, i), getGrundy(1, i), getGrundy(2, i); vector<pair<long long int, pair<long long int, long long int> > > base; for (int l = 0, colchin = 5; l < colchin; ++l) { pair<long long int, pair<long long int, long long int> > tuple = pair<long long int, pair<long long int, long long int> >( getGrundy(0, l), pair<long long int, long long int>( getGrundy(1, l), getGrundy(2, l))); base.push_back(tuple); } map<vector<pair<long long int, pair<long long int, long long int> > >, int> mp; mp[base] = 4; int index = -1e8; for (int l = 5, colchin = 100; l < colchin; ++l) { pair<long long int, pair<long long int, long long int> > tuple = pair<long long int, pair<long long int, long long int> >( getGrundy(0, l), pair<long long int, long long int>( getGrundy(1, l), getGrundy(2, l))); vector<pair<long long int, pair<long long int, long long int> > > nwbase = base; nwbase.erase(nwbase.begin()); nwbase.push_back(tuple); if (mp.count(nwbase) != 0) { index = l; base = nwbase; break; } else { mp[nwbase] = l; base = nwbase; } } period = index - mp[base]; init = mp[base] - 4; long long int xorsum = 0; for (int i = 0, colchin = n; i < colchin; ++i) cin >> arr[i], xorsum ^= gg(0, arr[i]); int cnt = 0; long long int attack[3]; attack[0] = x, attack[1] = y, attack[2] = z; for (int i = 0, colchin = n; i < colchin; ++i) { for (int j = 0, colchin = 3; j < colchin; ++j) { long long int newval = max((long long int)0, arr[i] - attack[j]); if ((gg(j, newval) ^ gg(0, arr[i]) ^ xorsum) == 0) cnt++; } } cout << cnt << "\n"; } return 0; }
6
#include <bits/stdc++.h> using namespace std; typedef signed long long ll; #undef _P #define _P(...) (void)printf(__VA_ARGS__) #define FOR(x,to) for(x=0;x<(to);x++) #define FORR(x,arr) for(auto& x:arr) #define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++) #define ALL(a) (a.begin()),(a.end()) #define ZERO(a) memset(a,0,sizeof(a)) #define MINUS(a) memset(a,0xff,sizeof(a)) //------------------------------------------------------- int N,K; deque<char> Q; void solve() { int i,j,k,l,r,x,y; string s; cin>>N>>K>>s; FORR(c,s) Q.push_back(c); bool rev=false; FOR(i,min(K,2*N+(K%2))) { if((Q[0]=='A')^rev) { Q[0]='A'+'B'-Q[0]; } else { Q.pop_front(); rev ^= 1; Q.push_back('A'+rev); } } FORR(r,Q) { if(rev) _P("%c",'A'+'B'-r); else _P("%c",r); } _P("\n"); } int main(int argc,char** argv){ string s;int i; if(argc==1) ios::sync_with_stdio(false), cin.tie(0); FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin); solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const long long INF = numeric_limits<long long>::max() / 3; template <class T> void mini(T &a, T b) { a = min(a, b); } template <class T> void maxi(T &a, T b) { a = max(a, b); } long long mrand() { return rand() * ((long long)RAND_MAX + 1LL) + rand(); } vector<long long> readVec(long long n) { vector<long long> result(n); for (long long i = 0; i < n; i++) { cin >> result[static_cast<std::size_t>(i)]; } return result; } void solve() { long long n; cin >> n; string input; cin >> input; while (input.size() > 0 && (input.back() - '0') % 2 == 0) { input.pop_back(); } if (input.size() == 0) { cout << -1 << endl; return; } long long sum = 0; for (char c : input) { sum += c - '0'; } if (sum % 2 == 1) { for (long long i = (long long)input.size() - 2; i >= 0; i--) { if ((input[i] - '0') % 2 == 1) { input.erase(input.begin() + i); cout << input << endl; return; } } cout << -1 << endl; return; } cout << input << endl; return; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); srand(chrono::steady_clock::now().time_since_epoch().count()); int t = 1; cin >> t; for (int i = 0; i < t; i++) { solve(); } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int INF = (int)1.01e9; const int N = 1 << 18; const int MOD = (int)1e9 + 7; int main() { int n; while (scanf("%d", &n) == 1) { vector<int> a(n), b(n); for (int i = 0; i < n; i++) { scanf("%d%d", &a[i], &b[i]); a[i]--; b[i]--; } vector<int> nxt(2 * n, -1); vector<vector<int> > e(2 * n); vector<vector<int> > g(2 * n); for (int i = 0; i < n; i++) { nxt[a[i]] = b[i]; e[a[i]].push_back(b[i]); g[b[i]].push_back(a[i]); } vector<int> c(2 * n); function<int(int)> dfs = [&](int v) { if (c[v] != 0) return 0; c[v] = 2; int ans = 1; for (int to : g[v]) ans += dfs(to); return ans; }; int ans = 1; for (int i = 0; i < 2 * n; i++) { if (e[i].empty() && !g[i].empty()) { ans = 1LL * ans * dfs(i) % MOD; } } for (int i = 0; i < 2 * n; i++) { if (nxt[i] == -1) continue; if (c[i] != 0) continue; int v = i; while (v != -1) { if (c[v] != 0) break; c[v] = 1; v = nxt[v]; } assert(v != -1); if (c[v] == 1) { int len = 0; while (c[v] == 1) { c[v] = 2; v = nxt[v]; len++; } if (len > 1) ans = 2LL * ans % MOD; } bool need = 1; v = i; while (c[v] == 1) { c[v] = 2; v = nxt[v]; } } printf("%d\n", ans); } return 0; }
5
#include <bits/stdc++.h> using namespace std; string s[12] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "B", "H"}, a, b, c; int x[12]; int main() { std::ios::sync_with_stdio(false); cin >> a >> b >> c; for (int i = 0; i < 12; i++) { if (a == s[i] || b == s[i] || c == s[i]) x[i] = 1; } for (int i = 0; i < 12; i++) { if (x[i]) { if (x[(i + 4) % 12] && x[(i + 7) % 12]) { cout << "major" << endl; return 0; } if (x[(i + 3) % 12] && x[(i + 7) % 12]) { cout << "minor" << endl; return 0; } } } cout << "strange" << endl; }
1
#include <bits/stdc++.h> using namespace std; template <class T> inline bool checkMin(T& a, T b) { return (a > b ? a = b, 1 : 0); } template <class T> inline bool checkMax(T& a, T b) { return (a < b ? a = b, 1 : 0); } const int N = 5005; vector<int> e[N]; int n, m; void init() { scanf("%d%d", &n, &m); } int vis[N]; void dfs(int u) { if (vis[u]) return; vis[u] = 1; for (int i = 0; i < (int)e[u].size(); i++) { int vv = e[u][i]; dfs(vv); } } int ss[N]; void solve() { int ans = 1; for (int i = 0; i < m; i++) { int u, v; scanf("%d%d", &u, &v); u--; v--; memset(vis, 0, sizeof(vis)); dfs(v); if (vis[u]) { ans = 2; ss[i] = ans; } else { e[u].push_back(v); ss[i] = 1; } } printf("%d\n", ans); for (int i = 0; i < m; i++) printf("%d ", ss[i]); } int main() { init(); solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; int x[100100], y[100100], z[100100], w[100100], tx[100100 * 2], dis[100100], from[100100]; struct cmp { bool operator()(int i, int j) const { return y[i] < y[j]; } }; multiset<int, cmp> f[100100 * 2]; int main() { int n, m; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d%d%d%d", &x[i], &y[i], &z[i], &w[i]); tx[i * 2] = x[i]; tx[i * 2 + 1] = z[i]; } memset(dis, -1, sizeof(dis)); tx[0] = -1; sort(tx, tx + n * 2 + 2); m = unique(tx, tx + n * 2 + 2) - tx; for (int i = 1; i <= n; i++) { int nx = lower_bound(tx, tx + m, x[i]) - tx; while (nx < 100100 * 2) { f[nx].insert(i); nx += nx & -nx; } } queue<int> q; q.push(0); dis[0] = 0; while (!q.empty()) { int u = q.front(); q.pop(); int nx = lower_bound(tx, tx + m, z[u]) - tx; while (nx > 0) { while (!f[nx].empty() && w[u] >= y[*f[nx].begin()]) { int v = *f[nx].begin(); f[nx].erase(f[nx].begin()); if (dis[v] == -1) { dis[v] = dis[u] + 1; from[v] = u; q.push(v); } } nx -= nx & -nx; } } vector<int> ans; printf("%d\n", dis[n]); if (dis[n] >= 0) { for (int i = n; i; i = from[i]) ans.push_back(i); for (int i = ans.size() - 1; i >= 0; i--) printf("%d ", ans[i]); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N=3e5+5; int a[N],b[N]; int n,k; int ans=1; void solve(int v) { long long s = 0; for(int i=0;i<n;i++){ b[i]=a[i]%v; s += b[i]; } sort(b,b+n); long long now = 0; for(int i=n-1; i>=0; i--){ if(s>now){ now += v-b[i]; s -= b[i]; } } assert(now == s); if(s<=k) ans=max(ans,v); } int main() { ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); cin>>n>>k; int sum=0; for(int i=0;i<n;i++) { cin>>a[i]; sum+=a[i]; } for(long long i=1;i*i<=sum;i++) { if(sum%i==0) { solve(i); solve(sum/i); } } cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int32_t main() { cin.tie(0); cout.tie(0); ios::sync_with_stdio(false); int tc; cin >> tc; while (tc--) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; vector<array<int, 2>> dp(n + 1, {((int)(1e9 + 5)), ((int)(1e9 + 5))}); dp[0][1] = 0; for (int i = 0; i < n; i++) { for (int c = 0; c < 2; c++) { for (int j = 1; j <= min(2, n - i); j++) { int h = a[i] + (j > 1 ? a[i + 1] : 0); dp[i + j][!c] = min(dp[i + j][!c], dp[i][c] + c * h); } } } cout << min(dp[n][0], dp[n][1]) << "\n"; } return 0; }
3
#include <iostream> #include <cmath> #include <vector> #include <algorithm> using namespace std; vector<int> Vec; int n; int Ans; static const int INF = 1e+9; void DFS(int a, int b, int depth) { if(depth == n) { Ans = min(Ans, abs(a - b)); } else { DFS(a + Vec[depth], b, depth + 1); DFS(a, b + Vec[depth], depth + 1); } } void solve() { while(cin >> n, n) { Vec.resize(n); for(int i = 0; i < n; ++i) { cin >> Vec[i]; } Ans = INF; DFS(0, 0, 0); cout << Ans << endl; } } int main() { solve(); return(0); }
0
#include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <string> #include <iomanip> #include <set> #include <map> #include <queue> #include <bitset> #include <random> #include <chrono> #include <array> typedef long long ll; typedef long double ld; using namespace std; const ll mod = 998244353; const int maxn = 79; void upd(ll& a, const ll& b) { a += b; if (a >= mod) a -= mod; } ll pwr(ll a, ll b) { if (!b) return 1; ll h = pwr(a, b >> 1); h = (h * h) % mod; if (b & 1) h = (h * a) % mod; return h; } int n; ll x; vector<ll> d(maxn), s, inv(maxn * maxn, 1), l(maxn), r(maxn); vector<int> p; ll nck(ll n, ll k) // idealne pre male k a velke n { if (k > n || k < 0) return 0; ll ans = 1; for (ll i = 0; i < k; i++) ans = (ans * (((n - i) * inv[i + 1]) % mod)) % mod; //cout << "C(" << n << ", " << k << ") = " << ans << "\n"; return ans; } ll split(ll n, ll k) // tak ako je ta analogia s rozdelovacmi { if (k == 0) return 1; return nck(n + k - 1, k - 1); } int main() { ios::sync_with_stdio(false); cin.tie(0); for (int i = 1; i < inv.size(); i++) inv[i] = pwr(i, mod - 2); cin >> n >> x; bool dec = true; for (int i = 0; i < n; i++) { cin >> d[i]; if (d[i] > 0) dec = false; l[i] = (i ? r[i - 1] : x); r[i] = l[i] + d[i]; s.push_back(l[i]), s.push_back(r[i]); } if (dec) { cout << 1 << " " << (x - r[n - 1] + 1) % mod << "\n"; return 0; } sort(s.begin(), s.end()); s.erase(unique(s.begin(), s.end()), s.end()); for (int i = 0; i < n; i++) { int idl = -1, idr = -1; // idl tam uz dala ta predosla cesta, ten sa nerata for (int j = 0; j < s.size(); j++) { if (s[j] == l[i]) idl = j; if (s[j] == r[i]) idr = j; } if (i == 0) p.push_back(idl); if (idl < idr) for (int j = idl + 1; j <= idr; j++) p.push_back(j); else for (int j = idl - 1; j >= idr; j--) p.push_back(j); } ll len = 0, ans = 0; int m = p.size(); for (int i = 0; i < m; i++) for (int j = i + 1; j < m; j++) if (p[i] < p[j]) len = max(len, s[p[j]] - s[p[i]] + 1); vector<vector<ll> > ways(m, vector<ll>(m, 0)); for (int i = 0; i < m; i++) { for (int j = i + 1; j < m; j++) { if (p[i] + 1 != p[j]) continue; int up = 0, dw = 0; ll need = s[p[j]] - s[p[i]] - 1; // prvy a posledny prvok su uz zafixovane for (int k = i; k < j; k++) { if (p[k] == p[i] && p[k + 1] == p[j]) up++; if (p[k] == p[j] && p[k + 1] == p[i]) dw++; } for (int use = 0; use <= min((ll)dw, need); use++) upd(ways[i][j], (split(need - use, up) * nck(dw, use)) % mod); //cout << i << " " << j << " : " << ways[i][j] << " (" << up << ", " << dw << ")\n"; } } for (int i = 0; i < s.size(); i++) { vector<ll> dp(m, 0); // pre kazdy prvok si pamatame, ze kolko sposobov mame, ako urobit optimalnu lis, ak chceme zacat na prvku s hodnotou s[i]s for (int j = 0; j < m; j++) if (p[j] == i) dp[j] = 1; for (int j = 0; j < m; j++) for (int k = j + 1; k < m; k++) upd(dp[k], (dp[j] * ways[j][k]) % mod); //for (int j = 0; j < m; j++) cout << dp[j] << " "; //cout << "\n"; for (int j = 0; j < m; j++) if (s[p[j]] - s[i] + 1 == len) upd(ans, dp[j]); } cout << len << " " << ans << "\n"; return 0; }
6
#include<bits/stdc++.h> using namespace std; int n,m,cc,vis[705],col[705],num[705][2],f[2][705],e[705][705]; void dfs(int x,int c) { num[cc][c]++;col[x]=c;vis[x]=1; for(int i=1;i<=n;i++)if(x!=i&&!vis[i]&&!e[x][i])dfs(i,c^1);else if(x!=i&&!e[x][i]&&col[i]==c){puts("-1");exit(0);} } int main() { scanf("%d%d",&n,&m); for(int i=1,u,v;i<=m;i++){scanf("%d%d",&u,&v);e[u][v]=e[v][u]=1;} for(int i=1;i<=n;i++)if(!vis[i])cc++,dfs(i,0); f[0][0]=1; for(int i=1,b=1;i<=cc;i++,b^=1) { memset(f[b],0,sizeof(f[b])); for(int j=0;j<=n;j++)if(f[b^1][j])f[b][j+num[i][0]]=f[b][j+num[i][1]]=1; } int ans=1e9; for(int i=0;i<=n;i++)if(f[cc&1][i])ans=min(ans,i*(i-1)/2+(n-i)*(n-i-1)/2); printf("%d\n",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, i, c = 0, s, have, j; cin >> n; vector<long long> v(n), vv; for (i = 0; i < n; i++) { cin >> v[i]; c += v[i]; } if (c <= 1) { cout << "-1" << endl; return 0; } for (i = 2; i * i <= c; i++) if (c % i == 0) { vv.push_back(i); while (c % i == 0) c /= i; } if (c > 1) vv.push_back(c); long long mn = LLONG_MAX, x; for (j = 0; j < vv.size(); j++) { x = vv[j]; s = 0, have = 0; for (i = 0; i < n; i++) { have += v[i]; have %= x; s += min(have, x - have); } mn = min(mn, s); } cout << mn << endl; }
5
#include <bits/stdc++.h> int main() { long long n, num; long long i, j; while (scanf("%I64d", &n) != EOF) { num = 0; for (i = 0;; i++) { long long x = (2 * (i + 1) * (i + 2)) / 2 + (i * (i + 1)) / 2, y; if (n >= x) y = n - x; else break; if (y % 3 == 0) num++; } printf("%I64d\n", num); } return 0; }
3
#include <bits/stdc++.h> using namespace std; double ans[3010][3010], x, c; int f[3010], g[3010]; int main() { int i, j, rr, cc, m, n, a, b; scanf("%d %d", &n, &m); for (i = 1; i <= m; i++) { scanf("%d %d", &a, &b); f[a] = 1; g[b] = 1; } rr = 0; cc = 0; for (i = 1; i <= n; i++) if (f[i] == 0) rr++; for (i = 1; i <= n; i++) if (g[i] == 0) cc++; ans[0][0] = 0.0; int t = max(rr, cc); for (i = 1; i <= t; i++) { x = 1.0 * i * ans[i - 1][0] / n; c = 1.0 * (n - i) / n; ans[i][0] = 1.0 * x / (1 - c) + 1.0 / (1 - c); } for (i = 1; i <= t; i++) { x = 1.0 * i * ans[0][i - 1] / n; c = 1.0 * (n - i) / n; ans[0][i] = 1.0 * x / (1 - c) + 1.0 / (1 - c); } for (i = 1; i <= rr; i++) { for (j = 1; j <= cc; j++) { x = (1.0 * ans[i - 1][j - 1] * i * j + 1.0 * i * (n - j) * ans[i - 1][j] + 1.0 * j * (n - i) * ans[i][j - 1]) / (n * n); c = 1.0 * (n - i) * (n - j) / (n * n); ans[i][j] = 1.0 * x / (1 - c) + 1.0 / (1 - c); } } printf("%.10lf\n", ans[rr][cc]); return 0; }
2
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; void open() { ((void)0); ((void)0); ((void)0); } const int MAX = 1e5; const int MOD = 1e9 + 7; const int INF = 0x3f3f3f3f; const long long LL_INF = 0x3f3f3f3f3f3f3f3fLL; const double PI = acos(-1); const long double ERR = 1e-10; const int move_r[] = {-1, 0, 1, 0, -1, -1, 1, 1}; const int move_c[] = {0, -1, 0, 1, -1, 1, -1, 1}; int N, B, C, A, ans; int MAP[2 * MAX]; int main(int argc, char *argv[]) { open(); scanf("%d %d %d", &N, &B, &A); C = A; for (int i = 0; i < N; ++i) scanf("%d", MAP + i); for (int i = 0; i < N; ++i) { if (MAP[i]) { if (A == C) { ans++; A--; } else if (B > 0) { ans++; B--; A++; } else if (A > 0) { ans++; A--; } } else { if (A > 0) { A--; ans++; } else if (B > 0) { B--; ans++; } } } printf("%d\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; int a, d; scanf("%d%d%d", &n, &a, &d); int t[n], v[n]; for (int i = 0; i < n; i++) { scanf("%d%d", &t[i], &v[i]); } double at[n]; if ((long long)v[0] * v[0] < (long long)d * 2.0 * a) { at[0] = 0.5 * v[0] / a + d * 1.0 / v[0] + t[0]; } else { at[0] = sqrt(2.0 * d / a) + t[0]; } for (int i = 1; i < n; i++) { if ((long long)v[i] * v[i] < (long long)d * 2.0 * a) { at[i] = 0.5 * v[i] / a + d * 1.0 / v[i] + t[i]; } else { at[i] = sqrt(2.0 * d / a) + t[i]; } if (at[i] < at[i - 1]) { at[i] = at[i - 1]; } } for (int i = 0; i < n; i++) { printf("%.12lf\n", at[i]); } return 0; }
1
#include <bits/stdc++.h> using namespace std; #pragma GCC push_options #pragma GCC optimize("unroll-loops") int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long n, x; cin >> n >> x; long long a[n + 1]; long long count = 0; long long count_even = 0; for (long long i = 1; i <= n; i++) { cin >> a[i]; if (a[i] % 2 != 0) { count++; } else { count_even++; } } if (n == x) { if (count % 2 != 0) { cout << "Yes" << "\n"; } else { cout << "No" << "\n"; } } else { if (count == 0) { cout << "No" << "\n"; } else { long long rem = x - 1; count = count - 1; if (rem >= (count / 2) * 2) { rem = rem - (count / 2) * 2; if (rem <= count_even) { cout << "Yes" << "\n"; } else { cout << "No" << "\n"; } } else { if (rem % 2 == 0) { cout << "Yes" << "\n"; } else { if (count_even == 0) { cout << "No" << "\n"; } else { cout << "Yes" << "\n"; } } } } } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int fibo[1001], N = 1001; fibo[0] = 1; fibo[1] = 2; for(int i=2;i<N;i++){ fibo[i] = (fibo[i-1] + fibo[i-2]) % N; } int v, d; while(cin >> v >> d){ vector<int> ls; for(int i=1;i<=v;i++) ls.push_back(fibo[i]); sort(ls.begin(), ls.end()); int cnt = 1; for(int i=0;i<v-1;i++){ if(ls[i+1]-ls[i] >= d) cnt++; } cout << cnt << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1E6 + 6; int n, m, x, y, w, s1, s2, s3, f[N], g[4][N], ans; bool o[N], O[N]; queue<int> q; char s[1005][1005]; vector<int> G[N]; void add(int x, int y) { if (!x || !y) return; G[x].push_back(y); if (y > n * m) G[y].push_back(x); } int get(int x, int y) { if (x <= 0 || y <= 0 || x > n || y > m || s[x][y] == '#') return 0; else return (x - 1) * m + y; } void spfa(int k, int s) { memset(f, 10, sizeof(f)); f[s] = 0; q.push(s); while (!q.empty()) { x = q.front(); q.pop(); o[x] = 0; if (x > n * m || O[x]) w = 0; else w = 1; for (int i = 0; i < G[x].size(); i++) if (f[y = G[x][i]] > f[x] + w) { f[y] = f[x] + w; if (!o[y]) { o[y] = 1; q.push(y); } } } for (int i = 1; i <= s3; i++) g[k][i] = f[i]; } int main() { cin >> n >> m; s1 = n * m + 1; s2 = s1 + 1; s3 = s2 + 1; for (int i = 1; i <= n; i++) scanf("\n%s", s[i] + 1); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (s[i][j] != '#') { add(get(i, j), get(i + 1, j)); add(get(i, j), get(i, j + 1)); add(get(i, j), get(i - 1, j)); add(get(i, j), get(i, j - 1)); if (s[i][j] == '1') add(get(i, j), s1); if (s[i][j] == '2') add(get(i, j), s2); if (s[i][j] == '3') add(get(i, j), s3); if (s[i][j] != '.') O[get(i, j)] = 1; } spfa(1, s1); spfa(2, s2); spfa(3, s3); ans = g[1][s2] + g[1][s3]; for (int i = 1; i <= n * m; i++) ans = min(ans, g[1][i] + g[2][i] + g[3][i] + 1); if (ans > n * m) cout << -1; else cout << ans; }
5
#include <bits/stdc++.h> using namespace std; int main() { int t = 1; while (t--) { int n, k, sol = 0; cin >> n >> k; bool flag = 1; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; for (int i = k - 1; i < n - 1; i++) { if (v[i] != v[i + 1]) { flag = 0; break; } } if (flag) { for (int i = k - 1; i > 0; i--) { if (v[i] != v[i - 1]) { break; } sol++; } } if (!flag) cout << "-1" << endl; else cout << k - sol - 1 << endl; } }
1