solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; template <class T> void printArr(T* arr, int size) { for (int i = 0; i < size; i++) cout << arr[i] << " "; cout << endl; } long long fast_exp(long long x, long long y) { if (y == 1) return x; if (y == 0) return 1; if (y & 1) return x * fast_exp(x, y - 1); else { long long re = fast_exp(x, y / 2); return re * re; } } pair<double, double> rotate90(pair<double, double> h, pair<double, double> p) { p.first -= h.first; p.second -= h.second; return make_pair(-p.second + h.first, p.first + h.second); } bool isSquare(vector<pair<double, double> > sq) { vector<long long> dist; for (int i = 0; i < sq.size(); i++) { for (int j = i + 1; j < sq.size(); j++) { dist.push_back( abs(sq[i].first - sq[j].first) * abs(sq[i].first - sq[j].first) + abs(sq[i].second - sq[j].second) * abs(sq[i].second - sq[j].second)); } } sort(dist.begin(), dist.end()); for (int i = 1; i < 4; i++) { if (dist[i] != dist[i - 1] || dist[i] == 0) return false; } if (dist[5] != dist[4] || dist[4] == 0) return false; return true; } int main() { ios::sync_with_stdio(0); int n; cin >> n; for (int tc = 0; tc < n; tc++) { pair<double, double> p1, p2, p3, p4; pair<double, double> h1, h2, h3, h4; cin >> p1.first >> p1.second >> h1.first >> h1.second; cin >> p2.first >> p2.second >> h2.first >> h2.second; cin >> p3.first >> p3.second >> h3.first >> h3.second; cin >> p4.first >> p4.second >> h4.first >> h4.second; int ans = -1; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { for (int k = 0; k < 4; k++) { for (int l = 0; l < 4; l++) { vector<pair<double, double> > sq; sq.push_back(p1); sq.push_back(p2); sq.push_back(p3); sq.push_back(p4); if (isSquare(sq)) { if (ans == -1) ans = i + j + k + l; else ans = min(ans, i + j + k + l); } p4 = rotate90(h4, p4); } p3 = rotate90(h3, p3); } p2 = rotate90(h2, p2); } p1 = rotate90(h1, p1); } cout << ans << endl; } return 0; }
3
#include <iostream> #include <algorithm> #include <cassert> #include <cctype> #include <complex> #include <cstdio> #include <map> #include <math.h> #include <queue> #include <set> #include <stack> #include <string> #include <vector> using namespace std; int m,n; string s; bool b[1001]; int main(){ while(cin>>m>>n){ if(!m)return 0; int c=1,p=m; fill(b,b+m+1,0); for(int i=1;i<=n;i++){ //cout<<"player "<<c<<":"<<endl; cin>>s; if(!(i%15)){ if(s!="FizzBuzz"){b[c]=1;p--;} //else cout<<"s"<<endl; } else if(!(i%3)){ if(s!="Fizz"){b[c]=1;p--;} //else cout<<"s"<<endl; } else if(!(i%5)){ if(s!="Buzz"){b[c]=1;p--;} //else cout<<"s"<<endl; } else{ int f=0; if(s.size()<6){ int cur=0,N=0; while(cur<s.size()){ if(!isdigit(s[cur])){f=2;break;} N=N*10+s[cur++]-'0'; } if(!f&&N==i)f=1; } if(f!=1){b[c]=1;p--;} //else cout<<"s"<<endl; } if(p==1){for(int j=i+1;j<=n;j++)cin>>s;break;} c++; if(c==m+1)c=1; while(b[c]){c++;if(c==m+1)c=1;} } int f=0; for(int i=1;i<=m;i++){ if(!b[i]){ if(f)cout<<" "; cout<<i; f=1; } } cout<<endl; } }
0
#include <bits/stdc++.h> using namespace std; int main(void){ int n,m,L,R,MIN=1e9,MAX=0; cin>>n>>m; for(int i=0;i<m;i++){ cin>>L>>R; MAX=max(MAX,L); MIN=min(MIN,R); } cout<<max(0,MIN-MAX+1)<<endl; }
0
#include <bits/stdc++.h> using namespace std; int n, ans; int a[2020], nxt[2020], pre[2020], d[2020]; int getnum() { char c = getchar(); int num = 0; while (c > '9' || c < '0') c = getchar(); while (c >= '0' && c <= '9') num = (num << 3) + (num << 1) + c - '0', c = getchar(); return num; } int main() { int i, j, tm; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &a[i]); for (i = 1; i <= n; i++) { pre[i] = 1; for (j = 1; j < i; j++) if (a[j] <= a[i]) pre[i] = max(pre[i], pre[j] + 1); } for (i = n; i >= 1; i--) { nxt[i] = 1; for (j = n; j > i; j--) if (a[j] >= a[i]) nxt[i] = max(nxt[i], nxt[j] + 1); } for (i = 1; i <= n; i++) ans = max(ans, pre[i - 1] + nxt[i]); printf("%d\n", ans); }
3
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; struct Node { string str; int pos; friend bool operator<(Node const& n1, Node const& n2) { return n2.str < n1.str; } }; priority_queue<Node> que; int main() { int k; string str; cin >> str; cin >> k; que.push({string{'z' + 1}, INF}); for (int i = 0; i < str.size(); ++i) { que.push({string{str[i]}, i}); } Node i; while (k-- && !que.empty()) { i = que.top(); que.pop(); if (i.pos == str.size() - 1 || i.pos == INF) continue; que.push({i.str + str[i.pos + 1], i.pos + 1}); } if (k && que.empty()) cout << "No such line." << endl; else cout << i.str << endl; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int A; cin >> A; if (A == 1) { cout << 1 << " " << 1 << '\n' << 1 << '\n'; return 0; } cout << (A - 1) * 2 << " " << 2 << '\n' << 1 << " " << 2 << '\n'; return 0; }
1
#include <bits/stdc++.h> using namespace std; int i, n, k, p; int main() { string a; cin >> a >> n; for (i = a.size() - 1; i > 0; i--) { if (a[i] != '0') { p++; } else k++; if (k == n) { cout << p; return 0; } } cout << a.size() - 1; }
2
#include<iostream> using namespace std; int main(){ long long int n,m,sum=0; cin>>n>>m; if(n*2<=m){ sum+=n; m-=n*2; sum+=m/4; }else{ sum=m/2; } cout<<sum<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; inline int L(int i) { return i << 1; } inline int R(int i) { return (i << 1) | 1; } inline int B(int i) { return 1 << i; } inline int low_bit(int x) { return x & (-x); } long long cnt[1010], top; long long tmp, mul; bool must_win(long long a, long long b) { if (a == 0 || b == 0) return 0; if (a > b) tmp = a, a = b, b = tmp; if (must_win(b % a, a) == 0) return 1; tmp = b; tmp /= a; tmp %= a + 1; if (tmp & 1) return 0; return 1; } int main() { int t; long long a, b; cin >> t; while (t--) { cin >> a >> b; if (must_win(a, b)) puts("First"); else puts("Second"); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int P = 1e9 + 7; struct Matrix { int n, m; int a[130][130]; Matrix operator*(const Matrix &b) const { Matrix C; memset(C.a, 0, sizeof(C.a)); C.n = n; C.m = b.m; for (int i = 0; i < C.n; ++i) for (int j = 0; j < C.m; ++j) for (int k = 0; k < m; ++k) C.a[i][j] = (C.a[i][j] + (long long)a[i][k] * b.a[k][j]) % P; return C; } } aa, I, a, z, g0, g1; int f[2][130]; void find(int n) { a = I, z = aa; for (; n; n >>= 1, z = z * z) if (n & 1) a = a * z; } void updata(int &x, int y) { x += y; if (x >= P) x -= P; } int main() { int cur = 0, nex = 1; f[cur][0] = 1; for (int i = 1; i <= 7; ++i) { memset(aa.a, 0, sizeof(aa.a)); aa.n = aa.m = 1 << i; I.n = I.m = 1 << i; for (int j = 0; j < I.n; ++j) I.a[j][j] = 1; for (int mask0 = 0; mask0 < 1 << i; ++mask0) for (int mask1 = 0; mask1 < 1 << i; ++mask1) { int res0 = 0, res1 = 1; for (int j = 0; j < i; ++j) { int ret0 = res0, ret1 = res1; res0 = res1 = 0; updata(res0, ret0); updata(res1, ret0); updata(res0, ret1); if (!((mask0 >> j & 1) && (mask1 >> j & 1))) updata(res1, ret1); } aa.a[mask0][mask1] = res1; } int x; scanf("%d", &x); find(x); memset(g0.a, 0, sizeof(g0.a)); memset(g1.a, 0, sizeof(g1.a)); g0.n = g1.n = 1 << i; g0.m = g1.m = 1; for (int mask = 0; mask < 1 << i - 1; ++mask) g0.a[mask | (1 << i - 1)][0] = f[cur][mask]; g1 = a * g0; for (int mask = 0; mask < 1 << i; ++mask) f[nex][mask] = g1.a[mask][0]; swap(cur, nex); } int ans = f[cur][(1 << 7) - 1]; printf("%d\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { vector<int> first; vector<int> second; int n, m; cin >> n; long long scoreA, maxA; maxA = scoreA = 3 * n; for (int i = 0; i < n; i++) { int x; cin >> x; first.push_back(x); } first.push_back(INT_MAX); cin >> m; long long scoreB, maxB; maxB = scoreB = 3 * m; for (int i = 0; i < m; i++) { int x; cin >> x; second.push_back(x); } second.push_back(INT_MAX); sort(first.begin(), first.end()); sort(second.begin(), second.end()); long long maxDiff = scoreA - scoreB; int i = 0, j = 0; while (i <= n && j <= m) { if (first[i] < second[j]) { scoreA--; i++; } else if (first[i] > second[j]) { scoreB--; j++; } else { scoreA--; scoreB--; i++; j++; } if (scoreA - scoreB > maxDiff) { maxDiff = scoreA - scoreB; maxA = scoreA; maxB = scoreB; } } cout << maxA << ':' << maxB << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; long long read() { long long 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 << 3) + (x << 1) + (ch ^ 48); ch = getchar(); } return x * f; } long long k, n, m, L1[2000005], L2[2000005], f[2000005][3], g[2000005][3], tot, cnt, all[2000005]; pair<long long, long long> a[2000005], b[2000005]; long long Id(long long x) { return lower_bound(all + 1, all + 1 + cnt, x) - all; } long long ksm(long long A, long long B) { long long res = 1; while (B) { if (B & 1) res *= A, res %= 1000000007; A *= A; A %= 1000000007; B >>= 1; } return res; } signed main() { k = read(); n = read(); m = read(); for (long long i = 1; i <= n; i++) { long long x = read() - 1, y = read(); a[i] = make_pair(x, y); all[++tot] = x; all[++tot] = y; } for (long long i = 1; i <= m; i++) { long long x = read() - 1, y = read(); b[i] = make_pair(x, y); all[++tot] = x; all[++tot] = y; } all[++tot] = 0; all[++tot] = k; sort(all + 1, all + 1 + tot); cnt = unique(all + 1, all + 1 + tot) - all - 1; L1[0] = L2[0] = 1; for (long long i = 1; i <= n; i++) { L1[Id(a[i].second)] = max(L1[Id(a[i].second)], Id(a[i].first) + 1); } for (long long i = 1; i <= m; i++) L2[Id(b[i].second)] = max(L2[Id(b[i].second)], Id(b[i].first) + 1); for (long long i = 1; i <= cnt; i++) L1[i] = max(L1[i], L1[i - 1]), L2[i] = max(L2[i], L2[i - 1]); f[1][2] = g[1][2] = 1; for (long long i = 2; i <= cnt; i++) { f[i][2] = (f[i - 1][0] + f[i - 1][1] + f[i - 1][2]) * (ksm(2, all[i] - all[i - 1]) - 2) % 1000000007; f[i][1] = (g[i - 1][0] - g[L1[i] - 1][0] + 1000000007) % 1000000007 + (g[i - 1][2] - g[L1[i] - 1][2] + 1000000007) % 1000000007; f[i][0] = (g[i - 1][1] - g[L2[i] - 1][1] + 1000000007) % 1000000007 + (g[i - 1][2] - g[L2[i] - 1][2] + 1000000007) % 1000000007; g[i][2] = g[i - 1][2] + f[i][2]; g[i][2] %= 1000000007; g[i][1] = g[i - 1][1] + f[i][1]; g[i][1] %= 1000000007; g[i][0] = g[i - 1][0] + f[i][0]; g[i][0] %= 1000000007; } printf("%lld\n", (f[cnt][0] + f[cnt][1] + f[cnt][2]) % 1000000007); return 0; }
5
#include <bits/stdc++.h> using namespace std; struct PathologicalPaths { struct node { set< string > pages; map< string, node* > children; }; vector< string > split(const string& input, char delimiter) { istringstream stream(input); string field; vector< string > result; while(getline(stream, field, delimiter)) result.push_back(field); return(result); } PathologicalPaths(vector< string >& base, vector< pair< string, string > >& query) { node* root = new node(); root -> children.insert({".", root}); for(auto s : base) { vector< string > spd = split(s, '/'); node* now = root; for(int i = 1; i < spd.size() - 1; i++) { auto p = now -> children.insert({spd[i], new node()}); if(p.second) p.first -> second -> children.insert({"..", now}); now = p.first -> second; now -> children.insert({".", now}); } now -> pages.insert(spd.back()); } for(auto ps : query) { vector< string > spd[2] = {split(ps.first, '/'), split(ps.second, '/')}; if(ps.first.back() == '/') spd[0].push_back(string()); if(ps.second.back() == '/') spd[1].push_back(string()); node *now[2] = {root, root}; bool found = true; for(int j = 0; j < 2; j++) { for(int i = 1; i < spd[j].size(); i++) { if(now[j] -> children.find(spd[j][i]) == now[j] -> children.end()) { if(i < spd[j].size() - 1) found = false; break; } else { now[j] = now[j] -> children[spd[j][i]]; if(i == spd[j].size() - 1) { spd[j].push_back("index.html"); break; } } } } if(spd[0].back().empty()) spd[0].back() = "index.html"; if(spd[1].back().empty()) spd[1].back() = "index.html"; auto p = now[0] -> pages.find(spd[0].back()); auto q = now[1] -> pages.find(spd[1].back()); if(!found || p == now[0] -> pages.end() || q == now[1] -> pages.end()) { cout << "not found" << endl; } else { if(p == q){ cout << "yes" << endl; } else { cout << "no" << endl; } } } } }; int main() { int N, M; while(cin >> N >> M, N) { vector< string > A(N); vector< pair< string, string > > B(M); for(int i = 0; i < N; i++) cin >> A[i]; for(int i = 0; i < M; i++) cin >> B[i].first >> B[i].second; new PathologicalPaths(A, B); } }
0
#include <bits/stdc++.h> int main() { char cad[120]; scanf("%s", &cad); int lo = strlen(cad); bool uno = false; bool dos = false; bool tres = false; bool cuatro = false; for (int i = 0; i < lo; i++) { if (cad[i] > 64 && cad[i] < 91) { dos = true; } if (cad[i] > 96 && cad[i] < 123) { tres = true; } if (cad[i] > 47 && cad[i] < 58) { cuatro = true; } } if (lo >= 5) { uno = true; } if (uno && dos && tres && cuatro) { printf("Correct"); } else { printf("Too weak"); } return 0; }
1
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main() { int n; cin >> n; ll res = 0; for(int i = 1; i <= n; i++) res += 1ll * i * (n - i + 1); for(int i = 1; i < n; i++) { int a, b; cin >> a >> b; if(a > b) swap(a, b); res -= 1ll * a * (n - b + 1); } cout << res << endl; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 1000008; const long long oo = 1LL << 50; bool blocked[MAXN]; long long prices[MAXN]; int lastfree[2 * MAXN]; vector<pair<long long, long long> > estimates; int main() { int n, m, k; scanf("%d %d %d", &n, &m, &k); for (int i = 0; i < n; i++) { blocked[i] = false; } int x; for (int i = 0; i < m; i++) { scanf("%d", &x); blocked[x] = true; } int mxbl = 0, cbl = 0; int lstf = 0; for (int i = 0; i < n; i++) { if (blocked[i]) { cbl++; mxbl = max(mxbl, cbl); } else { cbl = 0; lstf = i; } lastfree[i] = lstf; } for (int i = n; i < 2 * n; i++) { lastfree[i] = i; } for (int i = 1; i <= k; i++) { scanf("%lld", &prices[i]); } if (blocked[0] || mxbl >= k) { printf("-1\n"); return 0; } long long minpr = oo; for (int i = k; i > mxbl; i--) { if (prices[i] > minpr) continue; minpr = prices[i]; long long epr = (n + i - 1) / i * prices[i]; estimates.push_back(make_pair(epr, i)); } sort(estimates.begin(), estimates.end()); long long minres = oo; for (unsigned i = 0; i < estimates.size(); i++) { if (estimates[i].first >= minres) break; int clamp = estimates[i].second; int cpos = 0; long long cpr = 0; while (cpos < n) { cpr += prices[clamp]; cpos = lastfree[cpos + clamp]; } minres = min(minres, cpr); } printf("%lld\n", minres); return 0; }
5
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:167772160") using namespace std; using ll = long long; using ull = unsigned long long; using uint = unsigned int; using pii = pair<int, int>; using pli = pair<ll, int>; using pll = pair<ll, ll>; using dbl = double; const int INF = (int)1e9 + 10; const ll LINF = ll(2e18) + 10; const double PI = acosl(-1); const double eps = 1e-8; const int N = 31; int type[N]; void solve() { int m, n; cin >> m >> n; for (int i = 0; i < n; ++i) { cout << 1 << endl; int ans; cin >> ans; if (ans == 0) return; if (ans == 1) type[i] = 1; } int l = 1; int r = m + 1; int id = 0; while (r - l > 1) { auto mid = (l + r) / 2; cout << mid << endl; int ans; cin >> ans; if (ans == 0) return; if (type[id % n] == 0) ans = -ans; ++id; if (ans == -1) r = mid; else l = mid; } throw; } int main() { solve(); (void)0; }
2
#include <bits/stdc++.h> using namespace std; const int s = 1e6; int n, X[s + 1], Y[s + 1]; int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 0; i < n; ++i) { int x; cin >> x; X[x] = 1; } vector<pair<int, int>> p; int q = 0; for (int i = 1; i < s + 1 - i; ++i) { int j = s + 1 - i; int t = X[i] + X[j]; if (t == 0) p.emplace_back(i, j); else { if (t == 2) ++q; else { if (X[i]) Y[j] = 1; else Y[i] = 1; } } } for (int i = 0; i < q; ++i) Y[p[i].first] = Y[p[i].second] = 1; vector<int> ans; for (int i = 1; i <= s; ++i) { if (Y[i]) ans.push_back(i); } cout << ans.size() << '\n'; for (const auto &y : ans) cout << y << " "; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long int power(long long int x, long long int n, long long int p) { long long int result = 1; while (n > 0) { if (n % 2 == 1) result = (result % p * x % p) % p; x = (x % p * x % p) % p; n = n / 2; } return result % p; } bool compare(pair<long long int, long long int> a1, pair<long long int, long long int> a2) { return (a1.first > a2.first); } void solve() { long long int n; cin >> n; vector<pair<long long int, long long int>> v1(n); for (long long int i = 0; i <= n - 1; i++) { cin >> v1[i].first; v1[i].second = i + 1; } sort(v1.begin(), v1.end(), compare); long long int ans = 0; long long int x = 0; for (long long int i = 0; i <= n - 1; i++) { ans += (x * v1[i].first) + 1; x++; } cout << ans << endl; for (long long int i = 0; i <= n - 1; i++) { cout << v1[i].second << " "; } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t = 1; while (t--) { solve(); } }
2
#include "bits/stdc++.h" #define in std::cin #define out std::cout #define rep(i,N) for(int i=0;i<N;++i) typedef long long int LL; LL gcd(LL a, LL b) { if (a < b) std::swap(a, b); LL r = a % b; while (r != 0) { a = b; b = r; r = a % b; } return b; } LL lcm(LL a, LL b) { return a / gcd(a, b) * b; } LL mypow(LL x, LL y) { LL res = 1; rep(i, y) res *= x; return res; } int main() { std::string str; in >> str; LL p1 = -1, p2 = -1, p3 = -1; rep(i, str.size()) { if (str[i] == '(') p1 = i; if (str[i] == ')') p2 = i; if (str[i] == '.') p3 = i; } LL k1 = 0, k2 = 0, m1 = 1, m2 = 1; if (p1 != -1) { LL w1 = p1 - 1; std::string temp; rep(i, p1) temp += str[i]; if (temp.size() > 1) { k1 = std::stold(temp)*mypow(10, temp.size()); m1 = mypow(10, temp.size()); if (k1 > 0) { LL num1 = gcd(k1, m1); k1 /= num1; m1 /= num1; } } LL w2 = p2 - p1 - 1; k2 = std::stoi(str.substr(p1 + 1, w2)); m2 = mypow(10, w2) - 1; if (p2 - 2 > p3 + w2) m2 *= mypow(10, ((p2 - 2) - (p3 + w2))); LL num2 = gcd(k2, m2); k2 /= num2; m2 /= num2; } else { std::string temp; rep(i, str.size()) temp += str[i]; k1 = std::stold(temp)*mypow(10, temp.size()); m1 = mypow(10, temp.size()); if (k1 > 0) { LL num1 = gcd(k1, m1); k1 /= num1; m1 /= num1; } } LL m = m1 * m2, k = k1 * m2 + k2 * m1; LL num = gcd(k, m); m /= num; k /= num; out << k << "/" << m << std::endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; long long sum[300010]; long long f[101][300010]; int main() { int a, b, k, t; cin >> a >> b >> k >> t; long long ans = 0; f[0][100010] = 1; sum[0] = 0; for (int j = 1; j <= 299999; j++) sum[j] = sum[j - 1] + f[0][j]; for (int i = 1; i <= t; i++) { for (int s = -100010; s < 100010 + 10; s++) { int tMAX = min(s + k + 100010, 299999); int tMIN = max(s - k + 100010 - 1, 0); f[i][s + 100010] = ((sum[tMAX] - sum[tMIN]) % 1000000007 + 1000000007) % 1000000007; } sum[0] = 0; for (int j = 1; j <= 299999; j++) sum[j] = (sum[j - 1] + f[i][j]) % 1000000007; } int d = b - a + 1; for (int i = -100010; i <= 100010; i++) { int MIN = max(0, 100010 + i + d - 1); ans = (ans + f[t][i + 100010] * ((sum[299999] - sum[MIN]) % 1000000007 + 1000000007) % 1000000007) % 1000000007; } cout << (ans % 1000000007 + 1000000007) % 1000000007 << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; vector<long long> v[N]; long long vis[N]; long long nodes = 0; const long long m = 1e9 + 7; long long pow(long long a, long long b, long long m) { long long ans = 1; while (b) { if (b & 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans; } long long dfs(long long s) { vis[s] = true; nodes++; for (long long i : v[s]) { if (!vis[i]) dfs(i); } return nodes; } int32_t main() { long long n, k; cin >> n >> k; long long a, b, c; for (long long i = 1; i < n; i++) { cin >> a >> b >> c; if (c == 0) { v[a].push_back(b); v[b].push_back(a); } } long long p; long long fans = pow(n, k, m); for (long long i = 1; i <= n; i++) { if (!vis[i]) { nodes = 0; p = 0; p = dfs(i); fans -= pow(p, k, m); fans += m; fans %= m; } } cout << fans; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n, k, i, j, a[1001], x, ans = 10010; cin >> n >> k; for (i = 1; i <= n; i++) cin >> a[i]; for (i = 1; i < 1001; i++) { int s = 0, t = i; for (j = 1; j <= n; j++) { if (a[j] != t) s++; t = t + k; } if (ans > s) { x = i; ans = s; } } cout << ans << endl; int t = x; for (i = 1; i <= n; i++) { if (a[i] < t) cout << "+ " << i << " " << t - a[i] << endl; else if (a[i] > t) cout << "- " << i << " " << a[i] - t << endl; t = t + k; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 7; char s[2005][2005]; int a[2005][2005]; int n, k; void insert(int x1, int y1, int x2, int y2) { x1 = min(x1, n + 1); x2 = min(x2, n + 1); y1 = min(y1, n + 1); y2 = min(y2, n + 1); a[x1][y1]++; a[x2][y1]--; a[x1][y2]--; a[x2][y2]++; } int main() { scanf("%d %d", &n, &k); for (int i = 1; i <= n; i++) scanf("%s", s[i] + 1); int ma, mi; int ans = 0; for (int i = 1; i <= n; i++) { ma = 0, mi = n + 1; for (int j = 1; j <= n; j++) { if (s[i][j] == 'B') { ma = max(j, ma); mi = min(j, mi); } } if (!ma) { ans++; continue; } if (ma - mi + 1 > k) continue; insert(i, ma, i + k, mi + k); } for (int i = 1; i <= n; i++) { ma = 0, mi = n + 1; for (int j = 1; j <= n; j++) { if (s[j][i] == 'B') { ma = max(j, ma); mi = min(j, mi); } } if (!ma) { ans++; continue; } if (ma - mi + 1 > k) continue; insert(ma, i, mi + k, i + k); } int tot = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { a[i][j] += a[i - 1][j] + a[i][j - 1] - a[i - 1][j - 1]; tot = max(tot, a[i][j]); } } printf("%d\n", tot + ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; int c = 1; int cur = 1; for (int i = 1; i < n; i++) { cur += i; if (cur > n) cur %= n; cout << cur << ' '; } return 0; }
1
#include <bits/stdc++.h> using namespace std; vector<int> V; int main() { int n; cin >> n; V.resize(n); for (int i = 0; i < n; i++) { cin >> V[i]; } for (int i = 0; i < n / 2; i++) { V[i] -= i; V[n - i - 1] -= i; } if (n % 2 == 1) V[n / 2] -= n / 2; sort(V.begin(), V.end(), greater<int>()); int MAX = 1, buf = V[0], k = 1; for (int i = 1; i < n; i++) { if (V[i] < 1) break; if (buf == V[i]) { k++; } else { MAX = max(MAX, k); buf = V[i]; k = 1; } } MAX = max(MAX, k); cout << n - MAX; return 0; }
3
#include <bits/stdc++.h> using namespace std; template <typename T> inline T abs(T t) { return t < 0 ? -t : t; } const long long modn = 1000000007; inline long long mod(long long x) { return x % modn; } int main() { long long d1, d2, d3; cin >> d1 >> d2 >> d3; long long ans = min(d1 + d2 + d3, d1 * 2 + d2 * 2); ans = min(ans, min(d1 * 2 + d3 * 2, d2 * 2 + d3 * 2)); cout << ans << '\n'; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; int n; int A[maxn]; int main() { cin >> n; for (int i = 1; i <= n; ++i) cin >> A[i]; sort(A + 1, A + 1 + n); int ans = 0; for (int i = 1, j = n; i <= j; ++i, --j) { if (A[i] == 1 && A[j] == 2) { ++ans; continue; } if (A[i] == 1 && A[j] == 1) { ans = ans + (j - i + 1) / 3; break; } if (A[i] == 2 && A[j] == 2) break; } cout << ans << endl; return 0; }
1
#include<bits/stdc++.h> using namespace std; char s[200001]; int sum=0,ans=0; int main() { scanf("%s",s); int len=strlen(s); for (int i=0;i<len;i++) { if (s[i]=='S') sum++; else if (sum>0) { ans+=2; sum--; } } printf("%d",len-ans); }
0
#include <bits/stdc++.h> using namespace std; bool sortinrev(const pair<long long int, long long int> &a, const pair<long long int, long long int> &b) { return (a.first > b.first); } bool sortbysec(const pair<long long int, long long int> &a, const pair<long long int, long long int> &b) { return (a.second < b.second); } int dx[4] = {0, -1, 0, 1}; int dy[4] = {1, 0, -1, 0}; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t; cin >> t; while (t--) { long long int a, b, c, d; cin >> a >> b >> c >> d; long long int ans = 0; if (d >= c) { if (a <= b) { ans = b; } else ans = -1; } else if (d < c) { if (a <= b) ans = b; else if (a > b) { ans += b; long long int count = 0; long long int diff = (c - d); long long int need = a - b; count += (need / diff); long long int rem = need % diff; if (rem) count++; ans += (count * c); } } cout << ans << endl; } return 0; }
1
#include<iostream> #include<sstream> #include<algorithm> using namespace std; bool f(string a, string b, string c, int x){ int crr=0; for(int i=0;i<(int)c.length();i++){ int n,m, l; n=(i>=(int)a.length())?0 :(a[i]=='X')?x:a[i]-'0'; m=(i>=(int)b.length())?0 :(b[i]=='X')?x:b[i]-'0'; l=(i>=(int)c.length())?0 :(c[i]=='X')?x:c[i]-'0'; if((n+m+crr)%10!=l)return false; crr=(n+m+crr)/10; } return true; } int main(){ string str; while(cin>>str){ if(str=="END")break; string a,b,c; int flg=0; for(int i=0;i<(int)str.length();i++){ if(str[i]=='+'){ a = str.substr(0,i); reverse(a.begin(),a.end()); }else if(str[i]=='='){ b=str.substr(a.length()+1, i-a.length()-1); c=str.substr(i+1, str.length()-i); reverse(b.begin(),b.end()); reverse(c.begin(),c.end()); } } if(!((a.length()!=1&&(*(a.end()-1)=='X')) ||(b.length()!=1&&(*(b.end()-1)=='X')) ||(c.length()!=1&&(*(c.end()-1)=='X')))) { if(f(a,b,c,0)){ cout<<0<<endl;flg=1; } } for(int i=1;i<=9&&!flg;i++){ if(f(a,b,c,i)){ cout<<i<<endl;flg=1; } } if(!flg)cout<<"NA"<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1}; const int N = 1505; int n, m, sx, sy; int vis[N][N]; char mp[N][N]; struct myt { int x, y; } fir[N][N]; int read() { int s = 0, w = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') s = (s << 3) + (s << 1) + ch - '0', ch = getchar(); return s * w; } void dfs(int x, int y) { int xx = x, yy = y; while (xx < 0) xx += n; xx %= n; while (yy < 0) yy += m; yy %= m; if (mp[xx][yy] == '#') return; if (vis[xx][yy] && (fir[xx][yy].x != x || fir[xx][yy].y != y)) { printf("Yes"); exit(0); } if (vis[xx][yy]) return; vis[xx][yy] = 1; fir[xx][yy].x = x; fir[xx][yy].y = y; for (int i = 0; i < 4; ++i) dfs(x + dx[i], y + dy[i]); return; } int main() { n = read(); m = read(); for (int i = 0; i < n; ++i) { scanf("%s", mp[i]); for (int j = 0; j < m; ++j) if (mp[i][j] == 'S') sx = i, sy = j; } dfs(sx, sy); printf("No"); return 0; }
4
#include <bits/stdc++.h> using namespace std; typedef long long LL; LL x[300005], y[300005]; LL d[300005]; int n, m; std::vector<int> G[300005]; int main(int argc, char const *argv[]) { ios_base::sync_with_stdio(0); cin.tie(0); int n, m; cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> x[i] >> y[i]; d[i] = y[i] - x[i]; } for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; u--; v--; G[u].push_back(v); G[v].push_back(u); } LL sx = accumulate(x, x + n, 0LL); std::vector<LL> sorted_d(d, d + n); sort(sorted_d.begin(), sorted_d.end()); std::vector<LL> pre_d(n + 1); for (int i = 1; i <= n; ++i) pre_d[i] = pre_d[i - 1] + sorted_d[i - 1]; for (int i = 0; i < n; ++i) { LL ans = sx + y[i] * (n - 1) - x[i]; int pos = lower_bound(sorted_d.begin(), sorted_d.end(), d[i]) - sorted_d.begin(); ans += pre_d[pos] - pos * d[i]; for (int u : G[i]) { ans -= x[i] + x[u] + min(d[i], d[u]); } std::cout << ans << ' '; } return 0; }
5
#include <bits/stdc++.h> using namespace std; template <typename T> ostream& operator<<(ostream& _s, vector<T> _t) { for (int i = 0; i < (int)(_t.size()); i++) _s << (i ? " " : "") << _t[i]; return _s; } template <typename T> istream& operator>>(istream& _s, vector<T>& _t) { for (int i = 0; i < (int)(_t.size()); i++) _s >> _t[i]; return _s; } template <typename T, typename U> ostream& operator<<(ostream& _s, pair<T, U> _t) { _s << "(" << _t.first << "," << _t.second << ")"; return _s; } template <typename T, typename U> istream& operator>>(istream& _s, pair<T, U>& _t) { _s >> _t.first >> _t.second; return _s; } int find(const vector<int>& v, int value, int fr, int to) { for (int i = fr; i < (int)(to + 1); i++) { if (v[i] == value) { return i; } } assert(false); } vector<int> invert(const vector<int>& v) { vector<int> res(v.size()); for (int i = 0; i < (int)(v.size()); i++) { res[v[i]] = i; } return res; } int main() { ios_base::sync_with_stdio(false); int n; cin >> n; vector<int> p(n); cin >> p; for (int i = 0; i < (int)(n); i++) p[i]--; bool is_id = true; for (int i = 0; i < (int)(n); i++) { if (p[i] != i) is_id = false; } string base_s; for (int j = 0; j < (int)(n); j++) base_s += '.'; vector<string> res(n); for (int i = 0; i < (int)(n); i++) res[i] = base_s; if (is_id) { cout << n << endl; for (int i = 0; i < (int)(n); i++) { cout << base_s << endl; } return 0; } p = invert(p); int fr = 0, to = n - 1; for (int row = 0; row < (int)(n); row++) { if (row == 0) { int mid = n / 2; int pos = find(p, mid, fr, to); for (int i = fr; i < (int)(pos + 1); i++) { res[row][i] = '\\'; } for (int i = (pos)-1; i >= (int)(fr); i--) { p[i + 1] = p[i]; } p[fr] = -1; } else { bool left = row % 2 == 1; int free_pos = left ? fr : to; assert(p[free_pos] == -1); int pos = find(p, free_pos, fr, to); if (left) { res[row][fr] = '/'; for (int i = pos; i < (int)(to + 1); i++) { res[row][i] = '/'; } for (int i = pos; i < (int)(to); i++) { p[i] = p[i + 1]; } p[to] = -1; fr++; } else { res[row][to] = '\\'; for (int i = fr; i < (int)(pos + 1); i++) { res[row][i] = '\\'; } for (int i = (pos)-1; i >= (int)(fr); i--) { p[i + 1] = p[i]; } p[fr] = -1; to--; } } } cout << (n - 1) << endl; for (int i = 0; i < (int)(n); i++) { cout << res[i] << endl; } }
5
#include <bits/stdc++.h> using namespace std; int main() { int sum = 0, x; for (int i = 0; i < 5; i++) { cin >> x; sum += max(40, x); } cout << sum / 5 << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, k; long long ckj[500005]; inline void initckj() { ckj[0] = 1; for (int i = 1; i <= n; i++) { ckj[i] = ckj[i - 1] * i % 998244353; } } inline long long npy(long long a) { long long b = 998244353 - 2, res = 1, ans = a; while (b) { if (b & 1) { res *= ans; res %= 998244353; } ans *= ans; ans %= 998244353; b >>= 1; } return res; } inline long long C(int n, int m) { return ckj[n] * npy(ckj[m] * ckj[n - m] % 998244353) % 998244353; } int main() { scanf("%d%d", &n, &k); initckj(); long long ans = 0; for (int i = 1; i <= n; i++) { int m = n / i; if (m < k) { continue; } ans += C(m - 1, k - 1); ans %= 998244353; } printf("%lld\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; inline void rd(int &x) { x = 0; char ch = 0; int w = 0; while (!isdigit(ch)) ch = getchar(), w |= ch == '-'; while (isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); x = w ? -x : x; } double l, r = 1e9; double f[55][6005]; int n, m, a[55], b[55], p[55]; bool ok(double k) { for (int i = n; i; i--) { for (int j = m + 1; j < 6005; j++) f[i + 1][j] = k; for (int j = 0; j <= m; j++) f[i][j] = min(k, (f[i + 1][j + a[i]] + a[i]) * p[i] / 100.0 + (f[i + 1][j + b[i]] + b[i]) * (100 - p[i]) / 100.0); } return f[1][0] < k; } signed main() { rd(n); rd(m); for (int i = 1; i <= n; i++) rd(a[i]), rd(b[i]), rd(p[i]); for (int i = 1; i <= 60; i++) { double mid = (l + r) * 0.5; ok(mid) ? r = mid : l = mid; } printf("%.10lf", l); }
3
#include<bits/stdc++.h> using namespace std; int main() { long long n,i,sum=0; cin>>n; for(i=1;i<n;i++) { sum=sum+(n-1)/i; } cout<<sum; }
0
#include <iostream> #include <vector> #include <cmath> using namespace std; struct P { int x, y; P(int x, int y) : x{x}, y{y} {} }; int limit; vector<int> v; constexpr int X[] = {2, 1, 2, 3, 0, 1, 2, 3, 4, 1, 2, 3, 2}; constexpr int Y[] = {0, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 4}; int next_pos(int x, int y) { for (int i = 0; i < 13; i++) { if (x == X[i] && y == Y[i]) { return i; } } return -1; } int getMD() { int sum = 0; for (int i = 0; i < 13; i++) { if (v[i] == 0) continue; sum += abs(X[i] - X[v[i]]) + abs(Y[i] - Y[v[i]]); } return sum; } constexpr int dx[] = {-1, 0, 1, 0}; constexpr int dy[] = {0, -1, 0, 1}; bool dfs(const int* sp, int step) { int md = getMD(); if (md == 0) return 1; if (md + step > limit) { return 0; } for (int i = 0; i < 2; i++) { int x = X[sp[i]], y = Y[sp[i]]; for (int j = 0; j < 4; j++) { int nx = x + dx[j], ny = y + dy[j]; int next = next_pos(nx, ny); if (next == -1) continue; const int nsp1[] = {next, sp[1]}, nsp2[] = {sp[0], next}; swap(v[sp[i]], v[next]); if (i == 0 && dfs(nsp1, step + 1)) { return 1; } if (i == 1 && dfs(nsp2, step + 1)) { return 1; } swap(v[sp[i]], v[next]); } } return 0; } int main() { int x; while (cin >> x, x != -1) { int sp[2]; v.resize(13); v[0] = x; for (int i = 1; i < 13; i++) { cin >> v[i]; } for (int i = 0, j = 0; i < 13; i++) { if (v[i] == 0) { sp[j++] = i; } } bool found = 0; constexpr int LIMIT = 20; for (limit = 0; limit <= LIMIT; limit++) { if (dfs(sp, 0)) { cout << limit << endl; found = 1; break; } } if (!found) { cout << "NA" << endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; const double PI = atan(1.0) * 4; const int INF = 0x7ffffff; const long long MOD = 1000000007; const int maxn = 5010; long long GCD(long long a, long long b) { return b == 0 ? a : GCD(b, a % b); } int a[maxn]; int cnt[maxn]; int dp[maxn][maxn]; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) { while (i + cnt[i] <= n && a[i + cnt[i]] - a[i] <= 5) cnt[i]++; } for (int i = 1; i <= n; i++) { } for (int i = 1; i <= n; i++) { for (int j = 1; j <= k; j++) { dp[i][j] = max(dp[i][j], dp[i - 1][j]); dp[i + cnt[i] - 1][j] = max(dp[i + cnt[i] - 1][j], dp[i - 1][j - 1] + cnt[i]); } } for (int i = 0; i <= n; i++) { for (int j = 0; j <= k; j++) { } } printf("%d\n", dp[n][k]); return 0; }
3
#include <bits/stdc++.h> #define LL long long #define PB push_back #define X first #define Y second #define INF 1000 * 1000 * 1000 using namespace std; const LL mod = 1000 * 1000 * 1000 + 7; const LL ob = (mod + 1) >> 1; vector<pair<LL , LL> > cols[100005]; LL p[100005]; LL rowMin[100005]; LL value[100005]; LL R , C , n; vector<int> values[100005]; int find(int u) { if(u == p[u]) return u; return p[u] = find(p[u]); } void init() { for(LL i = 1; i <= R; i++) { p[i] = i; values[i].PB(i); } } int main() { ios_base::sync_with_stdio(0); cin >> R >> C >> n; for(LL i = 1; i <= R; i++) rowMin[i] = INF * (LL) INF; for(LL i = 0; i < n; i++) { LL r , c , a; cin >> r >> c >> a; rowMin[r] = min(rowMin[r] , a); cols[c].PB({r , a}); } init(); for(LL i = 1; i <= C; i++) { sort(cols[i].begin() , cols[i].end()); for(LL j = 1; j < cols[i].size(); j++) { LL diff = cols[i][j].Y - cols[i][j - 1].Y; int u = find(cols[i][j].X) , v = find(cols[i][j - 1].X); LL oldDiff = value[cols[i][j].X] - value[cols[i][j - 1].X]; if(values[u].size() < values[v].size()) { oldDiff = -oldDiff; swap(u , v); diff = -diff; } if(u == v && oldDiff != diff) { cout << "No" << endl; return 0; } LL newDiff = oldDiff - diff; if(u != v) { for(auto x : values[v]) { values[u].PB(x); value[x] += newDiff; } p[v] = u; } } } for(int i = 1; i <= R; i++) { if(p[i] == i) { LL minima = INF * (LL)INF; for(auto x : values[i]) minima = min(minima , value[x]); for(auto x : values[i]) { if(value[x] - minima > rowMin[x]) { cout << "No" << endl; return 0; } } } } cout << "Yes" << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, m, i, j, a[1000]; int main() { cin >> n >> m; while (m) { a[j++] = m % n; m /= n; } for (i = 0; i < j; i++) { if (a[i] >= n) { a[i + 1]++; a[i] %= n; } if (a[i] > 1 && n - a[i] != 1) { cout << "NO"; return 0; } else if (n - a[i] == 1 && n != 2) a[i + 1]++; } cout << "YES"; return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, m, x; const int mod = 1e9 + 7; vector<int> dp[320][320]; int solve(int rem, int grup, int pos) { if (rem < 0 || grup < 0) return 0; if (dp[rem][grup][pos] != -1) return dp[rem][grup][pos]; if (pos == m + 1) { if (rem == 0 && grup == 0) { return 1; } else return 0; } int ret; if (pos == x) { ret = solve(rem - 1, grup + 1, pos + 1) + solve(rem - 1, grup, pos + 1); ret %= mod; } else { ret = solve(rem - 1, grup + 1, pos + 1) + solve(rem, grup, pos + 1); ret %= mod; ret += solve(rem, grup - 1, pos + 1); ret %= mod; ret += solve(rem - 1, grup, pos + 1); ret %= mod; } return dp[rem][grup][pos] = ret; } int main() { ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0); cin >> n >> m >> x; if (n > m) { cout << 0 << endl; return 0; } for (int i = 0; i <= n + 1; i++) { for (int j = 0; j <= n + 1; j++) { for (int k = 0; k <= m + 1; k++) { dp[i][j].push_back(-1); } } } long long ans = (long long)solve(n, 0, 1); for (long long i = 1; i <= n; i++) { ans = (ans * i) % (long long)mod; } cout << ans << endl; }
5
#include <bits/stdc++.h> using namespace std; int main(){ int N,K; cin>>N>>K; vector<int>A(N); for(int i=0;i<N;i++)cin>>A[i]; vector<int>DP(N,1000000000); DP[0]=0; for(int i=0;i<N-1;i++){ for(int j=1;j<=K;j++){ if(i+j>=N)break; DP[i+j]=min(DP[i+j],DP[i]+abs(A[i+j]-A[i])); } } cout<<DP[N-1]<<endl; }
0
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1.0); #pragma GCC diagnostic ignored "-Wsign-compare" int n, m; vector<int> a(1e5 + 5, 0); vector<set<int> > v(17); long long ret = 0; long long calc(int second, int first) { long long cnt = second - first - 1; return cnt * (cnt + 1) / 2; } int main() { cin.sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int o = 0; o <= 16; o++) { v[o].insert(0); v[o].insert(n + 1); int cnt = 0; int num = 1 << o; for (int i = 1; i <= n; i++) if (a[i] & num) { cnt++; } else { v[o].insert(i); ret += cnt * (cnt + 1ll) / 2 * num; cnt = 0; } ret += cnt * (cnt + 1ll) / 2 * num; } for (int i = 0; i < m; i++) { int p, c; cin >> p >> c; for (int o = 0; o <= 16; o++) { int num = 1 << o; int tag = c & num; if (tag == (a[p] & num)) continue; if (tag) { auto t = v[o].find(p); auto l = t; auto r = t; --l; ++r; v[o].erase(t); ret -= calc(*t, *l) * num; ret -= calc(*r, *t) * num; ret += calc(*r, *l) * num; } else { auto t = v[o].insert(p).first; auto l = t; auto r = t; --l; ++r; ret -= calc(*r, *l) * num; ret += calc(*t, *l) * num; ret += calc(*r, *t) * num; } } a[p] = c; cout << ret << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long n, s, k, t; cin >> n >> s >> k; vector<int> a; for (long long i = 0; i < k; ++i) cin >> t, a.push_back(t); sort(a.begin(), a.end()); for (int i = 0; i <= k; i++) { if (!binary_search(a.begin(), a.end(), s + i) && s + i <= n) { cout << i; break; } else if (!binary_search(a.begin(), a.end(), s - i) && s - i > 0) { cout << i; break; } } cout << "\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; char st[100005]; int n, m, a[100005], f[100005], g[100005]; int main() { int i, j, p, q; scanf("%d\n", &m); scanf("%s", st + 1); n = strlen(st + 1); for (i = 'a'; i <= 'z'; i++) { for (j = 1; j <= n; j++) a[j] = (st[j] == i); for (j = 1; j <= n; j++) g[j] = f[j] | a[j]; p = 0, g[n + 1] = 1; for (j = 1; j <= n + 1; j++) if (g[j]) { if (j - p > m) break; p = j; } if (j <= n + 1) { for (j = 1; j <= n; j++) { if (a[j]) printf("%c", i); f[j] = g[j]; } continue; } else { p = q = 0; for (j = 1; j <= n + 1; j++) { if (j - p > m) { p = q, q = 0; printf("%c", i); } if (f[j]) { p = j, q = 0; } if (a[j]) q = j; } return 0; } } return 0; }
4
#include <bits/stdc++.h> using namespace std; template<class T> struct WeightedUnionFind{ vector<int> par,rnk; vector<T> pardiff; WeightedUnionFind(int n) : par(n),rnk(n),pardiff(n){ for(int i = 0;i < n;i++) par[i] = i; } int find(int x){ if(x == par[x]) return x; int root = find(par[x]); pardiff[x] += pardiff[par[x]]; return par[x] = root; } bool unite(int x,int y,T w){ if(same(x,y)) return diff(x,y) == w; w = w + weight(x) - weight(y); x = find(x); y = find(y); if(rnk[x] < rnk[y]) swap(x,y), w = -w; if(rnk[x] == rnk[y]) rnk[x]++; par[y] = x; pardiff[y] = w; return true; } inline bool same(int x,int y){ return find(x) == find(y); } T diff(int x,int y){ assert(same(x,y)); return weight(y) - weight(x); } T weight(int x){ find(x); return pardiff[x]; } }; using ll = long long; int n,m; void solve(){ WeightedUnionFind<ll> wuf(n); for(int i = 0;i < m;i++){ char c[1]; int a,b; scanf("%s %d %d",c,&a,&b); a--;b--; if(c[0] == '!'){ ll w; scanf("%lld",&w); wuf.unite(a,b,w); }else{ if(!wuf.same(a,b)) puts("UNKNOWN"); else printf("%lld\n",wuf.diff(a,b)); } } } signed main(){ while(scanf("%d %d",&n,&m),n) solve(); }
0
#include<bits/stdc++.h> using namespace std; set<int> s; int main() { int n; scanf("%d",&n); for(int i=0;i<n;i++) { int a; scanf("%d",&a); if(s.find(a)!=s.end()) { s.erase(a); } else { s.insert(a); } } printf("%d",s.size()); }
0
#include <bits/stdc++.h> int top; struct trie { int nxt[26]; int flag; void rs() { flag = 0; memset(nxt, 0, sizeof(nxt)); } }; trie root[100000]; void init() { top = 1; root[0].rs(); } void ins(char *str) { char *s = str; int p = 0; for (; *s; ++s) { int k = *s - 'a'; if (!root[p].nxt[k]) { root[top].rs(); root[p].nxt[k] = top++; } p = root[p].nxt[k]; } root[p].flag = 1; } bool find(char *s) { int p = 0; while (*s) { int k = *s - 'a'; if (root[p].nxt[k] == 0) return false; p = root[p].nxt[k]; ++s; } return true; } int main(int argc, char *argv[]) { int n; char s[25]; scanf("%d", &n); init(); for (int i = 0; i < n; ++i) { scanf("%s", s); char *p = s; while (*p) { ins(p++); } } for (int i = 0; i < 26; ++i) { s[0] = 'a' + i; s[1] = '\0'; if (!find(s)) { printf("%c\n", 'a' + i); return 0; } } for (int i = 0; i < 26; ++i) for (int j = 0; j < 26; ++j) { s[0] = 'a' + i; s[1] = 'a' + j; s[2] = '\0'; if (!find(s)) { printf("%s\n", s); return 0; } } return EXIT_SUCCESS; }
2
#include <bits/stdc++.h> using namespace std; const int inf = 2e9; const int N = 1e6 + 11; const int mod = 1e9 + 7; int f[N]; pair<int, int> a[N]; int main() { ios::sync_with_stdio(0); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i].first >> a[i].second; } sort(a, a + n); for (int i = 1; i < n; i++) { int l = -1, r = i, left = a[i].first - a[i].second; while (r - l > 1) { int m = (l + r) >> 1; if (a[m].first < left) l = m; else r = m; } f[i] = i - r + (r ? f[r - 1] : 0); } int res = n; for (int i = 1; i <= n; i++) { res = min(f[i - 1] + n - i, res); } cout << res; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { double l, d, v, g, r, a, b, c = 0, n; int m, k1, k2; cin >> l >> d >> v >> g >> r; k1 = d / v; k2 = g + r; a = d / v; b = g + r; m = k1 % k2; n = floor(a / b); if (m < g) { cout << setprecision(8) << (l / v) << endl; return 0; } else if (m >= g) { cout << setprecision(8) << ((n * b + b) + ((l - d) / v)) << endl; return 0; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n; int t; cin >> n; cin >> t; string a, b; cin >> a >> b; int p = 0; char c[a.length()]; c[a.length()] = '\0'; for (int i = 0; i < n; i++) { if (a[i] == b[i]) p++; } if (n - t <= p) { int r = n - t; for (int i = 0; i < n; i++) { if (r && a[i] == b[i]) { c[i] = a[i]; r--; } else { for (char t = 'a'; t <= 'z'; t++) { if (t != a[i] && t != b[i]) { c[i] = t; break; } } } } cout << c; } else { if (n - p < 2 * (n - t - p)) { cout << -1; } else { int r = 2 * (n - t - p); for (int i = 0; i < n; i++) { if (a[i] == b[i]) { c[i] = a[i]; } else { if (r) { if (r > (n - t - p)) { c[i] = a[i]; } else c[i] = b[i]; r--; } else { for (char t = 'a'; t <= 'z'; t++) { if (t != a[i] && t != b[i]) { c[i] = t; break; } } } } } cout << c; } } }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 10; int N, K; string str[MAXN]; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> N >> K; for (int i = 0; i < N; i++) { cin >> str[i]; } vector<int> perm; for (int i = 0; i < K; i++) perm.push_back(i); int ans = 1000000000; do { int mnm = 1000000000; int mks = 0; for (int i = 0; i < N; i++) { string result = str[i]; for (int j = 0; j < K; j++) { result[j] = str[i][perm[j]]; } int res_int = stoi(result); mnm = min(mnm, res_int); mks = max(mks, res_int); } ans = min(ans, mks - mnm); } while (next_permutation(perm.begin(), perm.end())); cout << ans << "\n"; }
2
#include <bits/stdc++.h> using namespace std; int arr[52][52]; int main() { int n, m; cin >> n >> m; int flag = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { cin >> arr[i][j]; if (arr[i][j]) { if (j == 0 || j == m - 1 || i == 0 || i == n - 1) flag = 1; } } if (flag) cout << 2 << endl; else cout << 4 << endl; }
1
#include <bits/stdc++.h> using namespace std; long long d(long long n) { for (long long i = 2; i <= sqrt(n); i++) { if (n % i == 0) return n / i; } return 1; } int main() { int q; cin >> q; for (int l = 1; l <= q; l++) { long long n; cin >> n; int x = 0; long long tmp = n; while (tmp > 0) { tmp /= 2; x++; } long long p = 1LL << x; if (n != p - 1) cout << p - 1; else cout << d(n); cout << endl; } }
3
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { int A, B, C, X, Y; cin >> A >> B >> C >> X >> Y; ll ans = min(A*X+B*Y, min(2*C*max(X,Y), 2*C*min(X,Y)+A*max(0,X-Y)+B*max(0,Y-X))); cout << ans << "\n"; }
0
#include <iostream> int main(){int a,b;std::cin>>a>>b;std::cout<<((a<=8&&b<=8)?"Yay!":":(");}
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 251, MAXE = 1e3 + 51, inf = 0x7fffffff; struct Edge { int to, prev, flow; }; Edge ed[MAXE << 2]; int nc, ec, source, sink, tot = 1, from, to, flow, maxFlow, rres, p, q, id, wt, ptr; int last[MAXN], depth[MAXN], inQueue[MAXN], curr[MAXN], res[MAXN], vis[MAXN << 1]; inline int read() { register int num = 0, neg = 1; register char ch = getchar(); while (!isdigit(ch) && ch != '-') { ch = getchar(); } if (ch == '-') { neg = -1; ch = getchar(); } while (isdigit(ch)) { num = (num << 3) + (num << 1) + (ch - '0'); ch = getchar(); } return num * neg; } inline void addEdge(int from, int to, int flow) { ed[++tot].prev = last[from]; ed[tot].to = to; ed[tot].flow = flow; last[from] = tot; } inline int Min(int x, int y) { return x < y ? x : y; } inline bool bfs() { queue<int> q; int top, to; memset(depth, 0x3f, sizeof(depth)); for (register int i = 1; i <= nc; i++) { curr[i] = last[i]; } depth[source] = 0, q.push(source); while (!q.empty()) { top = q.front(); q.pop(), inQueue[top] = 0; for (register int i = last[top]; i; i = ed[i].prev) { to = ed[i].to; if (depth[to] > depth[top] + 1 && ed[i].flow) { depth[to] = depth[top] + 1; if (!inQueue[to]) { q.push(to), inQueue[to] = 1; } } } } if (depth[sink] != 0x3f3f3f3f) { return 1; } return 0; } inline int dfs(int cur, int flow) { int curFlow = 0, low; if (cur == sink) { return flow; } for (register int i = curr[cur]; i; i = ed[i].prev) { curr[cur] = i; if (ed[i].flow && depth[ed[i].to] == depth[cur] + 1) { low = dfs(ed[i].to, Min(flow - curFlow, ed[i].flow)); ed[i].flow -= low, ed[i ^ 1].flow += low, curFlow += low; if (!low) { depth[ed[i].to] = 0; } if (curFlow == flow) { break; } } } return curFlow; } inline int Dinic() { int flow; while (bfs()) { while (flow = dfs(source, inf)) { maxFlow += flow; } } return maxFlow; } inline void clearFlow() { maxFlow = 0; for (register int i = 2; i <= tot; i += 2) { ed[i].flow += ed[i ^ 1].flow, ed[i ^ 1].flow = 0; } } namespace GomoryHuTree { struct Edge { int to, prev, dist; }; Edge ed[MAXN << 1]; int tot; int last[MAXN], nd[MAXN], tmp[MAXN], tmp2[MAXN]; inline void addEdge(int from, int to, int dist) { ed[++tot].prev = last[from]; ed[tot].to = to; ed[tot].dist = dist; last[from] = tot; } inline void setup() { for (register int i = 1; i <= nc; i++) { nd[i] = i; } } inline void create(int l, int r) { int cut, cnt, ccnt; if (l == r) { return; } source = nd[l], sink = nd[l + 1], cut = Dinic(), clearFlow(); addEdge(source, sink, cut), addEdge(sink, source, cut), cnt = ccnt = 0; for (register int i = l; i <= r; i++) { ::depth[nd[i]] != 0x3f3f3f3f ? tmp[++cnt] = nd[i] : tmp2[++ccnt] = nd[i]; } for (register int i = l; i < l + cnt; i++) { nd[i] = tmp[i - l + 1]; } for (register int i = l + cnt; i <= r; i++) { nd[i] = tmp2[i - cnt - l + 1]; } create(l, l + cnt - 1), create(l + cnt, r); } inline int dfs(int node, int fa) { int son = 0; for (register int i = last[node]; i; i = ed[i].prev) { if (ed[i].to != fa && !vis[i]) { if (ed[i].dist < wt) { p = node, q = ed[i].to, id = i, wt = ed[i].dist; } dfs(ed[i].to, node), son = 1; } } return son; } } // namespace GomoryHuTree using GomoryHuTree::create; using GomoryHuTree::setup; inline void ddfs(int node) { int x, y; wt = 0x3f3f3f3f; if (!GomoryHuTree::dfs(node, 0)) { res[++ptr] = node; return; } vis[id] = vis[((id - 1) ^ 1) + 1] = 1, rres += wt, x = p, y = q; ddfs(x), ddfs(y); } int main() { nc = read(), ec = read(), setup(); for (register int i = 0; i < ec; i++) { from = read(), to = read(), flow = read(); addEdge(from, to, flow), addEdge(to, from, 0); addEdge(to, from, flow), addEdge(from, to, 0); } create(1, nc), ddfs(1), printf("%d\n", rres); for (register int i = 1; i <= ptr; i++) { printf("%d ", res[i]); } }
5
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; string s; char ans[10][20]; void init() { cin >> s; } void solve() { int count[30]; int rep = 0; for (int i = 0; i < 26; ++i) count[i] = 0; for (int i = 0; i < s.size(); ++i) ++count[s[i] - 'A']; for (int i = 0; i < 26; ++i) if (count[i] > 1) rep = i; int a = 0, b = 0; for (int i = 0; i < s.size(); ++i) { if (s[i] - 'A' == rep) break; ++a; } for (int i = s.size() - 1; i >= 0; --i) { if (s[i] - 'A' == rep) break; ++b; } if (a + b == 25) { cout << "Impossible" << endl; return; } int sm = a + b; if (sm % 2 == 0) { if (a > b) { int sub = a - sm / 2; int idx = 0; for (int i = sub - 1; i >= 0; --i) ans[1][i] = s[idx++]; for (int i = 0; i < 13; ++i) ans[0][i] = s[idx++]; for (int i = 12; i >= sub; --i) { if (s[idx] - 'A' == rep) ++idx; ans[1][i] = s[idx++]; } } else if (a == b) { int idx = 0; for (int i = 0; i < 13; ++i) ans[0][i] = s[idx++]; for (int i = 12; i >= 0; --i) { if (s[idx] - 'A' == rep) ++idx; ans[1][i] = s[idx++]; } } else { int sub = b - sm / 2; int idx = 0; for (int i = sub; i < 13; ++i) ans[0][i] = s[idx++]; for (int i = 12; i >= 0; --i) { if (s[idx] - 'A' == rep) ++idx; ans[1][i] = s[idx++]; } for (int i = 0; i < sub; ++i) ans[0][i] = s[idx++]; } } else { if (a > (b + 1)) { int idx = 0; int sub = a - (a + b + 1) / 2; for (int i = sub - 1; i >= 0; --i) ans[1][i] = s[idx++]; for (int i = 0; i < 13; ++i) { if (s[idx] - 'A' == rep) ++idx; ans[0][i] = s[idx++]; } for (int i = 12; i >= sub; --i) { ans[1][i] = s[idx++]; } } else if (a == (b + 1)) { int idx = 0; for (int i = 0; i < 13; ++i) { if (s[idx] - 'A' == rep) ++idx; ans[0][i] = s[idx++]; } for (int i = 12; i >= 0; --i) ans[1][i] = s[idx++]; } else if ((a + 1) == b) { int idx = 0; for (int i = 0; i < 13; ++i) ans[0][i] = s[idx++]; for (int i = 12; i >= 0; --i) { if (s[idx] - 'A' == rep) idx++; ans[1][i] = s[idx++]; } } else { int idx = 0; int sub = b - (a + b + 1) / 2; for (int i = sub; i < 13; ++i) ans[0][i] = s[idx++]; for (int i = 12; i >= 0; --i) { if (s[idx] - 'A' == rep) idx++; ans[1][i] = s[idx++]; } for (int i = 0; i < sub; ++i) ans[0][i] = s[idx++]; } } for (int i = 0; i < 13; ++i) cout << ans[0][i]; cout << endl; for (int i = 0; i < 13; ++i) cout << ans[1][i]; cout << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); ; int __ = 1; while (__--) { init(); solve(); } return 0; }
3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:30777216") using namespace std; long long mod = 1000000007; long long n; vector<vector<long long> > edge, edge1; vector<long long> fact; long long t[500001]; set<long long> my_set; int main() { long long k, sum = 0, s, x_1, x_2, x_3, aa, bb; string a, b; cin >> a >> b; vector<long long> pref, suf; long long tr = 0; for (int i = 0; i < b.size(); i++) { if (b[i] == '1') tr++; pref.push_back(tr); } long long ans = 0; for (int i = 0; i < a.size(); i++) { long long u = 0; if (i == 0) u = 0; else u = pref[i - 1]; long long place = b.size() - a.size() + i; if (a[i] == '0') { ans += (pref[place] - u); } else { ans += (b.size() - a.size() + 1) - (pref[place] - u); } } cout << ans; return 0; }
2
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; const int MIN = -1e9, MAX = 1e9, MOD = 1e9 + 7; void yes() { cout << "YES\n"; return; } void no() { cout << "NO\n"; return; } void out(long long int n) { cout << n << endl; return; } void outs(string str) { cout << str << endl; return; } void init() {} void solve(int case_no) { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); vector<int> ans; bool ck[3] = {false}; bool idk = false; for (int i = 0; i < n; i++) { if (a[i] == 0 && !idk) { ans.push_back(a[i]); idk = true; } else if (a[i] == 100 && !ck[2]) { ans.push_back(a[i]); ck[2] = true; } else { if (a[i] < 10 && !ck[0]) { ans.push_back(a[i]); ck[0] = true; } else if (a[i] % 10 == 0 && !ck[1]) { ans.push_back(a[i]); ck[1] = true; } } } if (!ck[0] && !ck[1]) { for (int i = 0; i < n; i++) { if (0 < a[i] && a[i] < 100) { ans.push_back(a[i]); break; } } } out(ans.size()); for (int i : ans) cout << i << ' '; cout << endl; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); srand(time(NULL)); init(); int t = 1; for (int i = 1; i <= t; i++) solve(i); }
1
#include<bits/stdc++.h> using namespace std; #define N 201234 #define ll long long map<ll,int>id; ll bit[N]; int n,k,a[N];ll s[N],sb[N],sd[N]; int main(){ int i,j=n; ll ans=0; scanf("%d%d",&n,&k); for(i=1;i<=n;i++)scanf("%d",a+i),s[i]=s[i-1]+a[i]; for(i=1;i<=n;i++)sb[i]=s[i]=sd[i]=s[i-1]+a[i]-k; sort(sb+1,sb+n+1); int ln=unique(sb+1,sb+n+1)-sb-1; for(i=1;i<=ln;i++)id[sb[i]]=i; for(i=1;i<=n;i++)s[i]=id[s[i]]; for(i=1;i<=n;i++){ for(j=s[i];j;j-=j&-j)ans+=bit[j]; for(j=s[i];j<=ln;j+=j&-j)bit[j]++; ans+=sd[i]>=0; } cout<<ans; return 0; }
0
#include <iostream> #include <limits> #include <queue> #include <tuple> #include <vector> #define int long long #define loop(i, a, n) for (int i = (a); i < (n); i++) #define rep(i, n) loop(i, 0, n) template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; } using namespace std; using Weight = int; struct Edge { int src, dst; Weight weight; Edge(const int s = 0, const int d = 0, const Weight w = 0) : src(s), dst(d), weight(w) {} }; using Edges = std::vector<Edge>; class Graph { std::vector<Edges> g; public: Graph(const int size = 0) : g(size) {} size_t size() const { return g.size(); } const Edges &operator[](const int i) const { return g[i]; } void addArc(const int src, const int dst, const Weight w = 1) { g[src].emplace_back(src, dst, w); } void addEdge(const int node1, const int node2, const Weight w = 1) { addArc(node1, node2, w); addArc(node2, node1, w); } }; template<int inf = std::numeric_limits<Weight>::max() / 8> std::vector<Weight> dijkstra(const Graph &g, const int src) { using state = std::pair<Weight, int>; std::priority_queue<state, std::vector<state>, std::greater<state>> q; std::vector<Weight> dist(g.size(), inf); dist[src] = 0; q.emplace(0, src); while (q.size()) { Weight d; int v; std::tie(d, v) = q.top(); q.pop(); if (dist[v] < d) continue; for (auto &e : g[v]) { if (cmin(dist[e.dst], dist[v] + e.weight)) q.emplace(dist[e.dst], e.dst); } } return dist; } signed main() { int n, m, s, t; cin >> n >> m >> s >> t; --s, --t; Graph g(n); rep(i, m) { int a, b, c, d; cin >> a >> b >> c >> d; --a, --b, --c; g.addEdge(a, b, d); g.addEdge(b, c, d); g.addEdge(c, a, d); } cout << dijkstra(g, s)[t] << endl; }
0
#include <bits/stdc++.h> using namespace std; void debug_out() { cerr << endl; } template <class T> ostream& prnt(ostream& out, T v) { out << v.size() << '\n'; for (auto e : v) out << e << ' '; return out; } template <class T> ostream& operator<<(ostream& out, vector<T> v) { return prnt(out, v); } template <class T> ostream& operator<<(ostream& out, set<T> v) { return prnt(out, v); } template <class T1, class T2> ostream& operator<<(ostream& out, map<T1, T2> v) { return prnt(out, v); } template <class T1, class T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { return out << '(' << p.first << ' ' << p.second << ')'; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << " " << H; debug_out(T...); } int n, b, g, ans, i; int main() { ios_base::sync_with_stdio(false); cin >> b >> g >> n; if (b + g == n) return cout << 1, 0; cout << min(min(b, g) + 1 - max(n - max(b, g), 0), n + 1); }
2
#include <bits/stdc++.h> using namespace std; int n; bool h[102][102]; int main() { cin >> n; for (int i = 1; i <= n; ++i) { int m; cin >> m; for (int j = 1; j <= m; ++j) { int x; cin >> x; h[i][x] = true; } } for (int i = 1; i <= n; ++i) { bool flag = false; for (int j = 1; j <= n; ++j) if (j != i) { bool t = true; for (int k = 1; k <= 100; ++k) if (h[j][k]) if (h[i][k] == false) { t = false; break; } if (t) { flag = true; break; } } if (flag) cout << "NO" << endl; else cout << "YES" << endl; } return 0; }
2
#include <bits/stdc++.h> #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--) #define REP(i,n) for(int i=0;i<(n);i++) #define RREP(i,n) for(int i=n-1;i>=0;i--) #define PB push_back #define INF (1<<29) #define ALL(a) (a).begin(),(a).end() #define RALL(a) (a).rbegin(),(a).rend() #define CLR(a) memset(a,0,sizeof(a)) const int dx[] = {-1,0,0,1},dy[] = {0,1,-1,0}; typedef long long int ll; using namespace std; int n,k; vector<int> b[10]; int d[2001]; int main(){ cin >> n >> k; memset(d,0,sizeof(d)); REP(i,10){ b[i].PB(0); } REP(i,n){ int c,g; cin >> c >> g; b[g-1].PB(c); } REP(i,10){ sort(b[i].begin()+1,b[i].end(),greater<int>()); if(b[i].size() <= 0) continue; REP(j,b[i].size()-1){ b[i][j+1] += b[i][j]; } REP(j,b[i].size()){ b[i][j] += (j-1)*j; } } d[0] = 0; REP(i,10){ RREP(j,n+1){ if(d[j] < 0) continue; int s = min(n-j,(int)b[i].size() - 1); for(int l=s;l >= 0;l--){ d[j+l] = max(d[j+l],d[j]+b[i][l]); } } } int ans = 0; cout << d[k] << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; #define int long long #define endl '\n' #define MOD 1000000007 #define maxn 100010 void solve() { int n, s; cin >> n >> s; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; int sum = 0, ans = n + 10; for (int j = 0, i = 0; i < n and j < n; i++) { sum += v[i]; while (j < n and sum - v[j] >= s and j < i) { sum -= v[j]; j++; } if (sum >= s and (ans > i - j + 1)) ans = min(ans, i - j + 1); } if (ans == n + 10) ans = 0; cout << ans << endl; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int t = 1; // cin >> t; while (t--) solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long MAX = 2500000000; const int inf = 123456789; const double EPS = 1e-9; const double PI = 2 * asin(1.0); const long long mod = 1e9 + 7; int double_cmp(double x, double y = 0, double tol = EPS) { return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1; } int main() { int d, l; scanf("%d%d", &d, &l); int x, y; scanf("%d%d", &x, &y); double res = (l - d) / (double)(x + y); printf("%.8lf\n", res); }
1
#include <bits/stdc++.h> using namespace std; const int N = 7005; long long mask[N], b[N], ans = 0; bool good[N]; map<long long, int> h; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> mask[i]; h[mask[i]]++; } for (int i = 1; i <= n; i++) { cin >> b[i]; } for (int i = 1; i <= n; i++) { if (h[mask[i]] >= 2) { for (int j = 1; j <= n; j++) { if ((mask[i] | mask[j]) == mask[i]) { good[j] = true; } } } } for (int i = 1; i <= n; i++) { if (good[i]) ans += b[i]; } cout << ans << '\n'; return 0; }
4
#include <iostream> #include <stdio.h> #include <fstream> #include <algorithm> #include <string> #include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <limits.h> #include <math.h> #include <functional> #include <bitset> #define repeat(i,n) for (long long i = 0; (i) < (n); ++ (i)) #define debug(x) cerr << #x << ": " << x << '\n' #define debugArray(x,n) for(long long i = 0; (i) < (n); ++ (i)) cerr << #x << "[" << i << "]: " << x[i] << '\n' #define debugArrayP(x,n) for(long long i = 0; (i) < (n); ++ (i)) cerr << #x << "[" << i << "]: " << x[i].first<< " " << x[i].second << '\n' using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> Pii; typedef vector<int> vint; typedef vector<ll> vll; const ull INF = ULLONG_MAX; const ll MOD = 998244353; typedef ll Weight; struct Edge { int src, dst; Weight weight; Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) { } }; bool operator < (const Edge &e, const Edge &f) { return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!! e.src != f.src ? e.src < f.src : e.dst < f.dst; } typedef vector<Edge> Edges; typedef vector<Edges> Graph; pair<Weight, Edges> Prim(const Graph &g, int r = 0) { int n = g.size(); Edges T; Weight total = 0; vector<bool> visited(n); priority_queue<Edge> Q; Q.push(Edge(-1, r, 0)); while (!Q.empty()) { Edge e = Q.top(); Q.pop(); if (visited[e.dst]) continue; if(e.src!=-1)T.push_back(e); total += e.weight; visited[e.dst] = true; for (__typeof(g[e.dst].begin()) f = g[e.dst].begin(); f != g[e.dst].end(); f++) if (!visited[f->dst]) Q.push(*f); } return pair<Weight, Edges>(total, T); } struct HLDecomposition{ int V; vector<vint> g; vint dep,par,head,size,inv; vint in,out; int t; HLDecomposition(int size_) :V(size_),g(V),dep(V,0),par(V,-1),head(V),size(V),inv(V),in(V),out(V),t(0){} void add_edge(int u,int v){ g[u].push_back(v); g[v].push_back(u); } void dfs_size(int v=0){ size[v]=1; for(int& u:g[v]){ if(par[u]>=0)continue; par[u]=v; dfs_size(u); size[v]+=size[u]; if(size[u]>size[g[v][0]]){ swap(u,g[v][0]); } } } void dfs_hld(int v=0){ in[v] = t++; inv[in[v]]=v; for(int& u:g[v]){ if(par[u]!=v)continue; head[u]=(u==g[v][0]?head[v]:u); dfs_hld(u); } out[v]=t; } void build(int root=0){ par[root]=0; dfs_size(root); par[root]=-1; dfs_hld(root); } int lca(int u,int v){ while(1){ if(in[u]>in[v])swap(u,v); if(head[u]==head[v])return u; v=par[head[v]]; } } int distance(int u,int v){ return dep[u]+dep[v]-2*dep[lca(u,v)]; } }; inline int get_min2pow(int n) { int res = 1; while (res < n) res *= 2; return res; } ///Segment Tree//////// template<typename T,T dval> struct segtree { int N; vector<T> node; //例外値 ex)INF,0 T default_value = dval; static inline T merge(const T& l, const T& r) { //RMQ return max(l,r); //RSQ //return l+r; } segtree(int n) { N = get_min2pow(n); node.resize(2 * N, default_value); } segtree(vector<int> v) { int sz = v.size(); N = get_min2pow(sz); node.resize(2 * N, default_value); for (int i = 0; i < sz; i++) node[i + N - 1] = v[i]; for (int i = N - 2; i >= 0; i--) node[i] = merge(node[2 * i + 1], node[2 * i + 2]); } // update k th element void update(int k, T val) { k += N - 1; // leaf node[k] = val; while (k > 0) { k = (k - 1) / 2; node[k] = merge(node[k * 2 + 1], node[k * 2 + 2]); } } // [a, b) T query(int a, int b) { return query(a, b, 0, 0, N); } T query(int a, int b, int k, int l, int r) { if (r <= a or b <= l) return default_value; if (a <= l and r <= b) return node[k]; int m = (l + r) / 2; T vl = query(a, b, k * 2 + 1, l, m); T vr = query(a, b, k * 2 + 2, m, r); return merge(vl, vr); } }; int main(){ int N,M;cin>>N>>M; Graph g(N); repeat(i,M){ int a,b;Weight c; cin>>a>>b>>c; a--;b--; g[a].push_back({a,b,c}); g[b].push_back({b,a,c}); } Weight total; Edges tree; tie(total,tree)=Prim(g); HLDecomposition hld(N); repeat(i,tree.size()){ hld.add_edge(tree[i].src,tree[i].dst); } hld.build(); segtree<ll,0> RMQ(N); repeat(i,tree.size()){ if(hld.in[tree[i].src]>hld.in[tree[i].dst])swap(tree[i].src,tree[i].dst); RMQ.update(hld.in[tree[i].dst],tree[i].weight); } int Q;cin>>Q; repeat(q,Q){ int s,t;cin>>s>>t;s--;t--; ll mx=0; while(1){ if(hld.in[s]>hld.in[t])swap(s,t); if(hld.head[s]!=hld.head[t]){ mx = max(mx,RMQ.query(hld.in[hld.head[t]],hld.in[t]+1)); t=hld.par[hld.head[t]]; }else{ if(s!=t)mx = max(mx,RMQ.query(hld.in[s]+1,hld.in[t]+1)); break; } } cout << total-mx<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k; int a[110][110]; scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { scanf("%d", &a[i][j]); } } int stop[110], qian[110], rep[110], ans[110]; memset(stop, 0, sizeof(stop)); memset(ans, 0, sizeof(ans)); for (int j = 0; j < m; j++) { memset(rep, 0, sizeof(rep)); memset(qian, 0, sizeof(qian)); for (int i = 0; i < n; i++) { if (ans[i] == 0 && a[i][j]) { if (stop[a[i][j]]) { ans[i] = j + 1; } else if (rep[a[i][j]] == 0) rep[a[i][j]] = 1; else { stop[a[i][j]] = 1; ans[i] = j + 1; ans[qian[a[i][j]]] = j + 1; } qian[a[i][j]] = i; } } } for (int i = 0; i < n; i++) printf("%d\n", ans[i]); return 0; }
2
#include <bits/stdc++.h> using namespace std; double dist(vector<vector<int> >& v, int i1, int i2) { return sqrt(double(((v[i1][0] - v[i2][0]) * (v[i1][0] - v[i2][0])) + ((v[i1][1] - v[i2][1]) * (v[i1][1] - v[i2][1])) + ((v[i1][2] - v[i2][2]) * (v[i1][2] - v[i2][2])))); } int main() { int n; cin >> n; vector<vector<int> > v(n, vector<int>(3)); for (int i = 0; i < n; ++i) { cin >> v[i][0] >> v[i][1] >> v[i][2]; } double result = 1e100; for (int i = 1; i < n; ++i) { for (int j = 1; j < i; ++j) { result = min(result, (dist(v, 0, i) + dist(v, 0, j) + dist(v, i, j)) / 2.0); } } cout.precision(9); cout << fixed << result << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int INV(int k) { int ans = 0; while (k) { ans = ans * 10 + k % 10; k /= 10; } return ans; } int main() { int k, p; long long sum = 0; cin >> k >> p; int len = 0, base = 1; for (int i = 1; i <= k; ++i) { if (i % base == 0) base *= 10, ++len; sum += (1ll * i * base + INV(i)) % p; sum %= p; } cout << sum << endl; return 0; }
2
#include <bits/stdc++.h> template <class T1, class T2, class Pred = std::less<T2> > struct sort_pair_second { bool operator()(const std::pair<T1, T2> &left, const std::pair<T1, T2> &right) { Pred p; return p(left.second, right.second); } }; using namespace std; const int md = 1000000007; inline void add(int &a, int b) { a += b; if (a >= md) { a -= md; } } inline int mul(int a, int b) { return (long long)a * b % md; } const int N = 500010; char s[N], t[N]; int f[N], value[N]; bool into[N]; int p[N]; int jump[N]; int main() { scanf("%s", s + 1); scanf("%s", t + 1); int n = strlen(s + 1); int m = strlen(t + 1); int k = 0; p[1] = 0; for (int i = 2; i <= m; i++) { while (k > 0 && t[i] != t[k + 1]) k = p[k]; if (t[i] == t[k + 1]) k++; p[i] = k; } k = 0; for (int i = 1; i <= n; i++) { while (k > 0 && s[i] != t[k + 1]) k = p[k]; if (s[i] == t[k + 1]) k++; into[i] = (k == m); } jump[n] = n + 1; for (int i = n - 1; i >= 0; i--) { jump[i] = jump[i + 1]; if (i + m <= n && into[i + m]) { jump[i] = i + m; } } for (int i = 0; i <= n; i++) { value[i] = 0; f[i] = 0; } f[0] = 1; int sum = 0; for (int i = 0; i <= n; i++) { add(sum, value[i]); add(f[i], sum); add(f[i + 1], f[i]); add(value[jump[i]], f[i]); } add(f[n], md - 1); printf("%d\n", f[n]); return 0; }
4
#include <bits/stdc++.h> using namespace std; int dp[2][1005][55][55], a[1005], b[1005]; int mx(int x) { return max(0, x); }; int main() { int n, p, k; scanf("%d %d %d", &n, &p, &k); if (p > 2 * (n + k - 1) / k) { p = 2 * (n + k - 1) / k; } int z1; scanf("%d", &z1); for (int i = 1; i <= z1; i++) { int x; scanf("%d", &x); a[x] = 1; } int z2; scanf("%d", &z2); for (int i = 1; i <= z2; i++) { int x; scanf("%d", &x); b[x] = 1; } memset(dp, -0x3f, sizeof(dp)); dp[0][0][0][0] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= p; j++) { for (int x = 0; x < k; x++) { for (int y = 0; y < k; y++) { dp[i % 2][j + 1][k - 1][mx(y - 1)] = max(dp[i % 2][j + 1][k - 1][mx(y - 1)], dp[(i + 1) % 2][j][x][y] + (a[i] || (y && b[i]))); dp[i % 2][j + 1][mx(x - 1)][k - 1] = max(dp[i % 2][j + 1][mx(x - 1)][k - 1], dp[(i + 1) % 2][j][x][y] + (b[i] || (x && a[i]))); dp[i % 2][j][mx(x - 1)][mx(y - 1)] = max(dp[i % 2][j][mx(x - 1)][mx(y - 1)], dp[(i + 1) % 2][j][x][y] + ((a[i] && x) || (b[i] && y))); } } } memset(dp[(i + 1) % 2], -0x3f, sizeof(dp[(i + 1) % 2])); } int res = 0; for (int j = 0; j <= p; j++) { for (int x = 0; x < k; x++) { for (int y = 0; y < k; y++) { res = max(res, dp[n % 2][j][x][y]); } } } printf("%d\n", res); }
5
#include <bits/stdc++.h> using namespace std; int main() { long long n, a, t, m, x, y, z, x1, y1, z1, k; cin >> t; for (int i = 0; i < t; i++) { cin >> m >> n; if (m == n) { if (m % 2 == 0) cout << m << endl; else { m *= -1; cout << m << endl; } } else { m--; k = (n * (n + 1)) / 2; y = ((n + 1) / 2) * ((n + 1) / 2); z = k - y; k = (m * (m + 1)) / 2; y1 = ((m + 1) / 2) * ((m + 1) / 2); z1 = k - y1; z1 *= -1; y *= -1; x = z + z1; x1 = y + y1; cout << x + x1 << endl; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; struct node { long long t, w; } a[500001]; priority_queue<long long, vector<long long>, greater<long long> > q; long long i, j, n, m, k, l, ans = 1 << 30; inline long long read() { char c = getchar(); bool f = 0; long long x = 0; while (!isdigit(c)) f |= c == '-', c = getchar(); while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar(); if (f) x = -x; return x; } bool cmp(node x, node y) { if (x.t == y.t) return x.w < y.w; return x.t > y.t; } signed main() { n = read(); for (long long i = 1; i <= n; i++) a[i].t = read(), a[i].w = read(); sort(a + 2, a + 1 + n, cmp); k = 2; while (1) { while (k <= n && a[k].t > a[1].t) q.push(a[k].w - a[k].t + 1), k++; ans = min(ans, (long long)q.size() + 1); if (q.empty() || a[1].t < q.top()) break; a[1].t -= q.top(); q.pop(); } cout << ans << endl; }
4
#include <iostream> #include <map> #include <string> using namespace std; #define REP(i,n) for (int i=0; i<n; i++) int main(void){ map <char, int> m; int num[] = {1,5,10,50,100,500,1000}; REP(i,7) m["IVXLCDM"[i]] = num[i]; char c; string r; while (cin>>r){ int tmp, tmp2= m[r[0]], s=0; REP(i,r.size()){ tmp = m[r[i]]; if (tmp2<tmp) s-= tmp2*2; s += tmp; tmp2 = tmp; } cout<<s<<endl; } }
0
#include <bits/stdc++.h> using namespace std; int ans[1000001]; int main() { int i, n, q, t, x, odd, even; scanf("%d%d", &n, &q); odd = even = 0; while (q--) { scanf("%d", &t); if (t == 1) { scanf("%d", &x); odd = (odd + x + n) % n; even = (even + x + n) % n; } else { if (odd % 2) { odd = (odd + n - 1) % n; even = (even + 1) % n; } else { odd = (odd + 1) % n; even = (even + n - 1) % n; } } } for (i = 1; i < n + 1; i++) { if (i % 2) ans[(i + odd - 1 + n) % n + 1] = i; else ans[(i + even - 1 + n) % n + 1] = i; } for (i = 1; i < n + 1; i++) printf("%d ", ans[i]); printf("\n"); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { string s, k; int flag = 0, cnt = 0; for (int i = 1; i <= 8; i++) { cin >> s; for (int j = 1; j <= s.size(); j++) { if (s[j] == 'B' && s[j + 1] == 'B') { cnt++; } if (s[j] == 'W' && s[j + 1] == 'W') { cnt++; } } } if (cnt == 0) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
1
#include <cstdio> #include <cstdlib> #include <cmath> #include <cstring> #include <map> #include <utility> #include <set> #include <iostream> #include <string> #include <vector> #include <algorithm> #include <sstream> #include <complex> #include <stack> #include <queue> using namespace std; typedef long long LL; typedef pair<int, int> PII; typedef complex<double> P; static const double EPS = 1e-8; #define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i) #define REP(i,n) FOR(i,0,n) int main(void){ int n; cin>>n; while(n--){ string snake; cin>>snake; if(snake[0]=='>'&&snake[1]=='\''&&snake[snake.size()-1]=='~'){ int count=0; int end; for(int i=2;i<snake.size()&&snake[i]=='='; i++){ count++; end = i; } end++; if(count&&snake[end]=='#'&&end+count+2==snake.size()){ bool yes = true; for(int i=end+1;i<snake.size()-1;i++){ if(snake[i]!='=')yes = false; } if(yes){ cout<<"A"<<endl; }else{ cout<<"NA"<<endl; } }else{ cout<<"NA"<<endl; } }else if(snake[0]=='>'&&snake[1]=='^'&&snake[snake.size()-1]=='~'&&snake[snake.size()-2]=='~' &&snake[snake.size()-3]=='='){ bool yes = true; for(int i=2;i<snake.size()-2;i++){ if(i%2==0&&snake[i]!='Q') yes = false; if(i%2==1&&snake[i]!='=') yes = false; } if(yes){ cout<<"B"<<endl; }else{ cout<<"NA"<<endl; } }else{ cout<<"NA"<<endl; } } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long power(long long x, long long y) { long long res = 1; while (y > 0) { if (y & 1) res = res * x; y = y >> 1; x = x * x; } return res; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long tcount; tcount = 1; while (tcount--) { long long n, s; cin >> n >> s; if (n == 1) { if (s > 1) { cout << "YES" << endl; cout << s << endl; cout << s - 1 << endl; } else { cout << "NO" << endl; } continue; } if (n <= 2 * n - s) { cout << "NO" << endl; continue; } long long mx = s - (n - 1); long long mi = s / n; if (s >= 2 * n) { cout << "YES" << endl; for (long long i = 0; i < n - 1; i++) cout << 2 << " "; cout << s - (2 * (n - 1)) << endl; cout << 1 << endl; continue; } long long sl = s - n; if (sl > n / 2 + n % 2) { cout << "NO" << endl; continue; } cout << "NO" << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int head[5005 * 5005 / 2], nxt[5005 * 5005 / 2], lst = 1; pair<int, int> to[5005 * 5005 / 2]; int arr[5005 + 100]; int cnt = 0; int n, k; void add_edge(int f, pair<int, int> p) { nxt[lst] = head[f]; head[f] = lst; to[lst++] = p; } struct disjoint_set { int par[5005 + 100], in[5005 + 100], lst; bool vis[5005 + 100]; disjoint_set() { for (int i = 0; i <= 5005; i++) par[i] = i; lst = 0; } void reset() { for (int i = 0; i < lst; i++) par[in[i]] = in[i], vis[in[i]] = 0; lst = 0; } int find_par(int cur) { if (cur != par[cur]) return par[cur] = find_par(par[cur]); return par[cur]; } int join(int x, int y) { x = find_par(x), y = find_par(y); if (x == y) return 0; par[y] = x; if (!vis[y]) in[lst++] = y, vis[y] = 1; return 1; } } dsu; void check(int d) { for (int i = head[d]; i; i = nxt[i]) { cnt += dsu.join(to[i].first, to[i].second); if (cnt > k) return; } } int main() { scanf("%d %d", &n, &k); if (k + 1 >= n) { printf("1\n"); return 0; } int mx = 0; for (int i = 0; i < n; i++) scanf("%d", &arr[i]); sort(arr, arr + n, greater<int>()); mx = arr[0]; for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) add_edge(arr[i] - arr[j], make_pair(i, j)); for (int i = 2; i <= mx + 1; i++) { cnt = 0; dsu.reset(); for (int j = i; j <= mx + 1; j += i) { check(j); if (cnt > k) break; } if (cnt <= k) { printf("%d\n", i); return 0; } } }
5
#include <bits/stdc++.h> using namespace std; const double EPS = 1e-8; const double Pi = acos(-1); bool inline equ(double a, double b) { return fabs(a - b) < EPS; } int _R(int &x) { return scanf("%d", &x); } int _R(long long &x) { return scanf("%" PRId64, &x); } int _R(double &x) { return scanf("%lf", &x); } int _R(char *s) { return scanf("%s", s); } int _R(char c) { return scanf("%c", &c); } int R() { return 0; } template <typename T, typename... U> int R(T &x, U &...tail) { return _R(x) + R(tail...); } template <class T> void _W(const T &x) { cout << x; } void _W(const int &x) { printf("%d", x); } void _W(const int64_t &x) { printf("%" PRId64, 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> 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...); } const int N = 1e6 + 10; int n; long long a[N], tot; inline long long chk(long long p) { long long ls = 0, rs = 0; for (int i = (1); i <= (n); i++) rs += a[i]; long long res = 0; for (int i = (1); i <= (n - 1); i++) { ls += a[i]; rs -= a[i]; res += min(ls % p, rs % p); } return res; } int main() { R(n); for (int i = (1); i <= (n); i++) { R(a[i]); tot += a[i]; } if (tot == 1) { puts("-1"); return 0; } long long ans = 0; for (int i = (2); i <= (n); i++) { ans += (i - 1) * a[i]; } for (long long p = 2; p <= min(tot, (long long)N); ++p) if (tot % p == 0) { while (tot % p == 0) tot /= p; ans = min(ans, chk(p)); } if (tot > 1) { ans = min(ans, chk(tot)); } cout << ans << endl; }
5
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int f = 0; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 1; i < n; i++) { if (a[i] >= a[i - 1]) { f = 1; break; } } if (f) cout << "YES\n"; else cout << "NO\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n = 0; int x0 = 0; int a = 0; int b = 0; int m = 0; int M = 1e6; cin >> n >> x0; while (n--) { cin >> a >> b; if (a > b) { int tmp = a; a = b; b = tmp; } if (b < M) M = b; if (a > m) m = a; } if (m > M) { cout << -1 << endl; } else { if (x0 >= m && x0 <= M) { cout << 0 << endl; } else if (x0 < m) { cout << m - x0 << endl; } else if (x0 > M) { cout << x0 - M << endl; } } return 0; }
2
#include <bits/stdc++.h> int main() { int n, m, v, i, j, c; scanf("%d%d%d", &n, &m, &v); if ((n - 2) * (n - 1) / 2 + 1 < m) { printf("-1"); return 0; } if (m < n - 1) { printf("-1"); return 0; } for (i = 1; i <= n; i++) { if (i != v) printf("%d %d\n", v, i); } if (v == n) c = n - 1; else c = n; m -= (n - 1); if (m == 0) return 0; for (i = 1; i <= n; i++) for (j = i + 1; j <= n; j++) { if (i != v && j != v && i != c && j != c) { printf("%d %d\n", i, j); m--; } if (m == 0) return 0; } }
3
#include <bits/stdc++.h> using namespace std; const long long LINF = 1e18 + 7; const int N = 2e3 + 7; const int INF = 1e9 + 7; const int MOD = 1e9 + 7; const double PI = acos(-1.0); const double EPS = 1e-8; struct Node { string name; int total, scored, missed; Node() {} Node(string _n, int _t, int _s, int _m) { name = _n, total = _t, scored = _s, missed = _m; } bool operator<(const Node &p) const { if (total != p.total) return total > p.total; if (scored - missed != p.scored - p.missed) return scored - missed > p.scored - p.missed; if (scored != p.scored) return scored > p.scored; return name < p.name; } } a[4], b[4]; int cnt[4], goal1[5], goal2[5]; string team1[5], team2[5]; vector<string> S; bool check() { sort(a, a + 4); return a[0].name == "BERLAND" || a[1].name == "BERLAND"; } void gao(int id, int x, int y) { a[id].scored += x, a[id].missed += y; if (x > y) a[id].total += 3; else a[id].total += x == y; } void solve() { for (int i = (0); i < (4); ++i) b[i] = a[i]; int x = -1, y = -1; int id1, id2; id1 = lower_bound(S.begin(), S.end(), "BERLAND") - S.begin(); for (int i = (0); i < (4); ++i) if (cnt[i] == 2 && i != id1) { id2 = i; break; } for (int i = (1); i < (100); ++i) for (int j = (0); j < (i); ++j) { for (int k = (0); k < (4); ++k) a[k] = b[k]; gao(id1, i, j), gao(id2, j, i); if (check()) { if (x == -1 || (x - y > i - j) || (x - y == i - j && y > j)) { x = i, y = j; } } } if (~x) { printf("%d:%d", x, y); } else { puts("IMPOSSIBLE"); } } int main() { for (int i = (0); i < (5); ++i) { char ch; cin >> team1[i] >> team2[i] >> goal1[i] >> ch >> goal2[i]; S.push_back(team1[i]), S.push_back(team2[i]); } sort(S.begin(), S.end()); S.erase(unique(S.begin(), S.end()), S.end()); for (int i = (0); i < (4); ++i) a[i] = Node(S[i], 0, 0, 0); for (int i = (0); i < (5); ++i) { int id1 = lower_bound(S.begin(), S.end(), team1[i]) - S.begin(); int id2 = lower_bound(S.begin(), S.end(), team2[i]) - S.begin(); ++cnt[id1], ++cnt[id2]; gao(id1, goal1[i], goal2[i]), gao(id2, goal2[i], goal1[i]); } solve(); return 0; }
3
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m, dx, dy; cin >> n >> m >> dx >> dy; vector<int> who(n); { int cx = 0; int cy = 0; for (int i = 0; i < n; i++) { who[cx] = cy; cx = (cx + dx) % n; cy = (cy + dy) % n; } } vector<int> add(n); while (m--) { int x, y; cin >> x >> y; add[(y - who[x] + n) % n]++; } int pos = max_element(add.begin(), add.end()) - add.begin(); cout << 0 << ' ' << pos << '\n'; }
5
#include <bits/stdc++.h> using namespace std; template <class T> ostream& operator<<(ostream& os, vector<T> V) { for (auto v : V) os << v << " "; return cout << ""; } template <class T> ostream& operator<<(ostream& os, set<T> S) { for (auto s : S) os << s << " "; return cout << ""; } template <class L, class R> ostream& operator<<(ostream& os, pair<L, R> P) { return os << P.first << " " << P.second; } const long long mod = 998244353; long long inv(long long i) { if (i == 1) return 1; return (mod - ((mod / i) * inv(mod % i)) % mod) % mod; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long pwr(long long a, long long b) { a %= mod; long long res = 1; while (b > 0) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } long long lcm(long long f1, long long f2) { return (f1 * f2) / gcd(f1, f2); } long long i, j, n, m, f, t, k, d, x = 0, y = 0, z = 0, minn[300200], maxx[300200], asc[300200] = {0}, a[200200], b[200200]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); t = 1; while (t--) { cin >> n; if (n % 2 == 0) { cout << "white\n1 2"; return 0; } cout << "black"; } }
4
#include <bits/stdc++.h> using namespace std; inline bool EQ(double num1, double num2) { return fabs(num1 - num2) < 1e-9; } const int INF = 1 << 29; inline int two(int si) { return 1 << si; } inline int test(int si, int num2) { return (si >> num2) & 1; } inline void set_bit(int& si, int num2) { si |= two(num2); } inline void unset_bit(int& si, int num2) { si &= ~two(num2); } inline int last_bit(int si) { return si & (-si); } inline int ones(int si) { int resultAns = 0; while (si && ++resultAns) si -= si & (-si); return resultAns; } template <class T> void chmax(T& num1, const T& num2) { num1 = max(num1, num2); } template <class T> void chmin(T& num1, const T& num2) { num1 = min(num1, num2); } using namespace std; using namespace std; int n, m, aara[3000][3000], arrd[5002][5002]; string strr; int sizes; int main() { cin >> n >> m; getline(cin, strr); for (int i = 1; i <= n; i++) { getline(cin, strr); for (int j = 1; j <= m; j++) { aara[i][j] = strr[j - 1] - '0'; arrd[i][j] = arrd[i - 1][j] + arrd[i][j - 1] + aara[i][j] - arrd[i - 1][j - 1]; } } sizes = max(n, m) * 2 + 1; int resultAns = (1 << 30); for (int i = 1; i <= sizes; i++) for (int j = 1; j <= sizes; j++) if (i > n || j > m) arrd[i][j] = arrd[i - 1][j] + arrd[i][j - 1] - arrd[i - 1][j - 1]; for (int num3 = 2; num3 <= n; num3++) { int na = n / num3; if (n % num3) na++; int num5 = m / num3; if (m % num3) num5++; int present = 0; for (int i = 1; i <= na; i++) for (int j = 1; j <= num5; j++) { int rightJindex = j * num3; int listindex = (i - 1) * num3 + 1; int rightIndex = (i)*num3; int leftJindex = (j - 1) * num3 + 1; int ghnna = (rightIndex - listindex + 1) * (rightJindex - leftJindex + 1); int sum = arrd[rightIndex][rightJindex] - arrd[listindex - 1][rightJindex] - arrd[rightIndex][leftJindex - 1] + arrd[listindex - 1][leftJindex - 1]; present += min(sum, ghnna - sum); } resultAns = min(resultAns, present); } cout << resultAns; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long int i, k, l, j, m, n, rem, quo, w, flag = 0; cin >> w >> m; quo = m; while (quo != 0) { rem = quo % w; quo = quo / w; if ((rem != 0) && (rem != 1)) { if ((rem - w) == -1) { rem = -1; quo = quo + 1; } else { flag = 1; break; } } } if (flag == 1) cout << "NO\n"; else cout << "YES\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, k, f[30010][210][5]; const int inf = 0x3f3f3f3f; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= k; i++) { for (int j = 0; j <= 3; j++) { f[0][i][j] = -inf; } } for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); for (int j = 1; j <= k; j++) { int flag = 1; if (j != 1 && j != k) flag++; f[i][j][0] = max(f[i - 1][j][0], f[i - 1][j - 1][3]) - flag * x; f[i][j][1] = max(f[i - 1][j][1], f[i][j][0]); f[i][j][2] = max(f[i - 1][j][2], f[i - 1][j - 1][1]) + flag * x; f[i][j][3] = max(f[i - 1][j][3], f[i][j][2]); if (flag == 2) { f[i][j][1] = max(f[i][j][1], f[i - 1][j - 1][1]); f[i][j][3] = max(f[i][j][3], f[i - 1][j - 1][3]); } } } printf("%d", max(f[n][k][1], f[n][k][3])); return 0; }
5
#include <bits/stdc++.h> int main() { int i = 0, j, t, n; char p[100]; scanf("%s", p); n = strlen(p); for (; i < n - 1; i = i + 2) { for (j = 0; j < n - 1; j = j + 2) { if (p[j] > p[j + 2]) { t = p[j]; p[j] = p[j + 2]; p[j + 2] = t; } } } printf("%s\n", p); }
1
#include <bits/stdc++.h> using namespace std; const double PI = 3.1415926535; long long powi(long long base, int p) { long long res = 1; while (p--) res *= base; return res; } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } bool is_vowel(char c) { c = tolower(c); return c == 'a' || c == 'e' || c == 'o' || c == 'i' || c == 'u' || c == 'y'; } bool gen(int s, int p) { int i = (s / 50) % 475; for (int t = 0; t < 25; t++) { i = (i * 96 + 42) % 475; if (26 + i == p) return true; } return false; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int p, x, y; cin >> p >> x >> y; int res = 0; int neg = x; while (neg >= y && !gen(neg, p)) neg -= 50; if (neg >= y) { cout << 0; return 0; } while (!gen(x, p)) { res++; x += 100; if (gen(x - 50, p)) break; } cout << res; return 0; }
2
#include <bits/stdc++.h> using namespace std; struct Fight { int x; int y; }; Fight a[10001]; int main() { bool f = false, f1 = false; int n, x1, y1, k = 0, x2, y2; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i].x >> a[i].y; } if (n == 1 || n == 2) { cout << "0"; return 0; } if (n == 3) { x2 = a[0].x; y2 = a[0].y; x1 = a[1].x; y1 = a[1].y; if ((x2 - x1) * (a[2].y - y1) == (y2 - y1) * (a[2].x - x1)) { cout << "0"; return 0; } cout << "1"; return 0; } for (int i = 0; i < n; i++) { x2 = a[i].x; y2 = a[i].y; for (int j = i + 1; j < n; j++) { x1 = a[j].x; y1 = a[j].y; for (int o = j + 1; o < n; o++) { if ((x2 - x1) * (a[o].y - y1) != (y2 - y1) * (a[o].x - x1)) { k++; } } } } cout << k; return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long MXN = 1e6 + 1; const long long MOD = 1e9 + 7; const long long INF = 1e18; const long double EPS = 1e-9; int64_t n; int64_t cnt; int64_t res; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n; int64_t lol = (n + 1) / 2; while (n--) { int64_t x; cin >> x; if (x > 0) cnt++; if (x < 0) res++; } if (res >= lol) { cout << -1; } else if (cnt >= lol) { cout << 1; } else { cout << 0; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main(int argc, char **argv) { long long n, k; long long res = 0; cin >> n >> k; if (n == 1) res = 0; else if (n <= k) res = 1; else if (n > k * (k + 1) / 2 - 1 - (k - 1 - 1)) res = -1; else if (n == k * (k + 1) / 2 - 1 - (k - 1 - 1)) res = k - 1; else { long long l = 2, r = k; long long mid; long long t; while (r - l > 1) { mid = l + (r - l) / 2; t = (mid + k) * (k - mid + 1) / 2 - (k - mid); if (t > n) l = mid; else r = mid; } t = (r + k) * (k - r + 1) / 2 - (k - r); if (t == n) res = k - r + 1; else res = k - r + 1 + 1; } cout << res << endl; return 0; }
2
#include <bits/stdc++.h> int b[100], _A[100], n; long long Ans, AnsA, AnsB, AnsC, a[100], c[100][100], LastA, V, V2; void DfsB(int l, long long B) { if (!(V2 / B / B)) return; if (l == n) { long long C = V2 / B; if ((V / LastA + V / B + V / C) < Ans) { Ans = V / LastA + V / B + V / C; AnsA = LastA; AnsB = B; AnsC = C; } return; } for (int i = b[l] - _A[l]; i >= 0; i--) DfsB(l + 1, B * c[l][i]); } void DfsA(int l, long long A) { if (!(V / A / A / A)) return; if (l == n) { long long B = (long long)sqrt(V / A), C = (V / A + B - 1) / B; if ((V / A + V / B + V / C) >= Ans) return; LastA = A; V2 = V / LastA; DfsB(0, 1); return; } for (_A[l] = b[l]; _A[l] >= 0; _A[l]--) DfsA(l + 1, A * c[l][_A[l]]); } int main() { int i, j, TestCase; for (scanf("%d", &TestCase); TestCase--;) { scanf("%d", &n); V = 1; for (i = n - 1; i >= 0; i--) { scanf("%I64d%d", a + i, b + i); c[i][0] = 1; for (j = 0; j < b[i]; j++) { c[i][j + 1] = c[i][j] * a[i]; V *= a[i]; } } Ans = 2 * V + 2; DfsA(0, 1); printf("%I64d %I64d %I64d %I64d\n", 2 * Ans, AnsA, AnsB, AnsC); } return 0; }
4