func_code_string
stringlengths 59
71.4k
|
---|
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, r; cin >> n >> r; vector<double> a(1 << n); double sum = 0, div = (double)(1 << n); for (int i = 0; i < (1 << n); i++) { int x; cin >> x; a[i] = (double)x; sum += a[i]; } double ans = sum / div; cout << setprecision(7) << fixed << ans << n ; while (r--) { int z, g; cin >> z >> g; double gg = (double)g; sum += (gg - a[z]); a[z] = gg; ans = sum / div; cout << setprecision(7) << fixed << ans << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) { res = (res * x) % p; } y = y >> 1; x = (x * x) % p; } return res; } long long modInverse(long long a, long long p) { return power(a, p - 2, p); } long long gcd(long long x, long long y) { if (x == 0 || y == 0) { return max(y, x); } return gcd(y % x, x); } long long gcdExtended(long long a, long long b, long long &x, long long &y) { if (a == 0) { x = 0; y = 1; return b; } long long x1, y1; long long gcd = gcdExtended(b % a, a, x1, y1); x = y1 - (b / a) * x1; y = x1; return gcd; }; long long bit[1504]; void update(long long x) { while (x <= 1500) { bit[x]++; x += (x & (-x)); } } long long get(long long x) { long long ret = 0; while (x) { ret += bit[x]; x -= (x & (-x)); } return ret; } long long dp[1504][1504]; int main() { long long n; scanf( %lld , &n); vector<long long> a; for (long long i = 0; i < n; i++) { long long x; scanf( %lld , &x); a.push_back(x); } for (long long i = 0; i < n; i++) { for (long long j = 1; j <= n; j++) bit[j] = 0; long long here = 0; for (long long j = i; j < n; j++) { update(a[j]); here += get(a[j] - 1); dp[i][j] = here; } } long long q; scanf( %lld , &q); long long tot = dp[0][n - 1]; tot = (n) * (n - 1); tot /= 2; tot -= dp[0][n - 1]; tot %= 2; while (q--) { long long x, y; scanf( %lld %lld , &(x), &(y)); long long len = y - x + 1; long long here = (len) * (len - 1); here /= 2; here %= 2; tot = tot + here; tot %= 2; if (tot) cout << odd << endl; else cout << even << endl; } }
|
#include <bits/stdc++.h> using namespace std; const int M = 111111; int Tit[M]; int ra[M]; int k[M]; int r, j, rslt = 0; vector<int> vec[M]; void dfs(int u, int v) { ra[u] = ra[v] + 1; k[ra[u]] ^= 1; int j = vec[u].size(); int z; while (j--) { z = vec[u][j]; if (z != v) dfs(z, u); } } int main() { scanf( %d , &r); for (j = 2; j <= r; j = j + 1) scanf( %d , Tit + j), vec[Tit[j]].push_back(j); dfs(1, 0); for (j = 1; j <= r; j = j + 1) rslt += k[j]; cout << rslt; return 0; }
|
#include <bits/stdc++.h> typedef long long ll; #define endl n #define pq priority_queue <pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> #define graph1 vector<vector<pair<ll,ll>>> #define graph vector<vector<ll>> #define pq1 priority_queue<pair<ll, pair<ll, ll>>, vector<pair<ll, pair<ll,ll>>>, greater<pair<ll, pair<ll, ll>>>> #define loop(i,a,b) for (ll i = a; i < b; i++) using namespace std; vector <ll> arr; bool check3(ll i) { if(max(arr[i],arr[i+2]) >= arr[i+1] && min(arr[i],arr[i+2]) <= arr[i+1] ) { return true; } return false; } bool check4(ll i) { if(check3(i) ||check3(i + 1)) { return true; } if(max(arr[i],arr[i+3]) >= arr[i + 1] && min(arr[i],arr[i+3]) <= arr[i+1] ) { return true; } if(max(arr[i],arr[i+3]) >= arr[i + 2] && min(arr[i],arr[i+3]) <= arr[i+2] ) { return true; } return false; } int main() { ios::sync_with_stdio(0); cin.tie(0); ll t; cin >> t; while(t--) { ll n; cin >> n; arr.resize(n); loop(i,0,n) cin >> arr[i]; ll ans = n * 2 - 1; loop(i,0,n-2) { if(!check3(i)) ans++; } loop(i,0,n-3) { if(!check4(i)) ans++; } cout << ans << endl; } }
|
#include <bits/stdc++.h> using namespace std; using vi = vector<int>; using vvi = vector<vi>; using ll = long long; using vll = vector<ll>; using vvll = vector<vll>; using pii = pair<int, int>; using pll = pair<ll, ll>; ll power[40]; void dfs(int id, vi& v, vi& s) { if (id < v.size()) { s.push_back(v[id]); dfs(id * 2, v, s); dfs(id * 2 + 1, v, s); } } void solve() { int h, g, num; cin >> h >> g; vi v(power[h]); vvi mins(power[g], vi()); set<int> fin; for (int i = 1; i < power[h]; i++) { cin >> v[i]; } for (int i = 0; i < power[g - 1]; i++) { dfs(i + power[g - 1], v, mins[i + power[g - 1]]); sort(mins[i + power[g - 1]].begin(), mins[i + power[g - 1]].end(), greater<int>()); } for (int i = g; i > 0; i--) { for (int j = power[i - 1]; j < power[i]; j++) { fin.insert(*mins[j].rbegin()); } if (i == 1) { break; } for (int j = power[i - 1]; j < power[i]; j += 2) { num = max(*mins[j].rbegin(), *mins[j + 1].rbegin()); mins[j].pop_back(); mins[j + 1].pop_back(); mins[j / 2].push_back(v[j / 2]); merge(mins[j].begin(), mins[j].end(), mins[j + 1].begin(), mins[j + 1].end(), std::back_inserter(mins[j / 2]), greater<int>()); while (*mins[j / 2].rbegin() < num) { mins[j / 2].pop_back(); } } } ll sum = 0; vi print; print.reserve(power[h] - 1); for (int i = 1; i < power[h]; i++) { if (fin.find(v[i]) == fin.end()) { print.push_back(i); } else { sum += v[i]; } } reverse(print.begin(), print.end()); cout << sum << n ; for (int i : print) { cout << i << ; } cout << n ; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); power[0] = 1; for (int i = 1; i < 40; i++) { power[i] = 2ll * power[i - 1]; } int t; cin >> t; while (t--) { solve(); } }
|
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; struct Point { double x, y; Point(double x = 0, double y = 0) : x(x), y(y) {} }; double Dot(Point A, Point B) { return A.x * B.x + A.y * B.y; } double Length(Point A) { return sqrt(Dot(A, A)); } double Angle(Point A, Point B) { return acos(Dot(A, B) / Length(A) / Length(B)); } Point Normal(Point A) { double L = Length(A); return Point(-A.y / L, A.x / L); } double Cross(Point A, Point B) { return A.x * B.y - A.y * B.x; } Point operator+(Point A, Point B) { return Point(A.x + B.x, A.y + B.y); } Point operator-(Point A, Point B) { return Point(A.x - B.x, A.y - B.y); } Point operator*(Point A, double p) { return Point(A.x * p, A.y * p); } Point operator/(Point A, double p) { return Point(A.x / p, A.y / p); } int dcmp(double x) { if (fabs(x) < eps) return 0; return x < 0 ? -1 : 1; } bool operator<(const Point &a, const Point &b) { return a.x < b.x || (a.x == b.x && a.y < b.y); } bool operator==(const Point &a, const Point &b) { return dcmp(a.x - b.x) == 0 && dcmp(a.y - b.y) == 0; } double GetAngleByLength(double a, double b, double c) { double cosA = (b * b + c * c - a * a) / (b * c * 2); return acos(cosA); } Point Rotate(Point A, double rad) { return Point(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad)); } Point GetLineIntersection(Point P, Point v, Point Q, Point w) { Point u = P - Q; double t = Cross(w, u) / Cross(v, w); return P + v * t; } Point R; Point v; double solve(Point P, Point vP, Point Q, Point vQ) { auto X = GetLineIntersection(P, vP, R, v); auto Y = GetLineIntersection(Q, vQ, R, v); return Length(P - X) + Length(X - Y) + Length(Q - Y); } int main() { double a, b, c, x1, y1, x2, y2; cin >> a >> b >> c >> x1 >> y1 >> x2 >> y2; if (a == 0 || b == 0) { double ans = fabs(x1 - x2) + fabs(y1 - y2); printf( %.10f n , ans); return 0; } R.x = 0; R.y = -c / b; v = Point(b, -a); double ans = fabs(x1 - x2) + fabs(y1 - y2); ans = min(ans, solve(Point(x1, y1), Point(1, 0), Point(x2, y2), Point(1, 0))); ans = min(ans, solve(Point(x1, y1), Point(1, 0), Point(x2, y2), Point(0, 1))); ans = min(ans, solve(Point(x1, y1), Point(0, 1), Point(x2, y2), Point(1, 0))); ans = min(ans, solve(Point(x1, y1), Point(0, 1), Point(x2, y2), Point(0, 1))); printf( %.10f n , ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, m, a[N], type, l, r; set<int> q[N]; inline int myrand() { return rand() ^ (rand() << 16); } struct item { item *l, *r; int key, prior, val; long long sum; item() {} item(int _key, int _val) { val = sum = _val; key = _key; prior = myrand(); l = r = nullptr; } }; typedef item *pitem; pitem t[4 * N]; inline long long sum(pitem v) { return v ? v->sum : 0; } inline void upd_cnt(pitem v) { if (v) { v->sum = sum(v->l) + v->val + sum(v->r); } } void Merge(pitem &v, pitem l, pitem r) { if (!l || !r) { v = l ? l : r; } else if (l->prior > r->prior) { Merge(l->r, l->r, r); v = l; } else { Merge(r->l, l, r->l); v = r; } upd_cnt(v); } void Split(pitem v, int key, pitem &l, pitem &r) { if (!v) { l = r = nullptr; return; } if (key < v->key) { Split(v->l, key, l, v->l); r = v; } else { Split(v->r, key, v->r, r); l = v; } upd_cnt(v); } void Insert(pitem &v, int key, int val) { pitem cur; Split(v, key, v, cur); Merge(v, v, new item(key, val)); Merge(v, v, cur); } void Erase(pitem &v, int key) { if (!v) { cout << SOSI ; exit(0); } if (v->key == key) { pitem sosi = v; Merge(v, v->l, v->r); delete sosi; return; } Erase(key < v->key ? v->l : v->r, key); upd_cnt(v); } void update(int pos, int key, int val, int add) { for (int i = pos; i <= n; i |= i + 1) { if (add == 1) { Insert(t[i], key, val); } else { Erase(t[i], key); } } } inline long long Get(pitem v, int l, int r) { pitem cur1, cur2; Split(v, l - 1, v, cur1); Split(cur1, r, cur1, cur2); long long ans = sum(cur1); Merge(v, v, cur1); Merge(v, v, cur2); return ans; } long long get(int pos, int tl, int tr) { long long ans = 0; for (int i = pos; i >= 1; i &= i + 1, i--) { ans += Get(t[i], tl, tr); } return ans; } int main() { srand(time(nullptr)); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) { q[i].insert(-1e9); t[i] = nullptr; } for (int i = 1; i <= n; i++) { cin >> a[i]; int prv = *(--q[a[i]].insert(i).first); update(i, prv, i - prv, +1); } for (int i = 1; i <= m; i++) { cin >> type >> l >> r; if (type == 1) { auto it = q[a[l]].find(l); int prv = *prev(it); it = next(it); update(l, prv, -1, -1); if (it != q[a[l]].end()) { int nxt = *it; update(nxt, l, -1, -1); update(nxt, prv, nxt - prv, +1); } q[a[l]].erase(l); a[l] = r; it = q[a[l]].insert(l).first; prv = *prev(it); it = next(it); if (it != q[a[l]].end()) { int nxt = *it; update(nxt, prv, -1, -1); update(nxt, l, nxt - l, +1); } update(l, prv, l - prv, +1); } else { cout << get(r, l, r) - get(l - 1, l, r) << n ; } } }
|
#include <bits/stdc++.h> using namespace std; #define ll long long int #define fast ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); #define scan(a,n) for(long long int index=0;index<n;index++) cin>>a[index]; #define print(a,n) for(long long int index=0;index<n;index++) cout<<a[index]<< ;cout<<endl; #define mod 1000000007 #define pb push_back #define mp make_pair #define ss second #define ff first #define vli vector<long long int> #define vlli vector<pair<long long int,long long int>> #define vsi vector<string> #define all(n) n.begin(),n.end() #define forn(i,a,b) for(i=a;i<b;i++) ll mul(ll x,ll y){ return (x*y)%mod;} ll power(ll x, ll y) {ll res = 1; x %= mod; while (y) {if (y & 1)res = mul(res, x); y >>= 1; x = mul(x, x);} return res;} ll mod_inv(ll x) {return power(x, mod - 2);} ll fact(ll n) {ll res = 1; for (ll i = 2; i <= n; i++) res = mul(res , i); return res; } ll nCr(ll n, ll r) { return mul(fact(n),mod_inv(mul(fact(r),fact(n - r))));} int main() { fast; #ifndef ONLINE_JUDGE freopen( input.txt , r ,stdin); freopen( output.txt , w ,stdout); #endif ll t=1; cin>>t; while(t--){ ll n,k; cin>>n>>k; ll a[n],b[n],i; for(i=0;i<n;i++){ cin>>a[i]>>b[i]; } ll f=-1,j; ll x,y; for(i=0;i<n;i++){ x=0; for(j=0;j<n;j++){ y=abs(a[i]-a[j])+abs(b[i]-b[j]); if(y<=k) x++; } if(x==n) f=1; } cout<<f<<endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int arr[n]; long long sum = 0; for (int i = 0; i < n; i++) cin >> arr[i]; vector<int> odd, even; for (int i = 0; i < n; i++) { if (arr[i] & 1) odd.push_back(arr[i]); else even.push_back(arr[i]); } sort(odd.begin(), odd.end()); sort(even.begin(), even.end()); if (odd.size() > even.size()) { for (int i = 0; i < odd.size() - even.size() - 1; i++) sum += odd[i]; } else if (odd.size() == even.size()) sum = 0; else { for (int i = 0; i < even.size() - odd.size() - 1; i++) sum += even[i]; } cout << sum; }
|
#include <bits/stdc++.h> using namespace std; using vi = vector<long long int>; using vvi = vector<vi>; using vb = vector<bool>; using vc = vector<char>; using vs = vector<string>; using vld = vector<long double>; using pii = pair<long long int, long long int>; using psi = pair<string, long long int>; using pci = pair<char, long long int>; using vpii = vector<pii>; long long int mod = 1e9 + 7; long long int const maxn = 2e5 + 5; long long int const inf = 1e18; long long int add(long long int a, long long int b) { return ((a % mod) + (b % mod)) % mod; } long long int mul(long long int a, long long int b) { return ((a % mod) * (b % mod)) % mod; } long long int powm(long long int x, long long int n, long long int M) { long long int result = 1; while (n > 0) { if (n % 2 == 1) result = (result * x) % M; x = (x * x) % M; n = n / 2; } return result; } long long int modinverse(long long int a, long long int m) { return powm(a, m - 2, m); } bool prime(long long int x) { if (x <= 1) return false; for (int i = 2; i <= sqrt(x); i++) if (x % i == 0) return false; return true; } long long int divisor(long long int x) { long long int cnt = 0; for (int i = 1; i <= sqrt(x); i++) { if (x % i == 0) { if (i != x / i) cnt += 2; else cnt += 1; } } return cnt; } vector<long long int> sieve(long long int n) { bool prim[n + 1]; memset(prim, true, sizeof(prim)); for (long long int p = 2; p * p <= n; p++) { if (prim[p] == true) { for (int i = p * p; i <= n; i += p) prim[i] = false; } } vector<long long int> v; for (int i = 2; i <= n; i++) if (prim[i]) v.push_back(i); return v; } vvi adj(maxn); vb vis(maxn, false); long long int x, y; void dfs(long long int u) { if (vis[u] == true) return; vis[u] = true; if (x == -1 and y == -1) { x = u; y = u; } x = min(x, u); y = max(y, u); for (auto v : adj[u]) { if (!vis[v]) { dfs(v); } } } int main() { int t; t = 1; while (t--) { long long int n, m; cin >> n >> m; vpii ans; for (long long int i = 0; i < m; i++) { long long int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (long long int i = 1; i <= n; i++) { if (!vis[i]) { x = -1; y = -1; dfs(i); ans.push_back({x, y}); } } sort(ans.begin(), ans.end()); long long int cnt = 0; for (long long int i = 1; i <= (long long int)ans.size() - 1; i++) { if (ans[i - 1].second >= ans[i].first) { cnt++; ans[i].second = max(ans[i - 1].second, ans[i].second); } } cout << cnt << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int mx[8] = {-1, 1, -1, 1, -2, 2, -2, 2}; const int my[8] = {-2, -2, 2, 2, -1, -1, 1, 1}; const int MAXN = 1100; const int MAXM = 1100; int N, M; const int MAXV = MAXN * MAXM; int V; int source[2]; int target[2]; int dist[2][MAXV]; int nxt[2][MAXV]; int st_dist[2][2]; bool parity(int v) { return ((v / M) + (v % M)) & 1; } bool one_move(int a, int b) { int dx = abs(a / M - b / M); int dy = abs(a % M - b % M); return (dx == 1 && dy == 2) || (dx == 2 && dy == 1); } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> N >> M; V = N * M; { int X0, Y0, X1, Y1; cin >> X0 >> Y0 >> X1 >> Y1; X0--, Y0--, X1--, Y1--; source[0] = X0 * M + Y0; source[1] = X1 * M + Y1; } target[0] = (N / 2 - 1) * M + (M / 2 - 1); target[1] = (N / 2 + 1 - 1) * M + (M / 2 - 1); for (int z = 0; z < 2; z++) { for (int i = 0; i < V; i++) dist[z][i] = -1; queue<int> q; dist[z][target[z]] = 0; q.push(target[z]); while (!q.empty()) { int cur = q.front(); q.pop(); int x = cur / M, y = cur % M; for (int d = 0; d < 8; d++) { int nx = x + mx[d], ny = y + my[d]; if (0 <= nx && nx < N && 0 <= ny && ny < M) { int n = nx * M + ny; if (dist[z][n] == -1) { dist[z][n] = dist[z][cur] + 1; nxt[z][n] = cur; q.push(n); } } } } for (int x = 0; x < 2; x++) { st_dist[x][z] = dist[z][source[x]]; } } int adv; if (parity(source[0]) != parity(source[1])) { adv = 0; } else { adv = 1; } int dis_dist = st_dist[!adv][!adv]; int adv_turns = (adv == 0) ? dis_dist : dis_dist - 1; int me, goal; if (st_dist[adv][adv] <= adv_turns) { me = adv; goal = adv; } else if (st_dist[adv][!adv] - 1 <= adv_turns) { me = adv; goal = !adv; } else { me = !adv; goal = !adv; } nxt[0][target[0]] = target[0] + 2 * M + 1; nxt[1][target[1]] = target[1] - 2 * M + 1; cout << (me == 0 ? WHITE : BLACK ) << n << flush; int cur[2] = {source[0], source[1]}; for (int t = 0; true; t++) { if (t % 2 == me) { auto print_pos = [&]() { cout << (cur[me] / M + 1) << << (cur[me] % M + 1) << n << flush; }; if (one_move(cur[me], cur[!me])) { cur[me] = cur[!me]; print_pos(); exit(0); } if (cur[me] == target[goal]) goal = me; cur[me] = nxt[goal][cur[me]]; print_pos(); if (cur[me] == target[me]) { exit(0); } } else { int x, y; cin >> x >> y; if (x == -1 && y == -1) { exit(0); } x--, y--; cur[!me] = x * M + y; } } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 5000 + 7; int n, a[maxn], ans[maxn], b[maxn], id; map<int, int> mp; int t0 = 0, t1 = 0, t2 = 0; int cnt[maxn]; void work(int id) { for (int i = 2;; ++i) { int t = i * i; if (a[id] >= 0 && t > a[id]) return; if (a[id] < 0 && t > -1 * a[id]) return; if (a[id] % t == 0) { while (a[id] % t == 0) { a[id] /= t; } } } } void init() { scanf( %d , &n); id = 1; for (int i = 1; i <= n; ++i) { scanf( %d , &a[i]); work(i); int t; if (t = mp[a[i]]) b[i] = t; else b[i] = mp[a[i]] = id++; } memset(ans, 0, sizeof ans); } int main() { init(); for (int i = 1; i <= n; ++i) { for (int k = 1; k < id; ++k) cnt[k] = 0; int sum = 0; for (int j = i; j <= n; ++j) { if (a[j] && !cnt[b[j]]) { sum++; } cnt[b[j]]++; ans[max(1, sum)]++; } } for (int i = 1; i <= n; ++i) { printf( %d%c , ans[i], (i == n ? n : )); } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, a = 0, k = 2000000000; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; if (x % n <= i) x = x / n; else x = x / n + 1; if (x < k) { a = i; k = x; } } cout << (a + 1); }
|
#include <bits/stdc++.h> using namespace std; long long fact[100001], inv[100001], ifact[100001]; template <typename T, typename T1> T mod(T x, T1 p) { x %= p; if (x < 0) x += p; return x; } template <typename T> T inverse(T x, T p) { x = mod(x, p); if (x == 1) return x; return mod((1LL * (-p / x) * (inv[p % x] % p)), p); } int NcR(int n, int r) { int ret = (1LL * ifact[n - r] * ifact[r]) % 1000000007; ret = (1LL * ret * fact[n]) % 1000000007; return ret; } int main(int argc, char **argv) { ios::sync_with_stdio(false); fact[0] = 1; for (int i = 1; i < 100001; i++) { fact[i] = 1LL * fact[i - 1] * i % 1000000007; } ifact[0] = 1; for (int i = 1; i < 100001; i++) { inv[i] = inverse(i, 1000000007); ifact[i] = 1LL * ifact[i - 1] * inv[i] % 1000000007; } int N; cin >> N; long long ans = 0; for (int i = 1; i < N; i++) { ans = (ans + 1LL * NcR(N - 1, i) * NcR(N, i + 1)) % 1000000007; } ans = (ans * 2 + N) % 1000000007; cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int INF = 0x7fffffff; long long n, m, k, x, s; const int maxn = 2e5 + 10; long long a[maxn], b[maxn], c[maxn], d[maxn]; int main(void) { cin.sync_with_stdio(false); cin >> n >> m >> k >> x >> s; for (int i = (0); i < (m); i++) { cin >> a[i]; } for (int i = (0); i < (m); i++) { cin >> b[i]; } for (int i = (0); i < (k); i++) { cin >> c[i]; } for (int i = (0); i < (k); i++) { cin >> d[i]; } long long ans = n * x; for (int i = (0); i < (m); i++) { long long cur = 0; long long curd = s - b[i]; if (curd < 0) continue; int idx = upper_bound(d, d + k, curd) - d - 1; if (idx < 0) { cur = n * a[i]; ans = min(ans, cur); } else { cur = (n - c[idx]) * a[i]; ans = min(ans, cur); } } int idx = upper_bound(d, d + k, s) - d - 1; if (idx >= 0) { long long cur = (n - c[idx]) * x; ans = min(ans, cur); } cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 500; const int INF = 1e7 + 500; const int MOD = 1e9 + 7; const int LOG = 18; const double EPS = 1e-9; const double PI = 3.1415926535; vector<pair<int, int> > P; long double L = INF, R = -INF, M1, M2; long double dist(long double x0, long double y0, long double x1, long double y1) { return sqrt((x1 - x0) * (x1 - x0) + (y1 - y0) * (y1 - y0)); } long double f(long double sx) { long double s, h, r = 0; for (auto p : P) { s = dist(sx, 0, p.first, p.second); h = dist(sx, 0, sx, p.second); r = max(r, (s * s) / (2 * h)); } return r; } int main() { int N, xi, yi; bool pl = false, mi = false; cin >> N; for (int i = 0; i < N; i++) { cin >> xi >> yi; P.push_back({xi, yi}); if (yi > 0) pl = true; if (yi < 0) mi = true; if (L > xi) L = xi; if (R < xi) R = xi; } if (pl & mi) { cout << -1 << endl; return 0; } long double rad1, rad2, ans = 1e18; for (int i = 0; i < 500; i++) { M1 = (2 * L + R) / 3; M2 = (L + 2 * R) / 3; rad1 = f(M1); rad2 = f(M2); if (rad1 < rad2) { R = M2; ans = min(ans, rad1); } else { L = M1; ans = min(ans, rad2); } } cout << setprecision(20) << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 5005; vector<string> A[N]; vector<int> pq[N]; int n, m; bool val[N]; inline int fnc(int i, int x) { int ans = 0; for (__typeof((n)) j = (1); j <= (n); j++) { if (A[j].size() == 3) { if (!pq[j].empty()) val[j] = val[pq[j][0]]; else val[j] = A[j][2][0] == ? ? x : A[j][2][i] - 0 ; } else { if (A[j][3] == XOR ) { if (pq[j].size() == 0) val[j] = (A[j][2][0] == ? ? x : A[j][2][i] - 0 ) ^ (A[j][4][0] == ? ? x : A[j][4][i] - 0 ); else if (pq[j].size() == 1) { if (A[j][2][0] >= a and A[j][2][0] <= z ) val[j] = val[pq[j][0]] ^ (A[j][4][0] == ? ? x : A[j][4][i] - 0 ); else val[j] = val[pq[j][0]] ^ (A[j][2][0] == ? ? x : A[j][2][i] - 0 ); } else val[j] = val[pq[j][0]] ^ val[pq[j][1]]; } else if (A[j][3] == OR ) { if (pq[j].size() == 0) val[j] = (A[j][2][0] == ? ? x : A[j][2][i] - 0 ) | (A[j][4][0] == ? ? x : A[j][4][i] - 0 ); else if (pq[j].size() == 1) { if (A[j][2][0] >= a and A[j][2][0] <= z ) val[j] = val[pq[j][0]] | (A[j][4][0] == ? ? x : A[j][4][i] - 0 ); else val[j] = val[pq[j][0]] | (A[j][2][0] == ? ? x : A[j][2][i] - 0 ); } else val[j] = val[pq[j][0]] | val[pq[j][1]]; } else { if (pq[j].size() == 0) val[j] = (A[j][2][0] == ? ? x : A[j][2][i] - 0 ) & (A[j][4][0] == ? ? x : A[j][4][i] - 0 ); else if (pq[j].size() == 1) { if (A[j][2][0] >= a and A[j][2][0] <= z ) val[j] = val[pq[j][0]] & (A[j][4][0] == ? ? x : A[j][4][i] - 0 ); else val[j] = val[pq[j][0]] & (A[j][2][0] == ? ? x : A[j][2][i] - 0 ); } else val[j] = val[pq[j][0]] & val[pq[j][1]]; } } ans += val[j]; } return ans; } void solve() { string s; cin >> n >> m; cin.ignore(); unordered_map<string, int> idx; for (__typeof((n)) i = (1); i <= (n); i++) { getline(cin, s, n ); stringstream second(s); vector<string> v; string r; while (second >> r) v.push_back(r); A[i] = v; idx[v[0]] = i; if (idx.count(v[2])) pq[i].push_back(idx[v[2]]); if (v.size() == 5 and idx.count(v[4])) pq[i].push_back(idx[v[4]]); } string mn, mx; for (__typeof((m - 1)) i = (0); i <= (m - 1); i++) { int x = fnc(i, 0); int y = fnc(i, 1); if (x == y) { mn.push_back( 0 ); mx.push_back( 0 ); } else if (x < y) { mn.push_back( 0 ); mx.push_back( 1 ); } else { mn.push_back( 1 ); mx.push_back( 0 ); } } cout << mn << n << mx << n ; } signed main() { int t; ios_base::sync_with_stdio(false); t = 1; while (t--) solve(); return 0; }
|
#include <bits/stdc++.h> inline void in(int &MAGNUS) { scanf( %d , &MAGNUS); } inline void out(int MAGNUS) { printf( %d n , MAGNUS); } inline void in(int &MAGNUS, int &CLAR) { scanf( %d%d , &MAGNUS, &CLAR); } inline void out(int MAGNUS, int CLAR) { printf( %d %d n , MAGNUS, CLAR); } inline void inl(long long &LIV) { scanf( %lld , &LIV); } inline void inl(long long &LIV, long long &MART) { scanf( %lld%lld , &LIV, &MART); } inline void outl(long long LIV) { printf( %lld n , LIV); } inline void outl(long long LIV, long long MART) { printf( %lld %lld n , LIV, MART); } using namespace std; int main() { int N, one, two, twoone = 0; in(N); in(one, two); int ans = 0; for (int i = 1; i <= N; i++) { int x; in(x); if (x == 1) { if (one > 0) one--; else { if (two > 0) two--, twoone++; else if (twoone > 0) twoone--; else ans++; } } else if (x == 2) { if (two == 0) ans += 2; else two--; } } out(ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; bool sBs(const pair<int, int> &a, const pair<int, int> &b) { return (a.second < b.second); } long long SUMD(long long n); long long BS(vector<pair<long long, long long> > &PS, long long s, long long e, long long ser); long long MI(long long a, long long m); long long P[101LL + 1]; void Sieve(int n = 101LL); long long Powb(long long b, long long n, long long m); int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long q = 1, t; cin >> q; t = q; while (q--) { long long i, j, n, m, k, l = 0, r = 0, a = 0, b = 0, c = 0, d = 0, e, g = 1, p, u, v, w, x, y, flag = 1; cin >> n >> x; long long ans = 0; vector<long long> V(n); for (i = 0; i < n; i++) cin >> V[i]; sort(V.rbegin(), V.rend()); for (i = 0; i < n; i++) { if (V[i] * (i - l + 1) >= x) { ans += 1; l = i + 1; } } cout << ans << n ; } return 0; } long long Powb(long long b, long long n, long long m) { if (n == 0) return 1LL; if (n == 1) return b; long long temp = Powb(b, n / 2, m); if (n % 2 == 0) { return (temp * temp) % m; } else { return (b * ((temp * temp) % m)) % m; } } long long SUMD(long long n) { long long sum = 0; while (n > 0) { sum += n % 10; n = n / 10; } return sum; } long long BS(vector<pair<long long, long long> > &PS, long long s, long long e, long long ser) { if (s > e) return s; long long mid = (s + e) / 2; if (PS[mid].first == ser) { return mid; } else if (PS[mid].first > ser) { return BS(PS, s, mid - 1, ser); } else return BS(PS, mid + 1, e, ser); } long long MI(long long a, long long m) { long long m0 = m; long long y = 0, x = 1; if (m == 1) return 0; while (a > 1) { long long q = a / m; long long t = m; m = a % m, a = t; t = y; y = x - q * y; x = t; } if (x < 0) x += m0; return x; } void Sieve(int n) { for (int i = 1; i <= n; i++) P[i] = i; for (long long i = 2; i * i <= n; i++) { if (P[i] == i) { for (long long j = i * i; j <= n; j += i) { if (P[j] == j) P[j] = i; } } } }
|
#include <bits/stdc++.h> using namespace std; int n, m, k; struct point { int x, y, ind; }; vector<point> fd[200200]; vector<point> bd[200200]; long long out[100100]; int main() { scanf( %d%d%d , &n, &m, &k); memset(out, -1, sizeof out); for (int i = 0, x, y; i < k; ++i) { scanf( %d%d , &x, &y); fd[x - y + m].push_back({x, y, i}); bd[x + y].push_back({x, y, i}); } int x = 0, y = 0, dx = 1, dy = 1; long long t = 0; while (true) { if (dx == dy) { int l = x - y + m; for (int i = 0; i < (int)fd[l].size(); ++i) { point &p = fd[l][i]; if (out[p.ind] != -1) continue; long long dfx = x - p.x; out[p.ind] = t + abs(dfx); } } else { int l = x + y; for (int i = 0; i < (int)bd[l].size(); ++i) { point &p = bd[l][i]; if (out[p.ind] != -1) continue; long long dfx = x - p.x; out[p.ind] = t + abs(dfx); } } int mh, mv; if (dx == 1) mh = n - x; else mh = x; if (dy == 1) mv = m - y; else mv = y; if (mh < mv) { if (dx == 1) x = n; else x = 0; t += mh; y += mh * dy; dx *= -1; } else if (mh > mv) { if (dy == 1) y = m; else y = 0; t += mv; x += mv * dx; dy *= -1; } else { break; } } for (int i = 0; i < k; ++i) printf( %I64d n , out[i]); return 0; }
|
#include <bits/stdc++.h> using namespace std; const long long inf = 1000000001; const long long maxg = 4444444444LL; vector<long long> g; void recurs(int d, int len, long long z) { if (d == len) g.push_back(z); else { recurs(d + 1, len, z * 10 + 4); recurs(d + 1, len, z * 10 + 7); } } long long a[1000]; long long f[100]; long long fact(int n) { if (n > 50) return inf; if (f[n] != 0) return f[n]; if (n == 1 || n == 0) return f[n] = 1; return f[n] = min(inf, n * min(inf, (fact(n - 1)))); } bool good(int a) { if (a == 0) return false; while (a > 0) { if (!(a % 10 == 4 || a % 10 == 7)) return false; a /= 10; } return true; } int main() { for (int ll = 1; ll <= 9; ll++) { recurs(0, ll, 0); } g.push_back(maxg); long long n, k; cin >> n >> k; if (fact(n) < k) { cout << -1 << endl; return 0; } long long p = 1; long long m = n - 1; while (fact(n - m) + 1 <= k) m--; long long s = n - m; for (int i = 1; i <= s; i++) a[i] = m + i; while (p < k) { for (int i = 1;; i++) { if (fact(i + 1) + p > k) { p += fact(i); for (int j = s - i + 1;; j++) { if (a[j] > a[s - i]) { swap(a[j], a[s - i]); break; } } break; } } } long long ans = 0; for (int i = 0; i < int(g.size()); i++) { if (g[i] <= m) ans = i + 1; } for (int i = 1; i <= s; i++) { if (good(i + m) && good(a[i])) ans++; } cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 5; int main() { int x1, y1, x2, y2; while (~scanf( %d%d%d%d , &x1, &y1, &x2, &y2)) { long long x = x2 - x1 + 1; long long y = y2 - y1 + 2; long long s = x * y / 2 - x / 2; printf( %lld n , s); } }
|
#include <bits/stdc++.h> using namespace std; vector<long long int> facts; vector<long long int> inv_facts; long long int c(long long int n, long long int p) { if (p < 0 || n < p) return 0; return (facts[n] * inv_facts[p]) % 1000000007 * inv_facts[n - p] % 1000000007; } long long int pow_mod(long long int x, long long int n) { if (n == 0) return 1; if (n & 1) return x * pow_mod(x, n - 1) % 1000000007; long long int tmp = pow_mod(x, n / 2); return tmp * tmp % 1000000007; } int main(int argc, char* argv[]) { ios_base::sync_with_stdio(false); long long int X, Y, N; cin >> X >> Y >> N; facts.push_back(1); inv_facts.push_back(1); long long int maxF = X + Y + 1; for (long long int n = 1; n <= maxF; ++n) { long long int f = facts[n - 1] * n % 1000000007; facts.push_back(f); inv_facts.push_back(pow_mod(f, 1000000007 - 2)); } vector<pair<long long int, long long int> > xys; for (long long int n = 0; n < N; ++n) { long long int x, y; cin >> x >> y; xys.push_back(make_pair(x - 1, y - 1)); } xys.push_back(make_pair(X - 1, Y - 1)); sort(xys.begin(), xys.end()); vector<long long int> dp(N + 1); for (long long int n = 0; n <= N; ++n) { long long int x = xys[n].first, y = xys[n].second; long long int tmp = c(x + y, y); for (long long int m = 0; m < N; ++m) { long long int xm = xys[m].first, ym = xys[m].second; if (xm <= x && ym <= y) tmp = (tmp + 1000000007 - dp[m] * c(x - xm + y - ym, x - xm) % 1000000007) % 1000000007; } dp[n] = tmp; } cout << dp[N] << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 1000005, md = 998244353; int n, i, j, k, e, p[N], lmt, len, rt[N], ome[N], rome[N], g[N], h[N], w1[N], w2[N], inv[N]; long long a[N], b[N], w, xxj[65]; inline void Read(long long &x) { char c; while ((c = getchar()) < 0 || c > 9 ) ; x = c - 0 ; while ((c = getchar()) >= 0 && c <= 9 ) x = x * 10 + c - 0 ; } void ntt(int a[N], bool t) { for (int i = 0; i < len; ++i) if (i < rt[i]) swap(a[i], a[rt[i]]); for (int l = 2; l <= len; l <<= 1) { int m = l >> 1; for (int i = 0; i < len; i += l) for (int j = 0; j < m; ++j) { int w = 1ll * (t ? ome[len / l * j] : rome[len / l * j]) * a[i + j + m] % md; a[i + j + m] = (a[i + j] - w + md) % md; a[i + j] = (a[i + j] + w) % md; } } if (!t) for (int i = 0; i < len; ++i) a[i] = 1ll * a[i] * inv[len] % md; } void merge(int l, int r) { if (l == r) { p[l] = 2; return; } int mid = (l + r) / 2, i; merge(l, mid); merge(mid + 1, r); lmt = 0; while ((1 << lmt) <= r - l + 1) ++lmt; len = 1 << lmt; ome[0] = rome[0] = 1; for (i = 1; i < len; ++i) { rt[i] = 0; for (j = 0; j < lmt; ++j) if ((1 << j) & i) rt[i] |= 1 << (lmt - j - 1); ome[i] = 1ll * ome[i - 1] * w1[len] % md, rome[i] = 1ll * rome[i - 1] * w2[len] % md; } for (i = 0; i < len; ++i) g[i] = h[i] = 0; for (i = l; i <= mid; ++i) g[i - l] = p[i]; for (i = mid + 1; i <= r; ++i) h[i - mid - 1] = p[i]; ntt(g, true); ntt(h, true); for (i = 0; i < len; ++i) g[i] = 1ll * g[i] * h[i] % md; ntt(g, false); for (i = 0; i < len; ++i) { g[i + 1] += g[i] / 10; g[i] %= 10; } for (i = l; i <= r; ++i) p[i] = g[i - l]; } inline int pw(int a, int b) { int rtn = 1; while (b) { if (b & 1) rtn = 1ll * rtn * a % md; a = 1ll * a * a % md; b >>= 1; } return rtn; } int main() { scanf( %d , &n); for (i = 1; i <= n; ++i) { Read(a[i]); Read(b[i]); b[i] ^= a[i]; w ^= a[i]; } e = 0; for (i = 1; i <= n; ++i) { for (j = 60; j >= 0; --j) if ((1ll << j) & b[i]) { if (!xxj[j]) { xxj[j] = b[i]; break; } b[i] ^= xxj[j]; } if (b[i] == 0) ++e; } for (j = 60; j >= 0; --j) if ((1ll << j) & w) { if (!xxj[j]) { printf( 1/1 ); return 0; } w ^= xxj[j]; } if (n - e == 0) { printf( 0/1 ); return 0; } for (i = 1; i <= 2 * (n - e); ++i) { w1[i] = pw(3, (md - 1) / i), w2[i] = pw(w1[i], md - 2); if (i == 1) inv[i] = 1; else inv[i] = 1ll * inv[md % i] * (md - md / i) % md; } merge(1, n - e); for (i = n - e; p[i] == 0; --i) ; for (; i >= 2; --i) printf( %d , p[i]); printf( %d/ , p[i] - 1); for (i = n - e; p[i] == 0; --i) ; for (; i >= 1; --i) printf( %d , p[i]); return 0; }
|
#include <bits/stdc++.h> using namespace std; int dp[5005][15], oo = 1e5; char str[5005]; int solve(int i, int s) { if (str[i] == 0 and s == 2) { return 0; } else if (str[i] == 0 and s != 2) { return oo; } else if (~dp[i][s]) { return dp[i][s]; } else { int ans = 0; if (s == 0) { if (str[i] == a ) { ans = min(solve(i + 1, s), solve(i + 1, 1)); } else { ans = min(solve(i + 1, s) + 1, solve(i + 1, 1)); } } else if (s == 1) { if (str[i] == b ) { ans = min(solve(i + 1, s), solve(i + 1, 2)); } else { ans = min(solve(i + 1, s) + 1, solve(i + 1, 2)); } } else if (s == 2) { if (str[i] == a ) { ans = solve(i + 1, s); } else { ans = solve(i + 1, s) + 1; } } return dp[i][s] = ans; } } int main() { scanf( %s , str); memset(dp, -1, sizeof dp); if (str[1] == 0) { printf( 1 n ); } else { printf( %d n , strlen(str) - solve(0, 0)); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; int cnt[maxn][26]; int dif[26]; int getAns() { int cc = 0; for (int i = 0; i < 26; ++i) if (dif[i]) { ++cc; } return cc; } int main() { string s; cin >> s; int len = s.length(); for (int i = 1; i <= len; ++i) { for (int k = 0; k < 26; ++k) cnt[i][k] = cnt[i - 1][k]; ++cnt[i][s[i - 1] - a ]; } int q; cin >> q; while (q--) { int l, r; cin >> l >> r; --l; for (int i = 0; i < 26; ++i) dif[i] = cnt[r][i] - cnt[l][i]; int tot = getAns(); if (tot == 1) { if (r - l == 1) cout << YES << endl; else cout << NO << endl; } else if (tot == 2) { if (s[l] != s[r - 1]) cout << YES << endl; else cout << NO << endl; } else { cout << YES << endl; } } }
|
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; inline int add(int a, int b) { a += b; if (a >= mod) a -= mod; return a; } inline int mult(int a, int b) { long long t = 1ll * a * b; if (t >= mod) t %= mod; return t; } int k, dp[1010][1010]; int main() { scanf( %d , &k); dp[1][0] = dp[1][1] = 1; for (int p = 1; p < k; p++) { for (int i = 0; i <= (p >= 10 ? k : min(k, 1 << p)); i++) { for (int j = 0; i + j <= (p >= 10 ? k : min(k, (1 << (p + 1)))); j++) { int tmp = mult(dp[p][i], dp[p][j]); dp[p + 1][i + j] = add(dp[p + 1][i + j], tmp); dp[p + 1][i + j + 1] = add(dp[p + 1][i + j + 1], tmp); dp[p + 1][i + j] = add(dp[p + 1][i + j], mult(tmp, mult(i + j, 2))); if (i + j) dp[p + 1][i + j - 1] = add(dp[p + 1][i + j - 1], mult(tmp, mult(i + j, i + j - 1))); } } } printf( %d n , dp[k][1]); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int M = 3e5 + 5; int a[M], b[M], c, lst[M], sl[M], ans[M]; struct D { int p1, p, s; }; queue<D> q; int read() { int s = 0, t = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == - ) t = -1; for (; isdigit(ch); ch = getchar()) s = s * 10 + ch - 0 ; return s * t; } int main() { int n; scanf( %d , &n); for (int i = 1; i <= n; i++) a[i] = read(); for (int i = 1; i <= n; i++) b[i] = read(); int h = n; q.push({n, n, 0}); while (!q.empty()) { int p1 = q.front().p1, p = q.front().p, s = q.front().s; q.pop(); if (!p) { c = s; break; } for (int i = h - 1; i >= p - a[p]; i--) lst[i] = p1, q.push({i, i + b[i], s + 1}), h = i; } if (!c) return printf( -1 ), 0; printf( %d n , c); for (int i = 1, x = 0; i <= c; i++, x = lst[x]) ans[i] = x; for (int i = c; i; i--) printf( %d , ans[i]); }
|
#include <bits/stdc++.h> using namespace std; const int E4 = 1e4 + 2; const int E5 = 1e5 + 2; const int E6 = 1e6 + 2; const int MOD = 1e9 + 7; const int INF = 1e9 + 8; const double eps = 1e-12; const double pi = 3.14159265359; const long long INFL = 1ll * INF * INF; void getTime(double sub = 0.0) { cout << Time : << fixed << clock() * 1.0 / CLOCKS_PER_SEC - sub << n ; } double getClock() { return clock() * 1.0 / CLOCKS_PER_SEC; } long long pw(long long a, long long n, long long K = MOD) { return (n ? (pw(((a % K) * (a % K)) % K, n / 2ll, K) * ((n & 1ll) ? (a % K) : 1ll)) % K : 1ll); } int gcd(int a, int b) { return (b == 0 ? a : gcd(b, a % b)); } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int lcm(int a, int b) { return a * (b / gcd(a, b)); } long long lcm(long long a, long long b) { return (b / gcd(a, b)) * a; } int fact(int n) { return n > 0 ? n * fact(n - 1) : 1; } long long fact(long long n) { return n > 0 ? n * fact(n - 1) : 1; } int choose(int k, int n) { if (k > n) return 0; k = (n - k > k ? n - k : k); if (k == 0) return 1; int R = n; for (int i = 2; i <= k; i++) { R = R * (n - i + 1) / i; } return R; } long long choose(long long k, long long n) { if (k > n) return 0; k = (n - k > k ? n - k : k); if (k == 0) return 1; int R = n; for (int i = 2; i <= k; i++) { R = R * (n - i + 1) / i; } return R; } int lowbit(int m) { return m & (-m); } int hibit(int m) { for (int i = 30; i >= 0; i--) if ((1 << i) & m) return i; return 0; } long long hibit(long long m) { for (int i = 62; i >= 0; i--) if ((1ll << i) & m) return i; return 0; } int getbit(int x, int b) { return (x >> b) & 1; } long long getbit(long long x, int b) { return (x >> b) & 1; } int setbit(int x, int b) { return x | (1 << b); } void _setbit(int &x, int b) { x = setbit(x, b); } long long setbit(long long x, int b) { return x | (1ll << b); } void _setbit(long long &x, int b) { x = setbit(x, b); } int unsetbit(int x, int b) { return x & ((~0) - (1 << b)); } void _unsetbit(int &x, int b) { x = unsetbit(x, b); } int bitcnt(int x) { x = x - ((x >> 1) & 0x55555555); x = (x & 0x33333333) + ((x >> 2) & 0x33333333); return ((x + (x >> 4) & 0xF0F0F0F) * 0x1010101) >> 24; } long long bitcnt(long long x) { int p1 = (x >> 32) & ((1ll << 32) - 1); int p2 = x & ((1ll << 32) - 1); return bitcnt(p1) + bitcnt(p2); } string bin(int x, int len = -1) { if (len == -1) { len = hibit(x) + 1; } string res = ; for (int i = len - 1; i >= 0; i--) res.push_back(getbit(x, i) + 0 ); return res; } string bin(long long x, int len = -1) { if (len == -1) { len = hibit(x) + 1; } string res = ; for (int i = len - 1; i >= 0; i--) res.push_back(getbit(x, i) + 0 ); return res; } int zerocnt(int x) { return hibit(x) - bitcnt(x) + 1; } int zerocnt(long long x) { return hibit(x) - bitcnt(x) + 1; } struct IO { IO operator>>(int &n) const { n = 0; bool sign = 0; int a = getchar(); while (a < 0 || a > 9 ) { if (a == - ) { sign = 1; a = getchar(); break; } a = getchar(); } while (a >= 0 && a <= 9 ) { n = (n << 1) + (n << 3) + a - 0 ; a = getchar(); } if (sign) n = ~(n - 1); return *this; } IO operator>>(long long &n) { n = 0; bool sign = 0; char c = getchar(); while (c < 0 || c > 9 ) { if (c == - ) sign = true; c = getchar(); } while (c >= 0 && c <= 9 ) { n = n * 10 + c - 48; c = getchar(); } if (sign) n = ~(n - 1); return *this; } IO operator>>(double &x) const { scanf( %lf , &x); return *this; } IO operator>>(char &x) const { do { x = getchar(); } while (x == || x == n ); return *this; } IO operator>>(string &s) const { cin >> s; return *this; } IO operator<<(const int x) const { printf( %d , x); return *this; } IO operator<<(const double x) const { printf( %lf , x); return *this; } IO operator<<(const char x) const { putchar(x); return *this; } IO operator<<(const long long x) const { cout << x; return *this; } IO operator<<(string x) { printf( %s , (x.c_str())); return *this; } } in, out; namespace Input { template <template <typename, typename...> class Container, typename T> void cinv(Container<T> &a, int n) { for (int i = 0; i < n; i++) { T t; cin >> t; a.push_back(t); } } template <typename T> void cinv(T *ptr, int len) { for (int i = 0; i < len; i++) { in >> *(ptr + i); } } template <typename T> void cinv(T A, int n, int m) { for (int i = 0; i < n; i++) { cinv(A[i], m); } } } // namespace Input namespace Output { void split(char A) { if (A != 0 ) { putchar(A); } } template <typename T> void Print(T A, char sp = 0 ) { out << A; split(sp); } template <typename T1, typename T2> void Print(pair<T1, T2> A, char sp = 0 ) { cout << ( << A.first << , << A.second << ) ; split(sp); } template <typename T> void Print(T A[], int len, char sp = ) { for (int i = 0; i < len; i++) { out << A[i]; split(sp); } } template <typename T> void Print(T *A, int n, int m, char nsp = , char sp = n ) { for (int i = 0; i < n; i++) { Print(A[i], m, nsp); split(sp); } } template <template <typename, typename...> class Container, typename T> void Print(Container<T> C, char sp = ) { for (auto i : C) { Print(i); split(sp); } } template <typename T> void Print(stack<T> A, char sp = ) { while (A.size()) { Print(A.top()); split(sp); A.pop(); } } template <template <typename, typename...> class Container, typename T1, typename T2> void Print(Container<T1, T2> C, char sp = ) { for (auto i : C) { Print(i); split(sp); } } template <typename T> void Printl(T A) { Print(A, n ); } } // namespace Output using namespace Output; using namespace Input; int x; int o; int n; int e; int r; int z; int main() { string s; cin >> x >> s; for (auto i : s) { if (i == o ) { o++; } if (i == n ) { n++; } if (i == e ) { e++; } if (i == r ) { r++; } if (i == z ) { z++; } } for (int i = 0; i < n; i++) { cout << 1 << ; } for (int i = 0; i < z; i++) { cout << 0 << ; } }
|
#include <bits/stdc++.h> using namespace std; map<int, int> dp; int l[305], c[305]; int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> l[i]; for (int i = 0; i < n; i++) cin >> c[i]; dp[0] = 0; for (int i = 0; i < n; i++) { for (auto x : dp) { int a = gcd(x.first, l[i]); if (dp.count(a)) dp[a] = min(dp[a], x.second + c[i]); else dp[a] = x.second + c[i]; } } if (dp.count(1)) cout << dp[1] << endl; else cout << -1 << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int n, c, b, ans = 0; cin >> n; int a[n + 1]; for (int i = 1; i <= n - 1; i++) cin >> a[i]; cin >> c >> b; for (int i = c; i <= b - 1; i++) ans += a[i]; cout << ans; }
|
#include <bits/stdc++.h> using namespace std; int C[155][155]; int ans[155][2][155], cnt = 0; bool vis[150] = {0}; void DFS(int x, int c) { int i; if (x > 150) return; C[x][0] = C[x - 2][0]; for (i = 1; i < x; i++) { C[x][i] = c * C[x - 1][i - 1] + C[x - 2][i]; if (C[x][i] < -1 || C[x][i] > 1) return; } C[x][x] = c * C[x - 1][x - 1]; if (vis[x] == 0 && C[x][x] == C[x - 1][x - 1]) { vis[x] = 1; ++cnt; memcpy(ans[x][0], C[x], sizeof(C[x])); memcpy(ans[x][1], C[x - 1], sizeof(C[x - 1])); } DFS(x + 1, 1); DFS(x + 1, -1); } int main() { int i, j; ans[1][0][0] = 0; ans[1][0][1] = 1; ans[1][1][0] = 1; cnt = 1; C[0][0] = 1; C[1][0] = 0; C[1][1] = 1; DFS(2, 1); DFS(2, -1); C[0][0] = -1; DFS(2, 1); DFS(2, -1); int n; scanf( %d , &n); printf( %d n , n); for (i = 0; i <= n; i++) printf( %d , ans[n][0][n] * ans[n][0][i]); printf( n ); printf( %d n , n - 1); for (i = 0; i <= n - 1; i++) printf( %d , ans[n][0][n] * ans[n][1][i]); printf( n ); return 0; }
|
#include <bits/stdc++.h> using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } int bsr(int x) { return 31 - __builtin_clz(x); } template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; template <class T> T pow(T x, ll n) { T r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } template <uint MD> struct ModInt { uint v; ModInt() : v{0} {} ModInt(ll v) : v{normS(v % MD + MD)} {} static uint normS(const uint &x) { return (x < MD) ? x : x - MD; }; static ModInt make(const uint &x) { ModInt m; m.v = x; return m; } ModInt operator+(const ModInt &r) const { return make(normS(v + r.v)); } ModInt operator-(const ModInt &r) const { return make(normS(v + MD - r.v)); } ModInt operator*(const ModInt &r) const { return make((ull)v * r.v % MD); } ModInt &operator+=(const ModInt &r) { return *this = *this + r; } ModInt &operator-=(const ModInt &r) { return *this = *this - r; } ModInt &operator*=(const ModInt &r) { return *this = *this * r; } static ModInt inv(const ModInt &x) { return pow(ModInt(x), MD - 2); } }; using Mint = ModInt<TEN(9) + 7>; const int MD = 1001001; int factor[MD]; void first() { fill(begin(factor), end(factor), -1); for (int i = 2; i < MD; i++) { if (factor[i] != -1) continue; for (int j = i; j < MD; j += i) { factor[j] = i; } } } int main() { first(); V<int> id2v[300]; map<V<int>, int> vs; int id[MD]; for (int i = 1; i < MD; i++) { map<int, int> mp; int x = i; while (factor[x] != -1) { mp[factor[x]]++; x /= factor[x]; } V<int> v; for (auto p : mp) { v.push_back(p.second); } sort(begin(v), end(v)); if (vs.count(v)) { id[i] = vs[v]; } else { int nid = int(vs.size()); vs[v] = nid; id[i] = nid; id2v[nid] = v; } } int C = vs.size(); static Mint to1[MD][25], to0[MD][25]; to0[0][0] = 1; for (int i = 1; i < 30; i++) { to1[0][i] = 1; } for (int i = 1; i < MD; i++) { Mint sm0, sm1; for (int j = 0; j < 25; j++) { sm0 += to0[i - 1][j]; sm1 += to1[i - 1][j]; to0[i][j] = sm0; to1[i][j] = sm1; } } int q; scanf( %d , &q); for (int i = 0; i < q; i++) { int r, n; scanf( %d %d , &r, &n); V<int> v = id2v[id[n]]; Mint co[11]; co[0] = 1; int nw = 1; for (int d : v) { for (int i = nw; i >= 0; i--) { co[i] = co[i] * to0[r][d]; if (i) co[i] += co[i - 1] * to1[r][d]; } nw++; } Mint ans = 0; for (int i = 0; i < 11; i++) { ans += co[i] * Mint(1 << i); } printf( %d n , ans.v); } return 0; }
|
#include <bits/stdc++.h> using namespace std; int cross(const complex<int>& a, const complex<int>& b) { return a.real() * b.imag() - a.imag() * b.real(); } int hypot2(const complex<int>& a) { return a.real() * a.real() + a.imag() * a.imag(); } int N; vector<complex<int> > pt; vector<complex<int> > hull(const vector<complex<int> >& p) { int n = p.size(), k = 0; if (n < 3) return p; vector<complex<int> > h(2 * n); for (int i = 0; i < n; ++i) { while (k >= 2 && cross(h[k - 1] - h[k - 2], p[i] - h[k - 2]) <= 0) --k; h[k++] = p[i]; } for (int i = n - 2, e = k + 1; i >= 0; --i) { while (k >= e && cross(h[k - 1] - h[k - 2], p[i] - h[k - 2]) <= 0) --k; h[k++] = p[i]; } h.resize(k); h.pop_back(); return h; } bool with_one(const complex<int>& o, int m) { vector<complex<int> > p; copy_if(pt.begin(), pt.end(), back_inserter(p), [&](const complex<int>& a) { return hypot2(a - o) >= m; }); auto h = hull(p); switch (h.size()) { case 0: case 1: return false; case 2: return hypot2(h[1] - h[0]) >= m; } int n = h.size(); auto nx = [&](int i) { return i + 1 == n ? 0 : i + 1; }; auto cal = [&](int i) { return h[nx(i)] - h[i]; }; for (int i = 0, j = 0; i < n; ++i) { while (cross(cal(i), cal(j)) >= 0) { j = nx(j); if (hypot2(h[j] - h[i]) >= m) return true; } } return false; } bool ok(int m) { for (const complex<int>& p : pt) { if (with_one(p, m)) return true; } return false; } int main() { cout.precision(20); cout << fixed; cin >> N; for (int i = 0, x, y; i < N; ++i) { cin >> x >> y; pt.push_back({x, y}); } sort(pt.begin(), pt.end(), [](const complex<int>& a, const complex<int>& b) { return a.real() < b.real() || a.real() == b.real() && a.imag() < b.imag(); }); int l = 0, h = 1e9; while (l + 1 < h) { int m = (l + h) / 2; if (ok(m)) l = m; else h = m; } cout << sqrt(l) / 2 << n ; return 0; }
|
#include<bits/stdc++.h> #define rb(a,b,c) for(int a=b;a<=c;++a) #define rl(a,b,c) for(int a=b;a>=c;--a) #define LL long long #define IT iterator #define PB push_back #define II(a,b) make_pair(a,b) #define FIR first #define SEC second #define FREO freopen( check.out , w ,stdout) #define rep(a,b) for(int a=0;a<b;++a) #define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()) #define random(a) rng()%a #define ALL(a) a.begin(),a.end() #define POB pop_back #define ff fflush(stdout) #define fastio ios::sync_with_stdio(false) #define check_min(a,b) a=min(a,b) #define check_max(a,b) a=max(a,b) using namespace std; //inline int read(){ // int x=0; // char ch=getchar(); // while(ch< 0 ||ch> 9 ){ // ch=getchar(); // } // while(ch>= 0 &&ch<= 9 ){ // x=(x<<1)+(x<<3)+(ch^48); // ch=getchar(); // } // return x; //} const int INF=0x3f3f3f3f; typedef pair<int,int> mp; /*} */ const int MAXN=505; int t,n,a[MAXN<<1][MAXN]; bool era[MAXN<<1],cho[MAXN<<1]; int cnt[MAXN][MAXN]; int fa[MAXN<<1]; int root(int x){ return fa[x]=(x==fa[x]? x:root(fa[x])); } vector<int> g[MAXN<<1]; bool col[MAXN<<1]; bool vis[MAXN<<1]; void dfs(int now){ vis[now]=1; for(auto it:g[now]){ if(!vis[it]){ col[it]=col[now]^1; dfs(it); } } } void solve(){ scanf( %d ,&n); rb(i,1,n*2) rb(j,1,n) scanf( %d ,&a[i][j]); memset(era,0,sizeof(era)); memset(vis,0,sizeof(vis)); memset(cho,0,sizeof(cho)); memset(cnt,0,sizeof(cnt)); memset(col,0,sizeof(col)); rb(i,1,n*2) rb(j,1,n) cnt[j][a[i][j]]++; rb(T,1,n){ mp Tmp=II(-1,-1); rb(j,1,n){ rb(k,1,n) if(cnt[j][k]==1){ Tmp=II(j,k); break; } if(Tmp.FIR!=-1) break; } if(Tmp.FIR==-1) break; int x; rb(j,1,2*n){ if(!era[j]&&a[j][Tmp.FIR]==Tmp.SEC){ x=j; break; } } cho[x]=1; rb(j,1,2*n) if(!era[j]&&j!=x){ rb(k,1,n) if(j!=x&&a[j][k]==a[x][k]) era[j]=1; if(era[j]){ rb(k,1,n) cnt[k][a[j][k]]--; } } rb(k,1,n) cnt[k][a[x][k]]--; } rb(i,1,n*2) g[i].clear(),fa[i]=i; rb(i,1,n*2) era[i]|=cho[i]; rb(i,1,n*2) if(!era[i]) rb(j,i+1,n*2) if(!era[j]) { bool E=0; rb(k,1,n) if(a[i][k]==a[j][k]){ E=1; break; } if(E){ g[i].PB(j); g[j].PB(i); fa[root(i)]=root(j); } } rb(i,1,n*2) if(!era[i]&&!vis[i]) dfs(i); int ans=1; vector<int> res; rb(i,1,n*2){ if(!era[i]&&col[i]==0){ res.PB(i); } } rb(i,1,n*2) if(!era[i]&&root(i)==i) ans=(ans+ans)%998244353; rb(i,1,2*n) if(cho[i]) res.PB(i); printf( %d n ,ans); for(auto it:res){ printf( %d ,it); } puts( ); } int main(){ scanf( %d ,&t); while(t--) solve(); return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; long long int a[n]; long long int b[n]; int pos = 1; for (int i = 0; i < n; ++i) { cin >> a[i]; b[i] = a[i]; } sort(b, b + n); reverse(b, b + n); for (int i = 0; i < n; i++) { if (b[i] != a[i]) { pos = 0; break; } if (i != 0 && b[i] == b[i - 1]) pos = 0; } if (!pos) cout << YES << n ; else cout << NO << n ; } }
|
#include <bits/stdc++.h> using namespace std; int N, K; int a[2210], b[2210]; int cnt[2210]; long long func(void) { int i, j = N; int aans = -1, bans = -1, bid = -1; long long best = (1ll << 60); for (i = N - 1; i >= 0; i--) { while (j > i || (j > 0 && cnt[j - 1] > 0)) { j--; if (b[j] > 0 && (bid == -1 || b[j] < b[bid])) bid = j; } if (a[i] > 0 && bid != -1 && a[i] + b[bid] < best) { best = a[i] + b[bid]; aans = i; bans = bid; } } if (aans < bans) { for (i = aans; i < bans; i++) cnt[i]++; } else { for (i = aans - 1; i >= bans; i--) cnt[i]--; } a[aans] = b[bans] = 0; return best; } int main(void) { int i; cin >> N >> K; for ((i) = 0; (i) < (int)(N); (i)++) cin >> a[i]; for ((i) = 0; (i) < (int)(N); (i)++) cin >> b[i]; long long ans = 0; for ((i) = 0; (i) < (int)(K); (i)++) ans += func(); cout << ans << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 2; const int mod = 998244353; void inc(int& x, const int& y) { (x += y) >= mod ? x -= mod : 0; } void dec(int& x, const int& y) { x < y ? x += mod - y : x -= y; } void cmax(int& x, const int& y) { x = x > y ? x : y; } int n, k, m; int l[N], r[N], x[N], pos[N], a[N], f[N]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k >> m; for (int i = 1; i <= m; i++) cin >> l[i] >> r[i] >> x[i]; int ans = 1; for (int p = 0; p <= k - 1; p++) { for (int i = 1; i <= n + 1; i++) pos[i] = a[i] = 0; for (int i = 1; i <= m; i++) if (x[i] >> p & 1) ++a[l[i]], --a[r[i] + 1]; else cmax(pos[r[i] + 1], l[i]); for (int i = 2; i <= n + 1; i++) a[i] += a[i - 1], cmax(pos[i], pos[i - 1]); f[0] = 1; int sum = 1, l = 0; for (int i = 1; i <= n + 1; i++) { while (l < pos[i]) dec(sum, f[l]), f[l++] = 0; f[i] = a[i] ? 0 : sum, inc(sum, f[i]); } ans = 1ll * ans * f[n + 1] % mod; } cout << ans << n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; void Solution() { int n, k; cin >> n >> k; int a[n]; vector<int> v; for (int i = 0; i < n; i++) { cin >> a[i]; if (5 - a[i] >= k) { v.push_back(a[i]); } } cout << v.size() / 3 << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); Solution(); return 0; }
|
#include <bits/stdc++.h> using namespace std; long long a, b, x, y; long long r; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } long long binsearch(long long i, long long j, long long w, long long m) { if (i == j) { if (i * w <= a && i * m <= b) return i; return -1; } if (i == j - 1) { if (j * w <= a && j * m <= b) return j; if (i * w <= a && i * m <= b) return i; return -1; } long long mid = (i + j) / 2; if (mid * w <= a && mid * m <= b) return binsearch(mid, j, w, m); return binsearch(i, mid - 1, w, m); } int main() { cin >> a >> b >> x >> y; r = x / y; long long h = gcd(x, y); long long k = binsearch(0, 2000000000, x / h, y / h); if (k == -1) { cout << 0 << << 0; return 0; } cout << k * (x / h) << << k * (y / h); return 0; }
|
#include <bits/stdc++.h> using namespace std; struct point { int x, y; }; int n, k, l[1000002], r[1000002]; point a[1000002]; bool cmp(point a, point b) { return a.x < b.x; } void nhap() { cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> a[i].x; l[i] = 0; r[i] = 0; } for (int i = 1; i <= n; ++i) cin >> a[i].y; sort(a + 1, a + n + 1, cmp); } void sol1() { int cur = 1, st = 1; l[1] = 1; for (int i = 2; i <= n; ++i) { if (a[i].x - a[st].x <= k) cur++; else { while (a[i].x - a[st].x > k) { ++st; } cur = i - st + 1; } l[i] = max(l[i - 1], cur); } } void sol2() { int cur = 1, st = n; r[n] = 1; for (int i = n - 1; i >= 1; --i) { if (a[st].x - a[i].x <= k) cur++; else { while (a[st].x - a[i].x > k) --st; cur = st - i + 1; } r[i] = max(r[i + 1], cur); } } void xuly() { int kq = 0; for (int i = 1; i < n; ++i) kq = max(kq, l[i] + r[i + 1]); if (kq == 0) ++kq; cout << kq << n ; } int main(int argc, const char* argv[]) { int t; cin >> t; for (int i = 1; i <= t; ++i) { nhap(); sol1(); sol2(); xuly(); } return 0; }
|
#include <bits/stdc++.h> using namespace std; void fre() { freopen( c://test//input.in , r , stdin); freopen( c://test//output.out , w , stdout); } template <class T1, class T2> inline void gmax(T1 &a, T2 b) { if (b > a) a = b; } template <class T1, class T2> inline void gmin(T1 &a, T2 b) { if (b < a) a = b; } const int N = 1010, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f; int n; int a[N], b[N]; int main() { while (~scanf( %d , &n)) { for (int i = 1; i <= n; ++i) scanf( %d , &a[i]); sort(a + 1, a + n + 1); int p = 1; for (int i = 1; i <= n; i += 2) { b[i] = a[p++]; } for (int i = 2; i <= n; i += 2) { b[i] = a[p++]; } bool flag = 1; for (int i = 1; i <= n; i += 2) { if (i < n) { if (b[i] > b[i + 1]) flag = 0; } if (i > 1) { if (b[i] > b[i - 1]) flag = 0; } } if (!flag) puts( Impossible ); else { for (int i = 1; i <= n; ++i) printf( %d , b[i]); puts( ); } } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 1e5; int n, k, a[N]; long long seg[4 * N + 4], dp[N + 1][12]; void update(int p, int l, int r, int indx, long long val) { if (l == r) { seg[p] = val; return; } int mid = l + (r - l) / 2; if (indx <= mid) update(p * 2, l, mid, indx, val); else update(p * 2 + 1, mid + 1, r, indx, val); seg[p] = seg[p * 2] + seg[p * 2 + 1]; } long long get(int p, int l, int r, int a, int b) { if (l >= a && r <= b) { return seg[p]; } if (l > b || r < a) { return 0; } int mid = l + (r - l) / 2; return get(p * 2, l, mid, a, b) + get(p * 2 + 1, mid + 1, r, a, b); } int main() { scanf( %d%d , &n, &k); for (int i = 0; i < n; ++i) scanf( %d , a + i); for (int i = 0; i <= N; ++i) dp[i][1] = 1; for (int j = 2; j <= k + 1; ++j) { memset(seg, 0, sizeof seg); for (int i = 0; i < n; ++i) { dp[i][j] = get(1, 0, n - 1, 1, a[i] - 1); update(1, 0, n - 1, a[i], dp[i][j - 1]); } } long long ans = 0; for (int i = 0; i < n; ++i) { ans += dp[i][k + 1]; } printf( %lld n , ans); }
|
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) const long long INF = 0x3f3f3f3f3f3f3f3f; const long long llinf = (1LL << 61); const int inf = (1 << 30); const int nmax = 1e3 + 50; const int mod = 1e9 + 7; using namespace std; int n, m, i, j, a[nmax][nmax], aa[nmax][nmax], b[nmax][nmax], bb[nmax][nmax], ca[nmax][nmax], cb[nmax][nmax], mn; char c[nmax][nmax]; vector<pair<pair<int, int>, int> > ans; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cerr.tie(0); cout.tie(0); cin >> n >> m; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) cin >> c[i][j]; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) if (c[i][j] == * ) a[i][j] = a[i][j - 1] + 1; for (j = m; j >= 1; j--) if (c[i][j] == * ) aa[i][j] = aa[i][j + 1] + 1; } for (i = 1; i <= m; i++) { for (j = 1; j <= n; j++) if (c[j][i] == * ) b[j][i] = b[j - 1][i] + 1; for (j = n; j >= 1; j--) if (c[j][i] == * ) bb[j][i] = bb[j + 1][i] + 1; } for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { if (c[i][j] == * ) { mn = min(min(a[i][j], aa[i][j]), min(b[i][j], bb[i][j])); if (mn > 1) { ca[i][j - mn + 1]++; ca[i][j + mn]--; cb[i - mn + 1][j]++; cb[i + mn][j]--; ans.push_back({{i, j}, mn - 1}); } } } } for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) ca[i][j] += ca[i][j - 1]; for (i = 1; i <= m; i++) for (j = 1; j <= n; j++) cb[j][i] += cb[j - 1][i]; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) if (c[i][j] == * && !ca[i][j] && !cb[i][j]) return cout << -1 << endl, 0; cout << ans.size() << endl; for (i = 0; i < ans.size(); i++) cout << ans[i].first.first << << ans[i].first.second << << ans[i].second << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; class trio { public: long long first, second, third; }; long long gcdExtended(long long a, long long b, long long *x, long long *y) { if (a == 0) { *x = 0, *y = 1; return b; } long long x1, y1; long long gcd = gcdExtended(b % a, a, &x1, &y1); *x = y1 - (b / a) * x1; *y = x1; return gcd; } long long modInverse(long long a, long long m) { long long x, y; long long g = gcdExtended(a, m, &x, &y); long long res = (x % m + m) % m; return res; } long long power(long long x, long long y) { long long ans = 1, second = x; while (y) { if (y & 1) { ans *= second; ans %= 1000000007; } second *= second; second %= 1000000007; y >>= 1; } return ans; } const long long N = 51; long long dp1[N]; long long dp3[N]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; __int128 val = 1; dp1[0] = 1; dp1[1] = 1; dp1[2] = 1; for (long long i = 3; i < N; ++i) { val *= (i - 2); val = min(val, (__int128)1000000000000000001); dp1[i] = val; } dp3[0] = 1; dp3[1] = 1; for (long long i = 2; i < N; ++i) { val = 0; for (long long j = 1; j < i + 1; ++j) { val += (__int128)dp3[i - j] * dp1[j]; val = min(val, (__int128)1000000000000000001); } dp3[i] = val; } long long third; cin >> third; while (third--) { long long n, k; cin >> n >> k; long long arr[N] = {0}; if (dp3[n] < k) { cout << -1 << n ; continue; } for (long long i = 1; i <= n;) { long long len, left, st; __int128 count; for (long long j = i; j <= n; ++j) { len = j - i + 1; left = n - i - len + 1; st = j; count = (__int128)dp1[len] * dp3[left]; count = min(count, (__int128)1000000000000000001); if (count < k) { k -= (count); } else { break; } } arr[i] = st; set<long long> available; for (long long j = i; j < i + len; ++j) { available.insert(j); } available.erase(st); for (long long j = i + 1; j < i + len; ++j) { if (j == i + len - 1) { arr[j] = *(available.begin()); break; } for (auto it : available) { long long cur = it; while (cur != j && cur) { cur = arr[cur]; } if (cur == j) continue; count = (__int128)dp1[len - (j - i)] * dp3[left]; count = min(count, (__int128)1000000000000000001); if (count < k) { k -= (count); } else { arr[j] = it; break; } } available.erase(arr[j]); } i += len; } for (long long i = 1; i < n + 1; ++i) { cout << arr[i] << ; } cout << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; template <class T> inline void checkmin(T &a, T b) { if (b < a) a = b; } template <class T> inline void checkmax(T &a, T b) { if (b > a) a = b; } int cond = (long long)1; void _main() { int n, w, m; scanf( %d%d%d , &n, &w, &m); int guests = m; int usedbottles = 0; int botid = 1; int fuel = m; int used = 0; bool ok = 1; vector<vector<pair<int, int> > > sol; for (int i = (0); i < (m); ++i) { int needfuel = n; vector<pair<int, int> > result; while (needfuel > 0) { if (fuel > 0) { int use = min(fuel, needfuel); result.push_back(pair<int, int>(botid, use)); fuel -= use; needfuel -= use; ++used; if (used > 2) ok = 0; if (fuel == 0) { botid++; fuel = m; used = 0; } } else assert(0); } sol.push_back(result); } if (!ok) cout << NO << endl; cout.precision(6); if (ok) { cout << YES << endl; for (int i = (0); i < (m); ++i) { for (int j = (0); j < ((int)(sol[i]).size()); ++j) { if (j) cout << ; long double myfuel = (long double)sol[i][j].second * w / m; cout << fixed << sol[i][j].first << << myfuel; } cout << endl; } } } int main(int argc, char **argv) { string p = ../gcj/source/ + string( ) + argv[0][strlen(argv[0]) - 1]; for (int i = (0); i < (argc); ++i) if (strcmp(argv[i], 1n ) == 0) { freopen( 1.in , r , stdin); } for (int i = (0); i < (argc); ++i) if (strcmp(argv[i], 2n ) == 0) { freopen( 2.in , r , stdin); } for (int i = (0); i < (argc); ++i) if (strcmp(argv[i], 3n ) == 0) { freopen( 3.in , r , stdin); } for (int i = (0); i < (argc); ++i) if (strcmp(argv[i], 4n ) == 0) { freopen( 4.in , r , stdin); } for (int i = (0); i < (argc); ++i) if (strcmp(argv[i], 5n ) == 0) { freopen( 5.in , r , stdin); } for (int i = (0); i < (argc); ++i) if (strcmp(argv[i], s0 ) == 0) { freopen((p + -small-0.in ).c_str(), r , stdin); freopen((p + -small-0.out ).c_str(), w , stdout); } for (int i = (0); i < (argc); ++i) if (strcmp(argv[i], s1 ) == 0) { freopen((p + -small-1.in ).c_str(), r , stdin); freopen((p + -small-1.out ).c_str(), w , stdout); } for (int i = (0); i < (argc); ++i) if (strcmp(argv[i], s2 ) == 0) { freopen((p + -small-2.in ).c_str(), r , stdin); freopen((p + -small-2.out ).c_str(), w , stdout); } for (int i = (0); i < (argc); ++i) if (strcmp(argv[i], l0 ) == 0) { freopen((p + -large-0.in ).c_str(), r , stdin); freopen((p + -large-0.out ).c_str(), w , stdout); } for (int i = (0); i < (argc); ++i) if (strcmp(argv[i], q ) == 0) cond = 1 << 30; _main(); return 0; }
|
#include <bits/stdc++.h> using namespace std; int n, k, x; int a[255]; long long dp[255][255][255]; int main() { cin >> n >> k >> x; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i <= n; i++) for (int j = 0; j <= k; j++) { for (int p = 0; p <= x; p++) { dp[i][j][p] = -LLONG_MAX; } } dp[0][1][0] = 0; for (int i = 0; i < n; i++) { for (int j = 1; j <= k; j++) for (int choose = 0; choose <= x; choose++) { if (dp[i][j][choose] == -LLONG_MAX) continue; if (j != k) { dp[i + 1][j + 1][choose] = max(dp[i + 1][j + 1][choose], dp[i][j][choose]); } dp[i + 1][1][choose + 1] = max(dp[i + 1][1][choose + 1], dp[i][j][choose] + a[i]); } } long long res = -LLONG_MAX; for (int i = 0; i <= k; i++) res = max(res, dp[n][i][x]); if (res == -LLONG_MAX) res = -1; cout << res; }
|
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 11, mod = 998244353; int ans, n, k, blo, bel[N], f[N], g[N], a[N], L[N], R[N], las[N], bef[N], lim[N], sum[411][N], Sum[411]; inline void inc(int &x, int y) { x += y; if (x >= mod) x -= mod; } inline void deal(int l, int r, int x) { int o = bel[l]; for (register int i = l; i <= r; ++i) { if (f[i] <= lim[o]) inc(Sum[o], mod - g[i - 1]); inc(sum[o][f[i]], mod - g[i - 1]); f[i] += x; inc(sum[o][f[i]], g[i - 1]); if (f[i] <= lim[o]) inc(Sum[o], g[i - 1]); } } inline void modify(int l, int r, int x) { int o; if (bel[l] == bel[r]) { deal(l, r, x); return; } o = bel[l]; deal(l, R[o], x); o = bel[r]; deal(L[o], r, x); for (register int i = bel[l] + 1; i <= bel[r] - 1; ++i) { if (x == 1) inc(Sum[i], mod - sum[i][lim[i]]); lim[i] -= x; if (x == -1) inc(Sum[i], sum[i][lim[i]]); } } int main() { scanf( %d%d , &n, &k); blo = sqrt(n); for (register int i = 1; i <= n; ++i) { scanf( %d , a + i), bel[i] = (i - 1) / blo + 1; las[i] = bef[a[i]]; bef[a[i]] = i; } for (register int i = 1; i <= n; ++i) { if (!L[bel[i]]) L[bel[i]] = i; R[bel[i]] = i; } for (register int i = 1; i <= bel[n]; ++i) lim[i] = k; g[0] = 1; for (register int i = 1; i <= n; ++i) { inc(sum[bel[i]][0], g[i - 1]), inc(Sum[bel[i]], g[i - 1]); modify(las[i] + 1, i, 1); if (las[i]) modify(las[las[i]] + 1, las[i], -1); ans = 0; for (register int j = 1; j <= bel[i]; ++j) inc(ans, Sum[j]); g[i] = ans; } printf( %d n , g[n]); return 0; }
|
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; const int N = 1000 + 20; int n, m, q, part[N][N]; string s[N]; inline int get(int x) { return (__builtin_popcount(x) & 1 ? -1 : +1); } inline int get(int x, int y) { return get(x) * get(y); } inline pii calc(int x) { int a = x / 2, b = x / 2; if (x & 1) { if (get(x - 1) == 1) a++; else b++; } return pii(a, b); } inline pii calc(int x, int y) { pii p1 = calc(x), p2 = calc(y); return pii(p1.first * p2.first + p1.second * p2.second, p1.first * p2.second + p1.second * p2.first); } inline int f(int x, int y) { int res = 0; int X = x / n, Y = y / m; x %= n, y %= m; pii p = calc(X, Y); res += part[n][m] * p.first + (n * m - part[n][m]) * p.second; p = calc(X); if (get(Y) == -1) swap(p.first, p.second); res += part[n][y] * p.first + (n * y - part[n][y]) * p.second; p = calc(Y); if (get(X) == -1) swap(p.first, p.second); res += part[x][m] * p.first + (x * m - part[x][m]) * p.second; if (get(X, Y) == 1) res += part[x][y]; else res += (x * y - part[x][y]); return res; } int32_t main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> m >> q; for (int i = 0; i < n; i++) { cin >> s[i]; for (int j = 0; j < m; j++) { part[i + 1][j + 1] = (s[i][j] == 1 ) + part[i][j + 1] + part[i + 1][j] - part[i][j]; } } while (q--) { int x1, hlkhjl, x2, y2; cin >> x1 >> hlkhjl >> x2 >> y2; x1--, hlkhjl--; cout << f(x2, y2) - f(x1, y2) - f(x2, hlkhjl) + f(x1, hlkhjl) << n ; } }
|
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); long long a, b, m; long long vx, vy, vz; cin >> a >> b >> m >> vx >> vy >> vz; double time = -((double)(m) / (double)(vy)); double xc = (double)(a) / 2.0 + time * vx; if (xc >= 0) { long long p = xc / a; if (p % 2 == 1) { xc = (p + 1) * a - xc; } else { xc -= p * a; } } else { long long p = xc / a; if (p % 2 == 0) { xc -= p * a; xc = -xc; } else { xc -= p * a; xc = -xc; xc = a - xc; } } double zc = time * vz; if (zc >= 0) { long long p = zc / b; if (p % 2 == 1) { zc = (p + 1) * b - zc; } else { zc -= p * b; } } else { long long p = zc / b; if (p % 2 == 0) { zc -= p * b; zc = -zc; } else { zc -= p * b; zc = -zc; zc = b - zc; } } printf( %0.12llf %0.12llf , xc, zc); }
|
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 7; long long a[MAXN]; int n, k, x; void print() { for (int i = 1; i <= n; ++i) cout << a[i] << ; exit(0); } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> k >> x; vector<int> zeros; int n_pos = 0, n_neg = 0, n_zer = 0; for (int i = 1; i <= n; ++i) { cin >> a[i]; if (a[i] > 0) ++n_pos; if (a[i] < 0) ++n_neg; if (a[i] == 0) ++n_zer, zeros.push_back(i); } if (n_zer) { if (n_zer > k) print(); if ((n_zer & 1) == (n_neg & 1)) a[zeros.back()] += 1LL * x, zeros.pop_back(), ++n_pos, --k; for (auto v : zeros) a[v] -= 1LL * x, ++n_neg, --k; } set<pair<long long, int> > s; for (int i = 1; i <= n; ++i) s.insert({abs(a[i]), i}); if ((n_neg & 1) == 0) { int id = s.begin()->second; bool save = (a[id] > 0); s.erase(s.begin()); while (k && (a[id] == 0 || ((a[id] > 0) == save))) { if (save) a[id] -= 1LL * x; else a[id] += 1LL * x; --k; } if (k == 0) print(); s.insert({abs(a[id]), id}); } while (k--) { int id = s.begin()->second; s.erase(s.begin()); if (a[id] > 0) a[id] += 1LL * x; else a[id] -= 1LL * x; s.insert({abs(a[id]), id}); } print(); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MAX = 2e6 + 10; int n, k, count1; int a[MAX]; int res = 1e19 + 10; int binary_search1(int x, int l, int r) { int res = 0; while (l <= r) { int mid = (l + r) >> 1; if (a[mid] <= x) { res = mid; l = mid + 1; } else r = mid - 1; } return res; } int binary_search2(int x, int l, int r) { int res = 0; while (l <= r) { int mid = (l + r) >> 1; if (a[mid] < x) { l = mid + 1; } else { res = mid; r = mid - 1; } } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; scanf( %d %d n , &n, &k); count1 = 0; for (int i = 1; i <= n; i++) { char s; scanf( %c , &s); if (s == 0 ) { a[++count1] = i; } } sort(a + 1, a + 1 + count1); k += 1; for (int i = 1; i <= count1 - k + 1; i++) { int l = i; int r = k + i - 1; int pos1 = binary_search1((a[l] + a[r]) >> 1, l, r); int pos2 = binary_search2((a[l] + a[r]) >> 1, l, r); if (pos1 != 0) { res = min(res, max(a[pos1] - a[l], a[r] - a[pos1])); } if (pos2 != 0) { res = min(res, max(a[pos2] - a[l], a[r] - a[pos2])); } } printf( %d , res); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int inf = 0x7fffffff; const int Size = 1000 * 1000 + 1; char buffer[Size]; int solution(int nTest) { int n, m, K; scanf( %d%d%d , &n, &m, &K); vector<string> name; vector<vector<int> > a, b, c; for (int i = 0; i < n; i++) { scanf( %s , buffer); name.push_back(buffer); int ta, tb, tc; vector<int> va, vb, vc; for (int j = 0; j < m; j++) { scanf( %d%d%d , &ta, &tb, &tc); va.push_back(ta); vb.push_back(tb); vc.push_back(tc); } a.push_back(va); b.push_back(vb); c.push_back(vc); } int res = 0; for (int i = 0; i < n; i++) { for (int k = 0; k < n; k++) { int t = K; vector<pair<int, int> > v; for (int j = 0; j < m; j++) { v.push_back(make_pair(b[k][j] - a[i][j], j)); } sort(v.begin(), v.end()); reverse(v.begin(), v.end()); int sum = 0; for (int j = 0; j < m; j++) { if (v[j].first >= 0) { int ct = c[i][v[j].second]; if (t >= ct) { sum += ct * v[j].first; t -= ct; } else { sum += t * v[j].first; t = 0; } } } res = max(res, sum); } } printf( %d n , res); return 0; } int main() { if (0) { freopen( input.txt , r , stdin); freopen( output.txt , w , stdout); } int i = 0, n = 99999; while (i < n && solution(i)) { i++; } return 0; }
|
#include <bits/stdc++.h> const int N = 1e5, V = 5; const unsigned long long MOD = 1ull << 58, INV5 = 14757395258967641293ull; struct Com { unsigned long long a[V]; inline void clear() { a[0] = a[1] = a[2] = a[3] = a[4] = 0; } inline Com() { clear(); } inline unsigned long long &operator[](int x) { return a[x]; } inline const unsigned long long &operator[](const int &x) const { return a[x]; } inline Com operator+(const Com &b) const { Com c; for (int i = 0; i < V; i++) c[i] = a[i] + b[i]; return c; } inline Com operator-(const Com &b) const { Com c; for (int i = 0; i < V; i++) c[i] = a[i] - b[i]; return c; } inline Com operator*(const unsigned long long &b) const { Com c; for (int i = 0; i < V; i++) c[i] = a[i] * b; return c; } inline Com operator*(const Com &b) const { static unsigned long long d[V << 1]; for (int i = 0; i < V; i++) for (int j = 0; j < V; j++) d[i + j] += a[i] * b[j]; Com c; for (int i = 0; i < V; i++) c[i] = d[i] + d[i + V], d[i] = d[i + V] = 0; return c; } inline Com &operator+=(const Com &b) { return *this = *this + b; } inline Com &operator*=(const unsigned long long &b) { return *this = *this * b; } inline Com &operator*=(const Com &b) { return *this = *this * b; } inline void out() { printf( (%llu,%llu,%llu,%llu,%llu) n , a[0], a[1], a[2], a[3], a[4]); } inline unsigned long long val() { return (a[0] - a[4]) >> 5; } inline bool operator!=(const Com &b) const { for (int i = 0; i < V; i++) if (a[i] != b[i]) return 1; return 0; } } w[2][V << 1], a[N]; inline Com qpow(Com a, int b) { Com c; c[0] = 1; for (; b; b >>= 1, a *= a) if (b & 1) c *= a; return c; } inline void pre() { w[0][0][0] = w[1][0][0] = 1; w[0][1][3] = w[1][1][2] = -1; for (int i = 2; i < V << 1; i++) { w[0][i] = w[0][i - 1] * w[0][1], w[1][i] = w[1][i - 1] * w[1][1]; } } const int pw[V] = {1, 10, 100, 1000, 10000}; inline void fft(int opt) { for (int i = 0; i < V; i++) for (int j = 0; j < N; j++) if (!((j / pw[i]) % 10)) { static Com f[V << 1]; for (int a = 0; a < V << 1; a++) for (int b = 0; b < V << 1; b++) { f[a] += ::a[j + b * pw[i]] * (opt ? w[0][(10 - a * b % 10) % 10] : w[0][a * b % 10]); } for (int a = 0; a < V << 1; a++) { ::a[j + a * pw[i]] = f[a]; f[a].clear(); } } if (opt) { unsigned long long INV = INV5 * INV5 * INV5 * INV5 * INV5; for (int i = 0; i < N; i++) a[i] *= INV; } } int n; int main() { pre(); scanf( %d , &n); for (int i = 1, x; i <= n; i++) { scanf( %d , &x); a[x][0]++; } fft(0); for (int i = 0; i < N; i++) a[i] = qpow(a[i], n); fft(1); for (int i = 0; i < n; i++) printf( %llu n , a[i].val() % MOD); return 0; }
|
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; const int maxn = 2e5; const int INF = 0x7fffffff; const int mod = 1e9 + 7; const double eps = 1e-7; const double Pi = acos(-1.0); inline int read_int() { char c; int ret = 0, sgn = 1; do { c = getchar(); } while ((c < 0 || c > 9 ) && c != - ); if (c == - ) sgn = -1; else ret = c - 0 ; while ((c = getchar()) >= 0 && c <= 9 ) ret = ret * 10 + (c - 0 ); return sgn * ret; } inline long long read_ll() { char c; long long ret = 0, sgn = 1; do { c = getchar(); } while ((c < 0 || c > 9 ) && c != - ); if (c == - ) sgn = -1; else ret = c - 0 ; while ((c = getchar()) >= 0 && c <= 9 ) ret = ret * 10 + (c - 0 ); return sgn * ret; } int n, arr[maxn + 5], res[maxn + 5]; vector<int> dc, ic; int main() { n = read_int(); for (int i = 1; i <= n; ++i) arr[i] = read_int(); for (int i = 1; i <= n; ++i) { if (dc.empty() && ic.empty()) { if (i < n && arr[i] <= arr[i + 1]) ic.push_back(i); else dc.push_back(i); } else if (dc.empty()) { int id = ic.back(); if (arr[i] <= arr[id]) dc.push_back(i); else { if (i + 1 <= n && arr[i] <= arr[i + 1]) ic.push_back(i); else dc.push_back(i); } } else if (ic.empty()) { int id = dc.back(); if (arr[i] >= arr[id]) ic.push_back(i); else { if (i + 1 <= n && arr[i] <= arr[i + 1]) ic.push_back(i); else dc.push_back(i); } } else { int id1 = ic.back(), id2 = dc.back(); if (arr[i] <= arr[id1] && arr[i] >= arr[id2]) { cout << NO << endl; return 0; } else if (arr[i] > arr[id1] && arr[i] < arr[id2]) { if (i + 1 <= n && arr[i] <= arr[i + 1]) ic.push_back(i); else dc.push_back(i); } else if (arr[i] > arr[id1]) ic.push_back(i); else dc.push_back(i); } } for (int i = 0; i < dc.size(); ++i) res[dc[i]] = 1; cout << YES << endl; for (int i = 1; i <= n; ++i) printf( %d%c , res[i], n [i == n]); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 505, maxmax = 1000000; int n, javab[maxn][maxn]; vector<int> v; int dp(int l, int r) { int x = maxmax; if (l == r) { javab[l][r] = 1; return 1; } if (l == r - 1) { if (v[r] == v[l]) { javab[l][r] = 1; return 1; } else { javab[l][r] = 2; return 2; } } if (javab[l][r - 1] != 0) x = min(x, javab[l][r - 1] + 1); else { x = min(x, dp(l, r - 1) + 1); } for (int i = r - 1; i >= l; i--) { if (v[r] == v[i]) { if (i == l) { if (javab[l + 1][r - 1] != 0) { x = min(x, javab[l + 1][r - 1]); } else x = min(x, dp(l + 1, r - 1)); break; } if (javab[l][i - 1] != 0) { if (javab[i][r] != 0) { x = min(x, javab[l][i - 1] + javab[i][r]); } else { x = min(x, javab[l][i - 1] + dp(i, r)); } } else { if (javab[i][r] != 0) { x = min(x, dp(l, i - 1) + javab[i][r]); } else { x = min(x, dp(l, i - 1) + dp(i, r)); } } } } javab[l][r] = x; return x; } int main() { cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; v.push_back(x); } int javab = dp(0, v.size() - 1); cout << javab; return 0; }
|
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) #pragma GCC optimize( no-stack-protector ) #pragma GCC optimize( unroll-loops ) #pragma GCC optimize( fast-math ) #pragma GCC target( sse,sse2,sse3,ssse3,popcnt,abm,mmx,tune=native ) using namespace std; template <typename T> void uin(T &a, T b) { if (b < a) { a = b; } } template <typename T> void uax(T &a, T b) { if (b > a) { a = b; } } const long long N = 100 * 1000 + 228; vector<long long> g[N]; long long deg[N]; long long h[N], p[N]; void dfs(long long v, long long par = -1) { h[v] = (par == -1 ? 0 : h[par] + 1); p[v] = par; for (long long to : g[v]) { if (to != par) dfs(to, v); } } signed main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; for (long long i = 0; i < n - 1; ++i) { long long u, v; cin >> u >> v; ++deg[u]; ++deg[v]; g[u].emplace_back(v); g[v].emplace_back(u); } long long root = 1; for (long long i = 1; i <= n; ++i) { if (deg[i] > 1) { root = i; break; } } dfs(root); bool even = 0, odd = 0; for (long long i = 1; i <= n; ++i) { if (deg[i] == 1) { if (h[i] & 1) odd = 1; else even = 1; } } long long min_colors = 0; if (even && odd) { min_colors = 3; } else { min_colors = 1; } vector<long long> pars; long long leafs = 0; for (long long i = 1; i <= n; ++i) { if (deg[i] == 1) { pars.emplace_back(p[i]); ++leafs; } } sort(pars.begin(), pars.end()); pars.erase(unique(pars.begin(), pars.end()), pars.end()); long long max_colors = (long long)pars.size(); max_colors += (n - 1 - leafs); cout << min_colors << << max_colors << n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } const int MAXLEN = 15; long long dp[MAXLEN + 1][2]; long long dpleq[MAXLEN + 1][2]; inline int conv(char c) { if (isdigit(c)) return c - 0 ; if (islower(c)) return c - a + 10; assert(false); } long long calc(char* s) { int slen = strlen(s); long long ret = 0; for (int mx = (0); mx < (16); ++mx) { int pos = mx / 4, off = mx % 4; if (pos >= slen) continue; memset(dp, 0, sizeof(dp)); memset(dpleq, 0, sizeof(dpleq)); dp[0][0] = dpleq[0][0] = 1; for (int i = (0); i < (slen); ++i) for (int j = (0); j < (2); ++j) for (int k = (0); k <= (mx); ++k) { if (i == pos && (k & (1 << off)) == 0) continue; int nj = j == 1 || k == mx ? 1 : 0; dp[i + 1][nj] += dp[i][j]; if (k < conv(s[i])) dpleq[i + 1][nj] += dp[i][j]; else if (k == conv(s[i])) dpleq[i + 1][nj] += dpleq[i][j]; } long long cur = dpleq[slen][1]; ret += cur; } return ret; } void dec(char* s) { for (int i = 0;; ++i) if (s[i] == 0 ) s[i] = f ; else { assert(s[i] != 0 ); if (s[i] == a ) s[i] = 9 ; else --s[i]; break; } int slen = strlen(s); while (slen >= 2 && s[slen - 1] == 0 ) --slen; s[slen] = 0 ; } int nq; char sl[MAXLEN + 1], sr[MAXLEN + 1]; long long solve() { reverse(sl, sl + strlen(sl)); reverse(sr, sr + strlen(sr)); long long ret = calc(sr); if (strcmp(sl, 0 ) != 0) { dec(sl); ret -= calc(sl); } return ret; } void run() { scanf( %d , &nq); for (int qi = (0); qi < (nq); ++qi) { scanf( %s%s , sl, sr); printf( %lld n , solve()); } } int main() { run(); return 0; }
|
#include <bits/stdc++.h> int main() { int n; scanf( %d , &n); int ans = 0; int score = -1; for (int i = 0; i < n; i++) { int sub, subsc = 0; scanf( %d , &sub); subsc += sub; scanf( %d , &sub); subsc += sub; scanf( %d , &sub); subsc += sub; scanf( %d , &sub); subsc += sub; if (subsc > score) { ans++; } if (!i) score = subsc; } printf( %d , ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 450, inf = 1e8; int n1, n2, m, r, b, node, s, t, S, T, a[N], id[N]; char col1[N], col2[N]; int head[N], ce = 1, inq[N], dis[N], maxflow, mincost, need; struct edge { int to, nxt, cap, flow, w; } e[5000]; void lnk(int a, int b, int c, int w) { e[++ce] = (edge){b, head[a], c, 0, w}, head[a] = ce; e[++ce] = (edge){a, head[b], 0, 0, -w}, head[b] = ce; } bool bfs() { for (int i = 1; i <= node; ++i) dis[i] = inf, inq[i] = 0; dis[s] = 0, inq[s] = 1; queue<int> q; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); for (int i = head[u]; i; i = e[i].nxt) { int v = e[i].to; if (e[i].cap > e[i].flow && dis[v] > dis[u] + e[i].w) { dis[v] = dis[u] + e[i].w; if (!inq[v]) q.push(v), inq[v] = 1; } } inq[u] = 0; } return dis[t] < inf; } int dfs(int u, int lim) { if (u == t) return mincost += dis[t] * lim, lim; int tag = 0; inq[u] = 1; for (int i = head[u]; i; i = e[i].nxt) if (e[i].cap > e[i].flow && dis[e[i].to] == dis[u] + e[i].w && !inq[e[i].to]) { int d = dfs(e[i].to, min(lim - tag, e[i].cap - e[i].flow)); tag += d, e[i].flow += d, e[i ^ 1].flow -= d; if (tag == lim) { inq[u] = 0; return tag; } } return tag; } int main() { scanf( %d%d%d%d%d , &n1, &n2, &m, &r, &b); scanf( %s , col1 + 1); scanf( %s , col2 + 1); node = n1 + n2, S = ++node, T = ++node, s = ++node, t = ++node; for (int i = 1; i <= n1; ++i) if (col1[i] == R ) ++a[i], --a[S], lnk(S, i, inf, 0); else if (col1[i] == B ) --a[i], ++a[T], lnk(i, T, inf, 0); else lnk(S, i, inf, 0), lnk(i, T, inf, 0); for (int i = 1; i <= n2; ++i) if (col2[i] == R ) --a[i + n1], ++a[T], lnk(i + n1, T, inf, 0); else if (col2[i] == B ) ++a[i + n1], --a[S], lnk(S, i + n1, inf, 0); else lnk(S, i + n1, inf, 0), lnk(i + n1, T, inf, 0); for (int i = 1; i <= T; ++i) if (a[i] > 0) lnk(s, i, a[i], 0), need += a[i]; else if (a[i] < 0) lnk(i, t, -a[i], 0); lnk(T, S, inf, 0); for (int i = 1, u, v; i <= m; ++i) scanf( %d%d , &u, &v), id[i] = ce + 1, lnk(u, v + n1, 1, r), lnk(v + n1, u, 1, b); while (bfs()) maxflow += dfs(s, inf); if (maxflow < need) { puts( -1 ); return 0; } printf( %d n , mincost); for (int i = 1; i <= m; ++i) if (e[id[i]].flow) putchar( R ); else if (e[id[i] + 2].flow) putchar( B ); else putchar( U ); return 0; }
|
#include <bits/stdc++.h> using namespace std; int search_factor(int n) { if (n % 2 == 0) { return n / 2; } else { for (int i = (n - 1) / 2; i >= 1; i -= 1) { if (n % i == 0) return i; } } } int main() { int n; cin >> n; long long int sum = n; while (n != 1) { int res = search_factor(n); sum += res; n = res; } cout << sum; return 0; }
|
#include <bits/stdc++.h> using namespace std; struct sTree { int L, R; int pl, pr; long long sum_m; long long sum_r; } tree[2000000]; int treeSize; int s[100009]; int r[100009]; int m[100009]; int BuildTree(int L, int R) { int cur = treeSize++; tree[cur].L = L; tree[cur].R = R; tree[cur].sum_r = r[L]; if (L < R) { tree[cur].pl = BuildTree(L, (L + R) / 2); tree[cur].pr = BuildTree((L + R) / 2 + 1, R); tree[cur].sum_r = tree[tree[cur].pl].sum_r + tree[tree[cur].pr].sum_r; } return cur; } int Toggle(int i, int cur) { tree[treeSize] = tree[cur]; cur = treeSize; treeSize++; if (tree[cur].L == tree[cur].R) { tree[cur].sum_m = m[i]; tree[cur].sum_r = 0; return cur; } if (i <= tree[tree[cur].pl].R) tree[cur].pl = Toggle(i, tree[cur].pl); else tree[cur].pr = Toggle(i, tree[cur].pr); tree[cur].sum_m = tree[tree[cur].pl].sum_m + tree[tree[cur].pr].sum_m; tree[cur].sum_r = tree[tree[cur].pl].sum_r + tree[tree[cur].pr].sum_r; return cur; } pair<long long, long long> Get(int L, int R, int cur) { if (L <= tree[cur].L && R >= tree[cur].R) { return make_pair(tree[cur].sum_m, tree[cur].sum_r); } pair<long long, long long> ans = make_pair(0, 0); if (L <= tree[tree[cur].pl].R && tree[tree[cur].pl].L <= R) { pair<long long, long long> tmp = Get(L, R, tree[cur].pl); ans.first += tmp.first; ans.second += tmp.second; } if (L <= tree[tree[cur].pr].R && tree[tree[cur].pr].L <= R) { pair<long long, long long> tmp = Get(L, R, tree[cur].pr); ans.first += tmp.first; ans.second += tmp.second; } return ans; } int roots[100001]; long long Calc(int L, int R, int time) { if (L == R) { return min((long long)m[L], s[L] + (long long)time * r[L]); } pair<long long, long long> tmp = Get(L, R, roots[time]); return tmp.first + (long long)time * tmp.second; } vector<int> ind[100001]; int n; int fwt[100001]; int fwt_sum(int k) { int ans = 0; while (k) { ans += fwt[k]; k -= k & (-k); } return ans; } void fwt_add(int k, int d) { while (k <= n) { fwt[k] += d; k += k & (-k); } } int fwt_getKth(int k) { int ans = 0; for (int i = 18; i >= 0; i--) { if (ans + (1 << i) <= n && fwt[ans + (1 << i)] < k) { ans += (1 << i); k -= fwt[ans]; } } return ans + 1; } int lastTime[100001]; int main() { scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d %d %d , &s[i], &m[i], &r[i]); if (m[i] == 0) r[i] = 0; if (r[i]) { ind[m[i] / r[i] + 1].push_back(i); } } roots[0] = BuildTree(1, n); for (int k = 1; k <= 100000; k++) { roots[k] = roots[k - 1]; for (int i = 0; i < ind[k].size(); i++) roots[k] = Toggle(ind[k][i], roots[k]); } for (int k = 1; k <= n; k++) { fwt_add(k, 1); } int Q; scanf( %d , &Q); while (Q--) { int time, L, R; scanf( %d %d %d , &time, &L, &R); long long ans = 0; int last, ind = L; while (ind <= R) { int cur = fwt_sum(ind); int next = fwt_getKth(cur + 1) - 1; cur = fwt_getKth(cur); ans += Calc(ind, min(R, next), min(time - lastTime[cur], 100000)); s[cur] = 0; if (cur >= L) fwt_add(cur, -1); last = lastTime[cur]; ind = next + 1; } printf( %I64d n , ans); fwt_add(L, 1); lastTime[L] = time; if (R + 1 <= n && fwt_sum(R + 1) == fwt_sum(R)) { lastTime[R + 1] = last; fwt_add(R + 1, 1); } } }
|
#include <bits/stdc++.h> using namespace std; void rd(long long &x) { scanf( %lld , &x); } void rd(long long &x, long long &y) { scanf( %lld%lld , &x, &y); } void rd(long long &x, long long &y, long long &z) { scanf( %lld%lld%lld , &x, &y, &z); } void rd(long long &x, long long &y, long long &z, long long &s) { scanf( %lld%lld%lld%lld , &x, &y, &z, &s); } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << { ; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << , ; os << *it; } return os << } ; } template <typename T> ostream &operator<<(ostream &os, const set<T> &v) { os << [ ; for (auto it = v.begin(); it != v.end(); ++it) { if (it != v.begin()) os << , ; os << *it; } return os << ] ; } void faltu() { cerr << endl; } template <typename T> void faltu(T a[], int n) { for (int i = 0; i < n; ++i) cerr << a[i] << ; cerr << endl; } template <typename T, typename... hello> void faltu(T arg, const hello &...rest) { cerr << arg << ; faltu(rest...); } string s; const long long N = 2e5 + 5; long long n, m, cnt[N], mx[N][2]; bool visit[N]; pair<long long, long long> dp[N]; pair<long long, long long> back(long long pos) { if (pos >= n) return make_pair(0LL, 0LL); if (visit[pos]) return dp[pos]; visit[pos] = 1; pair<long long, long long> &r = dp[pos]; r = back(pos + 1); if (mx[pos][0] == m) { pair<long long, long long> q = back(pos + m); q.first++; q.second += cnt[pos + m - 1]; if (pos) q.second -= cnt[pos - 1]; if (q.first > r.first || (q.first == r.first && q.second < r.second)) r = q; } return r; } int main() { rd(n); cin >> s; rd(m); for (long long i = 0; i < (n); i++) { cnt[i] = (s[i] == ? ); if (i) cnt[i] += cnt[i - 1]; } for (int i = n; i < N; i++) cnt[i] = cnt[i - 1]; for (int i = n - 1; i >= 0; i--) { if (s[i] == a ) { mx[i][0] = min(mx[i + 1][1] + 1, m); mx[i][1] = 0; } else if (s[i] == b ) { mx[i][1] = min(mx[i + 1][0] + 1, m); mx[i][0] = 0; } else { mx[i][0] = min(mx[i + 1][1] + 1, m); mx[i][1] = min(mx[i + 1][0] + 1, m); } } pair<long long, long long> p = back(0); cout << (long long)p.second; return 0; }
|
#include <bits/stdc++.h> using namespace std; int tc = 0; long long a, b, c, d; inline long long sum(long long i) { return -a * i + b * (d - 1) * i + i * (i - 1) / 2 * b * d; } inline long long calc(long long i) { if (i < 0) return 0; long long ans = 0; if (i < (c + 1) / d) { ans = min(ans, sum(i) - a + i * b); } else { i = (c + 1) / d; ans = min(ans, sum(i) - a + (i - 1) * b + (c + 1 % d > 0) * b); ans = min(ans, sum(i)); ans = min(ans, -a); } return ans; } inline void solve() { ++tc; scanf( %lld%lld%lld%lld , &a, &b, &c, &d); if (c < d) { if (b * c >= a) printf( %lld n , a); else puts( -1 ); } else { long long i = floor(-(-a + b * (d - 1) + b - 1.0 * b * d / 2) / (b * d)); i = max(i, 0ll); long long dmg = (-a + b * (d - 1) + b * d * ((c + 1) / d - 1) + ((c + 1) % d) * b); long long ans = 0; if (dmg < 0) { puts( -1 ); } else { ans = min(ans, calc(i - 1)); ans = min(ans, calc(i)); ans = min(ans, calc(i + 1)); ans = min(ans, calc(i + 2)); printf( %lld n , -ans); } } return; } int main() { int T; for (scanf( %d , &T); T--; solve()) ; return 0; }
|
#include <bits/stdc++.h> using namespace std; int m, n; int a[110][110], b[110][110]; template <typename T> inline void chkmax(T &x, T y) { x = max(x, y); } template <typename T> inline void chkmin(T &x, T y) { x = min(x, y); } template <typename T> inline void read(T &x) { T f = 1; x = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == - ) f = -f; for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - 0 ; x *= f; } int main() { read(m); read(n); memset(a, 255, sizeof(a)); for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { read(b[i][j]); if (b[i][j] == 0) { for (int k = 1; k <= n; k++) a[i][k] = 0; for (int k = 1; k <= m; k++) a[k][j] = 0; } } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (a[i][j] == -1) a[i][j] = 1; } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { int res = 0; for (int k = 1; k <= n; k++) res |= a[i][k]; for (int k = 1; k <= m; k++) res |= a[k][j]; if (res != b[i][j]) { printf( NO n ); return 0; } } } printf( YES n ); for (int i = 1; i <= m; i++) { for (int j = 1; j < n; j++) { printf( %d , a[i][j]); } printf( %d n , a[i][n]); } return 0; }
|
#include <bits/stdc++.h> using namespace std; struct DT { int l, r, x; }; DT qr[300005]; int n, m, ans[300005], tree[4 * 300005]; void update(int nd, int b, int e, int l, int r, int v) { if (r < l || l < 1 || r > n) return; if (b > r || e < l) return; if (tree[nd] != 0 && b != e) { tree[2 * nd] = tree[nd]; tree[2 * nd + 1] = tree[nd]; tree[nd] = 0; } if (b >= l && e <= r) { tree[nd] = v; return; } int md = (b + e) / 2; update(2 * nd, b, md, l, r, v); update(2 * nd + 1, md + 1, e, l, r, v); } int query(int nd, int b, int e, int x, int v) { if (b == e) { if (v == 0) return tree[nd]; else return v; } int md = (b + e) / 2; if (v == 0) v = tree[nd]; if (x <= md) return query(2 * nd, b, md, x, v); else return query(2 * nd + 1, md + 1, e, x, v); } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> qr[i].l >> qr[i].r >> qr[i].x; } for (int i = m; i >= 1; i--) { int l, r, x; l = qr[i].l; r = qr[i].r; x = qr[i].x; if (x >= l && x <= r) { update(1, 1, n, l, x - 1, x); update(1, 1, n, x + 1, r, x); } else { update(1, 1, n, l, r, x); } } for (int i = 1; i <= n; i++) { ans[i] = query(1, 1, n, i, 0); } for (int i = 1; i <= n; i++) { if (i < n) cout << ans[i] << ; else cout << ans[i] << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int n, cnt = 0; int main() { ios::sync_with_stdio(false); cin >> n; if (n <= 3) { cout << NO ; return 0; } cout << YES << n ; if (n % 2) { cout << 5 - 3 = 2 << n ; cout << 2 + 1 = 3 << n ; cout << 3 * 4 = 12 << n ; cout << 12 * 2 = 24 << n ; while (n > 5) { cout << n << - << n - 1 << = << 1 << n ; cnt++; n -= 2; } cnt--; while (cnt > 0) { cout << 1 * 1 = 1 << n ; cnt--; } if (cnt == 0) cout << 1 * 24 = 24 << n ; } else { cout << 1 * 2 = 2 << n ; cout << 2 * 3 = 6 << n ; cout << 6 * 4 = 24 << n ; while (n > 4) { cout << n << - << n - 1 << = << 1 << n ; n -= 2; cnt++; } cnt--; while (cnt > 0) { cout << 1 * 1 = 1 << n ; cnt--; } if (cnt == 0) cout << 1 * 24 = 24 << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; const int INF = 1000000009; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int a, b; cin >> a >> b; if ((a || b) && max(a, b) - min(a, b) < 2) { cout << YES ; } else { cout << NO ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; vector<pair<pair<int, int>, int> > v; int a[(int)(1e5 + 50)]; string s; void solve() { int n; scanf( %d , &n); for (int i = 1; i <= n; i++) { int x; scanf( %d , &x); s += (char)(x + 0 ); } for (int i = 0; i < s.size();) { int st = i, len = 0; char c = s[i]; while (i < s.size() && c == s[i]) { len++; i++; } v.push_back(make_pair(make_pair(st, len), (int)(c - 0 ))); } int len = v.size(); if (v[len - 1].second == 1 || (v[len - 1].first.second == 2 && v[len - 1].second == 0 && len < 3)) { printf( NO ); return; } printf( YES n ); string ans; if (v[len - 1].first.second == 2) { for (int i = 0; i < len - 3; i++) { char toAdd = (char)(v[i].second + 0 ); for (int j = 0; j < v[i].first.second; j++) { ans += toAdd; ans += -> ; } } for (int j = 0; j < v[len - 3].first.second - 1; j++) { ans += 0 ; ans += -> ; } ans += (0->( ; for (int i = 0; i < v[len - 2].first.second; i++) { ans += 1 ; ans += -> ; } ans += 0))->0 ; cout << ans; return; } if (v[len - 1].first.second % 2 == 0) { ans += ( ; for (int i = 0; i < v[len - 1].first.second - 3; i++) { ans += 0-> ; } ans += (0->0) ; ans += ->0 ; ans += ) ; } else { ans += ( ; for (int i = 0; i < v[len - 1].first.second - 1; i++) { ans += 0-> ; } ans += 0 ; ans += ) ; } if (len - 1 == 0) { cout << ans; return; } reverse(ans.begin(), ans.end()); ans += >- ; ans += )1 ; for (int i = 0; i < v[len - 2].first.second - 1; i++) { ans += >-1 ; } ans += ( ; for (int i = len - 3; i >= 0; i--) { ans += >- ; char toAdd = (char)(v[i].second + 0 ); int curlen = v[i].first.second; for (int j = 0; j < curlen - 1; j++) { ans += toAdd; ans += >- ; } ans += toAdd; } reverse(ans.begin(), ans.end()); cout << ans; } int main() { int t = 1; for (int i = 1; i <= t; i++) { solve(); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int NMAX = 755; const long long INF = 1LL << 62; long long dp[NMAX][NMAX]; int main() { cin.sync_with_stdio(false); int n, m; cin >> n >> m; vector<int> v(n); for (int i = 0; i < n; i++) { cin >> v[i]; } for (int i = n - 1; i >= 0; i--) { for (int j = 0; j <= n - i; j++) { if (j) { dp[i][j] = dp[i + 1][j - 1]; } else { dp[i][j] = INF; } if (v[i] >= 0) { dp[i][j] = min(dp[i][j], max(0LL, dp[i + 1][j] - v[i])); } else { dp[i][j] = min(dp[i][j], dp[i + 1][j] - v[i]); } } } for (; m; m--) { long long x; cin >> x; int l = 0; int r = n; int ans = n; while (l <= r) { int mi = (l + r) / 2; if (dp[0][mi] <= x) { ans = mi; r = mi - 1; } else { l = mi + 1; } } cout << ans << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int n; cin >> n; long long int k; cin >> k; vector<long long int> arr(n, 0); long long int cnt = 0; for (int i = 0; i < n; ++i) { cin >> arr[i]; long long int var = 0; while (arr[i] > 0) { long long int rem = arr[i] % 10; if (rem == 4 || rem == 7) { var++; } arr[i] /= 10; } if (var > k) { cnt++; } } cout << n - cnt << n ; return 0; }
|
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> bool Max(T1& a, T2 b) { return a < b ? a = b, 1 : 0; } template <typename T1, typename T2> bool Min(T1& a, T2 b) { return a > b ? a = b, 1 : 0; } const int N = 1000001; bool prime[N]; vector<long long> pri; vector<pair<long long, long long> > nv, sv; long long n, m, s; long long ans; void rec(long long i, long long prod) { if (i == sv.size()) { ans++; return; } auto cur = sv[i]; for (int j = 0; j < (cur.second + 1); ++j) { rec(i + 1, prod); if ((prod *= cur.first) > n) break; } } long long pow(long long a, long long p) { long long ret = 1; while (p) { if (p & 1) ret *= a; a *= a; p >>= 1; } return ret; } void solve() { long long v; n = 1; for (int i = 0; i < (3); ++i) { cin >> v; n *= v; } m = 1; for (int i = 0; i < (3); ++i) { cin >> v; m *= v; } s = 2; for (int i = 0; i < (3); ++i) { cin >> v; s *= v; } long long ts = s, tn = n; nv.clear(); sv.clear(); for (auto& e : pri) { if (e * e > ts) break; long long cnt = 0; while (ts % e == 0) { cnt++; ts /= e; } if (cnt) sv.emplace_back(e, cnt); } if (ts > 1) sv.emplace_back(ts, 1); for (auto& e : pri) { if (e * e > tn) break; long long cnt = 0; while (tn % e == 0) { cnt++; tn /= e; } if (cnt) nv.emplace_back(e, cnt); } if (tn > 1) nv.emplace_back(tn, 1); ans = 0; rec(0, 1); sv.emplace_back(N, 1); vector<long long> tmp; long long i = 0; for (auto& e : nv) { while (sv[i].first < e.first) i++; if (sv[i].first > e.first) { tmp.emplace_back(e.first); } else { if (sv[i].second < e.second) tmp.emplace_back(pow(e.first, sv[i].second + 1)); i++; } } long long sz = tmp.size(); for (int sub = 0; sub < (1 << sz); ++sub) { long long t = 1; bool add = 1; for (int i = 0; i < (sz); ++i) if ((sub >> i) & 1) { t *= tmp[i]; add ^= 1; } if (add) ans += m / t; else ans -= m / t; } cout << ans << n ; } void init() { for (int i = 2; i <= (N - 1); ++i) if (!prime[i]) { pri.emplace_back(i); for (long long j = (long long)i * i; j < N; j += i) prime[j] = 1; } } int main(void) { ios::sync_with_stdio(false); cin.tie(0); cout.setf(ios::fixed); cout.precision(20); init(); int TC = 1; cin >> TC; for (int TI = 1; TI <= (TC); ++TI) { solve(); } return 0; }
|
#include <bits/stdc++.h> using namespace std; void test_case() { int n; long long k; cin >> n >> k; int l, r[2] = {-1, -1}; long long curr[2] = {k, k}, ans[2] = {0, 0}, monsters; for (int i = 0; i < n; i++) { int old_r[2] = {r[0], r[1]}, tmp_r; long long old_curr[2] = {curr[0], curr[1]}, old_ans[2] = {ans[0], ans[1]}; cin >> l >> tmp_r >> monsters; ans[0] = -1; for (int j = 0; j < 2; j++) { if (old_ans[j] == -1) continue; if (old_curr[j] + (tmp_r - l) * k < monsters) continue; if (ans[0] != -1 && ans[0] < old_ans[j] + monsters) continue; ans[0] = old_ans[j] + monsters; if (old_curr[j] >= monsters) { r[0] = l; curr[0] = old_curr[j] - monsters; } else { int turns = (monsters - old_curr[j] + k - 1) / k; r[0] = l + turns; curr[0] = k * turns + old_curr[j] - monsters; } } ans[1] = -1; for (int j = 0; j < 2; j++) { if (old_ans[j] == -1) continue; if ((tmp_r - l + 1) * k < monsters) continue; if (old_r[j] == l) continue; if (ans[1] != -1 && ans[1] <= old_ans[j] + old_curr[j] + monsters) continue; ans[1] = old_ans[j] + old_curr[j] + monsters; int turns = (monsters + k - 1) / k; r[1] = l + turns - 1; curr[1] = k * turns - monsters; } } if (ans[0] == -1) cout << ans[1] << n ; else if (ans[1] == -1) cout << ans[0] << n ; else cout << min(ans[0], ans[1]) << n ; } int main(void) { ios::sync_with_stdio(0); cin.tie(0); int Case = 1; for (int i = 0; i < Case; i++) { test_case(); } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; int vis[maxn]; vector<int> e[maxn]; vector<int> w[maxn]; int a[maxn]; int ans; void add_edge(int u, int v, int ww) { e[u].push_back(v); e[v].push_back(u); w[u].push_back(ww); w[v].push_back(ww); } void dfs(int num, int l, int c) { if (l > a[num] && c != 1) { c = 1; } if (c == 1) { ans++; } vis[num] = 1; int cc = e[num].size(); for (int i = 0; i < cc; i++) { if (!vis[e[num][i]]) { if (l < 0) l = 0; vis[e[num][i]] = 1; dfs(e[num][i], l + w[num][i], c); } } } int main() { memset(vis, 0, sizeof(vis)); int n; scanf( %d , &n); ans = 0; for (int i = 1; i <= n; i++) scanf( %d , &a[i]); for (int i = 2; i <= n; i++) { int u = i, v, ww; scanf( %d%d , &v, &ww); add_edge(u, v, ww); } ans = 0; dfs(1, 0, 0); printf( %d n , ans); }
|
#include <bits/stdc++.h> using namespace std; #define ll int #define vl vector<long long> #define fr(i,a,n) for(ll i=a; i<n; i++) #define all(x) x.begin(), x.end() int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll t; cin>>t; while(t--){ ll n, x=0, z[4]; cin>>n; for(ll i=0; i<4; i++) cin>>z[i]; for(ll i=0; i<=15; i++){ ll a=0, b=0, c=0, d=0; if(i&1) a++; if(i&2) b++; if(i&4) c++; if(i&8) d++; if(z[0]-a-b<=n-2 && z[0]>=a+b) x++; if(z[1]-c-b<=n-2 && z[1]>=c+b) x++; if(z[2]-c-d<=n-2 && z[2]>=c+d) x++; if(z[3]-a-d<=n-2 && z[3]>=a+d) x++; if(x==4) break; else x=0; } if(x==4) cout<< Yes n ; else cout<< No n ; } }
|
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; bool et[N]; int n; int mob[N]; vector<int> d[N]; int cnt[N]; bool get(int x) { int coprime = 0; for (auto p : d[x]) coprime += cnt[p] * mob[p]; return coprime > 0; } void update(int x, int val) { for (auto p : d[x]) cnt[p] += val; } int main() { for (int i = 1; i < N; i++) { for (int j = i; j < N; j += i) d[j].push_back(i); if (i == 1) mob[i] = 1; else if ((i / d[i][1]) % d[i][1] == 0) mob[i] = 0; else mob[i] = -mob[i / d[i][1]]; } cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; et[x] = 1; } for (int i = 1; i < N; i++) { for (auto x : d[i]) if (i != x) et[x] |= et[i]; } long long ans = 0; stack<int> s; for (int i = N - 1; i >= 1; i--) { if (et[i] == 0) continue; while (get(i) > 0) { ans = max(ans, 1LL * i * s.top()); update(s.top(), -1); s.pop(); } update(i, 1); s.push(i); } cout << ans; }
|
#include <bits/stdc++.h> using namespace std; int main() { int N, X, Y; cin >> N >> X >> Y; int p = ceil(Y / 100.0 * N); cout << max(p - X, 0) << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int main() { string s; int i, j, count_star, diff, res = 0, n; cin >> s; n = s.size(); for (i = 0; i < n; i++) { diff = count_star = 0; for (j = i; j < n; j++) { if (s[j] == ( ) diff++; else if (s[j] == ) ) diff--; else { count_star++; diff--; } if (diff < 0) { count_star--; if (count_star < 0) break; else diff += 2; } if (diff == 0 && (j - i + 1) % 2 == 0) res++; } } cout << res << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; int P; int g1() { double p = rand() / 32767.0, s = 0, g = 1; for (int k = 0;; k++) { if (k) g *= k; double c = pow(P, k) * exp(-P) / g; s += c; if (p <= s) return k; } } int g2() { double p = rand() / 32767.0, s = 0; for (int k = 0; k <= P + P; k++) { s += 1.0 / (2 * P + 1); if (p <= s) return k; } return P + P; } int main() { int v; scanf( %d , &v); while (v--) { vector<int> vs; for (int i = 0; i < 250; i++) { int x; scanf( %d , &x); vs.push_back(x); } double avr = 0, ss = 0; for (int i = 0; i < 250; i++) avr += (double)vs[i]; avr /= 250; int mx = 0; for (int i = 0; i < 250; i++) ss += (vs[i] - avr) * (vs[i] - avr), mx = max(mx, vs[i]); ss /= avr; if (ss < 500) printf( %d n , int(avr + 0.5)); else printf( %d n , int(mx / 2.0 + 0.5)); } }
|
#include <bits/stdc++.h> using namespace std; unsigned gcd(unsigned a, unsigned b) { unsigned r; do { r = a % b; a = b; b = r; } while (r > 0); return a; } unsigned arr[705]; bool dp[705][705][2]; bool en[705][705][2]; unsigned g[705][705]; bool f(int l, int r, int rm) { if (en[l][r][rm]) return dp[l][r][rm]; en[l][r][rm] = true; if (l == r) dp[l][r][rm] = g[l][l - 1 + 2 * rm] > 1; else { dp[l][r][rm] |= (g[l - 1 + rm * (r - l + 2)][l] > 1) && f(l + 1, r, 0); dp[l][r][rm] |= (g[l - 1 + rm * (r - l + 2)][r] > 1) && f(l, r - 1, 1); for (int curs = 1; curs < r - l; curs++) dp[l][r][rm] |= (g[l - 1 + rm * (r - l + 2)][l + curs] > 1) && f(l, l + curs - 1, 1) && f(l + curs + 1, r, 0); } return dp[l][r][rm]; } void init(int n) { for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) g[i][j] = gcd(arr[i], arr[j]); } int main() { int n; scanf( %d , &n); for (int i = 0; i < n; i++) scanf( %d , &arr[i]); init(n); if (f(1, n - 1, 0)) { printf( Yes n ); return 0; } if (f(0, n - 2, 1)) { printf( Yes n ); return 0; } for (int i = 1; i < n - 1; i++) { if (f(0, i - 1, 1) && f(i + 1, n - 1, 0)) { printf( Yes n ); return 0; } else continue; } printf( No n ); return 0; }
|
#include <bits/stdc++.h> using namespace std; void __dbg() { cout << endl; } template <typename Arg, typename... Args> void __dbg(Arg A, Args... B) { cout << << A; __dbg(B...); } const int inf = INT_MAX; const int ninf = INT_MIN; const int mod = 1e9 + 7; const int maxN = 1e6 + 2; int main() { int n; scanf( %d , &n); map<pair<int, int>, int> ma; vector<int> x(n), y(n); for (int i = 0; i < n; i++) { scanf( %d , &x[i]); scanf( %d , &y[i]); } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { int cx = x[i] + x[j]; int cy = y[i] + y[j]; ma[{cx, cy}]++; } } long long ans = 0; for (auto it : ma) { ans += it.second * 1LL * (it.second - 1) / 2; } printf( %lld n , ans); return 0; }
|
#include <bits/stdc++.h> using namespace std; int p[111]; int main() { int n, a = 0, b = 0, c = 0; cin >> n; for (int i = 1; i <= n; i++) { cin >> p[i]; if (i < 3 && i == 1 || i % 3 == 1) { a = a + p[i]; } else if (i < 3 && i == 2 || i % 3 == 2) { b = b + p[i]; } else if (i % 3 == 0) { c = c + p[i]; } } if (max(a, max(b, c)) == a) { cout << chest ; } else if (max(a, max(b, c)) == b) { cout << biceps ; } else if (max(a, max(b, c)) == c) { cout << back ; } }
|
#include <bits/stdc++.h> using namespace std; int main() { long long int t, n, l, r; cin >> t; while (t--) { cin >> n; r = n; l = (-1) * (n - 1); cout << l << << r << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; int main(void) { int n, m, v[100][100], x, y, cnt, ans = 0, dcnt = 1; vector<int> del; memset(v, 0, sizeof(v)); cin >> n >> m; for (int i = 0; i < m; i++) { cin >> x >> y; v[x - 1][y - 1] = 1; v[y - 1][x - 1] = 1; } while (dcnt != 0) { dcnt = 0; for (int i = 0; i < n; i++) { cnt = 0; for (int j = 0; j < n; j++) cnt += v[i][j]; if (cnt == 1) { dcnt++; del.push_back(i); for (int j = 0; j < n; j++) v[i][j] = 0; } } for (int i = 0; i < n; i++) { for (unsigned int j = 0; j < del.size(); j++) v[i][del[j]] = 0; } if (dcnt > 0) ans++; del.clear(); } cout << ans; return 0; }
|
#include <bits/stdc++.h> using namespace std; int N; long long dp[2 * 100000 + 10][2], arr[2 * 100000 + 10]; long long Solve(int, bool); int main() { scanf( %d , &N); dp[0][0] = dp[0][1] = dp[1][0] = dp[1][1] = -2; for (int i = 2; i <= N; i++) { scanf( %I64d , &arr[i]); dp[i][0] = dp[i][1] = -2; } for (int i = 2; i <= N; i++) Solve(i, 0), Solve(i, 1); for (int i = 1; i < N; i++) dp[1 + i][0] != -1 ? printf( %I64d n , dp[1 + i][0] + i) : printf( -1 n ); return 0; } long long Solve(int ind, bool f) { if (ind <= 0 || ind > N) return 0; if (dp[ind][f] == -3) return -1; if (dp[ind][f] != -2) return dp[ind][f]; dp[ind][f] = -3; long long ret, a; if (f) a = Solve(ind + arr[ind], 0); else a = Solve(ind - arr[ind], 1); if (a == -1) ret = -1; else ret = a + (long long)arr[ind]; return dp[ind][f] = ret; }
|
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long n, i, j; cin >> n; map<long long, long long> mp, cp; vector<long long> v(n); for (i = 0; i < n; i++) { cin >> v[i]; mp[v[i]]++; } map<long long, long long>::iterator it; for (it = mp.begin(); it != mp.end(); it++) { cp[it->second]++; } if (cp.find(1) == cp.end() || cp[1] % 2 == 0) { long long cnt = 0; cout << YES << endl; for (i = 0; i < n; i++) { if (mp[v[i]] == 1) { if (cnt < cp[1] / 2) { cout << A ; } else cout << B ; cnt++; } else cout << A ; } } else { if (cp[1] + 2 * cp[2] == n) { cout << NO ; } else { cout << YES << endl; long long cnt = 0, t = 1; for (i = 0; i < n; i++) { if (mp[v[i]] == 1) { if (cnt < (cp[1] + 1) / 2) { cout << A ; } else { cout << B ; } cnt++; } else if (mp[v[i]] == 2) { cout << A ; } else { if (t == 1) { cout << B ; t++; } else { cout << A ; } } } } } }
|
#include <bits/stdc++.h> using namespace std; int a[10][10], b[5010], c[51]; string s[110][110]; int main() { int maxx = 0; int summ = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cin >> a[i][j]; summ = summ + a[i][j]; } } a[1][1] = summ / 6; int x = a[1][1] + a[1][0] + a[1][2]; a[2][2] = abs(x - a[0][2] - a[1][2]); a[0][0] = abs(x - a[2][2] - a[1][1]); cout << endl; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cout << a[i][j] << ; } cout << endl; } return 0; }
|
#include <bits/stdc++.h> using namespace std; const int MOD = 1E9 + 9; int n, m, ans, cntNode(1), root(1), f[200], dp[1005][105][13], vis[1005][105][13]; string s; struct Trie { int son[4], fail, len; Trie() { for (int i = 0; i < 4; i++) son[i] = 0; fail = len = 0; } } tr[105]; void add(int& a, int b) { a = (a + b) % MOD; } void insert(string& s) { int cur = root; for (int i = 0; i < s.size(); i++) { if (!tr[cur].son[f[s[i]]]) tr[cur].son[f[s[i]]] = ++cntNode; cur = tr[cur].son[f[s[i]]]; } tr[cur].len = s.size(); } void getFail() { queue<int> Q; Q.push(root); tr[root].fail = root; while (!Q.empty()) { int now = Q.front(); Q.pop(); tr[now].len = max(tr[now].len, tr[tr[now].fail].len); for (int i = 0; i < 4; i++) { if (tr[now].son[i]) { tr[tr[now].son[i]].fail = now != root ? tr[tr[now].fail].son[i] : root; Q.push(tr[now].son[i]); } else tr[now].son[i] = tr[tr[now].fail].son[i] ? tr[tr[now].fail].son[i] : root; } } } void DP() { queue<tuple<int, int, int> > Q; dp[0][1][0] = 1; Q.push(make_tuple(0, 1, 0)); while (!Q.empty()) { int len = get<0>(Q.front()); int node = get<1>(Q.front()); int last = get<2>(Q.front()); Q.pop(); if (len == n) { if (!last) add(ans, dp[len][node][last]); continue; } vis[len][node][last] = 0; for (int i = 0; i < 4; i++) { int to = tr[node].son[i]; int tmp = tr[to].len > last ? 0 : last + 1; if (tmp > 10) continue; add(dp[len + 1][to][tmp], dp[len][node][last]); if (!vis[len + 1][to][tmp]) { vis[len + 1][to][tmp] = 1; Q.push(make_tuple(len + 1, to, tmp)); } } } return; } signed main() { ios::sync_with_stdio(false); cin >> n >> m; f[ A ] = 0, f[ T ] = 1, f[ C ] = 2, f[ G ] = 3; for (int i = 1; i <= m; i++) cin >> s, insert(s); getFail(); DP(); cout << ans; return 0; }
|
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> T pow_mod(T a, U b, int mod) { T r = 1; for (; b > 0; b >>= 1) { if (b & 1) r = (long long)r * a % mod; a = (long long)a * a % mod; } return r; } namespace combinatorics { const int mod = 1e9 + 7, N = 2e3 + 5; int fac[N], ifac[N]; void init() { fac[0] = ifac[0] = 1; for (int i = 1; i < N; ++i) { fac[i] = (long long)fac[i - 1] * i % mod; ifac[i] = pow_mod(fac[i], mod - 2, mod); } } int comb(int n, int k) { return (long long)fac[n] * ifac[n - k] % mod * ifac[k] % mod; } } // namespace combinatorics using namespace combinatorics; int main() { ios_base::sync_with_stdio(0), cin.tie(0); init(); int t = 1; while (t--) { int n, x, p; cin >> n >> x >> p; vector<int> a(n), c(2); a[p] = 1; auto f = [&]() { int l = 0, r = n; while (l < r) { int m = (l + r) >> 1; if (m <= p) { c[0] += a[m] == 0; a[m] = -1; l = m + 1; } else { c[1] += a[m] == 0; a[m] = +1; r = m; } } }; f(); int ans = 0; if (n - x >= c[1] && x - 1 >= c[0]) ans = (long long)comb(n - x, c[1]) * comb(x - 1, c[0]) % mod * fac[n - 1 - c[0] - c[1]] % mod * fac[c[0]] % mod * fac[c[1]] % mod; cout << ans << n ; } return 0; }
|
#include <bits/stdc++.h> using namespace std; char opt; long long a, b, c, ansa, ansb, ansc, anslen = 15; void dfs(long long a, long long b, long long c, long long nowa, long long nowb, int pre, long long len, long long step) { if (len >= anslen) return; if (!a && !b && !c && !pre) { ansa = nowa, ansb = nowb, anslen = len; return; } if (!c) { long long tmp = a + b + pre; int cnt = 0; while (tmp) cnt++, tmp /= 10; dfs(0, 0, 0, nowa + step * a, nowb + step * b, 0, len + cnt, step); return; } if ((a + b + pre) % 10 == c % 10) { dfs(a / 10, b / 10, c / 10, nowa + step * (a % 10), nowb + step * (b % 10), (a % 10 + b % 10 + pre) / 10, len, step * 10); return; } dfs(a * 10 + (c + 10 - b % 10 - pre) % 10, b, c, nowa, nowb, pre, len + 1, step); dfs(a, b * 10 + (c + 10 - a % 10 - pre) % 10, c, nowa, nowb, pre, len + 1, step); dfs(a, b, c * 10 + (a + b + pre) % 10, nowa, nowb, pre, len + 1, step); } int main() { cin >> a >> opt >> b >> opt >> c; dfs(a, b, c, 0, 0, 0, 0, 1); cout << ansa << + << ansb << = << ansa + ansb << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; constexpr int mod = 998244353; int x[5], y[5]; int px[32], nx[32], py[32], ny[32]; int dp[32][20][20][20][20][2][2]; void add(int& x, const int& y) { x += y; if (x >= mod) x -= mod; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n, m; cin >> n >> m; for (int i = 0; i < n; i += 1) cin >> x[i] >> y[i]; for (int mask = 0; mask < (1 << n); mask += 1) for (int i = 0; i < n; i += 1) if ((mask >> i) & 1) { (x[i] > 0 ? px : nx)[mask] += abs(x[i]); (y[i] > 0 ? py : ny)[mask] += abs(y[i]); } dp[0][0][0][0][0][0][0] = 1; for (int i = 0; i < (30); i += 1) for (int cpx = 0; cpx < (20); cpx += 1) for (int cnx = 0; cnx < (20); cnx += 1) for (int cpy = 0; cpy < (20); cpy += 1) for (int cny = 0; cny < (20); cny += 1) for (int bx = 0; bx < (2); bx += 1) for (int by = 0; by < (2); by += 1) for (int mask = 0; mask < (1 << n); mask += 1) if (dp[i][cpx][cnx][cpy][cny][bx][by]) { if (0) cout << i << << cpx << << cny << << cpy << << cny << << bx << << by << << dp[i][cpx][cnx][cpy][cny][bx][by] << endl; int npx = cpx + px[mask], nnx = cnx + nx[mask], npy = cpy + py[mask], nny = cny + ny[mask]; if ((npx ^ nnx) & 1) continue; if ((npy ^ nny) & 1) continue; int cx = npx & 1, mx = (m >> i) & 1; int cy = npy & 1, my = (m >> i) & 1; add(dp[i + 1][npx >> 1][nnx >> 1][npy >> 1][nny >> 1] [cx != mx ? (cx > mx) : bx] [cy != my ? (cy > my) : by], dp[i][cpx][cnx][cpy][cny][bx][by]); } cout << (dp[30][0][0][0][0][0][0] + mod - 1) % mod; return 0; }
|
#include <bits/stdc++.h> using namespace std; bool qo(bool x) { return (x ? 0 : qo(!x)); } const int mnx = 1e5 + 9; const int mod = 1e9 + 7; long long n, sum; long long a[mnx]; int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int m = 0; m < (1 << n); m++) { for (int j = 0; j < n; j++) { if (m & (1 << j)) sum += a[j]; else sum -= a[j]; } if (sum % 360 == 0) { cout << YES n ; exit(0); ; } sum = 0; } cout << NO n ; exit(0); ; }
|
#include <bits/stdc++.h> #pragma comment(linker, /STACK:256000000 ) using namespace std; const int INF = 1000001000; const int mod = 1000 * 1000 * 1000 + 7; const int mod9 = 1000 * 1000 * 1000 + 9; const double PI = 3.1415926535897932; double sqr(double first) { return first * first; }; long double sqr(long double first) { return first * first; }; long long sqr(long long first) { return first * first; }; long long sqr(int first) { return first * 1LL * first; }; long long gcd(long long a, long long b) { while (b) a %= b, swap(a, b); return a; } long long bpm(long long a, long long n = -2, long long m = mod) { n = n >= 0 ? n : m + n; long long r = 1; while (n) { if (n & 1) r = (r * a) % m; a = (a * a) % m; n >>= 1; } return r; } long long dist(long long x1, long long y1, long long x2, long long y2) { return sqr(x1 - x2) + sqr(y1 - y2); } std::ostream& operator<<(std::ostream& os, pair<int, int> p) { return os << { << p.first << << p.second << } ; } const int N = 200005; long long a[N]; pair<long long, long long> b[N]; int main() { ios::sync_with_stdio(false); long long n, m, s, d; cin >> n >> m >> s >> d; for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); int nwn = -1; for (int i = 0; i < n; i++) { b[i] = {a[i], a[i]}; if (i && a[i] - a[i - 1] < s + 2) { b[nwn].second = b[i].first; } else { b[++nwn] = b[i]; } } n = nwn + 1; vector<pair<long long, long long> > res; long long pos = 0; for (int i = 0; i < n; i++) { long long next = b[i].first; long long runD = next - 1 - pos; bool err = false; if (runD < s) { err = true; } res.push_back({1, runD}); pos += runD; long long fin = b[i].second + 1; if (fin > m || fin - pos > d) { err = true; } res.push_back({0, fin - pos}); pos = fin; if (err) { pos = m; res.clear(); res.push_back({-1, 0}); break; } } if (pos != m) { res.push_back({1, m - pos}); } for (pair<int, int> d : res) { switch (d.first) { case -1: cout << IMPOSSIBLE n ; break; case 0: cout << JUMP << d.second << n ; break; case 1: cout << RUN << d.second << n ; break; } } return 0; }
|
#include <bits/stdc++.h> const double EPS = 1e-10; const double PI = acos(-1.0); using namespace std; int main(void) { int n; cin >> n; vector<pair<int, int> > e; for (int i = (0); i < (n); ++i) { int a, b; cin >> a >> b; e.push_back(make_pair(a, b)); } sort((e).begin(), (e).end()); int answer = 0; for (int i = (0); i < (n); ++i) { if (answer <= e[i].second) answer = e[i].second; else answer = e[i].first; } cout << answer << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; char field[50][50]; bool used[50][50]; int n, m, k; int counts[26]; int cur_set; set<string> min_ways; int min_len = INT_MAX; int start_x, start_y, end_x, end_y; vector<char> alpha(0); bool comp(pair<int, int> a, pair<int, int> b) { if (field[a.first][a.second] == T ) return true; if (field[b.first][b.second] == T ) return false; return field[a.first][a.second] < field[b.first][b.second]; } vector<pair<int, int>> find_near(int from_y, int from_x, const set<char> &from_set) { vector<pair<int, int>> near(0); if (from_y > 0 && !used[from_y - 1][from_x] && from_set.find(field[from_y - 1][from_x]) != from_set.end()) near.push_back(make_pair(from_y - 1, from_x)); if (from_y < n - 1 && !used[from_y + 1][from_x] && from_set.find(field[from_y + 1][from_x]) != from_set.end()) near.push_back(make_pair(from_y + 1, from_x)); if (from_x > 0 && !used[from_y][from_x - 1] && from_set.find(field[from_y][from_x - 1]) != from_set.end()) near.push_back(make_pair(from_y, from_x - 1)); if (from_x < m - 1 && !used[from_y][from_x + 1] && from_set.find(field[from_y][from_x + 1]) != from_set.end()) near.push_back(make_pair(from_y, from_x + 1)); sort(near.begin(), near.end(), comp); return near; } set<char> find_set_near(int from_y, int from_x) { set<char> near = set<char>(); if (from_y > 0 && !used[from_y - 1][from_x]) near.insert(field[from_y - 1][from_x]); if (from_y < n - 1 && !used[from_y + 1][from_x]) near.insert(field[from_y + 1][from_x]); if (from_x > 0 && !used[from_y][from_x - 1]) near.insert(field[from_y][from_x - 1]); if (from_x < m - 1 && !used[from_y][from_x + 1]) near.insert(field[from_y][from_x + 1]); near.erase( T ); near.erase( S ); return near; } set<char> find_second_near_set(int from_y, int from_x, set<char> ¬_from) { bool l2, l1, r1, r2, u1, u2, d1, d2; l2 = from_x - 2 >= 0; l1 = from_x > 0; r1 = from_x + 2 < m; r2 = from_x + 1 < m; u2 = from_y - 2 >= 0; u1 = from_y > 0; d1 = from_y + 1 < n; d2 = from_y + 2 < n; int yu2 = from_y - 2; int yu1 = from_y - 1; int yd1 = from_y + 1; int yd2 = from_y + 2; int xl2 = from_x - 2; int xl1 = from_x - 1; int xr1 = from_x + 1; int xr2 = from_x + 2; set<char> ret = set<char>(); if (l1 && u2) ret.insert(field[yu2][xl1]); if (u2) ret.insert(field[yu2][from_x]); if (u2 && r1) ret.insert(field[yu2][xr1]); if (r2) { if (u1) ret.insert(field[yu1][xr2]); ret.insert(field[from_y][xr2]); if (d1) ret.insert(field[yd1][xr2]); } if (d2) { if (r1) ret.insert(field[yd2][xr1]); ret.insert(field[yd2][from_x]); if (l1) ret.insert(field[yd2][xl1]); } if (l2) { if (d1) ret.insert(field[yd1][xl2]); ret.insert(field[from_y][xl2]); if (u1) ret.insert(field[yu1][xl2]); } for (set<char>::iterator i = not_from.begin(); i != not_from.end(); i++) ret.erase(*i); return ret; } void read() { set<char> set_alpha = set<char>(); cin >> n >> m >> k; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { cin >> field[i][j]; if (field[i][j] == S ) { start_y = i; start_x = j; } else if (field[i][j] == T ) { end_y = i; end_x = j; } else set_alpha.insert(field[i][j]); } for (set<char>::iterator i = set_alpha.begin(); i != set_alpha.end(); i++) alpha.push_back(*i); } struct cell { pair<int, int> pos; string s; }; struct comp2 { bool operator()(const cell &a, const cell &b) { if (a.s.size() != b.s.size()) return a.s.size() < b.s.size(); else return a.s < b.s; } }; set<set<char>> last_sets; bool wave(set<char> &cur_set) { if (last_sets.find(cur_set) != last_sets.end()) return false; else last_sets.insert(cur_set); memset(used, false, 2500); used[start_y][start_x] = true; cur_set.insert( T ); multiset<cell, comp2> myque; cell tmp; tmp.pos = make_pair(start_y, start_x); tmp.s = string( ); myque.insert(tmp); while (!myque.empty()) { cell cur_cell = *myque.begin(); myque.erase(myque.begin()); if (cur_cell.s.length() > min_len) break; vector<pair<int, int>> near = find_near(cur_cell.pos.first, cur_cell.pos.second, cur_set); for (vector<pair<int, int>>::iterator i = near.begin(); i != near.end(); i++) { if (field[i->first][i->second] == T ) { if (cur_cell.s.length() < min_len) { min_ways.clear(); min_len = cur_cell.s.length(); } min_ways.insert(cur_cell.s); break; } used[i->first][i->second] = true; cell tmp = cur_cell; tmp.pos = *i; tmp.s.push_back(field[i->first][i->second]); myque.insert(tmp); } } cur_set.erase( T ); return false; } int main() { read(); string s( ); set<char> start_set = find_set_near(start_y, start_x); set<char> end_set = find_set_near(end_y, end_x); int l = alpha.size(); set<char> cur_set = set<char>(); if (!l) wave(cur_set); else { for (set<char>::iterator a = start_set.begin(); a != start_set.end(); a++) { cur_set.insert(*a); if (cur_set.size() == k || l < k && cur_set.size() == l) wave(cur_set); else { for (set<char>::iterator b = end_set.begin(); b != end_set.end(); b++) { cur_set.insert(*b); if (cur_set.size() == k || l < k && cur_set.size() == l) wave(cur_set); else { for (int i = 0; i < l; i++) { if (cur_set.end() != cur_set.find(alpha[i])) continue; cur_set.insert(alpha[i]); if (cur_set.size() == k || l < k && cur_set.size() == l) wave(cur_set); else { for (int j = i + 1; j < l; j++) { if (cur_set.end() != cur_set.find(alpha[j])) continue; cur_set.insert(alpha[j]); if ((*a) == (*b) && cur_set.size() < k) { set<char> second_set = find_second_near_set(start_y, start_x, cur_set); if (!second_set.size()) wave(cur_set); else { for (set<char>::iterator p = second_set.begin(); p != second_set.end(); p++) { cur_set.insert(*p); wave(cur_set); cur_set.erase(*p); } } } else wave(cur_set); cur_set.erase(alpha[j]); } } cur_set.erase(alpha[i]); } } cur_set.erase(*b); } } cur_set.erase(*a); } } label_end: if (min_len == INT_MAX) cout << -1 ; else cout << (*min_ways.begin()); }
|
#include <bits/stdc++.h> using namespace std; vector<long long> AE[1000005]; long long par[1000005]; enum LG { AND = 1, OR = 2, XOR = 3, NOT = 4, IN = 5 }; long long type[1000005], inp[1000005]; inline void nedge(long long a, long long b) { AE[a].push_back(b), par[b] = a; } bool oans[1000005]; bool dfs(long long p) { if (AE[p].size()) { volatile bool A = dfs(AE[p][0]), B = AE[p].size() > 1 ? dfs(AE[p][1]) : 0; if (type[p] == AND) oans[p] = (A and B); else if (type[p] == OR) oans[p] = (A or B); else if (type[p] == XOR) oans[p] = (A xor B); else if (type[p] == NOT) oans[p] = (not A); } else oans[p] = inp[p]; return oans[p]; } bool ans[1000005]; int32_t main() { ios::sync_with_stdio(0); cin.tie(0); ; long long n; cin >> n; for (long long i = 1; i <= n; i++) { string x; cin >> x; long long a, b; if (x == AND ) cin >> a >> b, nedge(i, a), nedge(i, b), type[i] = AND; if (x == OR ) cin >> a >> b, nedge(i, a), nedge(i, b), type[i] = OR; if (x == XOR ) cin >> a >> b, nedge(i, a), nedge(i, b), type[i] = XOR; if (x == NOT ) cin >> a, nedge(i, a), type[i] = NOT; if (x == IN ) cin >> a, inp[i] = a, type[i] = IN; } dfs(1); queue<long long> q; q.push(1); while (!q.empty()) { long long p = q.front(), pa = par[p]; q.pop(); bool nans = 0; if (p == 1) ans[p] = !oans[p]; else { long long a = AE[pa][0], b = AE[pa].size() > 1 ? AE[pa][1] : -1; if (type[pa] == AND) nans = ((oans[a] ^ (p == a)) and (oans[b] ^ (p == b))); else if (type[pa] == OR) nans = ((oans[a] ^ (p == a)) or (oans[b] ^ (p == b))); else if (type[pa] == XOR) nans = ((oans[a] ^ (p == a)) xor (oans[b] ^ (p == b))); else if (type[pa] == NOT) nans = (not(oans[a] ^ (p == a))); ans[p] = (nans != oans[pa]) ? ans[pa] : oans[1]; } for (long long x : AE[p]) q.push(x); } for (long long i = 1; i <= n; i++) if (type[i] == IN) cout << ans[i]; cout << n ; }
|
#include <bits/stdc++.h> using namespace std; struct osoba { string ime, prezime; }; int main() { int n; cin >> n; vector<osoba> osobe(n); vector<int> redoslijed(n); vector<string> minori(n); bool trigger = true; for (int i = 0; i < n; i++) cin >> osobe[i].ime >> osobe[i].prezime; for (int i = 0; i < n; i++) cin >> redoslijed[i]; if (strcmp(osobe[redoslijed[0] - 1].ime.c_str(), osobe[redoslijed[0] - 1].prezime.c_str()) < 0) minori[0] = osobe[redoslijed[0] - 1].ime; else minori[0] = osobe[redoslijed[0] - 1].prezime; for (int i = 1; i < n; i++) { if (strcmp(minori[i - 1].c_str(), osobe[redoslijed[i] - 1].ime.c_str()) > 0 && strcmp(minori[i - 1].c_str(), osobe[redoslijed[i] - 1].prezime.c_str()) <= 0) minori[i] = osobe[redoslijed[i] - 1].prezime; else if (strcmp(minori[i - 1].c_str(), osobe[redoslijed[i] - 1].ime.c_str()) <= 0 && strcmp(minori[i - 1].c_str(), osobe[redoslijed[i] - 1].prezime.c_str()) > 0) minori[i] = osobe[redoslijed[i] - 1].ime; else if (strcmp(minori[i - 1].c_str(), osobe[redoslijed[i] - 1].ime.c_str()) <= 0 && strcmp(minori[i - 1].c_str(), osobe[redoslijed[i] - 1].prezime.c_str()) <= 0) { if (strcmp(osobe[redoslijed[i] - 1].ime.c_str(), osobe[redoslijed[i] - 1].prezime.c_str()) < 0) minori[i] = osobe[redoslijed[i] - 1].ime; else minori[i] = osobe[redoslijed[i] - 1].prezime; } else { cout << NO << endl; trigger = false; break; } } if (trigger) cout << YES << endl; return 0; }
|
#include <bits/stdc++.h> using namespace std; void CyBerForCe() { long long n, k, x; cin >> n >> k >> x; vector<long long> v, u1, u2; long long f[1024] = {0}, temp[1024]; for (long long i = 0; i < n; i++) { long long a; cin >> a; v.push_back(a); f[a]++; } for (long long i = 0; i < k; i++) { for (long long j = 0; j < 1024; j++) temp[j] = 0; long long c = 0; for (long long j = 0; j < 1024; j++) { temp[j] += ((f[j] + c) / 2); temp[j ^ x] += ((f[j] + 1 - c) / 2); c = (c + f[j]) & 1; } for (long long j = 0; j < 1024; j++) f[j] = temp[j]; } long long mi = 1026, ma = -1; for (long long i = 0; i < 1024; i++) { if (f[i] > 0) { mi = min(mi, i); ma = max(ma, i); } } cout << ma << << mi << n ; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); CyBerForCe(); return 0; }
|
#include <bits/stdc++.h> using namespace std; const int N = 2e3 + 2; bitset<N> equa[N], value[N]; int ans[N], hehe[N]; signed main() { ios::sync_with_stdio(0); cin.tie(0); int n, m, i, j, k, l, num = 0; cin >> n >> m; for (i = 0; i < n; i++) { value[2 * i][2 * i] = 1; value[2 * i + 1][2 * i + 1] = 1; } for (i = 0; i < m; i++) { string s; char x; cin >> s; if (s == mix ) { cin >> j; for (k = 0; k < j; k++) { cin >> l; l--; equa[num] ^= value[2 * l]; equa[num + 1] ^= value[2 * l + 1]; } cin >> x; if (x == R ) { ans[num + 1] = 1; } if (x == Y ) { ans[num] = 1; } if (x == B ) { ans[num] = ans[num + 1] = 1; } num += 2; } if (s == RB ) { cin >> j; for (k = 0; k < j; k++) { cin >> l; l--; value[2 * l] ^= value[2 * l + 1]; } } if (s == RY ) { cin >> j; for (k = 0; k < j; k++) { cin >> l; l--; swap(value[2 * n], value[2 * l]); swap(value[2 * l], value[2 * l + 1]); swap(value[2 * l + 1], value[2 * n]); } } if (s == YB ) { cin >> j; for (k = 0; k < j; k++) { cin >> l; l--; value[2 * l + 1] ^= value[2 * l]; } } } int now = -1; for (i = 0; i < 2 * n; i++) { for (j = now + 1; j < num; j++) { if (equa[j][i]) { now++; swap(equa[j], equa[now]); swap(ans[j], ans[now]); for (k = 0; k < num; k++) { if (equa[k][i] && k != now) { equa[k] ^= equa[now]; ans[k] ^= ans[now]; } } break; } } } for (i = now + 1; i < num; i++) { assert(!equa[i].count()); if (ans[i] == 1) { cout << NO ; return 0; } } for (i = now; i > -1; i--) { for (j = 2 * n - 1; j > -1; j--) { if (equa[i][j]) { if (equa[i].count() == 1) { equa[i][j] = 0; hehe[j] = ans[i]; } else { equa[i][j] = 0; for (k = j; k > -1; k--) { if (equa[i][k]) { break; } } for (l = 0; l < i; l++) { if (equa[l][k]) { equa[l] ^= equa[i]; ans[l] ^= ans[i]; } } } } } } cout << YES n ; for (i = 0; i < n; i++) { if (hehe[2 * i] && hehe[2 * i + 1]) { cout << B ; } if (hehe[2 * i] && !hehe[2 * i + 1]) { cout << Y ; } if (!hehe[2 * i] && hehe[2 * i + 1]) { cout << R ; } if (!hehe[2 * i] && !hehe[2 * i + 1]) { cout << . ; } } }
|
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; const long long LINF = 0x3f3f3f3f3f3f3f3fLL; const double EPS = 1e-8; const int MOD = 998244353; const int dy[] = {1, 0, -1, 0}, dx[] = {0, -1, 0, 1}; struct IOSetup { IOSetup() { cin.tie(nullptr); ios_base::sync_with_stdio(false); cout << fixed << setprecision(20); cerr << fixed << setprecision(10); } } iosetup; int mod = MOD; struct ModInt { unsigned val; ModInt() : val(0) {} ModInt(long long x) : val(x >= 0 ? x % mod : x % mod + mod) {} ModInt pow(long long exponent) { ModInt tmp = *this, res = 1; while (exponent > 0) { if (exponent & 1) res *= tmp; tmp *= tmp; exponent >>= 1; } return res; } ModInt &operator+=(const ModInt &rhs) { if ((val += rhs.val) >= mod) val -= mod; return *this; } ModInt &operator-=(const ModInt &rhs) { if ((val += mod - rhs.val) >= mod) val -= mod; return *this; } ModInt &operator*=(const ModInt &rhs) { val = static_cast<unsigned long long>(val) * rhs.val % mod; return *this; } ModInt &operator/=(const ModInt &rhs) { return *this *= rhs.inv(); } bool operator==(const ModInt &rhs) const { return val == rhs.val; } bool operator!=(const ModInt &rhs) const { return val != rhs.val; } bool operator<(const ModInt &rhs) const { return val < rhs.val; } bool operator<=(const ModInt &rhs) const { return val <= rhs.val; } bool operator>(const ModInt &rhs) const { return val > rhs.val; } bool operator>=(const ModInt &rhs) const { return val >= rhs.val; } ModInt &operator++() { if (++val == mod) val = 0; return *this; } ModInt operator++(int) { ModInt res = *this; ++*this; return res; } ModInt &operator--() { val = (val == 0 ? mod : val) - 1; return *this; } ModInt operator--(int) { ModInt res = *this; --*this; return res; } ModInt operator+() const { return *this; } ModInt operator-() const { return ModInt(val ? mod - val : 0); } ModInt operator+(const ModInt &rhs) const { return ModInt(*this) += rhs; } ModInt operator-(const ModInt &rhs) const { return ModInt(*this) -= rhs; } ModInt operator*(const ModInt &rhs) const { return ModInt(*this) *= rhs; } ModInt operator/(const ModInt &rhs) const { return ModInt(*this) /= rhs; } friend ostream &operator<<(ostream &os, const ModInt &rhs) { return os << rhs.val; } friend istream &operator>>(istream &is, ModInt &rhs) { long long x; is >> x; rhs = ModInt(x); return is; } private: ModInt inv() const { unsigned a = val, b = mod; int x = 1, y = 0; while (b) { unsigned tmp = a / b; swap(a -= tmp * b, b); swap(x -= tmp * y, y); } return ModInt(x); } }; ModInt abs(const ModInt &x) { return x; } struct Combinatorics { int val; vector<ModInt> fact, fact_inv, inv; Combinatorics(int val = 10000000) : val(val), fact(val + 1), fact_inv(val + 1), inv(val + 1) { fact[0] = 1; for (int i = (1); i < (val + 1); ++i) fact[i] = fact[i - 1] * i; fact_inv[val] = ModInt(1) / fact[val]; for (int i = val; i > 0; --i) fact_inv[i - 1] = fact_inv[i] * i; for (int i = (1); i < (val + 1); ++i) inv[i] = fact[i - 1] * fact_inv[i]; } ModInt nCk(int n, int k) { if (n < 0 || n < k || k < 0) return ModInt(0); return fact[n] * fact_inv[k] * fact_inv[n - k]; } ModInt nPk(int n, int k) { if (n < 0 || n < k || k < 0) return ModInt(0); return fact[n] * fact_inv[n - k]; } ModInt nHk(int n, int k) { if (n < 0 || k < 0) return ModInt(0); return (k == 0 ? ModInt(1) : nCk(n + k - 1, k)); } }; int main() { int n, m, k; cin >> n >> m >> k; vector<vector<ModInt> > dp(k + 1, vector<ModInt>(k + 1, 0)); dp[0][0] = 1; for (int i = (0); i < (k); ++i) for (int j = (0); j < (i + 1); ++j) { dp[i + 1][j] += dp[i][j] * j; dp[i + 1][j + 1] += dp[i][j] * (n - j); } ModInt ans = 0; for (int j = (0); j < (k + 1); ++j) ans += dp[k][j] / ModInt(m).pow(j); cout << ans << n ; return 0; }
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.