solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int get() { int t; cin >> t; string s; for (int i = 0; i < t; i++) { cin >> s; } return t; } int main() { while (1) { cout << "next 0 1" << endl; get(); cout << "next 0" << endl; if (get() != 3) break; } while (1) { cout << "next 0 1 2 3 4 5 6 7 8 9 " << endl; if (get() != 2) break; } cout << "done" << endl; }
6
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5 + 2; const int max_sqr = 600; const int inf = 1e9; int a[maxn]; int b[maxn]; int c[maxn]; int q[max_sqr][max_sqr]; int uk[max_sqr]; int n, block_sz, cnt_block; int MOD; void re_build_block_1(int i) { int st = i * block_sz; for (int j = 0; j < block_sz; j++) c[st + j] += uk[i]; uk[i] = 0; } void re_build_block_2(int i) { memset(q[i], 0, sizeof(q[i])); int l = i * block_sz; for (int j = 0; j < block_sz; j++) if (c[l + j] <= block_sz + 2) q[i][c[l + j]]++; } void re_build() { for (int i = 0; i < cnt_block; i++) { re_build_block_1(i); re_build_block_2(i); } } void add(int l, int r, int val) { int i; r++; while (l < r) { if ((l & MOD) == 0 && l + block_sz <= r) { i = l >> 9; uk[i] += val; if (abs(uk[i]) > block_sz - 2) { re_build_block_1(i); re_build_block_2(i); } l += block_sz; continue; } i = l >> 9; re_build_block_1(i); for (int j = l; j < min(r, (i + 1) << 9); j++) c[j] += val; re_build_block_2(i); l = min(r, (i + 1) << 9); } } int get(int l, int r) { int ans = 0, i; r++; while (l < r) { if ((l & MOD) == 0 && l + block_sz <= r) { int i = l >> 9; if (1 - uk[i] > 0) ans += q[i][1 - uk[i]]; if (2 - uk[i] > 0) ans += q[i][2 - uk[i]]; l += block_sz; continue; } i = l >> 9; re_build_block_1(i); for (int j = l; j < r && j < (i + 1) << 9; j++) ans += (c[j] == 1 || c[j] == 2); re_build_block_2(i); l = min(r, (i + 1) << 9); } return ans; } void read() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); a[i]--; } } long long solve() { long long ans = 0; int res, d; vector<int> x; for (int i = 0; i < n; i++) b[a[i]] = i; res = 0; for (int i = 0; i < n; i++) { d = 0; if (b[i] != 0 && a[b[i] - 1] < i) d++; if (b[i] != n - 1 && a[b[i] + 1] < i) d++; res -= (d - 1); c[i] = res; } block_sz = 512; MOD = block_sz - 1; cnt_block = (n + block_sz - 1) / block_sz; re_build(); for (int i = 0; i < n; i++) { ans += get(i, n - 1); x.clear(); if (b[i] != 0 && a[b[i] - 1] > i) x.push_back(a[b[i] - 1]); if (b[i] != n - 1 && a[b[i] + 1] > i) x.push_back(a[b[i] + 1]); sort(x.begin(), x.end()); if ((int)x.size() == 2) { add(i + 1, x[0] - 1, -1); add(x[1], n - 1, 1); } if ((int)x.size() == 1) add(i + 1, x[0] - 1, -1); if ((int)x.size() == 0) add(i + 1, n - 1, -1); } return ans - n; } void gen_test() { n = 5000; for (int i = 0; i < n; i++) a[i] = i; random_shuffle(a, a + n); } long long stupid() { long long ans = 0; int cnt, d; for (int i = 0; i < n; i++) b[a[i]] = i; for (int i = 0; i < n; i++) { cnt = 0; for (int j = i; j < n; j++) { d = 1; if (b[j] != 0 && i <= a[b[j] - 1] && a[b[j] - 1] < j) d--; if (b[j] != n - 1 && i <= a[b[j] + 1] && a[b[j] + 1] < j) d--; cnt += d; ans += (cnt == 1 || cnt == 2); } } return ans - n; } void stress() { int t = 100; long long ans1, ans2; while (t-- > 0) { fprintf(stderr, "test id: %d\n", t); gen_test(); ans1 = solve(); ans2 = stupid(); if (ans1 != ans2) { for (int i = 0; i < n; i++) cerr << a[i] + 1 << " "; cerr << "\nans1 ans2 " << ans1 << " " << ans2 << endl; exit(0); } } exit(0); } int main() { if (0) stress(); read(); cout << solve() << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { string str; cin >> str; int i, j; for (i = 0; i < str.size() - 1; ++i) { if (str[i] == '0') { for (int j = i + 1; j < str.size(); ++j) { cout << str[j]; } return 0; } else { cout << str[i]; } } return 0; }
3
#include<iostream> #include<vector> #include<iomanip> #include<cmath> #include<algorithm> #include<cassert> using namespace std; #define EPS (1e-10) #define equals(a, b) (fabs((a) - (b)) < EPS) struct Point; typedef Point Vector; typedef vector<Point> Polygon; struct Circle; struct Segment; typedef Segment Line; double norm(Point a); double abs(Point a); double dot(Vector a, Vector b); double cross(Vector a, Vector b); double getDistance(Point a, Point b); double getDistanceLP(Line l, Point p); double getDistanceSP(Segment s, Point p); double getDistance(Segment s1, Segment s2); bool isOrthogonal(Vector a, Vector b); bool isOrthogonal(Point a1, Point a2, Point b1, Point b2); bool isOrthogonal(Segment s1, Segment s2); bool isParallel(Vector a, Vector b); bool isParallel(Point a1, Point a2, Point b1, Point b2); bool isParallel(Segment s1, Segment s2); int ccw(Point p0, Point p1, Point p2); bool intersect(Point p1, Point p2, Point p3, Point p4); bool intersect(Segment s1, Segment s2); bool intersect(Circle c, Line l); // 誤差の検証をしていない bool intersect(Circle c1, Circle c2); Point project(Segment s, Point p); Point reflect(Segment s, Point p); Point getCrossPoint(Segment s1, Segment s2); pair<Point,Point> getCrossPoints(Circle c, Line l); pair<Point,Point> getCrossPoints(Circle c1, Circle c2); // 誤差の検証をしていない pair<Point,Point> getContactPoints(Circle c, Point p); // 接点 点は円の外部 double area(Polygon g); // convexでなくてもよい. absを取れば符号付き面積 bool isConvex(Polygon g); // O(n^2) 線形時間アルゴリズムが存在するらしい int contains(Polygon g, Point p); double arg(Vector p); // 偏角 Vector polar(double a, double r); // 極座標系->ベクトル struct Point{ double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} Point operator + (Point p){ return Point(x+p.x, y+p.y); } Point operator - (Point p){ return Point(x-p.x, y-p.y); } Point operator * (double a){ return Point(a*x, a*y); } Point operator / (double a){ return Point(x/a, y/a); } double abs() { return sqrt(norm()); } double norm() { return x*x + y*y; } bool operator < (const Point &p) const{ return x != p.x ? x < p.x : y < p.y; } bool operator == (const Point &p) const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; } }; typedef Point Vector; typedef vector<Point> Polygon; struct Circle{ Point c; double r; Circle(Point c = Point(), double r = 0.0) : c(c), r(r) {} }; struct Segment{ Point p1, p2; Segment(Point p1, Point p2) : p1(p1), p2(p2) {} }; typedef Segment Line; double norm(Point a){ return a.x * a.x + a.y * a.y; } double abs(Point a){ return sqrt(norm(a)); } double dot(Vector a, Vector b){ return a.x * b.x + a.y * b.y; } double cross(Vector a, Vector b){ return a.x * b.y - a.y * b.x; } double getDistance(Point a, Point b){ return abs(a - b); } double getDistanceLP(Line l, Point p){ return abs(cross(l.p2 - l.p1, p - l.p1) / abs(l.p2 - l.p1)); } double getDistanceSP(Segment s, Point p){ if(dot(s.p2-s.p1, p-s.p1) < 0.0) return abs(p-s.p1); if(dot(s.p1-s.p2, p-s.p2) < 0.0) return abs(p-s.p2); return getDistanceLP(s, p); } double getDistance(Segment s1, Segment s2){ if(intersect(s1, s2)) return 0.0; return min({getDistanceSP(s1, s2.p1), getDistanceSP(s1, s2.p2), getDistanceSP(s2, s1.p1), getDistanceSP(s2, s1.p2)}); } bool isOrthogonal(Vector a, Vector b){ return equals(dot(a, b), 0.0); } bool isOrthogonal(Point a1, Point a2, Point b1, Point b2){ return isOrthogonal(a1-a2, b1-b2); } bool isOrthogonal(Segment s1, Segment s2){ return equals(dot(s1.p2-s1.p1, s2.p2-s2.p1), 0.0); } bool isParallel(Vector a, Vector b){ return equals(cross(a, b), 0.0); } bool isParallel(Point a1, Point a2, Point b1, Point b2){ return isParallel(a1-a2, b1-b2); } bool isParallel(Segment s1, Segment s2){ return equals(cross(s1.p2-s1.p1, s2.p2-s2.p1), 0.0); } static const int COUNTER_CLOCKWISE = 1; static const int CLOCKWISE = -1; static const int ONLINE_BACK = 2; // p2->p0->p1 static const int ONLINE_FRONT = -2; // p0->p1->p2 static const int ON_SEGMENT = 0; // p0->p2->p1 int ccw(Point p0, Point p1, Point p2){ Vector a = p1 - p0; Vector b = p2 - p0; if(cross(a, b) > EPS) return COUNTER_CLOCKWISE; if(cross(a, b) < -EPS) return CLOCKWISE; if(dot(a, b) < -EPS) return ONLINE_BACK; if(norm(a) < norm(b)) return ONLINE_FRONT; return ON_SEGMENT; } bool intersect(Point p1, Point p2, Point p3, Point p4){ return (ccw(p1, p2, p3) * ccw(p1, p2, p4) <= 0 && ccw(p3, p4, p1) * ccw(p3, p4, p2) <= 0); } bool intersect(Segment s1, Segment s2){ return intersect(s1.p1, s1.p2, s2.p1, s2.p2); } bool intersect(Circle c, Line l){ return getDistanceLP(l, c.c) < c.r+EPS; } bool intersect(Circle c1, Circle c2){ return abs(c1.r-c2.r) <= getDistance(c1.c, c2.c) && getDistance(c1.c, c2.c) < c1.r+c2.r+EPS; } Point project(Segment s, Point p){ Vector base = s.p2 - s.p1; double r = dot(p - s.p1, base) / norm(base); return s.p1 + base * r; } Point reflect(Segment s, Point p){ return p + (project(s, p) - p) * 2.0; } Point getCrossPoint(Segment s1, Segment s2){ Vector base = s2.p2 - s2.p1; double d1 = abs(cross(base, s1.p1-s2.p1)); double d2 = abs(cross(base, s1.p2-s2.p1)); double t = d1 / (d1 + d2); return s1.p1 + (s1.p2 - s1.p1) * t; } pair<Point,Point> getCrossPoints(Circle c, Line l){ assert(intersect(c, l)); Vector pr = project(l, c.c); Vector e = (l.p2 - l.p1) / abs(l.p2 - l.p1); double base = sqrt(c.r * c.r - norm(pr - c.c)); return make_pair(pr + e*base, pr - e*base); } pair<Point,Point> getCrossPoints(Circle c1, Circle c2){ assert(intersect(c1, c2)); double d = abs(c1.c - c2.c); double a = acos( (c1.r*c1.r + d*d - c2.r*c2.r)/(2*c1.r*d) ); double t = arg(c2.c - c1.c); return make_pair(c1.c + polar(c1.r, t+a), c1.c + polar(c1.r, t-a)); } pair<Point,Point> getContactPoints(Circle c, Point p){ assert(c.r < getDistance(c.c, p)); double d = getDistance(c. c, p); return getCrossPoints(c, Circle(p, sqrt(d*d-c.r*c.r))); } double area(Polygon g){ int n = g.size(); Point o(0.0, 0.0); double s = 0.0; for(int i = 0; i < n; i++) s += cross(g[i]-o, g[(i+1)%n]-o); return abs(s) / 2.0; } bool isConvex(Polygon g){ bool ret = true; int n = g.size(); for(int i = 0; i < n; i++){ for(int j = i+1; j < n; j++){ if(cross(g[i]-g[(i+n-1)%n], g[j]-g[(i+n-1)%n]) < -EPS || cross(g[(i+1)%n]-g[i], g[j]-g[i]) < -EPS){ ret = false; } } } return ret; } static const int IN = 2; static const int ON = 1; static const int OUT = 0; int contains(Polygon g, Point p){ int n = g.size(); bool x = false; for(int i = 0; i < n; i++){ Point a = g[i] - p, b = g[(i+1)%n] - p; if(abs(cross(a, b)) < EPS && dot(a, b) < EPS) return ON; if(a.y > b.y) swap(a, b); if(a.y < EPS && EPS < b.y && cross(a, b) > EPS) x = !x; } return x ? IN : OUT; } double arg(Vector p){ return atan2(p.y, p.x); } Vector polar(double a, double r){ return Point(a * cos(r), a * sin(r)); } int main(){ double x, y, cx, cy, r; cin >> x >> y >> cx >> cy >> r; pair<Point,Point> p = getContactPoints(Circle(Point(cx,cy),r), Point(x,y)); if(p.second < p.first) swap(p.first, p.second); cout << fixed << setprecision(12); cout << p.first.x << " " << p.first.y << endl; cout << p.second.x << " " << p.second.y << endl; return 0; }
0
#include<iostream> int main(){std::string s;std::cin>>s;s[3]=56;std::cout<<s;}
0
//hinagata.cpp /*includes*/ #include <iostream> #include <string> #include <vector> #include <algorithm> #include <cstdlib> #include <cmath> #include <stdio.h> #include <numeric> #include <iomanip> #include <limits> /*namespace*/ using namespace std; using ll= long long; /*define macro*/ #define REP(i,n) for(int i=0;i<(n);i++) #define REPi(i,a,b) for(int i=(a);i<(b);i++) #define REPRi(i,a,b) for(int i=(a);i>(b);i--) #define ALL(x) (x).begin(),(x).end() int sumd(ll A){ string S=to_string(A); ll size=S.size(); ll ans=0; REP(i,size){ ans+=S[i]-'0'; } return ans; } int main(){ int k; cin>>k; ll a=1; ll p=1; REP(i,k){ cout<<a<<endl; ll c=a+p; ll d=a+p*10; if(c*sumd(d)<=d*sumd(c)){ a=c; }else{ a=d; p*=10; } } }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, x, y, h, m, counter = 1; vector<int> v; cin >> n; if (n == 1) { cin >> h >> m; cout << counter << endl; } else { for (int i = 0; i < n; i++) { cin >> h >> m; if (i == 0) { x = h; y = m; } else if (i == (n - 1)) { if (x == h && y == m) { counter++; v.push_back(counter); } else { v.push_back(counter); counter = 1; v.push_back(counter); } } else { if (x == h && y == m) { counter++; x = h; y = m; } else { v.push_back(counter); x = h; y = m; counter = 1; } } } sort(v.rbegin(), v.rend()); cout << v[0] << endl; } }
1
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, m; vector<int> lalal[N]; vector<int> a[N], b[N]; int fx[4] = {-1, 0, 0, 1}; int fy[4] = {0, -1, 1, 0}; bool ok[N]; bool check(int o, int x, int y) { if (x <= 0 || y <= 0) return false; int size = lalal[o].size(); for (int u = 0; u < size; u++) if (lalal[o][u] == b[x][y]) return true; return false; } int myrand() { return rand() * rand() % (n * m) + 1; } void solve() { int num; for (int u = 1; u <= n; u++) for (int i = 1; i <= m; i++) { num = 0; int x = myrand(); while ((num <= (n * m) * 2) && (ok[x] == false || check(x, u - 1, i) || check(x, u, i - 1))) { x = myrand(); num++; } if (num > (n * m) * 2) return; ok[x] = false; b[u][i] = x; } printf("YES\n"); for (int u = 1; u <= n; u++) { for (int i = 1; i <= m; i++) printf("%d ", b[u][i]); printf("\n"); } exit(0); } int main() { srand(87); scanf("%d%d", &n, &m); for (int u = 1; u <= n; u++) { a[u].resize(m + 5); b[u].resize(m + 5); for (int i = 1; i <= m; i++) a[u][i] = (u - 1) * m + i; } for (int u = 1; u <= n; u++) for (int i = 1; i <= m; i++) for (int j = 0; j < 4; j++) { int nx = u + fx[j], ny = i + fy[j]; if (nx >= 1 && nx <= n && ny >= 1 && ny <= m) lalal[a[u][i]].push_back(a[nx][ny]); } for (int u = 1; u <= 100; u++) { memset(ok, true, sizeof(ok)); solve(); } printf("NO\n"); return 0; }
4
/* Problem : https://atcoder.jp/contests/yahoo-procon2019-qual/tasks/yahoo_procon2019_qual_f Algorithm : dp Status : AC */ #include <bits/stdc++.h> #include <cstring> #include <vector> #include <algorithm> #include <cstdio> #include <iostream> using namespace std; typedef long long ll; const int INF = 0x3f3f3f3f; const int MAXN = 2005; const int MOD = 998244353; int n,pre[MAXN * 2],dp[MAXN * 2][MAXN * 2]; char s[MAXN]; int main(){ scanf("%s",s + 1); n = strlen(s + 1); for(int i = 1;i <= n;i++) pre[i] = pre[i - 1] + (s[i] - '0'); for(int i = n + 1;i <= n * 2;i++) pre[i] = pre[i - 1]; dp[0][0] = 1; for(int i = 1;i <= n * 2;i++){ int l = max(pre[i] - i,0); int r = min(pre[i],i); for(int j = l;j <= r;j++) { if(j != 0) dp[i][j] = dp[i - 1][j - 1]; if(i > j) dp[i][j] = (dp[i][j] + dp[i - 1][j]) % MOD; } } printf("%d\n",dp[n * 2][pre[n]]); }
0
#include<bits/stdc++.h> using namespace std; int main(){ int N,M; cin>>N>>M; int ans=pow(2,M)*(100*N+1800*M); cout<<ans<<endl; }
0
#include <bits/stdc++.h> int n, m, a[1000006], b[1000006], pin, sd; bool u[1000006], flag; inline int read() { int n = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { n = n * 10 + c - '0'; c = getchar(); } return n * f; } void e1(int x) { puts("YES"); for (int i = 1; i <= n; ++i) if (i != x) printf("%d %d\n", i, x); } int main() { register int i, j, x, gs; n = read(); for (i = 1; i <= n; ++i) { a[i] = read(); if (a[i] == i) { e1(i); return 0; } } for (i = 1; i <= n; ++i) if (!u[i]) { b[++pin] = i; u[i] = true; for (x = a[i], gs = 1; x != i; x = a[x], ++gs) b[++pin] = x, u[x] = true; if (gs & 1) return 0 * printf("NO"); if (gs == 2) { flag = 1; sd = i; } } if (!flag) return 0 * printf("NO"); puts("YES"); printf("%d %d\n", sd, a[sd]); for (i = 1, x = sd; i <= n; ++i, x = a[x]) { if (b[i] == sd || b[i] == a[sd]) continue; printf("%d %d\n", x, b[i]); } }
2
#include <bits/stdc++.h> int main() { int t; int i, j, k; int flag; long long x1, x2, x3, x4, x, y; int ans[55][55]; scanf("%d", &t); while (t--) { scanf("%lld%lld%lld%lld", &x1, &x2, &x3, &x4); x = x3 - x1; y = x4 - x2; printf("%lld\n", x * y + 1); } return 0; }
3
#include <iostream> #include <queue> #include <vector> #include <utility> using namespace std; long long f(vector<pair<int,int>> a){ priority_queue<int> que; int size=a.size(); vector<vector<int>> in(size+1); for(auto p: a){ if(p.first>=size){ que.push(p.second); }else{ in[p.first].emplace_back(p.second); } } long long res=0; for(int i=size-1;i>=0;i--){ if(!que.empty()){ res+=que.top(); que.pop(); } for(auto p:in[i]){ que.push(p); } } return res; } int main(){ int T; cin>>T; long long ans; for(int i=0;i<T;i++){ int N; cin>>N; int k,l,r; vector<pair<int,int>> front; vector<pair<int,int>> end; ans=0; for(int i=0;i<N;i++){ cin>>k>>l>>r; if(k==N){ ans+=l; }else if(l>r){ ans+=r; front.emplace_back(k,l-r); }else{ ans+=l; end.emplace_back(N-k,r-l); } } ans+=f(front); ans+=f(end); cout<<ans<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long int k, n; while (cin >> k >> n) { set<long long> num; for (int i = 1; i <= sqrt(k); i++) { if (k % i == 0) { num.insert(i); num.insert(k / i); } } if (n > num.size()) cout << -1 << endl; else { set<long long>::iterator it = num.begin(); while (n--) it++; it--; cout << *it << endl; } } return 0; }
1
#include <iostream> using namespace std; const int INF = 1000000000; int main() { while(true){ int n, m; cin >> n >> m; if(n==0 && m==0) break; int C[100][100]; int T[100][100]; for(int i=0; i<m; i++){ for(int j=0; j<m; j++){ if(i==j){ C[i][j] = T[i][j] = 0; } else{ C[i][j] = T[i][j] = INF; } } } for(int i=0; i<n; i++){ int a, b, c, t; cin >> a >> b >> c >> t; C[a-1][b-1] = C[b-1][a-1] = c; T[a-1][b-1] = T[b-1][a-1] = t; } for(int k=0;k<m;k++){ for(int i=0;i<m;i++){ for(int j=0;j<m;j++){ if(C[i][j]>C[i][k]+C[k][j]){ C[i][j]=C[i][k]+C[k][j]; } } } } for(int k=0;k<m;k++){ for(int i=0;i<m;i++){ for(int j=0;j<m;j++){ if(T[i][j]>T[i][k]+T[k][j]){ T[i][j]=T[i][k]+T[k][j]; } } } } int k; cin>>k; for(int i=0;i<k;i++){ int p,q,r; cin>>p>>q>>r; if(r==0) cout<<C[p-1][q-1]<<endl; if(r==1) cout<<T[p-1][q-1]<<endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; char A[4][102]; int n; void Solve() { scanf("%d", &n); for (int i = 0; i < 2; ++i) { A[i][0] = 'x'; } char ch = 'a'; for (int c = 1; c < n; c += 2) { if (c + 1 < n) { A[0][c] = A[0][c + 1] = ch; A[1][c] = A[1][c + 1] = 'a' + 'b' - ch; ch = 'a' + 'b' - ch; } else { A[0][c] = A[1][c] = 'y'; } } ch = 'c'; for (int c = 0; c < n; c += 2) { if (c + 1 < n) { A[2][c] = A[2][c + 1] = ch; A[3][c] = A[3][c + 1] = 'c' + 'd' - ch; ch = 'c' + 'd' - ch; } else { A[2][c] = A[3][c] = 'y'; } } for (int i = 0; i < 4; ++i) { puts(A[i]); } } int main() { Solve(); return 0; }
1
#include <bits/stdc++.h> using namespace std; long long int sum[100010]; int main() { int n; long long int d, a, b; scanf("%d %lld", &n, &d); vector<pair<long long int, long long int>> p; for (int i = 0; i < n; i++) { scanf("%lld %lld", &a, &b); p.push_back(make_pair(a, b)); } sort(p.begin(), p.end()); int p1 = 0, p2 = 1; long long int max = p[p1].second, sum = p[p1].second; while (p1 < p2 && p2 < n) { if (p[p2].first - p[p1].first < d) { sum += p[p2].second; p2++; if (sum > max) { max = sum; } } else { sum = sum - p[p1].second; p1++; if (p1 == p2) { sum = p[p1].second; p2++; } if (sum > max) { max = sum; } } } printf("%lld\n", max); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int MAX = 1e5 + 5; double x[MAX], y[MAX]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> x[i] >> y[i]; double ret = 1e18; for (int i = 0; i < n; i++) { double den = sqrt(((x[i] - x[(i + 2) % n]) * (x[i] - x[(i + 2) % n])) + ((y[i] - y[(i + 2) % n]) * (y[i] - y[(i + 2) % n]))); double val = ((y[(i + 1) % n] - y[i]) * (x[i] - x[(i + 2) % n])) + (x[(i + 1) % n] - x[i]) * (y[(i + 2) % n] - y[i]); val /= den; val /= 2.0; ret = min(ret, abs(val)); } cout << fixed << setprecision(10) << ret; return 0; }
2
#include<iostream> #include<climits> #include<math.h> #include<vector> #include<algorithm> #include<cstdio> #include <string> #include <complex> #include <functional> using namespace std; typedef pair<int,int> P; double dat[100][100]; int dp[6][1010];//動的計画法 int prime[10000001]; char str[1010][1010]; vector<pair<int,int> > pc[100001]; int ABS(int a){return max(a,-a);} int main(){ int coin; int b,r,g,c,s,t; while(cin>>b>>r>>g>>c>>s>>t,b+r+g+c+s+t){ coin=100; coin+=15*(b+r)+13*(b*5+r*3)+7*g+2*c-(t-5*b-3*r-s)*3; cout<<coin<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; bool isprime(int n) { for (int i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } int main() { int n, m = 0; cin >> n; for (int i = 0, x; i < n; i++) { cin >> x; m = max(x, m); } m > 25 ? cout << m - 25 : cout << 0; }
1
#include <bits/stdc++.h> using namespace std; template <class T> T abs(T x) { return x > 0 ? x : -x; } int n; int m; vector<pair<int, int> > v[100000]; int main() { scanf("%d", &n); printf("%d\n", n - 1); for (int i = 0; i < n - 1; i++) { int a, b; scanf("%d%d", &a, &b); a--; b--; v[a].push_back(make_pair(b, i + 1)); v[b].push_back(make_pair(a, i + 1)); printf("2 %d %d\n", a + 1, b + 1); } for (int i = 0; i < n; i++) for (int j = 0; j < ((int)(v[i]).size()) - 1; j++) printf("%d %d\n", v[i][j].second, v[i][j + 1].second); return 0; }
4
#include <bits/stdc++.h> using namespace std; string s, ans; vector<pair<int, int> > seg; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> s; reverse(s.begin(), s.end()); int n = s.size(), cntz = 0, cnto = 0, ze = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '0') cntz++, ze++, ans = "0" + ans; else { cnto++; if (ze + 1 <= max(cntz, cnto)) { ze++; ans = "0" + ans; } else ans = "1" + ans; } } cout << ans; }
4
#include <bits/stdc++.h> using ll = long long; using namespace std; const int MAX = 600600; const ll MOD = 1000000007; ll fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++){ fac[i] = fac[i-1] * i % MOD; inv[i] = MOD - inv[MOD%i] * (MOD/i) % MOD; finv[i] = finv[i-1] * inv[i] % MOD; } } ll COM(int n, int k){ if (n < 0 || k < 0 || n < k) return 0; return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD; } ll solve(ll a, ll n, ll m) { ll ans = 0; for (ll i = 1; i <= a; i++) { for (ll j = 0; j <= n - m; j++) { ll num = a - m * i - j * i; if (num < 0) break; ll add = ( COM(n + num, n) - COM(n + num - m, n) + MOD) % MOD; add = add * COM(n - m, j) % MOD; if (j % 2 == 0) ans = ( ans + add ) % MOD; else ans = ( ans - add + MOD ) % MOD; } } ans = ans * COM(n, m) % MOD; return ( COM(n + a, n) - ans + MOD ) % MOD; } int main() { ll n, m, l , r; cin >> n >> m >> l >> r; COMinit(); cout << ( solve(r, n, m) - solve(l - 1, n, m) + MOD ) % MOD << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int i, j, m, n, a1[10], a2[10], pos[10]; cin >> n >> m; for (i = 0; i < n; i++) cin >> a1[i]; for (j = 0; j < m; j++) cin >> a2[j]; int count; for (i = 0; i < m; i++) { count = 0; for (j = 0; j < n; j++) { if (a1[j] == a2[i]) { count = 1; pos[i] = j; break; } } if (count == 0) a2[i] = -1; } int temp; for (i = 0; i < m - 1; i++) for (j = 0; j < m - i - 1; j++) { if (pos[j] > pos[j + 1]) { temp = a2[j]; a2[j] = a2[j + 1]; a2[j + 1] = temp; temp = pos[j]; pos[j] = pos[j + 1]; pos[j + 1] = temp; } } for (i = 0; i < m; i++) if (a2[i] >= 0) cout << a2[i] << " "; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long arr[200009]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; } long long x = 0; for (int i = 0; i < n; i++) { cout << arr[i] + x << " "; x = max(x, arr[i] + x); } cout << endl; }
2
#include <bits/stdc++.h> int main() { int a, b, c, d; scanf("%d%d%d%d ", &a, &b, &c, &d); int s1 = 3 * a / 10; int s2 = 3 * b / 10; int n1 = a - a / 250 * c; int n2 = b - b / 250 * d; int p1 = s1; if (s1 < n1) p1 = n1; else p1 = s1; int p2 = s2; if (s2 < n2) p2 = n2; else p2 = s2; if (p1 > p2) printf("Misha"); else if (p2 > p1) printf("Vasya"); else printf("Tie"); return 0; }
1
#include <bits/stdc++.h> using namespace std; const long double pi = 3.1415926535897932384626433832795; const long double eps = 0.000000001; const int INF = 1E9; const int MAXN = 210000; int n, k, s, m; bool fail; set<pair<int, int> > cur; pair<int, int> cnt, c2; vector<pair<int, int> > ans, nxt; int main() { cin >> n >> s; for (int i = 0; i < (int)(n); i++) { scanf("%d", &k); cur.insert(make_pair(-k, i + 1)); } fail = 0; while (!cur.empty()) { cnt = *cur.begin(); cur.erase(cur.begin()); m = abs(cnt.first); for (int i = 0; i < (int)(m); i++) { if (cur.empty()) { fail = 1; break; } c2 = *cur.begin(); cur.erase(cur.begin()); if (!c2.first) { fail = 1; break; } c2.first++; ans.push_back(make_pair(c2.second, cnt.second)); if (c2.first != 0) nxt.push_back(c2); } if (fail) break; for (int i = 0; i < (int)(nxt.size()); i++) cur.insert(nxt[i]); nxt.clear(); } if (fail) cout << "No"; else { cout << "Yes" << endl; cout << ans.size() << endl; for (int i = 0; i < (int)(ans.size()); i++) printf("%d %d\n", ans[i].first, ans[i].second); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int arr[1000005]; int main() { int n, q, i, j, d, diagnolSum, arr[1005]; scanf("%d", &n); diagnolSum = 0; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { scanf("%d", &d); if (i == j) { arr[i] = d; diagnolSum += d; } } } diagnolSum %= 2; scanf("%d", &q); while (q--) { scanf("%d", &d); if (d == 3) { printf("%d", diagnolSum); } else { scanf("%d", &j); diagnolSum = 1 - diagnolSum; } } return 0; }
3
#include <bits/stdc++.h> const long long int N = 300001; using namespace std; long long int f(long long int n) { if (n == 1) { return 1; } long long int ans = n; for (long long int i = 2; i * i <= n; i++) { if (n % i == 0) { ans /= i; ans *= (i - 1); while (n % i == 0) { n /= i; } } } if (n > 1) { ans /= n; ans *= (n - 1); } return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, k; cin >> n >> k; long long int t = (k + 1) / 2; while (t-- && n > 1) { n = f(n); } cout << n % 1000000007; }
5
#include <bits/stdc++.h> using namespace std; vector<int> arr; int n, k; long long sum[5500]; double mx = -1e9; int main() { cin >> n >> k; for (int x, i = 1; i <= n; i++) { cin >> x; arr.push_back(x); sum[i] = sum[i - 1] + x; } for (int i = 1; i <= n; i++) { for (int j = i + k - 1; j <= n; j++) { mx = max(mx, (sum[j] - sum[i - 1]) / (double(j - i + 1))); } } printf("%0.15lf", mx); }
3
#include <bits/stdc++.h> using namespace std; const long long maxn = 1000005; const long long maxv = 750000000000LL; long long n, q; long long val[maxn]; long long test(long long limit) { priority_queue<long long> pq; long long start = 0LL; long long use = 0; for (long long i = 0; i < n; i++) { pq.push(-val[i]); start += val[i]; while (limit > start and !pq.empty()) { start += pq.top(); use++; pq.pop(); } if (pq.empty() and limit > start) { return maxn; } } return use; } int main() { ios::sync_with_stdio(false); cin >> n >> q; for (long long i = 0; i < n; i++) { cin >> val[i]; } map<long long, long long> m; for (long long i = n; i >= 0; i--) { long long l = -maxv; long long r = 0LL; long long mostv = -maxv; while (l <= r) { long long mid = (l + r) / 2; if (test(mid) <= i) { mostv = mid; l = mid + 1; } else { r = mid - 1; } } m[mostv] = i; } for (long long i = 0; i < q; i++) { long long v; cin >> v; cout << m.lower_bound(-v)->second << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; template <typename T> bool chkmin(T &x, T y) { return x > y ? x = y, 1 : 0; } template <typename T> bool chkmax(T &x, T y) { return x < y ? x = y, 1 : 0; } int readint() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } const int cys = 1000000007; int n, m, ncnt; long long ch[50005][10], dep[50005], fail[5005], d[55][50005][2][2][2], q[50005]; char s[1005], x[55], y[55]; bool fl[50005]; void gettrie(char *s) { int p = 0; for (int i = 1; i <= m / 2; i++) { if (!ch[p][s[i] - '0']) ch[p][s[i] - '0'] = ++ncnt; p = ch[p][s[i] - '0']; } fl[p] = 1; } void getfail() { int front = 0, rear = 0; q[rear++] = 0; while (front < rear) { int t = q[front++]; fl[t] |= fl[fail[t]]; for (int i = 0; i < 10; i++) { if (ch[t][i]) { q[rear++] = ch[t][i]; fail[ch[t][i]] = t ? ch[fail[t]][i] : t; } else ch[t][i] = t ? ch[fail[t]][i] : t; } } } void upd(long long &x, long long y) { x += y; x = x >= cys ? x - cys : x; } long long getans() { d[0][0][0][1][1] = 1; for (int i = 0; i < m; i++) for (int j = 0; j <= ncnt; j++) for (int k = 0; k <= 1; k++) for (int c1 = 0; c1 <= 1; c1++) for (int c2 = 0; c2 <= 1; c2++) for (int num = (c1 ? x[i + 1] - '0' : 0); num <= (c2 ? y[i + 1] - '0' : 9); num++) upd(d[i + 1][ch[j][num]][k | fl[ch[j][num]]] [c1 & (num == x[i + 1] - '0')][c2 & (num == y[i + 1] - '0')], d[i][j][k][c1][c2]); long long ret = 0; for (int i = 0; i <= ncnt; i++) for (int c1 = 0; c1 <= 1; c1++) for (int c2 = 0; c2 <= 1; c2++) upd(ret, d[m][i][1][c1][c2]); return ret; } int main() { scanf("%s%s%s", s + 1, x + 1, y + 1); n = strlen(s + 1); m = strlen(x + 1); for (int i = 1; i <= n - m / 2 + 1; i++) gettrie(s + i - 1); getfail(); printf("%lld\n", getans()); return 0; }
6
#include<bits/stdc++.h> using namespace std; struct cww{cww(){ios::sync_with_stdio(false);cin.tie(0);}}init; typedef long long LL; struct edge{ int flow,to,rev; }; typedef vector<edge> E; typedef vector<E> FlowGraph; void addedge(FlowGraph &g,int from,int to,int f){ int a=g[from].size(); int b=g[to].size(); g[from].push_back(edge{f,to,b}); g[to].push_back(edge{0,from,a}); } typedef vector<LL> V; typedef vector<V> VV; const LL INF=1e15; V bfs(FlowGraph &g,int s){ int n=g.size(); queue<int> que; V dist(n,-1); dist[s]=0; que.push(s); for(;que.size();que.pop()){ auto v=que.front(); for(auto &e:g[v]){ if(e.flow>0&&dist[e.to]==-1){ dist[e.to]=dist[v]+1; que.push(e.to); } } } return dist; } int maxflow(FlowGraph &g,int s,int t){ int res=0,n=g.size(); while(true){ auto dist=bfs(g,s); if(dist[t]<0)break; vector<unsigned> iter(n,0); std::function<int(int,int)> dfs=[&](int v,int f){ if(v==s)return f; for(auto &i=iter[v];i<g[v].size();i++){ edge &e=g[v][i]; edge &re=g[e.to][e.rev]; if(re.flow>0&&dist[v]>dist[e.to]){ int d=dfs(e.to,min(f,re.flow)); if(d>0){e.flow+=d;re.flow-=d;return d;} } } return 0; }; int f; while((f=dfs(t,114514))>0)res+=f; } return res; } int main(){ for(int N,M,L;cin>>N>>M>>L,N+M+L;){ VV g(N,V(N,INF)); for(int i=0;i<M;i++){ int u,v,d; cin>>u>>v>>d; g[u][v]=g[v][u]=d; } for(int i=0;i<N;i++)g[i][i]=0; for(int k=0;k<N;k++) for(int i=0;i<N;i++) for(int j=0;j<N;j++) g[i][j]=min(g[i][j],g[i][k]+g[k][j]); using P=tuple<int,int>; vector<P> pt(L); for(int i=0;i<L;i++){ int p,t; cin>>p>>t; pt[i]=P(t ,p); } sort(pt.begin(),pt.end()); FlowGraph fg(2*L+2); int id=0; int S=id++; int T=id++; vector<int> bg(L),ed(L); for(int i=0;i<L;i++){ bg[i]=id++;ed[i]=id++; addedge(fg,S,ed[i],1); addedge(fg,bg[i],T,1); int pv,tv,pu,tu; tie(tv,pv)=pt[i]; for(int j=0;j<i;j++){ tie(tu,pu)=pt[j]; if(g[pv][pu]<=abs(tv-tu)) addedge(fg,ed[j],bg[i],1); } } cout<<L-maxflow(fg,S,T)<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, m[N]; int main() { scanf("%d", &n); long long sum = 0; for (int i = 0; i < n; ++i) { scanf("%d", m + i); sum += m[i]; } long long cap = sum % n; long long res = sum / n + (cap != 0); long long sec = 0; for (int i = 0; i < n; ++i) { if (m[i] > sum / n) { sec += m[i] - res; if (cap == 0 && sum % n) ++sec; else --cap; } } printf("%I64d\n", sec); }
3
#include <bits/stdc++.h> using namespace std; const int N = 100 + 10; int n; int a[N]; int mex[N]; map<int, int> sg; void solve(long long x) { if (sg.count(x)) return; vector<int> mex(30, 0); for (int i = 0; x >> i; ++i) { long long nx = ((x >> (i + 1)) | (x & ((1 << i) - 1))); solve(nx); mex[sg[nx]] = 1; } int ret = 0; for (; mex[ret]; ++ret) ; sg[x] = ret; } int main() { cin >> n; set<int> primes; for (int i = 0; i < n; ++i) { cin >> a[i]; int t = a[i]; for (int j = 2; j * j <= t; ++j) { if (t % j == 0) { primes.insert(j); while (t % j == 0) t /= j; } } if (t > 1) primes.insert(t); } sg[0] = 0; int ret = 0; for (auto e : primes) { int state = 0; for (int i = 0; i < n; ++i) { int cnt = 0; while (a[i] % e == 0) { a[i] /= e; cnt++; } if (cnt) state |= (1 << (cnt - 1)); } solve(state); ret ^= sg[state]; } cout << (ret ? "Mojtaba" : "Arpa") << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; void ff1(int arr[], int n, int mx) { int i; for (i = 0; i < n; i++) arr[i] = mx - arr[i]; } void ff2(int arr[], int n, int mx) { int i, mx1 = 0; for (i = 0; i < n; i++) arr[i] = mx - arr[i], mx1 = max(mx1, arr[i]); for (i = 0; i < n; i++) arr[i] = mx1 - arr[i]; } int main() { ios::sync_with_stdio(false); ; cin.tie(nullptr); int T, n, mx, i; long long k; cin >> T; while (T--) { cin >> n >> k; int arr[n]; mx = -1000000010; for (i = 0; i < n; i++) { cin >> arr[i]; mx = max(mx, arr[i]); } if (k % 2) ff1(arr, n, mx); else ff2(arr, n, mx); for (i = 0; i < n; i++) cout << arr[i] << ' '; cout << '\n'; } }
2
#include <bits/stdc++.h> using namespace std; #define for_(i,a,b) for(int i=a;i<b;++i) typedef long long lint; int H, W, dx[4] = { 0, 1, 0, -1 }, dy[4] = { -1, 0, 1, 0 }; lint L; string grid[110], dir = "NESW"; int visit[110][110][4], seq[41000]; bool able(int x, int y) { if (x < 0 || x >= W || y < 0 || y >= H) return false; return grid[y][x] == '.'; } int main() { std::ios::sync_with_stdio(false); while (cin >> H >> W >> L, H) { int x = 0, y = 0, d = 0; for_(i,0,H) { cin >> grid[i]; for_(j,0,W) if (grid[i][j] != '.' && grid[i][j] != '#') { x = j; y = i; d = dir.find(grid[i][j]); grid[i][j] = '.'; } } memset(visit, -1, sizeof(visit)); lint s = 0; while (visit[y][x][d] < 0) { visit[y][x][d] = s; seq[s] = (y << 10) | (x << 2) | d; while (!able(x + dx[d], y + dy[d])) d = (d + 1) % 4; x += dx[d]; y += dy[d]; ++s; } int t = visit[y][x][d], id = L; if (s < L) id = t + (L - t) % (s - t); y = seq[id] >> 10; x = (seq[id] >> 2) & 0xff; d = seq[id] & 3; cout << y + 1 << " " << x + 1 << " " << dir[d] << endl; } return 0; }
0
#include <iostream> #include <algorithm> #include <cstdio> using namespace std; #define REP(i,a,n) for(int i=(a); i<(int)(n); i++) #define rep(i,n) REP(i,0,n) #define DEB 0 #define all(x) x.begin(), x.end() //max:2192 unsigned short int dp[5][32770]; int main(){ unsigned short int i,j,k; int n; for(i=1; i*i<(1<<15); i++){ dp[1][i*i] = 1; for(j=i*i+1; j<(1<<15); j++){ for(k=1; k<4; k++){ dp[k+1][j] += dp[k][j-i*i]; } } } #if DEB int mx = 0; rep(i,1<<15){ rep(j,5){ mx = max(mx, dp[j][i]); } } printf("max:%d\n",mx); #endif while(scanf("%d",&n),n){ printf("%d\n",dp[1][n]+dp[2][n]+dp[3][n]+dp[4][n]); } return 0; }
0
#include<bits/stdc++.h> using namespace std; int main(){ int a,b,n; cin >> a >> b >> n; cout << n / 1000 * min(a, 2 * b) + (n % 1000 > 500 ? min(a, 2 * b) : n % 1000 == 0 ? 0 : min(a, b)) << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n; cin >> n; long long int s, c, b; s = floor(sqrt(n)); c = floor(cbrt(n)); b = floor(cbrt(sqrt(n))); cout << (s + c - b) << endl; } return 0; }
2
#include <bits/stdc++.h> #define PB push_back #define MP make_pair #define REP(i,n) for (int i=0;i<(n);i++) #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define ALL(a) (a).begin(),(a).end() using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> P; double EPS=1e-10; int p,q,a,n; int rec(int x,int y,int cnt,int num){ if(cnt>n)return 0; if(x*p==y*q)return 1; if(x*p< y*q)return 0; int res=0; for(int i=num;i*x<=a;i++){ res+=rec(x*i,y*i+x,cnt+1,i); } return res; } int main(){ while(cin>>p>>q>>a>>n&&(p||q||a||n)){ cout<<rec(1,0,0,1)<<endl; } }
0
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 5; int n, a[N], m, ans[N]; vector<int> pref[N], suf[N]; inline void Add(vector<int> &bas, int val) { for (auto it : bas) { val = min(val, (val ^ it)); } if (!val) { return; } for (auto &it : bas) { it = min(it, (it ^ val)); } bas.push_back(val); sort(bas.rbegin(), bas.rend()); } inline vector<int> Merge(vector<int> &a, vector<int> &b) { vector<int> c; if ((int)a.size() > (int)b.size()) { c = b; for (auto it : a) { Add(c, it); } } else { c = a; for (auto it : b) { Add(c, it); } } return c; } inline int Get(vector<int> &bas) { int cur = 0; for (auto it : bas) { cur = max(cur, (cur ^ it)); } return cur; } void rec(int l, int r, vector<pair<pair<int, int>, int> > &q) { if (l > r || q.empty()) { return; } if (l == r) { for (auto it : q) { ans[it.second] = a[l]; } q.clear(); return; } int mid = (r + l) >> 1; pref[mid + 1].clear(); Add(pref[mid + 1], a[mid + 1]); for (int i = mid + 2; i <= r; i++) { pref[i] = pref[i - 1]; Add(pref[i], a[i]); } suf[mid].clear(); Add(suf[mid], a[mid]); for (int i = mid - 1; i >= l; i--) { suf[i] = suf[i + 1]; Add(suf[i], a[i]); } vector<pair<pair<int, int>, int> > to_left, to_right; for (auto it : q) { int ll = it.first.first, rr = it.first.second; if (rr <= mid) { to_left.push_back(it); } else if (ll > mid) { to_right.push_back(it); } else { vector<int> cur = Merge(suf[ll], pref[rr]); ans[it.second] = Get(cur); } } q.clear(); rec(l, mid, to_left); rec(mid + 1, r, to_right); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; } cin >> m; vector<pair<pair<int, int>, int> > q; for (int i = 1; i <= m; i++) { int l, r; cin >> l >> r; q.push_back(make_pair(make_pair(l, r), i)); } rec(1, n, q); for (int i = 1; i <= m; i++) { cout << ans[i] << "\n"; } }
6
#include <bits/stdc++.h> using namespace std; const int MAXN = 8; const int MAXR = 1; const int MAXC = 1; const int INF = (int)1e9; priority_queue<int, vector<int>, greater<int>> q1; priority_queue<int, vector<int>, less<int>> q2; int arr[MAXN]; map<int, int> m1; map<string, int> m2; vector<pair<string, pair<int, int>>> v; int n, m, k; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, 1, 0, -1}; int main(void) { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k >> m; int x, y; string s; v.resize(n + 1); for (int i = 0; i < n; ++i) { cin >> s; v[i + 1] = make_pair(s, make_pair(i + 1, 0)); } for (int i = 0; i < n; ++i) { cin >> x; v[i + 1].second.second = x; }; ; for (int i = 0; i < k; ++i) { int gn = i + 1; cin >> x; m1[gn] = INF; for (int j = 0; j < x; ++j) { cin >> y; pair<string, pair<int, int>> p1 = v[y]; pair<int, int> p2 = p1.second; if (p2.second < m1[gn]) { m1[gn] = p2.second; } m2[p1.first] = gn; } }; ; long long int tc = 0; for (int i = 0; i < m; ++i) { cin >> s; int gn = m2[s]; int mc = m1[gn]; tc += mc * 1LL; } cout << tc; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAX_N = 555555; int mn[MAX_N << 2]; int a[MAX_N], fa[MAX_N][18], p[MAX_N], n, N, L[MAX_N]; int query(int x, int l, int r, int f, int t) { if (f <= l && r <= t) { return mn[x]; } int mid = (l + r) >> 1; int ret = 0; if (f <= mid) ret = query((x << 1), l, mid, f, t); if (t > mid) { int fk = query(((x << 1) + 1), mid + 1, r, f, t); if (L[ret] > L[fk]) ret = fk; } return ret; } void modify(int x, int l, int r, int v, int d) { if (l == r) { mn[x] = d; return; } int mid = (l + r) >> 1; if (v <= mid) modify((x << 1), l, mid, v, d); else modify(((x << 1) + 1), mid + 1, r, v, d); if (L[mn[(x << 1)]] > L[mn[((x << 1) + 1)]]) mn[x] = mn[((x << 1) + 1)]; else mn[x] = mn[(x << 1)]; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } N = n + n - 1; for (int i = n + 1; i <= N; ++i) a[i] = a[i - n]; L[0] = N + 1; for (int i = 1; i <= N; ++i) { L[i] = max(i - a[i], 1); } for (int i = 1; i <= N; ++i) { p[i] = query(1, 1, N, L[i], i); if (i == 1) p[i] = 0; fa[i][0] = p[i]; modify(1, 1, N, i, i); } L[0] = 0; for (int k = 1; k <= 17; ++k) for (int i = 1; i <= N; ++i) fa[i][k] = fa[fa[i][k - 1]][k - 1]; long long ans = 0; for (int i = 1; i <= n; ++i) { int u = i + n - 1, v = i; ans++; if (L[u] <= v) { continue; } for (int k = 17; k >= 0; --k) if (L[fa[u][k]] > v) u = fa[u][k], ans += (1 << k); u = fa[u][0]; ans++; } cout << ans << endl; }
2
#include <bits/stdc++.h> using namespace std; int MAXCOL; int MAXROW; vector<pair<int, int>> getnei(int row, int col) { vector<pair<int, int>> ans; if (row > 0) { ans.push_back({row - 1, col}); } if (row <= MAXROW - 1) { ans.push_back({row + 1, col}); } if (col > 0) { ans.push_back({row, col - 1}); } if (col <= MAXCOL - 1) { ans.push_back({row, col + 1}); } return ans; } struct unionFind { vector<int> u; vector<int> us; unionFind(int n) : u(n + 1), us(n + 1) { for (int i = 1; i <= n; i++) { u[i] = i; us[i] = 1; } } int get(int x) { if (x == u[x]) return x; return u[x] = get(u[x]); } void un(int a, int b) { a = get(a); b = get(b); if (a != b) { if (us[a] < us[b]) swap(a, b); us[a] += us[b]; u[b] = a; } } }; map<int, vector<pair<int, int>>> mama; void printans(unionFind& uf, int start, long long val, int total) { vector<vector<long long>> ans(MAXROW + 1, vector<long long>(MAXCOL + 1)); vector<bool> seen(1e6 + 5); deque<int> d; for (auto p : mama[val]) { int num = p.first * 1000 + p.second + 1; if (uf.get(num) == start) { d.push_back(num); } } int tot = 0; while (!d.empty()) { int cur = d.front(); d.pop_front(); if (seen[cur]) continue; seen[cur] = true; ans[(cur - 1) / 1000][(cur - 1) % 1000] = val; tot++; if (tot == total) { for (auto v : ans) { for (auto a : v) { cout << a << " "; } cout << "\n"; } return; } for (auto nb : getnei((cur - 1) / 1000, (cur - 1) % 1000)) { int num = nb.first * 1000 + nb.second + 1; if (uf.get(num) == start) { d.push_back(num); } } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n, m, k; cin >> n >> m >> k; MAXROW = n - 1; MAXCOL = m - 1; vector<pair<long long, pair<int, int>>> v; vector<vector<int>> mat(n, vector<int>(m)); vector<vector<bool>> seen(n, vector<bool>(m)); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> mat[i][j]; v.push_back({mat[i][j], {i, j}}); mama[mat[i][j]].push_back({i, j}); } } sort(v.rbegin(), v.rend()); unionFind uf(1000 * n + m + 50); for (int i = 0; i < v.size(); i++) { int row, col; tie(row, col) = v[i].second; int num = row * 1000 + col + 1; for (auto nb : getnei(row, col)) { int nbnum = nb.first * 1000 + nb.second + 1; if (seen[nb.first][nb.second]) { uf.un(uf.get(nbnum), uf.get(num)); } } if (k % v[i].first == 0 && uf.us[uf.get(num)] * v[i].first >= k) { cout << "YES" << endl; printans(uf, uf.get(num), v[i].first, k / v[i].first); return 0; } seen[row][col] = true; } cout << "NO" << endl; }
6
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n; cin >> n; vector<int> arr(n); vector<int> aux(n, 0); for (int i = 0; i < n; i++) { cin >> arr[i]; } int id = 0; sort(arr.begin(), arr.end()); for (int i = 0; i < s.length() / 2; i++) { while (id < n && arr[id] <= i + 1) id++; if (id % 2 == 1) swap(s[i], s[s.size() - i - 1]); } cout << s << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; string t = ""; char c = s[s.length() - 1]; t += c; for (int i = s.length() - 2; i >= 0; i--) { if (t[0] <= s[i]) t = s[i] + t; } cout << t; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long int a1, a2, a3, a4, a5, a6, d; cin >> a1 >> a2 >> a3 >> a4 >> a5 >> a6; d = pow(a1 + a2 + a3, 2) - pow(a1, 2) - pow(a3, 2) - pow(a5, 2); cout << d; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int c1, c2, c3, c4, c5, sum = 0; cin >> c1 >> c2 >> c3 >> c4 >> c5; sum = c1 + c2 + c3 + c4 + c5; if (sum % 5 == 0 && sum != 0) cout << sum / 5 << endl; else cout << -1 << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; struct R { long long n, d; R(long long l_n, long long l_d) { n = l_n; d = l_d; } }; bool smaller(R r1, R r2) { return r1.n * r2.d < r1.d * r2.n; } int main() { int n; cin >> n; R s1(100000000, 1), s2(0, 1); for (int i = 1; i <= n; i++) { int x; cin >> x; R t1((x + 1) * 10, i); R t2(x * 10, i); if (smaller(t1, s1)) s1 = t1; if (smaller(s2, t2)) s2 = t2; } s1.n *= n + 1; s2.n *= n + 1; long long v1 = s1.n / s1.d / 10; long long v2 = s2.n / s2.d / 10; if (v1 == v2 || (v1 == v2 + 1 && s1.n % (s1.d * 10) == 0)) { cout << "unique" << endl; cout << v2 << endl; } else { cout << "not unique" << endl; } }
3
#include <bits/stdc++.h> using namespace std; int main() { long long int n, m, x, y, ans, i, j; cin >> n >> m; long long int dp[100005]; vector<vector<long long int> > adj(n + 1); for (i = 0; i < m; i++) { cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } dp[1] = 1; for (i = 2; i <= n; i++) { dp[i] = 1; for (j = 0; j < adj[i].size(); j++) if (adj[i][j] < i) dp[i] = max(dp[i], dp[adj[i][j]] + 1); } ans = 0; for (i = 1; i <= n; i++) x = adj[i].size(), ans = max(ans, dp[i] * x); cout << ans; }
2
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; using pil = pair<int, ll>; using pli = pair<ll, int>; using pll = pair<ll, ll>; const int MOD = 1000000007; const int inf = (1 << 30) - 1; const ll INF = (1LL << 60) - 1; template <typename T> bool chmax(T &x, const T &y) { return (x < y) ? (x = y, true) : false; }; template <typename T> bool chmin(T &x, const T &y) { return (x > y) ? (x = y, true) : false; }; struct io_setup { io_setup() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << setprecision(15); } } io_setup; int main() { int T; cin >> T; vector<vector<int>> ok; vector<int> s; for (int i = 0; i < 8; i++) { for (int j = 0; j < i; j++) { vector<int> tmp; int now = 4; for (int k = 0; k < 8; k++) { if (k == i || k == j) now += 3, now %= 10; else tmp.emplace_back(now); } ok.emplace_back(tmp), s.emplace_back(accumulate(tmp.begin(), tmp.end(), 0)); } } while (T--) { ll N; cin >> N; string S = to_string(N); reverse(S.begin(), S.end()); int n = (int)S.size(); vector<vector<bool>> dp(n + 1, vector<bool>(10, false)); vector<vector<pii>> pre(n + 1, vector<pii>(10)); dp[0][0] = true; for (int i = 0; i < n; i++) { for (int j = 0; j < 10; j++) { if (dp[i][j] == false) continue; int x = S[i] - '0'; for (int k = 0; k < (int)s.size(); k++) { int p = j + s[k]; if (p % 10 == x) { dp[i + 1][p / 10] = true; pre[i + 1][p / 10] = pii(j, k); } } } } if (!dp[n][0]) { cout << "-1\n"; continue; } int j = 0; vector<string> ans(6); for (int i = n; i >= 1; i--) { auto [nj, nk] = pre[i][j]; for (int k = 0; k < 6; k++) { ans[k] += char('0' + ok[nk][k]); } j = nj; } for (int i = 0; i < 6; i++) { int j = 0; while (j < (int)ans[i].size() - 1 && ans[i][j] == '0') j++; cout << ans[i].substr(j) << (i == 5 ? '\n' : ' '); } } }
5
#include <bits/stdc++.h> using namespace std; template <class T> T min(T a, T b, T c) { return min(a, min(b, c)); } template <class T> T max(T a, T b, T c) { return max(a, max(b, c)); } int n, a[55], p[55]; int main() { scanf("%d", &n); for (int i = (1); i <= (n); ++i) { for (int j = (1); j <= (n); ++j) scanf("%d", &a[j]); p[i] = *max_element(a + 1, a + n + 1); } for (int i = (1); i <= (n); ++i) { if (p[i] == n - 1) { p[i] = n; break; } } for (int i = (1); i <= (n); ++i) printf("%d ", p[i]); return 0; }
2
#include <bits/stdc++.h> using namespace std; long long M = 1000000000 + 7; long long power(long long x, long long n) { long long result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % M; x = (x * x) % M; n = n / 2; } return result; } int main() { long long n, x; cin >> x >> n; vector<long long> prime; long long tx = x; long long div = 2; for (long long i = 2; i * i <= x; i++) { long long temptx = tx; while (tx % div == 0) { tx /= div; } if (tx != temptx) prime.push_back(div); div++; } if (tx >= 2) prime.push_back(tx); long long ans = 1; for (long long p : prime) { long long i = 1; long long sum = 0; long long ratio = n / p; i = 2; while (ratio != 0) { ans *= power(p, ratio); ans %= M; ratio /= p; } } cout << ans; return 0; }
3
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast") using namespace std; namespace ywy { int ch[2111][2], fa[2111], gpt = 1, size[2111]; unsigned char bv[2111]; int tot = 0, zx; void afs(int pt) { if (bv[pt] || !pt) return; if (!ch[pt][0] && !ch[pt][1]) { tot++; return; } afs(ch[pt][0]); afs(ch[pt][1]); } int minn = 1234567890; void bfs(int pt) { if (bv[pt] || !pt) return; int mx = 0; if (!ch[pt][0] && !ch[pt][1]) size[pt] = 1; else { size[pt] = 0; for (register int i = 0; i <= 1; i++) { if (bv[ch[pt][i]]) continue; bfs(ch[pt][i]); size[pt] += size[ch[pt][i]]; mx = max(mx, size[ch[pt][i]]); } if (max(mx, tot - size[pt]) < minn) zx = pt, minn = max(mx, tot - size[pt]); } } void digui(int pt, int me) { if (bv[pt] || (!ch[pt][0] && !ch[pt][1])) { int cjr = gpt; gpt++; ch[fa[pt]][(pt == ch[fa[pt]][1])] = cjr; fa[cjr] = fa[pt]; fa[me] = cjr; fa[pt] = cjr; ch[cjr][0] = me; ch[cjr][1] = pt; return; } tot = 0; afs(pt); minn = 1234567890; bfs(pt); int lef = zx; while (ch[lef][0]) lef = ch[lef][0]; int rgh = zx; while (ch[rgh][1]) rgh = ch[rgh][1]; printf("%d %d %d\n", lef, rgh, me); fflush(stdout); char c; cin >> c; bv[zx] = 1; if (c == 'Z') { digui(ch[zx][0], me); return; } if (c == 'Y') { digui(ch[zx][1], me); return; } if (c == 'X') { if (bv[fa[zx]] || !fa[zx]) { int cjr = gpt; gpt++; fa[cjr] = fa[zx]; ch[fa[zx]][(zx == ch[fa[zx]][1])] = cjr; ch[cjr][0] = zx; ch[cjr][1] = me; fa[zx] = cjr; fa[me] = cjr; return; } int cur = fa[zx]; while (!bv[fa[cur]] && fa[cur]) cur = fa[cur]; digui(cur, me); } } void ywymain() { int n; cin >> n; gpt = n + 2; ch[n + 1][0] = 1; ch[n + 1][1] = 2; fa[1] = fa[2] = n + 1; for (register int i = 3; i <= n; i++) { memset(bv, 0, sizeof(bv)); int cur = 1; while (fa[cur]) cur = fa[cur]; digui(cur, i); } printf("-1\n"); fflush(stdout); for (register int i = 1; i <= n * 2 - 1; i++) { if (!fa[i]) printf("-1 "); else printf("%d ", fa[i]); } cout << endl; fflush(stdout); } } // namespace ywy int main() { ywy::ywymain(); return (0); }
5
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; using ll = long long; using P = pair<ll, ll>; template <class T> using V = vector<T>; const ll inf = (1e18); const ll mod = 1000000007; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll lcm(ll c, ll d) { return c / gcd(c, d) * d; } struct __INIT { __INIT() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(15); } } __init; 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 (a > b) { a = b; return 1; } return 0; } ll dp[505][505], able[505][505]; ll solve(ll l, ll r) { if (dp[l][r]) return dp[l][r]; if (able[l][r]) return dp[l][r] = 1; ll res = inf; for (ll i = l + 1; i < r; i++) chmin(res, solve(l, i) + solve(i, r)); return dp[l][r] = res; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> able[i][i + 1]; for (int i = 2; i <= n; i++) { for (int j = 0; j <= n - i; j++) { for (int k = j + 1; k < i + j; k++) { if (able[j][k] && able[j][k] == able[k][i + j]) able[j][i + j] = able[j][k] + 1; } } } cout << solve(0, n) << "\n"; }
5
#include <bits/stdc++.h> using namespace std; const int N = 200500; int d, n, m; pair<int, int> x[N]; struct Cmp { bool operator()(int a, int b) const { if (x[a].second != x[b].second) return x[a].second < x[b].second; return a < b; } }; set<int, Cmp> tanks; void solve() { scanf("%d%d%d", &d, &n, &m); for (int i = 0; i < m; i++) scanf("%d%d", &x[i].first, &x[i].second); sort(x, x + m); int ptr = 0; long long cost = 0; int last_fueled_at = 0; int last_fueled_how = n; while (last_fueled_at + last_fueled_how < d) { while (ptr < m && x[ptr].first <= last_fueled_at + last_fueled_how) { tanks.insert(ptr); ptr++; } if (tanks.empty()) break; int id = *tanks.begin(); if (x[id].first < last_fueled_at) { tanks.erase(tanks.begin()); continue; } int can = min(d - (last_fueled_at + last_fueled_how), n - (last_fueled_how - (x[id].first - last_fueled_at))); assert(can >= 0); if (can == 0) { tanks.erase(tanks.begin()); continue; } if (ptr < m) can = min(can, x[ptr].first - (last_fueled_at + last_fueled_how)); cost += (long long)can * x[id].second; if (last_fueled_at == x[id].first) last_fueled_how += can; else { last_fueled_how = (last_fueled_how - (x[id].first - last_fueled_at)) + can; last_fueled_at = x[id].first; } } if (last_fueled_at + last_fueled_how >= d) printf("%lld\n", cost); else printf("-1\n"); } int main() { solve(); 0; return 0; }
4
#include <bits/stdc++.h> using namespace std; inline int read() { int X = 0, w = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') w = -1; c = getchar(); } while (c >= '0' && c <= '9') X = X * 10 + c - '0', c = getchar(); return X * w; } const int N = 100 + 10; inline int id(int x, int y) { if (x & 1) return (x - 1) * 10 + y; else return (x - 1) * 10 + 11 - y; } int to[N]; double dp[N]; int main() { for (register int i = 1; i <= 10; ++i) for (register int j = 1; j <= 10; ++j) to[id(i, j)] = id(i - read(), j); for (register int i = 2; i <= 6; ++i) { for (register int j = 1; j < i; ++j) dp[i] += dp[j] / 6; dp[i] = (dp[i] + 1) / (i - 1) * 6; } for (register int i = 7; i <= 100; ++i) { for (register int j = 1; j <= 6; ++j) dp[i] += min(dp[i - j], dp[to[i - j]]); dp[i] = dp[i] / 6 + 1; } printf("%.10lf\n", dp[100]); return 0; }
5
#include <bits/stdc++.h> using namespace std; template <class T> void _R(T &x) { cin >> x; } void _R(int &x) { scanf("%d", &x); } void _R(long long &x) { scanf("%lld", &x); } void _R(double &x) { scanf("%lf", &x); } void _R(char &x) { scanf(" %c", &x); } void _R(char *x) { scanf("%s", x); } void R() {} template <class T, class... U> void R(T &head, U &...tail) { _R(head); R(tail...); } template <class T> void _W(const T &x) { cout << x; } void _W(const int &x) { printf("%d", x); } void _W(const long long &x) { printf("%lld", x); } void _W(const double &x) { printf("%.16f", x); } void _W(const char &x) { putchar(x); } void _W(const char *x) { printf("%s", x); } template <class T, class U> void _W(const pair<T, U> &x) { _W(x.first); putchar(' '); _W(x.second); } template <class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); } void W() {} template <class T, class... U> void W(const T &head, const U &...tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); } int MOD = 1e9 + 7; void ADD(long long &x, long long v) { x = (x + v) % MOD; if (x < 0) x += MOD; } const int SIZE = 1e6 + 10; int calc(int t, int low1, int low2, int n1, int n2, int a, int b) { if (t <= 0) return 0; if ((long long)low1 * a + (long long)low2 * b > t) return 0; int res = low1 + low2; n1 -= low1; n2 -= low2; t -= low1 * a + low2 * b; int v = min(n1, t / a); res += v; t -= a * v; v = min(n2, t / b); res += v; return res; } void solve() { int n, T, a, b; R(n, T, a, b); vector<int> p(n); for (int i = 0; i < (n); ++i) R(p[i]); vector<int> gg[2]; int mi = 1e9; for (int i = 0; i < (n); ++i) { int t; R(t); gg[p[i]].push_back(t); mi = min(mi, t); } for (int i = 0; i < (2); ++i) sort((gg[i]).begin(), (gg[i]).end()); int it[2] = {}; int an = calc(mi - 1, 0, 0, ((int)(gg[0]).size()), ((int)(gg[1]).size()), a, b); while (it[0] < ((int)(gg[0]).size()) || it[1] < ((int)(gg[1]).size())) { mi = 1e9; for (int i = 0; i < (2); ++i) if (it[i] < ((int)(gg[i]).size())) mi = min(mi, gg[i][it[i]]); for (int i = 0; i < (2); ++i) { if (it[i] < ((int)(gg[i]).size()) && mi == gg[i][it[i]]) { it[i]++; } } int t = T; for (int i = 0; i < (2); ++i) { if (it[i] < ((int)(gg[i]).size())) t = min(t, gg[i][it[i]] - 1); } an = max(an, calc(t, it[0], it[1], ((int)(gg[0]).size()), ((int)(gg[1]).size()), a, b)); } W(an); } int main() { int ___T; scanf("%d", &___T); for (int cs = 1; cs <= ___T; cs++) { solve(); } return 0; }
3
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("-ffloat-store") using namespace std; clock_t time_p = clock(); void aryanc403() {} const long long int INF = 0xFFFFFFFFFFFFFFFL; long long int seed; mt19937 rng(seed = chrono::steady_clock::now().time_since_epoch().count()); inline long long int rnd(long long int l = 0, long long int r = INF) { return uniform_int_distribution<long long int>(l, r)(rng); } class CMP { public: bool operator()(pair<long long int, long long int> a, pair<long long int, long long int> b) { return !(a.first < b.first || (a.first == b.first && a.second <= b.second)); } }; void add(map<long long int, long long int> &m, long long int x, long long int cnt = 1) { auto jt = m.find(x); if (jt == m.end()) m.insert({x, cnt}); else jt->second += cnt; } void del(map<long long int, long long int> &m, long long int x, long long int cnt = 1) { auto jt = m.find(x); if (jt->second <= cnt) m.erase(jt); else jt->second -= cnt; } bool cmp(const pair<long long int, long long int> &a, const pair<long long int, long long int> &b) { return a.first < b.first || (a.first == b.first && a.second < b.second); } const long long int mod = 998244353LL; long long int T, n, i, j, k, in, cnt, l, r, u, v, x, y; long long int m; string s; vector<long long int> a; const int N = 2e5 + 5; long long int fac[N + 5], inv[N + 5]; long long int po(long long int a, long long int n) { if (n < 0) { a = po(a, mod - 2); n *= -1; } if (n == 0) return 1; if (n % 2) return (a * po(a, n - 1)) % mod; return po(a * a % mod, n / 2); } long long int nCr(long long int n, long long int r) { if (n < 0 || r < 0 || r > n) return 0; long long int ans = 1; ans = (ans * fac[n]) % mod; ans = (ans * inv[r]) % mod; ans = (ans * inv[n - r]) % mod; return ans; } void pre(long long int n) { long long int i; fac[0] = 1; for (i = (1); i <= (n); ++i) fac[i] = (i * fac[i - 1]) % mod; inv[n] = po(fac[n], mod - 2); for (i = (n); i >= (1); --i) inv[i - 1] = (i * inv[i]) % mod; assert(inv[0] == 1); } int main(void) { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); { cin >> n >> k; a.clear(); a.reserve(n + 2); a.push_back(0); for (i = 0; i < (n); ++i) { cin >> in; a.push_back(in); } a.push_back(a[1]); a[0] = a[n]; for (i = (1); i <= (n); ++i) cnt += (a[i] != a[i - 1]); pre(cnt); long long int ans = 0; for (i = (0); i <= (cnt); ++i) { long long int v = 1; v = (v * nCr(cnt, i)) % mod; v = (v * nCr(cnt - i, i)) % mod; v = (v * po(k - 2, cnt - 2 * i)) % mod; ans -= v; ; } ans %= mod; ans *= po(k, n - cnt); ans %= mod; ans += po(k, n); ans %= mod; ans *= (mod + 1) / 2; ans %= mod; ans += mod; ans %= mod; cout << ans << "\n"; } aryanc403(); return 0; }
4
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-9; int main() { double a, b, c, d, tmp, ans, tt; while (~scanf("%lf%lf%lf%lf", &a, &b, &c, &d)) { tmp = (1.0 - a / b) * (1.0 - c / d); tt = tmp * a / b; ans = a / b; while (tt > EPS) { ans += tt; tt *= tmp; } printf("%.7lf\n", ans); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); long long int i, ct = 0; string s, g, p, prev; cin >> s; set<string> s1, s2; s1.insert("0"); s1.insert("4"); s1.insert("8"); s2.insert("00"); s2.insert("04"); s2.insert("08"); for (i = 12; i <= 100; i += 4) { p = to_string(i); s2.insert(p); } for (i = 0; i < s.length(); i++) { p = s[i]; if (s1.count(p) != 0) ct++; } prev = s[0]; for (i = 1; i < s.length(); i++) { g = s[i]; g = prev + s[i]; if (s2.count(g) != 0) ct = ct + i; prev = s[i]; } cout << ct << "\n"; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, i; int xx[100100]; while (scanf("%d", &n) != EOF) { int temp; int max = 0; int min = 20000; memset(xx, 0, sizeof(xx)); map<int, int> he; he.clear(); for (i = 0; i < n; i++) { scanf("%d", &xx[i]); if (xx[i] >= max) max = xx[i]; he[xx[i]] += 2; } bool ans = 1; map<int, int>::iterator pre = he.begin(); for (map<int, int>::iterator it = ++he.begin(); it != he.end(); it++) { if (it->first != pre->first + 1) { ans = 0; break; } it->second = it->second - pre->second; if (it->second <= 0 && it->first != max) { ans = 0; break; } pre = it; } pre = --he.end(); ans = ans && (pre->second == 0); ans ? printf("YES\n") : printf("NO\n"); } }
4
#include <bits/stdc++.h> using namespace std; const int M = 1e9 + 7; const int MOD = 1e9 + 7; const double PI = 3.141592653589793238460; long long power(long long a, long long b) { long long res = 1; if (a == 0) return 0; if (a == 1) return 1; for (; b > 0; b >>= 1) { if (b & 1) res = (res * a); if (res > MOD) res %= MOD; a = (a * a); if (a > MOD) a = a % MOD; } return res; } bool isPrime[1000000 + 1]; vector<long long> prime; void seive(int limit) { isPrime[0] = isPrime[1] = 1; for (int i = 2; i * i <= limit; i++) { if (!isPrime[i]) for (int j = i * i; j <= limit; j += i) { isPrime[j] = 1; } } for (int i = 2; i <= limit; i++) if (!isPrime[i]) prime.push_back(i); } vector<int> v[26]; int dp[5001][5001]; int recur(int a, int b, string s, string t) { if (dp[a][b] != -1) return dp[a][b]; if (a == s.size()) return 0; if (b == t.size()) return 0; int tot = 0; tot += recur(a, b + 1, s, t); tot %= M; if (s[a] == t[b]) { tot += recur(a + 1, b + 1, s, t) + 1; tot %= M; } return dp[a][b] = tot; } void solve() { string s; string t; cin >> s >> t; for (int i = 0; i <= s.length(); i++) for (int j = 0; j <= t.length(); j++) dp[i][j] = 0; int rs = 0; for (int i = s.length() - 1; i >= 0; i--) { for (int j = t.length() - 1; j >= 0; j--) { if (s[i] == t[j]) { dp[i][j] = dp[i + 1][j + 1] + 1; if (dp[i][j] > M) dp[i][j] %= M; } dp[i][j] += dp[i][j + 1]; dp[i][j] %= M; } } for (int i = 0; i < s.length(); i++) { rs = (rs + dp[i][0]) % M; } cout << rs; } int main() { ios_base::sync_with_stdio(false), cin.tie(0); int tt; tt = 1; while (tt--) { solve(); } }
1
#include <bits/stdc++.h> using namespace std; int main() { int a; cin >> a; int n = 2 * (a - 1), m = 2; int d[2] = {1, 2}; if (a == 1) n = m = 1; cout << n << " " << m << endl; for (int i = 0; i < m; i++) cout << d[i] << (i == m - 1 ? "\n" : " "); }
1
#include <cstdio> #include <math.h> using namespace std; typedef struct vec2 { double x, y; vec2(){} vec2(int a, int b) { x = a; y = b; } vec2(double a, double b) { x = a; y = b; } vec2 operator*(double l) { return{ x*l,y*l }; } vec2 operator-(const vec2 &r) { return{ x - r.x,y - r.y }; } vec2 operator+(const vec2 &r) { return{ x + r.x,y + r.y }; } double dis() { return sqrt(pow(x, 2) + pow(y, 2)); } } vec2; double dot(vec2 v1, vec2 v2) { return v1.x*v2.x + v1.y*v2.y; } vec2 v[4]; int q, t1, t2; void read(int i) { scanf("%d%d", &t1, &t2); v[i] = vec2(t1, t2); } int main(){ read(0); read(1); v[1] = v[1] - v[0]; scanf("%d", &q); for (int I = 0; I < q; I++) { read(2); v[2] = v[2] - v[0]; v[3] = v[1] * (dot(v[1], v[2]) *(1.0 / pow(v[1].dis(),2))); v[3] = v[0] + v[3]; printf("%.9f %.9f\n", v[3].x, v[3].y); } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long tests; long long n, a[1000256], b[1000256], cnt, ans; void solve() { cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i] >> b[i]; if (a[i] - a[i - 1] < cnt) { cnt += b[i]; cnt -= a[i] - a[i - 1]; } else cnt = b[i]; if (ans < cnt) ans = cnt; } cout << a[n] + cnt << " " << ans; } int main() { solve(); }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, k = 0; string s, t; cin >> n; cin >> s >> t; vector<int> vect; for (int i = 0; i < n; i++) { if (s[i] == t[i]) continue; else { int flag = -1; for (int j = i + 1; j < n; j++) { if (s[j] == t[i]) { flag = j; break; } } if (flag == -1) { cout << "-1"; return 0; } for (int j = flag - 1; j >= i; j--) { swap(s[j], s[j + 1]); vect.push_back(j + 1); k++; } } } cout << k << endl; for (int i = 0; i < k; i++) cout << vect[i] << " "; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; string s; cin >> s; for (char ch = 'z'; ch >= 'a'; ch--) { for (long long int i = 1; i < s.length(); i++) { if (s[i] == ch && s[i - 1] + 1 == ch) s.erase(i, 1), i--; } for (long long int i = s.length() - 1; i >= 0; i--) { if (s[i] == ch && s[i + 1] + 1 == ch) s.erase(i, 1), i++; } } cout << n - s.length() << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; void c_p_c() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); } int32_t main() { c_p_c(); long long x; cin >> x; while (x--) { long long a, b; cin >> a >> b; if (a == b) cout << 0 << '\n'; else if (b > a) { if (a % 2 == 0 && b % 2 == 0) { cout << 2 << '\n'; } else if (a % 2 == 0 && b % 2 != 0) { cout << 1 << '\n'; } else if (a % 2 != 0 && b % 2 == 0) { cout << 1 << '\n'; } else { cout << 2 << '\n'; } } else { if (a % 2 == 0 && b % 2 == 0) { cout << 1 << '\n'; } else if (a % 2 == 0 && b % 2 != 0) { cout << 2 << '\n'; } else if (a % 2 != 0 && b % 2 == 0) { cout << 2 << '\n'; } else { cout << 1 << '\n'; } } } }
1
#include <bits/stdc++.h> using namespace std; int main() { int visit[1000] = { 0, }; int n; int k = 0; int i; cin >> n; int cnt = 0; for (i = 0; i < n; i++) { k = (k + i) % n; if (visit[k] == 0) { cnt++; visit[k] = 1; } } cout << (cnt == n ? "YES" : "NO") << endl; return 0; }
1
#include<iostream> #include<cstdio> #include<cmath> using namespace std; int main() { int n,m; cin>>n; m=sqrt(n); cout<<m*m; }
0
#include <bits/stdc++.h> using namespace std; int n; int ask(int i, int j) { printf("? %d %d\n", i, j); fflush(stdout); char c; cin >> c; if (c == '<') return 1; else if (c == '=') return 2; else return 3; } void solve() { scanf("%d", &n); if (n == 1) { printf("! 1 1\n"); fflush(stdout); return; } vector<int> v1, v2; for (int i = 2; i <= n; i += 2) { v1.push_back(i - 1); v2.push_back(i); if (ask(i - 1, i) > 2) { swap(v1.back(), v2.back()); } } if (n & 1) { if (ask(n - 1, n) <= 2) { v2.push_back(n); } else { v1.push_back(n); } } while (v1.size() > 1) { vector<int> vec; for (int i = 1; i < v1.size(); i += 2) { if (ask(v1[i - 1], v1[i]) <= 2) { vec.push_back(v1[i - 1]); } else { vec.push_back(v1[i]); } } if (v1.size() & 1) { vec.push_back(v1.back()); } v1 = vec; } while (v2.size() > 1) { vector<int> vec; for (int i = 1; i < v2.size(); i += 2) { if (ask(v2[i - 1], v2[i]) <= 2) { vec.push_back(v2[i]); } else { vec.push_back(v2[i - 1]); } } if (v2.size() & 1) { vec.push_back(v2.back()); } v2 = vec; } printf("! %d %d\n", v1[0], v2[0]); fflush(stdout); } int main() { int test; scanf("%d", &test); while (test--) { solve(); } }
2
#include <bits/stdc++.h> using namespace std; const int N = 200010; int n, k, s, t; long long g[N]; struct Node { int vol, p; Node() {} bool operator<(const Node &rhs) const { return (vol == rhs.vol) ? p < rhs.p : vol < rhs.vol; } } seq[N]; bool judge(int cap) { long long tot = 0; for (int i = 1; i <= k + 1; ++i) { if (cap < g[i] - g[i - 1]) { return false; } tot += max(g[i] - g[i - 1], (g[i] - g[i - 1]) * 3 - (long long)cap); if (tot > (long long)t) return false; } return true; } int main() { scanf("%d%d%d%d", &n, &k, &s, &t); for (int i = 0; i < n; ++i) scanf("%d%d", &seq[i].p, &seq[i].vol); for (int i = 1; i <= k; ++i) scanf("%I64d", g + i); g[0] = 0; g[k + 1] = s; sort(g, g + k + 1); sort(seq, seq + n); long long l = 0, r = 2000000000ll, mid; while (l < r) { mid = (l + r) >> 1; if (judge(mid)) { r = mid; } else { l = mid + 1; } } int ans = -1; for (int i = 0; i < n; ++i) { if (ans == -1 && seq[i].vol >= l || seq[ans].p > seq[i].p) { ans = i; } } if (ans < 0) puts("-1"); else printf("%d\n", seq[ans].p); return 0; }
3
#include<iostream> #include<string> using namespace std; int main() { int n, h, m, d, guest[3], ok[3]; char c; string s[3] = { "lunch", "dinner", "midnight" }; while( cin >> n ) { if( !n ) break; guest[0] = guest[1] = guest[2] = 0; ok[0] = ok[1] = ok[2] = 0; for( int i = 0; i < n; i++ ) { cin >> h >> c >> m >> d; int t = 3; if( 11 <= h && h <= 14 ) t = 0; if( 18 <= h && h <= 20 ) t = 1; if( 21 <= h && h <= 23 || h == 0 || h == 1 ) t = 2; if( t == 3 ) continue; guest[t]++; if( m > d ) d += 60; if( d - m <= 8 ) ok[t]++; } for( int i = 0; i < 3; i++ ) { if( guest[i] == 0 ) cout << s[i] << " no guest" << endl; else cout << s[i] << " " << 100 * ok[i] / guest[i] << endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int M = 100000 + 10; const long long inf = 1e16; int n, Q, s; int mp[M]; vector<vector<long long> > T[M << 2]; long long d[M << 2]; bool vis[M << 2]; bool flag[M << 2]; bool leaf[M << 2]; bool flag2[M << 2]; priority_queue<vector<long long>, vector<vector<long long> >, greater<vector<long long> > > q; struct Segtree { void build(int o, int l, int r) { if (l == r) { mp[l] = o; leaf[o] = true; return; } int mid = l + r >> 1; build(o << 1, l, mid); build(o << 1 | 1, mid + 1, r); } void ins1(int o, int l, int r, int pl, int pr, int u, int w) { if (pl <= l && r <= pr) { T[o].emplace_back(vector<long long>{w, u}); return; } int mid = l + r >> 1; if (pl <= mid) ins1(o << 1, l, mid, pl, pr, u, w); if (pr > mid) ins1(o << 1 | 1, mid + 1, r, pl, pr, u, w); } void ins2(int o, int l, int r, int pl, int pr, int u, int w) { if (pl <= l && r <= pr) { T[u].emplace_back(vector<long long>{w, o}); return; } int mid = l + r >> 1; if (pl <= mid) ins2(o << 1, l, mid, pl, pr, u, w); if (pr > mid) ins2(o << 1 | 1, mid + 1, r, pl, pr, u, w); } void update(int o, long long dis) { if (flag2[o]) return; flag2[o] = true; for (vector<long long> &v : T[o]) { if (dis + v[0] < d[v[1]]) { d[v[1]] = dis + v[0]; q.push(vector<long long>{d[v[1]], v[1]}); } } if (o == 1) return; update(o >> 1, dis); } void down(int o, long long dis) { if (flag[o]) return; flag[o] = true; if (dis < d[o]) { d[o] = dis; q.push(vector<long long>{d[o], o}); } if (leaf[o]) return; down(o << 1, dis); down(o << 1 | 1, dis); } } sgt; void dij() { for (int i = 1; i <= n << 2; ++i) { d[i] = inf; } d[mp[s]] = 0; q.push(vector<long long>{0, mp[s]}); while (!q.empty()) { vector<long long> now = q.top(); q.pop(); if (vis[now[1]]) continue; vis[now[1]] = true; sgt.down(now[1], now[0]); sgt.update(now[1], now[0]); } } int main() { scanf("%d%d%d", &n, &Q, &s); sgt.build(1, 1, n); int op, u, v, w, l, r; for (int i = 0; i < Q; ++i) { scanf("%d", &op); if (op == 1) { scanf("%d%d%d", &u, &v, &w); sgt.ins1(1, 1, n, u, u, mp[v], w); } else if (op == 2) { scanf("%d%d%d%d", &v, &l, &r, &w); sgt.ins2(1, 1, n, l, r, mp[v], w); } else { scanf("%d%d%d%d", &v, &l, &r, &w); sgt.ins1(1, 1, n, l, r, mp[v], w); } } dij(); for (int i = 1; i <= n; ++i) { printf("%I64d%c", d[mp[i]] == inf ? -1 : d[mp[i]], " \n"[i == n]); } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MX = 250005; const long long inf = 1e12; struct Treap { Treap *left, *right; int val, key, size; long long sum; Treap(int val = 0) : val(val), key(rand()), size(1), left(0), right(0), sum(val) {} } * t[MX]; inline int nodeSize(Treap* T) { return T ? T->size : 0; } inline long long nodeSum(Treap* T) { return T ? T->sum : 0; } inline void update(Treap* T) { if (T) { T->size = 1 + nodeSize(T->left) + nodeSize(T->right); T->sum = T->val + nodeSum(T->left) + nodeSum(T->right); } } void merge(Treap*& T, Treap* T1, Treap* T2) { if (!T1) T = T2; else if (!T2) T = T1; else if (T1->key > T2->key) merge(T1->right, T1->right, T2), T = T1; else merge(T2->left, T1, T2->left), T = T2; update(T); } void split(Treap* T, int x, Treap*& T1, Treap*& T2) { if (!T) return void(T1 = T2 = NULL); if (x < T->val) split(T->left, x, T1, T->left), T2 = T; else split(T->right, x, T->right, T2), T1 = T; update(T); } void insert(Treap*& T, Treap* x) { if (!T) T = x; else if (x->key > T->key) split(T, x->val, x->left, x->right), T = x; else insert(x->val < T->val ? T->left : T->right, x); update(T); } void insert(Treap*& T, int x) { insert(T, new Treap(x)); } void erase(Treap*& T, int x) { if (!T) return; if (T->val == x) merge(T, T->left, T->right); else erase(x < T->val ? T->left : T->right, x); update(T); } long long kth(Treap* T, int i) { if (!T || i < 1) return 0; int curr = nodeSize(T->left); if (i == curr + 1) return nodeSum(T->left) + T->val; if (i < curr + 1) return kth(T->left, i); return kth(T->right, i - curr - 1) + nodeSum(T->left) + T->val; } int n, a, b, c, x, vis[MX], p[MX]; long long res[MX], mem[2][MX]; vector<int> deg[MX], in; vector<pair<int, int> > adj[MX], gra[MX]; bitset<MX> bs; long long dp(int u, int p, int f) { long long& res = mem[f][u]; if (res != -1) return res; res = inf; vis[u] = x; long long s = 0; vector<long long> aux; for (pair<int, int>& v : gra[u]) if (v.first != p) { s += dp(v.first, u, 0); aux.push_back(dp(v.first, u, 1) - dp(v.first, u, 0) + v.second); } sort(aux.begin(), aux.end()); for (int k = 0; k <= aux.size(); k++) { int d = (int)adj[u].size() - x - f; if (d - k <= nodeSize(t[u])) res = min(res, s + kth(t[u], d - k)); if (k < aux.size()) s += aux[k]; } return res; } int main() { memset(mem, -1, sizeof(mem)); srand(time(0)); scanf("%d", &n); for (int i = 1; i < n; i++) { scanf("%d %d %d", &a, &b, &c); adj[a].emplace_back(b, c); adj[b].emplace_back(a, c); } for (int i = 1; i <= n; i++) deg[adj[i].size()].push_back(i); for (x = n - 1; x >= 0; x--) { for (int u : in) if (mem[0][u] == -1) res[x] += dp(u, -1, 0); for (int u : in) mem[0][u] = mem[1][u] = -1; for (int u : deg[x]) { bs[u] = 1; in.push_back(u); for (pair<int, int>& v : adj[u]) if (bs[v.first]) { erase(t[v.first], v.second); gra[u].emplace_back(v.first, v.second); gra[v.first].emplace_back(u, v.second); } else { insert(t[u], v.second); } } } for (int i = 0; i < n; i++) printf("%I64d ", res[i]); printf("\n"); return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.precision(10); cout << fixed; srand(time(0)); auto print = [&](long long x, long long y) { cout << x << " " << y; exit(0); }; long long n; cin >> n; if (n == 0) { cout << "0 0"; return 0; } long long l = 0, r = 1e9, a = 0; while (l <= r) { long long mid = (l + r) / 2; long long x = (mid * (mid + 1) / 2) * 6; if (x < n) { a = mid; l = mid + 1; } else { r = mid - 1; } } a++; long long b = (n - (a * (a - 1) / 2) * 6) - 1; long long x = 1 + 2 * (a - 1); long long y = 2; if (b > (a - 1)) { b -= (a - 1); x = x - (a - 1); y = y + 2 * (a - 1); } else { x = x - b; y = y + 2 * b; print(x, y); } if (b > a) { b -= a; x = x - 2 * a; y = y; } else { x = x - 2 * b; y = y; print(x, y); } if (b > a) { b -= a; x = x - a; y = y - 2 * a; } else { x = x - b; y = y - 2 * b; print(x, y); } if (b > a) { b -= a; x = x + a; y = y - 2 * a; } else { x = x + b; y = y - 2 * b; print(x, y); } if (b > a) { b -= a; x = x + 2 * a; y = y; } else { x = x + 2 * b; y = y; print(x, y); } if (b > a) { b -= a; x = x + a; y = y + 2 * a; } else { x = x + b; y = y + 2 * b; print(x, y); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n; vector<vector<int>> graph; int child[N]; int level[N]; int dfs(int s) { int ret = 0; for (int i = 0; i < (int)graph[s].size(); i++) { int v = graph[s][i]; level[v] = 1 + level[s]; ret += 1 + dfs(v); } return child[s] = ret; } int main() { ios_base::sync_with_stdio(false); cin >> n; graph.clear(); graph.resize(n); for (int i = 1; i < n; i++) { int x; cin >> x; x--; graph[x].push_back(i); } level[0] = 1; dfs(0); double ans; for (int i = 0; i < n; i++) { ans = (level[i] + n - child[i]) / 2.0; cout << fixed << setprecision(1) << ans << " "; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; vector<long long> a(n); for (long long &i : a) cin >> i; cout << "1 1\n" << -a[0] << '\n'; a[0] = 0; if (n == 1) { cout << "1 1\n0\n1 1\n0\n"; return 0; } cout << "2 " << n << '\n'; for (int i = 1; i < n; i++) { long long d = a[i] % n; if (d < 0) d += n; cout << (n - 1) * d << " "; a[i] += (n - 1) * d; } cout << '\n'; cout << "1 " << n << '\n'; for (int i = 0; i < n; i++) { cout << -a[i] << " "; } cout << '\n'; }
3
#include <bits/stdc++.h> using namespace std; int n; double ma[100005], mb[100005], a[100005], b[100005], prep[100005], preq[100005]; double f(double x, double y) { double tmp = (x - y + 1) * (x - y + 1) - 4 * x; tmp = max(tmp, 0.0); return sqrt(tmp); } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%lf", &ma[i]); for (int i = 1; i <= n; i++) scanf("%lf", &mb[i]); for (int i = 1; i <= n; i++) a[i] = a[i - 1] + ma[i]; for (int i = n; i >= 1; i--) b[i] = b[i + 1] + mb[i + 1]; for (int i = 1; i <= n; i++) { prep[i] = (a[i] - b[i] + 1 - f(a[i], b[i])) / 2; preq[i] = (a[i] - b[i] + 1 + f(a[i], b[i])) / 2; } for (int i = 1; i <= n; i++) printf("%.8lf ", prep[i] - prep[i - 1]); printf("\n"); for (int i = 1; i <= n; i++) printf("%.8lf ", preq[i] - preq[i - 1]); printf("\n"); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 412345; const int MAXINT = 2047483098; const long long int MOD = 1e9 + 7; const int MAX = 1e4; const long double pi = 2 * acosl(0); const long double EPS = 1e-10; int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } bool isPrime(long long unsigned n) { if (n <= 1) return false; if (n <= 3) return true; if (n % 2 == 0 || n % 3 == 0) return false; for (int i = 5; i * i <= n; i = i + 6) if (n % i == 0 || n % (i + 2) == 0) return false; return true; } int nCr(int n, int r) { int res = 1; if (n < r) return -1; r = min(r, n - r); for (long long unsigned int i = 0; i < r; i++) { res *= (n - i); res /= (i + 1); } return res; } int nPr(int n, int r) { int res = 1; if (n < r) return -1; for (long long unsigned int i = 0; i < r; i++) res *= (n - i); return res; } vector<long long int> vec[100010]; vector<long long int> nex(100010, 0); map<pair<long long int, long long int>, bool> mp; unordered_map<long long int, long long int> pre; void func(long long int node, long long int prev) { for (auto x : vec[node]) { if (x != prev) { func(x, node); pre[x] = node; } } for (auto x : vec[node]) { if (x != prev) { nex[node] += nex[x]; long long int a = min(node, x); long long int b = max(node, x); if (mp[{a, b}]) { nex[node]++; } } } } void solve() { long long int n, l, x, y; cin >> n >> l >> x >> y; vector<long long int> vec(n); for (long long unsigned int i = 0; i < n; i++) { cin >> vec[i]; } bool cool = false; bool school = false; for (long long int i = 0; i < n; i++) { long long int a = vec[i] + x; long long int b = vec[i] + y; if (binary_search(vec.begin(), vec.end(), a)) cool = true; if (binary_search(vec.begin(), vec.end(), b)) { school = true; } } if (cool && school) { cout << "0"; return; } else if (cool || school) { cout << "1" << '\n'; if (cool) { cout << vec[0] + y; } else if (school) { cout << vec[0] + x; } } else { for (long long int i = 0; i < n; i++) { long long int k = vec[i] + x; if (binary_search(vec.begin(), vec.end(), k + y) || binary_search(vec.begin(), vec.end(), k - y)) { if (k <= l) { cout << 1 << '\n'; cout << k; return; } } k = vec[i] + y; if (binary_search(vec.begin(), vec.end(), k + x) || binary_search(vec.begin(), vec.end(), k - x)) { if (k <= l) { cout << 1 << '\n'; cout << k; return; } } k = vec[i] - x; if (binary_search(vec.begin(), vec.end(), k + y) || binary_search(vec.begin(), vec.end(), k - y)) { if (k <= l && k >= 0) { cout << 1 << '\n'; cout << k; return; } } k = vec[i] - y; if (binary_search(vec.begin(), vec.end(), k + x) || binary_search(vec.begin(), vec.end(), k - x)) { if (k <= l && k >= 0) { cout << 1 << '\n'; cout << k; return; } } } cout << 2 << '\n'; cout << vec[0] + x << " " << vec[0] + y; return; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int T = 1; while (T--) { solve(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; long long int pow_ar[26]; vector<long long int> primes; long long int sieve[105]; long long int pascal_triangle[5005][5005]; long long int dx[] = {1, -1, 0, 0}; long long int dy[] = {0, 0, 1, -1}; long long int mod_pow(long long int n, long long int p, long long int mod) { long long int res = 1; while (p) { if (p & 1) res = (res * n) % mod; n = (n * n) % mod; p /= 2; } return res; } long long int mod_inv(long long int n, long long int mod) { return mod_pow(n, mod - 2, mod); } long long int mod_divison(long long int dividend, long long int divisor, long long int mod) { return (dividend * mod_inv(divisor, mod)) % mod; } long long int gcd(long long int a, long long int b) { long long int temp; if (a < b) { temp = a; a = b; b = temp; } while (a % b) { temp = a; a = b; b = temp % b; } return b; } void generate_power(long long int n, long long int p) { pow_ar[0] = 1; for (long long int i = 1; i < p; i++) pow_ar[i] = pow_ar[i - 1] * n; } void calc_sieve() { for (long long int i = 0; i < 105; i++) sieve[i] = 0; sieve[0] = sieve[1] = -1; for (long long int i = 2; i < 105; i++) { if (sieve[i] == 0) { primes.push_back(i); for (long long int j = i; j < 105; j = j + i) { sieve[j]++; } } } } void generate_pascal_triangle(long long int n) { pascal_triangle[0][0] = 1; for (int i = 1; i < n; i++) { pascal_triangle[i][0] = 1; for (int j = 1; j < i; j++) pascal_triangle[i][j] = (pascal_triangle[i - 1][j - 1] + pascal_triangle[i - 1][j]) % 1000000007L; pascal_triangle[i][i] = 1; } } string word; long long int digits[10]; vector<long long int> positions[10]; char num[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; int main() { long long int n, k; cin >> n >> k; cin >> word; for (long long int i = 0; i < word.length(); i++) digits[word[i] - '0']++, positions[word[i] - '0'].push_back(i); for (long long int i = 0; i <= 9; i++) { if (digits[i] >= k) { cout << "0 \n" << word << endl; return 0; } } long long int ans = n * 12; string ans_word; for (long long int i = 0; i <= 9; i++) { string temp_word = word; long long int temp_cost = 0; long long int temp = digits[i]; long long int cur_cost = 1, pos_small, pos_large; if (i - cur_cost >= 0) pos_small = positions[i - cur_cost].size() - 1; if (i + cur_cost < 10) pos_large = 0; while (temp < k && (i - cur_cost >= 0 || i + cur_cost <= 9)) { if ((i - cur_cost < 0 || pos_small < 0 || positions[i - cur_cost].size() == 0) && (i + cur_cost > 9 || positions[i + cur_cost].size() == 0 || pos_large == positions[i + cur_cost].size())) { cur_cost++; if (i - cur_cost >= 0) pos_small = positions[i - cur_cost].size() - 1; if (i + cur_cost < 10) pos_large = 0; } if (i + cur_cost <= 9 && pos_large < positions[i + cur_cost].size() && positions[i + cur_cost].size() != 0) { temp_cost += cur_cost; temp_word[positions[i + cur_cost][pos_large]] = num[i]; pos_large++; temp++; } else if (i - cur_cost >= 0 && pos_small >= 0 && positions[i - cur_cost].size() != 0) { temp_cost += cur_cost; temp_word[positions[i - cur_cost][pos_small]] = num[i]; pos_small--; temp++; } } if (temp == k && ans > temp_cost) ans = temp_cost, ans_word = temp_word; else if (temp == k && ans == temp_cost && temp_word < ans_word) ans_word = temp_word; } cout << ans << endl; cout << ans_word << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const int maxn = 3e5 + 5; long long sum[maxn], a[maxn]; long long qsm(long long x, long long b) { long long base = x; long long ans = 1; while (b) { if (b & 1) { ans = ans * base % mod; } base = base * base % mod; b >>= 1; } return ans; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%lld", &a[i]); } sort(a, a + n); for (int i = 0; i < n; i++) { if (!i) { sum[0] = a[i]; } else { sum[i] = (sum[i - 1] + a[i]) % mod; } } long long ans = 0; for (int i = 0; i < n - 1; i++) { ans = (ans + ((sum[n - 1] - sum[i] - sum[n - 2 - i]) % mod) * qsm(2, i)) % mod; } ans = (ans + mod) % mod; printf("%lld\n", ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; int p[1000 + 5][1000 + 5]; char c[1000 + 5][1000 + 5]; vector<char> ans; vector<pair<int, int> > pos; vector<pair<char, int> > go; int get_sum(int l1, int r1, int l2, int r2) { if (l1 > l2) swap(l1, l2); if (r1 > r2) swap(r1, r2); return p[l2][r2] - p[l1 - 1][r2] - p[l2][r1 - 1] + p[l1 - 1][r1 - 1]; } int main() { int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> c[i][j]; if (c[i][j] >= 'A' && c[i][j] <= 'Z') { pos.push_back({i, j}); } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { p[i][j] = p[i - 1][j] + p[i][j - 1] - p[i - 1][j - 1]; if (c[i][j] == '#') p[i][j]++; } } int k; cin >> k; for (int i = 0; i < k; i++) { char t; int r; cin >> t >> r; go.push_back({t, r}); } for (auto o : pos) { int l = o.first; int r = o.second; bool can = true; for (auto i : go) { int l1 = l; int r1 = r; if (i.first == 'N') l1 -= i.second; if (i.first == 'S') l1 += i.second; if (i.first == 'W') r1 -= i.second; if (i.first == 'E') r1 += i.second; if (l1 <= 0 || l1 > n || r1 <= 0 || r1 > m) { can = false; break; } if (get_sum(l, r, l1, r1) > 0) { can = false; break; } l = l1; r = r1; } if (can) { ans.push_back(c[o.first][o.second]); } } sort(ans.begin(), ans.end()); if (ans.empty()) { cout << "no solution"; return 0; } for (auto i : ans) { cout << i; } return 0; }
4
#include <bits/stdc++.h> using namespace std; long long int MOD = 1000000007; long long int inf = 1e15; void scan(int &x); long long int powermod(long long int _a, long long int _b, long long int _m) { long long int _r = 1; while (_b) { if (_b % 2 == 1) _r = (_r * _a) % _m; _b /= 2; _a = (_a * _a) % _m; } return _r; } long long int string_to_number(string s) { long long int x = 0; stringstream convert(s); convert >> x; return x; } long long int add(long long int a, long long int b) { long long int x = (a + b) % MOD; return x; } long long int mul(long long int a, long long int b) { long long int x = (a * b) % MOD; return x; } long long int sub(long long int a, long long int b) { long long int x = (a - b + MOD) % MOD; return x; } long long int divi(long long int a, long long int b) { long long int x = a; long long int y = powermod(b, MOD - 2, MOD); long long int res = (x * y) % MOD; return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; long long int m; cin >> m; long double sum[m], mul[m]; long double sumdmin = 0.0; long double sumdmax = 0.0; if (n != 1) { if (n % 2 == 0) { long long int left = n / 2; long long int right = n / 2 - 1; sumdmin += (((left) * (left + 1)) / 2); sumdmin += (((right) * (right + 1)) / 2); } else { long long int left = n / 2; long long int right = n / 2; sumdmin += (((left) * (left + 1)) / 2); sumdmin += (((right) * (right + 1)) / 2); } sumdmax = ((n) * (n - 1)) / 2; } else { if (n == 1) { sumdmin = 0.0; sumdmax = 0.0; } } long double suma = 0.0; for (int i = 0; i < m; i++) { cin >> sum[i] >> mul[i]; if (mul[i] < 0) { suma += ((long double)n * sum[i] + mul[i] * sumdmin); } else { suma += ((long double)n * sum[i] + mul[i] * sumdmax); } } cout << fixed << setprecision(12); cout << suma / (long double)n; return 0; }
3
#include<bits/stdc++.h> using namespace std; int main() {long long i,j; cin>>i; cout<<1-i;}
0
#include <bits/stdc++.h> const int MAX = 1123; int p[MAX]; int main(void) { int n, m, x, y, first, i, ans; scanf("%d %d", &n, &m); while (n--) { scanf("%d %d", &x, &y); for (i = x; i <= y; i++) p[i] = 1; } for (ans = 0, i = 1; i <= m; i++) ans += !p[i]; printf("%d\n", ans); if (ans) { for (first = i = 1; i <= m; i++) if (!p[i]) { printf("%s%d", !first ? " " : "", i); first = 0; } printf("\n"); } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 505; const int b = 130000; int f[2*b+5], s[2*b+5], g[2*b+5], ans[N]; int n, mod; void solve(){ for (int i = b; i <= 2 * b + 4; ++i) s[i]=1; for (int i = 1; i <= n; ++i) { int tmp = i * (i - 1) / 2; for (int k = - tmp + b; k <= tmp + b; ++k) { f[k] = (0ll + f[k - 1] - (s[k - 1] - s[k - i - 1]) + s[k + i - 1] - s[k - 1] + 3ll * mod) % mod; } for (int k = tmp + b; k >= - tmp + b; --k) { g[k] = (g[k + 1] + f[k]) % mod; } for(int j = 1; j <= i; ++j) { ans[i + 1] = (ans[i + 1] + (1ll * (i + 1 - j) * g[j + 1 + b]) % mod) % mod; } memset(s, 0, sizeof(s)); tmp = (i + 1) * i / 2; for (int k = - tmp - i - 1 + b; k <= tmp + b+ i + 1; ++k) { s[k] = (s[k - 1] + f[k]) % mod; } } for (int i = 1; i <= n; ++i) ans[i] = (ans[i] + (1ll * i * ans[i-1]) % mod) % mod; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> mod; solve(); cout << ans[n]; }
5
#include<bits/stdc++.h> using namespace std; template<typename TP>inline void read(TP &tar) { TP ret=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();} tar=ret*f; } namespace RKK { const int N=200011; struct sumireko{int to,ne;}e[N];int he[N],ecnt; void addline(int f,int t){e[++ecnt].to=t;e[ecnt].ne=he[f],he[f]=ecnt;} int n,fa[N]; int rnd[N],ra,onr[N]; bool vis[N]; void findrnd() { int x=1; while(!vis[x]) vis[x]=1,x=fa[x]; while(!onr[x]) onr[x]=1,rnd[++ra]=x,x=fa[x]; } int sg[N],sgvis[N],ipa; void dfs(int x) { for(int i=he[x],t=e[i].to;i;i=e[i].ne,t=e[i].to)if(!onr[t]) dfs(t); ipa++; for(int i=he[x],t=e[i].to;i;i=e[i].ne,t=e[i].to)if(!onr[t]) sgvis[sg[t]]=ipa; for(int i=0;;i++)if(sgvis[i]!=ipa){sg[x]=i;break;} } int Iris() { read(n);for(int i=1;i<=n;i++) read(fa[i]),addline(fa[i],i); findrnd(); int ma=-1,mi=n; if(ra&1) { for(int i=1,x=rnd[i];i<=ra;i++,x=rnd[i]) dfs(x),ma=max(ma,sg[x]),mi=min(mi,sg[x]); puts(ma==mi?"IMPOSSIBLE":"POSSIBLE"); }else puts("POSSIBLE"); return 0; } } int main(){return RKK::Iris();}
0
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; long long powmod(long long a, long long b) { long long res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1) res = res * a % mod; a = a * a % mod; } return res; } long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } inline int getint() { int ret = 0; bool ok = 0, neg = 0; for (;;) { int c = getchar(); if (c >= '0' && c <= '9') ret = (ret << 3) + ret + ret + c - '0', ok = 1; else if (ok) return neg ? -ret : ret; else if (c == '-') neg = 1; } } int m, d, n; long long base[10][10], a[10]; map<long long, int> hs; int main() { m = getint(); d = getint(); for (int zz = 0; zz < m; zz++) { int c = getint(); for (int j = 0; j < d; j++) for (int k = 0; k < d; k++) base[j][k] = 0; for (int z = 0; z < c; z++) { for (int k = 0; k < d; k++) { a[k] = getint(); if (a[k] < 0) a[k] += mod; } for (int j = 0; j < d; j++) { if (!a[j]) continue; if (base[j][j]) { long long w = mod - a[j]; for (int k = j; k < d; k++) a[k] = (a[k] + w * base[j][k]) % mod; } else { long long w = powmod(a[j], mod - 2); for (int k = j; k < d; k++) base[j][k] = a[k] * w % mod; break; } } } for (int j = 0; j < d; j++) if (base[j][j]) { for (int i = 0; i < j; i++) if (base[i][j]) { long long w = mod - base[i][j]; for (int k = j; k < d; k++) { base[i][k] = (base[i][k] + w * base[j][k]) % mod; } } } long long p = 0; for (int j = 0; j < d; j++) { for (int k = 0; k < d; k++) p = p * 13331 + base[j][k]; } if (!hs.count(p)) hs[p] = ++n; printf("%d ", hs[p]); } }
4
#include <bits/stdc++.h> int cmp(const void* a, const void* b) { return (*(int*)a - *(int*)b); } int main() { int q; scanf("%d", &q); while (q--) { int n; int cnt = 1; int flag = 0; int a[105] = {0}; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } qsort(a, n, sizeof(int), cmp); for (int i = 1; i < n; i++) { if (a[i] == a[i - 1] + 1) { flag = 1; } } if (flag) printf("2\n"); else printf("1\n"); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int dp[110], tmp[110], mod = 1e6 + 3, dir[200]; int n, w, h; int main() { long long sum = 1, x; scanf("%d%d%d", &n, &w, &h); dir[0] = 1; for (int i = 1; i <= 150; i++) dir[i] = dir[i - 1] * h % mod; queue<long long> q; for (int i = 0; i < w; i++) q.push(0); q.push(1); for (int i = 0; i < n; i++) { q.push(sum); x = q.front() * dir[(i) > (w) ? (w) : (i)] % mod; sum = (sum - x) * h + sum; sum = (sum % mod + mod) % mod; q.pop(); } printf("%I64d\n", sum); }
4
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") long long int power(long long int x, long long int n) { long long int result = 1; while (n) { if (n % 2 == 1) result = result * x; n = n / 2; x = x * x; } return result; } long long int gcd(long long int a, long long int b) { if (!b) return a; return gcd(b, a % b); } long long int lcm(long long int a, long long int b) { return (a * b) / gcd(a, b); } long long int BS(long long int a[], long long int s, long long int n, long long int val) { long long int mid, beg = s, end = n - 1; while (beg <= end) { mid = (beg + end) / 2; if (val == a[mid]) { break; } else if (val > a[mid]) { beg = mid + 1; } else { end = mid - 1; } } return mid; } inline long long int mul(long long int x, long long int y, long long int m) { long long int z = 1LL * x * y; if (z >= m) { z %= m; } return z; } long long int powmod(long long int x, long long int y, long long int m) { long long int r = 1; while (y) { if (y & 1) { r = mul(r, x, m); } y >>= 1; x = mul(x, x, m); } return r; } using namespace std; void start() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; } int main() { start(); long long int t; cin >> (t); while (t--) { long long int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; long long int d = abs(x1 - x2); if (y1 == y2) { cout << d << "\n"; continue; } long long int d1 = abs(y1 - y2); if (d > 0) { cout << d + d1 + 2 << "\n"; continue; } cout << d1 << "\n"; } return 0; }
1
#include<stdio.h> #include<algorithm> using namespace std; int s(int x,int y){ if(x<=y) return y-x; else return x-y+2; } int main(){ int x,y; scanf("%d %d",&x,&y); printf("%d\n",min(s(x,y),min(s(-x,y)+1,min(s(x,-y)+1,s(-x,-y)+2)))); return 0; }
0
#include <bits/stdc++.h> using namespace std; template <class T> ostream &operator<<(ostream &s, vector<T> &P) { for (long long i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } using P = pair<long long, long long>; const long long inf = 1e18; template <class T> long long count_smaller_than(vector<T> &V, T x) { auto itr = lower_bound(V.begin(), V.end(), x); return itr - V.begin(); } template <class T> long long smaller_than(vector<T> &V, T x) { auto itr = lower_bound(V.begin(), V.end(), x); if (itr == V.begin()) { return -inf; } return V[itr - V.begin() - 1]; } template <class T> vector<T> push(vector<T> &V) { vector<pair<T, long long>> A; vector<T> res(V.size()); for (long long i = 0; i < V.size(); i++) { A.push_back(P{V[i], i}); } sort(A.begin(), A.end()); T saved_val; long long prev_idx = 1; for (long long i = 0; i < V.size(); i++) { auto [val, idx] = A[i]; if (i == 0) { res[idx] = 1; saved_val = val; prev_idx = 1; continue; } if (saved_val == val) { res[idx] = prev_idx; continue; } else { saved_val = val; prev_idx++; res[idx] = prev_idx; continue; } } return res; } long long N, M, K; bool possible(vector<long long> &A, long long idx) { long long Vol = 0; long long Need_Count = 0; for (long long i = idx; i < N; i++) { if (Vol >= A[i]) { Vol -= A[i]; } else { Need_Count++; Vol = K; Vol -= A[i]; } } if (M >= Need_Count) { return true; } return false; } void slv() { cin >> N >> M >> K; vector<long long> a(N); for (long long i = 0; i < N; i++) { scanf("%lld", &a[i]); } if (possible(a, 0)) { cout << N << endl; return; } long long l = 0; long long r = N - 1; while (r - l > 1) { long long med = (r + l) / 2; if (possible(a, med)) { r = med; } else { l = med; } } cout << N - r << endl; return; } signed main() { long long t; t = 1; while (t--) { slv(); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { string t; cin >> t; int n = t.length(); string s = "", s1 = "", s2 = ""; int k = 0; for (int i = 0; i < n; i++) { if (t[i] != 'a') { s2 = s2 + t[i]; } } if (s2.length() % 2 == 0) { if (s2.substr(0, s2.length() / 2) == s2.substr(s2.length() / 2, s2.length() / 2)) { int j = s2.length() / 2; s1 = s2.substr(0, s2.length() / 2); int i = n - 1; int f = 1; while (j > 0) { if (t[i] != 'a') { j--; i--; } else { cout << ":(" << endl; f = 0; break; } } if (f == 1) { cout << t.substr(0, n - s2.length() / 2) << endl; } } else { cout << ":(" << endl; } } else { cout << ":(" << endl; } }
2
#define _USE_MATH_DEFINES #include <bits/stdc++.h> using namespace std; #define int long long #define rep(i,n) for(int i=0;i<n;i++) #define INF 1000000000000000000 #define MOD 1000000007 typedef pair<int, int> P; bool prime(int n) { for (int i = 2; i <= sqrt(n); i++) { if (n % i == 0)return false; } return n != 1; } int gcd(int x, int y) { if (y == 0)return x; return gcd(y, x % y); } int lcm(int x, int y) { return x / gcd(x, y) * y; } int mod_pow(int n, int p, int m) { if (p == 0) return 1; if (p % 2 == 0) { int t = mod_pow(n, p / 2, m); return (t * t) % m; } return n * mod_pow(n, p - 1, m) % m; } int extGCD(int a, int b, int& x, int& y) { if (b == 0) { x = 1; y = 0; return a; } long long d = extGCD(b, a % b, y, x); y -= a / b * x; return d; } int modinv(int a, int m) { int b = m, u = 1, v = 0; while (b) { long long t = a / b; a -= t * b; swap(a, b); u -= t * v; swap(u, v); } u %= m; if (u < 0) u += m; return u; } int digit(int x) { int cnt = 0; while (x > 0) { cnt += x % 10; x /= 10; } return cnt; } class BIT { private: vector<int>bit; int n; public: BIT(int _n) { rep(i, _n + 1) bit.push_back(0); n = _n; } int get() { return bit.size(); } int sum(int i) { int s = 0; while (i > 0) { s += bit[i]; i -= (i & -i); } return s; } int sum(int i, int j) { int be = sum(i - 1); int af = sum(j); return af - be; } void add(int i, int x) { while (i <= n) { bit[i] += x; i += (i & -i); } } }; signed main() { int n; int k; cin >> n >> k; int a[100005], sum[100005], x[100005]; sum[0] = 0; rep(i, n) { cin >> a[i]; a[i] -= k; sum[i + 1] = sum[i] + a[i]; } rep(i, n + 1) { x[i] = sum[i]; } sort(x, x + n + 1); map<int, int>mp; int now = 1; mp[x[0]] = 1; for (int i = 1; i < n + 1; i++) { if (x[i] > x[i - 1]) { now++; } mp[x[i]] = now; } rep(i, n + 1) { sum[i] = mp[sum[i]]; } int ans = 0; BIT bit(n); for (int i = n - 1; i >= 0; i--) { bit.add(sum[i + 1], 1); ans += (n - i) - bit.sum(sum[i] - 1); } cout << ans << endl; return 0; }
0
#include <iostream> #include <vector> #include <array> #include <list> #include <string> #include <stack> #include <queue> #include <deque> #include <map> #include <set> #include <unordered_map> #include <unordered_set> #include <tuple> #include <memory> #include <cmath> #include <algorithm> #include <functional> #include <iomanip> #include <numeric> #include <climits> #include <cfloat> #include <cassert> #include <random> #include <typeinfo> constexpr int MOD_SIZE = 10; constexpr std::array<long long int, MOD_SIZE> PRIMES{ 1073741827, 1073741831, 1073741833, 1073741839, 1073741843, 1073741857, 1073741891, 1073741909, 1073741939, 1073741953 }; long long int power(long long int base, long long int exp, long long int mod) { switch (exp) { case 0: return 1; case 1: return base % mod; default: return power(base * base % mod, exp / 2, mod) * power(base, exp % 2, mod) % mod; } } class Integer { std::array<long long int, MOD_SIZE> numbers; public: Integer(): numbers{0, 0, 0, 0, 0, 0, 0, 0, 0, 0} {} Integer(int value) { for (auto i = 0; i < PRIMES.size(); ++i) { numbers[i] = value % PRIMES[i]; } } Integer operator*(const int value) const { Integer result; for (auto i = 0; i < PRIMES.size(); ++i) { result.numbers[i] = (numbers[i] * value) % PRIMES[i]; } return result; } Integer& operator*=(const Integer& that) { for (auto i = 0; i < PRIMES.size(); ++i) { numbers[i] = (numbers[i] * that.numbers[i]) % PRIMES[i]; } return *this; } Integer& operator/=(const Integer& that) { for (auto i = 0; i < PRIMES.size(); ++i) { numbers[i] = (numbers[i] * power(that.numbers[i], PRIMES[i] - 2, PRIMES[i])) % PRIMES[i]; } return *this; } bool operator==(const Integer& that) const { for (auto i = 0; i < PRIMES.size(); ++i) { if (numbers[i] != that.numbers[i]) return false; } return true; } bool operator!=(const Integer& that) const { return !(*this == that); } }; class Relation { std::vector<Integer> relation; std::vector<int> parent; std::pair<int, Integer> find(int a) { if (parent[a] < 0) { return std::make_pair(a, relation[a]); } else { auto pair = find(parent[a]); relation[a] *= pair.second; parent[a] = pair.first; return std::make_pair(parent[a], relation[a]); } } public: Relation(int size) : parent(size, -1), relation(size, 1) {}; bool set_relation_unless_paradoxical(int a, int b, int value) { auto ar = find(a); auto br = find(b); if (ar.first != br.first) { if (parent[ar.first] < parent[br.first]) { parent[ar.first] += parent[br.first]; parent[br.first] = ar.first; relation[br.first] *= ar.second * value; relation[br.first] /= br.second; } else { parent[br.first] += parent[ar.first]; parent[ar.first] = br.first; relation[ar.first] *= br.second; relation[ar.first] /= ar.second * value; } return true; } else { return ar.second * value == br.second; } } }; int main() { int n, m; std::cin >> n >> m; std::vector<std::vector<std::pair<int, int>>> children(n); Relation uft(n); for (auto i = 0; i < m; ++i) { int a, b, x; std::cin >> a >> b >> x; --a; --b; if (!uft.set_relation_unless_paradoxical(a, b, x)) { std::cout << "No\n"; return 0; } } std::cout << "Yes\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, x, y; string f[100005], g[100005], t[100005]; map<string, int> m1, m2; int u[100005]; vector<int> v1, v2, r1, r2; vector<string> p1, p2; void cty(int k) { if (k <= x) { if (v1.size()) { int o = v1[v1.size() - 1]; int nex = m2[f[o]]; v1.pop_back(); p1.push_back(f[o]), p2.push_back(t[k]); u[nex] = 0; u[k] = o; f[o] = t[k]; cty(nex); return; } int o = r1[r1.size() - 1]; r1.pop_back(); p1.push_back(f[o]), p2.push_back(t[k]); u[k] = o; f[o] = t[k]; return; } if (v2.size()) { int o = v2[v2.size() - 1]; int nex = m1[g[o - x]]; v2.pop_back(); p1.push_back(g[o - x]), p2.push_back(t[k]); u[nex] = 0; u[k] = o; g[o - x] = t[k]; cty(nex); return; } int o = r2[r2.size() - 1]; r2.pop_back(); p1.push_back(g[o - x]), p2.push_back(t[k]); u[k] = o; g[o - x] = t[k]; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { char c; string s = ""; scanf(" %c", &c); while (c != ' ') { s += c; c = getchar(); } int e; scanf("%d", &e); if (e) f[++x] = s; else g[++y] = s; } for (int i = 1; i <= n; i++) { string s = ""; int o = i; while (o) { char c = '0' + o % 10; o /= 10; s = c + s; } t[i] = s; if (i <= x) m1[s] = i; else m2[s] = i; } for (int i = 1; i <= x; i++) { if (m1.count(f[i])) u[m1[f[i]]] = i; else if (m2.count(f[i])) u[m2[f[i]]] = i, v1.push_back(i); else r1.push_back(i); } for (int i = 1; i <= y; i++) { if (m2.count(g[i])) u[m2[g[i]]] = i + x; else if (m1.count(g[i])) u[m1[g[i]]] = i + x, v2.push_back(i + x); else r2.push_back(i + x); } for (int i = 1; i <= n; i++) if (!u[i]) cty(i); for (int i = 1; i <= x; i++) { if (u[i] > x) { p1.push_back(g[u[i] - x]), p2.push_back("escape"); g[u[i] - x] = "escape"; r2.push_back(u[i]); vector<int> V; for (int j = 0; j < v2.size(); j++) if (v2[j] != u[i]) V.push_back(v2[j]); v2 = V; u[i] = 0; cty(i); } } for (int i = x + 1; i <= n; i++) { if (u[i] <= x) { p1.push_back(f[u[i]]), p2.push_back("escape"); f[u[i]] = "escape"; r1.push_back(u[i]); vector<int> V; for (int j = 0; j < v1.size(); j++) if (v1[j] != u[i]) V.push_back(v1[j]); v1 = V; u[i] = 0; cty(i); } } printf("%d\n", p1.size()); for (int i = 0; i < p1.size(); i++) printf("move %s %s\n", p1[i].c_str(), p2[i].c_str()); return 0; }
3