solution
stringlengths
53
181k
difficulty
int64
0
13
#include <bits/stdc++.h> int main(void) { int B, b, m, L[200] = {}; scanf("%d%d", &b, &m); for (int i = 0; i < m; i++) { scanf("%d", &B); for (int j = 1; j <= b; j++) { if (L[j]) break; if (j >= B) L[j] = B; } } for (int i = 1; i <= b; i++) printf("%d ", L[i]); putchar('\n'); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int long long inf = numeric_limits<int long long>::max(); struct __io { __io() { ios_base::Init i; ios_base::sync_with_stdio(0); cin.tie(0); } } __io; signed main() { int long long t; cin >> t; while (t--) { int long long n; cin >> n; int long long k = 0; vector<int long long> a{1}; int long long now = 1; int long long sum = 1; while (sum + now * 2 <= n) { now *= 2; sum += now; a.push_back(now); } if (sum < n) { a.push_back(n - sum); } sort(a.begin(), a.end()); cout << a.size() - 1 << '\n'; for (int long long i = 1; i < a.size(); ++i) { cout << a[i] - a[i - 1] << ' '; } cout << '\n'; } }
5
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int maxn = 5e5 + 5; char s[maxn][2]; int d[maxn]; int n; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%s %d", s[i], &d[i]); } int number0 = 0, number1 = 1023; for (int i = 0; i < n; i++) { if (s[i][0] == '|') { number0 = number0 | d[i]; number1 = number1 | d[i]; } else if (s[i][0] == '^') { number0 = number0 ^ d[i]; number1 = number1 ^ d[i]; } else { number0 = number0 & d[i]; number1 = number1 & d[i]; } } int base = 1, XOR = 0, OR = 0, AND = 0; for (int i = 0; i < 10; i++) { int low0 = number0 & 1; int low1 = number1 & 1; if (low0 && !low1) { XOR = XOR | base; } else if (low0 && low1) { OR = OR | base; } else if (!low0 && !low1) { OR = OR | base; XOR = XOR | base; } base <<= 1; number0 >>= 1; number1 >>= 1; } printf("2\n| %d\n^ %d\n", OR, XOR); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int ans = 0; int inp; for (int i = 0; i < n; i++) { cin >> inp; int temp = (inp + k - 1) / k; ans += temp; } cout << (ans + 1) / 2 << endl; }
1
#include <bits/stdc++.h> #pragma GCC optimize("-O2") #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; int LIM1 = 1e4 + 101; int LIM = 3e5 + 10; int tree[300020]; int MAXI = 3e5 + 5; void update(int idx, int val) { while (idx <= MAXI) { tree[idx] += val; idx += idx & -idx; } } int pref(int idx) { int ans = 0; while (idx > 0) { ans += tree[idx]; idx -= idx & -idx; } return ans; } int query(int l, int r) { return pref(r) - pref(l - 1); } vector<vector<int> > Left(LIM), Right(LIM); vector<vector<int> > vec(LIM); int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, k; cin >> n >> k; vector<vector<int> > equal(LIM1); vector<vector<int> > inRange(LIM1); set<int> x1; vector<pair<int, pair<long long int, long long int> > > arr(n + 1); for (int i = 1; i <= n; i++) { int x, r, f; cin >> x >> r >> f; arr[i] = pair<int, pair<long long int, long long int> >( f, pair<long long int, long long int>(x, r)); x1.insert(x + r); x1.insert(x - r); x1.insert(x); equal[f].push_back(i); for (int j = max(1, f - k); j <= min(f + k, LIM1); j++) inRange[j].push_back(i); } map<int, int> pos; int counter = 1; for (auto it = x1.begin(); it != x1.end(); it++) pos[*it] = counter++; vector<int> start(n + 1, 0), end(n + 1, 0); vector<int> mid(n + 1, 0); for (int i = 1; i <= n; i++) { int x = arr[i].second.first; int r = arr[i].second.second; start[i] = pos[x - r]; end[i] = pos[x + r]; mid[i] = pos[x]; } long long int ans = 0; long long int sum = 0; for (int i = 1; i < LIM1; i++) { if (equal[i].size() == 0) continue; long long int s = 0; vector<int> iter; set<int> temp; for (int j = 0; j < inRange[i].size(); j++) { Left[start[inRange[i][j]]].push_back(inRange[i][j]); Right[end[inRange[i][j]]].push_back(inRange[i][j]); int y = inRange[i][j]; temp.insert(start[inRange[i][j]]); temp.insert(end[inRange[i][j]]); } vector<int> clean; for (int j = 0; j < equal[i].size(); j++) { vec[mid[equal[i][j]]].push_back(equal[i][j]); temp.insert(mid[equal[i][j]]); } for (auto it = temp.begin(); it != temp.end(); it++) { iter.push_back(*it); clean.push_back(*it); } for (int j = (int)iter.size() - 1; j >= 0; j--) { int x = iter[j]; for (int k = 0; k < Right[x].size(); k++) update(mid[Right[x][k]], 1); for (int k = 0; k < vec[x].size(); k++) { int point = vec[x][k]; ans = ans + query(mid[point] + 1, end[point]); } for (int k = 0; k < Left[x].size(); k++) update(mid[Left[x][k]], -1); } for (int j = 0; j < clean.size(); j++) { vec[clean[j]].resize(0); Left[clean[j]].resize(0); Right[clean[j]].resize(0); } } cout << ans << endl; ans = ans - n; return 0; }
7
#include <bits/stdc++.h> bool flag[1000000]; int ans[12005], ed; int main() { int i, j, t, k; memset(flag, true, sizeof(flag)); flag[0] = flag[1] = 0; ed = 0; for (i = 2; i < 1000000; ++i) { if (flag[i]) { for (j = 2; j * i < 1000000; ++j) { flag[i * j] = false; } if (i > 10) { k = i; t = 0; while (k) { t *= 10; t += k % 10; k /= 10; } if (t == i) continue; for (k = 2; k * k <= t; ++k) { if (t % k == 0) break; } if (k * k > t) ans[ed++] = i; if (ed > 11183) break; } } } scanf("%d", &i); printf("%d\n", ans[i - 1]); }
4
#include <bits/stdc++.h> using namespace std; int n, m, a, b; int w[55][55]; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> w[i][j]; } } cin >> a >> b; int ans = n * m; for (int i = 1; i + a - 1 <= n; i++) { for (int j = 1; j + b - 1 <= m; j++) { int cnt = 0; for (int k = i; k <= i + a - 1; k++) { for (int l = j; l <= j + b - 1; l++) { cnt += w[k][l]; } } ans = min(ans, cnt); } } for (int i = 1; i + b - 1 <= n; i++) { for (int j = 1; j + a - 1 <= m; j++) { int cnt = 0; for (int k = i; k <= i + b - 1; k++) { for (int l = j; l <= j + a - 1; l++) { cnt += w[k][l]; } } ans = min(ans, cnt); } } cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, k; vector<int> adj[10004]; void dfs(int node, int parent, int len) { if (len == 2) { k++; return; } for (auto child : adj[node]) { if (child != parent) dfs(child, node, len + 1); } } int main() { scanf("%d", &n); int x, y; for (int i = 0; i < n - 1; i++) { scanf("%d %d", &x, &y); adj[x].push_back(y); adj[y].push_back(x); } long long ans = 0; for (int i = 1; i <= n; i++) { k = 0; dfs(i, 0, 0); ans += k; } printf("%I64d\n", ans / 2); return 0; }
2
#include "bits/stdc++.h" using namespace std; typedef long double ld; typedef long long ll; #define sz(x) (int)(x).size() #define eb emplace_back #define pb push_back #define mp make_pair #define f first #define s second template<typename T, typename U> bool ckmin(T &a, const U &b){ return b < a ? a = b, true : false; } template<typename T, typename U> bool ckmax(T &a, const U &b){ return b > a ? a = b, true : false; } int tt; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> tt; while(tt--){ int n, m, k; cin >> n >> k >> m; vector<int> a(n, 0), b(m, 0), used(n, -1); iota(a.begin(), a.end(), 0); for(int i = 0; i < m; ++i){ cin >> b[i]; --b[i]; used[b[i]] = 0; } if((n - m)%(k - 1)){ cout << "NO\n"; continue; } if(n == m){ cout << "YES\n"; continue; } // cerr << "hi"; int cnt = 0, mx = ((n - m)/(k - 1)) * (k/2), md = 0; for(int i = 0; i < n; ++i){ if(!used[i]) continue; if(cnt < mx){ ++cnt, used[i] = -1; if(cnt == mx) md = i; // all L are to the <= md, all R > md } else used[i] = 1; } // cout << mx << "\n"; // for(auto i : used) cout << i << " "; // cout << "\n"; bool wks = false; // already a center for(int i = md + 1; i < n; ++i){ if(!used[i]){ wks = true; } else break; } //try removing from right-middle { int rcen = md + 1; if(rcen == -1) goto nxt; int to_right = 0, side = k/2; for(int i = rcen + 1; i < n; ++i){ if(used[i] == 1) ++to_right; } to_right = (to_right/side) * side; for(int i = md; i >= 0; --i){ if(!used[i]) wks = true; else{ --to_right; if(to_right < 0) break; } } } nxt:; //try removing from right-middle { int lcen = md; if(lcen == -1) goto nxt2; int to_left = 0, side = k/2; for(int i = lcen - 1; i >= 0; --i){ if(used[i] == -1) ++to_left; } to_left = (to_left/side) * side; for(int i = md + 1; i < n; ++i){ if(!used[i]) wks = true; else{ --to_left; if(to_left < 0) break; } } } nxt2:; cout << (wks ? "YES" : "NO") << "\n"; } return 0; }
7
#include <bits/stdc++.h> using namespace std; long long int dp[1000007]; long long int arr[1000007]; int main() { ios::sync_with_stdio(false); cin.tie(0); long long int n, i, j, k, i1, i2; cin >> n; for (i = 1; i <= n; i++) { cin >> arr[i]; i1 = lower_bound(arr, arr + i, arr[i] + 1 - 90) - arr; i2 = lower_bound(arr, arr + i, arr[i] + 1 - 1440) - arr; dp[i] = min(dp[i - 1] + 20, min(dp[i1 - 1] + 50, dp[i2 - 1] + 120)); cout << dp[i] - dp[i - 1] << "\n"; } return 0; }
4
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const double PI = acos(-1); int dcmp(double x) { if (fabs(x) < eps) return 0; if (x < 0) return -1; else return 1; } template <class T> class Point { public: T x, y; Point() { x = y = 0; } Point(T a, T b) : x(a), y(b) {} }; template <class T> class Vec : public Point<T> { public: Vec() : Point<T>() {} Vec(T a, T b) : Point<T>(a, b) {} Vec(Point<T> a) { Point<T>::x = a.x; Point<T>::y = a.y; } operator Point<T>() { Point<T> P(Point<T>::x, Point<T>::y); return P; } }; template <class T> Point<T> operator+(const Point<T> a, const Point<T> b) { return Point<T>(a.x + b.x, a.y + b.y); } template <class T> Point<T> operator-(const Point<T> a, const Point<T> b) { return Point<T>(a.x - b.x, a.y - b.y); } template <class T> T operator*(const Point<T> a, const Point<T> b) { return a.x * b.x + a.y * b.y; } template <class T> T operator^(const Point<T> a, const Point<T> b) { return a.x * b.y - a.y * b.x; } template <class T> double abs(const Point<T> a) { return sqrt(a.x * a.x + a.y * a.y); } template <class T> double arg(const Point<T> a) { return atan2(a.y, a.x); } template <class T> istream &operator>>(istream &input, Point<T> &a) { input >> a.x >> a.y; return input; } template <class T> bool cmpX(Point<T> a, Point<T> b) { if (a.x == b.x) return a.y < b.y; else return a.x < b.x; } template <class T> bool cmpY(Point<T> a, Point<T> b) { if (a.y == b.y) return a.x < b.x; else return a.y < b.y; } template <class T> vector<Point<T>> andrew(vector<Point<T>> &a) { sort(a.begin(), a.end(), cmpX<T>); vector<Point<T>> ans; ans.push_back(a[0]); for (int i = 1; i < a.size(); ++i) { int n = ans.size(); while (n > 1 && ((ans[n - 1] - ans[n - 2]) ^ (a[i] - ans[n - 1])) <= 0) ans.pop_back(), --n; ans.push_back(a[i]); } int temp = ans.size(); for (int i = a.size() - 2; i >= 0; --i) { int n = ans.size(); while (n > temp && ((ans[n - 1] - ans[n - 2]) ^ (a[i] - ans[n - 1])) <= 0) ans.pop_back(), --n; ans.push_back(a[i]); } if (a.size() > 1) ans.pop_back(); return ans; } template <class T> double maxDis(vector<Point<T>> &convex) { if (convex.size() == 1) return 0; if (convex.size() == 2) return abs(convex[1] - convex[0]); int v = 1, n = convex.size(); double ans = 0; convex.push_back(convex[0]); for (int i = 0; i < n; ++i) { while (fabs((convex[i + 1] - convex[i]) ^ (convex[v] - convex[i])) <= fabs((convex[i + 1] - convex[i]) ^ (convex[v + 1] - convex[i]))) v = (v + 1) % n; ans = max(ans, max(abs(convex[i] - convex[v]), abs(convex[i + 1] - convex[v]))); } return ans; } int p1, p2, p3; template <class T> double maxTri(vector<Point<T>> &convex) { if (convex.size() <= 2) return 0; int n = convex.size(); convex.push_back(convex[0]); double ans = 0; for (int i = 0; i < n; ++i) { int k = 2; for (int j = i + 1; j < n; ++j) { while (fabs((convex[j] - convex[i]) ^ (convex[k] - convex[i])) < fabs((convex[j] - convex[i]) ^ (convex[k + 1] - convex[i]))) k = (k + 1) % n; if (fabs((convex[j] - convex[i]) ^ (convex[k] - convex[i])) > ans) { ans = fabs((convex[j] - convex[i]) ^ (convex[k] - convex[i])); p1 = i, p2 = j, p3 = k; } } } return ans / 2.0; } double normalize(double args) { while (args < 0.0) args += 2 * PI; while (args > 2 * PI) args -= 2 * PI; return args; } int main() { long long n, S; scanf("%lld%lld", &n, &S); vector<Point<long long>> a(n); for (int i = 0; i < n; ++i) scanf("%lld%lld", &a[i].x, &a[i].y); a = andrew(a); maxTri(a); cout << a[p2].x + a[p3].x - a[p1].x << ' ' << a[p2].y + a[p3].y - a[p1].y << endl; cout << a[p1].x + a[p3].x - a[p2].x << ' ' << a[p1].y + a[p3].y - a[p2].y << endl; cout << a[p1].x + a[p2].x - a[p3].x << ' ' << a[p1].y + a[p2].y - a[p3].y << endl; return 0; }
9
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; int w[maxn]; int main() { int n; while (scanf("%d", &n) != EOF) { memset(w, 0, sizeof(w)); int temp; for (int i = 0; i < n; i++) { scanf("%d", &temp); w[temp]++; } int step = 0; for (int i = 0; i <= 1e6; i++) { w[i + 1] += (w[i] >> 1); if (w[i] & 1) step++; } int x = w[1000001]; while (x) { if (x & 1) step++; x >>= 1; } printf("%d\n", step); } return 0; }
3
#include<bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin>>t; while(t--) { int n,x,sum=0,temp; cin>>n; int a[3]={0}; for(int i=0;i<n;i++) { cin>>x; if(x%3==0) a[0]++; else if(x%3==1) a[1]++; else a[2]++; } temp=n/3; for(int i=0;i<3;i++) { if(a[i]>temp) { sum+=a[i]-temp; a[(i+1)%3]+=a[i]-temp; a[i]-=a[i]-temp; } } for(int i=0;i<3;i++) { if(a[i]>temp) { sum+=a[i]-temp; a[(i+1)%3]+=a[i]-temp; a[i]-=a[i]-temp; } } cout<<sum<<endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; struct ITree { vector<int> T; int p = 1; ITree(int n = 0) { while (p < n) p *= 2; T.resize(2 * p - 1, 0); } int par(int x) { return (x - 1) / 2; } void add(int l, int v) { l += p - 1; T[l] += v; while (l > 0) { l = par(l); T[l] += v; } } int query(int l, int r) { l += p - 1; r += p - 1; int ans = T[l]; if (l < r) ans += T[r]; while (par(l) != par(r)) { if (l % 2 == 1) ans += T[l + 1]; if (r % 2 == 0) ans += T[r - 1]; l = par(l); r = par(r); } return ans; } }; struct station { int x, r, f; }; const int F = 10002; void unify(vector<int> &vec) { sort(vec.begin(), vec.end()); vec.resize(unique(vec.begin(), vec.end()) - vec.begin()); } int main() { ios_base::sync_with_stdio(0); int n, k; cin >> n >> k; vector<station> vec(n); vector<int> coord[F]; for (int i = (0); i <= (n - 1); ++i) { cin >> vec[i].x >> vec[i].r >> vec[i].f; coord[vec[i].f].push_back(vec[i].x); } for (int i = (0); i <= (F - 1); ++i) { unify(coord[i]); } ITree tree[F]; for (int i = (0); i <= (F - 1); ++i) { tree[i] = ITree(coord[i].size()); } sort(vec.begin(), vec.end(), [](const station &a, const station &b) { return a.r > b.r; }); long long ans = 0; for (auto &s : vec) { int l = s.x - s.r, r = s.x + s.r; for (int freq = (max(0, s.f - k)); freq <= (min(F - 1, s.f + k)); ++freq) { int freqLeft = lower_bound(coord[freq].begin(), coord[freq].end(), l) - coord[freq].begin(); int freqRight = upper_bound(coord[freq].begin(), coord[freq].end(), r) - coord[freq].begin() - 1; if (freqLeft <= freqRight) { ans += tree[freq].query(freqLeft, freqRight); } } int pos = lower_bound(coord[s.f].begin(), coord[s.f].end(), s.x) - coord[s.f].begin(); tree[s.f].add(pos, 1); } cout << ans; return 0; }
7
#include <bits/stdc++.h> using namespace std; bitset<100000001> bs; unsigned int n, A, B, C, D, ans; void zad(unsigned int x) { unsigned int sum = A * x * x * x + B * x * x + C * x + D, o = n; while (o / x) ans += sum * (o /= x); return; } int main() { scanf("%u %u %u %u %u", &n, &A, &B, &C, &D); zad(2), zad(3); for (unsigned int i = 5; i <= n; i++) { if (i % 2 == 0 || i % 3 == 0) { continue; } if (bs[i / 3]) continue; zad(i); if (i > n / i) continue; for (unsigned int j = i * i; j <= n; j += i) { if (j % 3 == 0 || j % 2 == 0) { continue; } bs[j / 3] = 1; } } printf("%u", ans); }
8
#include <bits/stdc++.h> using namespace std; int main() { int t, i, n, x; cin >> t; while (t--) { cin >> x; n = 360 % (180 - x); if (n == 0) { cout << "YES" << endl; } else { cout << "NO" << endl; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; const int INF = INT_MAX; void files() { freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); } int a[MAXN], Min[MAXN]; int main() { int Max = -1, answer = 0, n; cin >> n; for (int i = 1; i < n + 1; i++) cin >> a[i]; Min[n] = a[n]; for (int i = n - 1; i >= 1; i--) Min[i] = min(a[i], Min[i + 1]); for (int i = 1; i < n; i++) { Max = max(Max, a[i]); if (Max <= Min[i + 1]) { answer++; Max = a[i + 1]; } } cout << answer + 1; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int a[n], f = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] != 1) f = 1; } sort(a, a + n); if (f) { cout << "1 "; for (int i = 0; i < n - 1; i++) cout << a[i] << " "; } else { for (int i = 0; i < n - 1; i++) cout << "1 "; cout << 2; } return 0; }
2
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse2") void reverseStr(string& str) { int n = str.length(); for (int i = 0; i < n / 2; i++) swap(str[i], str[n - i - 1]); } void printval(long long n, long long total) { if (total == n) { for (int i = 0; i < n; ++i) { cout << 1 << " "; } } else if (total == 1) { cout << n << " "; } else if (total <= n / 2 + 1) { printval(n / 2, total - 1); printval(n / 2, 1); } else { printval(n / 2, n / 2); printval(n / 2, total - n / 2); } } long long mint(long long a, long long b) { if (a > b) return b; else return a; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); std::cout.precision(15); string s; long long n, x, y; cin >> n >> x >> y; cin >> s; reverseStr(s); long long count = 0; for (int i = 0; i < x; i++) { if (i == y) { if (s[i] == '0') count++; } else { if (s[i] == '1') count++; } } cout << count; }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 105; using pi = pair<int, int>; int n; int a[MAXN], b[MAXN]; vector<int> reduce(int x) { vector<pi> bv; for (int j = 0; j < n; j++) bv.emplace_back(-b[j], j); sort(bv.begin(), bv.end()); vector<int> v; for (int i = 0; i < bv.size() && i < x; i++) { if (b[bv[i].second]) b[bv[i].second]--; v.push_back(bv[i].second); } return v; } int ans; vector<vector<int>> ansfrm; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); ans = -1; { memcpy(b, a, sizeof(b)); vector<vector<int>> v; while (count(b, b + n, b[0]) != n) { v.push_back(reduce(2)); } if (ans < b[0]) { ans = b[0]; ansfrm = v; } } if (n >= 3) { memcpy(b, a, sizeof(b)); vector<vector<int>> v; v.push_back(reduce(3)); while (count(b, b + n, b[0]) != n) { v.push_back(reduce(2)); } if (ans < b[0]) { ans = b[0]; ansfrm = v; } } cout << ans << endl; cout << ansfrm.size() << endl; for (auto &j : ansfrm) { int chk[105] = {}; for (auto &k : j) chk[k] = 1; for (int j = 0; j < n; j++) printf("%d", chk[j]); puts(""); } }
5
#include <bits/stdc++.h> using namespace std; long long i, n, x; vector<long long> v; int main() { cin >> n; for (i = 0; i < n; i++) { cin >> x; v.push_back(x); } sort(v.begin(), v.end()); for (i = 0; i < n; i += 2) cout << v[i] << " "; if (n % 2 == 0) { for (i = n - 1; i >= 0; i -= 2) cout << v[i] << " "; } else for (i = n - 2; i >= 0; i -= 2) cout << v[i] << " "; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long double x, y; cin >> x >> y; if (x == y) { cout << "="; return 0; } long double r1 = (long double)y * (long double)log2(x); long double r2 = (long double)x * (long double)log2(y); if (abs(r1 - r2) < (long double)1e-15) cout << "="; else if (r1 > r2) cout << ">"; else cout << "<"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, m; int main() { int tn, tm, res; cin >> tn >> tm; n = ((tn) < (tm) ? (tn) : (tm)); m = ((tn) > (tm) ? (tn) : (tm)); if (n == 1) { cout << m << endl; return 0; } if (n == 2) { res = (m / 4) * 4; if (m % 4 == 1) res += 2; else if (m % 4 == 2 || m % 4 == 3) res += 4; cout << res << endl; return 0; } cout << (n * m + 1) / 2 << endl; return 0; }
5
#include<bits/stdc++.h> #define ll long long #define N #define mod 998244353 #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,a,n) for (int i=n;i>=a;i--) #define inf 0x3f3f3f3f #define pb push_back #define mp make_pair #define pii pair<int,int> #define fi first #define se second #define lowbit(i) ((i)&(-i)) #define VI vector<int> #define all(x) x.begin(),x.end() #define SZ(x) ((int)x.size()) #define end qwq using namespace std; int n,m; namespace Trie{ int tr[405][26],rt = 0,end[405],cnt; void insert(char *s){ int cur = rt; for(int i = 1;s[i];++i){ if(!tr[cur][s[i]-'a']) tr[cur][s[i]-'a'] = ++cnt; cur = tr[cur][s[i]-'a']; } end[cur] = 1; } } using namespace Trie; struct matrix{ int a[205][205]; matrix(){memset(a,0,sizeof a);} int* operator[](int i){return a[i];} matrix operator*(matrix lhs) const{ matrix res; rep(i,0,200) rep(j,0,200) rep(k,0,200){ res[i][k] = (res[i][k] + 1ll*a[i][j]*lhs[j][k])%mod; } return res; } }base; queue<pii> Q; map<pii,int> id; int tot; int get(pii x){ if(x.fi > x.se) swap(x.fi,x.se); if(id.count(x) > 0) return id[x]; else{ id[x] = tot; Q.push(x); return tot++; } } matrix qpow(matrix a,int b){ matrix res; rep(i,0,200) res[i][i] = 1; while(b){ if(b&1) res = res*a; a = a*a; b >>= 1; } return res; } int main(){ //freopen(".in","r",stdin); //freopen(".out","w",stdout); scanf("%d%d",&n,&m); rep(i,1,n){ char s[105]; scanf("%s",s+1); insert(s); } // printf("%d\n",cnt); get(mp(0,0)); while(!Q.empty()){ pii u = Q.front(); Q.pop(); int x = u.fi,y = u.se,cid = get(mp(x,y)); // printf("%d %d %d\n",x,y,cid); rep(i,0,25){ int tx = tr[x][i],ty = tr[y][i]; if(!tx || !ty) continue; base[cid][get(mp(tx,ty))]++; if(end[tx]) base[cid][get(mp(0,ty))]++; if(end[ty]) base[cid][get(mp(0,tx))]++; if(end[tx] && end[ty]) base[cid][get(mp(0,0))]++; } } base = qpow(base,m); printf("%d\n",base[0][0]); return 0; }
9
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<pair<int, string> > pr; string ins; for (int i = 0; i < n; i++) { cin >> ins; pr.push_back(pair<int, string>(ins.length(), ins)); } sort(pr.begin(), pr.end()); for (int i = 0; i < n - 1; i++) { string t = pr[i].second; string b = pr[i + 1].second; if (b.find(t) == string::npos) { cout << "NO" << endl; return 0; } } cout << "YES" << endl; for (int i = 0; i < n; i++) { cout << pr[i].second << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 1000 + 5; bool p[maxn]; char s[maxn]; void is_prime() { memset(p, true, sizeof(p)); p[0] = p[1] = false; for (int i = 2; i * i <= maxn; i++) { if (p[i]) { for (int j = i * i; j <= maxn; j += i) p[j] = false; } } } int main() { is_prime(); int n; scanf("%d", &n); scanf("%s", s); int a[maxn], sum = 0, z = -1; for (int i = 0; i < n; i++) { a[i] = s[i] - '0'; sum += a[i]; z = max(z, a[i]); } bool flag = false; int sum1[maxn]; memset(sum1, 0, sizeof(sum1)); sum1[0] = a[0]; for (int i = 1; i < n; i++) { sum1[i] = sum1[i - 1] + a[i]; } if (sum == 0 || (p[sum] && z == 1)) { cout << "yes"; return 0; } for (int i = z; i < sum; i++) { if (flag) break; if (sum % i == 0) { int last = -1; for (int j = 0; j < n; j++) { if (last == -1) { if (sum1[j] == i) { last = j; } } else { if (sum1[j] - sum1[last] == i || sum1[j] == sum1[last]) { last = j; } } } if (last == n - 1 && i != sum) flag = true; } } if (flag) cout << "yes"; else cout << "no"; return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, m; int ans; int main() { int i, j, k; while (~scanf("%d", &n)) { k = 0; ans = 0; for (i = 1; i < n; i++) { k = i; bool flag = false; for (j = 1; j < n - 1; j++) { int o = k - 1; if (o % n == 0) { flag = true; break; } k *= i; k %= n; } if (flag == false) { if ((k - 1) % n == 0) { ans++; } } } printf("%d\n", ans); } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 20; pair<int, int> a[maxn]; pair<int, int> b[maxn]; int da[maxn]; int db[maxn]; int va[maxn]; int vb[maxn]; int n, m; int Chk(pair<int, int> x, pair<int, int> y) { if (x.first == y.first && x.second != y.second) return x.first; if (x.first == y.second && x.second != y.first) return x.first; if (x.second == y.first && x.first != y.second) return x.second; if (x.second == y.second && x.first != y.first) return x.second; return 0; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d%d", &a[i].first, &a[i].second); for (int i = 1; i <= m; i++) scanf("%d%d", &b[i].first, &b[i].second); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { int x = Chk(a[i], b[j]); if (!x) continue; if (x != va[i]) da[i]++, va[i] = x; if (x != vb[j]) db[j]++, vb[j] = x; } while (1) { bool sol = 1; int ansa = 0; for (int i = 1; i <= n; i++) if (da[i] >= 2 || (ansa && da[i] && va[i] != ansa)) sol = 0; else if (da[i]) ansa = va[i]; for (int i = 1; i <= m; i++) if (db[i] >= 2 || (ansa && db[i] && vb[i] != ansa)) sol = 0; if (sol) { printf("%d\n", ansa); return 0; } else break; } for (int i = 1; i <= n; i++) if (da[i] >= 2) { printf("-1\n"); return 0; } for (int i = 1; i <= m; i++) if (db[i] >= 2) { printf("-1\n"); return 0; } printf("0\n"); return 0; }
5
#include <cstring> #include <iostream> #include <vector> #include <cstdio> #include <vector> #include <string> #include <algorithm> #include <queue> #include <map> using namespace std; const int maxn = 505 + 7; int a[maxn][maxn],vis[maxn]; int b[maxn * maxn]; vector<pair<int,int> >res; int n; int dfs(vector<int>&vec) { int m = vec.size(); if(m == 1) return vec[0]; vector<vector<int> >nex; int mx = 0; for(int i = 0;i < m;i++) { for(int j = i + 1;j < m;j++) { mx = max(mx,a[vec[i]][vec[j]]); } } b[++n] = mx; int ans = n; for(int i = 0;i < m;i++) vis[i] = 0; for(int i = 0;i < m;i++) { if(vis[i]) continue; vector<int>tmp; for(int j = 0;j < m;j++) { if(vis[j]) continue; if(a[vec[i]][vec[j]] != mx) { tmp.push_back(vec[j]); vis[j] = 1; } } if(tmp.size()) nex.push_back(tmp); } for(int i = 0;i < nex.size();i++) { int pos = dfs(nex[i]); res.push_back({pos,ans}); } return ans; } int main() { scanf("%d",&n); int pos = n + 1; vector<int>vec; for(int i = 1;i <= n;i++) { vec.push_back(i); for(int j = 1;j <= n;j++) { scanf("%d",&a[i][j]); if(i == j) b[i] = a[i][j]; } } dfs(vec); printf("%d\n",n); for(int i = 1;i <= n;i++) { printf("%d",b[i]); if(i == n) printf("\n"); else printf(" "); } printf("%d\n",pos); for(int i = 0;i < n - 1;i++) printf("%d %d\n",res[i].first,res[i].second); return 0; }
7
#include <bits/stdc++.h> using namespace std; int main() { char c; vector<char> line; while (cin >> c) { line.push_back(c); } int s = line.size(); vector<int> result; for (int i = 0; i < s; i++) { if (line[i] == '.') { result.push_back(0); } else { if (line[i + 1] == '.') { result.push_back(1); i++; } else { result.push_back(2); i++; } } } for (int i = 0; i < result.size(); i++) { cout << result[i]; } cout << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; set<int> se[5005]; char arr[500005]; queue<int> que; int main() { int n; scanf("%d", &n); int num = n / 3; int a = 0, b = 0, c = 0; scanf("%s", arr); int st = 0; while (st < n) { if (arr[st] == '0') a++; if (arr[st] == '1') b++; if (arr[st] == '2') c++; st++; } if (a > num) { int ed = n - 1; while (a > num) { if (arr[ed] == '0' && c < num) { arr[ed] = '2'; c++; a--; } else if (arr[ed] == '0' && b < num) { arr[ed] = '1'; b++; a--; } ed--; } } else if (a < num) { int st = 0; while (a < num) { if (arr[st] == '1' && b > num) { arr[st] = '0'; a++; b--; } else if (arr[st] == '2' && c > num) { arr[st] = '0'; a++; c--; } st++; } } if (b > c) { int ed = n - 1; while (b > c) { if (arr[ed] == '1') { arr[ed] = '2'; b--; c++; } ed--; } } else if (b < c) { int st = 0; while (b < c) { if (arr[st] == '2') { arr[st] = '1'; b++; c--; } st++; } } printf("%s", arr); return 0; }
3
#include <bits/stdc++.h> using namespace std; int degree[70000], s[70000]; int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n; cin >> n; queue<int> Q; for (int i = 0; i < n; ++i) { cin >> degree[i] >> s[i]; if (degree[i] == 1) Q.push(i); } vector<pair<int, int> > edges; while (!Q.empty()) { int node = Q.front(); Q.pop(); if (degree[node] == 0) continue; int neighbor = s[node]; edges.push_back(make_pair(node, neighbor)); s[neighbor] ^= node; --degree[neighbor]; if (degree[neighbor] == 1) Q.push(neighbor); } cout << edges.size() << '\n'; for (int i = 0; i < edges.size(); ++i) cout << edges[i].first << ' ' << edges[i].second << '\n'; }
3
#include <bits/stdc++.h> char a[110]; int t[26]; int n; int change(int s, int e) { int i; int flag = 0; for (i = 0; i < n; i++) { if (t[i] == 0) { t[i] = 1; a[s] = 'a' + i; a[e] = 'a' + i; flag = 1; break; } } if (flag == 0) a[s] = a[e] = 'a'; return flag; } int check() { int i, flag; flag = 1; for (i = 0; i < n; i++) { if (t[i] == 0) { flag = 0; break; } } return flag; } int main() { int i, j, m, p; int flag, total; while (scanf("%d", &n) != EOF) { getchar(); for (i = 0; i < n; i++) t[i] = 0; gets(a); m = total = 0; j = strlen(a) - 1; flag = 1; for (i = 0; i <= j; i++, j--) { if (a[i] == '?' && a[j] == '?') { m++; } else if (a[i] != '?' && a[j] == '?') { a[j] = a[i]; } else if (a[i] == '?' && a[j] != '?') { a[i] = a[j]; } if (a[i] != '?') { if (a[i] - 'a' < n && t[a[i] - 'a'] == 0) { total++; t[a[i] - 'a'] = 1; } else if (a[i] - 'a' >= n) { flag = 0; break; } } } j = strlen(a) - 1; p = 0; for (i = 0; i <= j && flag; i++, j--) { if (a[i] == '?' && a[j] == '?') { if (p < m - (n - total)) { a[i] = a[j] = 'a'; p++; } else change(i, j); } else if (a[i] == '?' && a[j] != '?') { if (a[j] - 'a' + 1 > n) { flag = 0; break; } else { a[i] = a[j]; } } else if (a[i] != '?' && a[j] == '?') { if (a[i] - 'a' + 1 > n) { flag = 0; } else { a[j] = a[i]; } } else if (a[i] != '?' && a[j] != '?') { if (a[i] != a[j]) { flag = 0; } } } if (flag == 1 && check()) printf("%s\n", a); else printf("IMPOSSIBLE\n"); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int i; long long arr[100000]; int s = 0; for (i = sqrt(n); i >= 1; i--) { if (n % i == 0) { int x = n / i; long long ans = 2 + (x - 1) * i; ans = ans * x; ans = ans / 2; arr[s] = ans; s++; if (i != sqrt(n)) { x = n / x; ans = 2 + (x - 1) * (n / i); ans = ans * x; ans = ans / 2; arr[s] = ans; s++; } } } sort(arr, arr + s); for (i = 0; i < s; i++) { cout << arr[i] << " "; } }
3
#include <bits/stdc++.h> using namespace std; const int MX = 1000005; const string S = "aeiou"; int n, dic[26]; string u, sa[MX][5]; vector<pair<string, string>> fi, se; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> n; for (int i = 0; i < 5; i++) dic[S[i] - 'a'] = i; while (n--) { cin >> u; int cnt = 0, lst = 0; for (char &c : u) if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') { cnt++; lst = dic[c - 'a']; } if (sa[cnt][lst] == "") sa[cnt][lst] = u; else { se.emplace_back(sa[cnt][lst], u); sa[cnt][lst] = ""; } } for (int i = 1; i < MX; i++) { string cur = ""; for (int j = 0; j < 5; j++) if (sa[i][j] != "") { if (cur == "") cur = sa[i][j]; else { fi.emplace_back(sa[i][j], cur); cur = ""; } } } int sf = fi.size(), ss = se.size(); int m = min(sf, ss) + max(0, (ss - sf) / 2); cout << m << '\n'; while (!fi.empty() && !se.empty()) { pair<string, string> u = fi.back(), v = se.back(); fi.pop_back(); se.pop_back(); cout << u.first << " " << v.first << '\n' << u.second << " " << v.second << '\n'; } while (se.size() >= 2) { pair<string, string> u = se.back(); se.pop_back(); pair<string, string> v = se.back(); se.pop_back(); cout << u.first << " " << v.first << '\n' << u.second << " " << v.second << '\n'; } }
4
#include <bits/stdc++.h> using namespace std; using namespace std; const int N = 1e6 + 100; const int inf = 1e9 + 100; int n, w, m, a[N], seg[N << 2]; long long fen[N]; void change(int dex, int val, int v = 1, int s = 0, int e = N) { if (dex < s || e <= dex) { return; } if (e - s == 1) { seg[v] = val; return; } int mid = (s + e) >> 1, lc = v << 1, rc = lc | 1; change(dex, val, lc, s, mid); change(dex, val, rc, mid, e); seg[v] = max(seg[lc], seg[rc]); } int get_max(int l, int r, int v = 1, int s = 0, int e = N) { if (r <= s || e <= l) return -inf; if (l <= s && e <= r) return seg[v]; int mid = (s + e) >> 1, lc = v << 1, rc = lc | 1; return max(get_max(l, r, lc, s, mid), get_max(l, r, rc, mid, e)); } void add(int dex, long long val) { for (++dex; dex < N; dex += dex & -dex) fen[dex] += val; } long long get(int dex) { long long res = 0; for (; dex; dex ^= dex & -dex) res += fen[dex]; return res; } void ADD(int l, int r, long long x) { add(l, x); add(r, -x); } void act(int i) { ADD(i, i + 1, max(i + m < w || i >= w ? 0 : -inf, get_max(i + m - w, i + 1))); } void get_line() { int maxi = 0; cin >> m; for (int i = 0; i < m; i++) { cin >> a[i]; change(i, a[i]); maxi = max(maxi, a[i]); } for (int i = 0; i < m; i++) { act(i); } for (int i = 0; i < m && w - i - 1 >= m; i++) { act(w - i - 1); } if (2 * m < w) { ADD(m, w - m, maxi); } for (int i = 0; i < m; i++) { change(i, 0); } } int main() { cin >> n >> w; for (int i = 0; i < n; i++) { get_line(); } for (int i = 0; i < w; i++) { cout << get(i + 1) << ' '; } }
7
#include <bits/stdc++.h> using namespace std; struct base { long double r, i; base(long double x = 0, long double y = 0) : r(x), i(y) {} base operator+(const base &a) const { return base(r + a.r, i + a.i); } base operator-(const base &a) const { return base(r - a.r, i - a.i); } base operator*(const base &a) const { return base(r * a.r - i * a.i, r * a.i + i * a.r); } base operator/(long double value) { return base(r / value, i / value); } }; const long double pi = acos(-1.0); int rev(int n, int lg) { int res = 0; for (int i = 0; i < lg; i++) if (n & (1 << i)) res |= 1 << (lg - 1 - i); return res; } void DFT(vector<base> &v, bool inv) { int n = v.size(); int lg = 0; while ((1 << lg) < n) lg++; for (int i = 0; i < n; ++i) if (i < rev(i, lg)) swap(v[i], v[rev(i, lg)]); for (int len = 2; len <= n; len <<= 1) { long double ang = 2 * pi * (inv ? -1 : 1) / len; base wl(cos(ang), sin(ang)); for (int i = 0; i < n; i += len) { base w(1); for (int j = 0; j < len / 2; ++j) { base p = v[i + j], q = v[i + j + len / 2] * w; v[i + j] = p + q; v[i + j + len / 2] = p - q; w = w * wl; } } } if (inv) for (int i = 0; i < n; ++i) v[i] = v[i] / n; } vector<base> mu(vector<base> a) { vector<base> fa(a.begin(), a.end()); int n = 1; while (n < a.size()) n *= 2; n *= 2; fa.resize(n); DFT(fa, 0); for (int i = 0; i < n; i++) fa[i] = fa[i] * fa[i]; DFT(fa, 1); return fa; } int n, m; vector<base> p; bool a[1000001]; vector<int> sol; vector<int> b; bool f = 1; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m; p = vector<base>(m + 1); for (int i = 0; i < n; i++) { int x; cin >> x; a[x] = 1; p[x] = 1; } p = mu(p); for (int i = 1; f && i <= m; i++) { int x = p[i].r + 0.5; if (x && !a[i]) f = 0; if (!x && a[i]) sol.push_back(i); } if (f) { cout << "YES\n" << sol.size() << '\n'; for (int i : sol) cout << i << ' '; } else cout << "NO"; return 0; }
10
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (!b) return a; return gcd(b, a % b); } int main() { long long t, a, b, ans, d, tem; while (cin >> t >> a >> b) { d = gcd(a, b); if (t / b < a / d) ans = min(t, min(a, b) - 1); else { d = a / d * b; ans = t / d * min(a, b); tem = t - (t / d * d - 1); ans += min(tem, min(a, b)); ans--; } d = gcd(ans, t); ans /= d; t /= d; cout << ans << "/" << t << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; const long double PI = acos(-1.0); const int MAXN = 200001; int n, k; int t[MAXN]; long long s[MAXN]; long double f[MAXN]; long double sp[MAXN]; long double s2[MAXN]; int st[MAXN], b[MAXN]; int stc; const long double e = 1e-10; inline long double g(int l, int r) { return (sp[r] - sp[l]) - (long double)(s[l]) * (s2[r] - s2[l]); } inline int bt(int i1, int i2) { long double vl = f[i2] - f[i1] - g(i1, i2); if (vl < e) return i2 + 1; long double nd = (vl / (long double)(s[i2] - s[i1])); return upper_bound(s2, s2 + n + 1, nd + s2[i2]) - s2; } int main() { cin >> n >> k; s[0] = 0; for (int i = 0; i < n; i++) { cin >> t[i]; s[i + 1] = s[i] + t[i]; } sp[0] = 0; long double cur = 0; for (int i = 0; i < n; i++) { cur += t[i]; sp[i + 1] = sp[i] + cur / (long double)t[i]; } s2[0] = 0; for (int i = 0; i < n; i++) { s2[i + 1] = s2[i] + 1.0 / (long double)t[i]; } f[0] = 0; for (int i = 1; i <= n; i++) f[i] = 1e+20; for (int iter = 0; iter < k; iter++) { stc = 0; b[0] = 1; for (int i = 0; i < n; i++) { int db = 1; while (stc > 0) { db = bt(st[stc - 1], i); if (db > b[stc - 1]) break; stc--; } b[stc] = db; st[stc++] = i; } b[stc] = n + 1; for (int i = stc - 1; i >= 0; i--) { for (int j = b[i]; j < b[i + 1]; j++) { f[j] = f[st[i]] + g(st[i], j); } } } cout.setf(ios::fixed); cout.precision(20); cout << f[n] << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; int i, i0, n, m; char a[100005]; int main() { scanf("%s", a); int ans = 9999999, len = strlen(a); for (i = 'a'; i <= 'z'; i++) { int p0 = 0, p = -1; for (i0 = 0; i0 < len; i0++) { if (a[i0] == i) { p0 = max(p0, i0 - p); p = i0; } } p0 = max(p0, i0 - p); ans = min(ans, p0); } printf("%d\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; map<int, int> f; int main() { ios_base::sync_with_stdio(false); int n; cin >> n; long long ans = 0; while (n--) { string s; cin >> s; vector<int> cnt(26, 0); for (char c : s) cnt[c - 'a']++; int mask = 0; for (int i = 0; i < 26; ++i) if (cnt[i] & 1) { mask |= (1 << i); } if (f.count(mask)) ans += f[mask]; for (int i = 0; i < 26; ++i) { int nmask = mask ^ (1 << i); if (f.count(nmask)) ans += f[nmask]; } f[mask]++; } cout << ans << '\n'; }
4
#include <bits/stdc++.h> using namespace std; struct vert { int next; int comp; }; int main() { int n, i, l, comp_counter, j, q; cin >> q; while (q--) { cin >> n; vector<vert> G(n); vector<int> C; for (i = 0; i < n; i++) { cin >> G[i].next; G[i].next--; G[i].comp = -1; } comp_counter = 0; for (i = 0; i < n; i++) { if (G[i].comp == -1) { j = i; l = 0; do { G[j].comp = comp_counter; l++; j = G[j].next; } while (j != i); comp_counter++; C.push_back(l); } } for (i = 0; i < n; i++) { cout << C[G[i].comp] << " "; } cout << '\n'; } return 0; }
1
#include <bits/stdc++.h> using namespace std; inline int read() { static char ch; bool sgn = false; while (ch = getchar(), ch < '0' || ch > '9') if (ch == '-') sgn = true; int res = ch - 48; while (ch = getchar(), ch >= '0' && ch <= '9') res = res * 10 + ch - 48; return sgn ? -res : res; } const int INF = 1 << 30; const int N = 300005; int m, n, k, cnt, cost, S, T, cc[N], c[N], pre[N], prd[N], tot; bool bo[N]; vector<int> Eid[N]; struct Edge { int c, w, v; } E[N]; void AddEdge(int x, int y, int c, int w) { E[++cnt].v = y; E[cnt].c = c; E[cnt].w = w; Eid[x].push_back(cnt); E[++cnt].v = x; E[cnt].c = -c; E[cnt].w = 0; Eid[y].push_back(cnt); } bool spfa() { static int Q[N * 2]; int ql, qr; Q[ql = qr = 0] = S; fill(c, c + T + 1, INF); cc[T] = 0; cc[S] = INF; c[S] = 0; while (ql <= qr) { int x = Q[ql++]; bo[x] = 0; for (int i = 0; i < Eid[x].size(); i++) { const Edge &e = E[Eid[x][i]]; if (e.w && e.c + c[x] < c[e.v]) { if (!bo[e.v]) Q[++qr] = e.v; bo[e.v] = 1; c[e.v] = e.c + c[x]; cc[e.v] = min(e.w, cc[x]); pre[e.v] = x; prd[e.v] = Eid[x][i]; } } } return cc[T] > 0; } int MaxFlow() { cost = 0; while (spfa()) { int k = T; while (k != S) { E[prd[k]].w -= cc[T]; E[prd[k] ^ 1].w += cc[T]; k = pre[k]; } cost += cc[T] * c[T]; } return cost; } int cal(int x) { int res = 0; while (x) res += x & 1, x >>= 1; return res; } int a[300], ans[300], fir[300], ans0; string s[300]; int main() { cnt = -1; n = read(); m = read(); T = n + n + m + 1; for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= n; i++) for (int j = i + 1; j <= n; j++) if (a[i] == a[j]) AddEdge(i, j + n, 0, 1); else AddEdge(i, j + n, cal(a[j]), 1); for (int i = 1; i <= n; i++) AddEdge(S, i, 0, 1), AddEdge(i + n, T, 0, 1); for (int i = 2 * n + 1; i < T; i++) AddEdge(S, i, 0, 1); for (int i = 2 * n + 1; i < T; i++) for (int j = n + 1; j <= 2 * n; j++) AddEdge(i, j, cal(a[j - n]), 1); MaxFlow(); for (int i = 2 * n + 1; i < T; i++) { for (int j = Eid[i].size() - 1; j >= 0; j--) if (E[Eid[i][j]].w == 0 && E[Eid[i][j]].v > n) ans[E[Eid[i][j]].v - n] = i - 2 * n - 1, fir[E[Eid[i][j]].v - n] = 1, ans0++; } for (int i = 1; i <= n; i++) { for (int j = Eid[i].size() - 1; j >= 0; j--) if (E[Eid[i][j]].w == 0 && E[Eid[i][j]].v > n && (a[E[Eid[i][j]].v - n]) != (a[i])) ans0++; } cout << ans0 + n << " " << cost << endl; for (int i = 1; i <= n; i++) { if (fir[i]) printf("%c=%d\n", 'a' + ans[i], a[i]); printf("print(%c)\n", 'a' + ans[i]); for (int j = Eid[i].size() - 1; j >= 0; j--) if (E[Eid[i][j]].w == 0 && E[Eid[i][j]].v > n && (a[E[Eid[i][j]].v - n]) != (a[i])) ans[E[Eid[i][j]].v - n] = ans[i], fir[E[Eid[i][j]].v - n] = 1; else if (E[Eid[i][j]].w == 0 && E[Eid[i][j]].v > n && (a[E[Eid[i][j]].v - n]) == (a[i])) ans[E[Eid[i][j]].v - n] = ans[i]; } }
9
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n; vector<long long> b(n / 2); for (auto& e : b) cin >> e; vector<long long> a(n); for (int i = 0; i < n / 2; ++i) { if (i > 0) { a[i] = max(a[i - 1], a[i - 1] - b[i - 1] + b[i]); } a[n - i - 1] = b[i] - a[i]; } for (auto& e : a) cout << e << ' '; cout << '\n'; }
2
#include <bits/stdc++.h> using namespace std; const int MAX = 2e5 + 10; const int MOD = 1e9 + 7; struct Point { long long x, y; } p[MAX]; Point operator-(Point A, Point B) { return (Point){A.x - B.x, A.y - B.y}; } long long operator^(Point A, Point B) { return A.x * B.y - A.y * B.x; } vector<Point> v; int which(const Point& A) { if (A.x > 0 && A.y >= 0) return 1; if (A.x <= 0 && A.y > 0) return 2; if (A.x < 0 && A.y <= 0) return 3; if (A.x >= 0 && A.y < 0) return 4; } int cmp(const Point& A, const Point& B) { if (which(A) != which(B)) return which(A) < which(B); return (A ^ B) > 0; } int main() { long long n; cin >> n; for (int i = 1; i <= n; i++) scanf("%lld%lld", &p[i].x, &p[i].y); long long ans = 0; for (int i = 1; i <= n; i++) { v.clear(); for (int j = 1; j <= n; j++) if (i != j) v.push_back(p[j] - p[i]); sort(v.begin(), v.end(), cmp); long long k = 0; for (int j = 0; j < n - 1; j++) { while (k - j < n - 1 && (v[j] ^ v[k % (n - 1)]) >= 0) k++; ans -= (k - j - 1) * (k - j - 2) * (k - j - 3) / 6; } ans += (n - 1) * (n - 2) * (n - 3) * (n - 4) / 24; } cout << ans << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; char mapp[105][105] = {}; char Generating(int len1, int len2) { for (int i = 'a'; i <= 'z'; i++) { if (len1 == 0 && len2 == 0) return i; else if (len1 == 0 && len2 != 0 && mapp[len1][len2 - 1] != i && mapp[len1 + 1][len2 - 1] != i) return i; else if (len1 != 0 && len2 == 0 && mapp[len1 - 1][len2] != i && mapp[len1 - 1][len2 + 1] != i) return i; else if (mapp[len1 - 1][len2] != i && mapp[len1 - 1][len2 + 1] != i && mapp[len1][len2 - 1] != i && mapp[len1 + 1][len2 - 1] != i) return i; } } void fill_in_c(int n, int m, int &c) { int nn, mm, len1, len2; nn = n >> 1 << 1; mm = m >> 1 << 1; len1 = 0, len2 = 0; for (len1 = 0; len1 + 2 <= nn && c > 0; len1 += 2) { for (len2 = 0; len2 + 2 <= mm && c > 0; len2 += 2) { char ch; ch = Generating(len1, len2); mapp[len1][len2] = mapp[len1][len2 + 1] = mapp[len1 + 1][len2] = mapp[len1 + 1][len2 + 1] = ch; c--; } } } void fill_in_a(int n, int m, int &a) { int nn, mm, len1, len2; nn = n >> 1 << 1; mm = m >> 1 << 1; len1 = 0, len2 = 0; for (len1 = 0; len1 + 2 <= nn && a >= 2; len1 += 2) { for (len2 = 0; len2 + 2 <= mm && a >= 2; len2 += 2) { if (mapp[len1][len2] != 0) continue; char ch; ch = Generating(len1, len2); mapp[len1][len2] = ch; mapp[len1][len2 + 1] = ch; ch = Generating(len1 + 1, len2); mapp[len1 + 1][len2] = ch; mapp[len1 + 1][len2 + 1] = ch; a -= 2; } } } void fill_in_b(int n, int m, int &b) { int nn, mm, len1, len2; nn = n >> 1 << 1; mm = m >> 1 << 1; len1 = 0, len2 = 0; for (len1 = 0; len1 + 2 <= nn && b >= 2; len1 += 2) { for (len2 = 0; len2 + 2 <= mm && b >= 2; len2 += 2) { if (mapp[len1][len2] != 0) continue; char ch; ch = Generating(len1, len2); mapp[len1][len2] = ch; mapp[len1 + 1][len2] = ch; ch = Generating(len1, len2 + 1); mapp[len1][len2 + 1] = ch; mapp[len1 + 1][len2 + 1] = ch; b -= 2; } } } void fill_in_aa(int n, int m, int a) { int len1, len2; len1 = n - 1, len2 = 0; for (len2 = 0; len2 + 2 <= m && a >= 1; len2 += 2) { char ch; ch = Generating(len1, len2); mapp[len1][len2] = ch; mapp[len1][len2 + 1] = ch; a--; } } void fill_in_bb(int n, int m, int b) { int len1, len2; len1 = 0, len2 = m - 1; for (len1 = 0; len1 + 2 <= n && b >= 1; len1 += 2) { char ch; ch = Generating(len1, len2); mapp[len1][len2] = ch; mapp[len1 + 1][len2] = ch; b--; } } void judge_full(int n, int m) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (mapp[i][j] == 0) { printf("IMPOSSIBLE\n"); return; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { printf("%c", mapp[i][j]); } printf("\n"); } } int main() { int n, m, a, b, c, i, j, k; cin >> n >> m >> a >> b >> c; memset(mapp, 0, sizeof(mapp)); if (n % 2 == 1 && m % 2 == 1) printf("IMPOSSIBLE\n"); else if (n * m > a * 2 + b * 2 + c * 4) printf("IMPOSSIBLE\n"); else { fill_in_c(n, m, c); if (n % 2 == 0) fill_in_a(n, m, a); fill_in_b(n, m, b); if (n % 2 == 1 && m % 2 == 0) fill_in_a(n, m, a); if (m % 2 == 1) fill_in_bb(n, m, b); if (n % 2 == 1) fill_in_aa(n, m, a); judge_full(n, m); } return 0; }
6
#include <bits/stdc++.h> using namespace std; long long sum = 0; char n; map<char, long long> k; int main() { while ((n = getchar()) != '\n') { k[n]++; } for (auto it = k.begin(); it != k.end(); ++it) { sum += (it->second) * (it->second); } cout << sum; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, m; scanf("%d%d", &n, &m); int Max = 1e6 + 100; for (int i = 0; i < m; i++) { int l, r; scanf("%d%d", &l, &r); Max = min(Max, r - l + 1); } cout << Max << endl; int sign = 0; for (int i = 0; i < n; i++) { cout << sign << " "; sign++; sign = sign % Max; } cout << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-9; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, v; cin >> n >> v; vector<pair<int, int> > ingredients(n); cin >> ingredients[0].first; int total = ingredients[0].first; for (int i = 1; i < n; i++) { cin >> ingredients[i].first; total += ingredients[i].first; } for (int i = 0; i < n; i++) { cin >> ingredients[i].second; } auto minimum = ingredients[0]; for (int i = 1; i < n; i++) { if (ingredients[i].second * minimum.first < ingredients[i].first * minimum.second) { minimum = ingredients[i]; } } cout << fixed << setprecision(15); cout << min((double)total * ((double)minimum.second / (double)minimum.first), (double)v) << "\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; constexpr int max_n = 5e5 + 5, mod = 1000696969; int tab[max_n]; long long hash_pref[max_n], hash_pref_2[max_n], hash_pref_3[max_n], power[max_n], power_2[max_n], power_3[max_n]; vector<int> zliczanie[max_n]; map<long long, int> hash_cnt, hash_cnt_2, hash_cnt_3; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, lim = 0; cin >> n; power[0] = 1; for (int i = 1; i <= n; i++) power[i] = power[i - 1] * 500029 % mod; power_2[0] = 1; for (int i = 1; i <= n; i++) power_2[i] = power_2[i - 1] * 500041 % mod; power_3[0] = 1; for (int i = 1; i <= n; i++) power_3[i] = power_3[i - 1] * 500057 % mod; long long ans = 0; hash_cnt[0] = 1; hash_cnt_2[0] = 1; hash_cnt_3[0] = 1; for (int i = 1; i <= n; i++) { cin >> tab[i]; zliczanie[tab[i]].push_back(i); while (zliczanie[tab[i]].size() > 3 && lim < zliczanie[tab[i]][zliczanie[tab[i]].size() - 4]) { hash_cnt[hash_pref[lim]]--; hash_cnt_2[hash_pref_2[lim]]--; hash_cnt_3[hash_pref_3[lim]]--; lim++; } hash_pref[i] = (hash_pref[i - 1] + power[tab[i]] * 500029) % mod; hash_pref_2[i] = (hash_pref_2[i - 1] + power_2[tab[i]] * 500041) % mod; hash_pref_3[i] = (hash_pref_3[i - 1] + power_3[tab[i]] * 500057) % mod; if (!(zliczanie[tab[i]].size() % 3)) { hash_pref[i] = (((hash_pref[i] - power[tab[i]] * 500029 * 3) % mod) + mod) % mod; hash_pref_2[i] = (((hash_pref_2[i] - power_2[tab[i]] * 500041 * 3) % mod) + mod) % mod; hash_pref_3[i] = (((hash_pref_3[i] - power_3[tab[i]] * 500057 * 3) % mod) + mod) % mod; } ans += min({hash_cnt[hash_pref[i]], hash_cnt_2[hash_pref_2[i]], hash_cnt_3[hash_pref_3[i]]}); hash_cnt[hash_pref[i]]++; hash_cnt_2[hash_pref_2[i]]++; hash_cnt_3[hash_pref_3[i]]++; } cout << ans << "\n"; return 0; }
8
#include <bits/stdc++.h> using namespace std; int n, l, k; double dp[201][201][201]; int a[201]; double p[201]; int main() { scanf("%d %d %d", &n, &l, &k); for (int(i) = 0; (i) < (int)(n); (i)++) { cin >> p[i]; p[i] /= 100.0; } for (int(i) = 0; (i) < (int)(n); (i)++) scanf("%d", &a[i]); dp[0][0][0] = 1.0; for (int(i) = 0; (i) < (int)(n); (i)++) for (int(j) = 0; (j) < (int)(i + 1); (j)++) for (int(m) = 0; (m) < (int)(n + 1); (m)++) { int c = a[i] + 1; dp[i + 1][j + 1][min(m + c, n)] += dp[i][j][m] * p[i]; dp[i + 1][j][m] += dp[i][j][m] * (1.0 - p[i]); } double ans = 0.000; for (int(i) = 0; (i) < (int)(n + 1); (i)++) for (int(j) = 0; (j) < (int)(n + 1); (j)++) if (i >= l && k + j - i >= 0) ans += dp[n][i][j]; cout << ans << endl; }
5
#include <bits/stdc++.h> using namespace std; const int inf = 2e9; int a[200200], n; int t[200200 * 4]; void build(int v, int l, int r) { if (l == r) { t[v] = a[l]; return; } int mid = (l + r) >> 1; build(v + v, l, mid), build(v + v + 1, mid + 1, r); t[v] = max(t[v + v], t[v + v + 1]); } void update(int v, int l, int r, int pos, int val) { if (l == r) { t[v] = val; return; } int mid = (l + r) >> 1; if (pos <= mid) update(v + v, l, mid, pos, val); else update(v + v + 1, mid + 1, r, pos, val); t[v] = max(t[v + v], t[v + v + 1]); } int getmax(int v, int l, int r, int L, int R) { if (r < L || R < l) return -inf; if (L <= l && r <= R) return t[v]; int mid = (l + r) >> 1; return max(getmax(v + v, l, mid, L, R), getmax(v + v + 1, mid + 1, r, L, R)); } int get(int l, int r, int val) { if (l > r) return -1; if (l == r) { if (a[l] > val) return l; else return -1; } int mid = (l + r) >> 1; int res1 = getmax(1, 1, n, l, mid); int res2 = getmax(1, 1, n, mid + 1, r); if (res1 > val) return get(l, mid, val); else return get(mid + 1, r, val); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } build(1, 1, n); vector<int> cur; for (int i = 1, j = 1; i <= n; ++i) { cur.push_back(a[j]); update(1, 1, n, j, -inf); int nx = get(j + 1, n, a[j]); a[j] = -inf; if (nx == -1) { for (int k = 0; k < cur.size(); ++k) { cout << cur[k] << " "; } cout << "\n"; cur.clear(); j = get(1, n, -inf); } else { j = nx; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, m, a[10], b[10], td[10], mina = 1e9, minb = 1e9; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; mina = min(mina, a[i]); td[a[i]]++; } for (int i = 1; i <= m; i++) { cin >> b[i]; minb = min(minb, b[i]); td[b[i]]++; } for (int i = 1; i <= 9; i++) if (td[i] == 2) { cout << i; return 0; } if (mina > minb) cout << minb << mina; else cout << mina << minb; }
0
#include <bits/stdc++.h> using namespace std; int n, k; string a[8]; int b[8]; int main() { cin >> n >> k; for (int i = 0; i < n; ++i) { cin >> a[i]; } vector<int> p(0); for (int i = 0; i < k; ++i) { p.push_back(i); } int best = 1000000000; do { for (int i = 0; i < n; ++i) { string s(k, '-'); for (int j = 0; j < k; ++j) { s[p[j]] = a[i][j]; } b[i] = atoi(s.c_str()); } sort(b, b + n); best = min(best, b[n - 1] - b[0]); } while (next_permutation((p).begin(), (p).end())); cout << best << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0), cin.tie(0); long long n, k, x, z, sum = 0; cin >> n >> k; z = 5 - k; for (int i = 0; i < n; i++) { cin >> x; if (x <= z) sum++; } cout << sum / 3 << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int mxn = 1e3 + 5, mod = 1e9 + 7, MOD = 1e9 + 7, mod2 = 998244353; void shof_anta_ray7_fen() { int n; cin >> n; int sum = 0; for (int i = 1; i <= n; i++) { int x; cin >> x; sum += abs(x); } cout << sum; } int main() { ios::sync_with_stdio(0); cin.tie(nullptr); cout.tie(nullptr); int t = 1; while (t--) { shof_anta_ray7_fen(); } return 0; }
0
#include <bits/stdc++.h> using namespace std; unordered_map<int, int> X, pre; unordered_map<int, int>::iterator it; int Find(int u) { it = pre.find(u); if (it == pre.end()) { X[u] = 0; return u; } int v = it->second; int tmp = Find(v); X[u] ^= X[v]; return pre[u] = tmp; } bool join(int a, int b, int x) { int c = Find(a), d = Find(b); if (c == d) return false; pre[d] = c; X[d] = X[a] ^ X[b] ^ x; return true; } int main() { int q, last = 0; scanf("%d", &q); while (q--) { int tp, a, b; scanf("%d%d%d", &tp, &a, &b); a ^= last; b ^= last; if (a > b) swap(a, b); b++; if (tp == 1) { int x; scanf("%d", &x); join(a, b, x ^ last); } else { if (Find(a) != Find(b)) last = -1; else last = X[a] ^ X[b]; printf("%d\n", last); last = abs(last); } } return 0; }
8
#include <bits/stdc++.h> using namespace std; const int MAXN = 1.5e5 + 6.66; const int inf = 1e9 + 7; int seg[MAXN * 4], laz[MAXN * 4]; int a[MAXN], b[MAXN]; int n, m, h; void shift(int id, int val) { seg[id] += val; laz[id] += val; } void shift(int id) { shift(id << 1, laz[id]); shift(id << 1 | 1, laz[id]); laz[id] = 0; } void build(int id, int l, int r) { if (l >= r) return; if (r - l == 1) { seg[id] = -l - 1; return; } int mid = l + (r - l) / 2; build(id << 1, l, mid); build(id << 1 | 1, mid, r); seg[id] = min(seg[id << 1], seg[id << 1 | 1]); } void alter(int id, int l, int r, int b, int e, int val) { if (l >= r) return; if (l >= e || r <= b) return; if (l >= b && r <= e) { seg[id] += val; laz[id] += val; return; } shift(id); int mid = (l + r) >> 1; alter(id << 1, l, mid, b, e, val); alter(id << 1 | 1, mid, r, b, e, val); seg[id] = min(seg[id << 1], seg[id << 1 | 1]); } int get(int id, int l, int r, int b, int e) { if (l >= r) return inf; if (l >= e || r <= b) return inf; if (l >= b && r <= e) return seg[id]; shift(id); int mid = (l + r) >> 1; return min(get(id << 1, l, mid, b, e), get(id << 1 | 1, mid, r, b, e)); } void rem(int val) { int f = h - a[val]; int pos = lower_bound(b, b + m, f) - b; if (pos < m) alter(1, 0, m, pos, m, -1); } void add(int val) { int f = h - a[val]; int pos = lower_bound(b, b + m, f) - b; if (pos < m) alter(1, 0, m, pos, m, 1); } int get_min() { return get(1, 0, m, 0, m); } int main() { cin >> n >> m >> h; for (int i = 0; i < m; i++) cin >> b[i]; for (int i = 0; i < n; i++) cin >> a[i]; sort(b, b + m); build(1, 0, m); int ans = 0; for (int i = 0; i < n; i++) { if (i >= m) rem(i - m); add(i); if (get_min() >= 0) ans++; } cout << ans << endl; return 0; }
9
#include <bits/stdc++.h> using namespace std; long long int MOD = 1000000007; void solve() { long long int m, n; cin >> m >> n; long long int mn[n]; long long int x = 0; memset(mn, -1, sizeof(mn)); for (long long int i = 0; i < m; i++) { long long int a[n]; for (long long int j = 0; j < n; j++) { cin >> a[j]; mn[j] = max(mn[j], a[j]); } sort(a, a + n); x = max(x, a[n - 2]); } sort(mn, mn + n); long long int res = min(x, mn[0]); cout << res << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); int T; cin >> T; while (T--) solve(); return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; pair<long double, long double> point[N]; long double check(long double r, long long n) { long double mn = -1e8, mx = 1e8; for (long long i = 0; i < n; i++) { long double x = (point[i].second * r * 2) - (point[i].second * point[i].second); if (x < 0) return 0; x = sqrtl(x); mn = max(mn, -x + point[i].first); mx = min(mx, x + point[i].first); } return mn <= mx; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n; cin >> n; long long pos = 0, neg = 0; for (long long i = 0; i < n; i++) { cin >> point[i].first >> point[i].second; if (point[i].second <= 0) { point[i].second *= -1; neg = 1; } else pos = 1; } if (pos and neg) { cout << -1 << '\n'; return 0; } long double l = -1, r = 4e18 + 5; for (long long i = 0; i < 500; i++) { long double mid = (l + r) / 2; if (check(mid, n)) r = mid; else l = mid; } cout << fixed << setprecision(12); cout << l << '\n'; }
7
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; void solve(int n) { int i, s, r, l, j, k, a[1100]; scanf("%d", &k); if (n > k) swap(n, k); if (k == 1 || n == 1) printf("%d", k + n - 1); else if (n == 2) { if (k % 4 == 0) printf("%d", k); if (k % 4 == 1) printf("%d", k + 1); if (k % 4 == 2) printf("%d", k + 2); if (k % 4 == 3) printf("%d", k + 1); } else printf("%d", (n * k + 1) / 2); } int main() { int n, m, x, y, i, j, q = 0; while (scanf("%d", &n) != EOF) { solve(n); printf("\n"); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { long long n, m, ans = 0, k; cin >> n >> m; while (m-- > 1) { cin >> k; if (k % 2 && n % 2) ans = !ans; } cin >> k; if (k % 2) ans = !ans; if (ans % 2) cout << "odd"; else cout << "even"; return 0; }
0
#include <bits/stdc++.h> using namespace std; template <typename T> T gcd(T a, T b) { if (a == 0) return b; return gcd(b % a, a); } template <typename T> T pow(T a, T b, long long int m) { T ans = 1; while (b > 0) { if (b % 2 == 1) ans = ((ans % m) * (a % m)) % m; b /= 2; a = ((a % m) * (a % m)) % m; } return ans % m; } template <typename T> void tracev(vector<T> &a) { for (auto &i : a) { cout << i << ' '; } cout << '\n'; } template <typename T> void tracevv(vector<T> &a) { for (auto &i : a) { tracev(i); } } template <typename T> void tracevp(vector<T> &a) { for (auto &i : a) { cout << "i.ff" << ": " << i.first << " | " << "i.ss" << ": " << i.second << "\n"; ; } cout << '\n'; } bool comp(pair<long long int, long long int> &a, pair<long long int, long long int> &b) { return a.second < b.second; } int main() { long long int n; cin >> n; vector<pair<long long int, long long int> > a(n); for (long long int i = 0; i < n; i++) { cin >> a[i].first; a[i].second = i; } sort(a.begin(), a.end()); vector<pair<long long int, long long int> > dp(n + 1, {INT_MAX, INT_MAX}); dp[n] = {0, 0}; for (long long int i = n - 1; i >= 0; i--) { if (i - 2 >= 0) dp[i - 2] = min(dp[i - 2], make_pair(dp[i + 1].first + a[i].first - a[i - 2].first, 3ll)); if (i - 3 >= 0) dp[i - 3] = min(dp[i - 3], make_pair(dp[i + 1].first + a[i].first - a[i - 3].first, 4ll)); if (i - 4 >= 0) dp[i - 4] = min(dp[i - 4], make_pair(dp[i + 1].first + a[i].first - a[i - 4].first, 5ll)); if (i == n - 1) i -= 2; } long long int nx = 0; long long int cnt = 0; while (nx < n) { cnt++; long long int i = nx; for (; nx < i + dp[i].second && nx < n; nx++) { a[nx].first = cnt; } } cout << dp[0].first << ' ' << cnt << '\n'; sort(a.begin(), a.end(), comp); for (int i = 0; i < a.size(); i++) { cout << a[i].first << ' '; } return 0; }
6
#include <bits/stdc++.h> using namespace std; struct node { int d, w, next; }; long long g1[200010], g2[200010]; long long ans; node w[200010]; bool vst[200010]; int rd[200010]; int n, rc; void insert(int st, int ed, int wi) { ++rc; w[rc].d = ed; w[rc].w = wi; w[rc].next = rd[st]; rd[st] = rc; } bool judge(int x) { while (x) { if (x % 10 != 4 && x % 10 != 7) return false; x /= 10; } return true; } void dfs(int x) { int i; vst[x] = 1; for (i = rd[x]; i; i = w[i].next) if (!vst[w[i].d]) { dfs(w[i].d); if (judge(w[i].w)) { g1[x] += g1[w[i].d] + g2[w[i].d] + 1; } else { g1[x] += g1[w[i].d]; g2[x] += g2[w[i].d] + 1; } } vst[x] = 0; } void dfs2(int x, long long val1, long long val2, long long val3, bool valid) { long long tv, tg; int i; tv = g1[x]; vst[x] = 1; if (valid) { val1 += val2; val2 = val3 = 0; } tv += val1 + val3; ans += tv * (tv - 1); for (i = rd[x]; i; i = w[i].next) if (!vst[w[i].d]) { if (judge(w[i].w)) tg = g1[w[i].d] + g2[w[i].d] + 1; else tg = g1[w[i].d]; dfs2(w[i].d, val1, val2 + g1[x] + g2[x] - g1[w[i].d] - g2[w[i].d], val3 + g1[x] - tg, judge(w[i].w)); } vst[x] = 0; } int main() { int i, st, ed, wi; scanf("%d", &n); memset(rd, 0, sizeof(rd)); rc = 0; for (i = 0; i < n - 1; ++i) { scanf("%d%d%d", &st, &ed, &wi); --st; --ed; insert(st, ed, wi); insert(ed, st, wi); } for (i = 0; i < n; ++i) g1[i] = g2[i] = 0; memset(vst, 0, sizeof(vst)); ans = 0; dfs(0); dfs2(0, 0, 0, 0, 0); cout << ans << endl; }
5
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:36777216") using namespace std; int main() { ios_base::sync_with_stdio(false); string a, b; cin >> a >> b; int c = 0, d = 0; for (int i = 0; i < a.length(); i++) if (a[i] == '1') c++; for (int i = 0; i < b.length(); i++) if (b[i] == '1') d++; if (c % 2 == 1) c++; if (d % 2 == 1) d++; if (c >= d) cout << "YES"; else cout << "NO"; }
4
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pil = pair<int, ll>; using pli = pair<ll, int>; using pll = pair<ll, ll>; const int MOD = 1000000007; const int inf = (1 << 30) - 1; const ll INF = (1LL << 60) - 1; template <typename T> bool chmax(T &x, const T &y) { return (x < y) ? (x = y, true) : false; }; template <typename T> bool chmin(T &x, const T &y) { return (x > y) ? (x = y, true) : false; }; struct io_setup { io_setup() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(15); } } io_setup; int main() { string A, B, S; cin >> A >> B >> S; int N = (int)A.size(), M = (int)B.size(), K = (int)S.size(); vector<vector<int>> nxt(K, vector<int>(26)); for (int i = 0; i < K; i++) { for (int j = 0; j < 26; j++) { string T = S.substr(0, i); T += char('A' + j); for (int k = i + 1; k >= 0; k--) { if (T.substr(i + 1 - k) == S.substr(0, k)) { nxt[i][j] = k; break; } } } } vector<vector<vector<int>>> dp( N + 1, vector<vector<int>>(M + 1, vector<int>(K, -inf))); vector<vector<vector<int>>> pre( N + 1, vector<vector<int>>(M + 1, vector<int>(K, -1))); vector<vector<vector<int>>> pre2( N + 1, vector<vector<int>>(M + 1, vector<int>(K, -1))); dp[0][0][0] = 0; for (int i = 0; i < N + 1; i++) { for (int j = 0; j < M + 1; j++) { for (int k = 0; k < K; k++) { if (i + 1 <= N) { if (chmax(dp[i + 1][j][k], dp[i][j][k])) pre[i + 1][j][k] = 1; } if (j + 1 <= M) { if (chmax(dp[i][j + 1][k], dp[i][j][k])) pre[i][j + 1][k] = 2; } if (i + 1 <= N && j + 1 <= M) { if (A[i] != B[j]) continue; int ni = i + 1, nj = j + 1, nk = nxt[k][A[i] - 'A']; if (nk < K) { if (chmax(dp[i + 1][j + 1][nk], dp[i][j][k] + 1)) { pre[i + 1][j + 1][nk] = 3; pre2[i + 1][j + 1][nk] = k; } } } } } } int pk = -1, ans = -1; for (int i = 0; i < K; i++) { if (chmax(ans, dp[N][M][i])) pk = i; } if (ans == 0) { cout << "0\n"; return 0; } string T; int pi = N, pj = M; while (pi > 0 || pj > 0) { int x = pre[pi][pj][pk]; if (x == 1) pi--; else if (x == 2) pj--; else { pk = pre2[pi][pj][pk], pi--, pj--; T += A[pi]; } } reverse(T.begin(), T.end()); cout << T << '\n'; }
6
#include <bits/stdc++.h> using namespace std; int main() { long long x, y; scanf("%lld", &x); scanf("%lld", &y); if (y == 0) { puts("No"); exit(0); } if (y == 1) { if (x == 0) puts("Yes"); else puts("No"); exit(0); } long long now = y - 1; if (x < now) { puts("No"); exit(0); } long long rek = x - now; if (rek & 1) puts("No"); else puts("Yes"); }
2
#include <bits/stdc++.h> using namespace std; int n, m; vector<int> e[100005]; int k, x; long long dp[100005][3][30]; const long long mod = 1e9 + 7; void dfs(int u, int par = 0) { dp[u][0][0] = dp[u][1][1] = dp[u][2][0] = 1; long long tmp[3][30]; for (int i = 0; i < e[u].size(); i++) { int v = e[u][i]; if (v == par) continue; memset(tmp, 0, sizeof(tmp)); dfs(v, u); for (int j = 0; j <= x; j++) for (int h = 0; h + j <= x; h++) { tmp[0][j + h] = (tmp[0][j + h] + (dp[v][0][h] + dp[v][1][h] + dp[v][2][h]) * dp[u][0][j] % mod) % mod; tmp[1][j + h] = (tmp[1][j + h] + dp[v][0][h] * dp[u][1][j] % mod) % mod; tmp[2][j + h] = (tmp[2][j + h] + (dp[v][0][h] + dp[v][2][h]) * dp[u][2][j] % mod) % mod; } swap(tmp, dp[u]); } for (int i = 0; i <= x; i++) { dp[u][0][i] = dp[u][0][i] * (k - 1) % mod; dp[u][2][i] = dp[u][2][i] * (m - k) % mod; } return; } int main() { memset(dp, 0, sizeof(dp)); ios_base::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i < n; i++) { int u, v; cin >> u >> v; e[u].push_back(v); e[v].push_back(u); } cin >> k >> x; dfs(1); long long res = 0; for (int i = 0; i <= x; i++) res = (res + dp[1][0][i] + dp[1][1][i] + dp[1][2][i]) % mod; cout << res; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0), cout.tie(0); string s; cin >> s; vector<bool> ans(s.size()); for (int i = 0; i < (int)s.size(); ++i) { int j = i; while (j < (int)s.size() && s[j] == s[i]) { ++j; } if (s[i] == 'b' && j == s.size()) ; else ans[j - 1] = true; i = j - 1; } string t = s; sort(t.begin(), t.end()); for (int i = 0; i < ans.size(); ++i) { if (ans[i]) { reverse(s.begin(), s.begin() + i + 1); } } assert(t == s); for (const bool &i : ans) { cout << i << ' '; } cout << '\n'; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int inf = int(1e9); const double eps = 1e-9; const double pi = 4 * atan(double(1)); const int N = 80; const int dx[4] = {-1, 0, 1, 0}; const int dy[4] = {0, -1, 0, 1}; struct edge { int go, cost, f, c; }; int edges; int a[N + 2][N + 2], num[N + 2][N + 2]; int first[N * N + 100]; int d[N * N + 100], pd[N * N + 100], pe[N * N + 100], pv[N * N + 100]; edge p[(N * N * 4 + N * N + N * N) * 2 + 100]; int _next[(N * N * 4 + N * N + N * N) * 2 + 100]; set<pair<int, int> > S; inline void add(int from, int to, int cap, int cost) { p[++edges].go = to; p[edges].c = cap; p[edges].f = 0; p[edges].cost = cost; _next[edges] = first[from]; first[from] = edges; p[++edges].go = from; p[edges].c = 0; p[edges].f = 0; p[edges].cost = -cost; _next[edges] = first[to]; first[to] = edges; } int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf("%d", &a[i][j]); } } int ptr1 = 0, ptr2 = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if ((i + j) % 2 == 0) { num[i][j] = ptr1++; } else { num[i][j] = ptr2++; } } } int s = ptr1 + ptr2, t = s + 1; edges = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if ((i + j) % 2 == 0) { add(s, num[i][j], 1, 0); } else { add(num[i][j] + ptr1, t, 1, 0); } } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if ((i + j) % 2 == 0) { for (int z = 0; z < 4; z++) { int ni = i + dx[z], nj = j + dy[z]; if (ni >= 0 && ni < n && nj >= 0 && nj < m) { add(num[i][j], num[ni][nj] + ptr1, 1, (a[i][j] != a[ni][nj])); } } } } } for (int i = 0; i < n * m / 2; i++) { for (int j = 0; j <= t; j++) { d[j] = inf; } d[s] = 0; S.insert(make_pair(d[s], s)); while (!S.empty()) { int cur = (S.begin())->second; S.erase(S.begin()); for (int j = first[cur]; j > 0; j = _next[j]) { if (p[j].c - p[j].f > 0 && d[p[j].go] > d[cur] + pd[cur] + p[j].cost - pd[p[j].go]) { S.erase(make_pair(d[p[j].go], p[j].go)); d[p[j].go] = d[cur] + pd[cur] + p[j].cost - pd[p[j].go]; pv[p[j].go] = cur; pe[p[j].go] = j; S.insert(make_pair(d[p[j].go], p[j].go)); } } } int j = t; while (j != s) { p[pe[j]].f++; p[(pe[j] % 2 == 0 ? pe[j] - 1 : pe[j] + 1)].f--; j = pv[j]; } for (int j = 0; j <= t; j++) { pd[j] = min(pd[j] + d[j], inf); } } int ans = 0; for (int i = 0; i <= t; i++) { for (int j = first[i]; j > 0; j = _next[j]) { if (p[j].f > 0) { ans += p[j].f * p[j].cost; } } } printf("%d\n", ans); return 0; }
7
#include <bits/stdc++.h> int main() { int n; std::cin >> n; for (int i = 0; i < n; ++i) { int tmp; std::cin >> tmp; if (tmp % 2 == 0) { std::cout << tmp - 1 << " "; } else { std::cout << tmp << " "; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const int negative_infinite = 0xcfcfcfcf; const int mod = 1e9 + 7; const int MAXN = 1007; int n, m; int a[MAXN]; int main() { cin >> n >> m; while (m--) { int x; cin >> x; a[x]++; } int ans = INF; for (int i = 1; i <= n; i++) ans = min(ans, a[i]); cout << ans; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t, n; cin >> t; string s; while (t--) { cin >> n; cin >> s; int size = n; bool found = false; for (int i = 0; i < size; i++) { if (s[i] == '8' && (size - i) >= 11) found = true; } if (found) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int add(int a, int b, int MOD) { if ((a += b) >= MOD) a -= MOD; return a; } int mul(int a, int b, int MOD) { return 1ll * a * b % MOD; } int qpow(int a, int b, int MOD) { int r = 1; for (; b; b >>= 1) { if (b & 1) r = mul(r, a, MOD); a = mul(a, a, MOD); } return r; } const int N = 1e5 + 7; int n, m, seed, vmax; int a[N]; int rnd() { int ret = seed; seed = (7ll * seed + 13) % 1000000007; return ret; } vector<pair<long long, int> > v; struct Nd { long long v, tval; char ttp; void chg(long long nv) { v = tval = nv; ttp = 'c'; } void add(long long mv) { if (~v) v += mv; if (ttp != '-') tval += mv; else ttp = 'a', tval = mv; } void upd(const Nd &ls, const Nd &rs) { ttp = '-'; v = ls.v == rs.v ? ls.v : -1; } }; struct Tree { static const int N = ::N << 2; Nd nd[N]; void upd(int o) { nd[o].upd(nd[((o) << 1)], nd[((o) << 1 | 1)]); } void dwn(int o) { if (nd[o].ttp == '-') return; if (nd[o].ttp == 'a') { nd[((o) << 1)].add(nd[o].tval); nd[((o) << 1 | 1)].add(nd[o].tval); } else { nd[((o) << 1)].chg(nd[o].tval); nd[((o) << 1 | 1)].chg(nd[o].tval); } nd[o].ttp = '-'; } void bud(int o, int l, int r) { if (l == r) { nd[o].v = a[l]; nd[o].ttp = '-'; return; } int mid = l + r >> 1; bud(((o) << 1), l, mid); bud(((o) << 1 | 1), mid + 1, r); upd(o); } void add(int o, int l, int r, int ql, int qr, long long qv) { if (ql <= l && qr >= r) { nd[o].add(qv); return; } dwn(o); int mid = l + r >> 1; if (ql <= mid) add(((o) << 1), l, mid, ql, qr, qv); if (qr > mid) add(((o) << 1 | 1), mid + 1, r, ql, qr, qv); upd(o); } void chg(int o, int l, int r, int ql, int qr, long long qv) { if (ql <= l && qr >= r) { nd[o].chg(qv); return; } dwn(o); int mid = l + r >> 1; if (ql <= mid) chg(((o) << 1), l, mid, ql, qr, qv); if (qr > mid) chg(((o) << 1 | 1), mid + 1, r, ql, qr, qv); upd(o); } void qry(int o, int l, int r, int ql, int qr) { if (ql <= l && qr >= r && ~nd[o].v) { v.push_back({nd[o].v, r - l + 1}); return; } dwn(o); int mid = l + r >> 1; if (ql <= mid) qry(((o) << 1), l, mid, ql, qr); if (qr > mid) qry(((o) << 1 | 1), mid + 1, r, ql, qr); upd(o); } } tree; void solve() { for (int i = (1); i < (n + 1); ++i) a[i] = rnd() % vmax + 1; tree.bud(1, 1, n); for (int i = (1); i < (m + 1); ++i) { int op = (rnd() % 4) + 1; int l = (rnd() % n) + 1; int r = (rnd() % n) + 1; if (l > r) swap(l, r); int x = op == 3 ? (rnd() % (r - l + 1)) + 1 : (rnd() % vmax) + 1; int y; if (op == 4) y = (rnd() % vmax) + 1; if (op == 1) { tree.add(1, 1, n, l, r, x); } else if (op == 2) { tree.chg(1, 1, n, l, r, x); } else if (op == 3) { v.clear(); tree.qry(1, 1, n, l, r); sort((v).begin(), (v).end()); for (int i = (0); i < (((int)(v).size())); ++i) { if (x <= v[i].second) { printf("%lld\n", v[i].first); break; } else x -= v[i].second; } } else { v.clear(); tree.qry(1, 1, n, l, r); int ans = 0; for (int l = 0, r; l < ((int)(v).size()); l = r) { int cnt = 0; r = l; while (r < ((int)(v).size()) && v[r].first == v[l].first) cnt += v[r++].second; ans = add(ans, mul(qpow(v[l].first % y, x, y), cnt, y), y); } printf("%d\n", ans); } } } int main() { while (~scanf("%d%d%d%d", &n, &m, &seed, &vmax)) solve(); return 0; }
9
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 10; int n, t, k; int a[MAXN]; int calmin[MAXN], calmax[MAXN]; int main(int argc, const char* argv[]) { cin >> n >> t >> k; for (int i = 1; i <= t; i++) { cin >> a[i]; } int cmin = 0, cmax = 0; for (int i = 1; i <= t - 1; i++) { calmin[i] = max(0, a[i] - a[i + 1]); calmax[i] = a[i] - 1; cmin += calmin[i]; cmax += calmax[i]; } k -= a[t]; if (k < cmin || k > cmax) { cout << "-1" << endl; return 0; } int temp = k - cmin; int l = 2, r = 2 + a[1]; cout << n << endl; for (int i = 1; i <= a[1]; i++) { cout << "1 " << 1 + i << endl; } for (int i = 1; i <= t - 1; i++) { if (temp > 0) { int x = min(temp, calmax[i] - calmin[i]); for (int j = 1; j <= a[i] - calmin[i] - x; j++) { cout << l + j - 1 << " " << r + j - 1 << endl; } for (int j = a[i] - calmin[i] - x + 1; j <= a[i + 1]; j++) { cout << l << " " << r + j - 1 << endl; } temp -= x; } else { for (int j = 1; j <= a[i] - calmin[i]; j++) { cout << l + j - 1 << " " << r + j - 1 << endl; } for (int j = a[i] - calmin[i] + 1; j <= a[i + 1]; j++) { cout << l << " " << r + j - 1 << endl; } } l = r; r += a[i + 1]; } return 0; }
6
#include <bits/stdc++.h> using namespace std; const long long mxlli = 1e18; const int mxint = 1e9 + 7; int solv() { int n, m, k; cin >> n >> m >> k; int z = ((n * m) / 2) - k; char c[n][m]; int i, j; for (i = 0; i < n; i++) for (j = 0; j < m; j++) c[i][j] = '*'; if ((n % 2 == 1 and m % 2 == 1)) { cout << "NO\n"; return 0; } char ch, chh; if (m % 2) { if (k % 2) { cout << "NO\n"; return 0; } if (k <= ((m - 1) * n) / 2) { cout << "YES\n"; for (j = 0; j < m; j++) { for (i = 0; i < n; i++) { if (k && c[i][j] == '*') { if (i % 2 == (j / 2) % 2) chh = 'a'; else chh = 'b'; c[i][j] = chh; c[i][j + 1] = chh; k--; } else if (k == 0 && c[i][j] == '*') { if (j % 2 == (i / 2) % 2) chh = 'c'; else chh = 'd'; c[i][j] = chh; c[i + 1][j] = chh; } } } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) cout << c[i][j]; cout << "\n"; } } else cout << "NO\n"; return 0; } if (n % 2) { if (z % 2) { cout << "NO\n"; return 0; } if (z <= ((n - 1) * m) / 2) { cout << "YES\n"; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (z == 0 && c[i][j] == '*') { if (i % 2 == (j / 2) % 2) chh = 'a'; else chh = 'b'; c[i][j] = chh; c[i][j + 1] = chh; } else if (z && c[i][j] == '*') { if (j % 2 == (i / 2) % 2) chh = 'c'; else chh = 'd'; c[i][j] = chh; c[i + 1][j] = chh; z--; } } } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) cout << c[i][j]; cout << "\n"; } } else cout << "NO\n"; return 0; } if (k % 2) { cout << "NO\n"; return 0; } cout << "YES\n"; for (j = 0; j < m; j++) { for (i = 0; i < n; i++) { if (k && c[i][j] == '*') { if (i % 2 == (j / 2) % 2) chh = 'a'; else chh = 'b'; c[i][j] = chh; c[i][j + 1] = chh; k--; } else if (k == 0 && c[i][j] == '*') { if (j % 2 == (i / 2) % 2) chh = 'c'; else chh = 'd'; c[i][j] = chh; c[i + 1][j] = chh; } } } for (i = 0; i < n; i++) { for (j = 0; j < m; j++) cout << c[i][j]; cout << "\n"; } return 0; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int testcases = 1; cin >> testcases; while (testcases--) { solv(); } }
6
#include <bits/stdc++.h> using namespace std; int N, arr[100010]; void Debug() {} void Read() { cin >> N; for (int i = 1; i <= N; ++i) cin >> arr[i]; sort(arr + 1, arr + N + 1); } void Solve() { for (int i = 1; i <= N; ++i) if (arr[i] > arr[i - 1]) arr[i] = arr[i - 1] + 1; cout << arr[N] + 1 << "\n"; } int main() { Read(); Solve(); Debug(); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<int> l(m), ans(m), R(m); bool can = true; for (int i = 0; i < m; ++i) { cin >> l[i]; if (i + l[i] > n) { can = false; break; } } int last = n; for (int i = m - 1; i >= 0; --i) { R[i] = min(last - 1, n - l[i]); last = R[i]; if (last < 0) { can = false; break; } } int empty = 0; for (int i = 0; i < m; ++i) { ans[i] = min(empty, R[i]); empty = ans[i] + l[i]; } if (empty < n) can = false; if (!can) cout << "-1"; else { for (const int &ai : ans) cout << ai + 1 << " "; } cout << "\n"; return 0; }
5
#include <bits/stdc++.h> using namespace std; int num[2010]; long long count_winscore[5010], A_score[10010]; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &num[i]); sort(num, num + n); for (int i = 0; i < n; i++) for (int j = 0; j < i; j++) count_winscore[num[i] - num[j]]++; for (int i = 1; i <= 5000; i++) for (int j = 1; j <= 5000; j++) A_score[i + j] += count_winscore[i] * count_winscore[j]; for (int j = 1; j <= 10000; j++) A_score[j] += A_score[j - 1]; long long ans = 0; for (int i = 1; i <= 5000; i++) ans += count_winscore[i] * A_score[i - 1]; long long k = n * (n - 1) / 2; double possibility = (double)ans / k / k / k; printf("%.10f\n", possibility); return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, h, o, a; int main() { scanf("%d%d", &n, &h); o = 0; while (n--) { scanf("%d", &a); o += a > h ? 2 : 1; } printf("%d\n", o); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n][n]; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> a[i][j]; } } int sum = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { if (i == j) { sum = sum + a[i][j]; } } } for (int i = 0; i < n; ++i) { sum = sum + a[i][n - 1 - i]; } for (int i = 0; i < n; ++i) { sum = sum + a[(n - 1) / 2][i]; sum = sum + a[i][(n - 1) / 2]; } cout << sum - 3 * a[(n - 1) / 2][(n - 1) / 2]; }
0
#include <bits/stdc++.h> using namespace std; void solve() { int n, k; cin >> n >> k; string s; cin >> s; string t(n, '?'); for (int i = 0; i < k - 1; ++i) { t[2 * i] = '('; t[2 * i + 1] = ')'; } for (int i = 0; i <= n / 2 - k; ++i) { t[2 * k - 2 + i] = '('; t[n - 1 - i] = ')'; } vector<pair<int, int>> A; for (int i = 0; i < n; ++i) { if (s[i] == t[i]) continue; int l = i, r = -1; for (int j = i + 1; j < n; ++j) { if (s[j] == t[i]) { r = j; break; } } assert(r >= 0); reverse(s.begin() + l, s.begin() + r + 1); A.emplace_back(l + 1, r + 1); } cout << A.size() << endl; for (auto p : A) cout << p.first << " " << p.second << endl; } int main() { cin.tie(0); ios::sync_with_stdio(false); int t; cin >> t; while (t--) { solve(); } }
4
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; const int MAXN = 1e3 + 7; const int MOD = 1e9 + 7; int n; int dp[34][2][2][2]; int sum[34][2][2][2]; int x, y, k; int dfs(int pos, int limx, int limy, int limk, int &t) { if (pos == -1) return 1; int &st = sum[pos][limx][limy][limk]; if (~dp[pos][limx][limy][limk]) { (t += st) %= MOD; return dp[pos][limx][limy][limk]; } st = 0; int zx = 1, zy = 1; if (limx) zx = x >> pos & 1; if (limy) zy = y >> pos & 1; long long res = 0, posk = k >> pos & 1; long long ta; for (int i = 0; i <= zx; i++) for (int j = 0; j <= zy; j++) { long long tb = ((i ^ j) << pos) % MOD; if (limk) { if ((i ^ j) <= posk) { ta = dfs(pos - 1, limx && zx == i, limy && zy == j, (i ^ j) == posk, st); res += ta; st = (ta * tb % MOD + st) % MOD; } } else { ta = dfs(pos - 1, limx && zx == i, limy && zy == j, 0, st); res += ta; st = (ta * tb % MOD + st) % MOD; } } res %= MOD; (t += st) %= MOD; dp[pos][limx][limy][limk] = res; return res; } int solve(int a, int b) { if (a < 0 || b < 0) return 0; memset(dp, -1, sizeof(dp)); int ans = 0; x = a, y = b; long long tmp = dfs(30, 1, 1, 1, ans); tmp = (tmp + ans) % MOD; return tmp; } int main() { int q, x1, y1, x2, y2; scanf("%d", &q); while (q--) { scanf("%d%d%d%d%d", &x1, &y1, &x2, &y2, &k); x1--, y1--, x2--, y2--, k--; long long ans = solve(x2, y2) - solve(x1 - 1, y2) - solve(x2, y1 - 1) + solve(x1 - 1, y1 - 1); ans = ans % MOD; if (ans < 0) ans += MOD; printf("%d\n", ans); } return 0; }
9
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; inline int add(int a, int b) { if ((a += b) >= mod) a -= mod; return a; } inline int mult(int a, int b) { long long t = 1ll * a * b; if (t >= mod) t %= mod; return t; } namespace matrix { template <int N, int M> class matr { public: int dat[N][M]; void init() { memset(dat, 0, sizeof(dat)); } void iden() { init(); for (int i = 0; i < min(N, M); i++) dat[i][i] = 1; } matr<M, N> trans() { matr<M, N> out; for (int i = 0; i < M; i++) for (int j = 0; j < N; j++) out[i][j] = dat[j][i]; } const int *operator[](const int &x) const { return dat[x]; } int *operator[](const int &x) { return dat[x]; } matr<N, M> operator+(const matr<N, M> a) { matr<N, M> out; for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) out[i][j] = add(dat[i][j], a[i][j]); return out; } matr<N, M> operator*(const int &x) { matr<N, M> out = *this; for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) out[i][j] = mult(out[i][j], x); return out; } template <int X> friend matr<N, M> operator*(const matr<N, X> a, const matr<X, M> b) { matr<N, M> out; out.init(); for (int i = 0; i < N; i++) { for (int j = 0; j < X; j++) { if (a[i][j]) { for (int k = 0; k < M; k++) out[i][k] = add(out[i][k], mult(a[i][j], b[j][k])); } } } return out; } }; }; // namespace matrix using namespace matrix; int n, K, m; matr<13 * 16, 13 * 16> trans; inline int getid(int i, int j) { return (i << m) | j; } int main() { scanf("%d%d%d", &n, &K, &m); for (int i = 0; i <= K; i++) { for (int j = 0; j < (1 << m); j++) { int S1 = getid(i, j); int curs = (j >> 1); int S2 = getid(i, curs); trans[S1][S2] = add(trans[S1][S2], 1); if (i + 1 <= K) { curs = (j >> 1) | (1 << (m - 1)); S2 = getid(i + 1, curs); trans[S1][S2] = add(trans[S1][S2], __builtin_popcount(j) + 1); } } } matr<1, 13 * 16> ans; ans.init(); ans[0][getid(0, 0)] = 1; while (n) { if (n & 1) ans = ans * trans; trans = trans * trans; n >>= 1; } int ret = 0; for (int i = 0; i < (1 << m); i++) ret = add(ret, ans[0][getid(K, i)]); printf("%d\n", ret); return 0; }
10
#include <bits/stdc++.h> using namespace std; const int dx[] = {1, -1, 0, 0}; const int dy[] = {0, 0, 1, -1}; const int MAXN = 24; const int MAXA = 514; const int INF = 1029384756; class Isap { public: static const int MXN = MAXA * 4; class Edge { public: int v, f, re; Edge() { v = f = re = -1; } Edge(int _v, int _f, int _r) { v = _v; f = _f; re = _r; } }; int n, s, t, h[MXN], gap[MXN]; vector<Edge> E[MXN]; void init(int _n, int _s, int _t) { n = _n; s = _s; t = _t; for (int i = 0; i < n; i++) E[i].clear(); } void add_edge(int u, int v, int f) { E[u].push_back(Edge(v, f, E[v].size())); E[v].push_back(Edge(u, 0, E[u].size() - 1)); } int DFS(int u, int nf, int res = 0) { if (u == t) return nf; for (auto &it : E[u]) { if (h[u] == h[it.v] + 1 && it.f > 0) { int tf = DFS(it.v, min(nf, it.f)); res += tf; nf -= tf; it.f -= tf; E[it.v][it.re].f += tf; if (nf == 0) return res; } } if (nf) { if (--gap[h[u]] == 0) h[s] = n; gap[++h[u]]++; } return res; } int flow(int res = 0) { memset((h), 0, sizeof(h)); memset((gap), 0, sizeof(gap)); gap[0] = n; while (h[s] < n) res += DFS(s, 2147483647); return res; } } flow; int N, M, L; bool arr[MAXN][MAXN]; int r[MAXA * 2], c[MAXA * 2]; long long t[MAXA * 2]; int no[MAXN][MAXN]; int dis[MAXA][MAXA]; vector<int> edge[MAXA]; void dijk() { for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { if (!arr[i][j]) continue; for (int k = 0; k < 4; k++) { int nx = i + dx[k], ny = j + dy[k]; if (nx >= 0 && nx < N && ny >= 0 && ny < M && arr[nx][ny]) edge[no[i][j]].push_back(no[nx][ny]); } } } for (int i = 0; i < N * M; i++) for (int j = 0; j < N * M; j++) dis[i][j] = INF; for (int i = 0; i < N * M; i++) dis[i][i] = 0; for (int i = 0; i < N * M; i++) { queue<int> q; q.push(i); while (!q.empty()) { int v = q.front(); q.pop(); for (auto u : edge[v]) { if (dis[i][u] == INF) { dis[i][u] = dis[i][v] + 1; q.push(u); } } } } } bool test(long long m) { int n = 2 * L + 2 * N * M + 2; int ss = n - 2, tt = n - 1; flow.init(n, ss, tt); for (int i = 0; i < 2 * L; i++) { int x = r[i], y = c[i]; int v = no[x][y]; if (i < L) flow.add_edge(ss, i, 1); else flow.add_edge(i, tt, 1); for (int u = 0; u < N * M; u++) { if (dis[v][u] == INF || t[i] * dis[v][u] > m) continue; if (i < L) flow.add_edge(i, 2 * L + u, 1); else flow.add_edge(2 * L + N * M + u, i, 1); } } for (int i = 0; i < N * M; i++) flow.add_edge(2 * L + i, 2 * L + N * M + i, 1); int res = flow.flow(); return res == L; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int male, female; cin >> N >> M >> male >> female; for (int i = 0; i < N; i++) { string s; cin >> s; for (int j = 0; j < M; j++) arr[i][j] = (s[j] == '.'); } L = max(male, female); if (male == female - 1) cin >> r[L - 1] >> c[L - 1] >> t[L - 1]; else if (male == female + 1) cin >> r[2 * L - 1] >> c[2 * L - 1] >> t[2 * L - 1]; else { cout << -1 << endl; return 0; } for (int i = 0; i < male; i++) cin >> r[i] >> c[i] >> t[i]; for (int i = 0; i < female; i++) cin >> r[L + i] >> c[L + i] >> t[L + i]; for (int i = 0; i < 2 * L; i++) { r[i]--; c[i]--; } for (int i = 0; i < N; i++) for (int j = 0; j < M; j++) no[i][j] = i * M + j; dijk(); long long mx = 1E12; long long lb = 0, rb = mx; while (lb < rb) { long long mb = (lb + rb) / 2; if (test(mb)) rb = mb; else lb = mb + 1; } long long ans = lb; if (ans == mx) ans = -1; cout << ans << endl; return 0; }
9
#include <bits/stdc++.h> using namespace std; const int maxn = 262144; const long long inf = 4e18; struct lazyseg { vector<long long> a; lazyseg() : a(2 * maxn, inf) {} void update(int l, int r, long long v, int x = 1, int xl = 0, int xr = maxn - 1) { if (r < xl || xr < l) return; if (l <= xl && xr <= r) { a[x] = min(a[x], v); return; } int xm = (xl + xr) >> 1; update(l, r, v, 2 * x, xl, xm); update(l, r, v, 2 * x + 1, xm + 1, xr); } long long query(int p) { long long z = inf; p += maxn; while (p > 0) { z = min(z, a[p]); p >>= 1; } return z; } }; int n, k; string s; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); cin >> n >> k >> s; s = string(" ") + s; lazyseg dp; dp.update(0, 0, 0); for (int i = 1; i <= n; i++) { int l = i, r = i; if (s[i] == '1') l = max(1, i - k), r = min(n, i + k); long long v = dp.query(l - 1); dp.update(l, r, v + i); } cout << dp.query(n) << '\n'; }
6
#include <bits/stdc++.h> using namespace std; int main() { int t = 0; cin >> t; for (int i = 0; i < t; i++) { int n = 0; cin >> n; char s[n]; cin >> s; int count1 = 0; int count2 = 0; int ans = 0; for (int j = 0; j < n; j++) { if (s[j] == '(') count1++; else if (s[j] == ')') count2++; if (count2 > count1) { ans++; count2--; } } cout << ans << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; set<int> gSetka; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; set<int> setka; setka.insert(0); for (int i = 0; i < n; ++i) { int cur; cin >> cur; set<int> newSetka; gSetka.insert(cur); newSetka.insert(cur); for (auto it = setka.begin(); it != setka.end(); ++it) { newSetka.insert(*it | cur); gSetka.insert(*it | cur); } swap(setka, newSetka); } cout << gSetka.size(); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; template <typename T> void amin(T &x, T y) { if (y < x) x = y; } template <typename T> void amax(T &x, T y) { if (y > x) x = y; } vector<int> g[maxn]; vector<pair<int, int>> adj[maxn]; bool vis[maxn]; long long int dis[maxn]; int par[maxn]; int col[maxn]; string dir = "URDL"; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; void solve() { long long int y, k, n; cin >> y >> k >> n; long long int x; vector<long long int> pos; if (y <= k) x = k - y; else { x = k - y % k; } int f = 0; while (x + y <= n) { if ((x + y) % k == 0) { if (x != 0) cout << x << " "; x += k; f = 1; } else x++; } if (!f) cout << -1 << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int tc = 1; while (tc--) solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<pair<int, int> > arr; for (int i = 1; i <= n; i++) { int elem; cin >> elem; if (elem == 0) continue; arr.push_back(pair<int, int>(elem, i)); } if (int(arr.size()) <= 1) { cout << -1 << '\n'; return (0); } vector<pair<int, int> > ans; while (int(arr.size()) > 2) { pair<int, int> x = arr.back(); arr.pop_back(); pair<int, int> y = arr.back(); arr.pop_back(); pair<int, int> z = arr.back(); arr.pop_back(); vector<pair<int, int> > cur = {x, y, z}; sort(begin(cur), end(cur)); while (true) { if (cur[0].first == 0) break; if (cur[0].first == cur[1].first) { ans.push_back(pair<int, int>(cur[0].second, cur[1].second)); cur[0].first *= 2; cur[1].first = 0; sort(begin(cur), end(cur)); break; } if (cur[1].first == cur[2].first) { ans.push_back(pair<int, int>(cur[1].second, cur[2].second)); cur[1].first *= 2; cur[2].first = 0; sort(begin(cur), end(cur)); break; } int k = cur[1].first / cur[0].first; for (int bit = 0; (1 << bit) <= k; bit++) { if ((k >> bit) & 1) { cur[1].first -= cur[0].first; cur[0].first *= 2; ans.push_back(pair<int, int>(cur[0].second, cur[1].second)); } else { cur[2].first -= cur[0].first; cur[0].first *= 2; ans.push_back(pair<int, int>(cur[0].second, cur[2].second)); } } sort(begin(cur), end(cur)); } arr.push_back(cur[1]); arr.push_back(cur[2]); } cout << int(ans.size()) << '\n'; for (auto pp : ans) cout << pp.first << ' ' << pp.second << '\n'; return (0); }
11
#include <bits/stdc++.h> using namespace std; long long int roundup(long long int x, long long int a) { return (x < 0 ? x - x % a : x + 4 - x % a); } long long int rounddown(long long int x, long long int a) { return (x < 0 ? x - 4 - x % a : x - x % a); } int main() { long long int a, b, x1, y1, x2, y2, yy1, yy2, res1 = 0, res2 = 0; cin >> a >> b >> x1 >> y1 >> x2 >> y2; a *= 2; b *= 2; yy1 = y1 + x1; yy2 = y2 + x2; if (yy1 > yy2) swap(yy1, yy2); yy1 = roundup(yy1, a); yy2 = rounddown(yy2, a); if (yy1 <= yy2) res1 = (yy2 - yy1) / a + 1; yy1 = y1 - x1; yy2 = y2 - x2; if (yy1 > yy2) swap(yy1, yy2); yy1 = roundup(yy1, b); yy2 = rounddown(yy2, b); if (yy1 <= yy2) res2 = (yy2 - yy1) / b + 1; cout << max(res1, res2) << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; bool sub(char s1[], char s2[], int m, int n) { if (m == 0) return true; if (n == 0) return false; if (s1[m - 1] == s2[n - 1]) return sub(s1, s2, m - 1, n - 1); return sub(s1, s2, m, n - 1); } int main() { char s1[] = "heidi"; char s2[1000]; cin >> s2; int m = strlen(s1); int n = strlen(s2); sub(s1, s2, m, n) ? cout << "YES" : cout << "NO"; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 100 + 5; int num[maxn]; int pos[maxn]; int vis[maxn]; int main() { memset(vis, -1, sizeof(vis)); int n, x; scanf("%d%d", &n, &x); for (int i = 0; i < n; i++) { scanf("%d", &num[i]); vis[num[i]] = 1; } sort(num, num + n); pos[0] = num[0] - 0; for (int i = 1; i < n; i++) { pos[i] = num[i] - num[i - 1] - 1 + pos[i - 1]; } int p = 0; for (p = 0; p < n; p++) { if (num[p] >= x) break; } int cnt = 0; if (p == n) cnt = pos[n - 1] + x - num[n - 1] - 1; else { cnt = cnt + pos[p]; int t = num[p] - x; if (x == num[p]) t--; cnt = cnt - t; } printf("%d\n", cnt); return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, p; long long a[100005]; long long ans[10005]; long long sum1; int main() { scanf("%d %d", &n, &p); scanf("%lld", &a[0]); sum1 = a[0]; ans[0] = a[0]; for (int i = 1; i < n; i++) { scanf("%lld", &a[i]); sum1 += a[i]; ans[i] = ans[i - 1] + a[i]; } long long sum = 0; for (int i = 0; i < n; i++) { if ((ans[i] % p) + (sum1 - ans[i]) % p > sum) sum = (ans[i] % p) + (sum1 - ans[i]) % p; } printf("%lld\n", sum); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long int n; scanf("%lld", &n); long long int ar[n]; for (long long int i = 0; i < n; i++) scanf("%lld", &ar[i]); for (long long int i = 0; i < n; i++) { for (long long int j = 0; j < n - 1; j++) { if (ar[j] > ar[j + 1]) { cout << j + 1 << " " << j + 2 << endl; swap(ar[j], ar[j + 1]); } } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { char s[1000]; int i, j = 0, k, l, a[1000], m, n, t; cin >> s; l = strlen(s); for (i = 0; i < l; i++) { if ((i % 2) == 0) { a[j] = (int)s[i]; j++; } } for (m = 1; m < j; m++) { for (n = 0; n < (j - m); n++) { if (a[n] > a[n + 1]) { t = a[n]; a[n] = a[n + 1]; a[n + 1] = t; } } } for (k = 0; k < j; k++) { if (k == (j - 1)) { cout << (char)a[k]; } else { cout << (char)a[k] << '+'; } } cout << "\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int k, i, cnt = 0, a, sum; cin >> k; for (i = 19; cnt <= k; i++) { sum = 0; for (a = i; a > 0; a /= 10) { sum += (a % 10); } if (sum == 10) { cnt++; } if (cnt == k) { break; } } cout << i; return 0; }
1
#include <bits/stdc++.h> template <class t> inline void read(t &s) { s = 0; register int f = 1; register char c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) s = (s << 3) + (s << 1) + (c ^ 48), c = getchar(); s *= f; return; } template <class t, class... A> inline void read(t &x, A &...a) { read(x); read(a...); } template <class t> inline void write(t x) { if (x < 0) putchar('-'), x = -x; int buf[21], top = 0; while (x) buf[++top] = x % 10, x /= 10; if (!top) buf[++top] = 0; while (top) putchar(buf[top--] ^ '0'); return; } inline void setIn(std::string s) { freopen(s.c_str(), "r", stdin); return; } inline void setOut(std::string s) { freopen(s.c_str(), "w", stdout); return; } inline void setIO(std::string s = "") { setIn(s + ".in"); setOut(s + ".out"); return; } template <class t> inline bool ckmin(t &x, t y) { if (x > y) { x = y; return 1; } return 0; } template <class t> inline bool ckmax(t &x, t y) { if (x < y) { x = y; return 1; } return 0; } inline int lowbit(int x) { return x & (-x); } const int MaxN = 1e5 + 50; char str[MaxN]; int f[MaxN][1 << 6], T[6], vis[MaxN]; inline bool check(int st) { for (int mask = 0; mask < (1 << 6); ++mask) { register int s = 0; for (int j = 0; j < 6; ++j) if (mask & (1 << j)) s += T[j]; if (f[st][mask] < s) return false; } return true; } signed main(void) { std::scanf("%s", str); int n = std::strlen(str); for (int i = 0; i < n; ++i) ++T[str[i] - 'a']; int q; read(q); while (q--) { int x; std::string y; read(x); std::cin >> y; int msk = 0; for (auto i : y) msk |= (1 << (i - 'a')); vis[x] = msk; } for (int i = 1; i <= n; ++i) if (!vis[i]) vis[i] = (1 << 6) - 1; for (int i = n; i; --i) for (int j = 0; j < (1 << 6); ++j) f[i][j] = f[i + 1][j] + !!(j & vis[i]); if (!check(1)) return std::puts("Impossible"), 0; for (int i = 1; i <= n; ++i) for (int j = 0; j < 6; ++j) if (T[j] && (vis[i] & (1 << j))) { --T[j], str[i] = j + 'a'; if (check(i + 1)) break; ++T[j]; } std::puts(str + 1); return 0; }
8
#include <bits/stdc++.h> const int MAX = 1e5 + 11; using namespace std; int N, L, X, Y, A[MAX]; set<int> all; static bool can(int v) { for (int i = 0; i < (N); ++i) { if (all.count(v + A[i])) { return true; } } return all.count(v) > 0; } int main() { scanf("%d%d%d%d", &(N), &(L), &(X), &(Y)); for (int i = 0; i < (N); ++i) { scanf("%d", &(A[i])); all.insert(A[i]); } if (can(X) && can(Y)) { printf("0"); return 0; } if (can(X)) { printf("1\n%d", Y); return 0; } if (can(Y)) { printf("1\n%d", X); return 0; } set<int> xx, yy; for (int i = 0; i < (N); ++i) { if (X + A[i] < L) xx.insert(X + A[i]); if (Y + A[i] < L) yy.insert(Y + A[i]); } for (auto u : xx) { if (yy.count(u)) { printf("1\n%d", u); return 0; } } xx = set<int>(), yy = set<int>(); for (int i = 0; i < (N); ++i) { if (A[i] - X >= 0) xx.insert(A[i] - X); if (A[i] - Y >= 0) yy.insert(A[i] - Y); } for (auto u : xx) { if (yy.count(u)) { printf("1\n%d", u); return 0; } } xx = set<int>(), yy = set<int>(); for (int i = 0; i < (N); ++i) { if (A[i] - X >= 0) xx.insert(A[i] - X); if (A[i] + Y < L) yy.insert(A[i] + Y); } for (auto u : xx) { if (yy.count(u)) { printf("1\n%d", u); return 0; } } xx = set<int>(), yy = set<int>(); for (int i = 0; i < (N); ++i) { if (A[i] + X < L) xx.insert(A[i] + X); if (A[i] - Y >= 0) yy.insert(A[i] - Y); } for (auto u : xx) { if (yy.count(u)) { printf("1\n%d", u); return 0; } } A[N++] = X; if (can(Y)) { printf("1\n%d", X); return 0; } A[N - 1] = Y; if (can(X)) { printf("1%d\n", Y); return 0; } printf("2\n%d %d", X, Y); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int inf = ~0U >> 1; const long long INF = ~0ULL >> 1; template <class T> inline void read(T &n) { char c; int flag = 1; for (c = getchar(); !(c >= '0' && c <= '9' || c == '-'); c = getchar()) ; if (c == '-') flag = -1, n = 0; else n = c - '0'; for (c = getchar(); c >= '0' && c <= '9'; c = getchar()) n = n * 10 + c - '0'; n *= flag; } const int maxn = 120000; int N, M, P, Q, x, y, Index; int dfn[maxn], size[maxn], id[maxn], p[maxn], a[maxn]; vector<int> E[maxn]; struct node { int lazy; bitset<1000> s; } t[maxn * 4]; bitset<1000> top[1000]; int isPrime(int x) { for (int i = (2); i <= (x - 1); ++i) if (x % i == 0) return false; return true; } void dfs(int u, int fa) { dfn[u] = ++Index; id[Index] = u; size[u] = 1; for (auto v : E[u]) if (v != fa) dfs(v, u), size[u] += size[v]; } void build(int p, int l, int r) { if (l != r) { build(p << 1, l, l + r >> 1); build(p << 1 | 1, (l + r >> 1) + 1, r); t[p].s = t[p << 1].s | t[p << 1 | 1].s; } else t[p].s[a[id[l]]] = 1; } void add(int p, int x) { x %= M; t[p].s = ((t[p].s & top[x]) >> (M - x)) | (t[p].s << x); t[p].lazy = (t[p].lazy + x) % M; } void push(int p) { if (t[p].lazy) { add(p << 1, t[p].lazy); add(p << 1 | 1, t[p].lazy); t[p].lazy = 0; } } void add(int p, int L, int R, int l, int r, int x) { if (L == l && R == r) { add(p, x); return; } push(p); int m = L + R >> 1; if (r <= m) add(p << 1, L, m, l, r, x); else if (l > m) add(p << 1 | 1, m + 1, R, l, r, x); else add(p << 1, L, m, l, m, x), add(p << 1 | 1, m + 1, R, m + 1, r, x); t[p].s = t[p << 1].s | t[p << 1 | 1].s; } bitset<1000> query(int p, int L, int R, int l, int r) { if (L == l && R == r) return t[p].s; push(p); int m = L + R >> 1; if (r <= m) return query(p << 1, L, m, l, r); else if (l > m) return query(p << 1 | 1, m + 1, R, l, r); else return query(p << 1, L, m, l, m) | query(p << 1 | 1, m + 1, R, m + 1, r); } int main(int argc, char *argv[]) { scanf("%d%d", &N, &M); for (int i = (2); i <= (M - 1); ++i) if (isPrime(i)) p[++P] = i; for (int i = (1); i <= (M - 1); ++i) top[i] = top[i - 1], top[i][M - i] = 1; for (int i = (1); i <= (N); ++i) scanf("%d", &a[i]), a[i] %= M; for (int i = (1); i <= (N - 1); ++i) scanf("%d%d", &x, &y), E[x].push_back(y), E[y].push_back(x); dfs(1, 0); build(1, 1, N); scanf("%d", &Q); while (Q--) { int ty, v, x; scanf("%d", &ty); if (ty == 1) { scanf("%d%d", &v, &x); x %= M; add(1, 1, N, dfn[v], dfn[v] + size[v] - 1, x); } else { int ans = 0; scanf("%d", &v); bitset<1000> t = query(1, 1, N, dfn[v], dfn[v] + size[v] - 1); for (int i = (1); i <= (P); ++i) if (t[p[i]]) ++ans; printf("%d\n", ans); } } return 0; }
10