solution
stringlengths
52
181k
difficulty
int64
0
6
#include<cstdio> #include<algorithm> #define LL long long using namespace std; const int maxn = 3e2 + 5; int mod, n, k; LL dp[maxn][maxn][maxn], ans; int main() { scanf("%d%d%d", &n, &k, &mod); dp[0][1][0] = 1; for(register int i = 0; i <= n; ++i) for(register int j = 1; j <= k; ++j) for(register int p = i; p >= 0; --p) { if(p) (dp[i][j][p - 1] += dp[i][j][p]) %= mod; else (dp[i][j + 1][i] += dp[i][j][p]) %= mod; (dp[i + 1][j][p] += dp[i][j][p] * (p + 1)) %= mod; } printf("%lld", dp[n][k + 1][n]); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAX = 123; unordered_map<string, int> mm; struct node { long long w, h, ty, b, s, id; int con[MAX]; node() { memset(con, 0, sizeof(con)); w = h = b = s = 0; } void maintain(); }; node ns[MAX]; int cnt, n; inline void node::maintain() { if (ty == 0) return; if (ty == 1) { long long cc = 0, hz = 0; long long mw = LLONG_MIN; for (int i = 0; i < cnt; i++) if (con[i] != 0) cc += con[i], hz += con[i] * ns[i].h, mw = max(mw, ns[i].w); if (cc == 0) w = h = 0; else { w = mw + 2 * b; h = (cc - 1) * s + hz + 2 * b; } } else if (ty == 2) { long long cc = 0, wz = 0; long long mh = LLONG_MIN; for (int i = 0; i < cnt; i++) if (con[i] != 0) cc += con[i], wz += con[i] * ns[i].w, mh = max(mh, ns[i].h); if (cc == 0) w = h = 0; else { w = (cc - 1) * s + wz + 2 * b; h = mh + 2 * b; } } }; char buff[MAX]; int dp[MAX]; vector<int> adj[MAX]; void add(int a, int b) { adj[a].push_back(b); } int zn[MAX]; int curS; int sol(int v) { if (dp[v] != -1) return dp[v]; else { if (v == curS) return dp[v] = 0; dp[v] = INT_MIN; for (int i = 0; i < adj[v].size(); i++) { int u = adj[v][i]; int tmp = sol(u); if (tmp != INT_MIN) dp[v] = max(dp[v], tmp + 1); } return dp[v]; } } bool cmp(int a, int b) { return dp[a] < dp[b]; } int main() { scanf("%d", &n); getchar(); while (n--) { cin.getline(buff, 120, '\n'); int len = strlen(buff); int ite = 0; for (; ite < len; ite++) { if (buff[ite] == ' ') break; } if (ite < len) { string tyname(buff, buff + ite); if (tyname == "VBox") { string name(buff + ite + 1, buff + len); mm[name] = cnt++; ns[cnt - 1].ty = 1, ns[cnt - 1].id = cnt - 1; } else if (tyname == "HBox") { string name(buff + ite + 1, buff + len); mm[name] = cnt++, ns[cnt - 1].id = cnt - 1; ns[cnt - 1].ty = 2; } else { int ite2; ite++; for (ite2 = ite; ite2 < len; ite2++) if (buff[ite2] == '(') break; string name(buff + ite, buff + ite2); int w, h; sscanf(buff + ite2, "(%d,%d)", &w, &h); mm[name] = cnt++; ns[cnt - 1].ty = 0, ns[cnt - 1].id = cnt - 1; ns[cnt - 1].w = w, ns[cnt - 1].h = h; } } else { ite = 0; for (; ite < len; ite++) if (buff[ite] == '.') break; string name(buff, buff + ite); ite++; int ite2; for (ite2 = ite; ite2 < len; ite2++) if (buff[ite2] == '(') break; string fname(buff + ite, buff + ite2); if (fname == "set_border") { int val; sscanf(buff + ite2, "(%d", &val); int id = mm[name]; if (val != ns[id].b) { memset(dp, -1, sizeof(dp)); ns[id].b = val; for (int i = 0; i < cnt; i++) zn[i] = i; curS = id; for (int i = 0; i < cnt; i++) sol(i); sort(zn, zn + cnt, cmp); for (int i = 0; i < cnt; i++) { if (dp[zn[i]] != INT_MIN) ns[zn[i]].maintain(); } } } else if (fname == "set_spacing") { int val; sscanf(buff + ite2, "(%d", &val); int id = mm[name]; if (val != ns[id].s) { memset(dp, -1, sizeof(dp)); ns[id].s = val; for (int i = 0; i < cnt; i++) zn[i] = i; curS = id; for (int i = 0; i < cnt; i++) sol(i); sort(zn, zn + cnt, cmp); for (int i = 0; i < cnt; i++) { if (dp[zn[i]] != INT_MIN) ns[zn[i]].maintain(); } } } else { ite2++; int ite3; for (ite3 = ite2; ite3 < len; ite3++) if (buff[ite3] == ')') break; string zy(buff + ite2, buff + ite3); int id1 = mm[name], id2 = mm[zy]; add(id1, id2); ns[id1].con[id2]++; memset(dp, -1, sizeof(dp)); for (int i = 0; i < cnt; i++) zn[i] = i; curS = id1; for (int i = 0; i < cnt; i++) sol(i); sort(zn, zn + cnt, cmp); for (int i = 0; i < cnt; i++) { if (dp[zn[i]] != INT_MIN) ns[zn[i]].maintain(); } } } } vector<string> names; for (auto &ele : mm) names.push_back(ele.first); sort(names.begin(), names.end()); for (int i = 0; i < names.size(); i++) { int id = mm[names[i]]; printf("%s %lld %lld\n", names[i].data(), ns[id].w, ns[id].h); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main(){ int n,k; cin>>n>>k; vector<string> s(n); for(auto &a:s){ cin>>a; } sort(s.begin(),s.end()); for(auto a:s){ cout<<a; } }
0
#include <bits/stdc++.h> using namespace std; template <class T> inline void read(T& num) { num = 0; bool f = true; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = false; ch = getchar(); } while (ch >= '0' && ch <= '9') { num = num * 10 + ch - '0'; ch = getchar(); } num = f ? num : -num; } int out[100]; template <class T> inline void write(T x, char ch) { if (x == 0) { putchar('0'); putchar(ch); return; } if (x < 0) { putchar('-'); x = -x; } int num = 0; while (x) { out[num++] = (x % 10); x = x / 10; } for (int i = (num - 1); i >= (0); i--) putchar(out[i] + '0'); putchar(ch); } int n; int sx, sy; int m; int base1 = 233333, base2 = 1007, base3 = 37; bool hash[1000007]; struct P { int x, y, id; } p[5005]; P zu[5005][2]; long long best = 107000000 * 100000ll; P A[5005][2]; long long getdis(P a, P b) { return (sx - a.x) * (sx - a.x) + (sx - b.x) * (sx - b.x) + (sy - a.y) * (sy - a.y) + (sy - b.y) * (sy - b.y) + (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y); } int main() { srand(233); cin >> sx >> sy; cin >> n; for (int i = (1); i <= (n); i++) { cin >> p[i].x; cin >> p[i].y; p[i].id = i; } for (int i = (n + 1); i <= (2 * n); i++) { p[i].x = sx; p[i].y = sy; p[i].id = 0; } m = n; random_shuffle(p + 1, p + 2 * n + 1); for (int i = 1; i <= n; i++) { zu[i][0] = p[i * 2 - 1]; zu[i][1] = p[i * 2]; } long long ans = 0ll; for (int i = (1); i <= (m); i++) ans += getdis(zu[i][0], zu[i][1]); best = min(best, ans); for (int i = (1); i <= (m); i++) for (int j = (0); j <= (1); j++) A[i][j] = zu[i][j]; double T = 1.0; while (T > 0.0000001) { T = T * 0.99998; int a = rand() % m + 1, b = rand() % m + 1; int aa = rand() % 2; if (a == b) continue; long long t1 = getdis(zu[a][0], zu[a][1]) + getdis(zu[b][0], zu[b][1]); long long t2 = getdis(zu[a][0], zu[b][aa]) + getdis(zu[a][1], zu[b][1 - aa]); if (t2 < t1 || T > abs(sin(rand()))) { ans = ans + t2 - t1; P k1 = zu[b][aa], k2 = zu[a][1], k3 = zu[b][1 - aa]; zu[a][1] = k1; zu[b][0] = k2; zu[b][1] = k3; if (ans < best) { best = ans; for (int i = (1); i <= (m); i++) for (int j = (0); j <= (1); j++) A[i][j] = zu[i][j]; } } } cout << best << endl; write(0, ' '); for (int i = (1); i <= (m); i++) { if (A[i][0].id == 0 && A[i][1].id == 0) continue; for (int j = (0); j <= (1); j++) { if (A[i][j].id != 0) write(A[i][j].id, ' '); } write(0, ' '); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int a; cin >> a; if (a % 2 == 0) cout << "Mahmoud"; else cout << "Ehab"; return 0; }
1
#include <string> #include <iostream> #include <math.h> #include <algorithm> #include <vector> #include <iomanip> #include <map> #include <set> typedef long long ll; const int mod=1e9+7; #define lol(i,n) for(int i=0;i<n;i++) using namespace std; int h,w,x,y; int main() { cin >>h>>w>>x>>y; if((h*w)%2==1&&(x+y)%2==1)cout << "No"<< endl; else cout <<"Yes"<< endl; }
0
#include <bits/stdc++.h> using namespace std; int n; vector<int> g[200000]; long long int a[200000]; long long int m = LLONG_MIN; long long int sz[200000]; long long int mx[200000]; void dfs(int cur, int pr) { sz[cur] = a[cur]; mx[cur] = LLONG_MIN; vector<long long int> q; for (int i = 0; i < g[cur].size(); i++) if (g[cur][i] != pr) { dfs(g[cur][i], cur); sz[cur] += sz[g[cur][i]]; mx[cur] = mx[cur] > mx[g[cur][i]] ? mx[cur] : mx[g[cur][i]]; q.push_back(mx[g[cur][i]]); } mx[cur] = mx[cur] > sz[cur] ? mx[cur] : sz[cur]; if (q.size() < 2) return; sort(q.begin(), q.end(), greater<long long int>()); if (q[0] + q[1] > m) m = q[0] + q[1]; } int main() { int u, v; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%lld", &a[i]); for (int i = 1; i < n; i++) { scanf("%d %d", &u, &v); g[--u].push_back(--v); g[v].push_back(u); } dfs(0, -1); if (m == LLONG_MIN) printf("Impossible\n"); else printf("%lld\n", m); return 0; }
4
#include<bits/stdc++.h> #define fi first #define se second #define INF 100000000000000000LL #define ll long long #define debug(x) cout << #x << " "<< x <<endl; #define debug_vector(v) cout << #v << " "; _print(v); const ll inf =1e9+44; const int MAX=3e5+9; const ll MOD= 1e9+7; const double eps=1e-10; double const PI=3.1415926535897931; using namespace std; int dx[4] = {1 ,0 , - 1, 0}; int dy[4] = {0 ,1 , 0 , -1}; void _print(vector<int> v){ cout << "[ "; for ( auto u : v) cout << u << " "; cout << " ]"<< endl; } int main(){ int test; cin >> test; while (test--){ int n; cin >> n; vector < ll> v(n+1); vector < ll > pre(n+1); pre[0] = 0; for (int i = 1; i <= n ; i++) cin >> v[i]; sort(v.begin()+1 , v.end()); vector < ll > edges(n+1); for (int i = 2; i<=n ; i++) edges[i] = v[i]-v[i-1]; for (int i = 2; i <=n; i++) pre[i] = (edges[i])*(long long )(i-1) + pre[i-1]; ll tot = 0; for (int i = 2; i <=n; i++) tot = tot + edges[i] -pre[i]; cout << tot << endl; } }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; long long int item[n], client[n]; long long int total = 0; vector<pair<long long int, long long int> > extra; for (int i = 0; i < n; i++) { cin >> item[i] >> client[i]; if (item[i] == 0 || client[i] == 0 || client[i] <= item[i]) total += min(item[i], client[i]); else { extra.push_back( make_pair(min(2 * item[i], client[i]) - min(item[i], client[i]), i)); } } sort(extra.begin(), extra.end()); reverse(extra.begin(), extra.end()); for (int i = 0; i < extra.size(); i++) { int idx = extra[i].second; if (m > 0) { total += min(2 * item[idx], client[idx]); m--; } else total += min(item[idx], client[idx]); } cout << total << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 5e5 + 10; int n, k, d, a[maxn], dp[maxn]; int main() { cin >> n >> k >> d; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a, a + 1 + n); dp[0] = 1; int j = 1; for (int i = 1; i <= n; i++) { while (j < i && (a[i] - a[j] > d || dp[j - 1] == 0)) j++; if (dp[j - 1] && (i - j + 1) >= k && (a[i] - a[j]) <= d) dp[i] = 1; else dp[i] = 0; } if (dp[n]) cout << "YES\n"; else cout << "NO\n"; }
5
#include <bits/stdc++.h> using namespace std; struct pt { int x, y; pt(int _x = 0, int _y = 0) : x(_x), y(_y) {} pt operator-(const pt &p2) const { return pt(x - p2.x, y - p2.y); } int dist2() const { return x * x + y * y; } }; const int MAXN = 1000; int todel[MAXN]; int es[MAXN][MAXN]; int ecnt[MAXN]; int n; bool go(int pos, int rem) { if (pos >= n) { for (int i = 0; i < n && rem; i++) if (!todel[i]) { todel[i] = pos + 1; rem--; } return true; } if (todel[pos] || !ecnt[pos]) return go(pos + 1, rem); int need = 0; for (int i = 0; i < ecnt[pos]; i++) { int b = es[pos][i]; if (todel[b]) continue; todel[b] = pos + 1; need++; } if (need <= rem && go(pos + 1, rem - need)) return true; for (int i = 0; i < ecnt[pos]; i++) { int b = es[pos][i]; if (todel[b] == pos + 1) todel[b] = 0; } if (rem > 0 && need > 1) { todel[pos] = pos + 1; if (go(pos + 1, rem - 1)) return true; todel[pos] = 0; } return false; } int k; int ds[MAXN][MAXN]; bool check(int maxd) { fprintf(stderr, "check(%d)\n", maxd); memset(ecnt, 0, sizeof ecnt); for (int a = 0; a < n; a++) for (int b = 0; b < n; b++) if (ds[a][b] > maxd) { es[a][ecnt[a]++] = b; } memset(todel, 0, sizeof todel); return go(0, k); } int main() { while (scanf("%d%d", &n, &k) >= 1) { vector<pt> pts(n); for (int i = 0; i < n; i++) scanf("%d%d", &pts[i].x, &pts[i].y); if (k + 1 == n) { for (int i = 0; i < k; i++) printf("%d%c", i + 1, "\n "[i + 1 < k]); continue; } vector<int> ads; for (int a = 0; a < n; a++) { ds[a][a] = 0; for (int b = a + 1; b < n; b++) { ads.push_back(ds[a][b] = ds[b][a] = (pts[b] - pts[a]).dist2()); } } sort(ads.begin(), ads.end()); ads.erase(unique(ads.begin(), ads.end()), ads.end()); int L = 0, R = ((int)(ads).size()) - 1; if (check(ads[L])) R = L; assert(check(ads[R])); while (L + 1 < R) { int M = (L + R) / 2; if (check(ads[M])) R = M; else L = M; } bool res = check(ads[R]); assert(res); for (int i = 0; i < n; i++) if (todel[i]) printf("%d ", i + 1); printf("\n"); } return 0; }
4
#include <iostream> #include <algorithm> class Matryoshika{ public: int h, r; bool operator<(const Matryoshika& m)const{ return h < m.h; } bool operator>(const Matryoshika& m)const{ return h > m.h; } bool operator<<(const Matryoshika& m)const{ return h < m.h && r < m.r; } bool operator>>(const Matryoshika& m)const{ return h > m.h && r > m.r; } }; const int MAX_HR = 1001, MAX_NM = 201; int N, M; Matryoshika datas[MAX_NM]; int dp[MAX_NM]; int main(){ while(true){ std::cin >> N; if(N == 0) break; for(int i = 0; i < N; ++i){ std::cin >> datas[i].h >> datas[i].r; dp[i] = 1; } std::cin >> M; for(int i = N; i < N + M; ++i){ std::cin >> datas[i].h >> datas[i].r; dp[i] = 1; } std::sort(datas, datas + (N + M)); int ans = 0; bool flag = true; for(int i = 0; i < (N + M - 1); ++i){ for(int j = i + 1; j < (N + M); ++j){ if(datas[i] << datas[j]){ flag = false; dp[j] = std::max(dp[i] + 1, dp[j]); ans = std::max(dp[j], ans); } } } std::cout << ans + flag << std::endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, x, ans = 1, temp = 0; cin >> n >> x; if (n == 1 || n == 2) { cout << ans << endl; continue; } n = n - 2; if (n < x) { cout << ans + 1 << endl; continue; } ans = ans + (n / x); if (n % x != 0) { ans++; } cout << ans << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int n; string s; long long dp[200011]; const long long Mod = 1e9 + 7; long long f[200011][3]; long long power(long long x, long long n) { if (n < 0) return 0; if (n == 0) return 1; if (n % 2 == 0) return power(x * x % Mod, n / 2); return x * power(x * x % Mod, n / 2) % Mod; } int main() { cin >> n; cin >> s; s = '.' + s; int c = 0; for (int i = n; i >= 1; i--) { if (s[i] == '?') c++; } long long cnt1 = 0; long long cnt2 = 0; for (int i = 1; i <= n; i++) { if (s[i] == 'b') { f[i][0] += cnt1; f[i][1] += cnt2; } if (s[i] == '?') { f[i][1] += cnt1; f[i][2] += cnt2; } if (s[i] == 'a') { cnt1++; } if (s[i] == '?') cnt2++; } for (int i = 1; i <= n; i++) { for (int j = 0; j <= 2; j++) (f[i][j] += f[i - 1][j]) %= Mod; } long long ans = 0; for (int i = 1; i <= n; i++) { if (s[i] == 'c') { ans += f[i - 1][0] * power(3, c); ans %= Mod; ans += f[i - 1][1] * power(3, c - 1); ans %= Mod; ans += f[i - 1][2] * power(3, c - 2); ans %= Mod; } if (s[i] == '?') { ans += f[i - 1][0] * power(3, c - 1); ans %= Mod; ans += f[i - 1][1] * power(3, c - 2); ans %= Mod; ans += f[i - 1][2] * power(3, c - 3); ans %= Mod; } } cout << ans; return 0; }
6
#include <bits/stdc++.h> using namespace std; string str(char c) { return string(1, c); } int main() { string s; cin >> s; int start = 0; if (s.size() % 2 == 0) start = s.size() / 2 - 1; else start = s.size() / 2; int i = start - 1; int j = start + 1; string ans = str(s[start]); while (i >= 0 && j < s.size()) { ans += str(s[j]); ans += str(s[i]); --i; ++j; } if (s.size() % 2 == 0) ans += s[s.size() - 1]; cout << ans; }
1
#include <bits/stdc++.h> using namespace std; long long n, a, b; int main() { ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> n >> a >> b; cout << min((n + a) - b, min(min(a, b), n)) + 1; }
2
#include <bits/stdc++.h> using namespace std; struct Node { int x; int y; Node(int x = 0, int y = 0) : x(x), y(y) {} int Level() const { return max(x, y); } bool operator<(const Node &ant) const { int l1 = Level(); int l2 = ant.Level(); if (l1 != l2) return l1 < l2; if (x != ant.x) { return x < ant.x; } else { return y > ant.y; } } int dis(int a, int b) { return abs(x - a) + abs(y - b); } int dis(const Node &x) { return dis(x.x, x.y); } Node &operator=(const Node &ant) { x = ant.x; y = ant.y; return *this; } }; const int maxn = 2e5 + 10; Node a[maxn]; int main() { int n; while (scanf("%d", &n) != EOF) { for (int i = 0; i < n; i++) { scanf("%d%d", &a[i].x, &a[i].y); } sort(a, a + n); long long ans1 = 0, ans2 = 0; Node n1, n2; for (int i = 0; i < n;) { int j = i; while (j < n && a[j].Level() == a[i].Level()) { j++; } j--; int len = a[i].dis(a[j]); long long l = min(ans1 + n1.dis(a[j]) + len, ans2 + n2.dis(a[j]) + len); long long r = min(ans1 + n1.dis(a[i]) + len, ans2 + n2.dis(a[i]) + len); ans1 = l; ans2 = r; n1 = a[i]; n2 = a[j]; i = j + 1; ; } cout << min(ans1, ans2) << endl; } return 0; }
6
#include<bits/stdc++.h> using namespace std; int main() { set<int> s; int n; scanf("%d",&n); for(int i=0;i<n;i++) { int t; scanf("%d",&t); s.insert(t); } scanf("%d",&n); int ans=0; for(int i=0;i<n;i++) { int t; scanf("%d",&t); ans+=s.count(t); } printf("%d\n",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, k, s = 0, a; int sg(int x) { if (x == 0 || x == 1) return x; if (k % 2 == 0) { if (x == 2) return 2; if (x % 2 == 0) return 1; else return 0; } else { if (x == 2) return 0; if (x == 3) return 1; if (x == 4) return 2; if (x % 2 == 0) { int cnt = 0; while (x % 2 == 0) x /= 2, cnt++; if (x == 3) cnt++; if (cnt % 2) return 1; else return 2; } else return 0; } } int main(void) { cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a; s ^= sg(a); } if (s == 0) cout << "Nicky\n"; else cout << "Kevin\n"; }
3
#include <bits/stdc++.h> using namespace std; typedef map<int, int>::iterator iMII; int n; vector<int> V; vector<vector<int> > M; vector<int> primers; vector<int> sobren; int main() { cin >> n; V = vector<int>(n + 3, -1); M = vector<vector<int> >(n + 3); for (int i = 2; i <= n / 2; ++i) if (V[i] == -1) { primers.push_back(i); for (int j = 1; j <= n / i; ++j) if (V[j * i] == -1) { V[j * i] = 0; M[i].push_back(i * j); } } int comptador = 0; int conta = 0; for (int i = 1; i < int(primers.size()); ++i) { int x = primers[i]; int t = int(M[x].size()); if (t % 2 == 1) { ++conta; comptador += t / 2 + 1; } else comptador += t / 2; } comptador += (int(M[2].size()) - conta) / 2; cout << comptador << endl; for (int i = 1; i < int(primers.size()); ++i) { int x = primers[i]; int t = int(M[x].size()); if (t % 2 == 0) for (int j = 0; j < t / 2; ++j) cout << M[x][j] << ' ' << M[x][t / 2 + j] << endl; else { M[2][x - 1] = 0; for (int j = 0; j < t - 1; j += 2) cout << M[x][j] << ' ' << M[x][j + 1] << endl; cout << M[x][t - 1] << ' ' << 2 * x << endl; } } int cont = 0; int s = int(M[2].size()); while (cont < s) { while (M[2][cont] == 0) ++cont; int j = cont; ++cont; while (M[2][cont] == 0) ++cont; int k = cont; if (k < s) cout << M[2][j] << ' ' << M[2][k] << endl; ++cont; } }
5
#include <bits/stdc++.h> using namespace std; const int inv = numeric_limits<int>::max(); const int minv = numeric_limits<int>::min(); const int max_n = 5010; int n; long long A[max_n]; int P[max_n]; int dp[max_n]; bool cm(int j, int i) { if (A[j] % A[i]) return false; if (i - j <= P[i]) return (P[i] - P[j] == i - j); return true; } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { cin >> A[i]; while ((A[i] % (2ll)) == 0) { A[i] /= (2ll); ++P[i]; } } dp[0] = 1; int res = n - 1; for (int i = 1; i < n; ++i) { dp[i] = 1; for (int j = 0; j < i; ++j) { if (cm(j, i)) dp[i] = max(dp[i], dp[j] + 1); } res = min(res, n - dp[i]); } printf("%d\n", res); return 0; }
4
#include <bits/stdc++.h> using namespace std; int Set(int N, int pos) { return N = N | (1 << pos); } int reset(int N, int pos) { return N = N & ~(1 << pos); } bool check(int N, int pos) { return (bool)(N & (1 << pos)); } int main() { map<pair<int, int>, pair<int, int> > ob; int n; scanf("%d", &n); double r1 = -1, r2 = -1; int idx1, idx21, idx22; for (int i = 1; i <= n; i++) { int arr[3]; scanf("%d%d%d", &arr[0], &arr[1], &arr[2]); sort(arr, arr + 3); double m = arr[0]; m /= 2.0; if (r1 < m) { idx1 = i; r1 = m; } long long int sum = 0; for (int k = 0; k < 3; k++) sum += arr[k]; for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { if (j == k) continue; int A = min(arr[j], arr[k]); int B = max(arr[j], arr[k]); int C = sum - (A + B); if (ob.find(pair<int, int>(A, B)) != ob.end()) { pair<int, int> p = ob[pair<int, int>(A, B)]; m = min(A, min(B, p.first + C)); m /= 2.0; if (r2 < m) { r2 = m; idx21 = i; idx22 = p.second; } } } } for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { if (j == k) continue; int A = min(arr[j], arr[k]); int B = max(arr[j], arr[k]); int C = sum - (A + B); if (ob.find(pair<int, int>(A, B)) == ob.end()) { ob[pair<int, int>(A, B)] = pair<int, int>(C, i); } else if (C > ob[pair<int, int>(A, B)].first) { ob[pair<int, int>(A, B)] = pair<int, int>(C, i); } } } } if (r1 >= r2) { cout << "1\n" << idx1; } else { cout << "2\n" << idx21 << " " << idx22; } return 0; }
4
#include <bits/stdc++.h> const double PI = acosl(-1.0); int bits[1 << 23]; void pre(int n) { int LOG = 0; while (1 << (LOG + 1) < n) { LOG++; } for (int i = 1; i < n; i++) { bits[i] = (bits[i >> 1] >> 1) | ((i & 1) << LOG); } } std::vector<std::complex<double> > fft(std::vector<std::complex<double> > a, bool inv = false) { int n = a.size(); pre(n); for (int i = 0; i < n; i++) { int to = bits[i]; if (to > i) { std::swap(a[to], a[i]); } } double angle = inv ? -1 : 1; angle *= PI; for (int len = 1; len < n; len *= 2) { std::complex<double> delta(cosl(angle / len), sinl(angle / len)); for (int i = 0; i < n; i += 2 * len) { std::complex<double> cur_root = 1; for (int j = 0; j < len; j++) { std::complex<double> u = a[i + j], v = a[i + j + len] * cur_root; a[i + j] = u + v; a[i + j + len] = u - v; cur_root *= delta; } } } if (inv) { for (int i = 0; i < n; i++) a[i] /= n; } return a; } std::vector<int> mul(std::vector<int> &a, std::vector<int> &b) { std::vector<std::complex<double> > A, B; A.resize(a.size()); B.resize(b.size()); for (int i = 0; i < a.size(); i++) { A[i] = a[i]; B[i] = b[i]; } A = fft(A); B = fft(B); for (int i = 0; i < a.size(); i++) { A[i] *= B[i]; } A = fft(A, true); std::vector<int> ans(a.size()); for (int i = 0; i < a.size(); i++) { ans[i] = std::real(A[i]) + 0.5; } return ans; } int decode[256]; int freq[4]; void print_vector(std::vector<int> A) { for (auto a : A) std::cout << a << ' '; std::cout << std::endl; } int main() { int n, m, k; std::cin >> n >> m >> k; std::string s, t; std::cin >> s >> t; int N = 1; while (N < n + m) N <<= 1; std::vector<int> A[4], B[4]; for (int i = 0; i < 4; i++) { A[i].resize(N); B[i].resize(N); } decode['A'] = 0; decode['G'] = 1; decode['C'] = 2; decode['T'] = 3; for (int i = 0; i < n; i++) { A[decode[s[i]]][std::max(i - k, 0)] += 1; if (i + k + 1 < n) A[decode[s[i]]][i + k + 1] -= 1; } for (int i = 0; i < m; i++) { B[decode[t[i]]][m - i - 1] = 1; freq[decode[t[i]]]++; } for (int i = 0; i < 4; i++) { for (int j = 1; j < N; j++) A[i][j] += A[i][j - 1]; } for (int i = 0; i < 4; i++) { for (int j = 0; j < N; j++) A[i][j] = std::min(1, A[i][j]); } std::vector<int> C[4]; for (int j = 0; j < 4; j++) { C[j] = mul(A[j], B[j]); } int ans = 0; for (int i = 0; i <= n - m; i++) { bool can = true; for (int j = 0; j < 4; j++) can = can && (C[j][m - 1 + i] == freq[j]); if (can) { ans++; } } std::cout << ans << '\n'; }
4
#include <bits/stdc++.h> using namespace std; struct s_ix { long long ix; long long val; }; bool operator<(s_ix aa, s_ix bb) { return aa.val - aa.ix < bb.val - bb.ix; } int main() { ios::sync_with_stdio(0); cin.tie(0); long long T; cin >> T; while (T--) { long long n, k, sum, nbw, maxnbw; cin >> n; vector<s_ix> nb(n); vector<string> s(n); for (int j = 0; j < n; j++) cin >> s[j]; maxnbw = 0; for (int l = 0; l < 26; l++) { nbw = 0; for (int j = 0; j < n; j++) { nb[j].val = 0; nb[j].ix = 0; for (int jj = 0; jj < s[j].size(); jj++) { if (s[j][jj] - 'a' == l) nb[j].val++; else nb[j].ix++; } } sort(nb.begin(), nb.end()); reverse(nb.begin(), nb.end()); long long nl = 0; int j = 0; while (j < n) { nl += nb[j].val - nb[j].ix; if (nl <= 0) break; j++; nbw++; } maxnbw = max(maxnbw, nbw); } cout << maxnbw << endl; } }
3
#include <iostream> #include <climits> #include <algorithm> #include <vector> using ll = long long; int main(){ int t{0}; std::cin >> t; std::vector<ll> results; while(t--){ int n{0}; std::cin >> n; std::vector<int> arr(n); for(auto &val: arr) std::cin >> val; ll maxCost = 0; for(int i{1}; i < n; i++){ maxCost += abs(arr[i] - arr[i - 1]); } int maxRed = std::max(abs(arr[1] - arr[0]), abs(arr[n - 1] - arr[n - 2])); for(int i{1}; i < n - 1; i++){ auto curr = abs(arr[i + 1] - arr[i]) + abs(arr[i] - arr[i - 1]) - abs(arr[i + 1] - arr[i -1]); maxRed = std::max(maxRed, curr); } results.push_back(maxCost - maxRed); } for(auto &result : results) std::cout << result << '\n'; return 0; }
2
#include <bits/stdc++.h> const long long mod = 998244353; const int N = 2e5 + 100, LG = 20; std::vector<int> g[2][N]; long long answ = 0; long long dp[N][LG][2]; long long d[N][2]; long long n, m; long long INF; long long get(long long x) { return (1LL << x) - 1; } struct pos { long long cost; int i; int t; friend bool operator<(pos a, pos b) { if (a.cost == b.cost) { if (a.i == b.i) return a.t < b.t; return a.i < b.i; } return a.cost < b.cost; } }; bool solve1() { memset(dp, 0x3f, sizeof(dp)); INF = dp[0][0][0]; dp[0][0][0] = 0; for (int lg = 0; lg < LG - 1; ++lg) { std::set<pos> q; for (int v = 0; v < (int)(n); ++v) { for (int t = 0; t < (int)(2); ++t) { q.insert({dp[v][lg][t], v, t}); } } while (!q.empty()) { auto v = *q.begin(); q.erase(q.begin()); for (auto to : g[v.t][v.i]) { if (dp[to][lg][v.t] > v.cost + 1) { q.erase({dp[to][lg][v.t], to, v.t}); dp[to][lg][v.t] = v.cost + 1; q.insert({dp[to][lg][v.t], to, v.t}); } } } for (int v = 0; v < (int)(n); ++v) { for (int t = 0; t < 2; ++t) { dp[v][lg + 1][t ^ 1] = std::min(dp[v][lg + 1][t ^ 1], dp[v][lg][t]); } } } long long minAnsw = INF; for (int lg = 0; lg < (int)(LG); ++lg) { for (int t = 0; t < (int)(2); ++t) { minAnsw = std::min(minAnsw, get(lg) + dp[n - 1][lg][t]); } } if (minAnsw >= INF) return false; answ = minAnsw % mod; return 1; } long long bin_p(long long a, long long b) { if (!b) return 1; if (b % 2 == 0) { long long cur = bin_p(a, b / 2); return cur * cur % mod; } else { return a * bin_p(a, b - 1) % mod; } } const long long BORDER = 1e12; void solve2() { memset(d, 0x3f, sizeof(d)); d[0][0] = 0; std::set<pos> q; q.insert({d[0][0], 0, 0}); while (!q.empty()) { auto v = *q.begin(); q.erase(q.begin()); for (auto to : g[v.t][v.i]) { if (d[to][v.t] > v.cost + 1) { q.erase({d[to][v.t], to, v.t}); d[to][v.t] = v.cost + 1; q.insert({d[to][v.t], to, v.t}); } } if (d[v.i][v.t ^ 1] > v.cost + BORDER) { q.erase({d[v.i][v.t ^ 1], v.i, v.t ^ 1}); d[v.i][v.t ^ 1] = v.cost + BORDER; q.insert({d[v.i][v.t ^ 1], v.i, v.t ^ 1}); } } long long answ = std::min(d[n - 1][0], d[n - 1][1]); long long rev = answ / BORDER, rev2 = answ % BORDER; long long realAnsw = (bin_p(2, rev) - 1) + rev2; realAnsw = (realAnsw % mod + mod) % mod; std::cout << realAnsw; } signed main() { std::ios_base::sync_with_stdio(0); std::cin.tie(0); std::cin >> n >> m; for (int i = 0; i < (int)(m); ++i) { int a, b; std::cin >> a >> b; --a; --b; g[0][a].push_back(b); g[1][b].push_back(a); } if (solve1()) { std::cout << answ; } else { solve2(); } }
3
#include <bits/stdc++.h> using namespace std; const double eps = 1e-10; const double pi = 3.1415926535897932384626433832795; const double eln = 2.718281828459045235360287471352; const long long mod = 1e9 + 7; long long sum[5005]; bool b[27]; long long c[5005]; char s[5005], s1[5005]; long long dp[5005][27]; int i, j, k, l, m, n; long long ans; long long mi(long long x, long long y) { long long aa = 1; while (y > 0) { if (y & 1) aa = (aa * x) % mod; x = (x * x) % mod; y >>= 1; } return aa; } int main() { scanf("%d\n", &n); if (n - 1 <= 1) c[0] = c[1] = 1; else { l = n - 1; c[0] = 1; c[1] = l; for (i = 2; i <= l; i++) { c[i] = c[i - 1]; c[i] *= (long long)(l - i + 1); c[i] %= mod; c[i] *= mi((long long)i, mod - 2); c[i] %= mod; } } gets(s1 + 1); m = 1; s[1] = s1[1]; for (i = 2; i <= n; i++) if (s1[i] != s1[i - 1]) s[++m] = s1[i]; memset(b, 0, sizeof(b)); for (i = 1; i <= m; i++) { int t = s[i] - 96; if (!b[t]) { sum[1]++; dp[1][t] = 1; b[t] = true; } for (k = 2; k <= m; k++) { long long t1 = (sum[k - 1] - dp[k - 1][t] + mod) % mod; long long t2 = dp[k][t]; dp[k][t] = t1; sum[k] = (sum[k] + mod + t1 - t2) % mod; } } for (i = 1; i <= m; i++) { ans += (sum[i] * c[i - 1]) % mod; ans %= mod; } printf("%lld", ans); return 0; }
4
#include <bits/stdc++.h> #define rep(i,n) for(int i = 0; i < n; i++) using namespace std; typedef long long ll; typedef pair<ll,ll> P; const double EPS = 1e-10; const ll INF = 100000000; const ll MOD = 1000000007; ll n, m, k; ll kai[1000001]; ll p3[1000001]; ll dp[1000001]; void init (int _n) { kai[0] = 1; p3[0] = 1; for (int i = 1; i <= _n; i++) kai[i] = kai[i-1]*i%MOD; for (int i = 1; i <= _n; i++) p3[i] = p3[i-1]*3%MOD; } ll mod_pow (ll x, ll y) { ll ret = 1; while (y) { if (y&1) ret = ret*x%MOD; x = x*x%MOD; y /= 2; } return ret; } ll conb (ll x, ll y) { ll z = x-y; ll ret = kai[x]*mod_pow(kai[y],MOD-2)%MOD; ret = ret*mod_pow(kai[z],MOD-2)%MOD; return ret; } int main() { cin >> n >> m >> k; ll s = n+m+k; ll ans = 0; init(s); dp[0] = 1; for (ll i = 1; i <= s; i++) { if (i <= m && i <= k) { dp[i] = dp[i-1]*2%MOD; } else if (i <= m) { dp[i] = (dp[i-1]*2-conb(i-1,i-1-k)+MOD*MOD)%MOD; } else if (i <= k) { dp[i] = (dp[i-1]*2-conb(i-1,m)+MOD*MOD)%MOD; } else { dp[i] = (dp[i-1]*2-conb(i-1,m)-conb(i-1,i-1-k)+2*MOD*MOD)%MOD; } } for (ll i = n; i <= s; i++) { ll add = p3[s-i]; add = add * conb(i-1, n-1) % MOD; add = add * dp[i-n] % MOD; ans = (ans + add) % MOD; } cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; void solve() { int n, x; cin >> n >> x; vector<int> a(n), b(n); for (auto &x : a) cin >> x; for (auto &x : b) cin >> x; sort(a.begin(), a.end()); sort(b.begin(), b.end(), greater<int>()); for (int i = 0; i < n; i++) { if (a[i] + b[i] > x) { cout << "NO" << endl; return; } } cout << "YES" << endl; } signed main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); int T; cin >> T; while (T--) solve(); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int OO = (int)1e9; const double eps = 1e-10; const int MX = 1e5 + 5; int n, m, k, s, t, d, w; long long mem[MX][205]; vector<vector<pair<int, int> > > add(MX), del(MX); vector<pair<int, int> > next_move(MX); multiset<pair<int, int> > active; long long dp(int idx, int left) { if (idx == n + 1) return 0; long long& ret = mem[idx][left]; if (ret != -1) return ret; long long ch1 = next_move[idx].first + dp(next_move[idx].second + 1, left); long long ch2 = 1e18; if (left) ch2 = dp(idx + 1, left - 1); return ret = min(ch1, ch2); } int main() { ios::sync_with_stdio(false); cout.precision(10); cin >> n >> m >> k; for (int i = 0; i < int(k); ++i) { cin >> s >> t >> d >> w; add[s].push_back({w, d}); del[t + 1].push_back({w, d}); } for (int i = 1; i < int(n + 1); ++i) { for (auto& pr : add[i]) active.insert({-pr.first, -pr.second}); for (auto& pr : del[i]) active.erase(active.find({-pr.first, -pr.second})); if (!active.empty()) next_move[i] = {-(active.begin()->first), -(active.begin()->second)}; else next_move[i] = {0, i}; } for (int i = 0; i < int(MX); ++i) for (int j = 0; j < int(205); ++j) mem[i][j] = -1; cout << dp(1, m) << '\n'; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MAXM = 100; int N, K; int sx, sy; bool vis[MAXM][MAXM]; int cal(int x, int yl, int yr) { int i, j = 0; for (i = yl; i <= yr; i++) j += abs(x - sx) + abs(i - sy); return j; } void check(int x) { int i, j, k, pre, mins = -1, l, r, row; for (i = 1; i <= K; i++) { pre = 0; j = 1; while (j <= K) { if (!vis[i][j]) { pre++; if (pre == x) { k = cal(i, j - x + 1, j); if (mins == -1 || mins > k || mins == k && (i < row || (i == row && j - x + 1 < l))) { mins = k; row = i; l = j - x + 1; r = j; } pre = x - 1; } } else pre = 0; j++; } } if (mins == -1) cout << -1 << endl; else { for (i = l; i <= r; i++) vis[row][i] = true; cout << row << ' ' << l << ' ' << r << endl; } } int main() { cin >> N >> K; sx = K % 2 ? K / 2 + 1 : K / 2; sy = sx; int i; while (N--) { cin >> i; check(i); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main(){ list<int> l; list<int>::iterator it=l.end(); int q; cin>>q; while(q--){ int y; cin>>y; if(y==0){ int x; cin>>x; it=l.insert(it,x); } else if(y==1){ int d; cin>>d; if(d>0){ for(int i=0;i<d;i++){ it++; } } else{ for(int i=d;i<0;i++){ it--; } } } else{ it=l.erase(it); } } for(it=l.begin();it!=l.end();it++){ cout<<*it<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 4e5 + 5; const int M = 1e9 + 7; int pa[N << 2], lpa[N << 2]; int a[N]; long long oa[N << 2], loa[N << 2]; int invprime[70]; int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293}; long long power(long long x, long long y) { x %= M; long long ans = 1; while (y) { if (y & 1) ans = (ans * x) % M; y >>= 1; x = (x * x) % M; } return (ans % M); } long long divi(long long a, long long b) { return ((a * power(b, M - 2)) % M); } void pprop(int node, int st, int end) { if (lpa[node] == 1) return; pa[node] = (1LL * pa[node] * power(lpa[node], (end - st + 1))) % M; if (st != end) { lpa[node << 1] = (1LL * lpa[node << 1] * lpa[node]) % M; lpa[(node << 1) | 1] = (1LL * lpa[(node << 1) | 1] * lpa[node]) % M; } lpa[node] = 1; } void pup(int node, int st, int end, int l, int r, int val) { pprop(node, st, end); if (st > end || r < st || l > end) return; if (l <= st && end <= r) { lpa[node] = (1LL * lpa[node] * val) % M; pprop(node, st, end); return; } int mid = st + end >> 1; pup(node << 1, st, mid, l, r, val); pup((node << 1) | 1, mid + 1, end, l, r, val); pa[node] = (1LL * pa[node << 1] * pa[(node << 1) | 1]) % M; } int pquery(int node, int st, int end, int l, int r) { pprop(node, st, end); if (st > end || r < st || l > end) return 1; if (l <= st && end <= r) return pa[node]; int mid = st + end >> 1; return ((1LL * pquery(node << 1, st, mid, l, r) * pquery((node << 1) | 1, mid + 1, end, l, r)) % M); } void oprop(int node, int st, int end) { if (loa[node] == 0) return; oa[node] |= loa[node]; if (st != end) { loa[node << 1] |= loa[node]; loa[(node << 1) | 1] |= loa[node]; } loa[node] = 0; } void oup(int node, int st, int end, int l, int r, long long val) { oprop(node, st, end); if (st > end || r < st || l > end) return; if (l <= st && end <= r) { loa[node] |= val; oprop(node, st, end); return; } int mid = st + end >> 1; oup(node << 1, st, mid, l, r, val); oup((node << 1) | 1, mid + 1, end, l, r, val); oa[node] = oa[node << 1] | oa[(node << 1) | 1]; } long long oquery(int node, int st, int end, int l, int r) { oprop(node, st, end); if (st > end || r < st || l > end) return 0; if (l <= st && end <= r) return oa[node]; int mid = st + end >> 1; return (oquery(node << 1, st, mid, l, r) | oquery((node << 1) | 1, mid + 1, end, l, r)); } vector<long long> mask(320, 0); void pre() { for (int i = 0; i < (N << 2); i++) pa[i] = 1, lpa[i] = 1; for (int i = 1; i <= 300; i++) { for (int j = 0; j < 62; j++) { if (i % prime[j] == 0) mask[i] |= (1LL << j); } } for (int i = 0; i < 62; i++) { invprime[i] = power(prime[i], M - 2); } } int main() { ios_base ::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); pre(); int n, q; cin >> n >> q; for (int i = 1; i <= n; i++) { cin >> a[i]; pup(1, 1, n, i, i, a[i]); oup(1, 1, n, i, i, mask[a[i]]); } string yo; int l, r; int x; while (q--) { cin >> yo >> l >> r; if (yo == "MULTIPLY") { cin >> x; pup(1, 1, n, l, r, x); oup(1, 1, n, l, r, mask[x]); } else { int ans = pquery(1, 1, n, l, r); long long smask = oquery(1, 1, n, l, r); for (int i = 0; i < 62; i++) { if ((1LL << i) & smask) { ans = (1LL * ans * (prime[i] - 1)) % M; ans = (1LL * ans * invprime[i]) % M; } } cout << ans << "\n"; } } return 0; }
6
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<vector> #include<map> #include<set> #include<string> #include<queue> #include<stack> #include<complex> #include<numeric> using namespace std; #define MOD 1000000007 #define MOD2 998244353 #define PRIM 3 #define INF (1<<29) #define LINF (1LL<<60) #define EPS (1e-10) #define PI 3.1415926535897932384626433832795028 typedef long long Int; typedef pair<Int, Int> P; typedef long double Real; typedef complex<Real> CP; struct BidirectionalList; set<BidirectionalList*> pos[4][4][4]; int cnt[3]; struct BidirectionalList{ BidirectionalList *prev, *next; int val; BidirectionalList(int v):val(v), prev(nullptr), next(nullptr){} BidirectionalList *remove(){ auto n = this->next, p = this->prev; if(n)n->prev = p; if(p)p->next = n; cnt[this->val]--; return n; } void insert(BidirectionalList &new_next){ auto n = this->next; if(n)n->prev = &new_next; new_next.next = n; this->next = &new_next; new_next.prev = this; } void set_triple(){ if(val < 3) pos[prev->val][val][next->val].insert(this); } void unset_triple(){ if(val < 3) pos[prev->val][val][next->val].erase(this); } }; BidirectionalList *remove(BidirectionalList *x){ if(x->prev->val != x->next->val){ auto n = x->next, p = x->prev; x->unset_triple(); n->unset_triple(); p->unset_triple(); x->remove(); n->set_triple(); p->set_triple(); return n; } else{ auto n = x->next, p = x->prev, nn = x->next->next; x->unset_triple(); n->unset_triple(); nn->unset_triple(); p->unset_triple(); x->remove(); n->remove(); nn->set_triple(); p->set_triple(); return n; } } // remove b bool check(int a, int b, int c){ if(pos[a][b][c].empty())return false; auto tmp = *pos[a][b][c].begin();pos[a][b][c].erase(pos[a][b][c].begin()); remove(tmp); return true; } int main(){ string s; cin >> s; BidirectionalList start(3), end(3); start.insert(end); auto last = &start; for(char c:s){ if(c-'A' == last->val)continue; BidirectionalList *tmp = new BidirectionalList(c-'A'); last->insert(*tmp); last = tmp; } int n = s.size(); for(auto it = start.next;it != &end;it = it->next){ cnt[it->val]++; it->set_triple(); } if(cnt[0] == 0 || cnt[1] == 0 || cnt[2] == 0){ cout << endl; return 0; } while(cnt[0] != cnt[1] || cnt[1] != cnt[2]){ int ord[] = {0, 1, 2}; if(cnt[ord[0]] < cnt[ord[1]])swap(ord[0], ord[1]); if(cnt[ord[1]] < cnt[ord[2]])swap(ord[1], ord[2]); if(cnt[ord[0]] < cnt[ord[1]])swap(ord[0], ord[1]); int min_cnt = cnt[ord[2]]; int only_0 = pos[ord[2]][ord[0]][ord[2]].size(); int only_1 = pos[ord[2]][ord[1]][ord[2]].size(); int both = min_cnt + 1 - only_0 - only_1; if(start.next->val == ord[2])both--; if(end.prev->val == ord[2])both--; /* for(auto it = start.next;it != &end;it = it->next){ cout << char('A' + it->val); }cout << endl;*/ if(only_0 > only_1 + both){ auto tmp = *pos[ord[2]][ord[0]][ord[2]].begin(); remove(tmp); continue; } if(check(ord[2], ord[0], ord[1]))continue; if(check(ord[1], ord[0], ord[2]))continue; if(check(3, ord[0], ord[1]))continue; if(check(ord[1], ord[0], 3))continue; if(check(3, ord[0], ord[2]))continue; if(check(ord[2], ord[0], 3))continue; if(check(ord[1], ord[0], ord[1]))continue; if(n-- == 0)return -1; } for(auto it = start.next;it != &end;it = it->next){ cout << char('A' + it->val); }cout << endl; return 0; }
0
#include <bits/stdc++.h> #define pb push_back #define F first #define S second #define all(x) x.begin(), x.end() #define debug(x) cerr << #x << " : " << x << '\n' using namespace std; typedef long long ll; typedef long double ld; typedef string str; typedef pair<ll, ll> pll; const ll Mod = 1000000007LL; const int N = 2e5 + 10; const ll Inf = 2242545357980376863LL; const ll Log = 30; ll a[N]; vector<ll> V; bool Add(ll x){ for(auto y : V) x = min(x, x ^ y); if(x){ V.pb(x); sort(all(V)); reverse(all(V)); } return (x > 0); } void Main(){ int n; V.clear(); cin >> n; for(int i = 0; i < n; i++){ cin >> a[i]; } str s; cin >> s; for(int i = n - 1; i >= 0; i--){ if(s[i] == '0') Add(a[i]); else { if(Add(a[i])){ cout << "1\n"; return ; } } } cout << "0\n"; } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tc; cin >> tc; while(tc --) Main(); return 0; }
0
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const int INF = 2e9; const long long LINF = 1e18 + 5; const long long MOD = 1e9 + 7; const int MAXN = 1e5 + 5; const int MN = 1e6 + 2; double x[1001]; double y[1001]; vector<pair<pair<double, double>, double> > v; int le[MN]; int ri[MN]; bool eq(int idx) { double rx1 = v[idx - 1].first.first; double ry1 = v[idx - 1].first.second; double rx2 = v[idx].first.first; double ry2 = v[idx].first.second; if (abs(rx1 - rx2) < 1e-9 && abs(ry1 - ry2) < 1e-9) return 1; else return 0; } bool tcmp(pair<pair<double, double>, double> d1, pair<pair<double, double>, double> d2) { if (abs(d1.first.first - d2.first.first) > 1e-9) return d1.first.first < d2.first.first; if (abs(d1.first.second - d2.first.second) > 1e-9) return d1.first.second < d2.first.second; if (abs(d1.second - d2.second) > 1e-9) return d1.second < d2.second; return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; int n; cin >> n; double mm; int a, b, c, d; for (int i = 0; i < n; ++i) { cin >> a >> b >> c >> d; double xx = (1.0 * a) / b; double yy = (1.0 * c) / d; x[i] = xx / (((xx) * (xx)) + ((yy) * (yy))); y[i] = yy / (((xx) * (xx)) + ((yy) * (yy))); } for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { double rx = x[i] + x[j]; double ry = y[i] + y[j]; if (abs(x[i] - x[j]) < 1e-9) mm = 1e50; else mm = (y[i] - y[j]) / (x[i] - x[j]); v.push_back(make_pair(make_pair(rx, ry), mm)); } } sort((v).begin(), (v).end(), tcmp); v.push_back(make_pair(make_pair(-1e50, -1e50), -1e50)); int _sz = 1; le[_sz - 1] = 0; for (int i = 1; i < v.size(); ++i) { if (!eq(i)) { ri[_sz - 1] = i - 1; le[_sz] = i; ++_sz; } } --_sz; long long ans = 0; long long u = 1; long long ret = 0; vector<int> ss; for (int i = 0; i < _sz; ++i) { ss.clear(); ss.push_back(1); for (int j = le[i] + 1; j <= ri[i]; ++j) { if (abs(v[j].second - v[j - 1].second) < 1e-9) { int len = ss.size(); ss[len - 1]++; } else { ss.push_back(1); } } ret = 0; u = 1; for (int j = 0; j < ss.size(); ++j) { u = (u * (ss[j] + 1)) % MOD; ret += ss[j]; } ans = (ans + u - ret - 1) % MOD; } if (ans < 0) ans += MOD; cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; const int maxn = 2e3 + 5; char s[maxn]; int vis[maxn]; vector<pair<int, int>> g[maxn]; void add(int u, int v, int c) { g[u].push_back(make_pair(v, c)); g[v].push_back(make_pair(u, c)); } int check(int u) { if (vis[u] == -1) vis[u] = 0; for (int i = 0; i < (int)g[u].size(); ++i) { int v, c; tie(v, c) = g[u][i]; if (vis[v] == -1) { vis[v] = c ? vis[u] ^ 1 : vis[u]; if (check(v) == 0) return 0; } else { if ((c && vis[v] == vis[u]) || (!c && vis[v] != vis[u])) return 0; } } return 1; } long long Pow(long long a, long long b) { long long ret = 1; while (b) { if (b & 1) { ret *= a; ret %= mod; } a *= a; a %= mod; b >>= 1; } return ret; } long long solve(int n, int m) { for (int i = 0; i < maxn; ++i) g[i].clear(); for (int i = 1; i <= n / 2; ++i) add(i, n + 1 - i, 0); for (int i = 1; i <= m / 2; ++i) add(n + n - m + i, n + n + 1 - i, 0); for (int i = 1; i <= n; ++i) { if (s[i - 1] == '?') continue; add(i, i + n, s[i - 1] - '0'); } add(2 * n + 1, 1, 0); add(2 * n + 1, n + n - m + 1, 0); for (int i = 1; i <= n - m; ++i) add(2 * n + 2, i + n, 0); int cnt = 0; fill(vis, vis + maxn, -1); for (int i = 1; i <= 2 * n + 2; ++i) { if (vis[i] != -1) continue; if (check(i)) cnt++; else return 0; } return Pow(2, cnt - 1); } int main() { scanf("%s", s); int len = strlen(s); long long ans = 0; for (int i = 1; i < len; ++i) { ans += solve(len, i); ans %= mod; } printf("%lld\n", ans % mod); return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, m, a[110], b[110]; void getans(int n, int a[110]) { if (n == 1) a[1] = 1; else if (n == 2) a[1] = 3, a[2] = 4; else if (n % 2) { a[1] = 2; for (int i = 2; i < n; i++) a[i] = 1; a[n] = (n + 1) / 2; } else { for (int i = 1; i < n; i++) a[i] = 1; a[n] = (n - 2) / 2; } } int main() { scanf("%d%d", &n, &m); getans(n, a); getans(m, b); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) printf("%d ", a[i] * b[j]); printf("\n"); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int a[105][105], u[105], v[105]; int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) scanf("%d", &a[i][j]); for (int j = 0; j < m; j++) { int cnt[105] = {}; for (int i = 0; i < n; i++) if (!u[i]) ++cnt[a[i][j]]; for (int i = 0; i < n; i++) if (!u[i] && a[i][j]) { if (v[a[i][j]]) u[i] = j + 1; else if (cnt[a[i][j]] >= 2) u[i] = v[a[i][j]] = j + 1; } } for (int i = 0; i < n; i++) printf("%d\n", u[i]); }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 100 * 1000 + 100; int n, q; long long sum[maxn]; int a[maxn]; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; ++i) cin >> a[i]; sort(a, a + n, greater<int>()); for (int i = 1; i < n + 1; ++i) sum[i] = sum[i - 1] + a[i - 1]; long long ansp = 0; for (int i = 1; i < n + 1; ++i) ansp += sum[n] - sum[i]; cin >> q; for (int i = 0; i < q; ++i) { long long t; cin >> t; if (t == 1) { cout << ansp << endl; continue; } long long ans = 0; long long cur = 1; for (long long k = t; cur < n; k *= t) { ans += (sum[n] - sum[cur]); cur += k; } cout << ans << " "; } cout << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; struct node { int u, v, l, r; } que[105000]; map<pair<int, int>, int> mp; int n, q; int fa[105000]; int val[105000]; int dep[105000]; int tot, cnt; int save[105000 << 1]; int getfa(int x) { if (x == fa[x]) return x; return getfa(fa[x]); } int getval(int x) { int ans = 0; while (x != fa[x]) { ans ^= val[x]; x = fa[x]; } return ans; } void undo(int tag) { while (cnt != tag) { if (save[cnt] < 0) { dep[-save[cnt--]]--; } else { fa[save[cnt]] = save[cnt]; val[save[cnt]] = 0; cnt--; } } } void unio(int u, int v, int Val) { fa[u] = v; val[u] = Val; save[++cnt] = u; } void solve(int L, int R, int num) { int tag = cnt; for (int i = 1; i <= num; i++) { if (que[i].l <= L && que[i].r >= R) { int fu = getfa(que[i].u), fv = getfa(que[i].v); int valu = getval(que[i].u), valv = getval(que[i].v); if (fu == fv && (valu ^ valv) == 0) { for (int i = L; i <= R; i++) printf("NO\n"); undo(tag); return; } unio(fu, fv, valu ^ valv ^ 1); swap(que[i--], que[num--]); } } if (L == R) { printf("YES\n"); undo(tag); return; } int mid = (L + R) / 2; int pnum = num; for (int i = 1; i <= pnum; i++) { if (que[i].l > mid) swap(que[i--], que[pnum--]); } solve(L, mid, pnum); pnum = num; for (int i = 1; i <= pnum; i++) { if (que[i].r <= mid) swap(que[i--], que[pnum--]); } solve(mid + 1, R, pnum); undo(tag); } int main() { cin >> n >> q; for (int i = 1; i <= q; i++) { int x, y; cin >> x >> y; if (mp.find(make_pair(x, y)) == mp.end()) { que[++tot] = (node){x, y, i, q}; mp[make_pair(x, y)] = tot; } else { que[mp[make_pair(x, y)]].r = i - 1; mp.erase(mp.find(make_pair(x, y))); } } for (int i = 1; i <= n; i++) fa[i] = i; solve(1, q, tot); }
6
#include <bits/stdc++.h> using namespace std; int n, ans; int cnt[100001] = {1}, dep[100001]; int h[100001], nxt[100001], to[100001], tot; inline void ins(int x, int y) { nxt[++tot] = h[x]; to[tot] = y; h[x] = tot; } void DFS(int u) { for (int i = h[u]; i; i = nxt[i]) { dep[to[i]] = dep[u] + 1; ++cnt[dep[u] + 1]; DFS(to[i]); } } int main() { scanf("%d", &n); int x; for (int i = 2; i <= (n); ++i) scanf("%d", &x), ins(x, i); DFS(1); for (int i = 0; i <= (n); ++i) ans += (cnt[i] & 1); printf("%d", ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; long long mod = 1000000007; long long ans[4][410000]; long long mul_inv(long long a, long long b = mod) { long long b0 = b, t, q; long long x0 = 0, x1 = 1; if (b == 1) return 1; while (a > 1) { q = a / b; t = b, b = a % b, a = t; t = x0, x0 = x1 - q * x0, x1 = t; } if (x1 < 0) x1 += b0; return x1; } long long *go(int n) { int layer = (n - 1) / 4; int c = layer * 2; long long cur = 1; for (int i = 0; i <= c; ++i) { ans[0][i * 2] = cur; cur = (cur * (c - i)) % mod; cur = (cur * mul_inv(i + 1)) % mod; } int ns = layer * 4 + 1; for (int i = 0; i < ns + 1; ++i) { if (i == 0 || i == ns) ans[1][i] = 1; else ans[1][i] = ans[0][i] + ans[0][i - 1]; ans[1][i] %= mod; ans[1][i] += mod; ans[1][i] %= mod; } cur = 1; for (int i = 0; i < ns + 2; ++i) { if (i == 0) ans[2][i] = 1; else { ans[2][i] = ans[1][i] + cur * ans[1][i - 1]; cur = -1 * cur; } ans[2][i] %= mod; ans[2][i] += mod; ans[2][i] %= mod; } cur = 1; for (int i = 0; i < ns + 3; ++i) { if (i == 0) ans[3][i] = 1; else ans[3][i] = ans[2][i - 1] + cur * ans[2][i]; cur = -1 * cur; ans[3][i] %= mod; ans[3][i] += mod; ans[3][i] %= mod; } return ans[(n - 1) % 4]; } int main() { int n; scanf("%d", &n); long long *arr = go(n); long long ans = 0; for (int i = 0; i < n; ++i) { int val; scanf("%d", &val); ans += (val * arr[i]) % mod; ans %= mod; ans += mod; ans %= mod; } printf("%I64d\n", ans); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int inf = 1 << 28; const double INF = 1e12, EPS = 1e-9; const int mod = (int)1e9 + 7; int dp[701][701][3][3]; string in; int n, p[701]; int rec(int l, int r, int fl, int fr) { int &res = dp[l][r][fl][fr]; if (res >= 0) return res; if (l >= r) return res = 1; res = 0; for (int cl = 0; cl < 3; cl++) for (int cr = 0; cr < 3; cr++) { if (cl > 0 && cr > 0 || cl == 0 && cr == 0) continue; if (fl > 0 && cl == fl) continue; if (p[l] == r - 1 && fr > 0 && cr == fr) continue; (res += (long long)rec(l + 1, p[l], cl, cr) * rec(p[l] + 1, r, cr, fr) % mod) %= mod; } return res; } void run() { cin >> in; n = in.size(); stack<int> st; for (int i = 0; i < n; i++) { if (in[i] == '(') { st.push(i); } else { p[st.top()] = i; p[i] = st.top(); st.pop(); } } memset(dp, -1, sizeof(dp)); cout << rec(0, n, 0, 0) << endl; } int main() { run(); }
4
#include <bits/stdc++.h> using namespace std; using ull = uint64_t; using ll = int64_t; using ld = long double; const int N = 100228; const int V = 7001; using BS = bitset<V>; BS a[N]; BS b[V]; BS c[V]; bool bad[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout.setf(ios::fixed); cout.precision(20); int n, q; cin >> n >> q; for (int i = 2; i < V; ++i) { for (int j = i * i; j < V; j += i * i) { bad[j] = true; } } for (int i = 1; i < V; ++i) { for (int j = 1; j < V; ++j) { b[i][j] = i % j == 0; } for (int j = 1; j * i < V; ++j) { if (!bad[j]) { c[i][j * i] = true; } } } for (int i = 0; i < q; ++i) { int t; cin >> t; if (t == 1) { int x, v; cin >> x >> v; a[x] = b[v]; } else if (t == 2) { int x, y, z; cin >> x >> y >> z; a[x] = a[y] ^ a[z]; } else if (t == 3) { int x, y, z; cin >> x >> y >> z; a[x] = a[y] & a[z]; } else { int x, v; cin >> x >> v; cout << (a[x] & c[v]).count() % 2; } } }
6
#include <bits/stdc++.h> using namespace std; struct node { int l, r, pos, id; bool operator<(const node &a) const { if (pos == a.pos) return r < a.r; return pos < a.pos; } } ask[200005]; int a[200005], s[200005 << 3]; long long res, ans[200005]; int n, m, B; void cha(int p, int k) { res = res + 1ll * 2 * k * s[p] * p + p; s[p] += k; } void work() { int l = 1, r = 0; for (int i = 1; i <= m; i++) { while (l < ask[i].l) { cha(a[l], -1); l++; } while (l > ask[i].l) { l--; cha(a[l], 1); } while (r > ask[i].r) { cha(a[r], -1); r--; } while (r < ask[i].r) { r++; cha(a[r], 1); } ans[ask[i].id] = res; } for (int i = 1; i <= m; i++) printf("%lld\n", ans[i]); } int main() { scanf("%d %d", &n, &m); B = sqrt(n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= m; i++) { scanf("%d %d", &ask[i].l, &ask[i].r); ask[i].pos = ask[i].l / B; ask[i].id = i; } sort(ask + 1, ask + m + 1); work(); return 0; }
4
#include <bits/stdc++.h> #pragma GCC optimize("O3") const long long MOD = 1e9 + 7; using namespace std; long long n_bits(long long n) { long long x = __builtin_popcount(n); return x; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); double x, l, h, n; cin >> h >> l; long long temp = pow(l, 2); long long cnt = pow(h, 2); x = (temp - cnt) / (2 * h); cout << std::setprecision(12) << x; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 30; int root[N], sz[N], n; vector<int> ans[30]; string a, b; int getroot(int u) { if (u == root[u]) return u; return root[u] = getroot(root[u]); } void mrg(int u, int v) { int ru = getroot(u); int rv = getroot(v); if (ru == rv) return; if (sz[rv] > sz[ru]) swap(ru, rv); sz[rv] += sz[ru]; root[rv] = root[ru]; } int main(int argc, char const *argv[]) { cin >> n; cin >> a >> b; for (int i = 0; i < 26; ++i) { sz[i] = 1; root[i] = i; } for (int i = 0; i < n; ++i) { int u = a[i] - 'a'; int v = b[i] - 'a'; mrg(u, v); } int s = 0; for (int i = 0; i < 26; ++i) { int r = getroot(i); ans[r].push_back(i); } for (int i = 0; i < 26; ++i) { s += max(0, (int)ans[i].size() - 1); } cout << s << '\n'; for (int i = 0; i < 26; ++i) { int x = ans[i].size(); for (int j = 0; j < x - 1; ++j) { cout << (char)(ans[i][j] + 'a') << ' ' << (char)(ans[i][j + 1] + 'a') << '\n'; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; void task1() { int n; string s, t; cin >> n >> s >> t; int pos = n - 1; auto flip = [&](int r) { for (int i = 0; i <= r; ++i) { if (s[i] == '1') s[i] = '0'; else s[i] = '1'; } reverse(s.begin(), s.begin() + r + 1); }; vector<int> ans; while (pos >= 0) { if (t[pos] != s[0]) { ans.push_back(pos); flip(pos); } else { ans.push_back(0); flip(0); ans.push_back(pos); flip(pos); } pos--; } cout << ans.size() << " "; for (int x : ans) cout << x + 1 << " "; cout << "\n"; } void task2() { int n; string s, t; cin >> n >> s >> t; int pos = n - 1; vector<int> ans; bool turn = 0; int l = 0; int r = n - 1; while (pos >= 0) { if (!turn) { char val = s[l]; if (t[pos] != val) { ans.push_back(pos); } else { ans.push_back(0); ans.push_back(pos); } turn = 1 - turn; l++; } else { char val = s[r] == '1' ? '0' : '1'; if (t[pos] != val) { ans.push_back(pos); } else { ans.push_back(0); ans.push_back(pos); } turn = 1 - turn; r--; } pos--; } cout << ans.size() << " "; for (int x : ans) cout << x + 1 << " "; cout << "\n"; } int main() { int t; cin >> t; while (t--) { task2(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int parent(int x, vector<int> &v) { return v[x] = (v[x] == x ? x : parent(v[x], v)); } void u(int x, int y, vector<int> &v, vector<int> &r) { x = parent(x, v); y = parent(y, v); if (r[x] == r[y]) { r[x]++; } if (r[x] > r[y]) { v[y] = x; } else { v[x] = y; } return; } int main() { int n; scanf("%d\n", &n); vector<string> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } vector<int> v(26, -1); vector<int> r(26); for (int i = 0; i < n; i++) { for (int j = 0; j < (int)a[i].size(); j++) { v[a[i][j] - 'a'] = a[i][j] - 'a'; } } for (int i = 0; i < n; i++) { for (int j = 1; j < (int)a[i].size(); j++) { u(a[i][j - 1] - 'a', a[i][j] - 'a', v, r); } } unordered_map<int, int> h; for (int i = 0; i < 26; i++) { if (v[i] != -1) { h[parent(v[i], v)] = 1; } } cout << h.size(); return 0; }
4
#include <cstdio> #include <algorithm> #include <vector> #include <set> #include <cstdlib> #include <utility> #include <cmath> #include <queue> #include <stack> #include <cstring> using namespace std; #define ll long long #ifndef ONLINE_JUDGE #define debug(format, ...) fprintf(stderr, \ "%s:%d: " format "\n", __func__, __LINE__,##__VA_ARGS__) #else #define debug(format, ...) #define NDEBUG #endif int n; ll a[200005]; ll b[200005]; ll tmp[200005]; ll c[200005]; ll f[200005]; long double e[200005]; void cshift(ll* arr, int amt) { for (int i = 1; i <= n; i++) { tmp[(i - 1 + amt) % n + 1] = arr[i]; } for (int i = 1; i <= n; i++) { arr[i] = tmp[i]; } } long double slope(int x, int y) { return (long double)(f[y] - f[x]) / (long double)(y - x); } int main() { scanf("%d", &n); ll max_a = -100, max_loc = 0; for (int i = 1; i <= n; i++) { scanf("%lld", a + i); if (a[i] > max_a) { max_loc = i; max_a = a[i]; } } for (int i = 1; i <= n; i++) scanf("%lld", b + i); cshift(a, n - max_loc); cshift(b, n - max_loc); a[0] = a[n]; b[0] = b[n]; for (int i = 2; i <= n; i++) { c[i] = 2LL * b[i-1] + 2LL * c[i-1] - c[i-2]; } for (int i = 0; i <= n; i++) { f[i] = a[i] - c[i]; //printf("%lld ", f[i]); } //printf("\n"); vector<int> hull; for (int i = 0; i <= n; i++) { while (hull.size() >= 2 && slope(i, hull[hull.size() - 1]) > slope(hull[hull.size() - 1], hull[hull.size() - 2])) hull.pop_back(); hull.push_back(i); } //for (int i = 0; i < hull.size(); i++) printf("%d ", hull[i]); int hullcur = 0; for (int i = 0; i <= n; i++) { while (hull[hullcur] < i) hullcur++; if (hull[hullcur] == i) { e[i] = f[i]; } else { e[i] = (long double)(f[hull[hullcur - 1]]) + slope(hull[hullcur - 1], hull[hullcur]) * (long double)(i - hull[hullcur - 1]); } //printf("%.15Lf\n", e[i]); } long double ans = 0.0; for (int i = 1; i <= n; i++) { ans += (e[i] + (long double)c[i]) / (long double)n; } printf("%.15Lf", ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000000; int prime[MAXN + 1]; void getPrime() { memset(prime, 0, sizeof(prime)); for (int i = 2; i <= MAXN; i++) { if (!prime[i]) prime[++prime[0]] = i; for (int j = 1; j <= prime[0] && prime[j] <= MAXN / i; j++) { prime[prime[j] * i] = 1; if (i % prime[j] == 0) break; } } } long long factor[100][2]; int fatCnt; int getFactors(long long x) { fatCnt = 0; long long tmp = x; for (int i = 1; prime[i] <= tmp / prime[i]; i++) { factor[fatCnt][1] = 0; if (tmp % prime[i] == 0) { factor[fatCnt][0] = prime[i]; while (tmp % prime[i] == 0) { factor[fatCnt][1]++; tmp /= prime[i]; } fatCnt++; } } if (tmp != 1) { factor[fatCnt][0] = tmp; factor[fatCnt++][1] = 1; } return fatCnt; } int a[5010]; int b[5010]; int c[5010]; long long gcd(long long a, long long b) { if (b == 0) return a; else return gcd(b, a % b); } map<int, int> mp; int calc(int num) { getFactors(num); int ans = 0; for (int i = 0; i < fatCnt; i++) if (mp.find(factor[i][0]) == mp.end()) ans += factor[i][1]; else ans -= factor[i][1]; return ans; } int main() { getPrime(); int n, m; while (scanf("%d%d", &n, &m) == 2) { int tmp = 0; for (int i = 0; i < n; i++) { scanf("%d", &a[i]); tmp = gcd(tmp, a[i]); c[i] = tmp; } mp.clear(); for (int i = 0; i < m; i++) { scanf("%d", &b[i]); mp[b[i]] = 1; } int ret = 0; for (int i = 0; i < n; i++) ret += calc(a[i]); tmp = 1; for (int i = n - 1; i >= 0; i--) { c[i] /= tmp; if (calc(c[i]) < 0) { tmp *= c[i]; ret += calc(c[i]) * (-(i + 1)); } } printf("%d\n", ret); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int n; int righ[300010], Left[300010]; struct node { int x, y; } ddd[300010]; void zser() { int l = righ[n - 1], r = Left[0]; int ans = r - l; if (ans < 0) ans = 0; for (int i = 0; i < n; i++) { l = righ[n - 1], r = Left[0]; if (ddd[i].x == l && ddd[i].y == r) l = righ[n - 2], r = Left[1]; else if (ddd[i].x == l) l = righ[n - 2]; else if (ddd[i].y == r) r = Left[1]; int k = r - l; if (k > 0 && ans < k) ans = k; } printf("%d\n", ans); } void ppp() { for (int i = 0; i < n; i++) { scanf("%d%d", &ddd[i].x, &ddd[i].y); righ[i] = ddd[i].x, Left[i] = ddd[i].y; } } int main(void) { scanf("%d", &n); ppp(); sort(Left, Left + n); sort(righ, righ + n); zser(); return 0; }
3
#include <bits/stdc++.h> using namespace std; template <typename T> T mymax(T a, T b) { return (a > b) ? a : b; } template <typename T> T mymin(T a, T b) { return (a < b) ? a : b; } template <typename T> T myabs(T a) { return (a < 0) ? (-1 * a) : a; } template <typename T> T mysqrt(T x) { if (x == 0 | x == 1) return x; int start = 1, End = x, ans; while (start <= End) { int mid = (start + End) / 2; if (mid * mid == x) return mid; if (mid * mid < x) { start = mid + 1; ans = mid; } else End = mid - 1; } return ans; } void fastIO() { ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(20); } long long A[500006]; long long B[500006]; long long C[500006]; vector<long long> Ans; vector<pair<long long, long long> > VP; map<long long, long long> M; vector<long long> prime; bool mark[10000008]; long long N = 1e7; void seive() { int limit = sqrt(N * 1.0) + 2; mark[1] = 1; for (int i = 4; i <= N; i += 2) mark[i] = 1; prime.push_back(2); for (int i = 3; i <= N; i += 2) { if (!mark[i]) { prime.push_back(i); if (i <= limit) { for (int j = i * i; j <= N; j += i * 2) mark[j] = 1; } } } } int main() { fastIO(); long long a, b, c, n, m, k, p, q, r; long long cnt = 0, cntr = 0, sum = 0, ans = 1, check = 0; float x, y, z; long long Max = 0, Max1 = -1e18 + 1; long long Min = 1e18; string s, s1, s3, s4; cin >> n; while (n--) { cin >> m; long long zero = 0; long long one = 0; vector<long long> V; check = 0; for (int i = 0; i < m; i++) { cin >> s; for (int j = 0; j < s.size(); j++) { if (s[j] == '0') zero++; else one++; } if (s.size() % 2 == 1) check = 1; V.push_back(s.size()); } if (zero % 2 != 0 && one % 2 != 0 && check == 0) cout << m - 1 << endl; else cout << m << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; long long a[1000001]; map<long long, long long> M1; map<long long, long long> M2; map<long long, long long>::iterator it; long long F1[1000001]; long long F2[1000001]; long long BIT[1000001]; void update(int u, long long k, int n) { while (u <= n) { BIT[u] += k; u += (u & (-u)); } } long long query(int u) { long long result = 0; while (u > 0) { result += BIT[u]; u -= (u & (-u)); } return result; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); M1[a[i]]++; it = M1.find(a[i]); F1[i] = it->second; } for (int i = n - 1; i >= 0; i--) { M2[a[i]]++; it = M2.find(a[i]); F2[i] = it->second; } long long ans = 0, a1; for (int i = n - 1; i >= 0; i--) { a1 = query(F1[i] - 1); ans += a1; update(F2[i], 1, n); } printf("%lld", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int inv = numeric_limits<int>::max(); const int minv = numeric_limits<int>::min(); const int max_n = 100; int n; long long a[max_n]; int nsol[2]; long long sol[2][max_n]; void solve(long long p, long long q) { sol[0][nsol[0]++] = p / q; if (q != 1) solve(q, p % q); } long long fgcd(long long a, long long b) { if (b == 0) return a; else return fgcd(b, a % b); } bool f(int p) { if (n != nsol[p]) return false; for (int i = 0; i < n; ++i) if (a[i] != sol[p][i]) return false; return true; } int main() { long long p, q; long long g; cin >> p; cin >> q; g = fgcd(p, q); p /= g; q /= g; scanf("%d", &n); for (int i = 0; i < n; ++i) cin >> a[i]; nsol[0] = 0; solve(p, q); nsol[1] = 0; for (int i = 0; i < nsol[0]; ++i) sol[1][nsol[1]++] = sol[0][i]; --sol[1][nsol[1] - 1]; sol[1][nsol[1]++] = 1ll; if (f(0) or f(1)) printf("YES\n"); else printf("NO\n"); return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, a[100005], mil = -1000000000, mir = -1000000000, mal = 1000000000, mar = 1000000000, mini, maxi; string b; int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; cin >> b; for (int i = 4; i < n; i++) { if (b[i] == '1' && b[i - 1] == '0' && b[i - 2] == '0' && b[i - 3] == '0' && b[i - 4] == '0') { maxi = -1000000000; for (int j = 0; j < 5; j++) { maxi = max(maxi, a[i - j]); } mil = max(mil, maxi + 1); } else if (b[i] == '0' && b[i - 1] == '1' && b[i - 2] == '1' && b[i - 3] == '1' && b[i - 4] == '1') { mini = 1000000000; for (int j = 0; j < 5; j++) { mini = min(mini, a[i - j]); } mar = min(mar, mini - 1); } else if (b[i] == '0' && b[i - 1] == '0' && b[i - 2] == '0' && b[i - 3] == '0' && b[i - 4] == '0') { maxi = -1000000000; for (int j = 0; j < 5; j++) { maxi = max(maxi, a[i - j]); } mal = min(mal, maxi); } else if (b[i] == '1' && b[i - 1] == '1' && b[i - 2] == '1' && b[i - 3] == '1' && b[i - 4] == '1') { mini = 1000000000; for (int j = 0; j < 5; j++) { mini = min(mini, a[i - j]); } mir = max(mir, mini); } } cout << mil << ' ' << max(mir, mil); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); int m, n; cin >> m >> n; vector<vector<bool>> st(m, vector<bool>(n)); for (int i = 0; i < m; ++i) { int t; cin >> t; while (t--) { int s; cin >> s; st[i][s - 1] = true; } } string ans = "possible"; for (int i = 0; i < m; ++i) { for (int j = i + 1; j < m; ++j) { bool ok = false; for (int k = 0; k < n; ++k) { ok |= st[i][k] & st[j][k]; } if (!ok) { ans = "impossible"; } } } cout << ans; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, k; int vis[N]; int pos[N]; long long a[N]; long long ABS(long long x) { return x < 0 ? -x : x; } int get(int len, long long down, long long up, int t) { if (t == 1) { long long be = -len / 2 + len; if (be < up) return -len / 2; else return up - len; } else if (t == -1) { long long be = len / 2 - len + 1; if (be > down) return be; else return down + 1; } else { long long be = -len / 2; long long tot1 = 0, tot2 = 0, tot3 = 0, now1 = down + 1, now2 = up - 1; for (int i = 0; i < len; i++) tot1 += ABS(now1), now1++; for (int i = 0; i < len; i++) tot2 += ABS(now2), now2--; if (down < be && -be < up) { for (int i = 0; i < len; i++) tot3 += be, be++; } else tot3 = 1LL << 50; if (tot1 <= tot2 && tot1 <= tot3) return down + 1; if (tot2 <= tot1 && tot2 <= tot3) return now2 + 1; if (tot3 <= tot1 && tot3 <= tot2) return -len / 2; } } bool work(int be) { int cnt = 0, num = 0; for (int i = be; i <= n; i += k) { pos[++cnt] = i; if (vis[i]) num++; } if (num == 0) { for (int i = be + k; i <= n; i += k) if (a[i] <= a[i - k]) return false; return true; } if (cnt == num) { int st = -cnt / 2; for (int i = be; i <= n; i += k) if (vis[i]) a[i] = st++; } else { if (vis[be]) { int len = 0; long long up; for (int i = be; i <= n; i += k) if (!vis[i]) { up = a[i]; len = i - k; break; } long long st = get((len - be) / k + 1, 0, up, 1); for (int i = be; i <= len; i += k) if (vis[i]) a[i] = st++; } if (vis[pos[cnt]]) { int len = 0; long long down; for (int i = cnt; i >= 1; i--) { int in = pos[i]; if (!vis[in]) { down = a[in]; len = i; break; } } long long st = get(cnt - len, down, 0, -1); for (int i = len + 1; i <= cnt; i++) if (vis[pos[i]]) a[pos[i]] = st++; } int last = 1; for (int i = 2; i <= cnt; i++) { int in = pos[i], on = pos[last]; if (!vis[in]) { if (a[in] - a[on] - 1 < i - last - 1) { return false; } long long st = get(i - last - 1, a[on], a[in], 0); for (int j = last + 1; j < i; j++) if (vis[pos[j]]) a[pos[j]] = st++; last = i; } } } return true; } int main() { scanf("%d%d", &n, &k); memset(a, 0, sizeof(a)); memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++) { char s[15]; scanf("%s", s); if (s[0] == '?') vis[i]++; else sscanf(s, "%I64d", &a[i]); } int flag = 0; for (int i = 1; i <= k && !flag; i++) if (!work(i)) flag = 1; if (flag) puts("Incorrect sequence"); else { for (int i = 1; i <= n; i++) { if (i != 1) printf(" "); cout << a[i]; } puts(""); } return 0; }
5
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define Would #define you #define please typedef tuple<int, int, int, int> TP; TP narabi(int a, int b, int c, int d) { TP kari[4]; kari[0] = tie(a, b, c, d); kari[1] = tie(b, c, d, a); kari[2] = tie(c, d, a, b); kari[3] = tie(d, a, b, c); sort(kari, kari + 4); return kari[0]; } int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; TP C[400]; map<TP, int> MP; rep(i, N) { int a, b, c, d; cin >> a >> b >> c >> d; C[i] = narabi(a, b, c, d); MP[C[i]]++; } ll kotae = 0; rep(i, N) rep(j, N) if (i != j) { MP[C[i]]--; MP[C[j]]--; int a, b, c, d, e, f, g, h; a = get<0>(C[i]); b = get<1>(C[i]); c = get<2>(C[i]); d = get<3>(C[i]); e = get<0>(C[j]); f = get<1>(C[j]); g = get<2>(C[j]); h = get<3>(C[j]); rep(x, 4) { TP D[4]; D[0] = narabi(a, d, f, e); D[1] = narabi(a, e, h, b); D[2] = narabi(b, h, g, c); D[3] = narabi(c, g, f, d); ll kari = 1; rep(k, 4) { kari *= MP[D[k]]--; if (get<0>(D[k]) == get<2>(D[k]) && get<1>(D[k]) == get<3>(D[k])) { kari *= 2; if (get<1>(D[k]) == get<2>(D[k])) kari *= 2; } if (kari < 0) kari = 0; } rep(k, 4) MP[D[k]]++; kotae += kari; int tmp = e; e = f; f = g; g = h; h = tmp; } MP[C[i]]++; MP[C[j]]++; } co(kotae / 6); Would you please return 0; }
0
#include<iostream> using namespace std; int main(){ int m,d; int M[]={0,31,29,31,30,31,30,31,31,30,31,30,31}; while(cin>>m>>d){ if(m==0)break; int day=2; for(int i=m-1;i>0;i--){ day+=M[i]; } day+=d; if(day%7==0)cout<<"Monday"<<endl; if(day%7==1)cout<<"Tuesday"<<endl; if(day%7==2)cout<<"Wednesday"<<endl; if(day%7==3)cout<<"Thursday"<<endl; if(day%7==4)cout<<"Friday"<<endl; if(day%7==5)cout<<"Saturday"<<endl; if(day%7==6)cout<<"Sunday"<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1010; int n, a[N], A[N][N]; string s; int main() { cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; for (int i = 1; i <= n; ++i) { cin >> s; for (int j = 0; j < n; ++j) { if (s[j] == '1') A[i][j + 1] = 1; else A[i][j + 1] = 0; } } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (A[j][i] == 0) continue; for (int k = 1; k <= n; ++k) { if (A[i][k]) A[j][k] = 1; } } } for (int i = 1; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { if (A[i][j] && a[i] > a[j]) swap(a[i], a[j]); } } for (int i = 1; i <= n; ++i) cout << a[i] << ' '; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; string s; cin >> s; for (int i = 0; i < s.size(); ++i) if (s[i] - '0' == 8 && s.size() - i >= 11) { cout << "YES\n"; goto end; } cout << "NO\n"; end:; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int n; long double fat[51], memo[51]; long double num_good_permutations(int x) { if (x > n) return 1; if (memo[x] != -1) return memo[x]; long double ret = 0; for (int len = 1; x + len - 1 <= n; len++) { if (len == 1) ret += num_good_permutations(x + len); else ret += fat[len - 2] * num_good_permutations(x + len); if (ret >= ((long long)1e18 + 1)) break; } return memo[x] = ret; } int p[51], used[51]; int main() { fat[0] = 1; for (int i = 1; i < 51; i++) { fat[i] = min((long double)((long long)1e18 + 1), i * fat[i - 1]); } int t; scanf("%d", &t); while (t--) { long long k; scanf("%d%lld", &n, &k); for (int i = 1; i <= n; i++) { memo[i] = -1; } if (num_good_permutations(1) < k) { printf("-1\n"); continue; } for (int i = 0; i <= n; i++) p[i] = 0; long double cnt = 0; int x = 1; while (x <= n) { int len = 1, max_value = x; while (max_value <= n) { long double tmp; if (len == 1) { tmp = num_good_permutations(x + len); } else { tmp = fat[len - 2] * num_good_permutations(x + len); } if (cnt + tmp >= k) break; cnt += tmp; len++; max_value++; } for (int i = x; i <= max_value; i++) { used[i] = false; } p[x] = max_value; used[max_value] = true; for (int i = x + 1; i <= max_value; i++) { int val = x; while (true) { if (used[val]) { val++; continue; } p[i] = val; if (i != max_value) { int j = val, visited = 1; while (j and j != i) { j = p[j]; } if (j == i) { p[i] = 0; val++; continue; } } long double tmp; if (len - (i - x) == 1) { tmp = num_good_permutations(x + len); } else { tmp = fat[len - (i - x) - 2] * num_good_permutations(x + len); } if (cnt + tmp >= k) break; cnt += tmp; val++; } p[i] = val; used[val] = true; } x = max_value + 1; } for (int i = 1; i <= n; i++) { if (i > 1) printf(" "); printf("%d", p[i]); } printf("\n"); } }
5
#include <bits/stdc++.h> const int N = 505; int n, k, a[N][N], cnt, Sum; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) for (int j = 1; j < k; j++) a[i][j] = ++cnt; for (int i = 1; i <= n; i++) for (int j = k; j <= n; j++) a[i][j] = ++cnt; for (int i = 1; i <= n; i++) Sum += a[i][k]; printf("%d\n", Sum); for (int i = 1; i <= n; i++, puts("")) for (int j = 1; j <= n; j++) printf("%d ", a[i][j]); }
3
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> bool Max(T1 &a, T2 b) { return a < b ? a = b, 1 : 0; } template <typename T1, typename T2> bool Min(T1 &a, T2 b) { return a > b ? a = b, 1 : 0; } template <int MOD> struct ModInt { unsigned x; ModInt() : x(0) {} ModInt(signed sig) : x(sig) {} ModInt(signed long long sig) : x(sig % MOD) {} int get() const { return (int)x; } ModInt power(unsigned p) { ModInt res = 1, a = *this; while (p) { if (p & 1) res *= a; a *= a; p >>= 1; } return res; } ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; } ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; } ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; } ModInt &operator/=(ModInt that) { return (*this) *= that.power(MOD - 2); } ModInt operator+(ModInt that) const { return ModInt(*this) += that; } ModInt operator-(ModInt that) const { return ModInt(*this) -= that; } ModInt operator*(ModInt that) const { return ModInt(*this) *= that; } ModInt operator/(ModInt that) const { return ModInt(*this) /= that; } }; const int N = 401; ModInt<1000000007> f[N][N + 1]; void solve() { int k; cin >> k; f[1][0] = f[1][1] = 1; for (int i = 1; i <= (k - 1); ++i) { for (int l = 0; l < (k + 1); ++l) if (f[i][l].x) { for (int r = 0; r < (k + 1); ++r) if (f[i][r].x && l + r < 400) { ModInt<1000000007> x = f[i][l] * f[i][r]; f[i + 1][l + r] += x; f[i + 1][l + r + 1] += x; f[i + 1][l + r] += x * (l + r) * 2; if (l + r > 0) f[i + 1][l + r - 1] += x * (l + r) * (l + r - 1); } } } cout << f[k][1].x << '\n'; } void init() {} int main(void) { ios::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20); init(); int TC = 1; for (int TI = 1; TI <= (TC); ++TI) { solve(); } return 0; }
4
#include <stdio.h> #include <algorithm> #include <map> using namespace std; int n , k; int a[120000] , b[120000] , c[120000] , tot; void work () { int i , j , l , ti; scanf ( "%d%d" , &n , &k ); if ( 2 * k - 1 > n ) { printf ( "-1\n" ); return ; } for ( i = 1 , j = k ; j <= k + n - 1 ; i++ , j += 2 ) { a[i] = j; } ti = i - 1; l = k + n; for ( j -= 2 ; j >= k ; j -= 2 , ti-- ) { b[ti] = l; l++; } l = k + n + n - 1; for ( j = k + 1 ; j <= k + n - 1 ; i++ , j += 2 ) { a[i] = j; b[i] = l; l--; } for ( i = 1 ; i <= n ; i++ ) c[i] = k + n + n + i - 1; for ( i = 1 ; i <= n ; i++ ) { printf ( "%d %d %d\n" , a[i] , b[i] , c[i] ); } } int main () { work (); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 3e5 + 10; struct node { int x = 0; int more = 0; int l = -1; int r = -1; node() {} }; node pool[4 * MAXN]; int pt = 0; int build(int L, int R) { node A; if (L == R - 1) { pool[pt] = A; pt++; return pt - 1; } int M = (L + R) / 2; A.l = build(L, M); A.r = build(M, R); pool[pt] = A; pt++; return pt - 1; } void relax(int root) { pool[root].x = max(pool[pool[root].l].x, pool[pool[root].r].x); } void push(int root) { int L = pool[root].l, R = pool[root].r; int val = pool[root].more; pool[L].x += val; pool[L].more += val; pool[R].x += val; pool[R].more += val; pool[root].more = 0; } void upd(int root, int L, int R, int l, int r, int x) { if (L == l && R == r) { pool[root].x += x; pool[root].more += x; return; } push(root); int M = (L + R) / 2; if (l < M) { upd(pool[root].l, L, M, l, min(M, r), x); } if (r > M) { upd(pool[root].r, M, R, max(l, M), r, x); } relax(root); } int main() { int n; cin >> n; vector<int> a(n); vector<int> p(n); for (int i = 0; i < n; i++) { cin >> a[i]; a[i]--; p[a[i]] = i; } vector<int> q(n); for (int i = 0; i < n; i++) { cin >> q[i]; q[i]--; } int root = build(0, n); int ans = n - 1; upd(root, 0, n, 0, p[ans] + 1, 1); for (int i = 0; i < n; i++) { while (pool[root].x <= 0) { ans--; upd(root, 0, n, 0, p[ans] + 1, 1); } cout << ans + 1 << " "; upd(root, 0, n, 0, q[i] + 1, -1); } cout << endl; }
5
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } char ch[10]; int main() { int ans = 0; scanf("%s", ch + 1); for (int i = 1; i <= 7; i++) { int tmp; if (ch[i] == 'A') tmp = 1; else { if (ch[i] == '1' && ch[i + 1] == '0') tmp = 10, i++; else tmp = ch[i] - '0'; } ans += tmp; } cout << ans; return 0; }
6
#include <bits/stdc++.h> using namespace std; int n; int a[312345]; map<int, int> last; long long cumSum[312345]; int main() { ios_base::sync_with_stdio(0); cin >> n; vector<int> toRemove; long long totalSum = 0; for (int i = 1; i <= n; i++) { cin >> a[i]; last[a[i]] = i; totalSum += (a[i] >= 0) * a[i]; } for (int i = n; i >= 1; i--) { cumSum[i] = (a[i] >= 0 ? a[i] + cumSum[i + 1] : cumSum[i + 1]); } long long sum = 0; long long best = -1e15; int best_l = n + 1; int best_r = -1; for (int i = 1; i <= n; i++) { if (i < last[a[i]]) { long long curSum; if (a[i] >= 0) { curSum = totalSum - sum - cumSum[last[a[i]] + 1]; } else { curSum = totalSum - sum - cumSum[last[a[i]] + 1] + 2 * a[i]; } if (curSum > best) { best = curSum; best_l = i; best_r = last[a[i]]; } } sum += (a[i] >= 0) * a[i]; } for (int i = 1; i < best_l; i++) toRemove.push_back(i); for (int i = best_r + 1; i <= n; i++) toRemove.push_back(i); for (int i = best_l + 1; i <= best_r - 1; i++) { if (a[i] < 0) toRemove.push_back(i); } sort(toRemove.begin(), toRemove.end()); int sz = toRemove.size(); cout << best << " " << sz << "\n"; for (int i : toRemove) cout << i << " "; }
1
#include <bits/stdc++.h> using namespace std; queue<int> que; struct str { int v, nex, cap, cost; } edge[101000]; int e, n, m, s, t; int f[210], vis[210], fst[210], h[210], dis[210], mark[210]; int ans, flow; void make_edge(int a, int b, int cap, int cost) { edge[++e].nex = fst[a]; fst[a] = e; edge[e].v = b; edge[e].cap = cap; edge[e].cost = cost; edge[++e].nex = fst[b]; fst[b] = e; edge[e].v = a; edge[e].cap = 0; edge[e].cost = -cost; } bool bfs() { for (int i = 1; i <= t; i++) dis[i] = 1 << 29; dis[s] = 0; for (int i = 1; i <= t; i++) mark[i] = 0; que.push(s); mark[s] = 1; while (!que.empty()) { int u = que.front(); que.pop(); mark[u] = 0; for (int i = fst[u]; i; i = edge[i].nex) { int v = edge[i].v; if (!edge[i].cap) continue; if (dis[v] > dis[u] + edge[i].cost) { dis[v] = dis[u] + edge[i].cost; if (!mark[v]) { mark[v] = 1; que.push(v); } } } } return (dis[t] != (1 << 29)); } int dfs(int u, int ma) { if (vis[u]) return 0; if (u == t || !ma) return ma; vis[u] = 1; int f, flow = 0; for (int i = fst[u]; i; i = edge[i].nex) { int v = edge[i].v; if (dis[v] == dis[u] + edge[i].cost && (f = dfs(v, min(edge[i].cap, ma)))) { flow += f; ma -= f; edge[i].cap -= f; edge[i ^ 1].cap += f; if (!ma) { vis[u] = 0; return flow; } } } vis[u] = 0; dis[u] = (1 << 30); return flow; } void solve() { while (bfs()) { int flow = dfs(s, (1 << 30)); ans += flow * dis[t]; } } int main() { scanf("%d %d", &n, &m); s = n + 1; t = s + 1; e = 1; for (int i = 1; i <= m; i++) { int a, b, c, d; scanf("%d %d %d %d", &a, &b, &c, &d); f[b] += d; f[a] -= d; if (c > d) { make_edge(a, b, c - d, 1); make_edge(a, b, (1 << 30), 2); make_edge(b, a, d, 1); } else { make_edge(a, b, (1 << 30), 2); make_edge(b, a, d - c, 0); make_edge(b, a, c, 1); ans += d - c; } } for (int i = 2; i <= n - 1; i++) { if (f[i] > 0) make_edge(s, i, f[i], 0); if (f[i] < 0) make_edge(i, t, -f[i], 0); } make_edge(1, n, (1 << 30), 0); make_edge(n, 1, (1 << 30), 0); if (f[1] + f[n] > 0) make_edge(s, 1, f[1] + f[n], 0); if (f[1] + f[n] < 0) make_edge(1, t, -(f[1] + f[n]), 0); solve(); printf("%d\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 500100; int a[N]; int b[N]; int c[N]; bool vis[N]; int mabs(int x) { return x >= 0 ? x : -x; } int main() { int n; int cnt = 0; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); if (a[i] != 0) cnt++; } if (cnt == 0) { printf("%d ", n * (n + 1) / 2); for (int i = 1; i < n; i++) printf("0 "); } else { for (int i = 0; i < n; i++) { if (a[i] == 0) continue; int tmp = mabs(a[i]); for (int j = 2; j * j <= tmp; j++) { int t = j * j; while (a[i] % t == 0) { a[i] /= t; } } } for (int i = 0; i < n; i++) c[i] = a[i]; sort(c, c + n); int js = unique(c, c + n) - c; for (int i = 0; i < n; i++) { if (a[i] == 0) continue; int p = lower_bound(c, c + js, a[i]) - c + 1; a[i] = p; } for (int i = 0; i < n; i++) { memset(vis, 0, sizeof(vis)); int num = 0; for (int j = i; j < n; j++) { if (!vis[a[j]] && a[j] != 0) { num++; vis[a[j]] = 1; } int tt = max(num, 1); b[tt]++; } } for (int i = 1; i <= n; i++) { printf("%d ", b[i]); } } return 0; }
4
#include <bits/stdc++.h> using namespace std; struct fenvick { vector<bool> got; vector<long long> fen; fenvick() {} fenvick(int n) { fen.resize(n); got.resize(n); } void upd(int pos) { if (got[pos] == true) { return; } got[pos] = true; while (pos < fen.size()) { fen[pos]++; pos |= (pos + 1); } } long long getPref(int pos) { if (pos < 0) { return 0; } long long res = 0; while (pos >= 0) { res += fen[pos]; pos &= (pos + 1); pos--; } return res; } long long getSum(int l, int r) { return getPref(r) - getPref(l - 1); } }; bool comp(pair<long long, long long>& a, pair<long long, long long>& b) { if (a.second == b.second) { return a.first < b.first; } else { return a.second > b.second; } } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); long long n, cur = 0, ans = 0; cin >> n; vector<pair<long long, long long>> arr(n); vector<bool> used; map<long long, int> cnt, indX; for (int i = 0; i < n; i++) { cin >> arr[i].first >> arr[i].second; cnt[arr[i].first]++; } sort(arr.begin(), arr.end(), comp); used.resize(n); for (auto to : cnt) { indX[to.first] = cur; cur++; } fenvick sosi(cnt.size()); for (int i = 0; i < n; i++) { int pos = indX[arr[i].first]; int tmpI = i; while (tmpI < n) { if (used[tmpI]) { break; } if (arr[tmpI].second == arr[i].second) { used[tmpI] = true; sosi.upd(indX[arr[tmpI].first]); tmpI++; } else { break; } } long long l0 = 0, r0 = cnt.size() - 1; if (i > 0) { if (arr[i - 1].second == arr[i].second) { l0 = indX[arr[i - 1].first] + 1; } } ans += sosi.getSum(l0, pos) * sosi.getSum(pos, r0); } cout << ans << '\n'; return 0; }
6
#include <bits/stdc++.h> using namespace std; int check(int k) { int ans; if (k <= 9) { ans = k; } else if (k < 190) { k = k - 9; int g = (k - 1) / 2 + 10; if (k % 2 == 0) ans = g % 10; else ans = g / 10; } else if (k < 2890) { k = k - 189; int g = (k - 1) / 3 + 100; if (k % 3 == 0) ans = g % 10; else if (k % 3 == 2) ans = (g / 10) % 10; else ans = g / 100; } else { k = k - 2889; int g = (k - 1) / 4 + 1000; if (k % 4 == 0) ans = g % 10; else if (k % 4 == 2) ans = (g / 100) % 10; else if (k % 4 == 3) ans = (g / 10) % 10; else ans = g / 1000; } return ans; } int main() { int n; cin >> n; cout << check(n); }
1
#include <bits/stdc++.h> using namespace std; long long n, k, A, B; const int maxn = 1e5 + 5; long long a[maxn]; long long rec(long long l, long long r) { long long x = upper_bound(a + 1, a + k + 1, r) - lower_bound(a + 1, a + k + 1, l), mid = (l + r) / 2; if (l == r) return x == 0 ? A : B * x; return x == 0 ? A : min(B * x * (r - l + 1), rec(l, mid) + rec(mid + 1, r)); } int main() { scanf("%lld%lld%lld%lld", &n, &k, &A, &B); for (int i = 1; i <= k; i++) scanf("%lld", a + i); sort(a + 1, a + k + 1); cout << rec(1LL, 1LL << n) << endl; return 0; }
3
#include<bits/stdc++.h> using namespace std; #define li long int #define lli long long int #define ld long double void solve(){ li n; cin>>n; string s; cin>>s; if(s[n-4]=='2'&&s[n-3]=='0'&&s[n-2]=='2'&&s[n-1]=='0'){ cout<<"YES\n"; return; } if(s[0]=='2'&&s[n-3]=='0'&&s[n-2]=='2'&&s[n-1]=='0'){ cout<<"YES\n"; return; } if(s[0]=='2'&&s[1]=='0'&&s[n-2]=='2'&&s[n-1]=='0'){ cout<<"YES\n"; return; } if(s[0]=='2'&&s[1]=='0'&&s[2]=='2'&&s[n-1]=='0'){ cout<<"YES\n"; return; } if(s[0]=='2'&&s[1]=='0'&&s[2]=='2'&&s[3]=='0'){ cout<<"YES\n"; return; } else cout<<"NO\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); li t; cin>>t; while(t--){ solve(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; char ch[20]; int n; bool found; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } int main() { scanf("%s", ch + 1); n = strlen(ch + 1); for (int i = 1; i <= n / 2; i++) { if (ch[i] == '1' || ch[i] == '2') found = true; else if (ch[i] == '3') { if (ch[n - i + 1] != '3') found = true; } else if (ch[i] == '4') { if (ch[n - i + 1] != '6') found = true; } else if (ch[i] == '5') { if (ch[n - i + 1] != '9') found = true; } else if (ch[i] == '6') { if (ch[n - i + 1] != '4') found = true; } else if (ch[i] == '7') { if (ch[n - i + 1] != '7') found = true; } else if (ch[i] == '8') { if (ch[n - i + 1] != '0') found = true; } else if (ch[i] == '9') { if (ch[n - i + 1] != '5') found = true; } else if (ch[i] == '0') { if (ch[n - i + 1] != '8') found = true; } } if (n & 1) { if (ch[n / 2 + 1] != '3' && ch[n / 2 + 1] != '7') found = true; } if (found) puts("NO"); else puts("YES"); return 0; }
4
#include <bits/stdc++.h> using namespace std; template <class T, class second> ostream& operator<<(ostream& s, pair<T, second>& p) { s << '{' << p.first << ", " << p.second << '}'; return s; } template <class T> ostream& operator<<(ostream& s, vector<T>& v) { if (v.empty()) { s << "[]"; return s; } s << '['; for (int i = 0; i < v.size() - 1; i++) { s << v[i] << ", "; } s << v.back() << ']'; return s; } struct edg { int v, w; edg() { v = w = 0; } edg(const int _v, const int _w) { v = _v; w = _w; } }; int n, m, second; int und[200005]; int hei[200005]; int par[200005][20]; bool vis[200005]; int64_t dist[200005]; vector<edg> adj[200005]; vector<int> adj3[200005]; vector<int> adj2[200005], radj[200005]; const int64_t inf = INT64_MAX >> 4; inline void dijkstra() { fill(dist, dist + n + 1, inf); dist[second] = 0; priority_queue<pair<int64_t, int64_t>> pq; pq.push({0, second}); while (!pq.empty()) { auto [d, v] = pq.top(); pq.pop(); if (vis[v]) { continue; } vis[v] = true; d = -d; for (auto x : adj[v]) { const int64_t rd = d + x.w; if (rd < dist[x.v]) { dist[x.v] = rd; pq.push({-rd, x.v}); } } } } inline int lca(int a, int b) { if (hei[a] < hei[b]) { swap(a, b); } int diff = hei[a] - hei[b]; for (int i = 0; i < 20; i++) { if ((diff >> i) & 1) { a = par[a][i]; } } assert(a); assert(b); if (a == b) { return a; } assert(hei[a] == hei[b]); for (int i = 20 - 1; i >= 0; i--) { if (par[a][i] != par[b][i]) { a = par[a][i]; b = par[b][i]; } } assert(hei[a] == hei[b]); assert(par[a][0] == par[b][0]); assert(par[a][0]); return par[a][0]; } int dfs(int v) { int sz = 1; for (auto x : adj3[v]) { sz += dfs(x); } return und[v] = sz; } int main() { scanf("%d%d%d", &n, &m, &second); vector<array<int, 3>> edges; for (int i = 1; i <= m; i++) { int a, b, w; scanf("%d%d%d", &a, &b, &w); adj[a].push_back(edg(b, w)); adj[b].push_back(edg(a, w)); edges.push_back(array<int, 3>{a, b, w}); } dijkstra(); for (int i = 0; i < m; i++) { const auto [a, b, w] = edges[i]; if ((dist[a] + w) == dist[b]) { adj2[a].push_back(b); radj[b].push_back(a); } if ((dist[b] + w) == dist[a]) { adj2[b].push_back(a); radj[a].push_back(b); } } vector<int> tsort; { queue<int> dz; vector<int> degs(n + 1, 0); for (int i = 1; i <= n; i++) { if (dist[i] < inf) { degs[i] = adj2[i].size(); if (degs[i] == 0) { dz.push(i); } } else { degs[i] = INT32_MAX; } } while (!dz.empty()) { int v = dz.front(); dz.pop(); tsort.push_back(v); for (auto x : radj[v]) { degs[x]--; if (degs[x] == 0) { dz.push(x); } } } reverse(tsort.begin(), tsort.end()); assert(tsort[0] == second); hei[second] = 1; } for (int i = 1; i < tsort.size(); i++) { int v = tsort[i]; assert(radj[v].size()); int l = radj[v][0]; for (int i = 1; i < radj[v].size(); i++) { l = lca(l, radj[v][i]); } adj3[l].push_back(v); par[v][0] = l; hei[v] = hei[l] + 1; for (int i = 1; i < 20; i++) { par[v][i] = par[par[v][i - 1]][i - 1]; } } dfs(second); int mx = 0; for (int i = 1; i <= n; i++) { if (i == second) { continue; } mx = max(mx, und[i]); } printf("%d\n", mx); return 0; }
6
#include <iostream> #include <set> using namespace std; int main() { int n; cin >> n; set<int> a_set; for (int i = 0; i < n; i++) { int a; cin >> a; a_set.insert(a); } cout << (a_set.size() - (a_set.size() % 2 == 0 ? 1 : 0)) << endl; }
0
#include <iostream> #include <string> #include <vector> #include <tuple> using namespace std; typedef long long ll; #define rep(i, n) for(int i=0; i<(n); ++i) const int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1}; int H, W; ll L; vector<string> maze; vector<vector<vector<ll> > > vis; inline bool inrect(int x, int y){ return 0 <= x && x < H && 0 <= y && y < W; } tuple<int, int, char> solve(){ vis.assign(H, vector<vector<ll> >(W, vector<ll>(4, -1))); int x = 0, y = 0, d = 0; rep(i, H)rep(j, W)rep(k, 4)if(maze[i][j] == "NESW"[k]){ x = i; y = j; d = k; break; } for(ll step=0; step<=L; ++step){ if(vis[x][y][d] != -1){ L = vis[x][y][d] + (L - step) % (step - vis[x][y][d]); break; } vis[x][y][d] = step; int nx = x + dx[d], ny = y + dy[d]; while(!inrect(nx, ny) || maze[nx][ny] == '#'){ (d += 1) %= 4; nx = x + dx[d]; ny = y + dy[d]; } x = nx; y = ny; } rep(i, H)rep(j, W)rep(k, 4)if(vis[i][j][k] == L){ x = i; y = j; d = k; break; } return make_tuple(x+1, y+1, "NESW"[d]); } int main(){ while(cin >> H >> W >> L, H|W|L){ maze.assign(H, ""); rep(i, H)cin >> maze[i]; int x, y; char d; tie(x, y, d) = solve(); cout << x << ' ' << y << ' ' << d << '\n'; } return 0; }
0
#include <bits/stdc++.h> using namespace std; template <class T> inline T min(T &a, T &b) { return a < b ? a : b; } template <class T> inline T max(T &a, T &b) { return a > b ? a : b; } template <class T> void read(T &x) { char ch; while ((ch = getchar()) && !isdigit(ch)) ; x = ch - '0'; while ((ch = getchar()) && isdigit(ch)) x = x * 10 + ch - '0'; } struct point { int x, y; point() {} point(int _x, int _y) : x(_x), y(_y) {} }; int n, m, l; long long ans; int main() { read(n), read(m); l = n + 1; int c; for (int j = m + 1; j <= n - m; j++) { for (int i = m + 1; i <= n - m; i++) { int x = j * (j * 2 - l * 3) + l * l, y = l - j, z = l + j; if (i == m + 1) c = max((x + z * i) / (i + y), m); else while ((c + 1) * (i + y) <= x + z * i) c++; if (c + 1 <= n - m) ans += (long long)(n - m - c); else break; } } printf("%lld\n", ans * 3LL); }
4
#include <bits/stdc++.h> using namespace std; const int nax = 1e6 + 5; const int inf = 1e9 + 5; int t[nax], res[nax]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &t[i]); int worst = 0; for (int i = 1; i <= n; ++i) { worst = min(worst, t[i] - i); res[i] = i + worst; } worst = n + 1; for (int i = n; i >= 1; --i) { worst = min(worst, t[i] + i); res[i] = min(res[i], worst - i); } int R = 0; for (int i = 1; i <= n; ++i) R = max(R, res[i]); printf("%d\n", R); return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; int n, k; long long a[1000010]; vector<vector<int>> inds(1000010); long long modvals[1000010]; multiset<long long> curin; long long myans[1000010]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { inds[i % (k - 1)].push_back(i); } for (int i = n - 1; i >= 0; i--) { curin.insert(a[i]); if (i + k < n) { curin.erase(curin.find(a[i + k])); } modvals[i] = *curin.rbegin(); } int val; for (int i = 0; i < k - 1; i++) { stack<pair<long long, int>> cur; for (int j = inds[i].size() - 1; j >= 0; j--) { val = inds[i][j]; long long cv = modvals[val]; while (cur.size() && cv >= cur.top().first) { cur.pop(); } if (cur.size()) { int indo = cur.top().second; long long ca = ((indo - j) * cv) % mod; ca = (ca + myans[inds[i][indo]]); myans[val] = ca; } else { long long nr = inds[i].size() - 1 - j; myans[val] = (nr * cv) % mod; } cur.push(pair<long long, int>(cv, j)); } } long long ans = 0LL; for (int i = 0; i < n; i++) { ans = (ans + myans[i]) % mod; } cout << ans << endl; }
6
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; ll n, m; vector<vector<bool>> ok; bool dfs(ll i = 0, ll j = 0) { if (i >= n || j >= m || !ok[i][j]) return false; if (i == n - 1 && j == m - 1) return true; ok[i][j] = false; if (dfs(i + 1, j)) return true; if (dfs(i, j + 1)) return true; return false; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n >> m; ok.assign(n, vector<bool>(m)); for (ll i = 0; i < n; i++) { for (ll j = 0; j < m; j++) { char c; cin >> c; ok[i][j] = c == '.'; } } ll res = 0; while (dfs()) { res++; ok[0][0] = true; ok[n - 1][m - 1] = true; } cout << res << endl; }
4
#include<iostream> #include<algorithm> int ans; int x, y; int main() { std::cin >> x >> y; if (y < x)y = -y,ans++; ans += std::min(std::abs(y - x), std::abs(x + y) + 1); std::cout << ans << std::endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n; priority_queue<long long> v[64]; int findSize(long long a) { int ret = 0; while (a != 0) { a /= 2; ret++; } return ret; } int first0(long long a) { int size = findSize(a); for (int i = size - 1; i >= 0; i--) { if ((((long long)1 << i) & a) == 0) return i + 1; } return 0; } vector<long long> res; int main() { cin >> n; for (int i = 0; i < n; i++) { long long imsi; cin >> imsi; v[findSize(imsi)].push(imsi); } long long sum; for (int i = 0; i < 64; i++) { if (!v[i].empty()) { sum = v[i].top(); v[i].pop(); res.push_back(sum); break; } } for (int i = 0; i < n - 1; i++) { int size = findSize(sum); int first = first0(sum); int k; for (k = 0; k < size; k++) { if ((((long long int)1 << k) & sum) == 0) { if (!v[k + 1].empty()) { sum ^= v[k + 1].top(); res.push_back(v[k + 1].top()); v[k + 1].pop(); break; } } } if (k != size) continue; int j; for (j = size + 1; j < 64; j++) { if (!v[j].empty()) { sum ^= v[j].top(); res.push_back(v[j].top()); v[j].pop(); break; } } if (j == 64) { cout << "No" << endl; return 0; } } cout << "Yes" << endl; for (int i = 0; i < res.size(); i++) { cout << res[i] << ' '; } }
3
#include <bits/stdc++.h> const int maxn = 2005; using namespace std; int dp[maxn][maxn], n; int gcd(int a, int b) { if (a > b) return b ? gcd(b, a % b) : a; else return a ? gcd(b % a, a) : b; } int main() { scanf("%d", &n); int ceng = 0, sum = 0; for (int i = 1; i <= n; i++) { scanf("%d", &dp[n][i]); if (dp[n][i] == 1) { ceng = n; sum++; } } if (ceng) { printf("%d\n", n - sum); return 0; } for (int i = n - 1; i >= 1; i--) { for (int j = 1; j <= i; j++) { dp[i][j] = gcd(dp[i + 1][j], dp[i + 1][j + 1]); if (dp[i][j] == 1) ceng = max(ceng, i); } } if (ceng) printf("%d\n", 2 * n - ceng - 1); else printf("-1\n"); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); vector<int> a(n); for (int i = 0; i < n; ++i) { scanf("%d", &a[i]); } sort(a.begin(), a.end()); int cur = 1; for (int x : a) { if (x >= cur) { ++cur; } } printf("%d\n", cur); return 0; }
2
#include <bits/stdc++.h> using namespace std; void RAJ_MEHTA_IS_PRO() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int x, first; cin >> x >> first; long long int sum = 0; for (int i = 0; i < n; i++) sum += (a[i] + first - 1) / (x + first); cout << sum * first << "\n"; } int main() { int t = 1; while (t--) { RAJ_MEHTA_IS_PRO(); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int const MAXN = 505; int g[MAXN][MAXN], n, m, q, I, J; int main() { scanf("%d %d %d", &n, &m, &q); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) scanf("%d", &g[i][j]); for (int i = 0; i < q; i++) { scanf(" %d %d", &I, &J); if (g[I][J] == 1) g[I][J] = 0; else g[I][J] = 1; int maior = -1, soma = 0; for (int j = 1; j <= n; j++) { soma = 0; for (int u = 1; u <= m; u++) { soma += g[j][u]; if (soma > maior) maior = soma; if (g[j][u] == 0) soma = 0; } if (soma > maior) maior = soma; } cout << maior << '\n'; } }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, i, j = 0, x = 0, y = 0; cin >> n; int a[n]; for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) { x = i; break; } } for (i = x; i < n - 1; i++) { if (a[i] < a[i + 1]) { y = i; break; } } if (i == n - 1) y = n - 1; reverse(a + x, a + y + 1); for (i = 0; i < n - 1; i++) { if (a[i] > a[i + 1]) break; } if (i == n - 1) cout << "yes" << endl << x + 1 << " " << y + 1; else cout << "no"; }
2
#include<bits/stdc++.h> using namespace std; #define int long long int a[2000]; signed main(){ int n,k; cin >> n >> k; for(int i = 1; i <= n; i++){ cin >> a[i]; a[i] += a[i-1]; } vector<int> vec; for(int i = 0; i < n; i++){ for(int j = i+1; j <= n; j++){ vec.push_back(a[j] - a[i]); //cout << a[j] - a[i] << endl; } } int ans = 0; for(int i = 40; i >= 0; i--){ int cnt = 0; int mask = ans + (1LL << i); for(int j = 0; j < vec.size(); j++){ if((mask & vec[j]) == mask) cnt++; } if(cnt >= k) ans += pow(2,i); } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long n, i; cin >> n; long long A[n + 5], max[n]; for (i = 0; i < n; i++) cin >> A[i]; if (A[0] != 0) cout << 1; else { max[0] = A[0]; for (i = 1; i < n; i++) { if (A[i] > max[i - 1]) max[i] = A[i]; else max[i] = max[i - 1]; } for (i = 1; i < n; i++) { if (A[i] - max[i - 1] > 1) break; } if (i == n) cout << -1; else cout << i + 1; cout << "\n"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int arr[100], per[100], i, j, ac, n, tot; int main() { scanf("%d", &n); for (i = 0; i < n; i++) { scanf("%d", &arr[i]); tot += arr[i]; } if (tot % 2 == 0) { sort(arr, arr + n); for (i = 0; i < n; i++) { if (arr[i] % 2 != 0) { tot -= arr[i]; break; } } if (i == n) tot = 0; } printf("%d", tot); }
2
#include <algorithm> #include <cstring> #include <deque> #include <iostream> #include <map> #include <numeric> #include <queue> #include <set> #include <vector> using namespace std; using ll = long long; struct Trie { Trie *child[2]; Trie() { child[0] = child[1] = nullptr; } void insert(const string &s) { Trie *node = this; for (char c : s) { int k = c - '0'; if (node->child[k] == nullptr) { node->child[k] = new Trie(); } node = node->child[k]; } } }; ll grundy(Trie *node, ll L) { if (node == nullptr) { return L & -L; } return grundy(node->child[0], L - 1) ^ grundy(node->child[1], L - 1); } int main() { int N; ll L; cin >> N >> L; Trie *trie = new Trie(); for (int i = 0; i < N; i++) { string s; cin >> s; trie->insert(s); } cout << (grundy(trie, L + 1) != 0 ? "Alice" : "Bob") << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; inline char gc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } inline int read() { int x = 0; char ch = getchar(); bool positive = 1; for (; !isdigit(ch); ch = getchar()) if (ch == '-') positive = 0; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; return positive ? x : -x; } inline void write(int a) { if (a >= 10) write(a / 10); putchar('0' + a % 10); } inline void writeln(int a) { if (a < 0) { a = -a; putchar('-'); } write(a); puts(""); } const int N = 2005, mod = 1000000007; string a, b, quan; int dp[N][N][2][2], d, m; int dfs(int pos, int yu, bool flag, bool fff, bool fla) { if (pos < 0) return yu == 0; if (!flag && dp[pos][yu][fff][fla] != -1) return dp[pos][yu][fff][fla]; int meiju = flag ? quan[quan.length() - pos - 1] - '0' : 9, ans = 0; if (!fla) { for (int i = 0; i <= meiju; i++) if (i != d || (d == 0 && !fff)) { ans = (ans + dfs(pos - 1, (yu * 10 + i) % m, flag && i == meiju, fff || i, (fla ^ 1) && (fff || i))) % mod; } if (!flag) dp[pos][yu][fff][fla] = ans; return ans; } else { for (int i = 0; i <= meiju; i++) if (i == d) { ans = (ans + dfs(pos - 1, (yu * 10 + i) % m, flag && i == meiju, fff || i, (fla ^ 1) && (fff || i))) % mod; } if (!flag) dp[pos][yu][fff][fla] = ans; return ans; } } int calc(string x) { quan = x; memset(dp, -1, sizeof(dp)); return dfs(x.length() - 1, 0, 1, 0, 0); } int main() { m = read(); d = read(); cin >> a >> b; for (int i = a.length() - 1;; i--) { if (a[i] == '0') a[i] = '9'; else { a[i]--; break; } } if (a[0] == '0' && a.length() > 1) a.erase(a.begin()); cout << (calc(b) - calc(a) + mod) % mod << endl; }
4
#include <bits/stdc++.h> using namespace std; const int N = 100005; const long long inf = 0x7fffffffffffffff; long long cost[N][3]; vector<int> G[N]; int fa[N]; int son[N]; int degree[N]; void dfs(int v, int f) { fa[v] = f; int len = G[v].size(); for (int i = 0; i < len; i++) { int u = G[v][i]; if (u != fa[v]) { son[v] = u; dfs(u, v); } } } long long dp[N][3][3]; int ans[N]; int root; int main() { int n; scanf("%d", &n); for (int i = 0; i < 3; i++) { for (int j = 1; j <= n; j++) { scanf("%I64d", &cost[j][i]); } } for (int i = 1, u, v; i < n; i++) { scanf("%d%d", &u, &v); G[u].push_back(v); G[v].push_back(u); degree[u]++; degree[v]++; if (degree[u] > 2 || degree[v] > 2) { printf("-1\n"); return 0; } } for (int i = 1; i <= n; i++) { if (degree[i] == 1) { root = i; break; } } dfs(root, 0); dp[son[root]][0][1] = cost[root][0] + cost[son[root]][1]; dp[son[root]][0][2] = cost[root][0] + cost[son[root]][2]; dp[son[root]][1][0] = cost[root][1] + cost[son[root]][0]; dp[son[root]][1][2] = cost[root][1] + cost[son[root]][2]; dp[son[root]][2][0] = cost[root][2] + cost[son[root]][0]; dp[son[root]][2][1] = cost[root][2] + cost[son[root]][1]; int v; for (v = son[son[root]];;) { dp[v][0][1] = dp[fa[v]][2][0] + cost[v][1]; dp[v][0][2] = dp[fa[v]][1][0] + cost[v][2]; dp[v][1][0] = dp[fa[v]][2][1] + cost[v][0]; dp[v][1][2] = dp[fa[v]][0][1] + cost[v][2]; dp[v][2][0] = dp[fa[v]][1][2] + cost[v][0]; dp[v][2][1] = dp[fa[v]][0][2] + cost[v][1]; if (son[v] == 0) { break; } v = son[v]; } int u = fa[v]; int p, q; long long t = inf; if (t > dp[v][0][1]) { t = dp[v][0][1]; p = 0; q = 1; } if (t > dp[v][0][2]) { t = dp[v][0][2]; p = 0; q = 2; } if (t > dp[v][1][0]) { t = dp[v][1][0]; p = 1; q = 0; } if (t > dp[v][1][2]) { t = dp[v][1][2]; p = 1; q = 2; } if (t > dp[v][2][0]) { t = dp[v][2][0]; p = 2; q = 0; } if (t > dp[v][2][1]) { t = dp[v][2][1]; p = 2; q = 1; } int pq = 0 ^ 1 ^ 2; ans[v] = q; ans[u] = p; while (true) { u = fa[u]; if (u == 0) { break; } int c = pq ^ p ^ q; ans[u] = c; q = p; p = c; } printf("%I64d\n", t); for (int i = 1; i <= n; i++) { printf("%d ", ans[i] + 1); } return 0; }
4
#include <bits/stdc++.h> using namespace std; struct tr { int x; struct tr* left; struct tr* right; }; int main() { ios_base::sync_with_stdio(0), cin.tie(0); int n; cin >> n; set<int> s; map<int, int> left; map<int, int> right; int x; cin >> x; s.insert(x); for (int i = 1; i < n; ++i) { cin >> x; auto greater = s.upper_bound(x); if (greater != s.end() && !left.count(*greater)) left[*greater] = x, cout << *greater; else --greater, right[*greater] = x, cout << *greater; if (i < n - 1) cout << " "; s.insert(x); } cout << endl; }
4
#include <bits/stdc++.h> namespace ZDY { #pragma GCC optimize(3) template <class T> __inline__ __attribute__((always_inline)) T ABS(T x) { return x > 0 ? x : -x; } template <class T> __inline__ __attribute__((always_inline)) T MAX(T x, T y) { return x > y ? x : y; } template <class T> __inline__ __attribute__((always_inline)) T MIN(T x, T y) { return x < y ? x : y; } template <class T> __inline__ __attribute__((always_inline)) T GCD(T x, T y) { return y ? GCD(y, x % y) : x; } template <class T> __inline__ __attribute__((always_inline)) void SWAP(T &x, T &y) { T t = x; x = y; y = t; } } // namespace ZDY using namespace ZDY; using namespace std; namespace IO { const int str = 1 << 20; static char in_buf[str], *in_s, *in_t; bool __ = 0; __inline__ __attribute__((always_inline)) char gc() { return (in_s == in_t) && (in_t = (in_s = in_buf) + fread(in_buf, 1, str, stdin)), in_s == in_t ? __ = 1, EOF : *in_s++; } __inline__ __attribute__((always_inline)) void in(string &ch) { ch.clear(); if (__) return; char c; while ((c = gc()) != EOF && isspace(c)) ; if (c == EOF) { __ = 1; return; } ch += c; while ((c = gc()) != EOF && !isspace(c)) ch += c; if (c == EOF) __ = 1; } __inline__ __attribute__((always_inline)) void in(char &ch) { if (__) return; char c; while ((c = gc()) != EOF && isspace(c)) ; if (c == EOF) __ = 1; else ch = c; } __inline__ __attribute__((always_inline)) void in(char *ch) { *ch = '\0'; if (__) return; char c; while ((c = gc()) != EOF && isspace(c)) ; if (c == EOF) { __ = 1; return; } *ch = c; ch++; while ((c = gc()) != EOF && !isspace(c)) *ch = c, ch++; if (c == EOF) __ = 1; *ch = '\0'; } template <typename T> __inline__ __attribute__((always_inline)) void in(T &x) { if (__) return; char c = gc(); bool f = 0; while (c != EOF && (c < '0' || c > '9')) f ^= (c == '-'), c = gc(); if (c == EOF) { __ = 1; return; } x = 0; while (c != EOF && '0' <= c && c <= '9') x = x * 10 + c - 48, c = gc(); if (c == EOF) __ = 1; if (f) x = -x; } template <typename T, typename... arr> __inline__ __attribute__((always_inline)) void in(T &x, arr &...y) { in(x), in(y...); } const char ln = '\n'; static char out_buf[str], *out_s = out_buf, *out_t = out_buf + str; __inline__ __attribute__((always_inline)) void flush() { fwrite(out_buf, 1, out_s - out_buf, stdout); out_s = out_buf; } __inline__ __attribute__((always_inline)) void pt(char c) { (out_s == out_t) ? (fwrite(out_s = out_buf, 1, str, stdout), *out_s++ = c) : (*out_s++ = c); } __inline__ __attribute__((always_inline)) void out(const char *s) { while (*s) pt(*s++); } __inline__ __attribute__((always_inline)) void out(char *s) { while (*s) pt(*s++); } __inline__ __attribute__((always_inline)) void out(char c) { pt(c); } __inline__ __attribute__((always_inline)) void out(string s) { for (int i = 0; s[i]; i++) pt(s[i]); } template <typename T> __inline__ __attribute__((always_inline)) void out(T x) { if (!x) { pt('0'); return; } if (x < 0) pt('-'), x = -x; char a[50], t = 0; while (x) a[t++] = x % 10, x /= 10; while (t--) pt(a[t] + '0'); } template <typename T, typename... arr> __inline__ __attribute__((always_inline)) void out(T x, arr &...y) { out(x), out(y...); } } // namespace IO using namespace IO; const int N = 300011; int n, m, cnt = 1, head[N], st[N], tp = 0, bl[N]; struct edge { int to, nxt; } e[N << 1]; __inline__ __attribute__((always_inline)) void add(int x, int y) { e[++cnt].to = y; e[cnt].nxt = head[x]; head[x] = cnt; } int dfn[N], low[N], sz = 0; void tarjan(int x, int la) { dfn[x] = low[x] = ++sz; st[++tp] = x; for (int i(head[x]), to; to = e[i].to, i; i = e[i].nxt) if (i != la) { if (!dfn[to]) tarjan(to, i ^ 1), low[x] = MIN(low[x], low[to]); else low[x] = MIN(low[x], dfn[to]); } if (low[x] == dfn[x]) while (int k = st[tp--]) { bl[k] = x; if (k == x) break; } } int q[N], d[N]; int bfs(int x) { memset(d, 0, sizeof(d)); d[x] = 1; int h = 0, t = 1, mx = 0; q[0] = x; while (h < t) { x = q[h++]; if (d[x] > d[mx]) mx = x; for (int i(head[x]), to; to = e[i].to, i; i = e[i].nxt) if (!d[to]) d[to] = d[x] + 1, q[t++] = to; } return mx; } int main() { in(n, m); int x, y; for (int i(1); i <= m; ++i) in(x, y), add(x, y), add(y, x); tarjan(1, 0); cnt = 0; memset(head, 0, sizeof(head)); for (int i(1); i <= m; ++i) if ((x = bl[e[i * 2].to]) != (y = bl[e[i * 2 + 1].to])) add(x, y), add(y, x); printf("%d\n", d[bfs(bfs(bl[1]))] - 1); }
5
#include<iostream> #include<string> using namespace std; int main() { string s; cin >> s; int c = s[0] - '0'; for (int i = 1; i < s.size(); i++) { if (s[i] != '9') { c--; break; } } cout << c + 9 * (s.size() - 1) << endl; return 0; }
0