func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int x, y; cin >> x >> y; int ans = 0; int d = max(abs(x), abs(y)); if (d == 0) { cout << 0 << n ; return 0; } else ans = (d - 1) * 4; if (x == d && y != -1 * (d - 1) && y != -1 * (d)) ans++; if (y == d && x != d) ans += 2; if (x == -1 * d && y != d) ans += 3; if (y == -1 * d && x != -1 * d) ans += 4; cout << ans << n ; }
#include <bits/stdc++.h> using namespace std; struct Aho { Aho *son[26]; Aho *fail; int cntstr, faildp; Aho() { for (int i = 0; i < 26; ++i) son[i] = NULL; fail = NULL; cntstr = 0; faildp = -1; } }; void insert(Aho *x, int i, const string &str) { if (i < str.size()) { if (x->son[str[i] - a ] == NULL) { x->son[str[i] - a ] = new Aho(); } insert(x->son[str[i] - a ], i + 1, str); } else { x->cntstr++; } } void calcFailEdges(Aho *root) { deque<Aho *> q; root->fail = root; q.push_back(root); while (!q.empty()) { Aho *x = q.front(); q.pop_front(); for (int i = 0; i < 26; ++i) { if (x->son[i] != NULL) { q.push_back(x->son[i]); Aho *pnt = x->fail; if (x == root) x->son[i]->fail = root; else { while (pnt != root && pnt->son[i] == NULL) { pnt = pnt->fail; } if (pnt->son[i] != NULL) x->son[i]->fail = pnt->son[i]; else x->son[i]->fail = root; } } } } } int getDp(Aho *t) { if (t->faildp == -1) { t->faildp = t->cntstr; t->faildp += getDp(t->fail); } return t->faildp; } const int MAX_N = 200000; Aho *state[1 + MAX_N]; Aho *advance(Aho *t, char ch) { while (t->fail != t && t->son[ch - a ] == NULL) t = t->fail; if (t->son[ch - a ] == NULL) return t; return t->son[ch - a ]; } Aho *state1[MAX_N], *state2[MAX_N]; void doStuff(int n, Aho **state, const string &str, const vector<string> &vec) { Aho *T = new Aho(); for (int i = 0; i < n; ++i) insert(T, 0, vec[i]); T->faildp = T->cntstr; calcFailEdges(T); Aho *pnt = T; for (int i = 1; i <= str.size(); ++i) { pnt = advance(pnt, str[i - 1]); state[i] = pnt; } } int main() { int n; string str; cin >> str >> n; vector<string> vec; for (int i = 0; i < n; ++i) { string p; cin >> p; vec.push_back(p); } doStuff(n, state1, str, vec); for (int i = 0; i < n; ++i) reverse(vec[i].begin(), vec[i].end()); reverse(str.begin(), str.end()); doStuff(n, state2, str, vec); long long rez = 0LL; for (int i = 1; i < str.size(); ++i) rez = (long long)getDp(state1[i]) * getDp(state2[str.size() - i]) + rez; printf( %lld , rez); return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int MOD = 1e9 + 7; const int base = 101; inline void chmax(int& x, int y) { if (x < y) x = y; } inline void chmin(int& x, int y) { if (x > y) x = y; } inline int LSB(int i) { return (i & -i); } int n, m, dp[10005]; char s[10005], str[10005]; map<long long, string> mp; vector<string> v; inline long long Hash(int a, int b) { return (1LL * a * base + b) % MOD; } int main() { scanf( %d%s%d , &n, &s, &m); for (int i = 0; i < m; i++) { scanf( %s , &str); long long h = 1; for (int j = 0; j < strlen(str); j++) { h = Hash(h, tolower(str[j])); } mp[h] = str; } fill(dp, dp + n, -1); dp[0] = 0; for (int i = 0; i < n; i++) { long long h = 1; for (int j = i; j >= 0; j--) { h = Hash(h, s[j]); if (dp[j] != -1 && mp.find(h) != mp.end()) { dp[i + 1] = h; break; } } } int p = n; while (p > 0) { v.push_back(mp[dp[p]]); p -= mp[dp[p]].length(); } reverse(v.begin(), v.end()); for (string& i : v) printf( %s , &i[0]); return 0; }
#include <bits/stdc++.h> using namespace std; const long long p = 1e9 + 7; int mk[705][705][3][3]; char s[705]; long long ck(int l, int r, int lc, int rc) { if (l > r) return !lc || !rc || lc != rc; if (mk[l][r][lc][rc]) return mk[l][r][lc][rc]; for (int i = l, cnt = 0;; i++) { if (s[i] == ) ) cnt++; else cnt--; if (!cnt) { long long m = ck(l + 1, i - 1, 0, 1) * ck(i + 1, r, 1, rc) % p + ck(l + 1, i - 1, 0, 2) * ck(i + 1, r, 2, rc); if (lc != 1) m += ck(l + 1, i - 1, 1, 0) * ck(i + 1, r, 0, rc); if (lc != 2) m += ck(l + 1, i - 1, 2, 0) * ck(i + 1, r, 0, rc); return mk[l][r][lc][rc] = m % p; } } } int main() { scanf( %s , s + 1); printf( %lld , ck(1, strlen(s + 1), 0, 0)); }
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) #pragma GCC target( avx,avx2,fma ) using namespace std; using ll = long long; using ull = unsigned long long; template <typename T> void print(vector<T> v) { for (T i : v) cout << i << ; cout << n ; } template <typename T> void print(vector<vector<T>>& v) { for (vector<T>& vv : v) { for (T& i : vv) cout << i << ; cout << n ; } } template <typename T> void print(T&& t) { cout << t << n ; } template <typename T, typename... Args> void print(T&& t, Args&&... args) { cout << t << ; print(forward<Args>(args)...); } template <typename T> istream& operator>>(istream& is, vector<T>& v) { for (auto& i : v) is >> i; return is; } template <typename T> ostream& operator<<(ostream& os, vector<T>& v) { for (auto& i : v) os << i << ; return os; } void __print(int x) { cerr << x; } void __print(long x) { cerr << x; } void __print(long long x) { cerr << x; } void __print(unsigned x) { cerr << x; } void __print(unsigned long x) { cerr << x; } void __print(unsigned long long x) { cerr << x; } void __print(float x) { cerr << x; } void __print(double x) { cerr << x; } void __print(long double x) { cerr << x; } void __print(char x) { cerr << << x << ; } void __print(const char* x) { cerr << << x << ; } void __print(const string& x) { cerr << << x << ; } void __print(bool x) { cerr << (x ? true : false ); } template <typename T, typename V> void __print(const pair<T, V>& x) { cerr << { ; __print(x.first); cerr << , ; __print(x.second); cerr << } ; } template <typename T> void __print(const T& x) { int f = 0; cerr << { ; for (auto& i : x) cerr << (f++ ? , : ), __print(i); cerr << } ; } void _print() { cerr << ] n ; } template <typename T, typename... V> void _print(T t, V... v) { __print(t); if (sizeof...(v)) cerr << , ; _print(v...); } void loop() { ll n; cin >> n; vector<ll> a(n); cin >> a; if (n == 1) { print( Alice ); return; } vector<ll> inc, dec; inc.push_back(a[0]); for (ll i = 0; i < n - 1; i++) { if (a[i] < a[i + 1]) { inc.push_back(a[i + 1]); } else break; } dec.push_back(a[n - 1]); for (ll i = n - 1; i > 0; i--) { if (a[i] < a[i - 1]) { dec.push_back(a[i - 1]); } else break; } if (dec.size() % 2) { print( Alice ); return; } else if ((inc.size() - 1 + dec.size()) % 2 == 0) { print( Alice ); return; } print( Bob ); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll t; t = 1; while (t--) { loop(); } }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; const int INF = 0x3f3f3f3f; int n, m, a[maxn], b[maxn], f[maxn], g[maxn], l[maxn], p[maxn], ans[maxn]; bool vis[maxn]; int main() { scanf( %d , &n); for (int i = (1), _end_ = (n); i <= _end_; ++i) scanf( %d , &a[i]); a[++n] = INF; scanf( %d , &m); for (int i = (1), _end_ = (m); i <= _end_; ++i) scanf( %d , &b[i]); sort(b + 1, b + m + 1); for (int i = (1), _end_ = (n + 1); i <= _end_; ++i) f[i] = INF; for (int i = (1), _end_ = (n); i <= _end_; ++i) if (a[i] != -1) { int L = 1, R = n, pos = 0; while (L <= R) { int Mid = L + R >> 1; if (f[Mid] < a[i]) { pos = Mid; L = Mid + 1; } else R = Mid - 1; } f[pos + 1] = a[i]; g[pos + 1] = i; l[i] = pos + 1; p[i] = g[pos]; } else { int pos = n; for (int j = m; j >= 1; --j) { while (f[pos] >= b[j]) --pos; f[pos + 1] = b[j]; g[pos + 1] = i; } } for (int len = l[n], pos = n, val = a[n]; len >= 1; --len) if (a[pos] != -1) { if (a[p[pos]] != -1) { pos = p[pos]; val = a[pos]; } else { int t = lower_bound(b + 1, b + m + 1, val) - b - 1; vis[t] = true; pos = p[pos]; val = ans[pos] = b[t]; } } else { for (int i = pos - 1; i >= 1; --i) if (a[i] != -1 && l[i] == len - 1 && a[i] < val) { pos = i; val = a[i]; goto Next; } for (int i = pos - 1; i >= 1; --i) if (a[i] == -1) { int t = lower_bound(b + 1, b + m + 1, val) - b - 1; vis[t] = true; pos = i; val = ans[pos] = b[t]; goto Next; } Next:; } int pos = 1; for (int i = (1), _end_ = (n - 1); i <= _end_; ++i) if (a[i] != -1) ans[i] = a[i]; else if (!ans[i]) { while (vis[pos]) ++pos; ans[i] = b[pos]; ++pos; } for (int i = (1), _end_ = (n - 1); i <= _end_; ++i) printf( %d , ans[i]); puts( ); return 0; }
#include <bits/stdc++.h> using namespace std; int getChar(char c) { if (c == + ) return 1; return 0; } string s; stack<int> mystack; int main() { cin >> s; int n = s.size(); int temp; for (int i = 0; i < n; i++) { temp = getChar(s[i]); if (mystack.empty()) mystack.push(temp); else { if (mystack.top() == temp) mystack.pop(); else mystack.push(temp); } } if (mystack.empty()) { cout << Yes << endl; return 0; } cout << No << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int inf = 2000000000; const double eps = 1e-9, Pi = 2 * acos(0); int main() { ifstream fin( input.txt ); ofstream fout( output.txt ); string s; cin >> s; map<string, int> make_pair; for (int i = 0; i <= 9; i++) { string x; cin >> x; make_pair[x] = i; } for (int i = 0; i < 8; i++) { string x = ; for (int j = i * 10; j < (i + 1) * 10; j++) x += s[j]; cout << make_pair[x]; } return 0; }
#include <bits/stdc++.h> using namespace std; int N, memo[1 << 25]; pair<int, int> arr[30]; int dist(int a, int n) { int k1 = arr[n].first - arr[a].first; int k2 = arr[n].second - arr[a].second; return k1 * k1 + k2 * k2; } int dp(int mask) { if (__builtin_popcount(mask) == N) { return 0; } if (memo[mask] != 0) return memo[mask]; int res = 1e9; for (int i = 1; i <= N; i++) { if (!((mask >> (i - 1) & 1))) { res = min(res, dp(mask | (1 << (i - 1))) + 2 * dist(i, 0)); for (int j = i + 1; j <= N; j++) { if (!(mask >> (j - 1) & 1) and i != j) { res = min(res, dp(mask | (1 << (i - 1)) | (1 << (j - 1))) + dist(i, 0) + dist(i, j) + dist(j, 0)); } } break; } } memo[mask] = res; return res; } void backtrack(int mask) { if (__builtin_popcount(mask) == N) { return; } for (int i = 1; i <= N; i++) { if (!((mask >> (i - 1) & 1))) { if (dp(mask | (1 << (i - 1))) + 2 * dist(i, 0) == dp(mask)) { cout << 0 << << i << ; backtrack(mask | (1 << (i - 1))); return; } for (int j = i + 1; j <= N; j++) { if (!(mask >> (j - 1) & 1) and i != j) { if (dp(mask | (1 << (i - 1)) | (1 << (j - 1))) + dist(i, 0) + dist(i, j) + dist(j, 0) == dp(mask)) { cout << 0 << << i << << j << ; backtrack(mask | (1 << (i - 1)) | (1 << (j - 1))); return; } } } break; } } return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> arr[0].first >> arr[0].second; cin >> N; for (int i = 1; i <= N; i++) cin >> arr[i].first >> arr[i].second; cout << dp(0) << endl; backtrack(0); cout << 0 << endl; }
#include <bits/stdc++.h> using namespace std; char a[51][51]; int main() { memset(a, 0 , sizeof(a)); int n; cin >> n; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cin >> a[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) { int x = a[i][j] - 0 ; if (x != 0) { if (a[i - 1][j - 1] != 0 ) x--; if (a[i - 1][j + 1] != 0 ) x--; if (a[i + 1][j - 1] != 0 ) x--; if (a[i + 1][j + 1] != 0 ) x--; if (x != 0) return cout << No , 0; } } cout << Yes ; return 0; }
#include <bits/stdc++.h> using namespace std; char str[2][400000 + 10]; int n; void sortstr(int t, int l, int r) { if ((r - l + 1) & 1) return; int md = (l + r) / 2; sortstr(t, l, (l + r) / 2), sortstr(t, (l + r) / 2 + 1, r); bool needswap = false; for (int i = l, j = md + 1; i <= md; i++, j++) { if (str[t][i] < str[t][j]) break; if (str[t][i] > str[t][j]) { needswap = true; break; } } if (needswap) { for (int i = l, j = md + 1; i <= md; i++, j++) swap(str[t][i], str[t][j]); } } int main() { for (int i = 0; i < 2; i++) scanf( %s , str[i]), n = strlen(str[i]), sortstr(i, 0, n - 1); bool equ = true; for (int i = 0; i < n; i++) if (str[0][i] != str[1][i]) equ = false; printf( %s n , equ ? YES : NO ); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, gxx; pair<int, int> a[411111]; int p[411111]; int Gxx[411111], bb[411111]; int degree[411111]; int h, t; void make_ans(int x, int y, int tot) { int now_x = degree[x] - 1 - tot; int need_x = max(h - now_x, 0); int now_y = degree[y] - 1 - tot; int need_y = max(t - now_y, 0); for (int i = p[x]; i < p[x + 1]; i++) Gxx[a[i].second] = 1; for (int i = p[y]; i < p[y + 1]; i++) if (Gxx[a[i].second] == 1) bb[a[i].second] = 1; printf( YES n ); printf( %d %d n , x, y); bool bo = false; for (int i = p[x]; i < p[x + 1]; i++) if ((bb[a[i].second] == 0 || need_x) && a[i].second != y && h) { h--; if (bb[a[i].second]) { bb[a[i].second] = -1; need_x--; } if (bo) printf( ); bo = true; printf( %d , a[i].second); } printf( n ); bo = false; for (int i = p[y]; i < p[y + 1]; i++) if ((bb[a[i].second] == 0 || need_y) && a[i].second != x && t && bb[a[i].second] != -1) { t--; if (bb[a[i].second]) need_y--; if (bo) printf( ); bo = true; printf( %d , a[i].second); } printf( n ); } int find(int x, int y) { int ans = 0; for (int i = p[x]; i < p[x + 1]; i++) if (a[i].second != y) { int t = lower_bound(a + p[y], a + p[y + 1], make_pair(y, a[i].second)) - a; if (a[t].second == a[i].second) ans++; } return ans; } int main() { scanf( %d%d%d%d , &n, &m, &h, &t); for (int i = 1; i <= m; i++) { scanf( %d%d , &a[i].first, &a[i].second); degree[a[i].first]++; degree[a[i].second]++; a[i + m].second = a[i].first; a[i + m].first = a[i].second; } int M = m * 2; sort(a + 1, a + M + 1); a[0].first = 0; a[M + 1].first = n + 1; int now = 0; for (int i = 1; i <= M + 1; i++) while (now != a[i].first) { now++; p[now] = i; } p[0] = 0; for (int i = 1; i <= M; i++) if (degree[a[i].first] > h && degree[a[i].second] > t) { int t1 = a[i].first; int t2 = a[i].second; int tot = 0; if (t1 < t2) tot = find(t1, t2); else tot = find(t2, t1); if (degree[t1] + degree[t2] - 2 - tot >= h + t) { make_ans(t1, t2, tot); return 0; } } printf( NO n ); return 0; }
#include <bits/stdc++.h> using namespace std; int n, k; int v[100000 + 1]; int mx, rez; pair<int, int> rz[100000 + 1]; int stkLen; int stkLen2; int stk[100000 + 1]; int stk2[100000 + 1]; int s, s2; void readFile() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> v[i]; } int cautBin(int nr, int stk[], int stkLen) { int i = 0; int pas = 1 << 16; while (pas != 0) { if (i + pas <= stkLen && abs(v[stk[i + pas]] - nr) > k) i += pas; pas >>= 1; } return stk[i] + 1; } bool verif(int st, int dr) { while (s < stkLen && stk[s] < st) s++; while (s2 < stkLen2 && stk2[s2] < st) s2++; return (abs(v[stk[s]] - v[stk2[s2]]) <= k); } void solve() { stkLen = 0; int st = 1; s = s2 = 1; for (int i = 1; i <= n; i++) { while (stkLen >= s && v[i] >= v[stk[stkLen]]) stkLen--; stk[++stkLen] = i; while (stkLen2 >= s2 && v[i] <= v[stk2[stkLen2]]) stkLen2--; stk2[++stkLen2] = i; while (st < i && verif(st, i) == 0) st++; int len = i - st + 1; if (len > mx) { mx = len; rez = 0; } if (len == mx) rz[++rez] = {st, i}; } } void printFile() { cout << mx << << rez << n ; for (int i = 1; i <= rez; i++) cout << rz[i].first << << rz[i].second << n ; } int main() { readFile(); solve(); printFile(); return 0; }
#include <bits/stdc++.h> using namespace std; long long ar[200010]; void solve() { long long a, b, c = 0, i, j, t, k, lie, m, n, o, x, y, z; long long p = 0, sm = 0, cnt = 0; scanf( %lld , &n); string s; cin >> s; if (n == 1) { printf( Yes n ); return; } for (i = 0; i < n; i++) ar[s[i] - a ]++; for (i = 0; i < 26; i++) { if (ar[i] > 1) { printf( Yes n ); return; } } printf( No n ); } int main() { solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const int mod = 1e9 + 7; const int N = 1e6 + 10; const int inf = 1e9; int t, n, k; char s[N]; int cnt[N]; int a[N], b[N]; int main() { ios::sync_with_stdio(false); cin >> t; while (t--) { cin >> n >> k; cin >> s + 1; for (int i = 1; i <= n; i++) { cnt[i] = cnt[i - 1]; if (s[i] == 1 ) cnt[i]++; } int ans = inf; for (int i = 1; i <= k; i++) { int ret = 0, mn = inf, tp = inf, l = 0; vector<int> q; q.push_back(0); for (int j = i; j <= n; j += k) { int tmp = cnt[j - 1] - cnt[l]; ret += tmp; q.push_back(cnt[j] - cnt[j - 1]); l = j; } ret += cnt[n] - cnt[l]; int sz = q.size() - 1; for (int i = 1; i <= sz; i++) { a[i] = a[i - 1]; b[i] = b[i - 1]; if (q[i] == 1) a[i]++; else b[i]++; } tp = a[sz]; for (int i = sz; i >= 1; i--) { mn = min(mn, b[i] - a[i] + a[sz]); tp = min(tp, a[i - 1] - b[i - 1] + mn); } ans = min(ans, ret + tp); } cout << ans << n ; } return 0; }
#include <bits/stdc++.h> using std::string; using std::vector; namespace Template { struct IN { template <class T> void fr(T& a) { T s = 0; int w = 1; char ch = getchar(); while (ch < 0 || ch > 9 ) { if (ch == - ) w = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) s = s * 10 + ch - 0 , ch = getchar(); a = w * s; } template <class T> T fr() { T a; fr(a); return a; } IN& operator>>(long long& x) { return fr(x), *this; } IN& operator>>(signed& x) { return fr(x), *this; } IN& operator>>(unsigned& x) { return fr(x), *this; } IN& operator>>(char& c) { while (isspace(c = getchar())) ; return *this; } IN& operator>>(string& str) { char ch; while (isspace(ch = getchar())) ; str = ch; while (!isspace(ch = getchar())) str += ch; return *this; } } cin; struct OUT { char D[1 << 27], *A = D - 1; ~OUT() { fwrite(D, 1, A - D + 1, stdout); } template <class T> void fw(T x) { static char C[20]; static int f; x < 0 ? * ++A = - , x = -x : 0; while (C[++f] = 48 + x % 10, x /= 10) ; while (*++A = C[f], --f) ; } OUT& operator<<(char c) { return *++A = c, *this; } OUT& operator<<(string str) { int len = str.length(); for (int i = 0; i < len; i++) *++A = str[i]; return *this; } OUT& operator<<(long long x) { return fw(x), *this; } OUT& operator<<(signed x) { return fw(x), *this; } OUT& operator<<(unsigned x) { return fw(x), *this; } } cout; char endl = n ; int DEBUG = 0; template <class T> T sqr(T a) { return a * a; } template <class T> T abs(T a) { return a < 0 ? -a : a; } template <class T> T cmin(T a, T b) { return a < b ? a : b; } template <class T> T cmax(T a, T b) { return a > b ? a : b; } template <class T> void cswp(T& a, T& b) { T t = a; a = b; b = t; } } // namespace Template using namespace Template; const int N = 1e5; int n, blo, tot = 0, fa[N + 10], len[N + 10], arr[N + 10]; vector<int> E[N + 10]; void adde(int u, int v) { E[u].push_back(v); } void dfs(int u, int f) { fa[u] = f, arr[++tot] = u; for (int v : E[u]) if (v != f) dfs(v, u); } int calc(int k) { int ret = 0; for (int i = 1; i <= n; i++) len[i] = 1; for (int i = n; i > 1; i--) { int u = arr[i], f = fa[u]; if (len[f] != -1 && len[u] != -1) { if (len[f] + len[u] >= k) ret++, len[f] = -1; else len[f] = cmax(len[f], len[u] + 1); } } return ret; } int main() { cin >> n; blo = sqrt(n * log2(n)); for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; adde(u, v), adde(v, u); } dfs(1, 0), cout << n << endl; for (int i = 2; i <= blo; i++) cout << calc(i) << endl; for (int i = blo + 1; i <= n;) { int f = calc(i), l = i, r = n, pos = 0; while (l <= r) { int mid = (l + r) >> 1; if (calc(mid) == f) l = mid + 1, pos = mid; else r = mid - 1; } for (; i <= pos; i++) cout << f << endl; } return 0; }
#include <bits/stdc++.h> template <typename T> void MACRO_VAR_Scan(T& t) { std::cin >> t; } template <typename First, typename... Rest> void MACRO_VAR_Scan(First& first, Rest&... rest) { std::cin >> first; MACRO_VAR_Scan(rest...); } template <typename T> void MACRO_VEC_ROW_Init(int n, T& t) { t.resize(n); } template <typename First, typename... Rest> void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) { first.resize(n); MACRO_VEC_ROW_Init(n, rest...); } template <typename T> void MACRO_VEC_ROW_Scan(int p, T& t) { std::cin >> t[p]; } template <typename First, typename... Rest> void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) { std::cin >> first[p]; MACRO_VEC_ROW_Scan(p, rest...); } template <class T> inline T CHMAX(T& a, const T b) { return a = (a < b) ? b : a; } template <class T> inline T CHMIN(T& a, const T b) { return a = (a > b) ? b : a; } template <class T> using V = std::vector<T>; template <class T> using VV = V<V<T>>; template <typename S, typename T> std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) { os << ( << p.first << , << p.second << ) ; return os; } using ll = long long; using ull = unsigned long long; using ld = long double; using PAIR = std::pair<ll, ll>; using PAIRLL = std::pair<ll, ll>; constexpr ll INFINT = 1 << 30; constexpr ll INFINT_LIM = (1LL << 31) - 1; constexpr ll INFLL = 1LL << 60; constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); constexpr double EPS = 1e-6; constexpr ll MOD = 1000000007; constexpr double PI = 3.141592653589793238462643383279; template <class T, size_t N> void FILL(T (&a)[N], const T& val) { for (auto& x : a) x = val; } template <class ARY, size_t N, size_t M, class T> void FILL(ARY (&a)[N][M], const T& val) { for (auto& b : a) FILL(b, val); } template <class T> void FILL(std::vector<T>& a, const T& val) { for (auto& x : a) x = val; } template <class ARY, class T> void FILL(std::vector<std::vector<ARY>>& a, const T& val) { for (auto& b : a) FILL(b, val); } struct Trie { std::vector<std::vector<ll>> g; ll p; Trie() : p(0) { g.emplace_back(std::vector<ll>(2, -1)); ++p; } void add(const std::string& s) { size_t par = 0; for (size_t i = 0; i < s.size(); ++i) { ll t = s[i] - 0 ; if (g[par][t] == -1) { g.emplace_back(std::vector<ll>(2, -1)); g[par][t] = p++; } par = g[par][t]; } } ll next(size_t par, char c) { ll t = c - 0 ; return g[par][t]; } }; bool ng(std::string s) { return s == 1100 || s == 1010 || s == 0111 || s == 1111 ; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); ; ll n; MACRO_VAR_Scan(n); ; std::string s; for (ll i = 0; i < ll(n); ++i) { char c; MACRO_VAR_Scan(c); ; s += c; } Trie tr; std::vector<ll> c(n, 0); for (ll i = 0; i < ll(n); ++i) { auto S = s.substr(0, i + 1); std::reverse((S).begin(), (S).end()); std::vector<ll> dp(S.size() + 1, 0); dp[0] = 1; for (ll j = 0; j < ll(S.size()); ++j) { for (ll k = 0; k < ll(4); ++k) { if (j < k) break; if (k == 3) { if (ng(S.substr(j - 3, 4))) continue; } (dp[j + 1] += dp[j - k]) %= MOD; } } ll p = 0; ll len = 0; for (ll j = 0; j < ll(S.size()); ++j) { ll ne = tr.next(p, S[j]); if (ne == -1) break; p = ne; dp[++len] = 0; } tr.add(S); for (ll j = (1); j < (S.size() + 1); ++j) { (c[i] += dp[j]) %= MOD; } } for (ll i = 0; i < ll(n - 1); ++i) (c[i + 1] += c[i]) %= MOD; for (ll i = 0; i < ll(n); ++i) { std::cout << (c[i]); std::cout << n ; ; } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename A, typename B, typename C> struct triple { A X; B Y; C Z; triple(A a = 0, B b = 0, C c = 0) : X(a), Y(b), Z(c) {} }; template <typename A, typename B, typename C> triple<A, B, C> make_triple(A a = 0, B b = 0, C c = 0) { return triple<A, B, C>(a, b, c); } template <typename A, typename B, typename C> bool operator<(const triple<A, B, C>& a, const triple<A, B, C>& b) { if (a.X != b.X) return a.X < b.X; if (a.Y != b.Y) return a.Y < b.Y; return a.Z < b.Z; } template <typename T, typename SS> ostream& operator<<(ostream& ofs, const pair<T, SS>& p) { ofs << ( << p.first << , << p.second << ) ; return ofs; } template <typename T> void print(T a) { for (auto i : a) cout << i << ; cout << n ; } template <typename T> T max(T a, T b, T c) { return max(a, max(b, c)); } template <typename T> T min(T a, T b, T c) { return min(a, min(b, c)); } template <typename T, typename D> D min(T a) { return *min_element(a.begin(), a.end()); } template <typename T, typename D> D max(T a) { return *max_element(a.begin(), a.end()); } struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; void solve() { string s; cin >> s; if ((long long)(s.size()) & 1) s = 0 + s; long long n = (long long)(s.size()); for (long long i = 0; i < (n); i++) s[i] -= 0 ; reverse(s.begin(), s.end()); vector<vector<long long> > dp(n / 2 + 1, vector<long long>(4)); dp[0][0] = 1; for (long long i = 0; i < (n / 2); i++) for (long long j = 0; j < (4); j++) { long long x = 2 * i, y = x + 1; long long dx = j & 1, dy = j >> 1; for (long long d11 = 0; d11 < (10); d11++) for (long long d12 = 0; d12 < (10); d12++) { long long sx = d11 + d12 + dx; if (sx % 10 != s[x]) continue; long long nmask = (sx >= 10); for (long long d21 = 0; d21 < (10); d21++) for (long long d22 = 0; d22 < (10); d22++) { long long sy = d21 + d22 + dy; if (sy % 10 != s[y]) continue; if (sy >= 10) nmask |= 2; dp[i + 1][nmask] += dp[i][j]; } } } cout << dp[n / 2][0] - 2 << n ; }; signed main() { if (0) { freopen( a .in , r , stdin); freopen( a .out , w , stdout); } srand(time(NULL)); cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(false); long long t = 1; cin >> t; for (long long _ = 0; _ < (t); _++) solve(); }
#include <bits/stdc++.h> using namespace std; const int inf = 1000000000 + 7; const int N = 1000006; int n, a[305], ar[6], s, aa, bb, cc, mn = inf, x, t; int main() { cin >> n >> s; for (int i = 1; i <= n; i++) { scanf( %d , &x); ar[x]++; } for (int i = s / n; i >= 0; i--) { for (int j = (s - i * ar[3]) / (ar[4] + ar[5]); j >= i; j--) { t = (s - i * ar[3] - j * ar[4]) / ar[5]; if (t * ar[5] - j * ar[4] >= mn) break; if (i * ar[3] + j * ar[4] + t * ar[5] == s && j <= t) { if (mn > abs(i * ar[3] - j * ar[4]) + abs(j * ar[4] - t * ar[5])) { aa = i; bb = j; cc = t; mn = abs(i * ar[3] - j * ar[4]) + abs(j * ar[4] - t * ar[5]); } } } } if (mn == inf) cout << -1 << endl; else cout << aa << << bb << << cc << endl; }
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); const int MAXN = 200005; int n, x[MAXN]; long long a[MAXN], t; long long b[MAXN]; void solve() { for (int i = 0; i + 1 < n; i++) { if (x[i] > x[i + 1]) { cout << No << endl; return; } } for (int i = 0; i < n; i++) { if (i > x[i]) { cout << No << endl; return; } } for (int i = 0; i < n;) { int pos = x[i]; int j = i; while (j < n && x[j] == pos) { j++; } if (j < x[i] + 1) { cout << No << endl; return; } long long num = a[j - 1]; for (int k = i; k + 1 < j; k++) { b[k] = a[k + 1] + t; } b[j - 1] = a[j - 1] + t + (j - i > 1); if (j < n && b[j - 1] >= a[j] + t) { cout << No << endl; return; } i = j; } cout << Yes << endl; for (int i = 0; i < n; i++) { cout << b[i] << ; } cout << endl; } int main() { ios_base::sync_with_stdio(false); while (cin >> n >> t) { for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> x[i]; x[i]--; } solve(); } }
#include <bits/stdc++.h> using namespace std; int N, Q, C, Rt, L[200005], R[200005], G[200005], F[200005], ch[200005][2]; struct Nod { int Now, Max, Min, Laz; Nod() { Min = 1 << 30; Max = -Min; } } T[200005]; vector<int> son[200005]; void Upd(int x, int k) { if (x) T[x].Now += k, T[x].Max += k, T[x].Min += k, T[x].Laz += k; } void Up(int x) { int l = ch[x][0], r = ch[x][1]; T[x].Max = max(T[x].Now, max(T[l].Max, T[r].Max)); T[x].Min = min(T[x].Now, min(T[l].Min, T[r].Min)); } void Dn(int x) { int k = T[x].Laz; if (k) Upd(ch[x][0], k), Upd(ch[x][1], k), T[x].Laz = 0; } void Rot(int x) { int y = F[x], z = F[y], k = ch[y][0] == x, t; if (t = ch[y][!k] = ch[x][k]) F[t] = y; if (F[x] = z) ch[z][ch[z][1] == y] = x; ch[F[y] = x][k] = y; Up(y); } void Splay(int x, int Aim) { for (Dn(x); F[x] ^ Aim; Rot(x)) { int y = F[x], z = F[y]; Dn(z); Dn(y); Dn(x); if (F[y] ^ Aim) (ch[z][0] == y ^ ch[y][0] == x) ? Rot(x) : Rot(y); } Up(x); if (!Aim) Rt = x; } void DFS(int u, int d) { L[u] = ++C; G[C] = u; T[C].Now = d; for (int i = 1, lim = son[u].size(); i <= lim; i++) DFS(son[u][i - 1], d + 1); R[u] = ++C; G[C] = u; T[C].Now = d; } void Build(int& u, int l, int r, int fa) { F[u = l + r >> 1] = fa; if (l < u) Build(ch[u][0], l, u - 1, u); if (u < r) Build(ch[u][1], u + 1, r, u); Up(u); } int Find(int x, int k) { int l = ch[x][0], r = ch[x][1]; Dn(x); if (T[r].Min <= k && k <= T[r].Max) return Find(r, k); return T[x].Now == k ? x : Find(l, k); } int Pre(int x) { Splay(x, 0); x = ch[x][0]; for (; ch[x][1]; x = ch[x][1]) Dn(x); return x; } int Suf(int x) { Splay(x, 0); x = ch[x][1]; for (; ch[x][0]; x = ch[x][0]) Dn(x); return x; } void Get(int x, int y) { Splay(x, 0); Splay(y, x); } int main() { scanf( %d%d , &N, &Q); for (int i = 1, lim = N; i <= lim; i++) { int x, t; scanf( %d , &x); for (; x--;) scanf( %d , &t), son[i].push_back(t); } C++; DFS(1, 1); C++; Build(Rt, 1, C, 0); for (int k, x, y, z, l, r, w, t; Q--;) { scanf( %d , &k); if (k == 1) { scanf( %d%d , &x, &y); if (x == y) puts( 0 ); else { x = L[x]; y = L[y]; Get(x, y); z = (ch[x][0] == y ? T[ch[y][1]] : T[ch[y][0]]).Min; printf( %d n , T[x].Now + T[y].Now - 2 * min(z - 1, min(T[x].Now, T[y].Now))); } } else if (k == 2) { scanf( %d%d , &x, &y); Get(1, L[x]); w = G[Find(ch[L[x]][0], T[L[x]].Now - y)]; l = Pre(L[x]); r = Suf(R[x]); Get(l, r); t = ch[r][0]; ch[r][0] = 0; Up(r); Up(l); r = R[w]; l = Pre(r); Get(l, r); ch[r][0] = t; F[t] = r; Upd(t, -y + 1); Splay(t, 0); } else scanf( %d , &x), printf( %d n , G[Find(Rt, x + 1)]); } }
#include <bits/stdc++.h> using namespace std; const long long N = 112, INF = 1e18; long long t, n, m, a[N]; long long B10(string s) { reverse(s.begin(), s.end()); long long ret = 0; for (long long i = 0; i < s.size(); i++) { if (s[i] == 1 ) ret += (1ll << i); } return ret; } string B2(long long a) { string ret; for (long long i = 0; i < m; i++) { if (a & (1ll << i)) ret += 1 ; else ret += 0 ; } reverse(ret.begin(), ret.end()); return ret; } long long BS() { long long low = 0, high = (1ll << m); while (high - low > 1) { long long mid = (low + high) >> 1; long long v1 = mid - (lower_bound(a, a + n, mid) - a); long long v2 = ((1ll << m) - n - 1) / 2; if (v1 <= v2) low = mid; else high = mid; } return low; } int32_t main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> t; while (t--) { cin >> n >> m; for (long long i = 0; i < n; i++) { string s; cin >> s; a[i] = B10(s); } sort(a, a + n); cout << B2(BS()) << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int p[100100]; int main() { int n, a, b, t, cn = 1; cin >> n; for (int i = 0; i < n; i++) { cin >> a; p[i] = a; } sort(p, p + n); int ans = 0; vector<int> v; for (int i = 0; i < n; i++) { int j = 0; for (j = 0; j < v.size(); j++) { if (p[i] >= v[j]) { v[j]++; break; } } if (j == v.size()) { v.push_back(1); } } cout << v.size() << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2100; char mp[N][N]; int n, m, lp, rp, dis[N][N]; bool vis[N][N]; struct poi { int x, y; } q[N * N], st; void expan(int x, int y, int di) { int i; for (i = x; mp[i][y] != * ; i--) { q[++rp] = (poi){i, y}; dis[i][y] = di; vis[i][y] = 1; } for (i = x + 1; mp[i][y] != * ; i++) { q[++rp] = (poi){i, y}; dis[i][y] = di; vis[i][y] = 1; } } int main() { int i, j, k, le, ri, x, y; scanf( %d%d%d%d%d%d , &n, &m, &st.x, &st.y, &le, &ri); for (i = 1; i <= n; i++) scanf( %s , mp[i] + 1); for (i = 0; i <= m + 1; i++) mp[0][i] = mp[n + 1][i] = * ; for (i = 0; i <= n + 1; i++) mp[i][0] = mp[i][m + 1] = * ; lp = 1; rp = 0; expan(st.x, st.y, 0); while (lp <= rp) { poi now = q[lp++]; x = now.x; y = now.y; if ((!vis[x][y - 1]) && (mp[x][y - 1] != * )) expan(x, y - 1, dis[x][y] + 1); if ((!vis[x][y + 1]) && (mp[x][y + 1] != * )) expan(x, y + 1, dis[x][y] + 1); } int ans = 0; for (i = 1; i <= n; i++) for (j = 1; j <= m; j++) { int fix = st.y - j; if ((vis[i][j]) && (mp[i][j] != * ) && (le >= ((dis[i][j] + fix) >> 1)) && (ri >= ((dis[i][j] - fix) >> 1))) ans++; } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; char buf[25]; struct node { long long x, y; bool operator<(node tp) const { if (x != tp.x) return x < tp.x; return y < tp.y; } }; map<long long, long long> h1, h2; map<node, long long> t, q; const long long inf = 1e17; const long long maxn = 200010; long long L1[maxn], R1[maxn], l1[maxn], r1[maxn], s1[maxn], a[maxn]; long long L2[maxn], R2[maxn], l2[maxn], r2[maxn], s2[maxn]; long long n, m, sum1, sum2; long long ans; long long read() { long long x = 0, f = 0; char ch; do { ch = getchar(); if (ch == - ) f = 1; } while (ch < 0 || ch > 9 ); while (ch >= 0 && ch <= 9 ) { x = (x << 1) + (x << 3) + (ch ^ 48); ch = getchar(); } return f ? -x : x; } void write(long long x) { if (x < 0) { putchar( - ); x = -x; } if (!x) { putchar( 0 ); return; } long long cnt = 0; while (x) { buf[++cnt] = 0 + x % 10; x /= 10; } for (long long i = cnt; i >= 1; --i) putchar(buf[i]); } signed main() { srand(19267180); n = read(); m = read(); for (long long i = 1; i <= n; ++i) { h1[i] = (1ll * rand() * rand() * rand() * rand() % inf + inf) % inf; h2[i] = (1ll * rand() * rand() * rand() * rand() % inf + inf) % inf; sum1 = h1[i] ^ sum1; sum2 = h2[i] ^ sum2; } for (long long i = 1; i <= n; ++i) { long long l = read(), r = read(); ++a[l]; --a[r + 1]; s1[l] ^= h1[i]; s1[r + 1] ^= h1[i]; L1[l - 1] ^= h1[i]; R1[r + 1] ^= h1[i]; s2[l] ^= h2[i]; s2[r + 1] ^= h2[i]; L2[l - 1] ^= h2[i]; R2[r + 1] ^= h2[i]; } for (long long i = m - 1; i >= 1; --i) L1[i] ^= L1[i + 1], L2[i] ^= L2[i + 1]; for (long long i = 2; i <= m; ++i) { R1[i] ^= R1[i - 1], s1[i] ^= s1[i - 1]; R2[i] ^= R2[i - 1], s2[i] ^= s2[i - 1]; } for (long long i = 2; i <= m; ++i) s1[i] ^= s1[i - 1], s2[i] ^= s2[i - 1]; for (long long i = 1; i <= m; ++i) { l1[i] = s1[i - 1] ^ R1[i], r1[i] = s1[i] ^ L1[i]; l2[i] = s2[i - 1] ^ R2[i], r2[i] = s2[i] ^ L2[i]; } for (long long i = m; i >= 1; --i) { t[(node){r1[i], r2[i]}] += 1; q[(node){r1[i], r2[i]}] += i; long long x_1 = sum1 ^ l1[i], x_2 = sum2 ^ l2[i]; ans += q[(node){x_1, x_2}] - 1ll * t[(node){x_1, x_2}] * (i - 1); } for (long long i = 1; i <= m; ++i) a[i] += a[i - 1]; long long x = 0; for (long long i = 1; i <= m; ++i) { if (a[i] == 0) ++x; else x = 0; if (x) if (i == m || a[i + 1] != 0) for (long long j = 1; j <= x; ++j) ans -= 1ll * j * (x - j + 1); } write(ans); return 0; }
#include <bits/stdc++.h> using namespace std; void RI() {} template <typename... T> void RI(int& head, T&... tail) { scanf( %d , &head); RI(tail...); } template <typename T> void _dump(const char* s, T&& head) { cerr << s << = << head << endl; } template <typename T, typename... Args> void _dump(const char* s, T&& head, Args&&... tail) { int c = 0; while (*s != , || c != 0) { if (*s == ( || *s == [ || *s == { ) c++; if (*s == ) || *s == ] || *s == } ) c--; cerr << *s++; } cerr << = << head << , ; _dump(s + 1, tail...); } template <typename Iter> ostream& _out(ostream& s, Iter b, Iter e) { s << [ ; for (auto it = b; it != e; it++) s << (it == b ? : ) << *it; s << ] ; return s; } template <typename A, typename B> ostream& operator<<(ostream& s, const pair<A, B>& p) { return s << ( << p.first << , << p.second << ) ; } template <typename T> ostream& operator<<(ostream& s, const vector<T>& c) { return _out(s, begin(c), end(c)); } template <typename T> ostream& operator<<(ostream& s, const set<T>& c) { return _out(s, begin(c), end(c)); } template <typename A, typename B> ostream& operator<<(ostream& s, const map<A, B>& c) { return _out(s, begin(c), end(c)); } vector<int> app[1000010]; int ask(int l, int r, int v) { auto it = lower_bound(begin(app[v]), end(app[v]), l); auto jt = upper_bound(begin(app[v]), end(app[v]), r); return jt - it; } int n, m, a[300010], sa[300010], ord[300010], l[300010], r[300010]; bool vis[300010]; bool cmp(int x, int y) { return a[x] < a[y]; } long long calc(int l1, int r1, int l2, int r2, int mx) { long long ans = 0; mx %= m; if (r1 - l1 <= r2 - l2) { for (int i = (l1 - 1); i <= int(r1 - 1); i++) { int x = sa[i] + mx; if (x >= m) x -= m; int now = ask(l2, r2, x); ans += now; } } else { for (int i = (l2); i <= int(r2); i++) { int x = sa[i] - mx; if (x < 0) x += m; int now = ask(l1 - 1, r1 - 1, x); ans += now; } } return ans; } int main() { RI(n, m); for (int i = (1); i <= int(n); i++) RI(a[i]); for (int i = (1); i <= int(n); i++) l[i] = r[i] = i; for (int i = (1); i <= int(n); i++) ord[i] = i; for (int i = (1); i <= int(n); i++) sa[i] = (sa[i - 1] + a[i]) % m; for (int i = (0); i <= int(n); i++) app[sa[i]].push_back(i); sort(ord + 1, ord + n + 1, cmp); long long ans = 0; for (int ii = (1); ii <= int(n); ii++) { int i = ord[ii]; vis[i] = 1; if (i > 1 && vis[i - 1]) { ans += calc(l[i - 1], i - 1, i, i, a[i]); l[i] = l[i - 1]; r[l[i - 1]] = i; } if (i < n && vis[i + 1]) { ans += calc(l[i], i, i + 1, r[i + 1], a[i]); r[l[i]] = r[i + 1]; l[r[i + 1]] = l[i]; } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, m; long long a, b[100100], p[100100]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> n >> m >> a; for (int i = 1; i <= n; i++) cin >> b[i]; for (int i = 1; i <= m; i++) cin >> p[i]; sort(b + 1, b + n + 1); sort(p + 1, p + m + 1); int le = -1, ri = min(n, m) + 1; while (le + 1 < ri) { int mi = (le + ri) / 2; long long cnt = 0; for (int i = 1; i <= mi; i++) { cnt += max(0LL, p[mi + 1 - i] - b[n + 1 - i]); } if (cnt > a) { ri = mi; } else { le = mi; } } long long tot = 0; for (int i = 1; i <= le; i++) tot += p[i]; cout << le << << max(tot - a, 0LL) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int n, a[100000], outpalsz, inn, b[100000]; long long result; int inicount[100001], iniodd; int ccount[100001], codd; int main() { cin.sync_with_stdio(false); cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n - 1 - i && a[i] == a[n - 1 - i]; i++) outpalsz++; for (int i = outpalsz; i < n - outpalsz; i++) { inicount[a[i]]++; if (inicount[a[i]] % 2) iniodd++; else iniodd--; b[inn++] = a[i]; } if (inn <= 1) { cout << 1LL * n * (n + 1) / 2 << n ; return 0; } if (iniodd <= 1) result = 1LL * (outpalsz + 1) * (outpalsz + 1); for (int i = 0; i <= 100000; i++) ccount[i] = inicount[i]; codd = iniodd; for (int i = 0; i < inn; i++) { ccount[b[i]] -= 2; if (i * 2 + 1 == inn) ccount[b[i]]++; if (i > inn - 1 - i && b[i] == b[inn - 1 - i]) ccount[b[i]] += 2; if (ccount[b[i]] < 0) break; if (codd <= 1) result += outpalsz + 1; } for (int i = 0; i <= 100000; i++) ccount[i] = inicount[i]; codd = iniodd; for (int i = inn - 1; i >= 0; i--) { ccount[b[i]] -= 2; if (i * 2 + 1 == inn) ccount[b[i]]++; if (i < inn - 1 - i && b[i] == b[inn - 1 - i]) ccount[b[i]] += 2; if (ccount[b[i]] < 0) break; if (codd <= 1) result += outpalsz + 1; } cout << result << n ; return 0; }
#include <bits/stdc++.h> using namespace std; long long get(string a) { long long len = a.length(); long long b = 0; long long cnt = 1; for (int i = len - 1; i >= 0; i--) { b += ((a[i] - 0 ) % 2) * cnt; cnt *= 2; } return b; } int p[262150]; int main() { int t; cin >> t; while (t--) { char c; cin >> c; string a; cin >> a; long long b = get(a); switch (c) { case + : { p[b]++; break; } case - : { p[b]--; break; } case ? : { cout << p[b] << endl; break; } } } return 0; }
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int INF = (int)1e9; const long long LINF = (long long)1e18; const long double PI = acos((long double)-1); const long double EPS = 1e-9; inline long long gcd(long long a, long long b) { long long r; while (b) { r = a % b; a = b; b = r; } return a; } inline long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } inline long long fpow(long long n, long long k, int p = MOD) { long long r = 1; for (; k; k >>= 1) { if (k & 1) r = r * n % p; n = n * n % p; } return r; } template <class T> inline int chkmin(T& a, const T& val) { return val < a ? a = val, 1 : 0; } template <class T> inline int chkmax(T& a, const T& val) { return a < val ? a = val, 1 : 0; } template <class T> inline T isqrt(T k) { T r = sqrt(k) + 1; while (r * r > k) r--; return r; } template <class T> inline T icbrt(T k) { T r = cbrt(k) + 1; while (r * r * r > k) r--; return r; } inline void addmod(int& a, int val, int p = MOD) { if ((a = (a + val)) >= p) a -= p; } inline void submod(int& a, int val, int p = MOD) { if ((a = (a - val)) < 0) a += p; } inline int mult(int a, int b, int p = MOD) { return (long long)a * b % p; } inline int inv(int a, int p = MOD) { return fpow(a, p - 2, p); } const int maxn = 2000 + 10; const int maxm = 500000 + 10; int n, m; int a[maxm]; int b[maxm]; bitset<maxn << 1> mat[maxn]; void solve() { cin >> n >> m; for (int i = (0); i < (m); i++) { cin >> a[i] >> b[i], a[i]--, b[i]--; mat[b[i]][a[i]] = 1; } for (int i = (0); i < (n); i++) for (int j = (0); j < (n); j++) mat[i][j + n] = i == j; for (int i = (0); i < (n); i++) { if (!mat[i][i]) { for (int j = (i + 1); j < (n); j++) if (mat[j][i]) { mat[i] ^= mat[j]; break; } } for (int j = (0); j < (n); j++) { if (j == i) continue; if (mat[j][i]) { mat[j] ^= mat[i]; } } } for (int i = (0); i < (m); i++) { if (!mat[a[i]][b[i] + n]) { cout << YES n ; } else { cout << NO n ; } } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { string str; cin >> str; int ans(0); for (int i = 0; i < str.size(); ++i) if (str[i] != str[str.size() - 1 - i]) ++ans; if (ans == 2 || (ans == 0 && str.size() % 2 == 1)) cout << YES << endl; else cout << NO << endl; return 0; }
#include <bits/stdc++.h> #pragma comment(linker, /STACK:256000000 ) using namespace std; int main() { srand(1373737); ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int n, t, k, d, q; cin >> n >> q; vector<int> arr(n); for (int i = 0; i < (int)q; i++) { cin >> t >> k >> d; int free = 0; for (int j = 0; j < (int)n; j++) { if (arr[j] <= t) free++; } if (free < k) { cout << -1 << n ; continue; } int res = 0; for (int j = 0; j < n && k; j++) { if (arr[j] <= t) { arr[j] = t + d; k--; res += j + 1; } } cout << res << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> inline bool uin(T& a, T b) { return a > b ? (a = b, true) : false; } template <class T> inline bool uax(T& a, T b) { return a < b ? (a = b, true) : false; } int main() { ios::sync_with_stdio(false); cin.tie(0); long long N, K; cin >> N >> K; if (K == 1) { cout << N << n ; return 0; } string be, en; cin >> be >> en; long long ans = 0, cur = 1; for (int i = 0; i < N; ++i) { cur *= 2LL; if (be[i] == b ) --cur; if (en[i] == a ) --cur; uin(cur, K); ans += cur; } cout << ans << n ; }
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize( O2 ) #pragma GCC optimize( unroll-loops ) struct POINT { int x, y; POINT() : x(0), y(0) {} POINT(int x, int y) : x(x), y(y) {} POINT operator+(const POINT &t) const { return POINT(x + t.x, y + t.y); } POINT operator-(const POINT &t) const { return POINT(x - t.x, y - t.y); } POINT operator!() const { return POINT(-y, x); } }; inline long long dot(const POINT &a, const POINT &b) { return (long long)a.x * b.x + (long long)a.y * b.y; } inline long long cross(const POINT &a, const POINT &b) { return (long long)a.x * b.y - (long long)a.y * b.x; } inline long long cross(const POINT &a, const POINT &b, const POINT &c) { return cross(a - c, b - c); } inline bool halfPlane(const POINT &p) { if (p.y == 0) return p.x < 0; return p.y < 0; } inline bool angSortCmp(const POINT &a, const POINT &b) { bool ha = halfPlane(a); bool hb = halfPlane(b); if (ha != hb) return ha < hb; return cross(a, b) > 0; } const int N = 2000; int n; long long S; POINT tab[N + 7]; int poz[N + 7]; POINT stab[N + 7]; struct kierunek { POINT wek; int p, q; }; int main() { ios_base::sync_with_stdio(0); cin >> n >> S; S *= 2; for (int i = 1; i <= n; ++i) cin >> tab[i].x >> tab[i].y; sort(tab + 1, tab + n + 1, [](const POINT &a, const POINT &b) -> bool { if (a.x == b.x) return a.y > b.y; return a.x < b.x; }); for (int i = 1; i <= n; ++i) { poz[i] = i; stab[i] = tab[i]; } vector<kierunek> kol((n * (n - 1)) / 2); int it = 0; for (int i = 1; i <= n; ++i) { for (int j = i + 1; j <= n; ++j) { kol[it].wek = !(tab[j] - tab[i]); kol[it].p = i; kol[it].q = j; it++; } } sort(kol.begin(), kol.end(), [](const kierunek &a, const kierunek &b) -> bool { return angSortCmp(a.wek, b.wek); }); for (kierunek u : kol) { int p = u.p; int q = u.q; swap(stab[poz[p]], stab[poz[q]]); swap(poz[p], poz[q]); int l = 1; int r = n; while (l <= r) { int s = (l + r) / 2; long long ww = cross(tab[q], stab[s], tab[p]); if (ww == S) { cout << Yes << endl; cout << tab[p].x << << tab[p].y << endl; cout << tab[q].x << << tab[q].y << endl; cout << stab[s].x << << stab[s].y << endl; exit(0); } if (ww > S) r = s - 1; else l = s + 1; } l = 1; r = n; while (l <= r) { int s = (l + r) / 2; long long ww = cross(tab[q], stab[s], tab[p]); if (ww == -S) { cout << Yes << endl; cout << tab[p].x << << tab[p].y << endl; cout << tab[q].x << << tab[q].y << endl; cout << stab[s].x << << stab[s].y << endl; exit(0); } if (ww > -S) r = s - 1; else l = s + 1; } } cout << No << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); int i, j, n, m, k, l, r; cin >> n >> m; int a[n][m]; pair<int, int> start_end[n][m]; for (i = 0; i < n; i += 1) for (j = 0; j < m; j += 1) { cin >> a[i][j]; start_end[i][j].first = i; start_end[i][j].second = i; } for (j = 0; j < m; j += 1) for (i = 1; i < n; i += 1) if (a[i][j] >= a[i - 1][j]) start_end[i][j].first = start_end[i - 1][j].first; int min_l[n]; for (i = 0; i < n; i++) min_l[i] = 100010; for (i = 0; i < n; i += 1) for (j = 0; j < m; j += 1) if (start_end[i][j].first < min_l[i]) min_l[i] = start_end[i][j].first; cin >> k; while (k--) { cin >> l >> r; cout << ((min_l[r - 1] <= l - 1) ? Yes n : No n ); } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &x) { x = 0; char ch = getchar(); long long fh = 1; while (ch < 0 || ch > 9 ) { if (ch == - ) fh = -1; ch = getchar(); } while (ch >= 0 && ch <= 9 ) x = x * 10 + ch - 0 , ch = getchar(); x *= fh; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar( - ); if (x > 9) write(x / 10); putchar(x % 10 + 0 ); } template <typename T> void writeln(T x) { write(x); puts( ); } long long n, a[100005]; signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { cin >> n; for (long long i = 1; i <= n; i++) cin >> a[i]; long long x = 0; for (long long i = n; i >= 1; i--) if (a[i] < 0) x += -a[i]; else if (x >= a[i]) x -= a[i]; else x = 0; cout << x << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, m, h; cin >> n >> m >> h; vector<int> ms(m), ns(n); vector<vector<int> > ok(n, vector<int>(m)); for (int i = 0; i < m; i++) { cin >> ms[i]; } for (int i = 0; i < n; i++) { cin >> ns[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> ok[i][j]; } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (ok[i][j] == 0) { cout << 0 << ; } else { cout << min(ms[j], ns[i]) << ; } } cout << endl; } }
#include <bits/stdc++.h> using namespace std; int r, g, j, f[200001], sc, kq, mod = round(1e9) + 7; long long i, h; int main() { cin >> r >> g; f[0] = 1; for (i = 1; i * (i + 1) / 2 <= r + g; i++) { for (j = r; j >= 0; j--) { if (j >= i) f[j] = (f[j] + f[j - i]) % mod; } } h = i - 1; for (i = 0; i <= r; i++) if (h * (h + 1) / 2 - i <= g) kq = (kq + f[i]) % mod; cout << kq; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 7; vector<pair<int, int> > s; int co[maxn], deg[maxn]; int main() { int n; cin >> n; for (int i = 0; i < n - 1; i++) { pair<int, int> q; cin >> q.first >> q.second; s.push_back(q); } for (int i = 1; i <= n; i++) { cin >> co[i]; } int x = 0; for (auto i : s) { if (co[i.first] != co[i.second]) { deg[i.first]++; deg[i.second]++; x++; } } int flag = 0; for (int i = 1; i <= n; i++) { if (deg[i] == x) { flag = i; } } if (flag) { printf( YES n%d n , flag); } else { printf( NO n ); } }
#include <bits/stdc++.h> using namespace std; void solve() { long long n, k, kol = 0; cin >> n >> k; string s; cin >> s; vector<long long> a(n); for (long long i = 0; i < n; i++) { a[i] = s[i] - 0 ; } for (long long i = 0; i < n - 1 && k; i++) { if (a[i] == 4 && a[i + 1] == 7) { if (i % 2 == 0) { a[i + 1] = 4; k--; } else { if (i > 0 && a[i - 1] == 4) { if (k % 2 == 1) { a[i] = 7; } break; } a[i] = a[i + 1] = 7; k--; } } } for (long long i = 0; i < n; i++) { cout << a[i]; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; while (t--) { solve(); } }
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; int n, l; long long a[300000][3]; long long b[300000][3]; long long t[300000][2]; vector<int> c[3000000]; vector<pair<long long, long long> > g; long long p[3000000]; long long dab[3000000][2]; void sayno() { printf( NO ); exit(0); } void calc() { int i, j, k; long long d = -1; for (i = 0; i < l; i++) { if (c[i].size() >= 2) { d = (t[c[i][1]][1] - t[c[i][0]][1]) / ((t[c[i][1]][0] - t[c[i][0]][0]) / l); break; } } if (d == -1) { int past = 0; g.clear(); for (i = 1; i <= l; i++) { if (!c[i % l].size()) continue; int x = c[past][0], y = c[i % l][0]; if (t[x][0] < t[y][0]) { long long u = (t[y][0] - t[x][0]) / l; long long v = t[y][1] - t[x][1]; long long w = (t[y][0] - t[x][0]) % l; if (u != 0) g.push_back(pair<long long, long long>((v - w + u - 1) / u, v / u)); } else { long long u = (t[x][0] - t[y][0]) / l + 1; long long v = t[x][1] - t[y][1]; long long w = t[y][0] + u * l - t[x][0]; if (u != 0) g.push_back(pair<long long, long long>((v + u - 1) / u, (v + w) / u)); } past = i; } sort(g.begin(), g.end()); for (auto e : g) { if (e.second < g.back().first) sayno(); } d = g.back().first; } memset(p, -1, sizeof(p)); for (i = 0; i < n; i++) { long long u = t[i][1] - d * (t[i][0] / l); long long v = t[i][0] % l; if (p[v] == -1) p[v] = u; else if (p[v] != u) sayno(); } p[l] = d; int past = 0; for (i = 1; i <= l; i++) { if (p[i] == -1) continue; if (p[i] - p[past] < 0 || p[i] - p[past] > i - past) sayno(); for (j = 0; j < p[i] - p[past]; j++) { dab[past + j][1] = 1; } for (j = p[i] - p[past]; j < i - past; j++) { dab[past + j][1] = -1; } past = i; } } int main() { int i, j, k; cin >> n >> l; for (i = 0; i <= n; i++) { if (i != 0) scanf( %I64d%I64d%I64d , &a[i][0], &a[i][1], &a[i][2]); b[i][0] = a[i][0], b[i][1] = a[i][1] + a[i][2], b[i][2] = a[i][1] - a[i][2]; if ((b[i][1] + b[i][0]) % 2 == 1 || (b[i][2] + b[i][0]) % 2 == 1) sayno(); b[i][1] = (b[i][1] + b[i][0]) / 2, b[i][2] = (b[i][2] + b[i][0]) / 2; c[b[i][0] % l].push_back(i); } n++; for (i = 0; i < n; i++) t[i][0] = b[i][0], t[i][1] = b[i][1]; calc(); for (i = 0; i < l; i++) dab[i][0] = dab[i][1]; for (i = 0; i < n; i++) t[i][0] = b[i][0], t[i][1] = b[i][2]; calc(); for (i = 0; i < l; i++) { if (dab[i][0] == -1 & dab[i][1] == -1) printf( L ); else if (dab[i][0] == 1 && dab[i][1] == 1) printf( R ); else if (dab[i][0] == -1 && dab[i][1] == 1) printf( D ); else printf( U ); } return 0; }
#include <bits/stdc++.h> using namespace std; vector<vector<long long> > matmul(vector<vector<long long> > &a, vector<vector<long long> > &b) { int i, j, k, n = a.size(); vector<vector<long long> > ans(n, vector<long long>(n)); for (i = 0; i < n; i++) for (j = 0; j < n; j++) { long long temp = 0; for (k = 0; k < n; k++) temp = (temp + a[i][k] * b[k][j]) % (1000000007LL); ans[i][j] = temp; } return ans; } vector<vector<long long> > matpwr(vector<vector<long long> > &a, long long p) { int n = a.size(); vector<vector<long long> > ans(n, vector<long long>(n)); for (int i = 0; i < n; i++) ans[i][i] = 1; while (p) { if (p & 1) ans = matmul(ans, a); a = matmul(a, a); p >>= 1; } return ans; } long long pwr(long long base, long long p, long long mod = (1000000007LL)) { long long ans = 1; while (p) { if (p & 1) ans = (ans * base) % mod; base = (base * base) % mod; p /= 2; } return ans; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return (a * b) / gcd(a, b); } long long invMod(long long n, long long mod = (1000000007LL)) { return pwr(n, mod - 2); } int n, arr[2000003]; int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 1; i <= n; i++) arr[i] = i; int start = 1; for (int iter = 2; iter <= n; iter++) { int last = arr[start]; for (int j = start + iter; j < start + n; j += iter) { int temp = last; last = arr[j]; arr[j] = temp; } arr[start + n] = last; start++; } for (int i = start; i < start + n; i++) cout << arr[i] << ; return 0; }
#include <bits/stdc++.h> using namespace std; int n, a[110], b[110], sum = 0; pair<int, int> dp[110][11000], q = {-1, -1}; pair<int, int> Rec(int i, int W) { if (i == n) { if (W >= sum) return {0, 0}; else return {1e9, 1e9}; } if (dp[i][W] != q) return dp[i][W]; pair<int, int> Res1 = {1e9, 1e9}, Res2 = {1e9, 1e9}; Res1 = {0 + Rec(i + 1, W).first, a[i] + Rec(i + 1, W).second}; Res2 = {1 + Rec(i + 1, W + b[i]).first, 0 + Rec(i + 1, W + b[i]).second}; return dp[i][W] = min(Res1, Res2); } int main() { memset(dp, -1, sizeof(dp)); cin >> n; for (int i = 0; i < n; i++) cin >> a[i], sum += a[i]; for (int i = 0; i < n; i++) cin >> b[i]; cout << Rec(0, 0).first << << Rec(0, 0).second; return 0; }
#include <bits/stdc++.h> using namespace std; char s[200005]; int n; int mem[200005][27]; int dirr[200005][27]; bool vis[200005][27]; char other(char c, char d) { if ( a != c && a != d) return a ; if ( b != c && b != d) return b ; return c ; } int solve(int ind, int lst) { if (ind >= n) return 0; if (vis[ind][lst]) return mem[ind][lst]; vis[ind][lst] = 1; int ans1 = -1, ans2 = -1, ans = -1, dir = -1; if (s[ind] - a != lst) ans1 = solve(ind + 1, s[ind] - a ); ans2 = 1 + solve(ind + 1, other(s[ind + 1], lst + a ) - a ); if (ans1 == -1 || ans2 <= ans1) dir = 1, ans = ans2; else dir = 0, ans = ans1; dirr[ind][lst] = dir; return mem[ind][lst] = ans; } int main() { ios::sync_with_stdio(false); scanf( %s , s); n = strlen(s); solve(0, 26); int lst = 26; for (int i = 0; i < n; i++) if (dirr[i][lst] == 0) lst = s[i] - a ; else s[i] = lst = other(s[i + 1], lst + a ), lst -= a ; puts(s); return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; const int N = 3020; const int D = 1001; int c[N][N], x[N], y[N], b[N], v[N][N], f[N][N], w[N]; int dis(int i, int j) { return max(abs(x[i] - x[j]), abs(y[i] - y[j])); } int C(int x, int y) { return x >= y ? c[x][y] : 0; } int main() { int n, m, r, i, j, u, d, l, ri, ans = 0, tmp; scanf( %d%d%d , &n, &m, &r); c[0][0] = 1; for (i = 1; i <= 2000; i++) { c[i][0] = 1; for (j = 1; j <= i; j++) c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % mod; } for (i = 1; i <= n; i++) { scanf( %d%d%d , &x[i], &y[i], &b[i]); x[i] += D, y[i] += D; v[x[i]][y[i]]++; } for (i = 1; i <= 3000; i++) for (j = 1; j <= 3000; j++) v[i][j] += v[i - 1][j] + v[i][j - 1] - v[i - 1][j - 1]; for (i = 1; i <= n - 1; i++) { for (j = i + 1; j <= n; j++) { u = max(y[i], y[j]) - r; d = min(y[i], y[j]) + r; l = max(x[i], x[j]) - r; ri = min(x[i], x[j]) + r; if (l <= ri && u <= d) f[i][j] = v[ri][d] - v[l - 1][d] - v[ri][u - 1] + v[l - 1][u - 1]; } } for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { if (dis(i, j) > r) w[i]++; } ans = (ans + (long long)(C(n, m) - C(w[i], m) + mod) * b[i] % mod * b[i] % mod) % mod; } for (i = 1; i <= n - 1; i++) for (j = i + 1; j <= n; j++) { tmp = ((C(w[i], m) + C(w[j], m) - C(n - (n - w[i] + n - w[j] - f[i][j]), m)) % mod + mod) % mod; ans = (ans + (long long)(C(n, m) - tmp + mod) * b[j] % mod * b[i] * 2 % mod) % mod; } printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, t1, t2; vector<int> q1, q2; vector<vector<int> > Visted; int des(int point1, int point2, int point3, int point4) { return abs(point1 - point3) + abs(point2 - point4); } int main() { cin >> n >> m >> t1; Visted.resize(n + 10, vector<int>(m + 10)); q1.resize(t1); for (int i = 0; i < t1; i++) cin >> q1[i]; cin >> t2; q2.resize(t2); for (int i = 0; i < t2; i++) cin >> q2[i]; sort(q1.begin(), q1.end()); sort(q2.begin(), q2.end()); for (int i = 0; i < t1; i++) { bool take = false; int desEnd = -1e9, X, Y; for (int j = 1; j <= n; j++) { for (int z = 1; z <= m; z++) { if (!Visted[j][z] && des(0, 0, j, z) <= q1[i] && des(0, m + 1, j, z) > desEnd) { desEnd = des(0, m + 1, j, z); take = true; X = j, Y = z; } } } if (take) { Visted[X][Y] = 1; } else { cout << NO ; return 0; } } for (int i = 0; i < t2; i++) { bool take = false; int desEnd = 1e9, X, Y; for (int j = 1; j <= n; j++) { for (int z = 1; z <= m; z++) { if (!Visted[j][z] && des(0, m + 1, j, z) <= q2[i] && des(0, m + 1, j, z) < desEnd) { take = true; X = j, Y = z; } } } if (take) { Visted[X][Y] = 1; } else { cout << NO ; return 0; } } cout << YES ; return 0; }
#include <bits/stdc++.h> using namespace std; template <class TH> void _dbg(const char *sdbg, TH h) { cerr << sdbg << = << h << endl; } template <class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) { while (*sdbg != , ) cerr << *sdbg++; cerr << = << h << , ; _dbg(sdbg + 1, a...); } template <class T> ostream &operator<<(ostream &os, vector<T> V) { os << [ ; for (auto vv : V) os << vv << , ; return os << ] ; } template <class L, class R> ostream &operator<<(ostream &os, pair<L, R> P) { return os << ( << P.first << , << P.second << ) ; } template <class T> using min_heap = priority_queue<T, vector<T>, greater<T>>; template <class T> using max_heap = priority_queue<T>; const int mod = 1e9 + 7; long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; } struct PairHash { template <typename T1, typename T2> std::size_t operator()(const pair<T1, T2> &p) const { return hash<T1>()(p.first) ^ hash<T2>()(p.second); } }; void solve(int ncase) { int a, b, c, d; cin >> a >> b >> c >> d; int minx = min({a, b, c}); a -= minx; b -= minx; c -= minx; cout << (((a % 2 + b % 2 + c % 2 + (d + 3 * 1LL * minx) % 2) <= 1) || (minx >= 1 && ((a + 1) % 2 + (b + 1) % 2 + (c + 1) % 2 + (d + 3 * 1LL * minx - 3) % 2) <= 1) ? Yes : No ) << endl; } void solve_all_cases() { int T = 1; cin >> T; int ncase = 0; while (T--) { solve(++ncase); } } int main() { cout << std::fixed; cout << setprecision(6); solve_all_cases(); }
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf( %d , &n); int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; for (int i = 0; i < n - 1; i++) cout << (a[i] + a[i + 1]) << ; cout << a[n - 1]; }
#include <bits/stdc++.h> using namespace std; int j, i, a, b, c, k, d, e, f[1005], n, mi, ma; int main() { cin >> n; for (i = 0; i < n; i++) { cin >> f[i]; ma = max(f[i], ma); } for (i = 0; i < n - 1; i++) for (j = i + 1; j < n; j++) { c = 0; for (k = i; k <= j; k++) c = f[k] ^ c; ma = max(ma, c); } cout << ma; return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) #pragma GCC target( avx,avx2,fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) using namespace std; long long MOD = 1e9 + 7; long long k; vector<long long> v, w, num; vector<pair<long long, pair<long long, long long> > > zp; vector<vector<long long> > dp; long long st(long long t, long long id, long long mod) { long long res = 1, a = t, n = id; while (n) { if (n & 1) res = (res * a) % mod; a = (a * a) % mod; n >>= 1; } return res % mod; } void add(long long id) { vector<long long> mem(k + 1); dp.push_back(mem); long long lst = dp.size() - 1; for (int i = 0; i < k + 1; i++) { dp[lst][i] = dp[lst - 1][i]; if (i >= w[id]) dp[lst][i] = max(dp[lst - 1][i], dp[lst - 1][i - w[id]] + v[id]); } } void del() { dp.pop_back(); } long long ans() { long long lst = dp.size() - 1; long long anss = 0; for (int i = 1; i < k + 1; i++) { anss = ((10000019 * (long long)anss) + (long long)dp[lst][k - i + 1]) % MOD; if (anss > MOD) anss -= MOD; } return anss; } void get(long long l, long long r) { if (r == l + 1) { if (zp[l].first == 3) { zp[l].second.first = ans(); } return; } long long mid = (l + r) / 2; long long kol = 0; for (int i = mid; i < r; i++) { if (zp[i].first == 2) { if (num[zp[i].second.second] < l) { add(zp[i].second.second); kol++; } } } get(l, mid); while (kol--) del(); kol = 0; for (int i = l; i < mid; i++) { if (zp[i].first == 1) { if (zp[i].second.second >= r) { add(zp[i].second.first); kol++; } } } get(mid, r); while (kol--) del(); } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0), cout << fixed << setprecision(20); long long n; cin >> n >> k; v.resize(n), w.resize(n), num.resize(n); vector<long long> mem(k + 1); dp.push_back(mem); for (int i = 0; i < n; i++) { cin >> v[i] >> w[i]; zp.push_back({1, {i, MOD}}); num[i] = i; } long long q; cin >> q; while (q--) { long long sz = zp.size(); zp.push_back({0, {num.size(), MOD}}); cin >> zp[zp.size() - 1].first; if (zp.back().first == 1) { long long vv, ww; cin >> vv >> ww; v.push_back(vv); w.push_back(ww); num.push_back(sz); } if (zp.back().first == 2) { long long x; cin >> x; x--; zp[num[x]].second.second = sz; zp[sz].second.second = x; } } get(0, zp.size()); for (auto i : zp) { if (i.first == 3) { cout << i.second.first << endl; } } }
#include <bits/stdc++.h> using namespace std; long long read() { long long w = 0; char c = getchar(); while (c < 0 || c > 9 ) c = getchar(); while (c >= 0 && c <= 9 ) w = w * 10 + c - 48, c = getchar(); return w; } long long n, s[100005], e[100005]; bool f[100005][2]; bool check(long long x, long long y, long long op) { if ((y & 1) && (!op)) return (y - x) & 1; long long h = y >> 1; if (x > h) return op ? 1 : (y - x) & 1; return check(x, h, op ^ 1); } signed main() { n = read(); for (long long i = 1; i <= n; i++) s[i] = read(), e[i] = read(), f[i][0] = check(s[i], e[i], 0), f[i][1] = check(s[i], e[i], 1); bool need = f[n][0]; for (long long i = n - 1; i > 0; i--) need = f[i][need]; printf(need ? 1 : 0 ); need = f[n][1]; for (long long i = n - 1; i > 0; i--) need = f[i][need]; printf(need ? 1 : 0 ); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 16, Inf = 1e9, lastS = (1 << N) - 1; static int n, sqrn, R, B, ans, lasts; struct TCard { bool c; int r, b; } card[N]; static int f[lastS][N * N + 1][2], cardr[lastS], cardb[lastS]; inline int GetF(int s, int rem, bool c) { rem = min(rem, sqrn); if (s == lasts) return 0; auto &fs = f[s][rem][c]; if (fs != -1) return fs; fs = Inf; for (int i = 0; i < n; ++i) { if (s & 1 << i) continue; int R = max(0, card[i].r - cardr[s]), B = max(0, card[i].b - cardb[s]), turn, _r, _b; if (c) { turn = max(R - rem, B); _r = rem + turn - R; _b = turn - B; } else { turn = max(B - rem, R); _b = rem + turn - B; _r = turn - R; } if (_r) fs = min(fs, turn + GetF(s ^ 1 << i, _r, 1)); else fs = min(fs, turn + GetF(s ^ 1 << i, _b, 0)); } return fs; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; sqrn = n * n; lasts = (1 << n) - 1; for_each(card, card + n, [](TCard &C) { char tmp; cin >> tmp >> C.r >> C.b; C.c = tmp == R ; if (C.r > n) R += C.r - n, C.r = n; if (C.b > n) B += C.b - n, C.b = n; }); for (int s = 1; s <= lasts; ++s) { int i = __builtin_ctz(s); cardr[s] = cardr[s ^ 1 << i] + card[i].c; cardb[s] = cardb[s ^ 1 << i] + !card[i].c; } memset(f, -1, sizeof f); if (R > B) ans = R + GetF(0, R - B, 0); else ans = B + GetF(0, B - R, 1); cout << ans + n; return 0; }
#include <bits/stdc++.h> using namespace std; ifstream file( input.txt ); using namespace std; void maker(int x); int main() { int y; cin >> y; maker(y + 1); } void maker(int x) { int a, b, c, d; a = (x / 1000) % 10; b = (x / 100) % 10; c = (x / 10) % 10; d = (x / 1) % 10; while (a == b || a == c || a == d || b == c || b == d || c == d) { x++; a = (x / 1000) % 10; b = (x / 100) % 10; c = (x / 10) % 10; d = (x / 1) % 10; } cout << (a * 1000 + b * 100 + c * 10 + d); }
#include <bits/stdc++.h> using namespace std; template <typename... Args> void logger(string vars, Args&&... values) {} template <typename T> inline void maxa(T& a, T b) { a = max(a, b); } template <typename T> inline void mina(T& a, T b) { a = min(a, b); } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const int mxN = 2e5 + 5; const int mod = 1e9 + 7; int main() { cin.sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; vector<int> a(n); vector<char> ans(n); for (int& i : a) cin >> i; string s; cin >> s; sort(a.begin(), a.end()); int cnt = 0; for (int i = 1; i < n; i++) cnt += (s[i] != s[i - 1]); int l = n - cnt - 1; int r = n - cnt - 1; cout << a[l] << << s[0] << n ; ans[l] = s[0]; for (int i = 1; i < (int)(s.size()); i++) { if (s[i] != s[i - 1]) { ans[++r] = ans[r] == L ? R : L ; cout << a[r] << << ans[r] << n ; } else { ans[--l] = ans[l] == L ? R : L ; cout << a[l] << << ans[l] << n ; } } }
#include <bits/stdc++.h> using namespace std; const int maxn = 2000 + 100; map<pair<long long, long long>, int> rat; int n; pair<long long, long long> px[maxn], py[maxn]; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int main() { cin >> n; for (int i = 1, a, b, c; i <= n; i++) { cin >> a >> b >> c; px[i] = make_pair(c * b, a * a + b * b); py[i] = make_pair(a * c, a * a + b * b); } long long ans = 0; for (int i = 1; i <= n; i++) { rat.clear(); long long two0 = 0; for (int j = i + 1; j <= n; j++) { long long a = px[j].first * px[i].second - px[j].second * px[i].first; long long b = py[j].first * py[i].second - py[j].second * py[i].first; long long g = gcd(abs(a), abs(b)); if (g != 0) a /= g, b /= g; if (a == 0 && b == 0) { two0++; ans += j - i - 1; } else { if (a == 0) b = 1; if (b == 0) a = 1; if (a < 0 && b < 0) a *= -1, b *= -1; if (a > 0 && b < 0) a *= -1, b *= -1; ans += two0; if (rat.count(make_pair(a, b))) { ans += rat[make_pair(a, b)]; rat[make_pair(a, b)]++; } else rat[make_pair(a, b)] = 1; } } } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 25; int dp[MAXN][MAXN][MAXN]; int n, t; int main() { scanf( %d%d , &n, &t); t = 2 * t; dp[1][0][1] = dp[1][0][2] = dp[1][0][3] = 1; for (int i = 1; i < n; ++i) for (int j = 0; j <= t; ++j) for (int k = 1; k <= 4; ++k) for (int l = 1; l <= 4; ++l) { if ((i == 1 && l > k) || (i != 1 && l != k)) { int nj = j; if (j % 2 && l < k) ++nj; else if (j % 2 == 0 && l > k) ++nj; dp[i + 1][nj][l] += dp[i][j][k]; } } int ans = 0; for (int i = 1; i <= 4; ++i) ans += dp[n][t][i]; printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; multiset<long long int> s; map<string, int> mp; while (t--) { char ch; long long int x; cin >> ch >> x; string str(18, 0 ); string temp = to_string(x); int j = 17; for (int i = ((int)(temp).size()) - 1; i >= 0; i--) { if ((temp[i] - 0 ) % 2 != 0) { str[j] = 1 ; } j--; } if (ch == + ) { mp[str]++; } else if (ch == - ) { mp[str]--; } else { cout << mp[str] << n ; } } }
#include <bits/stdc++.h> #pragma warning(disable : 4996) #pragma comment(linker, /STACK:666000000 ) using namespace std; const int INF = (1 << 30) - 1; const long double EPS = 1e-9; const long double PI = fabs(atan2(0.0, -1.0)); int n; void load() { cin >> n; } const int SZ = 1010; int sv[SZ]; int rec(const int v) { if (v < SZ && -1 != sv[v]) return sv[v]; bool can[10]; int p9; memset(can, false, sizeof(can)); { int p = 0; int tmp = v; while (tmp != 0) { can[tmp % 10] = true; if (9 == tmp % 10) p9 = p; tmp /= 10; p *= 10; } } if (v >= SZ) { if (can[9] && p9 > 10) { int cnt = 9 * (p9 / 10) - 8; return cnt + rec(v - cnt * 9); } for (int i = 9; i >= 1; i--) if (can[i]) return 1 + rec(v - i); } int res = INT_MAX; for (int i = 1; i <= 9; i++) if (can[i]) res = min(res, 1 + rec(v - i)); if (v < SZ) sv[v] = res; return res; } void solve() { memset(sv, -1, sizeof(sv)); sv[0] = 0; int res = rec(n); cout << res; } int main() { load(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 1e5; map<int, int> dic; int n, a[N + 10], ma[N + 10], ma2[N + 10]; int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; ma[1] = a[1]; ma2[1] = -1; for (int i = 2; i <= n; i++) { if (a[i] > ma[i - 1]) { ma2[i] = ma[i - 1]; ma[i] = a[i]; } else { ma[i] = ma[i - 1]; if (ma2[i - 1] < a[i]) { ma2[i] = a[i]; } else ma2[i] = ma2[i - 1]; } if (a[i] != ma[i] && a[i] == ma2[i]) { dic[ma[i]]++; } } int tot = -1e6, idx = -1; for (int i = 1; i <= n; i++) { int temp = dic[a[i]]; if (a[i] == ma[i]) temp--; if (temp > tot) { tot = temp; idx = a[i]; } else if (temp == tot) { idx = min(idx, a[i]); } } cout << idx << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long a[1000005] = {0}; long long b[1000005] = {0}; long long c[1000005] = {0}; long long d[1000005] = {0}; long long s; long long i, j = 0, l; long long count1 = 0; long long count2 = 0; long long count3 = 0; long long count4 = 0; long long ans = 0; bool cmp(long long aa, long long bb) { return aa > bb; } int main() { long long n; scanf( %lld n , &n); for (i = 0; i < n; i++) { cin >> s >> j; if (s == 11) { a[count1++] = j; ans += j; } else if (s == 10) { b[count2++] = j; } else if (s == 1) { c[count3++] = j; } else { d[count4++] = j; } } sort(b, b + count2, cmp); sort(c, c + count3, cmp); for (i = 0; i < min(count2, count3); i++) ans += (b[i] + c[i]); for (i = 0; i < abs(count2 - count3); i++) { if (count3 > count2) d[count4 + i] = c[min(count2, count3) + i]; else if (count3 < count2) d[count4 + i] = b[min(count2, count3) + i]; } count4 += abs(count2 - count3); sort(d, d + count4, cmp); for (i = 0; i < count1 && i < count4; i++) ans += d[i]; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int a[35], b[35], diff[35]; int main() { std::ios::sync_with_stdio(false); int t; t = 1; while (t--) { long long x, x1, val, val1, i, j, res = 0; cout << ? 0 0 << endl; cin >> x; x1 = x; if (x == 0) { for (i = 0; i < 30; i++) { val = (1 << i); cout << ? << val << << 0 << endl; cin >> x; if (x == -1) { res += (1 << i); } } cout << ! << res << << res << endl; } else { { x1 = x; i = 29; val = 0; val1 = 0; for (i = 29; i >= 0; i--) { val += (1 << i); val1 += (1 << i); cout << ? << val << << val1 << endl; cin >> x; if (x != x1) { diff[i] = 1; val -= (1 << i); if (x == -1) { a[i] = 1; b[i] = 0; } else { a[i] = 0; b[i] = 1; } cout << ? << val << << val1 << endl; cin >> x; x1 = x; } else { diff[i] = 0; } } for (i = 29; i >= 0; i--) { val = 0; val1 = 0; if (diff[i]) { continue; } for (j = 0; j < 30; j++) { if (j == i) { continue; } if (diff[j]) { val += (1 << j); } } { val += (1 << i); cout << ? << val << << val1 << endl; cin >> x; if (x == -1) { a[i] = 1; b[i] = 1; } else { a[i] = 0; b[i] = 0; } } } val = 0; val1 = 0; for (i = 0; i < 30; i++) { if (a[i]) { val += (1 << i); } if (b[i]) { val1 += (1 << i); } } cout << ! << val << << val1 << endl; } } } }
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int n, k; cin >> n >> k; long long int ans = 1; while (k % 2 == 0) { k = k / 2; ans++; } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; const int N = 2e4 + 10, mod = 1e9 + 7, M = 510; const double PI = 3.1415926535; long long res[40], p[40], ei[40], sum[N], cnt; double pre[N]; int path[100]; void dfs(int u, int s, long long va) { if (u == -1) { sum[s] = ((long long)sum[s] + va) % mod; return; } for (int i = 0; i <= cnt; i++) { path[i]++; if (res[i] - path[i] + 1) dfs(u - 1, s + i, (long long)va * (res[i] - path[i] + 1) % mod); path[i]--; } } int main() { int n; scanf( %d , &n); vector<int> temp; int t = n; while (t) t /= 10, cnt++; p[1] = 1; for (int i = 2; i <= 11; i++) p[i] = p[i - 1] * 10; ei[0] = 1; for (int i = 1; i < 11; i++) ei[i] = ei[i - 1] * 8; for (int i = 0; i < (1 << cnt); i++) { vector<int> temp; for (int j = 0; j < cnt; j++) if (i >> j & 1) temp.push_back(j); int len = temp.size(); for (int j = 0; j < 1 << len; j++) { int a[15] = {0}; for (int k = 0; k < len; k++) if (j >> k & 1) a[temp[k]] = 7; else a[temp[k]] = 4; long long f = p[cnt], va = 0; for (long long k = cnt - 1, r = 1; k >= 0; k--, r *= 10) va += (long long)a[k] * r; if (va > n) continue; for (int k = 0; k < cnt; k++) { if (a[k]) { f /= 10; continue; } for (int r = 9; r >= 0; r--) if ((long long)r * f + va <= n) { a[k] = r, va = r * f + va; break; } f /= 10; } long long num = 0, ans = 0; for (int k = 0; k < cnt; k++) { if (num < temp.size() && temp[num] == k) { num++; if (k == cnt - 1) ans++; continue; } if (a[k] == 7 || a[k] == 4) { int sum = cnt - 1 - k; int ts = temp.size() - num; int tnum = a[k] + 1; if (a[k] == 4) tnum--; else tnum -= 2; ans = ans + ei[sum - ts] * (long long)tnum; break; } else { int sum = cnt - 1 - k; int ts = temp.size() - num; int tnum = a[k] + 1; if (a[k] > 7) tnum -= 3; else if (a[k] > 4) tnum -= 2; else tnum--; if (k == cnt - 1) tnum++; ans = ans + ei[sum - ts] * (long long)tnum; } } res[len] = (res[len] + (long long)ans); } } res[0]--; dfs(5, 0, 1); long long ans = 0, pre = 0; for (int i = 1; i <= cnt; i++) { pre = pre + sum[i - 1]; ans = (ans + pre * res[i]) % mod; } cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int N, good; bool prime[100010]; vector<int> fact, num, lis; int main() { memset(prime, true, sizeof(prime)); for (int i = 2; i * i <= 100000; i++) if (prime[i]) for (int j = 2; i * j <= 100000; j++) prime[i * j] = false; for (int i = 2; i <= 100000; i++) if (prime[i]) { num.push_back(i); lis.push_back(0); } scanf( %d , &N); while (N--) { scanf( %d , &good); int temp = good; fact.clear(); int longest = 0; for (int i = 0; i < num.size() && num[i] * num[i] <= good; i++) if (good % num[i] == 0) { fact.push_back(i); longest = max(longest, lis[i] + 1); while (temp % num[i] == 0) temp /= num[i]; } if (temp > 1) { int i = lower_bound(num.begin(), num.end(), temp) - num.begin(); fact.push_back(i); longest = max(longest, lis[i] + 1); } for (int i = 0; i < fact.size(); i++) lis[fact[i]] = max(lis[fact[i]], longest); } int ans = 1; for (int i = 0; i < num.size(); i++) ans = max(ans, lis[i]); printf( %d n , ans); return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2300; int dp[N], a[N], res = 0, n; void dfs(int u, int d) { if (d == n) { dp[u] = 0; return; } int l = u * 2, r = l + 1; dfs(l, d + 1); dfs(r, d + 1); int x = dp[l] + a[l], y = dp[r] + a[r]; if (x < y) swap(x, y); dp[u] = x; res += x - y; } int main() { cin >> n; for (int i = 2; i < (1 << n + 1); i++) { scanf( %d , &a[i]); } dfs(1, 0); cout << res << endl; }
#include <bits/stdc++.h> using namespace std; long long n, root, val, rem; int main() { cin >> n; root = floor((sqrt(1.0 + 8.0 * n) - 1.0) / 2.0); val = root * (root + 1) / 2; if (n == val) --root; val = root * (root + 1) / 2; rem = n - val; cout << rem << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 2; const long long M = 1e6 + 2; const long long mod = 998244353; long long ar[N], fact[M], inv[M], cnt[N]; long long binpow(long long x, long long y) { long long tich = 1; while (y) { if (y & 1) { tich *= x; tich %= mod; } x *= x; x %= mod; y >>= 1; } return tich; } long long C(long long x, long long y) { return ((fact[y] * inv[y - x]) % mod * inv[x]) % mod; } signed main() { ios::sync_with_stdio(0); cin.tie(0); long long n, i, j, k, l, ans = 0, sum = 0; cin >> n; for (i = 1; i <= n; i++) { cin >> ar[i]; cnt[ar[i] % n]++; sum += ar[i]; } fact[0] = 1; for (i = 1; i < M - 1; i++) { fact[i] = (fact[i - 1] * i) % mod; } inv[M - 2] = binpow(fact[M - 2], mod - 2); for (i = M - 2; i > 0; i--) { inv[i - 1] = (inv[i] * i) % mod; } sort(ar + 1, ar + 1 + n); long long pos = 0; for (i = 1; i < n; i++) { if (ar[i + 1] < i) { pos = i; break; } } if (pos) { j = 1; for (i = 0; i < pos; i++) { while (ar[j] < i) { j++; } ans += C(n - 1, i - (j - 1) + n - 1); if (ans >= mod) { ans -= mod; } } cout << ans; } else { for (i = 1; i <= n; i++) { sum -= ar[i] % n; } sum /= n; for (i = 1; i <= n; i++) { sum = sum + cnt[n - i] - 1; if (sum >= 0) { ans += C(n - 1, sum + n - 1); if (ans >= mod) { ans -= mod; } } } cout << ans; } }
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; vector<pair<int, int>> v; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; v.push_back(make_pair(a, b)); } int x1 = v[0].first; int y1 = v[0].second; vector<int> p1, p2; for (int i = 1; i < m; i++) { int a = v[i].first; int b = v[i].second; if (x1 != a && x1 != b) { p1.push_back(i); } if (y1 != a && y1 != b) { p2.push_back(i); } } if (p1.size() == 0 || p2.size() == 0) { cout << YES << endl; return 0; } x1 = v[p1[0]].first; y1 = v[p1[0]].second; int c1 = 0, c2 = 0, c3 = 0, c4 = 0; for (int i = 1; i < p1.size(); i++) { int a = v[p1[i]].first; int b = v[p1[i]].second; if (x1 != a && x1 != b) { c1++; } if (y1 != a && y1 != b) { c2++; } } x1 = v[p2[0]].first; y1 = v[p2[0]].second; for (int i = 1; i < p2.size(); i++) { int a = v[p2[i]].first; int b = v[p2[i]].second; if (x1 != a && x1 != b) { c3++; } if (y1 != a && y1 != b) { c4++; } } if (c1 == 0 || c2 == 0) { cout << YES << endl; } else if (c3 == 0 || c4 == 0) { cout << YES << endl; } else { cout << NO << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> inline bool chmax(T &a, T b) { return a < b ? (a = b, 1) : 0; } template <class T> inline bool chmin(T &a, T b) { return a > b ? (a = b, 1) : 0; } int main() { int n, m; cin >> n >> m; map<int, vector<int>> obs; int left = n; while (m--) { int x, y; cin >> x >> y; if (x == 1) chmin(left, y - 1); else obs[x].emplace_back(y); } vector<pair<int, int>> segs = {{1, left}}; int prv = 1; for (auto &p : obs) { auto &v = p.second; sort(begin(v), end(v)); if (p.first != prv + 1) { segs.assign(1, {segs.begin()->first, n}); } vector<pair<int, int>> segs2; auto it = segs.begin(); int p_v = 0, sz_v = v.size(); int lst = -1; while (it != segs.end() && p_v < sz_v) { chmax(it->first, lst); if (it->first > it->second) { it++; continue; } if (v[p_v] <= it->first) { it->first += v[p_v++] == it->first; continue; } segs2.emplace_back(it->first, v[p_v] - 1); lst = v[p_v++] + 1; } if (p_v == sz_v) { while (it != segs.end()) { chmax(it->first, lst); if (it->first <= it->second) break; it++; } if (it != segs.end()) { segs2.emplace_back(it->first, n); } } if ((segs = segs2).empty()) return !(cout << -1 << endl); prv = p.first; } if (prv < n) { segs.assign(1, {segs.begin()->first, n}); } cout << (segs.rbegin()->second == n ? (n - 1) * 2 : -1) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int t; cin >> t; while (t--) { long long n, k; cin >> n >> k; if (n > k) cout << k << endl; else { int ne = (k - 1) / (n - 1); cout << k + ne << endl; } } }
#include <bits/stdc++.h> int mat[110][110]; int main() { int n, k, i, j; scanf( %d %d , &n, &k); if (n * n < k) { printf( -1 n ); return 0; } for (i = 0; i < n; i++) { for (j = i; j < n; j++) { if (i == j) { if (k > 0) { k--; mat[i][j] = 1; } else break; } else { if (k >= 2) { mat[i][j] = 1; mat[j][i] = 1; k = k - 2; } else if (k == 0) break; } } } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf( %d , mat[i][j]); } printf( n ); } return 0; }
#include <bits/stdc++.h> int n, k; bool visited[101][101][1010]; double ar[101], dp[101][101][1010]; double F(int i, int j, int k) { if (i > j) return 1.0 - F(j, i, k); if (k == 0) return 1.0; if (visited[i][j][k]) return dp[i][j][k]; int a, b, c, d, l; double x, y, p = 1.0 / ((n * (n + 1)) >> 1), res = 0.0; for (a = 0; a < n; a++) { for (b = a; b < n; b++) { c = i, d = j; if (c >= a && c <= b) c = b - (c - a); if (d >= a && d <= b) d = b - (d - a); res += (p * F(c, d, k - 1)); } } visited[i][j][k] = true; return (dp[i][j][k] = res); } int main() { int i, j, l; while (scanf( %d %d , &n, &k) != EOF) { if (k > 1000) k = 1000; for (i = 0; i < n; i++) scanf( %d , &ar[i]); memset(visited, 0, sizeof(visited)); double res = 0.0; for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (ar[i] < ar[j]) res += (1.0 - F(i, j, k)); else res += F(i, j, k); } } printf( %0.12f n , res + 1e-15); } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); const double EPS = 1e-9; const double PI = acos(-1.); const long long LL_INF = 1e17 + 16; const int INF = 1e9 + 10; const long long MOD = 1e9 + 7; const int MAXN = 20; set<long long> goods; long long f[MAXN]; void gen() { for (int i = (1); i <= (int)(10); i++) { for (int j = (0); j < (int)(1 << i); j++) { long long v = 0; long long st10 = 1; for (int k = (0); k < (int)(i); k++) { if (j & (1 << k)) { v += 7 * st10; } else { v += 4 * st10; } st10 *= 10; } goods.insert(v); } } } int per[MAXN]; int used[MAXN]; void solve() { gen(); long long n, k; cin >> n >> k; f[0] = 1; int m; for (m = 1;; m++) { f[m] = f[m - 1] * m; if (f[m] >= k) { break; } } if (m > n) { printf( -1 n ); return; } int ans = 0; int offset = n - m; for (__typeof((goods).begin()) it = (goods).begin(); it != (goods).end(); it++) { if ((*it) <= offset) { ans++; } } k--; int top = 1; for (int i = m; i >= 1; i--) { int v = k / f[i - 1] + 1; int cnt = 0; for (int j = (1); j <= (int)(m); j++) { if (!used[j]) { cnt++; if (cnt == v) { per[top++] = j; used[j] = 1; break; } } } k %= f[i - 1]; } for (int i = (1); i <= (int)(m); i++) { if (goods.count(i + offset) && goods.count(per[i] + offset)) { ans++; } } cout << ans << endl; } int main() { int t = 1; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; void dfs(set<int> &temp, list<int> adj[], vector<bool> &v, int s, int &t1, int &t2, vector<int> b, vector<int> w) { v[s] = true; list<int>::iterator it; for (it = adj[s].begin(); it != adj[s].end(); it++) { if (!v[*it]) { temp.insert(*it); t1 += b[*it]; t2 += w[*it]; dfs(temp, adj, v, *it, t1, t2, b, w); } } } list<int> adj[1001]; int main() { int n, m, weight; cin >> n >> m >> weight; vector<int> b(n + 1); vector<int> w(n + 1); for (int i = 1; i <= n; i++) cin >> w[i]; for (int i = 1; i <= n; i++) cin >> b[i]; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } vector<bool> v(n + 1, false); vector<set<int> > arr; vector<int> tw; vector<int> tb; for (int i = 1; i <= n; i++) { if (!v[i]) { set<int> temp; temp.insert(i); int t1 = b[i], t2 = w[i]; dfs(temp, adj, v, i, t1, t2, b, w); arr.push_back(temp); tb.push_back(t1); tw.push_back(t2); } } vector<vector<int> > dp(arr.size() + 1, vector<int>(weight + 1, 0)); for (int i = 1; i <= arr.size(); i++) { for (int j = 1; j <= weight; j++) { int max = 0; if (dp[i - 1][j] > max) max = dp[i - 1][j]; if (j - tw[i - 1] >= 0) if (dp[i - 1][j - tw[i - 1]] + tb[i - 1] > max) max = dp[i - 1][j - tw[i - 1]] + tb[i - 1]; set<int>::iterator it; for (it = arr[i - 1].begin(); it != arr[i - 1].end(); it++) { if (j - w[*it] >= 0) if (dp[i - 1][j - w[*it]] + b[*it] > max) max = dp[i - 1][j - w[*it]] + b[*it]; } dp[i][j] = max; } } int max = 0; for (int i = 0; i <= weight; i++) if (dp[arr.size()][i] > max) max = dp[arr.size()][i]; cout << max; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, m, ans, x, i, j, s; double a[100], b[100]; map<double, pair<long long, long long> > mp; map<double, pair<long long, long long> >::iterator it, it2; long long num(long long x) { s = 0; while (x) { s++; x -= x & -x; } return s; } int main() { cin >> n >> m; for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < m; i++) cin >> b[i]; for (i = 0; i < n; i++) for (j = 0; j < m; j++) { mp[(a[i] + b[j]) / 2.0].first |= (1LL << i); mp[(a[i] + b[j]) / 2.0].second |= (1LL << j); } for (it = mp.begin(); it != mp.end(); it++) for (it2 = it; it2 != mp.end(); it2++) { x = num((*it).second.first | (*it2).second.first) + num((*it).second.second | (*it2).second.second); if (x > ans) ans = x; } cout << ans; return 0; }
#include <bits/stdc++.h> int main() { long long n, k; scanf( %I64d , &n); while (n % 3 == 0) n /= 3; printf( %I64d , 1 + n / 3); getchar(); getchar(); }
#include <bits/stdc++.h> using namespace std; constexpr long long INF = numeric_limits<long long>::max() / 4; constexpr long long n_max = 2e5 + 10; template <typename A, typename B> string to_string(pair<A, B> p); string to_string(const string &s) { return + s + ; } string to_string(const char c) { return to_string((string)&c); } string to_string(bool b) { return (b ? true : false ); } template <size_t N> string to_string(bitset<N> v) { string res = ; for (size_t i = 0; i < N; i++) res += static_cast<char>( 0 + v[i]); return res; } template <typename A> string to_string(A v) { bool first = true; string res = { ; for (const auto &x : v) { if (!first) res += , ; first = false; res += to_string(x); } res += } ; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return ( + to_string(p.first) + , + to_string(p.second) + ) ; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << << to_string(H); debug_out(T...); } template <class T> bool chmax(T &a, T b) { if (a < b) { a = b; return true; } return false; } template <class T> bool chmin(T &a, T b) { if (a > b) { a = b; return true; } return false; } signed main() { cin.tie(nullptr); ios::sync_with_stdio(false); long long t; cin >> t; for (long long _ = 0; _ < (long long)(t); _++) { long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < (long long)(n); i++) cin >> a[i]; vector<long long> dp(n + 1); for (long long i = 1; i < (long long)(n) + 1; i++) { dp[i]++; for (long long j = i * 2; j <= n; j += i) { if (a[j - 1] > a[i - 1]) chmax(dp[j], dp[i]); } } long long ans = 0; for (long long i = 0; i < (long long)(n + 1); i++) chmax(ans, dp[i]); cout << ans << n ; } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int test = 1; cin >> test; while (test--) { long long int n, m; cin >> n >> m; long long int sum1 = 0, sum2 = 0; long long int a[n], b[n], mina[n], mi = 0; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; sum1 += a[i]; sum2 += b[i]; mina[i] = max(0LL, m - b[i]); mi += mina[i]; a[i] = min(a[i], m); } long long int ans = sum1 - sum2 + (n * m); long long int x = ans - 2LL * mi; for (int i = 0; i < n; i++) { if (x <= 0) break; long long int temp = min(2LL * (a[i] - mina[i]), x); temp /= 2; mina[i] += temp; x -= 2LL * temp; } cout << abs(x) << n ; for (int i = 0; i < n; i++) cout << mina[i] << << m - mina[i] << n ; } return 0; }
#include <bits/stdc++.h> #pragma warning(disable : 4996) using namespace std; bool compare(int a, int b) { return a > b; } int main() { int t, p, f, cnts, cntw, s, w; scanf( %d , &t); while (t--) { scanf( %d%d , &p, &f); scanf( %d%d , &cnts, &cntw); scanf( %d%d , &s, &w); if (s > w) { int temp = s; s = w; w = temp; temp = cnts; cnts = cntw; cntw = temp; } int mx = min(cnts, p / s); int ans = 0; for (int s1 = 0; s1 <= mx; s1++) { int w1 = min(cntw, (p - s1 * s) / w); int s2 = min(cnts - s1, f / s); int w2 = min(cntw - w1, (f - s2 * s) / w); ans = max(ans, s1 + s2 + w1 + w2); } printf( %d n , ans); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 10; long long n, k; int main() { scanf( %I64d%I64d , &n, &k); long long x; x = sqrt(9 + 8 * (n + k)) - 3; x /= 2; printf( %I64d n , n - x); return 0; }
#include <bits/stdc++.h> using namespace std; const long long MOD = 998244353; const int N = 3e3; long long dmod(long long x) { return x % MOD; } int n, k; int h[N + 5]; long long memo[N + 5][2 * N + 5]; long long dp(int idx, int gap) { if (idx > n) { if (gap > 0) return 1; return 0; } long long &ret = memo[idx][gap + N]; if (ret != -1) return ret; ret = 0ll; if (h[idx] == h[idx % n + 1]) { ret = dmod((long long)k * dp(idx + 1, gap)); } else { ret = dp(idx + 1, gap + 1); ret = dmod(ret + dp(idx + 1, gap - 1)); ret = dmod(ret + dmod((long long)(k - 2) * dp(idx + 1, gap))); } return ret; } int main() { memset(memo, -1, sizeof(memo)); scanf( %d%d , &n, &k); for (int i = 1; i <= n; i++) { scanf( %d , &h[i]); } printf( %lld n , dp(1, 0)); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, x, m = 0, sum = 0; cin >> n >> x; int *arr = new int[n]; for (long long i = 0; i < n; i++) cin >> arr[i], sum += arr[i]; if (sum > 0) while (sum > 0) { sum -= x; m++; } else while (sum < 0) { sum += x; m++; } cout << m; return 0; }
#include <bits/stdc++.h> using namespace std; int brute(int s, int x) { int ans = 0; for (int i = 1; i <= s; ++i) for (int j = 1; j <= s; ++j) if (i + j == s && (i ^ j) == x) ++ans; return ans; } long long ways0(long long s, long long x) { if (s == x) return 2; return 0; } long long solve(long long s, long long x) { long long dp[60][2] = {}; if (s % 2 != x % 2) { return 0; } if (x & (1LL << 0)) { dp[0][0] = 2; } else { dp[0][0] = 1; dp[0][1] = 1; } for (int i = 1; i < 60; ++i) { if (x & (1LL << i)) { if (s & (1LL << i)) { dp[i][0] += 2 * dp[i - 1][0]; } else { dp[i][1] += 2 * dp[i - 1][1]; } } else { if (s & (1LL << i)) { dp[i][0] += dp[i - 1][1]; dp[i][1] += dp[i - 1][1]; } else { dp[i][0] += dp[i - 1][0]; dp[i][1] += dp[i - 1][0]; } } } return dp[59][0] - ways0(s, x); } int main() { long long s, x; cin >> s >> x; cout << solve(s, x) << n ; }
#include <bits/stdc++.h> using namespace std; template <class T> inline void dmin(T& x, T y) { y < x ? x = y : 0; } template <class T> inline void dmax(T& x, T y) { y > x ? x = y : 0; } template <class T> inline void dmin(T& x, vector<T> y) { for (auto t : y) t < x ? x = t : 0; } template <class T> inline void dmax(T& x, vector<T> y) { for (auto t : y) t > x ? x = t : 0; } const int N = 5 + 1e6; const long long INF = -2e18; int n, k; long long gk(int x, long long a[], int i, long long f) { return a[x] + (i - x) / 3 * f; } bool cmp(int x, int y, long long a[], int i, long long f) { return gk(x, a, i, f) > gk(y, a, i, f); } void opera(long long a[], int n, long long f, int k) { static long long b[N]; static long long q[3][N], left[3], right[3]; for (int i = 0; i < 3; ++i) left[i] = 1, right[i] = 0; for (int i = 0; i < n; ++i) b[i] = INF; for (int i = 0; i < n; ++i) { int t = i % 3; while (left[t] <= right[t] && q[t][left[t]] < i - 3 * k) ++left[t]; while (left[t] <= right[t] && cmp(i, q[t][right[t]], a, i, f)) --right[t]; q[t][++right[t]] = i; b[i] = gk(q[t][left[t]], a, i, f); } for (int i = 0; i < n; ++i) a[i] = b[i]; } void opera2(long long a[], int n, long long f, int k) { static long long f1[N], f2[N]; for (int i = 0; i < n; ++i) { f1[i] = f2[i] = a[i]; for (int j = 1; j < 10 && i - j >= 0; ++j) f2[i] = max(f2[i], a[i - j]); } opera(f1, n, f, 3 * k); opera(f2, n, f, 3 * (k - 1)); for (int i = 0; i < n; ++i) a[i] = max(f1[i], f2[i]); } long long a[N], b[N]; int main() { int k; scanf( %d , &k); long long f[6]; for (int i = 0; i < 6; ++i) scanf( %lld , &f[i]); a[0] = 0; n = 1; for (int i = 5; i >= 0; --i) { for (int j = 0; j < 10 * n; ++j) b[j] = INF; for (int j = 0; j < n; ++j) b[j * 10] = a[j]; n *= 10; opera2(b, n, f[i], k); for (int j = 0; j < n; ++j) a[j] = b[j]; } int q; scanf( %d , &q); while (q--) { int x; scanf( %d , &x); printf( %lld n , a[x]); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); bool f = true; for (int j = 1; j < n; j++) { int diff = abs(arr[j] - arr[j - 1]); if (diff > 1) { f = false; } } if (f) { cout << YES n ; } else { cout << NO n ; } } }
#include <bits/stdc++.h> using namespace std; int a, b, k, t, suan, d[1100002], f[1110000]; int main() { cin >> a >> b >> k >> t; int shu = k * 2 * t; d[shu] = 1; int y = d[0]; for (int i = 1; i <= t * 2; i++) { f[0] = y; for (int j = 1; j <= shu * 2 + k; j++) { f[j] = (f[j - 1] + d[j]) % 1000000007; } for (int j = 0; j <= k; j++) { d[j] = f[j + k]; } for (int j = k + 1; j <= shu * 2; j++) { d[j] = (f[j + k] - f[j - 1 - k] + 1000000007) % 1000000007; } } for (int i = shu - a + b + 1; i <= shu * 2; i++) { suan = (suan + d[i]) % 1000000007; } cout << suan; return 0; }
#include <bits/stdc++.h> using namespace std; long long int modulo = 1000000007; const int limite = 1000001; long long int elev[limite]; int ocupado[limite]; int main() { ios_base::sync_with_stdio(false); int n, m, k; cin >> n >> m >> k; elev[0] = 1; for (int i = 1; i < limite; i++) elev[i] = (elev[i - 1] * 2) % modulo; int ini = 0; int fin = n + 1; int inf = k + 2; int sup = n; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; if (v == u + 1) ; else if (v == u + k + 1) { ocupado[v] = 1; ini = max(ini, u); fin = min(fin, v); inf = max(inf, u + 1); sup = min(sup, v + k); } else { cout << 0 << endl; exit(0); } } if (fin <= ini) { cout << 0 << endl; exit(0); } int libres = 0; long long int maneras = 1; for (int i = inf; i <= sup; i++) { if (not ocupado[i]) { maneras = (maneras + elev[libres]) % modulo; libres++; } if (i - k >= inf and not ocupado[i - k]) libres--; } cout << maneras << endl; }
#include <bits/stdc++.h> using namespace std; bool v[6]; int a[100009][6], maxx = -1, t; long long m = 1e9 + 7; struct pi { long long ah[60][60]; }; pi multi(pi x, pi y) { pi z; for (int i = 1; i <= t; i++) { for (int j = 1; j <= t; j++) { z.ah[i][j] = 0; for (int k = 1; k <= t; k++) z.ah[i][j] = (z.ah[i][j] % m + x.ah[i][k] % m * y.ah[k][j] % m) % m; z.ah[i][j] %= m; } } return z; } pi quimul(pi x, long long int n) { pi an; for (int i = 1; i <= t; i++) an.ah[i][i] = 1; for (; n; n >>= 1) { if (n & 1) an = multi(an, x); x = multi(x, x); } return an; } int main() { long long n; int x, v, p[600], zh[90][90]; char c = a , u; for (int i = 1; i <= 52; i++) for (int j = 1; j <= 52; j++) zh[i][j] = 1; for (int i = 1; i <= 26; i++) p[(int)c + i - 1] = i; c = A ; for (int i = 27; i <= 52; i++) p[(int)c + i - 27] = i; cin >> n >> t >> v; for (int i = 0; i < v; i++) { scanf( %c %c , &c, &u); zh[p[(int)c]][p[(int)u]] = 0; } pi ans; for (int i = 1; i <= t; i++) for (int j = 1; j <= t; j++) ans.ah[i][j] = zh[i][j]; ans = quimul(ans, n - 1); long long int pp = 0; for (int i = 1; i <= t; i++) for (int j = 1; j <= t; j++) pp = (pp + ans.ah[i][j]) % m; cout << (pp + m) % m << endl; }
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, n, i, j, money; cin >> a >> b >> c; cin >> n; int m[n]; money = 0; for (i = 0; i < n; i++) { cin >> m[i]; if (m[i] > b && m[i] < c) money = money + 1; } if (a > b && a < c) cout << money; else cout << 0; return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> void display(T a[], int size) { for (int i = 0; i < size; i++) { cout << a[i] << ; } cout << endl; } void display(vector<pair<int, int> > a) { for (int i = 0; i < a.size(); i++) { cout << ( << a[i].first << , << a[i].second << ) << ; } cout << endl; } void display(const vector<int>& a) { for (int i = 0; i < a.size(); i++) { cout << a[i]; if (i == a.size() - 1) cout << endl; else cout << ; } } template <typename T> void initialise(T a[], T value, int length) { for (int i = 0; i < length; i++) a[i] = value; } template <typename T> void initialise(vector<T>& a, T value) { for (int i = 0; i < a.size(); i++) a[i] = value; } bool compare(pair<int, int> a, pair<int, int> b) { if (a.first != b.first) return a.first < b.first; else return a.second < b.second; } int max(int a[], int n) { int max = -1000000000; for (int i = 0; i < n; i++) { if (a[i] > max) max = a[i]; } return max; } bool find(string a[], string s, int n) { int left = 0; int right = n - 1; while (left < right) { int mid = (left + right) / 2; if (s.compare(a[mid]) == 0) return true; else if (s.compare(a[mid]) < 0) { right = mid; } else { left = mid + 1; } } return false; } void factor(int a[], int base, int num, int n) { for (int i = n - 1; i >= 0; i--) { a[i] = num % base; num = num / base; } } int findLength(int n, int base) { int i = 0; while (n > 0) { i++; n = n / base; } return i; } int gcd(int a, int b) { while (a % b != 0 && b % a != 0) { if (b > a) { b = b % a; } else if (a > b) { a = a % b; } } if (a <= b) return a; else return b; } int main() { int n; cin >> n; int dir[n]; int step[n]; for (int i = 0; i < n; i++) { char now; cin >> now; if (now == > ) dir[i] = 1; else dir[i] = -1; } for (int i = 0; i < n; i++) { cin >> step[i]; } int trace[n]; initialise(trace, 0, n); int curr = 0; while (true) { if (trace[curr] == 1) { cout << INFINITE << endl; break; } else { trace[curr] = 1; curr = curr + dir[curr] * step[curr]; if (curr >= n || curr < 0) { cout << FINITE << endl; break; } } } return 0; }
#include <bits/stdc++.h> using namespace std; int N, dp[100005][3], MOD = 1000000007; char s[2][100005]; long long ans = 1; int main() { scanf( %d%s%s , &N, s[0], s[1]); for (int i = 0; i < N; ++i) { if (s[0][i] == ? ) ans = (((ans * 10) % (MOD) + (MOD)) % (MOD)); if (s[1][i] == ? ) ans = (((ans * 10) % (MOD) + (MOD)) % (MOD)); for (int x = 0; x < 10; ++x) { for (int y = 0; y < 10; ++y) { if ((s[0][i] == ? || s[0][i] == x + 0 ) && (s[1][i] == ? || s[1][i] == y + 0 )) { if (x >= y) { dp[i][0] = (((dp[i][0] + (i ? dp[i - 1][0] : 1)) % (MOD) + (MOD)) % (MOD)); } if (x <= y) { dp[i][1] = (((dp[i][1] + (i ? dp[i - 1][1] : 1)) % (MOD) + (MOD)) % (MOD)); } if (x == y) { dp[i][2] = (((dp[i][2] + (i ? dp[i - 1][2] : 1)) % (MOD) + (MOD)) % (MOD)); } } } } } if (N == 1) puts( 0 ); else printf( %d n , (((ans - dp[N - 1][0] - dp[N - 1][1] + dp[N - 1][2]) % (MOD) + (MOD)) % (MOD))); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n, c = 0, d = 0, k; string str; cin >> n >> str; for (int i = 0; i < n; i++) { if (str[i] == - ) c++; } if (c == n) { cout << 0 << endl; return 0; } else if (str[0] == - ) { for (int i = 0; i < n; i++) { if (str[i] == - ) { continue; } else { k = i; break; } } for (int i = k; i < n; i++) { if (str[i] == + ) d += 1; else { d -= 1; if (d < 0) d = 0; } } } else { for (int i = 0; i < n; i++) { if (str[i] == + ) d += 1; else { d -= 1; if (d < 0) d = 0; } } } cout << d << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int M; const int MAX = 100001; vector<int> _size, marked, _pow, _ipow; vector<pair<int, int> > adjList[MAX + 1]; int getSize(int u, int p = -1) { _size[u] = 1; for (auto [v, w] : adjList[u]) if (v != p && !marked[v]) _size[u] += getSize(v, u); return _size[u]; } int getCentroid(int u, int p, int n) { for (auto [v, w] : adjList[u]) if (v != p && !marked[v] && _size[v] * 2 > n) return getCentroid(v, u, n); return u; } void add(unordered_map<int, int> &aux, int u, int p, int lvl, int rem) { aux[rem]++; for (auto [v, w] : adjList[u]) if (v != p && !marked[v]) add(aux, v, u, lvl + 1, (int(1LL * w * _pow[lvl] % M) + rem) % M); } void remove(unordered_map<int, int> &aux, int u, int p, int lvl, int rem) { aux[rem]--; for (auto [v, w] : adjList[u]) if (v != p && !marked[v]) remove(aux, v, u, lvl + 1, (int(1LL * w * _pow[lvl] % M) + rem) % M); } long long count(unordered_map<int, int> &aux, int u, int p, int lvl, int rem) { int nxt = int(1LL * _ipow[lvl] * (M - rem) % M); long long ans = aux[nxt]; for (auto [v, w] : adjList[u]) if (v != p && !marked[v]) ans += count(aux, v, u, lvl + 1, (rem * 10LL + w) % M); return ans; } long long solve(int u) { int N = getSize(u); u = getCentroid(u, -1, N); marked[u] = 1; unordered_map<int, int> aux; aux[0] = 1; for (auto [v, w] : adjList[u]) if (!marked[v]) add(aux, v, u, 1, w % M); long long ans = aux[0] - 1; for (auto [v, w] : adjList[u]) if (!marked[v]) { remove(aux, v, u, 1, w % M); ans += count(aux, v, u, 1, w % M); add(aux, v, u, 1, w % M); } aux.clear(); for (auto [v, w] : adjList[u]) if (!marked[v]) ans += solve(v); return ans; } int gcd(int a, int b, int &x, int &y) { if (b == 0) { x = 1; y = 0; return a; } int x1, y1; int d = gcd(b, a % b, x1, y1); x = y1; y = x1 - y1 * (a / b); return d; } int getInv() { int x, y; int g = gcd(10, M, x, y); assert(g == 1); return (x % M + M) % M; } int main() { ios_base ::sync_with_stdio(false); cin.tie(nullptr); int n; cin >> n >> M; _size = vector<int>(n), _pow = vector<int>(n + 1, 1), _ipow = vector<int>(n + 1, 1), marked = vector<int>(n + 1); _ipow[1] = getInv(); _pow[1] = 10; for (int i = 2; i <= n; i++) { _pow[i] = int(1LL * _pow[i - 1] * 10 % M); _ipow[i] = int(1LL * _ipow[i - 1] * _ipow[1] % M); } for (int i = 0; i < n - 1; i++) { int u, v, w; cin >> u >> v >> w; adjList[u].push_back({v, w}); adjList[v].push_back({u, w}); } cout << solve(0) << n ; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 2e6 + 100, M = 1e6 + 100; int n, up[N], down[M], nei[M][2]; int lim; bool mark[M], bad[M]; vector<int> vec; int f(int a) { return lower_bound(vec.begin(), vec.end(), a) - vec.begin(); } bool dfs(int v) { mark[v] = true; for (int i = 0; i < 2 && nei[v][i] <= lim; i++) { int u = nei[v][i]; if (up[u] == -1 || (mark[up[u]] == false && dfs(up[u]))) { up[u] = v; down[v] = u; return true; } } return false; } bool ok(int X) { lim = X; fill(up, up + N, -1); fill(down, down + M, -1); memset(bad, false, sizeof bad); int matching = 0; bool done = false; for (int v = 0; v < n; v++) if (nei[v][0] > lim) return false; else if (nei[v][1] > lim) { if (up[nei[v][0]] != -1) return false; down[v] = nei[v][0]; up[nei[v][0]] = v; bad[v] = true; matching++; } while (done == false) { done = true; copy(bad, bad + n, mark); for (int v = 0; v < n; v++) if (mark[v] == false && down[v] == -1 && dfs(v)) { matching++; done = false; } } return matching == n; } int main() { ios::sync_with_stdio(false), cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> nei[i][0] >> nei[i][1]; vec.push_back(nei[i][0]); vec.push_back(nei[i][1]); } sort(vec.begin(), vec.end()); vec.resize(unique(vec.begin(), vec.end()) - vec.begin()); for (int i = 0; i < n; i++) nei[i][0] = f(nei[i][0]), nei[i][1] = f(nei[i][1]); if (ok(vec.size() - 1) == false) { cout << -1; return 0; } if (ok(vec.size() - 2) == false) { cout << vec.back(); return 0; } int l = -1, r = vec.size() - 1; while (r - l > 1) { if (ok((l + r) >> 1)) r = lim; else l = lim; } cout << vec[r]; return 0; }
#include <bits/stdc++.h> using namespace std; int val[200007], add[200007]; int main() { int n, t, a, x, k, l = 1; long long sum = 0; scanf( %d , &n); for (int i = 0; i < n; ++i) { scanf( %d , &t); if (t == 1) { scanf( %d%d , &a, &x); add[a] += x; sum += a * x; } else if (t == 2) { scanf( %d , &k); val[++l] = k; sum += val[l]; } else if (l > 1) { sum -= val[l]; sum -= add[l]; add[l - 1] += add[l]; add[l] = 0; --l; } printf( %.6lf n , (double)sum / l); } return 0; }
#include <bits/stdc++.h> using namespace std; constexpr int N = 2e5 + 10; long long n, w, k, happy[N], nice, t[N], mytime, ans; bool in[N]; pair<int, int> lowest; set<pair<int, int>> st; queue<int> q; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> w >> k; for (int i = 0; i < n; i++) cin >> happy[i]; for (int i = 0; i < n; i++) cin >> t[i]; for (int i = n - 1; ~i; i--) { st.insert({t[i], -i}); if ((int)st.size() == w) { lowest = *st.begin(); mytime += (t[i] + 1) / 2; in[i] = true; } else if ((int)st.size() > w) { if (t[i] >= lowest.first) { in[-lowest.second] = false; mytime += (t[i] + 1) / 2; mytime -= (lowest.first + 1) / 2; mytime += lowest.first; lowest = *st.upper_bound(lowest); in[i] = true; } else mytime += t[i]; } else { in[i] = true; mytime += (t[i] + 1) / 2; } nice += happy[i]; q.push(i); while (mytime > k) { nice -= happy[q.front()]; if (in[q.front()]) { mytime -= (t[q.front()] + 1) / 2; in[q.front()] = false; st.erase({t[q.front()], -q.front()}); if ((int)st.size() >= w) { lowest = *(--st.lower_bound(lowest)); in[-lowest.second] = true; mytime -= lowest.first; mytime += (lowest.first + 1) / 2; } } else { mytime -= t[q.front()]; st.erase({t[q.front()], -q.front()}); } q.pop(); } ans = max(ans, nice); } cout << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; inline void speedUp() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } long long int t, n, m, p[100005], q[100005], Podd, Peven, Qodd, Qeven, tot; int main() { speedUp(); int t, n; cin >> t; while (t--) { tot = Podd = Peven = Qodd = Qeven = 0; cin >> n; for (int i = 1; i <= n; i++) { cin >> p[i]; if (p[i] % 2 == 0) Peven++; else Podd++; } cin >> m; for (int i = 1; i <= m; i++) { cin >> q[i]; if (q[i] % 2 == 0) Qeven++; else Qodd++; } for (int i = 1; i <= n; i++) { if (p[i] % 2 == 0) tot += Qeven; else tot += Qodd; } cout << tot << endl; } }
#include <bits/stdc++.h> using namespace std; int main(void) { int n; scanf( %d , &n); int v[n]; for (int i = 0; i < n; i++) scanf( %d , v + i); if (n > 2) { for (int i = 0; i + 3 <= n; i++) { int a = v[i], b = v[i + 1], c = v[i + 2]; int ans1 = -1, ans2 = -1; set<int> ms; ms.insert(a); ms.insert(b); ms.insert(c); if (ms.size() == 1) continue; else if (ms.size() == 3) { if (a == *ms.begin()) ans1 = i, ans2 = i + 1; else if (a == *(++ms.begin())) ans1 = i, ans2 = i + 2; else if (a == *ms.rbegin()) ans1 = i, ans2 = i + 1; } else if (ms.size() == 2) { if (a == c) { int d = -1; if (i > 0) { d = v[i - 1]; if (d != a) ans1 = i - 1, ans2 = i; else ans1 = i, ans2 = i + 1; } else if (i + 3 < n) { d = v[i + 3]; if (d != a) ans1 = i + 2, ans2 = i + 3; else ans1 = i + 1, ans2 = i + 2; } else continue; } else if (a == b) ans1 = i + 1, ans2 = i + 2; else if (b == c) ans1 = i, ans2 = i + 1; } printf( %d %d n , ans1 + 1, ans2 + 1); return 0; } } printf( -1 n ); return (0); }
#include <bits/stdc++.h> using namespace std; const double eps = 1e-2; struct cplx { double x, y; cplx(double x1 = 0, double x2 = 0) { x = x1, y = x2; } } g[2000001], f[2000001]; cplx operator+(cplx a, cplx b) { return cplx(a.x + b.x, a.y + b.y); } cplx operator-(cplx a, cplx b) { return cplx(a.x - b.x, a.y - b.y); } cplx operator*(cplx a, cplx b) { return cplx(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); } void swap(cplx &a, cplx &b) { swap(a.x, b.x), swap(a.y, b.y); } int a[2000001], b[2000001]; int r[2000001]; const double Pi = acos(-1); void fft(cplx f[], int op, int n) { for (int i = 0; i < n; i++) if (i < r[i]) swap(f[i], f[r[i]]); for (int w = 2; w <= n; w <<= 1) { int len = w / 2; cplx tmp(cos(Pi / len), op * sin(Pi / len)); for (int k = 0; k < n; k += w) { cplx buf(1, 0); for (int l = k; l < k + len; l++) { cplx tt = f[len + l] * buf; f[len + l] = f[l] - tt; f[l] = f[l] + tt; buf = buf * tmp; } } } if (op == -1) { for (int i = 0; i < n; i++) f[i].x /= n, f[i].y /= n; } } char p[2000001]; char q[2000001]; int res[2000001]; int l1, l2, k, n, m; void solve(char c) { for (int i = 0; i <= n; i++) f[i].x = f[i].y = g[i].x = g[i].y = 0; int far = -1; for (int i = 0; i < l1; i++) { if (p[i] == c) far = i + k; if (i <= far) f[i].x = 1; } far = l1; for (int i = l1 - 1; i >= 0; i--) { if (p[i] == c) far = i - k; if (i >= far) f[i].x = 1; } for (int i = 0; i < l2; i++) if (q[i] == c) g[i].x = 1; fft(f, 1, n); fft(g, 1, n); for (int i = 0; i < n; i++) f[i] = f[i] * g[i]; fft(f, -1, n); for (int i = 0; i < n; i++) res[i] += (int)(f[i].x + eps); } signed main() { int ans = 0; scanf( %d%d%d , &l1, &l2, &k); cin >> p; cin >> q; reverse(p, p + l1); n = l1, m = l2; for (m += n, n = 1; n <= m; n <<= 1) ; for (int i = 0; i < n; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) ? n >> 1 : 0); solve( A ); solve( T ); solve( G ); solve( C ); for (int i = 0; i <= l1 - l2; i++) if (res[l1 - i - 1] >= l2) ans++; printf( %d , ans); return 0; }
#include <bits/stdc++.h> struct edge { int to, nxt; } e[100005], e2[100005]; int h[100005], n, cnt, hei[100005], gson[100005], node[100005], N, fa[100005]; void ins(int x, int y) { e[++cnt].nxt = h[x]; e[cnt].to = y; h[x] = cnt; } void dfs1(int x) { hei[x] = 1; for (int i = h[x]; i; i = e[i].nxt) { dfs1(e[i].to); hei[x] = std::max(hei[x], hei[e[i].to] + 1); if (hei[gson[x]] < hei[e[i].to]) gson[x] = e[i].to; } } void dfs2(int x) { node[++N] = x; for (int i = h[x]; i; i = e[i].nxt) if (e[i].to != gson[x]) dfs2(e[i].to); if (gson[x]) dfs2(gson[x]); } int main() { scanf( %d , &n); for (int i = 2; i <= n; i++) scanf( %d , &fa[i]), ins((++fa[i]), i); dfs1(1); dfs2(1); for (int i = 1; i <= n; i++) printf( %d , node[i] - 1); printf( n%d n , n - hei[1]); for (int i = 1, j = 1, k = 0; i <= n - hei[1]; i++) { while (j <= n && fa[node[j]] == k) k = node[j++]; if (j > n) break; printf( %d , node[j] - 1); k = fa[k]; } }