solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int n; long double w, u, v; vector<pair<long double, long double> > cood; bool nostop() { bool r1 = 1, r2 = 1; for (auto it : cood) { long double t1 = it.second / u; long double t2 = it.first / v; if (t1 > t2 || t1 < 0) return 0; } return 1; } int lowest() { int ret; long double low = 1e10; for (int i = (0); i < (cood.size()); i++) { if (cood[i].second < low) { low = cood[i].second; ret = i; } } return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> w >> v >> u; for (int i = (0); i < (n); i++) { long double x, y; cin >> x >> y; cood.push_back({x, y}); } if (nostop()) { cout.precision(10); cout << w / u << endl; } else { int k = lowest(); long double ans = 0; long double cury = 0; long double dex = 0; do { long double t1 = (cood[k].second - cury) / u; long double t2 = (cood[k].first - dex) / v; t1 = max(t1, 0.0L); t2 = max(t2, 0.0L); ans += max(t1, t2); cury = cood[k].second; dex += max(t1, t2) * v; k++; k %= n; } while (cood[k].first > cood[(k - 1 + n) % n].first); ans += (w - cood[(k - 1 + n) % n].second) / u; cout.precision(10); cout << ans << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, h, q; scanf("%d %d %d", &n, &h, &q); vector<int> v(n + 1, h); for (int i = 0; i < q; i++) { int l, r, x; scanf("%d %d %d", &l, &r, &x); for (int j = l; j <= r; j++) v[j] = min(v[j], x); } long long ans = 0; for (auto x : v) ans += x * x * 1LL; ans -= v[0] * v[0]; printf("%lld\n", ans); }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t; cin >> t; while (t--) { long long int n; cin >> n; long long int a[n + 1]; for (long long int i = 1; i <= n; i++) { cin >> a[i]; } long long int ans = 0; vector<long long int> freq((n * n) + n + n, 0); for (long long int k = 2; k < n; k++) { long long int j = k - 1; for (long long int i = 1; i <= k - 2; i++) { freq[(n * (a[i] - 1)) + a[j]]++; } for (long long int l = k + 1; l <= n; l++) { ans += freq[(n * (a[k] - 1)) + a[l]]; } } cout << ans << "\n"; } }
4
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<vector<unsigned long long> > arr(n); for (int i = 0; i < n; ++i) { arr[i] = vector<unsigned long long>(n); } int zi, zj; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cin >> arr[i][j]; if (arr[i][j] == 0) { zi = i; zj = j; } } } if (n == 1) { cout << 42 << endl; return 0; } int fullI = (zi == 0 ? 1 : 0); unsigned long long fullSum = 0; for (int j = 0; j < n; ++j) { fullSum += arr[fullI][j]; } unsigned long long zeroSum = 0; for (int j = 0; j < n; ++j) { zeroSum += arr[zi][j]; } if (fullSum <= zeroSum) { cout << -1 << endl; return 0; } unsigned long long res = fullSum - zeroSum; arr[zi][zj] = res; for (int i = 0; i < n; ++i) { unsigned long long sh = 0, sv = 0; for (int j = 0; j < n; ++j) { sh += arr[i][j]; sv += arr[j][i]; } if (sh != fullSum || sv != fullSum) { cout << -1 << endl; return 0; } } unsigned long long d1 = 0, d2 = 0; for (int i = 0; i < n; ++i) { d1 += arr[i][i]; d2 += arr[n - 1 - i][i]; } if (d1 != fullSum || d2 != fullSum) { cout << -1 << endl; return 0; } cout << res << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; string s; int main() { cin >> s; int t = (s.size() - 1) / 2 + 1; bool ok = 1; if (s.size() % 2 == 1) { for (int i = 1; i < s.size(); i++) if (s[i] == '1') { ok = 0; } } else ok = 0; cout << t - ok; return 0; }
1
#include <bits/stdc++.h> using namespace std; struct P { double x, y; void scan() { scanf("%lf %lf", &x, &y); } double dis(P& a) { return hypot(x - a.x, y - a.y); } } a, b, c; double ab, bc, ac, tb, tc, l, r, lm, rm; double cal(double k) { P u = (P){k * b.x + (1 - k) * c.x, k * b.y + (1 - k) * c.y}; double au = a.dis(u), ub = u.dis(b), uc = u.dis(c); if (au + ub < tb && au + uc < tc) return min(tb - ub, tc - uc); double l = 0, r = 1, m; for (int i = 0; i < 99; i++) { m = (l + r) * .5; P v = (P){m * u.x + (1 - m) * a.x, m * u.y + (1 - m) * a.y}; if (m * au + v.dis(b) < tb && m * au + v.dis(c) < tc) l = m; else r = m; } return (l + r) * .5 * au; } int main() { scanf("%lf %lf", &tb, &tc); a.scan(), c.scan(), b.scan(); ab = a.dis(b), ac = a.dis(c), bc = b.dis(c); tc += ac + 1e-12, tb += ab + 1e-12; if (tc > ab + bc) return printf("%.9lf", min(tb + bc, tc)), 0; l = 0, r = 1; for (int i = 0; i < 99; i++) { lm = (2 * l + r) / 3, rm = (l + 2 * r) / 3; if (cal(lm) > cal(rm)) r = rm; else l = lm; } printf("%.8lf", cal((l + r) * .5)); return 0; }
4
#include <bits/stdc++.h> using namespace std; int n,c[10][10]; signed main() { ios::sync_with_stdio(false); cin>>n; for(int i=1;i<=n;i++){ int x=i%10,p=i,y=0; while(p!=0){ y=p%10; p/=10; } c[x][y]++; } int ans=0; for(int i=1;i<=9;i++){ for(int j=1;j<=9;j++){ ans+=c[i][j]*c[j][i]; } } cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;} template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();} template<class T> inline T sqr(T x) {return x*x;} typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<string> vs; typedef pair<int, int> pii; typedef long long ll; #define all(a) (a).begin(),(a).end() #define rall(a) (a).rbegin(), (a).rend() #define pb push_back #define mp make_pair #define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i) #define exist(s,e) ((s).find(e)!=(s).end()) #define range(i,a,b) for(int i=(a);i<(b);++i) #define rep(i,n) range(i,0,n) #define clr(a,b) memset((a), (b) ,sizeof(a)) #define dump(x) cerr << #x << " = " << (x) << endl; #define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl; const double eps = 1e-10; const double pi = acos(-1.0); const ll INF =1LL << 62; const int inf =1 << 29; int n; int pw; int vw[100010]; int pf[100010]; int vf[100010]; int th[100010]; double check(double water){ double res=0.0; rep(i,n){ double fi=max(0.0,1.0*(th[i]-1.0*water*vw[i])/vf[i]); res+=pf[i]*fi; } res+=water*pw; return res; } int main(void){ while(1){ cin >> n; if(n==0) break; cin >> pw; rep(i,n) cin >> vw[i] >> pf[i] >> vf[i] >> th[i]; double l=0.0,r=inf; rep(loop,200){ if(check((2.0*l+r)/3.0) < check((l+2.0*r)/3.0) ) r=(l+2.0*r)/3.0; else l=(2.0*l+r)/3.0; } double ans=check((l+r)*0.5); cout.precision(9); cout << fixed << ans << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 100; const long long mod = 1e9 + 7; vector<pair<long long, long long> > a[N]; long long rev[N], f[N]; void check(long long x) { for (long long i = 0; i < a[x].size(); i++) { long long tmp = x; while (tmp % a[x][i].first == 0) { tmp /= a[x][i].first; a[x][i].second++; } } } long long choose(long long n, long long k) { long long ans = (f[n] * rev[k]) % mod; ans *= rev[n - k]; ans %= mod; return ans; } long long tavan(long long n, long long k) { if (k == 0) return 1; n %= mod; long long ans = tavan(n, k / 2); ans *= ans; ans %= mod; if (k % 2 == 1) ans *= n; ans %= mod; return ans; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); long long q; cin >> q; f[0] = 1; rev[0] = 1; for (long long i = 1; i < N; i++) { f[i] = (f[i - 1] * i) % mod; } rev[N - 1] = tavan(f[N - 1], mod - 2); for (long long i = N - 2; i >= 1; i--) { rev[i] = (rev[i + 1] * (i + 1)) % mod; } for (long long i = 2; i < N; i++) { if (a[i].size() != 0) continue; for (long long j = i; j < N; j += i) { a[j].push_back(make_pair(i, 0)); } } for (long long i = 2; i < N; i++) check(i); for (long long _ = 0, r, n; _ < q; _++) { cin >> r >> n; long long ans = 1; for (long long i = 0, x; i < a[n].size(); i++) { x = a[n][i].second; ans *= (choose(x + r, r) + choose(x + r - 1, r)) % mod; ans %= mod; } cout << ans << "\n"; } return 0; }
5
// C - Bugged #include <bits/stdc++.h> using namespace std; int main(){ int N; cin>>N; int sumS = 0; int minS = INT_MAX; for(int i=1; i<=N; ++i){ int s; cin>>s; sumS += s; if(s%10) minS = min(minS, s); } if(sumS%10==0) sumS = max(0, sumS - minS); cout<< sumS <<endl; }
0
#include <bits/stdc++.h> struct P { long long int x; long long int y; }; struct Seg { P a; P b; }; bool isEqual(P const &a, P const &b) { return a.x == b.x && a.y == b.y; } bool hasCommonPoint(Seg const &one, Seg const &two) { return isEqual(one.a, two.a) || isEqual(one.a, two.b) || isEqual(one.b, two.a) || isEqual(one.b, two.b); } int getThirdSeg(std::pair<int, int> idxs) { if (idxs.first != 0 && idxs.second != 0) return 0; if (idxs.first != 1 && idxs.second != 1) return 1; if (idxs.first != 2 && idxs.second != 2) return 2; return -1; } std::pair<int, int> getFirstAndSecondSegment(std::vector<Seg> const &ss) { assert(ss.size() == 3); Seg one = ss[0]; Seg two = ss[1]; Seg three = ss[2]; for (int i = 0; i < ss.size(); i++) { for (int j = i + 1; j < ss.size(); j++) { if (hasCommonPoint(ss[i], ss[j])) { return {i, j}; } } } return {-1, -1}; } long long int cross(P const &a, P const &b) { return std::abs(a.x * b.y - b.x * a.y); } long long int dot(P const &a, P const &b) { return a.x * b.x + b.y * a.y; } P makeV(P const &a, P const &b) { return P{b.x - a.x, b.y - a.y}; } double getMagnitude(P const &a) { return std::sqrt(a.x * a.x + a.y * a.y); } bool isCorrectAngle(P const &a, P const &b) { return dot(a, b) >= 0; }; bool isOnLine(Seg const &line, P const &point) { auto first = makeV(line.a, line.b); auto second = makeV(line.a, point); if (cross(first, second) != 0) { return false; } if (first.x < 0 && second.x > 0 || first.x > 0 && second.x < 0) { return false; } else if (first.y < 0 && second.y > 0 || first.y > 0 && second.y < 0) { return false; } return true; } bool hasCorrectRatio(Seg const &first, Seg const &second, Seg const &third) { double one = getMagnitude(makeV(first.a, third.a)) / getMagnitude(makeV(first.a, first.b)); double two = getMagnitude(makeV(second.a, third.b)) / getMagnitude(makeV(second.a, second.b)); if ((0.1999999999999 <= one && one <= .8) && (0.1999999999999 <= two && two <= .8)) { return true; } return false; } bool isA(std::vector<Seg> &ss) { auto segs = getFirstAndSecondSegment(ss); if (segs.first < 0) { return false; } auto first = ss[segs.first]; auto second = ss[segs.second]; auto thirdI = getThirdSeg(segs); if (thirdI < 0) { return false; } auto third = ss[thirdI]; if (isEqual(first.a, second.a)) { std::swap(first.a, first.b); } else if (isEqual(first.a, second.b)) { std::swap(first.a, first.b); std::swap(second.a, second.b); } else if (isEqual(first.b, second.b)) { std::swap(second.a, second.b); } if (!isEqual(first.b, second.a)) { return false; } if (!isOnLine(first, third.a)) { std::swap(third.a, third.b); if (!isOnLine(first, third.a)) { return false; } } if (!isOnLine(second, third.b)) { return false; } if (!hasCorrectRatio(first, second, third)) { return false; } if (!isCorrectAngle(makeV(first.b, first.a), makeV(second.a, second.b))) { return false; } return true; } int main() { unsigned long long int c, i; long long int x, y; std::string output; std::ios::sync_with_stdio(false); std::cin >> c; std::stringstream msg; std::vector<std::vector<Seg>> sss; for (i = 0; i < c; i++) { std::vector<Seg> ss; for (int j = 0; j < 3; j++) { Seg s; std::cin >> s.a.x >> s.a.y >> s.b.x >> s.b.y; ss.push_back(s); } sss.push_back(ss); } for (auto &segs : sss) { if (isA(segs)) { std::cout << "YES\n"; } else { std::cout << "NO\n"; } } }
2
#include <bits/stdc++.h> using namespace std; int main() { int i, j; int a, b, c, d; cin >> a >> b >> c >> d; for (i = 0; i < 10000; i++) { for (j = 0; j < 10000; j++) { if (b + a * i == d + c * j) { cout << b + a * i << endl; return 0; } } } cout << "-1" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; int even = 0, odd = 0; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] % 2 == 0) { even++; } else { odd++; } } if (odd % 2 == 0) { cout << even << "\n"; } else { cout << odd << "\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int map[100][100]; int n, m, k; cin >> n >> m >> k; int crossx[26]; int crossy[26]; char c; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> c; if (c == '#') map[i][j] = 0; else if (c >= 'a' && c <= 'z') { map[i][j] = 1; crossx[c - 'a'] = i; crossy[c - 'a'] = j; } else if (c >= '0' && c <= '9') map[i][j] = (int)(c - '0'); } } int x1, y1, x2, y2; string s; cin >> x1 >> y1 >> s >> x2 >> y2; x1--; x2--; y1--; y2--; int t = 0; int in = 0; while (t < k && in < s.size()) { t += map[x1][y1]; if (t > k) break; if (x1 == crossx[s[in] - 'a']) { if (y1 < crossy[s[in] - 'a']) { y1++; } else { y1--; } } else if (y1 == crossy[s[in] - 'a']) { if (x1 < crossx[s[in] - 'a']) { x1++; } else { x1--; } } if (x1 == crossx[s[in] - 'a'] && y1 == crossy[s[in] - 'a']) { in++; } } while (t < k) { if (x1 == x2) { if (y1 < y2) { y1++; } else if (y1 > y2) { y1--; } } else if (y1 == y2) { if (x1 < x2) { x1++; } else if (x1 > x2) { x1--; } } t += map[x1][y1]; } cout << x1 + 1 << " " << y1 + 1 << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; vector<long long> v(n); long long ans = 0; long long prev = 0; for (long long i = 0; i < n; i++) { cin >> v[i]; ans += abs(v[i] - prev); prev = v[i]; } ans += n - 1 + n; cout << ans << "\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, i = 1; cin >> a >> b; for (long long i = 2; i <= 6000000; i++) { if (a <= i / 2 && b <= i / 3 && a + b <= (i / 2 + i / 3 - i / 6)) { cout << i; return 0; } } }
3
#include <bits/stdc++.h> using namespace std; int main() { long long int n, i; cin >> n; long long int a[n]; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); long long int s1 = 0, s = 0; for (i = 0; i < n; i++) s += a[i]; if (n == 1) s1 = s; else s1 = 2 * s; for (i = 0; i < (n - 2); i++) { s = s - a[i]; s1 += s; } cout << s1 << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); std::vector<int> tiempo(n, 0); for (int i = 0; i < n; i++) { scanf("%d", &tiempo[i]); } int T; std::sort(tiempo.begin(), tiempo.end()); scanf("%d", &T); int counter = 0; for (int j = 0; j < n; j++) { int save = 1; for (int k = j + 1; j < n; k++) { if (tiempo[k] <= (tiempo[j] + T)) { ++save; } else { ; break; }; } if (save > counter) { counter = save; } } if (counter > n) { printf("%d", n); } else { printf("%d", counter); } }
2
#include <bits/stdc++.h> using namespace std; using namespace std::chrono; template <typename A> ostream &operator<<(ostream &cout, vector<A> const &v); template <typename A, typename B> ostream &operator<<(ostream &cout, pair<A, B> const &p) { return cout << "(" << p.first << ", " << p.second << ")"; } template <typename A> ostream &operator<<(ostream &cout, vector<A> const &v) { cout << "["; for (int i = 0; i < v.size(); i++) { if (i) cout << ", "; cout << v[i]; } return cout << "]"; } template <typename A, typename B> istream &operator>>(istream &cin, pair<A, B> &p) { cin >> p.first; return cin >> p.second; } mt19937 rng(61378913); void usaco(string filename) { freopen((filename + ".in").c_str(), "r", stdin); freopen((filename + ".out").c_str(), "w", stdout); } const long double pi = 3.14159265358979323846; const long long mod = 1000000007; long long n, m, k, q, l, r, x, y, z; long long a[1000005]; long long b[1000005]; long long c[1000005]; string second, t; long long ans = 0; bitset<300000005> prime; void sieve(int n) { for (int p = 2; p * p <= n; p++) { if (!prime[p]) { for (int i = p * p; i <= n; i += p) prime[i] = 1; } } for (int p = max(5LL, l); p <= r; p++) if (!prime[p] && (p & 3) == 1) ++ans; } void solve(int tc) { cin >> l >> r; if (l <= 2 && 2 <= r) ++ans; sieve(r); cout << ans << '\n'; } int main() { { ios_base::sync_with_stdio(false); } { cin.tie(NULL); cout.tie(NULL); } int tc; tc = 1; for (int t = 0; t < tc; t++) solve(t); }
3
#include <bits/stdc++.h> using namespace std; const int N = 200010; vector<int> G[N], to[N]; int n, m; int Ex[N], Ey[N], fa[N]; int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } int deg[N]; void DFS(int o, int f) { for (int v : G[o]) { if (v == f) continue; DFS(v, o); if (deg[v]) to[v].push_back(o); else to[o].push_back(v), deg[o] ^= 1; } } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) fa[i] = i; for (int i = 1; i <= m; i++) { int u, v; scanf("%d%d", &u, &v); Ex[i] = u, Ey[i] = v; u = find(u), v = find(v); if (u ^ v) { fa[u] = v; G[Ex[i]].push_back(Ey[i]), G[Ey[i]].push_back(Ex[i]); } else to[Ex[i]].push_back(Ey[i]), deg[Ex[i]] ^= 1; } int ans = m; for (int i = 1; i <= n; i++) if (fa[i] == i) { DFS(i, 0); ans -= deg[i]; } printf("%d\n", ans / 2); for (int i = 1; i <= n; i++) for (int j = 0; j + 1 < int(to[i].size()); j += 2) printf("%d %d %d\n", to[i][j], i, to[i][j + 1]); return 0; }
4
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int maxn = 500; int n; ll f[maxn + 1][maxn + 1][maxn + 1], ans = 1e18; ll s[maxn + 10][maxn + 10], t[maxn + 10][maxn + 10], a[maxn + 10][maxn + 10]; void upd(ll &x, ll y) { x = min(x, y); } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) { if (i != j) scanf("%lld", &a[i][j]); s[i][j] = s[i][j - 1] + a[i][j]; t[i][j] = t[i - 1][j] + a[i][j]; } memset(f, 0x3f, sizeof f); f[1][1][1] = 0; for (int i = 1; i < n; ++i) for (int j = 1; j <= i; ++j) for (int k = 1; k <= j; ++k) { upd(f[i + 1][j][k], f[i][j][k] + t[i][i + 1] - t[j - 1][i + 1] + s[i + 1][k - 1]); upd(f[i + 1][i + 1][j], f[i][j][k] + s[i + 1][j - 1]); upd(f[i + 1][i + 1][i + 1], f[i][j][k] + s[i + 1][i]); } for (int i = 1; i <= n; ++i) for (int j = 1; j <= i; ++j) ans = min(ans, f[n][i][j]); printf("%lld", ans); }
0
#include <bits/stdc++.h> using namespace std; int c[1005]; int main() { int t; scanf("%d", &t); while (t--) { int n, m; scanf("%d%d", &n, &m); int cnt = 0; memset(c, 0, sizeof(c)); for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); if (c[x] == 0) c[x] = 1; } for (int i = 1; i <= m; i++) { int x; scanf("%d", &x); if (c[x] == 1) c[x] = 2, cnt++; } if (cnt == 0) printf("NO\n"); else { printf("YES\n"); for (int i = 1; i <= 1000; i++) { if (c[i] == 2) { printf("1 %d\n", i); break; } } } } }
1
#include <bits/stdc++.h> using namespace std; double dp[205][205][205]; struct node { double p; int v; int i; } a[205]; bool cmp(node a, node b) { return a.v > b.v; } int main() { int n, l, kk; scanf("%d %d %d", &n, &l, &kk); for (int i = 0; i < n; i++) { scanf("%lf", &a[i].p); a[i].i = i; a[i].p = a[i].p / 100; } for (int i = 0; i < n; i++) scanf("%d", &a[i].v); sort(a, a + n, cmp); dp[0][0][kk] = 1; for (int i = 0; i < n; i++) { for (int j = 0; j <= 200; j++) for (int k = 0; k <= 200; k++) if (dp[i][j][k] != 0) { if (a[i].v + k >= 0) { if (a[i].v + k > 200) dp[i + 1][j + 1][200] += dp[i][j][k] * a[i].p; else dp[i + 1][j + 1][a[i].v + k] += dp[i][j][k] * a[i].p; } dp[i + 1][j][k] += dp[i][j][k] * (1 - a[i].p); } } double ans = 0; for (int i = l; i <= n; i++) for (int j = 0; j <= 200; j++) { ans += dp[n][i][j]; } printf("%.10lf\n", ans); return 0; }
2
#include<bits/stdc++.h> // Created by kobortor On 01:26:04 on 04-12-2020 using namespace std; #define allof(x) (x).begin(), (x).end() typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; struct point { ll x, y; point () : x(0), y(0) {} point (ll _x, ll _y) : x(_x), y(_y) {} }; istream& operator>> (istream& in, point &p) { return in >> p.x >> p.y; } ostream& operator<< (ostream& out, const point &p) { return out << "(" << p.x << " " << p.y << ")"; } bool operator<(const point &p1, const point &p2) { if (p1.x == p2.x) { return p1.y < p2.y; } else { return p1.x < p2.x; } } /* 2-3 |.| 1-4 */ ll get_cost(point p1, point p2, point p3, point p4) { // cout << p1 << " " << p2 << " " << p3 << " " << p4 << endl; vector<ll> xs = {p1.x, p2.x, p3.x, p4.x}; vector<ll> ys = {p1.y, p2.y, p3.y, p4.y}; sort(allof(xs)); sort(allof(ys)); ll x = xs[1]; ll y = ys[1]; ll ans = 0; for (ll &xi : xs) ans += abs(xi - x); for (ll &yi : ys) ans += abs(yi - y); return ans; } ll solve(point p1, point p2, point p3, point p4) { //cout << "------------------------\n"; //cout << p1 << " " << p2 << " " << p3 << " " << p4 << endl; ll ans = get_cost(p1, p2, p3, p4); vector<ll> ks; ks.push_back(p3.x - p1.x); ks.push_back(p4.x - p1.x); ks.push_back(p3.x - p2.x); ks.push_back(p4.x - p2.x); ks.push_back(p2.y - p1.y); ks.push_back(p3.y - p1.y); ks.push_back(p2.y - p4.y); ks.push_back(p3.y - p4.y); for (ll k : ks) { k = abs(k); // cout << "Tested " << k << endl; ll cost = get_cost( point(p1.x, p1.y), point(p2.x, p2.y - k), point(p3.x - k, p3.y - k), point(p4.x - k, p4.y)); ans = min(ans, cost); } return ans; } int main(){ cin.tie(0); cin.sync_with_stdio(0); int t; cin >> t; while (t--) { vector<point> P(4); for (point &p : P) { cin >> p; } sort(allof(P)); ll ans = 1e18; do { ans = min(ans, solve(P[0], P[1], P[2], P[3])); } while (next_permutation(allof(P))); cout << ans << "\n"; } }
5
#include <bits/stdc++.h> const long long mod = 1000000007; using namespace std; const long long N = 100005; long long last[N]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long q, id = 1; cin >> q; memset(last, -1, sizeof(last)); while (q--) { long long x, y, ans = 0; cin >> x >> y; long long left = id - y; long long right = id - 1; for (long long k = 1; k * k <= x; k++) { if (x % k == 0) { long long d1 = k; long long d2 = x / k; if (last[d1] < left) ans++; if (d1 != d2) if (last[d2] < left) ans++; last[d1] = id; last[d2] = id; } } id++; cout << ans << '\n'; } return 0; }
2
#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 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; } const long double eps = 1e-10L; string s; long long len; long long lps[1000005]; long long a[1000005]; bool ok[1000005]; long long cnt[1000005]; void LPS() { len = s.length(); lps[0] = 0; for (long long i = 1; i < len; i++) { long long j = lps[i - 1]; while (j > 0 && s[j] != s[i]) j = lps[j - 1]; if (s[i] == s[j]) j++; lps[i] = j; } } bool isvalid(long long x) { long long s1 = a[x]; long long s2 = a[x + 1]; long long overlap = (s1 + len) - s2; if (overlap <= 0) return true; return ok[overlap - 1]; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, m; cin >> n >> m; cin >> s; for (long long i = 1; i <= m; i++) { cin >> a[i]; } LPS(); long long j = lps[s.length() - 1]; while (j > 0) { ok[j - 1] = 1; j = lps[j - 1]; } for (long long i = 1; i < m; i++) { if (!isvalid(i)) { cout << "0"; return 0; } } for (long long i = 1; i <= m; i++) { long long st = a[i]; long long end1 = a[i] + len; cnt[st]++; cnt[end1]--; } long long ans = 1; for (long long i = 1; i <= n; i++) { cnt[i] += cnt[i - 1]; if (cnt[i] == 0) { ans = (ans * 26); ans %= (long long)(1000 * 1000 * 1000 + 7); } } cout << ans; }
4
#include <bits/stdc++.h> using namespace std; string t; int N; const int tam = 3e5 + 9; long long mod1 = 1e9 + 7, mod2 = 1e9 + 9; long long p1 = 37, p2 = 41, shut1 = 0, shut2 = 0; long long v1[tam], v2[tam], po1[tam], po2[tam]; struct trie { list<pair<string, int> > m; } tr[tam]; long long exp(long long b, long long e, long long mod) { long long ans = 1; while (e) { if (e & 1) { ans = (ans * b) % mod; } b = (b * b) % mod; e >>= 1; } return ans; } long long DFS(int idx = 0, int pos = 1) { if (tr[idx].m.size() == 0) { return 0; } long long ans = 0; for (auto i : tr[idx].m) { for (int j = 0; j < i.first.size(); j++) { v1[pos + j] = ((po1[pos + j] * (i.first[j] - 'a' + 1)) % mod1 + v1[pos + j - 1]) % mod1; v2[pos + j] = ((po2[pos + j] * (i.first[j] - 'a' + 1)) % mod2 + v2[pos + j - 1]) % mod2; if (N <= pos + j) { if (((v1[pos + j] - v1[pos + j - N] + mod1) % mod1 * exp(po1[pos + j - N + 1], mod1 - 2, mod1)) % mod1 == shut1 && ((v2[pos + j] - v2[pos + j - N] + mod2) % mod2 * exp(po2[pos + j - N + 1], mod2 - 2, mod2)) % mod2 == shut2) ans++; } } ans += DFS(i.second, pos + i.first.size()); } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, pv; string temp; cin >> n; for (int i = 1; i < n; i++) { cin >> pv >> temp; pv--; tr[pv].m.push_back(pair<string, int>(temp, i)); } long long a = 1, b = 1; for (int i = 0; i < tam; i++) { po1[i] = a; po2[i] = b; a = (a * p1) % mod1; b = (b * p2) % mod2; } cin >> t; N = t.size(); for (int i = 0; i < N; i++) { shut1 = (shut1 + (po1[i] * (t[i] - 'a' + 1)) % mod1) % mod1; shut2 = (shut2 + (po2[i] * (t[i] - 'a' + 1)) % mod2) % mod2; } cout << DFS() << '\n'; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int sz = 1e5 + 10, sz2 = 200, bl = 510; int n, m; int q[sz2][sz], fi[sz2][sz], la[sz2][sz], to[sz], ma[sz2], add[sz2]; int l, r; int ar[sz]; void ins(int nu, int p, int va) { if (q[nu][p]) { to[va] = fi[nu][p], fi[nu][p] = va; } else { fi[nu][p] = va, la[nu][p] = va, to[va] = -p; } q[nu][p]++; } void merge(int nu, int f, int t) { if (q[nu][t]) to[la[nu][t]] = fi[nu][f]; else fi[nu][t] = fi[nu][f]; la[nu][t] = la[nu][f]; to[la[nu][t]] = -t; q[nu][t] += q[nu][f], q[nu][f] = 0; } void cha(int nu, int x) { for (int a = nu * bl + 1; a <= min(n, (nu + 1) * bl); a++) ar[a] = 0; for (int a = nu * bl + 1; a <= min(n, (nu + 1) * bl); a++) { if (ar[a] == 0) { int cu = a; while (cu > 0) cu = to[cu]; int p = -cu; cu = fi[nu][p]; while (cu > 0) { ar[cu] = p - add[nu]; if (ar[cu] > x and cu >= l + 1 and cu <= r + 1) ar[cu] -= x; cu = to[cu]; } q[nu][p] = 0; } } for (int a = nu * bl + 1; a <= min(n, (nu + 1) * bl); a++) ins(nu, ar[a], a); add[nu] = 0; } int que(int nu, int x) { int re = 0; if (x <= ma[nu]) { x += add[nu]; if (q[nu][x]) { int cu = fi[nu][x]; while (cu > 0) { if (cu >= l + 1 and cu <= r + 1) re++; cu = to[cu]; } } } return re; } int main() { for (int a = 0; a < sz2; a++) for (int b = 1; b < sz; b++) q[a][b] = 0; cin >> n >> m; for (int a = 0; a < n; a++) { int x; scanf("%d", &x); int nu = a / bl; ma[nu] = sz - 1, add[nu] = 0; ins(nu, x, a + 1); } for (int a = 0; a < m; a++) { int ty, x; scanf("%d%d%d%d", &ty, &l, &r, &x); l--, r--; int lg = l / bl, rg = r / bl; if (ty == 1) { cha(lg, x); if (rg != lg) cha(rg, x); for (int b = lg + 1; b < rg; b++) { if (ma[b] > x) { if (ma[b] - x < x) { for (int c = ma[b] + add[b]; c > x + add[b]; c--) { if (q[b][c]) merge(b, c, c - x); } } else { for (int c = 1 + add[b]; c <= x + add[b]; c++) { if (q[b][c]) merge(b, c, c + x); } add[b] += x; } ma[b] = max(ma[b] - x, x); } } } else { int an = que(lg, x); if (lg != rg) an += que(rg, x); for (int b = lg + 1; b < rg; b++) { if (x <= ma[b]) an += q[b][x + add[b]]; } printf("%d\n", an); } } }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 105000; struct Edge { int nex, y; } e[maxn * 2]; int lis[maxn], cnt; vector<int> V[maxn]; int n, m, top, tim, tot, ans; int belong[maxn], st[maxn], dfn[maxn], low[maxn], f[maxn]; bool flag, instack[maxn], v[maxn]; inline void addedge(int x, int y) { e[++cnt] = (Edge){lis[x], y}; lis[x] = cnt; } void tarjan(int x) { dfn[x] = low[x] = ++tim; instack[st[++top] = x] = true; for (int i = lis[x]; i; i = e[i].nex) { int y = e[i].y; if (!dfn[y]) { tarjan(y); if (low[x] > low[y]) low[x] = low[y]; } else if (instack[y] && dfn[x] > dfn[y]) dfn[x] = dfn[y]; } if (dfn[x] == low[x]) { int j; ++tot; do { instack[j = st[top--]] = false; belong[j] = tot; f[tot]++; } while (j != x); } } void dfs(int x) { v[x] = true; int p = belong[x]; if (f[p] > 1) flag = true; for (auto y : V[x]) if (!v[y]) dfs(y); } int main() { scanf("%d%d\n", &n, &m); while (m--) { int x, y; scanf("%d%d\n", &x, &y); V[x].push_back(y); V[y].push_back(x); addedge(x, y); } for (int i = 1; i <= n; i++) if (!dfn[i]) tarjan(i); ans = 0; for (int i = 1; i <= n; i++) if (!v[i]) { flag = false; dfs(i); if (!flag) ans++; } printf("%d\n", n - ans); return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long MOD = 998244353; const long long INF = 1e10; using Graph = vector<vector<long long>>; char solve(char a) { if (a == '9') a = '1'; else a++; return a; } signed main() { long long T; cin >> T; for (long long t = 0; t < (long long)(T); t++) { vector<string> A(9); for (long long i = 0; i < (long long)(9); i++) cin >> A[i]; A[0][0] = solve(A[0][0]); A[1][3] = solve(A[1][3]); A[2][6] = solve(A[2][6]); A[3][1] = solve(A[3][1]); A[4][4] = solve(A[4][4]); A[5][7] = solve(A[5][7]); A[6][2] = solve(A[6][2]); A[7][5] = solve(A[7][5]); A[8][8] = solve(A[8][8]); for (long long i = 0; i < (long long)(9); i++) cout << A[i] << endl; } }
4
#include <bits/stdc++.h> inline int zs(int a) { if (a < 2) return 0; for (register int i = 2; i * i <= a; i++) { if (a % i == 0) return 0; } return 1; } int f[1005], num, ans, n, k; int main() { scanf("%d %d", &n, &k); for (register int i = 2; i <= n; i++) { if (zs(i)) f[++num] = i; } for (register int i = 1; i <= num; i++) { if (zs(f[i] + f[i + 1] + 1) && f[i] + f[i + 1] + 1 <= n) { ans++; } } if (ans >= k) printf("YES"); else printf("NO"); }
1
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); const int N = 100010; int main(void) { int n, s; cin >> n >> s; s *= 100; int resp = -1; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; int k = x * 100 + y; int diff = s - k; if (diff < 0) continue; resp = max(resp, diff % 100); } cout << resp << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long n, x; cin >> n; x = 1; while (x < n) { n -= x; x++; } cout << n; return 0; }
1
#include <bits/stdc++.h> using namespace std; struct ppap { int x, y, l, r; friend bool operator<(ppap x, ppap y) { return x.l < y.l; } }; multiset<ppap> s; const int N = 1000010; int n, m, tot, to[2000010], nxt[2000010], R[2000010], head[500010][2], L[500010][2]; void add(int x, int y, int opt, int r) { to[++tot] = y, R[tot] = r, nxt[tot] = head[x][opt], head[x][opt] = tot; } void Extend(int x, int l, int r, int opt) { L[x][opt] = max(L[x][opt], r); for (int i = head[x][opt]; i; i = nxt[i]) { ppap p; p.x = x, p.y = to[i], p.l = l, p.r = R[i]; s.insert(p); } head[x][opt] = 0; } int main() { cin >> n >> m; if (n == 1) { cout << 0; return 0; } for (int i = 1, x, y, l, r; i <= m; i++) { cin >> x >> y >> l >> r; r--; ppap p; p.x = x, p.y = y, p.l = l, p.r = r - (r - l) % 2, s.insert(p); p.x = y, p.y = x, p.l = l, p.r = r - (r - l) % 2, s.insert(p); p.x = x, p.y = y, p.l = l + 1, p.r = r - !((r - l) % 2), s.insert(p); p.x = y, p.y = x, p.l = l + 1, p.r = r - !((r - l) % 2), s.insert(p); } memset(L, -1, sizeof(L)); L[1][0] = 0; while (!s.empty()) { ppap p = *s.begin(); s.erase(s.begin()); if (p.l > p.r) continue; int x = p.x, y = p.y, opt = p.l & 1; if (L[x][opt] >= p.l) { if (y == n) { cout << (p.l + 1); return 0; } Extend(y, p.l + 1, p.r + 1, opt ^ 1); } else add(x, y, opt, p.r); } cout << -1; }
6
#include <bits/stdc++.h> using namespace std; int n; int vec[100011]; int pila[100011], top = 0; int main() { scanf("%d", &n); int sol = 0; pila[top = 0] = -1; for (int i = 0; i < n; i++) { scanf("%d", &vec[i]); while (top > 0 && vec[pila[top]] < vec[i]) { sol = max(sol, vec[i] ^ vec[pila[top]]); --top; } pila[++top] = i; } pila[top = 0] = n; for (int i = n - 1; i >= 0; --i) { while (top > 0 && vec[pila[top]] < vec[i]) { sol = max(sol, vec[i] ^ vec[pila[top]]); --top; } pila[++top] = i; } printf("%d", sol); return 0; }
4
#include <bits/stdc++.h> using namespace std; pair<int, int> solve(long long s, int n) { long long state, x, y, row; state = x = y = 0; for (int i = 2 * n - 2; i >= 0; i -= 2) { row = 4 * state | (s >> i) & 3; x = (x << 1) | (0x936C >> row) & 1; y = (y << 1) | (0x39C6 >> row) & 1; state = (0x3E6B94C1 >> 2 * row) & 3; } return make_pair(x, y); } int main() { for (long long a, b; cin >> a >> b;) { pair<long long, long long> ans = solve(b, a); cout << ans.first << " " << ans.second << endl; } return 0; }
2
#include <bits/stdc++.h> int main() { int y, a, b, c, d; scanf("%d", &y); while (1) { y = y + 1; int a = y / 1000; int b = y / 100 % 10; int c = y / 10 % 10; int d = y % 10; if (a != b && a != c && a != d && b != c && b != d && c != d) { break; } } printf("%d\n", y); }
1
#include<bits/stdc++.h> #define rep(i,a,b) for(int i=a;i<b;i++) #define rrep(i,a,b) for(int i=a;i>=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() #pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; template<class T>bool chmax(T &a, const T &b) { if (a<b) { a = b; return 1; } return 0; } template<class T>bool chmin(T &a, const T &b) { if (b<a) { a = b; return 1; } return 0; } //--------------------------------------------------------------------------------------------------- /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int N, D[301010]; //--------------------------------------------------------------------------------------------------- int check() { int ok = 0; rep(i, 0, N) { if (ok < i) return 0; chmax(ok, i + D[i] / 10); } return 1; } //--------------------------------------------------------------------------------------------------- void _main() { cin >> N; rep(i, 0, N) cin >> D[i]; if (!check()) { printf("no\n"); return; } reverse(D, D + N); if (!check()) { printf("no\n"); return; } printf("yes\n"); }
0
#include <bits/stdc++.h> using namespace std; map<string, int> NameSet; int NameSetLen, A[1010], B[1010], n, m, K; struct Block { vector<int> Peo, Key, Room; void Check() { sort(Peo.begin(), Peo.end()); sort(Key.begin(), Key.end()); sort(Room.begin(), Room.end()); } }; bool operator==(Block A, Block B) { return A.Peo == B.Peo && A.Key == B.Key && A.Room == B.Room; } struct Day { vector<Block> ForBlocks; vector<int> Key[1010]; vector<int> Room[1010]; int _Key[1010], Father[1010], _Room[1010]; bool bl[1010]; string Name[1010]; Block Tmp; int Find(int x) { return (Father[x] == x) ? x : Father[x] = Find(Father[x]); } void Solve() { int i, j, k, l; for (i = 1; i <= n; i++) Father[i] = i; for (;;) { for (i = 1; i <= m; i++) if (Find(A[i]) != Find(B[i]) && (Find(_Room[_Key[i]]) == Find(A[i]) || Find(_Room[_Key[i]]) == Find(B[i]))) break; if (i == m + 1) break; Father[Find(A[i])] = Father[Find(B[i])]; } for (i = 1; i <= n; i++) if (Find(i) == i) { Tmp.Peo.clear(); Tmp.Key.clear(); Tmp.Room.clear(); for (j = 1; j <= n; j++) if (Find(j) == i) { Tmp.Room.push_back(j); for (k = Room[j].size() - 1; k >= 0; k--) { Tmp.Peo.push_back(Room[j][k]); for (l = Key[Room[j][k]].size() - 1; l >= 0; l--) Tmp.Key.push_back(Key[Room[j][k]][l]); } } Tmp.Check(); ForBlocks.push_back(Tmp); } } void Read() { string Name; int i, j, k, l, room; for (i = 0; i < K; i++) { cin >> Name >> room; if (!NameSet.count(Name)) NameSet[Name] = ++NameSetLen; j = NameSet[Name]; Room[room].push_back(j); _Room[j] = room; cin >> k; for (; k--;) { cin >> l; Key[j].push_back(l); _Key[l] = j; } } Solve(); } } Thursday, Friday; int main() { cin >> n >> m >> K; for (int i = 1; i <= m; i++) cin >> A[i] >> B[i]; Thursday.Read(); Friday.Read(); if (Thursday.ForBlocks == Friday.ForBlocks) cout << "YES\n"; else cout << "NO\n"; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { vector<int> b1; int a2[203], b2[203], a, b, c; cin >> a >> b; for (int i = 0; i < a; i++) { cin >> c; a2[i] = c; } for (int i = 0; i < b; i++) { cin >> c; b1.push_back(-c); b2[i] = c; } sort(b1.begin(), b1.end()); int j = 0; for (int i = 0; i < a; i++) { if (a2[i] == 0) { a2[i] = b1[j] * (-1); j++; } } b = 0; for (int i = 0; i < a - 1; i++) { if (a2[i] < a2[i + 1]) b++; } if (b == a - 1) cout << "No"; else cout << "Yes"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n = 0; scanf("%d", &n); if (n % 2) printf("-1\n"); else while (n) printf("%d ", n--); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; int a[1000] = {}, save[200] = {}; transform(s.begin(), s.end(), s.begin(), ::tolower); for (int i = 0; i < s.length(); i += 2) { if (s[i] != s[i + 1]) { if (save[s[i + 1]] > 0) save[s[i + 1]]--, save[s[i]]++; else a[s[i + 1]]++, save[s[i]]++; } } int res = 0; for (int i = 0; i <= 200; i++) if (a[i] >= 0) res += a[i]; cout << res; }
1
#include <bits/stdc++.h> using namespace std; int n; int a[300005]; int nxt[300005]; long long dp[300005]; map<int, int> nxtX[300005]; void solve() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } for (int i = 0; i < n + 2; i++) { nxt[i] = -1; nxtX[i].clear(); dp[i] = 0; } for (int i = n - 1; i >= 0; i--) { if (nxtX[i + 1].count(a[i])) { int pos = nxtX[i + 1][a[i]]; assert(pos < n && a[pos] == a[i]); nxt[i] = pos; swap(nxtX[i], nxtX[pos + 1]); if (pos < n - 1) { nxtX[i][a[pos + 1]] = pos + 1; } } nxtX[i][a[i]] = i; } long long ans = 0; for (int i = n - 1; i >= 0; i--) { if (nxt[i] == -1) continue; dp[i] = 1 + dp[nxt[i] + 1]; ans += dp[i]; } printf("%lld\n", ans); } int main(void) { int q; scanf("%d", &q); for (int i = 0; i < q; i++) { solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; bool b = 0; for (int i = 0; i < s.length(); i++) { if (s.at(i) == 'H' || s.at(i) == 'Q' || s.at(i) == '9') { b = 1; cout << "YES"; break; } } if (b == 0) cout << "NO"; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; const int INF = 1e9 + 10; struct SegTree { int n; vector<pair<int, int>> d; void init(int _n) { n = _n; d.assign(4 * n + 1, {INF, 0}); } void update(int id, int l, int r, int u, int v, pair<int, int> val) { if (v < l || r < u) return; if (u <= l && r <= v) { d[id] = min(d[id], val); return; } int mid = (l + r) >> 1; update(id << 1, l, mid, u, v, val); update(id << 1 | 1, mid + 1, r, u, v, val); } pair<int, int> getMin(int id, int l, int r, int pos) { if (pos < l || r < pos) return {INF, 0}; if (l == r) return d[id]; int mid = (l + r) >> 1; return min(d[id], min(getMin(id << 1, l, mid, pos), getMin(id << 1 | 1, mid + 1, r, pos))); } pair<int, int> getMin(int pos) { return getMin(1, 1, n, pos); } void update(int l, int r, pair<int, int> val) { update(1, 1, n, l, r, val); } }; int n, q, s, d, a[N], f[N]; SegTree pre, nxt; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> q >> s >> d; for (int i = 1; i <= n; i++) cin >> a[i]; fill(f + 1, f + 1000001, INF); priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq; set<int> alive; for (int i = 1; i <= n; i++) alive.insert(a[i]); pre.init(1000000); nxt.init(1000000); pq.push({0, a[s]}); auto getCost = [&](int pos) { return min(max(pos + pre.getMin(pos).first, pre.getMin(pos).second), max(nxt.getMin(pos).first - pos, nxt.getMin(pos).second)); }; auto update = [&](int pos, int last, bool upd) { if (upd) { if (pos <= 1000000) pre.update(pos, 1000000, {-pos, last}); if (pos >= 1) nxt.update(1, pos, {pos, last}); } auto it = alive.lower_bound(pos); if (it != alive.end()) pq.push({getCost(*it), *it}); if (it != alive.begin()) { --it; pq.push({getCost(*it), *it}); } }; while (!pq.empty()) { int u = pq.top().second; int val = pq.top().first; pq.pop(); if (!alive.count(u)) continue; f[u] = val; alive.erase(u); update(u + d, val, 1); update(u - d, val, 1); update(u, val, 0); } while (q--) { int x, k; cin >> x >> k; cout << (f[a[x]] <= k? "Yes" : "No") << '\n'; } return 0; }
6
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, ll> P; const int INF = (1<<30); const ll INFLL = (1ll<<60); const ll MOD = (ll)(1e9+7); #define l_ength size void mul_mod(ll& a, ll b){ a *= b; a %= MOD; } void add_mod(ll& a, ll b){ a = (a<MOD)?a:(a-MOD); b = (b<MOD)?b:(b-MOD); a += b; a = (a<MOD)?a:(a-MOD); } int n,db[100100][20],cnt=0,dp[100100],id[100100]; bool visited[100100]; vector<P> path[225816]; struct edge{ int to,c; ll d; }; vector<edge> g[100100]; void dfs(int v, int p=0){ int i; edge e; visited[v] = true; dp[v] = dp[p]+1; db[v][0] = p; id[v] = cnt; ++cnt; // cout << v << " "; for(i=(g[v].l_ength()-1); i>=0; --i){ e = g[v][i]; if(visited[e.to]){ continue; } path[0].push_back(P(cnt,e.d)); path[e.c].push_back(P(cnt,e.d)); path[e.c+n].push_back(P(cnt,1ll)); // cout << "( " << v << "->" << e.to << ") "; dfs(e.to,v); path[0].push_back(P(cnt,-e.d)); path[e.c].push_back(P(cnt,-e.d)); path[e.c+n].push_back(P(cnt,-1ll)); // cout << "( " << e.to << "->" << v << ") "; } } int lca(int u, int v){ int d = 0,i; d = dp[u] - dp[v]; if(d < 0){ swap(u,v); d = (-d); } for(i=0; i<20; ++i){ if(d&(1<<i)){ u = db[u][i]; } } if(u==v){ return u; } for(i=19; i>=0; --i){ if(db[u][i] == db[v][i]){ continue; } u = db[u][i]; v = db[v][i]; } return db[u][0]; } ll query(int c, int v){ int l,r,m; v = id[v]; l = 0; r = path[c].l_ength(); while(r-l>1){ m = ((l+r)>>1); if(path[c][m].first > v){ r = m; }else{ l = m; } } return path[c][l].second; } int main(void){ int q,i,j,x,a,b,c; ll d,y,ans; edge e; cin >> n >> q; for(i=1; i<n; ++i){ cin >> a >> b >> e.c >> e.d; --a; --b; e.to = b; g[a].push_back(e); e.to = a; g[b].push_back(e); } for(j=0; j<n*2; ++j){ path[j].push_back(P(-1,0ll)); } dp[0] = -1; dfs(0); // cout << endl; for(j=1; j<20; ++j){ for(i=0; i<n; ++i){ db[i][j] = db[db[i][j-1]][j-1]; } } for(j=0; j<n*2; ++j){ for(i=1; i<path[j].l_ength(); ++i){ path[j][i].second += path[j][i-1].second; } } for(i=0; i<q; ++i){ cin >> x >> y >> a >> b; --a; --b; c = lca(a,b); ans = query(x+n,a)+query(x+n,b)-query(x+n,c)*2; ans *= y; ans -= query(x,a)+query(x,b)-query(x,c)*2; ans += query(0,a)+query(0,b)-query(0,c)*2; cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> int main(void) { int n, i; long long a[50001] = {0}, average = 0, ans = 0, d, b[50001] = {0}; scanf("%d", &n); i = 1; while (i <= n) scanf("%I64d", a + i), b[i] = a[i], average += a[i++]; average /= n; for (i = 1; i < n; i++) { d = a[i] - average; a[i + 1] += d; if (d >= 0) ans += d; else ans += -d; } for (d = 0; i > 1; i--) { b[i - 1] += b[i] - average; if (b[i] - average >= 0) d += b[i] - average; else d += average - b[i]; } printf("%I64d\n", ans > d ? d : ans); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5+10; int n; int h[maxn], a[maxn]; long long loga[maxn]; long long dp[maxn]; void update(int x, long long val) { for (x++; x < maxn; x += x & -x) loga[x] = max(loga[x], val); } long long query(int x) { long long out = 0; for (x++; x > 0; x -= x & -x) out = max(out, loga[x]); return out; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", h+i); for (int i = 0; i < n; i++) scanf("%d", a+i); long long sol = 0; for (int i = 0; i < n; i++) { dp[i] = query(h[i]) + a[i]; update(h[i], dp[i]); sol = max(sol, dp[i]); } printf("%lld", sol); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int di[] = {-1, 0, 1, 0}; const int dj[] = {0, 1, 0, -1}; const long long MOD = 1e9 + 7; const long long INF = 1e9; const double EPS = 1e-5; int n, start, x; vector<int> num, nxt; unsigned int Random() { unsigned int r = rand(); r *= 12387; r += rand(); return r; } void ask(int idx) { cout << "? " << idx + 1 << endl; cin >> num[idx] >> nxt[idx]; nxt[idx]--; } void askRandom(int asks) { int found = 0; for (int i = 0; i < asks && found < n; i++) { int randIdx; do { randIdx = Random() % n; } while (num[randIdx] != -1); found++; ask(randIdx); } } int getPrev() { vector<pair<int, int> > tmp; for (int i = 0; i < n; i++) if (num[i] != -1) tmp.push_back(pair<int, int>(num[i], i)); sort(tmp.begin(), tmp.end()); auto it = lower_bound(tmp.begin(), tmp.end(), pair<int, int>(x, 0)); if (it == tmp.end()) return tmp.back().second; if (it->first == x) return it->second; if (it == tmp.begin()) { ask(start); return start; } return prev(it)->second; } int find() { int cur = getPrev(); while (nxt[cur] >= 0) { if (num[cur] >= x) return num[cur]; cur = nxt[cur]; if (num[cur] == -1) ask(cur); } return num[cur] >= x ? num[cur] : -1; } int main() { ios::sync_with_stdio(false), cin.tie(0); srand(time(NULL)); cin >> n >> start >> x; start--; num.assign(n, -1); nxt = num; askRandom(300); cout << "! " << find() << endl; cin.ignore(), cin.get(); }
2
#include <iostream> using namespace std; int main(void) { int x; cin>>x; cout<<x/3600<<":"<<x%3600/60<<":"<<x%60<<endl; return 0; }
0
#include <bits/stdc++.h> const int N = 100 + 10; int arr[N]; int main() { int n, m; while (~scanf("%d%d", &n, &m)) { int t = n / m; for (int i = 0; i < m; ++i) arr[i] = t; int num = n - t * m; for (int i = 0; i < num; ++i) arr[i]++; bool first = true; for (int i = 0; i < m; ++i) { if (first) printf("%d", arr[i]); else printf(" %d", arr[i]); first = false; } printf("\n"); } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 1100, INF = 1000000007; int a, b, c, l; long long ans, temp; long long C(long long x) { if (x < 2) return 0; return x * (x - 1) / 2; } int main() { cin >> a >> b >> c >> l; ans = 0; for (int i = 0; i <= l; i++) { int s = a + b + c + i, limit = (a + b + c + i - 1) / 2; if (a > limit || b > limit || c > limit) continue; temp = C(i + 2); temp -= C(i - (limit - a) + 1) + C(i - (limit - b) + 1) + C(i - (limit - c) + 1); ans += temp; } cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool upmin(T &x, T y) { return y <= x ? x = y, 1 : 0; } template <typename T> inline bool upmax(T &x, T y) { return x <= y ? x = y, 1 : 0; } const long double eps = 1e-9; const long double pi = acos(-1); const int oo = 1 << 30; const long long loo = 1ll << 60; const int MAXN = 200005; const int MAXM = 4005; const int MAXP = 7000000; const int MX = 200000; const int G = 3; const int mods = 1e9 + 7; const int Gi = (mods + 1) / G; const int SZ = 62; const int inv2 = (mods + 1) >> 1; const int INF = 0x3f3f3f3f; namespace FastIO { constexpr int SIZE = (1 << 21) + 1; int num = 0, f; char ibuf[SIZE], obuf[SIZE], que[65], *iS, *iT, *oS = obuf, *oT = obuf + SIZE - 1, c; inline void flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; } inline void putc(char c) { *oS++ = c; if (oS == oT) flush(); } inline void getc(char &c) { for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)), (iS == iT ? EOF : *iS++)) : *iS++); c != '.' && c != 'X' && c != EOF; c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)), (iS == iT ? EOF : *iS++)) : *iS++)) ; } inline void reads(char *st) { char c; int n = 0; getc(st[++n]); for (c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)), (iS == iT ? EOF : *iS++)) : *iS++); c == '.' || c == 'X'; c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)), (iS == iT ? EOF : *iS++)) : *iS++)) st[++n] = c; st[++n] = '\0'; } template <class I> inline void read(I &x) { for (f = 1, c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)), (iS == iT ? EOF : *iS++)) : *iS++); c < '0' || c > '9'; c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)), (iS == iT ? EOF : *iS++)) : *iS++)) if (c == '-') f = -1; for (x = 0; c >= '0' && c <= '9'; c = (iS == iT ? (iT = ((iS = ibuf) + fread(ibuf, 1, SIZE, stdin)), (iS == iT ? EOF : *iS++)) : *iS++)) x = (x << 3) + (x << 1) + (c & 15); x *= f; } template <class I> inline void print(I x) { if (x < 0) putc('-'), x = -x; if (!x) putc('0'); while (x) que[++num] = x % 10 + 48, x /= 10; while (num) putc(que[num--]); } inline void putstr(string st) { for (int i = 0; i < (int)st.size(); ++i) putc(st[i]); } struct Flusher_ { ~Flusher_() { flush(); } } io_Flusher_; } // namespace FastIO using FastIO ::getc; using FastIO ::print; using FastIO ::putc; using FastIO ::putstr; using FastIO ::read; using FastIO ::reads; int cnt, num, sz[MAXN], vis[MAXN], tmp[MAXN], Size, MN, id, ans = 1; struct enode { int v, c, d; }; vector<enode> e[MAXN]; struct Node { int x, y, c; } b[MAXN], c[MAXN]; vector<Node> V; int s[MAXN]; void add(int x, int y) { for (; x; x -= x & (-x)) s[x] += y; } int query(int x) { int ans = 0; for (; x <= cnt; x += x & (-x)) ans += s[x]; return ans; } int quick_pow(int x, int y) { int ret = 1; for (; y; y >>= 1) { if (y & 1) ret = 1ll * ret * x % mods; x = 1ll * x * x % mods; } return ret; } void getrt(int x, int father) { int mx = 0; sz[x] = 1; for (auto t : e[x]) { int v = t.v; if (v == father || vis[v]) continue; getrt(v, x); sz[x] += sz[v]; upmax(mx, sz[v]); } upmax(mx, Size - sz[x]); if (mx < MN) MN = mx, id = x; } void dfs(int x, int father, int num0, int num1, int mul) { V.push_back(((Node){num0, num1, mul})); for (auto t : e[x]) { int v = t.v, d = t.d; if (vis[v] || v == father) continue; dfs(v, x, num0 + (d == 0), num1 + (d == 1), 1ll * mul * t.c % mods); } } void getans(int opt) { sort(b + 1, b + num + 1, [&](Node a, Node b) { return a.x < b.x; }); sort(c + 1, c + num + 1, [&](Node a, Node b) { return a.x < b.x; }); sort(tmp + 1, tmp + cnt + 1); cnt = unique(tmp + 1, tmp + cnt + 1) - tmp - 1; for (int i = 1; i <= cnt; ++i) s[i] = 0; for (int i = 1, nw = 1; i <= num; ++i) { while (nw <= num && b[nw].x <= c[i].x) add(lower_bound(tmp + 1, tmp + cnt + 1, b[nw].y) - tmp, 1), ++nw; int p = quick_pow(c[i].c, query(lower_bound(tmp + 1, tmp + cnt + 1, c[i].y) - tmp)); if (opt != 1) p = quick_pow(p, mods - 2); ans = 1ll * ans * p % mods; } } void Solve(int x) { vis[x] = 1; V.clear(), cnt = 0, num = 0; for (auto t : e[x]) { int v = t.v, d = t.d; if (vis[v]) continue; dfs(v, x, (d == 0), (d == 1), t.c); } V.push_back(((Node){0, 0, 1})); for (auto v : V) { ++num; b[num] = (Node){v.y - v.x - v.x, v.y + v.y - v.x, v.c}; c[num] = (Node){v.x + v.x - v.y, v.x - v.y - v.y, v.c}; tmp[++cnt] = v.y + v.y - v.x, tmp[++cnt] = v.x - v.y - v.y; } getans(1); for (auto t : e[x]) { int v = t.v, d = t.d; if (vis[v]) continue; V.clear(), cnt = 0, num = 0; dfs(v, x, (d == 0), (d == 1), t.c); for (auto v : V) { ++num; b[num] = (Node){v.y - v.x - v.x, v.y + v.y - v.x, v.c}; c[num] = (Node){v.x + v.x - v.y, v.x - v.y - v.y, v.c}; tmp[++cnt] = v.y + v.y - v.x, tmp[++cnt] = v.x - v.y - v.y; } getans(-1); } for (auto t : e[x]) { int v = t.v; if (vis[v]) continue; Size = sz[v], MN = INF, id = 0, getrt(v, x), Solve(id); } } signed main() { int n; read(n); for (int i = 1, u, v, c, d; i < n; ++i) read(u), read(v), read(c), read(d), e[u].push_back(((enode){v, c, d})), e[v].push_back(((enode){u, c, d})); Size = n, MN = INF, id = 0, getrt(1, 0), Solve(id); print(ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; void err(istream_iterator<string> it) { cerr << endl; } template <typename T, typename... Args> void err(istream_iterator<string> it, T a, Args... args) { cerr << "[ " << *it << " = " << a << " ] "; err(++it, args...); } void test(int T) { int h, m, s, t1, t2; cin >> h >> m >> s >> t1 >> t2; bool mid[13][13] = {0}; int p, next; mid[h][h] = 1; if (m / 5 == 0) p = 12, next = 1; else p = m / 5, next = p + 1; if (m % 5 == 0) mid[p][p] = 1; else mid[p][next] = 1, mid[next][p] = 1; if (s / 5 == 0) p = 12, next = 1; else p = s / 5, next = p + 1; if (s % 5 == 0) mid[p][p] = 1; else mid[p][next] = 1, mid[next][p] = 1; int i; p = t1; while (1) { if (mid[p][p]) break; next = p + 1; if (next == 13) next = 1; if (!mid[p][next]) p = next; else break; if (p == t2) { cout << "YES" << endl; return; } } p = t1; while (1) { next = p - 1; if (next == 0) next = 12; if (!mid[p][next]) p = next; else break; if (p == t2 and mid[p][p] == 0) { cout << "YES" << endl; return; } if (mid[p][p]) break; } cout << "NO" << endl; } void Test() { int T; scanf("%d\n", &T); for (int cs = 1; cs <= T; cs++) test(cs); } int main() { test(1); return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, m; long long dp[(1 << 19)][20]; int lowbit[(1 << 19)]; bool G[20][20]; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 1; i < (1 << 19); ++i) { int j; for (j = 0;; ++j) { if ((i & (1 << j))) { break; } } lowbit[i] = j; } for (int i = 0; i < m; ++i) { int a, b; cin >> a >> b; G[--a][--b] = 1, G[b][a] = 1; } for (int i = 0; i < n; ++i) { dp[(1 << i)][i] = 1; } for (int mask = 1; mask < (1 << n); ++mask) { for (int i = 0; i < n; ++i) { if (dp[mask][i] > 0) { for (int j = lowbit[mask] + 1; j < n; ++j) { if ((!(mask & (1 << j))) && (G[i][j])) { dp[mask | (1 << j)][j] += dp[mask][i]; } } } } } long long ans = 0; for (int mask = 1; mask < (1 << n); ++mask) { if (__builtin_popcount(mask) >= 3) { for (int i = 0; i < n; ++i) { if (G[i][lowbit[mask]]) { ans += dp[mask][i]; } } } } cout << ans / 2 << '\n'; return 0; }
4
#include <iostream> #define r(i,n) for(int i=0;i<n;i++) using namespace std; int main(){ string s; while(cin>>s){ int a=0,b=0; r(i,s.size()-2){ if(s[i]=='J'&&s[i+1]=='O'&&s[i+2]=='I')a++; if(s[i]=='I'&&s[i+1]=='O'&&s[i+2]=='I')b++; } cout<<a<<endl<<b<<endl; } }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, k, count = 0; cin >> n >> k; int y[n]; for (int i = 0; i < n; i++) { cin >> y[i]; if (y[i] + k <= 5) { count++; } } cout << (count / 3); return 0; }
1
#include <bits/stdc++.h> using namespace std; int p, nr, i, j; int put(int x, int y) { int rez = 1; while (y > 0) if (y % 2 == 0) { x *= x; x %= p; y /= 2; } else { rez *= x; rez %= p; --y; } return (rez); } int main(void) { cin >> p; for (i = 1; i < p; ++i) { bool ok1 = 1; for (j = 1; j < p - 1; ++j) if (put(i, j) == 1) { ok1 = 0; break; } if (ok1 == 1 && put(i, p - 1) == 1) ++nr; } cout << nr; return (0); }
1
#include <bits/stdc++.h> using namespace std; template <typename T, typename T1> T amax(T &a, T1 b) { if (b > a) a = b; return a; } template <typename T, typename T1> T amin(T &a, T1 b) { if (b < a) a = b; return a; } void dfs(vector<long long> adj[], long long i, vector<bool> &visited) { if (visited[i]) return; visited[i] = true; for (long long j = 0; j < adj[i].size(); j++) { if (!visited[adj[i][j]]) dfs(adj, adj[i][j], visited); } } void Chaliye_Shuru_Karte_Hai() { long long n; cin >> n; vector<long long> adj[n + 1]; for (long long i = 0; i < n; i++) { long long a; cin >> a; adj[a].push_back(i + 1); adj[i + 1].push_back(a); } long long ans = 0; vector<bool> visited(n + 1, 0); for (long long i = 1; i < n + 1; i++) { if (!visited[i]) { ans++; dfs(adj, i, visited); } } cout << ans << '\n'; } signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long tc = 1; while (tc--) { Chaliye_Shuru_Karte_Hai(); } }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, k; cin >> n >> k; string s; cin >> s; char ch = 'a'; long long sum = 0; while (sum < k) { for (int i = 0; i < n && sum < k; ++i) { if (s[i] == ch) { ++sum; s[i] = '.'; } } ++ch; } for (char x : s) if (x != '.') cout << x; cout << endl; return 0; }
3
#include<stdio.h> int main(void) { int dp[1000]={0}; int n,i; scanf("%d",&n); dp[0]=1; dp[1]=1; for(i=2;i!=n+1;i++){ dp[i]=dp[i-1]+dp[i-2]; } printf("%d\n",dp[n]); }
0
#include<iostream> using namespace std; int main(){ int a,b; cin>>a>>b; cout<<a*b<<" "<<a+b+a+b<<endl; }
0
#include <bits/stdc++.h> using namespace std; int ln[205][3], n, m, fi[205], c[1050 * 2][3], h[205], li[205], ss = 1, st = 0, ans; bool b[1050 * 2], d[205]; int Ans[205], S, T, sg[205]; inline int Read() { int x = 0; char y; do y = getchar(); while (y < '0' || y > '9'); do x = x * 10 + y - '0', y = getchar(); while (y >= '0' && y <= '9'); return x; } inline void Line(int z, int y, int x) { c[++ss][0] = y; c[ss][1] = fi[x]; c[ss][2] = z; fi[x] = ss; c[++ss][0] = x; c[ss][1] = fi[y]; c[ss][2] = z; fi[y] = ss; } bool BFS() { int le = 1, ri = 1; memset(h, 0, sizeof(h)); h[li[le] = S] = 1; for (; le <= ri; le++) for (int i = fi[li[le]]; i; i = c[i][1]) if (c[i][2] && !h[c[i][0]]) h[li[++ri] = c[i][0]] = h[li[le]] + 1; return h[T] > 0; } int DFS(int x, int y) { int k, l = 0; if (x == T) return y; for (int i = fi[x]; i && y; i = c[i][1]) if (c[i][2] && h[c[i][0]] == h[x] + 1) { k = DFS(c[i][0], min(y, c[i][2])); if (!k) continue; l += k; y -= k; c[i][2] -= k; c[i ^ 1][2] += k; } return l; } int Dinic() { int cnt = 0; while (BFS()) cnt += DFS(S, 0x3f7f7f7f); return cnt; } void DAC(int x, int y) { if (x == y) { b[sg[x]] = 1; return; } S = sg[x]; T = sg[y]; ln[++st][0] = S; ln[st][1] = T; ln[st][2] = Dinic(); int le = x, ri = x, now = x - 1; li[le] = S; memset(d, 0, sizeof(d)); d[S] = 1; for (; le <= ri; le++) for (int i = fi[li[le]]; i; i = c[i][1]) if (!d[c[i][0]] && c[i][2]) d[li[++ri] = c[i][0]] = 1; for (int i = x; i <= y; i++) if (!d[sg[i]]) swap(sg[++now], sg[i]); for (int i = 2; i <= ss; i += 2) c[i][2] += c[i + 1][2], c[i + 1][2] = (c[i][2] >>= 1); for (int i = now + 1; i <= y; i++) b[sg[i]] = 1; DAC(x, now); for (int i = x; i <= now; i++) b[sg[i]] = 1; for (int i = now + 1; i <= y; i++) b[sg[i]] = 0; DAC(now + 1, y); for (int i = now + 1; i <= y; i++) b[sg[i]] = 1; } void Solve(int x, int y) { int le = 1, ri = 1, ma = 0x3f7f7f7f, now = x - 1; li[1] = sg[x]; d[sg[x]] = 1; if (x == y) { Ans[++st] = sg[x]; b[sg[x]] = 1; return; } memset(d, 0, sizeof(d)); for (; le <= ri; le++) for (int i = fi[li[le]]; i; i = c[i][1]) if (!b[c[i][0]] && !d[c[i][0]]) { d[li[++ri] = c[i][0]] = 1; if (c[i][2] < ma) ma = c[i][2], S = li[le], T = c[i][0]; } le = ri = 1; li[1] = T; ans += ma; b[T] = 1; for (; le <= ri; le++) for (int i = fi[li[le]]; i; i = c[i][1]) if (!b[c[i][0]] && c[i][0] != S) b[li[++ri] = c[i][0]] = 1; for (int i = x; i <= y; i++) if (!b[sg[i]]) swap(sg[++now], sg[i]); Solve(x, now); for (int i = x; i <= now; i++) b[sg[i]] = 1; for (int i = now + 1; i <= y; i++) b[sg[i]] = 0; Solve(now + 1, y); for (int i = now + 1; i <= y; i++) b[sg[i]] = 1; } int main() { n = Read(); m = Read(); ss = 1; for (int i = 1; i <= m; i++) Line(Read(), Read(), Read()); for (int i = 1; i <= n; i++) d[sg[i] = i] = 1; DAC(1, n); memset(fi, 0, sizeof(fi)); ss = 1; for (int i = 1; i < n; i++) Line(ln[i][2], ln[i][1], ln[i][0]), c[ss][2] = ln[i][2]; for (int i = 1; i <= n; i++) sg[i] = i; memset(b, 0, sizeof(b)); st = 0; Solve(1, n); cout << ans << endl; for (int i = 1; i <= n; i++) printf("%d ", Ans[i]); puts(""); return 0; }
5
#include<iostream> #include<queue> using namespace std; #define rep(i, n) for (int i = 0 ;i < int(n); ++i) struct edge{int to, len, den;}; vector<edge> ed[111]; int ee[111][111]; int main() { int n, m, l; for (;;) { cin >> n >> m >> l; if (n == 0 && m == 0 && l == 0) break; rep (i, 111) ed[i].clear(); rep (i, m) { int a, b, c, d; cin >> a >> b >> c >> d; --a;--b; edge e; e.to = b; e.len = c; e.den = d; ed[a].push_back(e); e.to = a; ed[b].push_back(e); } rep (i, 111) rep(j, 111) ee[i][j] = 1000000000; priority_queue<pair<int, pair<int,int> >, vector<pair<int, pair<int,int> > >, greater<pair<int, pair<int,int> > > > que; que.push(make_pair(0,make_pair(0,l))); while(!que.empty()){ pair<int, pair<int,int> > now = que.top(); que.pop(); int den = now.first; int x = now.second.first; int money = now.second.second; if (ee[x][money] <= den) continue; //cout << den << " " << x <<" "<< money << endl; rep (i, money + 1) ee[x][i] = min(den, ee[x][i]); rep (i, ed[x].size()) { que.push(make_pair(den + ed[x][i].den, make_pair(ed[x][i].to, money))); if (money >= ed[x][i].len) que.push(make_pair(den, make_pair(ed[x][i].to, money - ed[x][i].len))); } } cout << ee[n-1][0] << endl; } }
0
#include <bits/stdc++.h> using namespace std; class point { public: double x, y; point() {} point(double a, double b) { x = a; y = b; } double dis(point a) { return (a.x - x) * (a.x - x) + (a.y - y) * (a.y - y); } double tri(point a, point b) { a.x -= x; b.x -= x; a.y -= y; b.y -= y; return 0.5 * fabs(a.x * b.y - a.y * b.x); } }; void sf(int &x) { scanf("%d", &x); } void sf(long long &x) { scanf("%lld", &x); } void sf(long long &x, long long &y) { scanf("%lld%lld", &x, &y); } void sf(float &x) { scanf("%f", &x); } void sf(double &x) { scanf("%lf", &x); } void sf(int &x, int &y) { scanf("%d%d", &x, &y); } void sf(float &x, float &y) { scanf("%f%f", &x, &y); } void sf(double &x, double &y) { scanf("%lf%lf", &x, &y); } void sf(double &x, double &y, double &z) { scanf("%lf%lf%lf", &x, &y, &z); } void sf(int &x, int &y, int &z) { scanf("%d%d%d", &x, &y, &z); } void sf(long long &x, long long &y, long long &z) { scanf("%lld%lld%lld", &x, &y, &z); } void sf(float &x, float &y, float &z) { scanf("%u%u%u", &x, &y, &z); } void sf(char &x) { x = getchar(); } void sf(char *s) { scanf("%s", s); } void sf(string &s) { cin >> s; } void sf(point &x) { double a, b; sf(a, b); x = point(a, b); } void pf(int x) { printf("%d\n", x); } void pf(int x, int y) { printf("%d %d\n", x, y); } void pf(int x, int y, int z) { printf("%d %d %d\n", x, y, z); } void pf(long long x) { printf("%lld\n", x); } void pf(long long x, long long y) { printf("%lld %lld\n", x, y); } void pf(long long x, long long y, long long z) { printf("%lld %lld %lld\n", x, y, z); } void pf(float x) { printf("%u\n", x); } void pf(double x) { printf("%.9lf\n", x); } void pf(double x, double y) { printf("%.5lf %.5lf\n", x, y); } void pf(char x) { printf("%c\n", x); } void pf(char *x) { printf("%s\n", x); } void pf(string x) { cout << x; puts(""); } template <class T> T bigmod(T b, T p, T m) { if (p == 0) return 1 % m; T x = b; T ans = 1; while (p) { if (p & 1) ans = (ans * x) % m; p >>= 1; x = (x * x) % m; } return ans; } template <class T> T gcd(T x, T y) { if (y == 0) return x; return gcd(y, x % y); } template <typename T> T POW(T b, T p) { if (p == 0) return 1; if (p == 1) return b; if (p % 2 == 0) { T s = POW(b, p / 2); return s * s; } return b * POW(b, p - 1); } template <typename T> T modinv(T num, T m) { return bigmod(num, m - 2, m); } int n, a[2000009], b[2000009]; int main() { sf(n); int posA = 0, posB = 0, la = 0, lb = 0; for (int i = 0; i < n; i++) { int in; sf(in); if (in != 0) { a[la] = in; la++; } } for (int i = 0; i < n; i++) { int in; sf(in); if (in != 0) { b[lb] = in; if (b[lb] == a[0]) { posB = lb; } lb++; } } int nn = n; while (nn--) { if (a[posA] != b[posB]) { pf("NO"); return 0; } posA = (posA + 1 == la) ? 0 : posA + 1; posB = (posB + 1 == lb) ? 0 : posB + 1; } pf("YES"); return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long INF = 1e6 + 5; const long long MAX = 1e7 + 5; const double pi = 3.14159265359; bitset<10000007> bs; vector<long long> prims; void sieve(long long n) { bs.set(); bs[0] = 0; bs[1] = 0; for (long long i = 2; i < n; i++) { if (bs[i]) { prims.push_back(i); for (long long j = i * i; j < n; j += i) bs[j] = 0; } } } bool isPrime(long long a) { if (a < 10000000) return bs[a]; long long b = sqrt(a) + 2; for (int i = 0; i < prims.size(); i++) { if (prims[i] > b) return true; else if (a % prims[i] == 0) return false; } } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); long long n; cin >> n; sieve(10000000); if (isPrime(n) || n == 1) { return cout << 1 << "\n" << 0, 0; } vector<long long> pr; int i = 0; while (n > 1) { long long div = prims[i++]; while (n > 1 && n % div == 0) { n /= div; pr.push_back(div); } if (i == (int)prims.size()) { pr.push_back(n); break; } } if (pr.size() == 2) { cout << 2; } else { cout << 1 << endl; cout << pr[0] * pr[1]; } }
3
#include <bits/stdc++.h> using namespace std; void RI() {} template <typename... T> void RI(int& head, T&... tail) { scanf("%d", &head); RI(tail...); } const int N = 110; int n, m; char s[3][N]; bool vis[3][N]; bool ok(int x, int y) { if (x < 0 || x >= 3) return 0; if (y >= n) return 1; char c = s[x][y]; return c == 's' || c == '.'; } bool go(int x, int y) { if (y >= n) return 1; if (vis[x][y]) return 0; vis[x][y] = 1; if (!ok(x, y + 1)) return 0; y++; for (int i = (-1); i <= int(+1); i++) { int nx = x + i; int ny = y; if (!ok(nx, ny)) continue; ny++; if (!ok(nx, ny)) continue; ny++; if (!ok(nx, ny)) continue; if (go(nx, ny)) return 1; } return 0; } int main() { int t; RI(t); while (t--) { memset(s, 0, sizeof(s)); RI(n, m); for (int i = 0; i < int(3); i++) scanf("%s", s[i]); int sx = 0, sy = 0; while (sx < 3 && s[sx][sy] != 's') sx++; assert(sx < 3); memset(vis, 0, sizeof(vis)); puts(go(sx, sy) ? "YES" : "NO"); } return 0; }
2
#include <bits/stdc++.h> using namespace std; struct candy { int t; int h, m; bool eat; }; candy c[2005]; int n, x; bool cmp(candy a, candy b) { if (a.m == b.m) return a.h > b.h; else return a.m > b.m; } int solve(int which) { for (int i = 0; i < n; i++) c[i].eat = false; int status = which, energy = x; int ans = 0; bool flag = false; for (int i = 0; i < n; i++) { if (c[i].t == which && c[i].h <= energy) { flag = true; c[i].eat = true; ans++; energy += c[i].m; break; } } if (!flag) return 0; else { flag = true; while (flag) { flag = false; for (int i = 0; i < n && !flag; i++) { if (!c[i].eat && c[i].t != status && c[i].h <= energy) { flag = true; c[i].eat = true; ans++; status = 1 - status; energy += c[i].m; } } } return ans; } } int main() { while (~scanf("%d%d", &n, &x)) { for (int i = 0; i < n; i++) { scanf("%d%d%d", &c[i].t, &c[i].h, &c[i].m); c[i].eat = false; } sort(c, c + n, cmp); int a1 = solve(0), a2 = solve(1); printf("%d\n", a1 > a2 ? a1 : a2); } return 0; }
1
#include <cstdio> #include <iostream> #include <algorithm> using namespace std; int f[120][120],s[10000],sp,dis[120]; bool b[120]; const int maxdis=1000000000; int main(){ int n,m; while(scanf("%d%d",&n,&m)==2){ if(n==0&&m==0) break; int i,j,k; for(i=1;i<=n;i++){ for(j=1;j<=n;j++) f[i][j]=0; } int v1,v2,e;sp=0; for(i=0;i<m;i++) scanf("%d%d%d",&v1,&v2,&e),f[v1][v2]=e,f[v2][v1]=e,s[sp++]=e; sort(s,s+sp); int diff=maxdis; for(i=0;i<sp;i++){ if(i&&s[i]==s[i-1]) continue; bool bb=1; for(j=2;j<=n;j++) b[j]=0;b[1]=1; for(j=2;j<=n;j++){ if(f[1][j]>=s[i]) dis[j]=f[1][j]; else dis[j]=maxdis; } int minind,mindis,maxd=0,mind=maxdis; for(j=2;j<=n;j++){ mindis=maxdis; for(k=1;k<=n;k++){ if(b[k]) continue; if(mindis>dis[k]&&dis[k]>=s[i]) mindis=dis[k],minind=k; } if(mindis==maxdis){bb=0;break;} if(maxd<mindis) maxd=mindis; if(mind>mindis) mind=mindis; b[minind]=1; for(k=1;k<=n;k++){ if(b[k]) continue; if(dis[k]>f[k][minind]&&f[k][minind]>=s[i]) dis[k]=f[k][minind]; } } if(!bb) break; if(diff>maxd-mind) diff=maxd-mind; } if(!i) puts("-1"); else printf("%d\n",diff); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1505; int a[N], b[N], c[N * N]; struct node { int l, r; node(int x, int y) { l = x, r = y; } }; vector<node> v[N * N / 2]; int n; int main() { std::ios::sync_with_stdio(false); ; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; b[i] = b[i - 1] + a[i]; } int cnt = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j < i; j++) { c[++cnt] = b[i] - b[j]; } } sort(c + 1, c + 1 + cnt); cnt = unique(c + 1, c + 1 + cnt) - c - 1; for (int i = 1; i <= n; i++) { for (int j = 0; j < i; j++) { int x = b[i] - b[j]; int k = lower_bound(c + 1, c + 1 + cnt, x) - c; v[k].push_back(node(j + 1, i)); } } int ans = 0, id = 0; for (int i = 1; i <= cnt; i++) { int res = 1; int p = v[i][0].r; for (int j = 1; j < v[i].size(); j++) { if (v[i][j].l > p) { res++; p = v[i][j].r; } } if (res > ans) { id = i; ans = res; } } cout << ans << endl; int p = 0; for (int i = 0; i < v[id].size(); i++) { if (v[id][i].l > p) { cout << v[id][i].l << " " << v[id][i].r << endl; p = v[id][i].r; } } }
6
#include <bits/stdc++.h> using namespace std; long long n, k, ans = 1; int main() { scanf("%lld %lld", &n, &k); if (k >= 2) ans += n * (n - 1) / 2; if (k >= 3) ans += 2 * n * (n - 1) * (n - 2) / 6; if (k >= 4) ans += 9 * n * (n - 1) * (n - 2) * (n - 3) / 24; printf("%lld\n", ans); }
4
#include<bits/stdc++.h> #define int long long using namespace std; signed main() { int H, W, Cy, Cx, Dy, Dx; cin >> H >> W >> Cy >> Cx >> Dy >> Dx; --Dy, --Dx, --Cy, --Cx; vector<string> S(H); for (auto& s : S) cin >> s; const int inf = 1e18; vector<vector<int>> g(H, vector<int> (W, inf)); deque<pair<int, int>> q; q.push_front({Cy, Cx}); g[Cy][Cx] = 0; while (q.size()) { auto p = q.front(); q.pop_front(); int y = p.first; int x = p.second; for (int i = -2; i <= 2; i++) { for (int j = -2; j <= 2; j++) { int d = abs(i) + abs(j); int ny = y + i; int nx = x + j; if (d == 0) continue; if (ny < 0 || ny >= H) continue; if (nx < 0 || nx >= W) continue; if (S[ny][nx] != '.') continue; if (d == 1 && g[ny][nx] > g[y][x]) { g[ny][nx] = g[y][x]; q.push_front({ny, nx}); } else if (g[ny][nx] == inf) { g[ny][nx] = g[y][x] + 1; q.push_back({ny, nx}); } } } } cout << (g[Dy][Dx] == inf ? -1 : g[Dy][Dx]) << endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; for (int i = 0; i < n; i++) cout << i % 2; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAXN = 100002; int main() { int vec[MAXN]; int sort1[MAXN]; int sort2[MAXN]; int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &vec[i]); sort1[i] = sort2[i] = vec[i]; } if (n <= 2) { printf("-1\n"); return 0; } sort(sort1, sort1 + n); sort(sort2, sort2 + n, greater<int>()); for (int i = 1; i < n; ++i) if (!((vec[i] == sort1[i - 1] && vec[i - 1] == sort1[i]) || (vec[i] == sort2[i - 1] && vec[i - 1] == sort2[i]) || (vec[i] == vec[i - 1]))) { printf("%d %d", i, i + 1); return 0; } printf("-1\n"); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 100005; vector<int> e[N]; int n, ans, f[N][2], g[N][2]; void dfs1(int x, int fa) { f[x][0] = 0; f[x][1] = 0; int d = e[x].size() - (fa != 0); for (auto i : e[x]) if (i != fa) { dfs1(i, x); f[x][0] = max(f[x][0], max(f[i][0], f[i][1]) - 1); f[x][1] = max(f[x][1], f[i][0]); } f[x][0] += d; f[x][1]++; } void upd(pair<int, int> &x, pair<int, int> &y, pair<int, int> z) { if (z > x) swap(x, z); y = max(y, z); } void dfs2(int x, int fa) { if (!fa) g[x][0] = 0, g[x][1] = 0; pair<int, int> mx1(0, x), mx2(-1, -1), mx3(0, x), mx4(-1, -1); upd(mx1, mx2, pair<int, int>(max(g[x][0], g[x][1]) - 1, -1)); upd(mx3, mx4, pair<int, int>(g[x][0], -1)); for (auto i : e[x]) if (i != fa) { upd(mx1, mx2, pair<int, int>(max(f[i][0], f[i][1]) - 1, i)); upd(mx3, mx4, pair<int, int>(f[i][0], i)); } int d = e[x].size() - 1; ans = max(ans, max(mx1.first + d + 1, mx3.first)); for (auto i : e[x]) if (i != fa) { g[i][0] = (mx1.second == i ? mx2.first : mx1.first) + d; g[i][1] = (mx3.second == i ? mx4.first : mx3.first) + 1; dfs2(i, x); } } int main() { srand(time(NULL)); scanf("%d", &n); for (int i = (int)(1); i <= (int)(n - 1); i++) { int x, y; scanf("%d%d", &x, &y); e[x].push_back(y); e[y].push_back(x); } dfs1(1, 0); dfs2(1, 0); printf("%d\n", ans); }
4
#include <bits/stdc++.h> using namespace std; int main() { long n; cin >> n; int m = 0; while (n > 2) { int x = log2(n); n -= pow(2, x); if (n == 0) m--; m++; } cout << m + 1; }
1
#include <bits/stdc++.h> using namespace std; const int M = 500000; int n, m, t; int a[M], ans[M]; struct edge { int u, next, last; } e[M]; void add(int u, int v) { t++; e[t].u = v; e[t].next = e[u].last; e[u].last = t; } int main() { int x, y; t = 0; priority_queue<int> q; scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) { scanf("%d%d", &x, &y); a[x]++; add(y, x); } for (int i = 1; i <= n; i++) if (a[i] == 0) q.push(i); int j = n; while (q.size()) { x = q.top(); q.pop(); ans[x] = j--; for (int i = e[x].last; i; i = e[i].next) { y = e[i].u; if (a[y] > 0) a[y]--; if (a[y] == 0) q.push(y); } } for (int i = 1; i < n; i++) printf("%d ", ans[i]); printf("%d\n", ans[n]); return 0; }
5
#include<bits/stdc++.h> using namespace std; #define INF 100000000 int po[8]; int t; int result; vector<int> ans; vector<int> anss; int Stoi(string s){ if(s == "") return 0; else{ int res = 0; int n = (int)s.size(); n--; for(int i = 0; i < s.size(); i++){ res += (int)(s[i] - '0') * po[n]; n--; } return res; } } void rec(vector<int>& a, string num, int idx, string tmp, int sum){ tmp += num[idx]; if(idx == (int)num.size() - 1){ ans.push_back(Stoi(tmp)); sum += Stoi(tmp); if(sum <= t){ if(result == sum){ a.push_back(sum); }else if(result < sum || result == INF){ result = sum; anss = ans; } } ans.pop_back(); return; } //切らない rec(a, num, idx + 1, tmp, sum); //切る ans.push_back(Stoi(tmp)); sum += Stoi(tmp); tmp = ""; rec(a, num, idx + 1, tmp, sum); ans.pop_back(); return; } int main(){ po[0] = 1; for(int i = 1; i < 8; i++){ po[i] = po[i - 1] * 10; } //cout << Stoi("157") << " " << Stoi("") << endl; while(1){ string num; cin >> t >> num; if(!t) break; vector<int> a; result = INF; rec(a, num, 0, "", 0); for(int i = 0; i < a.size(); i++){ if(result == a[i]){ result = -1; break; } } if(result == INF) cout << "error" << endl; else if(result == -1) cout << "rejected" << endl; else{ cout << result << " "; for(int i = 0; i < (int)anss.size(); i++){ if(i == (int)anss.size() - 1) cout << anss[i] << endl; else cout << anss[i] << " "; } } } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long N, K; long long f[15]; int count(long long x, long long k) { if (x > k) return 0; return 1 + count(10 * x + 4, k) + count(10 * x + 7, k); } bool check(long long x) { if (x == 0) return false; while (x) { long long r = x % 10; if (r != 4 && r != 7) return false; x /= 10; } return true; } int main() { cin >> N >> K; f[1] = 1; for (int i = 2; i <= 14; i++) f[i] = f[i - 1] * i; if (N <= 14 && K > f[N]) { cout << -1 << endl; return 0; } int ans = count(0, max(N - 13, 0ll)) - 1; vector<int> t; for (int i = N - 12; i <= N; i++) t.push_back(i); K--; for (int i = 13; i > 1 && t.size() > 0; i--) { int tmp = K / f[i - 1]; if (check(N - i + 1) && check(t[tmp])) ans++; t.erase(t.begin() + tmp); K %= f[i - 1]; } if (check(N) && check(t[0])) ans++; cout << ans << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; int ans[12 * MAXN]; int a[MAXN], b[MAXN], prime[MAXN], k = 0; void ini() { memset(prime, 0, sizeof(prime)); for (int i = 2; i < MAXN; i++) if (!prime[i]) { for (int j = i + i; j < MAXN; j += i) prime[j] = 1; } } void solve(int i, int j) { if (j > i) { while (j > i) { if (!prime[j - i + 1]) { b[a[i]] = j; swap(a[i], a[j]); ans[k++] = i; ans[k++] = j; break; } if (!prime[j - i + 1 - 1]) { b[a[j - 1]] = j; swap(a[j], a[j - 1]); ans[k++] = j - 1; ans[k++] = j; j -= 1; b[a[i]] = j; swap(a[i], a[j]); ans[k++] = i; ans[k++] = j; break; } else if (!prime[j - i + 1 - 2]) { b[a[j - 2]] = j; swap(a[j], a[j - 2]); ans[k++] = j - 2; ans[k++] = j; j -= 2; b[a[i]] = j; swap(a[i], a[j]); ans[k++] = i; ans[k++] = j; break; } b[a[j - 4]] = j; swap(a[j], a[j - 4]); ans[k++] = j - 4; ans[k++] = j; j -= 4; } } else if (j < i) { while (j < i) { if (!prime[i - j + 1]) { b[a[i]] = j; swap(a[i], a[j]); ans[k++] = j; ans[k++] = i; break; } if (!prime[i - j + 1 - 1]) { b[a[j + 1]] = j; swap(a[j], a[j + 1]); ans[k++] = j; ans[k++] = j + 1; j += 1; b[a[i]] = j; swap(a[i], a[j]); ans[k++] = j; ans[k++] = i; break; } else if (!prime[i - j + 1 - 2]) { b[a[j + 2]] = j; swap(a[j], a[j + 2]); ans[k++] = j; ans[k++] = j + 2; j += 2; b[a[i]] = j; swap(a[i], a[j]); ans[k++] = j; ans[k++] = i; break; } b[a[j + 4]] = j; swap(a[j + 4], a[j]); ans[k++] = j; ans[k++] = j + 4; j += 4; } } } int main() { ini(); int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); b[a[i]] = i; } for (int i = 1; i <= n; i++) { int j = b[i]; if (i != j) solve(i, j); } printf("%d\n", k / 2); for (int i = 0; i < k; i += 2) printf("%d %d\n", ans[i], ans[i + 1]); }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, l, r, ql, qr; cin >> n >> l >> r >> ql >> qr; int w[n]; int sum = 0; for (int i = 0; i < n; i++) { cin >> w[i]; sum += w[i]; } int cur = 0; int ans = 2000000000; for (int i = 0; i <= n; i++) { int k = l * cur + r * (sum - cur); if (i > n - i) { k += ql * (2 * i - n - 1); } else if (i < n - i) { k += qr * (n - 2 * i - 1); } ans = min(ans, k); cur += w[i]; } cout << ans << endl; }
1
#include <bits/stdc++.h> using namespace std; int main() { string s; int ren=0; cin >> s; vector<int> ans(s.size()); for(int i=0;i<s.size();i++) { ren++; if(s[i]=='L') { if(ren%2==0) ans[i-ren]++; else ans[i-ren+1]++; } if(i+1==s.size()) continue; if(s[i]=='R'&&s[i+1]=='L') { ans[i]+=ren-(ren/2); ans[i+1]+=ren/2; ren=0; } if(s[i]=='L'&&s[i+1]=='R') ren=0; } for(int i=0;i<s.size();i++) cout << ans.at(i) << " "; cout << endl; }
0
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-9; const long long mod = 1e9 + 7; const int max_n = 1000100; bool vis[100100] = {0}; int par[100100]; vector<int> g[100100]; bool ok; void dfs(int x) { vis[x] = 1; for (int i = 0; i < g[x].size(); i++) { if (!vis[g[x][i]]) { par[g[x][i]] = x; dfs(g[x][i]); } else { if (g[x][i] != par[x]) ok = true; } } } int main() { int n, m; cin >> n >> m; int x, y; for (int i = 0; i < m; i++) { cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } int ans = 0; for (int i = 1; i <= n; i++) { ok = false; if (!vis[i]) { par[i] = -1; dfs(i); if (!ok) ++ans; } } cout << ans << endl; }
5
#include <bits/stdc++.h> using namespace std; long long n, m, k, mod = 1e9 + 7; long long power(long long x, long long n) { if (n == 0) return 1; long long p = power(x, n / 2); p = (p * p) % mod; if (n % 2) return (p * x) % mod; return p; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> m >> k; if ((n ^ m) & 1 && k == -1) cout << "0"; else cout << power(power(2, m - 1), n - 1); }
2
#include <iostream> #include <map> #include <vector> #include <string> #include <algorithm> using namespace std; int main(){ map<string,vector<int> > m; map<string,vector<int> >::iterator it; vector<int>::iterator itv; string s;int a; while(cin>>s){ cin>>a; m[s].push_back(a); } for(it=m.begin();it!=m.end();it++){ cout<<it->first<<endl; sort(it->second.begin(),it->second.end()); for(itv=it->second.begin();itv!=it->second.end() - 1;itv++)cout << *itv << ' '; cout << *itv << endl; } }
0
#include <bits/stdc++.h> int main() { int i, q[100], b, c; scanf("%d", &b); for (i = 0; i < b; i++) { scanf("%d", &q[i]); if (q[i] == 1) c = 0; } if (c == 0) printf("HARD"); else printf("EASY"); return 0; }
1
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; long long mulmod(long long a, long long b, long long MOD) { if (b == 0) return 0; long long res = mulmod(a, b >> 1, MOD); res += res; res %= MOD; return (b & 1) ? (a + res) % MOD : res; } int gcd(int a, int b) { while (a != 0 && b != 0) { if (a > b) { a %= b; } else { b %= a; } } return a + b; } long long gcd(long long a, long long b) { while (a != 0 && b != 0) { if (a > b) { a %= b; } else { b %= a; } } return a + b; } int main() { ios::sync_with_stdio(0); string s; cin >> s; int n = s.length(); int a[300]; memset(a, 0, sizeof(a)); for (int i = 0; i < n; i++) { a[s[i]]++; } int ans = 1; bool pr[1013]; memset(pr, 0, sizeof(pr)); for (int i = 3; i <= n; i += 2) { bool pp = 1; for (int j = 2; j * j <= i; j++) { if (i % j == 0) { pp = 0; break; } } if (pp) { if (i * 2 > n) { ans++; pr[i] = 1; } } } pr[1] = 0; char aans[1013]; memset(aans, 0, sizeof(aans)); aans[n] = 0; bool qq = 0; for (int i = 0; i < 300; i++) { if (a[i] >= n - ans) { for (int j = 2; j <= n; j++) { if (!pr[j]) { aans[j - 1] = i; a[i]--; } } qq = 1; break; } } if (!qq) { cout << "NO"; return 0; } for (int i = 0; i < 300; i++) { for (int j = 0; j < n; j++) { if (a[i] == 0) { break; } if (aans[j] == 0) { aans[j] = i; a[i]--; } } } cout << "YES" << endl; cout << aans; return 0; }
1
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-8; const double PI = acos(-1.0); int popcount(int n) { return __builtin_popcount(n); } int popcount(long long n) { return __builtin_popcountll(n); } template <class T> int SIZE(T a) { return a.size(); } template <class T> string IntToString(T num) { string res; stringstream ss; ss << num; return ss.str(); } template <class T> T gcd(T a, T b) { if (b == 0) return a; return gcd(b, a % b); } template <class T> T lcm(T a, T b) { return a / gcd(a, b) * b; } template <class T> void PrintSeq(T &a, int sz) { for (int i = 0; i < sz; i++) { cout << a[i]; if (sz == i + 1) cout << endl; else cout << ' '; } } bool EQ(double a, double b) { return abs(a - b) < EPS; } void fastStream() { cin.tie(0); std::ios_base::sync_with_stdio(0); } vector<string> split(string str, char del) { vector<string> res; for (int i = 0, s = 0; i < SIZE(str); i++) { if (str[i] == del) { if (i - s != 0) res.push_back(str.substr(s, i - s)); s = i + 1; } else if (i == SIZE(str) - 1) { res.push_back(str.substr(s)); } } return res; } int H, W, K; char field[501][501]; bool downFound[501][501]; bool rightFound[501][501]; int xSums[501][501]; int ySums[501][501]; const int dy[] = {0, -1, 0, 1, 0}, dx[] = {0, 0, 1, 0, -1}; bool foundStar(int cy, int cx) { for (int i = 0; i < 5; i++) { int ny = cy + dy[i]; int nx = cx + dx[i]; if (!(ny >= 0 && nx >= 0 && ny < H && nx < W && field[ny][nx] == '1')) return false; } return true; } int main() { cin >> H >> W >> K; for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) cin >> field[i][j]; for (int i = 0; i < H; i++) for (int j = 2; j < W; j++) rightFound[i][j] = (foundStar(i, j - 1)); for (int i = 2; i < H; i++) for (int j = 0; j < W; j++) downFound[i][j] = (foundStar(i - 1, j)); for (int i = 0; i < H; i++) for (int j = 0; j < W; j++) xSums[i][j + 1] = xSums[i][j] + downFound[i][j]; for (int j = 0; j < W; j++) for (int i = 0; i < H; i++) ySums[i + 1][j] = ySums[i][j] + rightFound[i][j]; long long res = 0; for (int miny = 0; miny < H; miny++) { for (int minx = 0; minx < W; minx++) { int maxy = miny + 1; int cnt = 0; for (int maxx = W - 1; maxx >= minx + 1; maxx--) { while (maxy < H && cnt < K) { maxy++; cnt += xSums[maxy][maxx] - xSums[maxy][minx + 1]; } if (maxy == H) { if (cnt >= K) res++; } else res += (H - maxy); cnt -= ySums[maxy][maxx] - ySums[miny + 1][maxx]; } } } cout << res << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; long long power(long long n) { long long res = 1ll; long long m = 2ll; while (n > 0) { if (n & 1) res = (res * m) % 1000000007; m = (m * m) % 1000000007; n = n >> 1; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s; cin >> s; int n = s.length(); long long ans = 0; long long a = 0, b = 0; for (int i = 0; i < n;) { while (i >= 0 && s[i] == 'a') { a++; i++; } while (i >= 0 && s[i] == 'b') { b++; i++; } ans = (ans + ((power(a) - 1) * b) % 1000000007) % 1000000007; b = 0; } cout << ans; }
4
#include<iostream> #include<vector> #include<algorithm> #include<utility> #include<string> #include<cmath> #include<cstring> #include<queue> #include<map> #include<climits> #include<set> #include<tuple> #include<stack> using namespace std; typedef pair<int, int> pii; typedef long long int ll; typedef pair<ll, ll> pll; typedef tuple<int, int, int> tpl; int dx[4] = { 1,0,0,-1 }; int dy[4] = { 0,1,-1,0 }; #define ARRAY_MAX 100005 const int INF = 1e9 + 7; const int MOD = 1e9 + 7; bool used[30]; int main() { int N, M, C; cin >> N >> M >> C; vector<int> num(ARRAY_MAX); vector<int> cnt(ARRAY_MAX, 0); for (int i = 1; i <= C; i++) { cin >> num[i]; } priority_queue<pii> que; for (int i = 0; i < N; i++) { int color, value; cin >> color >> value; que.push(pii(value, color)); } int sum = 0; int ans = 0; while (sum < M && que.size()) { int color = que.top().second;//色 int value = que.top().first;//価値 que.pop(); if (cnt[color] < num[color]) { cnt[color]++; sum++; ans += value; } } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; long long a[200002], b[200002], c[200002]; int n; long long temp; long long f(long long x) { if (c[x] != 0) { return c[x]; } long long xx = x; if (b[x] == 1) { c[x] = -1; return -1; } b[x] = 1; long long y = a[x]; x += a[x]; if (x > n) { c[xx] = y; b[xx] = 0; return y; } y += a[x]; x -= a[x]; if (x <= 0) { c[xx] = y; b[xx] = 0; return y; } else if (x == 1) { c[xx] = -1; b[xx] = 0; return -1; } temp = f(x); if (temp != -1) { c[xx] = temp + y; b[xx] = 0; return temp + y; } else { c[xx] = -1; b[xx] = 0; return -1; } } int main() { long long x, y; int i, j; scanf("%d", &n); for (i = 2; i <= n; i++) { cin >> a[i]; b[i] = 0; c[i] = 0; } b[1] = b[0] = 0; c[1] = c[0] = 0; for (i = 2; i <= n; i++) { f(i); } for (i = 1; i < n; i++) { x = 1; y = 0; j = 0; a[1] = i; y += a[x]; x += a[x]; if (x > n) { cout << y << endl; continue; } y += a[x]; x -= a[x]; if (x <= 0) { cout << y << endl; continue; } else if (x == 1) { cout << "-1\n"; continue; } if (c[x] == -1) { cout << "-1\n"; } else { cout << c[x] + y << endl; } } }
4
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int hf = 999983; const int N = 1e6; vector<int> g[N]; int W[N], D[N]; multiset<pair<int, long long> > dp[N]; multiset<pair<int, long long> >::iterator its, its2; void dfs(int u, int p) { for (int i = 0; i < g[u].size(); i++) { int v = g[u][i]; if (v == p) continue; dfs(v, u); if (dp[u].size() < dp[v].size()) swap(dp[u], dp[v]); for (its = dp[v].begin(); its != dp[v].end(); its++) dp[u].insert(*its); } if (W[u] != 0) { long long w = W[u]; int d = D[u]; dp[u].insert(pair<int, long long>(d, w)); its = dp[u].upper_bound(pair<int, long long>(d, 1e18)); while (its != dp[u].end()) { its2 = its; its2++; if (its->second <= w) { w -= its->second; dp[u].erase(its); } else { d = its->first; w = its->second - w; dp[u].erase(its); dp[u].insert(pair<int, long long>(d, w)); break; } its = its2; } } } int main() { int n, m, k, p; scanf("%d%d%d", &n, &m, &k); for (int i = 2; i <= n; i++) { scanf("%d", &p); g[i].push_back(p); g[p].push_back(i); } for (int i = 1; i <= m; i++) { int x, d, w; scanf("%d%d%d", &x, &d, &w); W[x] = w; D[x] = d; } dfs(1, 0); long long ans = 0; for (its = dp[1].begin(); its != dp[1].end(); its++) ans += its->second; printf("%lld\n", ans); }
2
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:17000000") const double eps = 1e-9; const double inf = 1e+9; int n; string s; string q[100009]; long long mod = 1000000007; vector<long long> a(10); vector<long long> d(10); int main() { cin >> s >> n; for (int i = 0; i < n; i++) { cin >> q[i]; } for (int i = 0; i < 10; i++) { a[i] = i; d[i] = 10; } for (int i = n - 1; i >= 0; i--) { long long D = 1, v = 0; for (int j = q[i].size() - 1; j > 2; j--) { v = (v + a[q[i][j] - '0'] * D) % mod; D = (D * d[q[i][j] - '0']) % mod; } a[q[i][0] - '0'] = v; d[q[i][0] - '0'] = D; } int asd = 0; long long D = 1, v = 0; for (int j = s.size() - 1; j >= 0; j--) { v = (v + a[s[j] - '0'] * D) % mod; D = (D * d[s[j] - '0']) % mod; } cout << v; }
3
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; if (n % 7 >= 2 && n % 7 <= 5) { cout << (n / 7) * 2 << " " << (n / 7) * 2 + 2; } else if (n % 7 == 6) { cout << (n / 7) * 2 + 1 << " " << (n / 7) * 2 + 2; } else if (n % 7 == 1) { cout << (n / 7) * 2 << " " << (n / 7) * 2 + 1; } else cout << (n / 7) * 2 << " " << (n / 7) * 2; }
1
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const double eps = 1e-11; template <class T> inline void ckmin(T &a, T b) { a = min(a, b); } template <class T> inline void ckmax(T &a, T b) { a = max(a, b); } template <class T> inline T sqr(T x) { return x * x; } using uint = unsigned int; using int64 = long long int; using uint64 = unsigned long long int; using ipair = pair<int, int>; using VI = vector<int>; using VD = vector<double>; void _print(int64 t) { cerr << t; } void _print(int t) { cerr << t; } void _print(string t) { cerr << t; } void _print(char t) { cerr << t; } void _print(double t) { cerr << t; } void _print(uint64 t) { cerr << t; } template <class T, class V> void _print(pair<T, V> p); template <class T> void _print(vector<T> v); template <class T> void _print(set<T> v); template <class T, class V> void _print(map<T, V> v); template <class T> void _print(multiset<T> v); template <class T, class V> void _print(pair<T, V> p) { cerr << "{"; _print(p.ff); cerr << ","; _print(p.ss); cerr << "}"; } template <class T> void _print(vector<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(set<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T> void _print(multiset<T> v) { cerr << "[ "; for (T i : v) { _print(i); cerr << " "; } cerr << "]"; } template <class T, class V> void _print(map<T, V> v) { cerr << "[ "; for (auto i : v) { _print(i); cerr << " "; } cerr << "]"; } struct gg { int x; int y; char direction; gg(int x, int y, char direction) : x(x), y(y), direction(direction) {} }; bool valid(int x, int y, int n, int m) { return (x >= 0 && x < n && y < m && y >= 0); } int xx[] = {1, -1, 0, 0}; int yy[] = {0, 0, 1, -1}; char dd[] = {'d', 'u', 'l', 'r'}; char rdd[] = {'u', 'd', 'r', 'l'}; int main() { std::ios::sync_with_stdio(false); int test; cin >> test; while (test--) { int n, m; cin >> n >> m; string a[n]; queue<gg> q; for (int i = 0; i < (n); ++i) { cin >> a[i]; } int cc = 0; int hh = 0; for (int i = 0; i < (n); ++i) for (int j = 0; j < (m); ++j) { if (a[i][j] == 'L') { cc++; for (int k = 0; k < 4; ++k) { int x = i + xx[k]; int y = j + yy[k]; if (valid(x, y, n, m) && a[x][y] == '.') { q.push(gg(x, y, dd[k])); } } } if (a[i][j] == '#') hh++; } if (cc > 0 && (n == 1 || m == 1) && hh == 0) { for (int i = 0; i < (n); ++i) { for (int j = 0; j < (m); ++j) if (a[i][j] == '.') a[i][j] = '+'; printf("%s\n", a[i].c_str()); } continue; } while (!q.empty()) { gg g = q.front(); q.pop(); int count = 0; int x1, y1, ind; for (int i = 0; i < 4; ++i) { if (g.direction == rdd[i]) continue; int x = g.x + xx[i]; int y = g.y + yy[i]; if (valid(x, y, n, m) && a[x][y] == '.') { count++; x1 = x; y1 = y; ind = i; } } if (count == 1) { q.push(gg(x1, y1, dd[ind])); } if (count <= 1) { a[g.x][g.y] = '+'; } } for (int i = 0; i < (n); ++i) { printf("%s\n", a[i].c_str()); } } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int INF = 1000000007; const long long MOD = 1000000007; void solve_test() { int n; cin >> n; vector<int> arr(n); vector<int> ct(101); for (int i = 0; i < n; i++) { cin >> arr[i]; ct[arr[i]]++; } int max_val = -1; int max_freq = -1; for (int i = 1; i <= 100; i++) { if (ct[i] > max_freq) { max_freq = ct[i]; max_val = i; } } unordered_map<int, int> um; int ans = 0; for (int i = 1; i <= 100; i++) { if (max_val == i) continue; um.clear(); int st = 0; um[0] = -1; for (int j = 0; j < n; j++) { if (arr[j] == i) { st++; } else if (arr[j] == max_val) { st--; } if (um.find(st) == um.end()) { um[st] = j; } else { ans = max(ans, j - um[st]); } } } cout << ans; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); int tests; tests = 1; while (tests--) { solve_test(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3f; const int MOD = 1e9 + 7; template <typename S, typename T> inline bool Min(S &a, const T &b) { return a > b ? a = b, true : false; } template <typename S, typename T> inline bool Max(S &a, const T &b) { return a < b ? a = b, true : false; } template <typename S, typename T> inline void Adm(S &a, const T &b) { a = (a + b) % MOD; if (a < 0) a += MOD; } template <typename S, typename T> inline void Mum(S &a, const T &b) { a = 1LL * a * b % MOD; } template <typename T> inline bool IsPri(T x) { if (x < 2) return false; for (T i = 2; i * i <= x; ++i) if (x % i == 0) return false; return true; } template <typename T> inline T Gcd(T a, T b) { while (b) { T t = b; b = a % b; a = t; } return a; } template <typename T> inline int BitCnt(T x) { int cnt = 0; while (x) ++cnt, x &= x - 1; return cnt; } template <typename T> inline T MDis(T x1, T y1, T x2, T y2) { return abs(x1 - x2) + abs(y1 - y2); } template <typename T> inline T MDis(T x1, T y1, T z1, T x2, T y2, T z2) { return abs(x1 - x2) + abs(y1 - y2) + abs(z1 - z2); } inline double EDis(double x1, double y1, double x2, double y2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } inline double EDis(double x1, double y1, double z1, double x2, double y2, double z2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2) + (z1 - z2) * (z1 - z2)); } inline long long Pow(long long a, long long n) { long long t = 1; a %= MOD; while (n > 0) { if (n & 1) t = t * a % MOD; a = a * a % MOD, n >>= 1; } return t % MOD; } inline int read() { static char buf[1000000], *p1 = buf, *p2 = buf; register int x = false; register char ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++; ; register bool sgn = false; while (ch != '-' && (ch < '0' || ch > '9')) ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++; ; if (ch == '-') sgn = true, ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++; ; while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++; ; return sgn ? -x : x; } int main() { long long x0, y0, ax, ay, bx, by, sx, sy, t; cin >> x0 >> y0 >> ax >> ay >> bx >> by >> sx >> sy >> t; vector<pair<long long, long long> > v; while (x0 - sx + y0 - sy <= t) { v.push_back({x0, y0}); x0 = x0 * ax + bx, y0 = y0 * ay + by; } int ans = 0; for (int i = 0; i < ((int)(v).size()); ++i) for (int j = 0; j < ((int)(v).size()); ++j) { long long dis = MDis(sx, sy, v[i].first, v[i].second); if (j <= i) dis += MDis(v[i].first, v[i].second, v[j].first, v[j].second); else { dis += +MDis(v[i].first, v[i].second, v[0].first, v[0].second); dis += +MDis(v[j].first, v[j].second, v[0].first, v[0].second); } if (dis <= t) Max(ans, j > i ? j + 1 : i - j + 1); } cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; struct edge { int a, b, c; edge(int _a = 0, int _b = 0, int _c = 0) : a(_a), b(_b), c(_c) {} int operator<(const edge &e) const { if (a == e.a) { return b < e.b; } return a < e.a; } }; const int MAXN = 200000; vector<edge> ve[MAXN + 10]; int S[MAXN + 10], in[MAXN + 10], U[MAXN + 10]; int main(void) { int n, m; scanf("%d%d", &n, &m); vector<edge> E; set<edge> res; for (int i = 0; i < (m); ++i) { edge tmp; scanf("%d%d%d", &tmp.a, &tmp.b, &tmp.c); --tmp.a; --tmp.b; E.push_back(tmp); ve[tmp.a].push_back(tmp); swap(tmp.a, tmp.b); ve[tmp.a].push_back(tmp); } for (int i = 0; i < (n); ++i) { for (__typeof((ve[i]).begin()) it = (ve[i]).begin(); it != (ve[i]).end(); ++it) { S[i] += it->c; } } queue<int> Q; Q.push(0); while (!Q.empty()) { int v = Q.front(); U[v] = 1; Q.pop(); for (__typeof((ve[v]).begin()) it = (ve[v]).begin(); it != (ve[v]).end(); ++it) { int w = it->b; if (U[w]) { continue; } res.insert(*it); in[w] += it->c; if (in[w] * 2 == S[w] && w != n - 1) { Q.push(w); } } } for (__typeof((E).begin()) it = (E).begin(); it != (E).end(); ++it) { if (res.find(*it) != res.end()) { puts("0"); } else { puts("1"); } } return 0; }
5
#include <bits/stdc++.h> const int N = 10; int n, L, ALL; int a[N]; int f[32][324]; inline int min(const int x, const int y) {return x < y ? x : y;} inline int max(const int x, const int y) {return x < y ? y : x;} long double solve() { int i, j, k, c, to, len = n * L + L; memset(f, 0, sizeof f), f[0][(n + 1) * a[n]] = 1; for (i = 1; i < len; ++i) if (i % (n + 1)) { c = i % (n + 1) - 1, to = min(i + (n + 1) * a[c], len); for (j = 0; j <= ALL; ++j) if (!(j >> c & 1)) for (k = i; k <= len; ++k) f[j | 1 << c][max(k, to)] += f[j][k]; } return f[ALL][i]; } int main() { int i, cc = 0; long double ans = 0.; scanf("%d%d", &n, &L), ALL = ~(-1 << --n); for (i = 0; i <= n; ++i) scanf("%d", a + i); std::sort(a, a + (n + 1)); do ans += solve(), ++cc; while (std::next_permutation(a, a + n)); printf("%.12Lg\n", ans / (cc * powl(L, n))); return 0; }
0
#include <bits/stdc++.h> using namespace std; template <typename... T> void read(T&... args) { ((cin >> args), ...); } template <typename... T> void write(T&&... args) { ((cout << args << " "), ...); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k; string s; cin >> s; char ch; bool h[26]; memset(h, 0, sizeof(h)); ; for (int i = 0; i < (k); i++) { cin >> ch; h[ch - 'a'] = 1; } int dp[n]; memset(dp, 0, sizeof(dp)); ; for (int i = 0; i < (n); i++) { if (h[s[i] - 'a'] == 0) { dp[i] = 1; } } long long count = 0, ans = 0; for (int i = 0; i < (n); i++) { if (dp[i] == 1) { ans += ((count * (count + 1)) / 2); count = 0; } else { count++; } } ans += ((count * (count + 1)) / 2); cout << ans << endl; return 0; }
3