solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> long long MOD = 1000000007; long long inf = 1e18; using namespace std; vector<long long> S; long long p[300001]; vector<long long> Children[300001]; int main() { ios::sync_with_stdio(0); long long i, n, x, y, w, q, a, b, e, j; long long ans = 0; cin >> n >> w; for (i = 0; i < n; i++) { cin >> x >> y; x--; y--; p[x]++; p[y]++; Children[x].push_back(y); Children[y].push_back(x); } for (i = 0; i < n; i++) { S.push_back(p[i]); } sort(S.begin(), S.end()); for (i = 0; i < n; i++) { q = p[i]; a = -1; b = S.size(); while (b - a > 1) { e = (b + a) / 2; if (S[e] + q >= w) { b = e; } else { a = e; } } ans += S.size() - b; if (2 * p[i] >= w) ans -= 1; for (j = 0; j < Children[i].size(); j++) { p[Children[i][j]] -= 1; if (q + p[Children[i][j]] == w - 1) { ans -= 1; } } for (j = 0; j < Children[i].size(); j++) { p[Children[i][j]] += 1; } } cout << ans / 2; return 0; }
3
#include <bits/stdc++.h> using namespace std; int a[200005]; int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); int sum = 0; for (int i = 0, j = k - 1; j < n; j++) { if (a[j] + 1 - a[i] <= m) { a[j] = -1; sum++; if (i == j) i++; } else { i++; while (a[i] == -1) i++; } } cout << sum << endl; }
4
#include <bits/stdc++.h> using namespace std; map<int, int> mymap; const int N = 2000001; int audio[N], sub[N]; int main() { int n, m, x; cin >> n; for (int i = 0; i < n; i++) { cin >> x; mymap[x]++; } cin >> m; int nho = 1, a, b, y; pair<int, int> p; for (int i = 0; i < m; i++) cin >> audio[i]; for (int i = 0; i < m; i++) cin >> sub[i]; for (int i = 0; i < m; i++) { x = audio[i], y = sub[i]; x = mymap[x], y = mymap[y]; if (p < make_pair(x, y)) p = make_pair(x, y), nho = i + 1; } cout << nho << endl; }
3
#include <bits/stdc++.h> using namespace std; int main() { //while(true) { int m,n; cin>>m>>n; map<int,int> was; map<int,int> acs; int sum = 0; for( int i=0; i<n; i++ ) { int a; string b; cin>>a>>b; if( b == "WA") was[a]++; else { if(!acs.count(a)) sum += was[a]; acs[a] = 1; } } cout << acs.size() << " " << sum << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, k, a[1000], s = 0; cin >> n >> k; for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { if (a[i] <= 8) s += a[i]; else { s += 8; a[i + 1] += a[i] - 8; } if (s >= k) { cout << i + 1; return 0; } } cout << -1; }
1
#include <bits/stdc++.h> using namespace std; int N, sol; string a[100009], b[100009]; vector<pair<int, int> > ans; struct nod { nod *urm[26]; vector<int> a, b; nod() { memset(urm, 0, sizeof(urm)); a.clear(), b.clear(); } } *R = new nod; void Push(string &s, int val) { nod *q = R; for (auto it = s.begin(); it != s.end(); it++) { int i = (*it) - 'a'; if (q->urm[i] == 0) q->urm[i] = new nod; q = q->urm[i]; } if (val > 0) q->a.push_back(val); else q->b.push_back(-val); } void dfs(nod *&q, nod *&t) { for (int i = 0; i < 26; i++) if (q->urm[i] != 0) dfs(q->urm[i], q); while ((!q->a.empty()) && (!q->b.empty())) { ans.push_back(make_pair(q->a[q->a.size() - 1], q->b[q->b.size() - 1])); q->a.erase(q->a.begin() + q->a.size() - 1), q->b.erase(q->b.begin() + q->b.size() - 1); } for (auto it = q->a.begin(); it != q->a.end(); it++) t->a.push_back(*it); for (auto it = q->b.begin(); it != q->b.end(); it++) t->b.push_back(*it); } void add_to_sol(int i, int j) { for (int k = 0; k < a[i].size() && k < b[j].size(); k++) if (a[i][k] != b[j][k]) break; else sol++; } int main() { cin >> N; for (int i = 1; i <= N; i++) cin >> a[i], Push(a[i], i); for (int i = 1; i <= N; i++) cin >> b[i], Push(b[i], -i); dfs(R, R); for (auto it = ans.begin(); it != ans.end(); it++) add_to_sol(it->first, it->second); printf("%d\n", sol); for (auto it = ans.begin(); it != ans.end(); it++) printf("%d %d\n", it->first, it->second); return 0; }
1
#include <bits/stdc++.h> using namespace std; int a[100010]; int main() { int sum = 0, x, n, l, i; cin >> n >> l; for (i = 1; i <= n; i++) { cin >> x; sum += x; } if (sum + n - 1 == l) printf("YES\n"); else printf("NO\n"); return 0; }
2
#include <bits/stdc++.h> using namespace std; #define Rep(i,N) for(int i = 0;i < N;i++) int main() { int L; int A, B, C, D; cin >> L; cin >> A >> B >> C >> D; cout << L - max((A + C - 1) / C,(B + D - 1) / D) << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; void solve() { ll m, n; cin >> m >> n; if (n / 2 < m) cout << n % m << endl; else { if (n % 2 == 0) cout << n - (n / 2 + 1) << endl; else cout << n / 2 << endl; } return; } int main() { int tc; cin >> tc; while (tc--) { solve(); } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long a[200005], b[200005], k, sa, sb; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, m, d, pa, push_back; cin >> n; for (long long i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); cin >> m; for (long long i = 0; i < m; i++) cin >> b[i]; sort(b, b + m); sa = 3 * n; sb = 3 * m; d = sa - sb; pa = sa; push_back = sb; for (long long i = 0; i < n; i++) { long long p; for (long long j = k; j < m; j++) { if (a[i] <= b[j]) { k = j; break; } p = j; sb -= 1; } sa -= 1; if (p == m - 1) k = m; if (sa - sb == d) { if (sa > pa) { pa = sa; push_back = sb; } } else if (sa - sb > d) { d = sa - sb; pa = sa; push_back = sb; } } sa = 3 * n; sb = 3 * m; k = 0; for (long long i = 0; i < m; i++) { sb -= 1; long long p; for (long long j = k; j < n; j++) { if (a[j] > b[i]) { k = j; break; } p = j; sa -= 1; } if (p == n - 1) k = n; if (sa - sb == d) { if (sa > pa) { pa = sa; push_back = sb; } } else if (sa - sb > d) { d = sa - sb; pa = sa; push_back = sb; } } cout << pa << ":" << push_back; }
3
#include <bits/stdc++.h> using namespace std; bool compare(pair<int, int> a, pair<int, int> b) { if (a.first == b.first) return a.second < b.second; return a.first > b.first; } int main() { int n, k; vector<pair<int, int>> v; int arr[100] = {0}; cin >> n >> k; for (int i = 0; i < n; i++) { pair<int, int> temp; cin >> temp.first >> temp.second; v.push_back(temp); } cout << endl; sort(v.begin(), v.end(), compare); int pos = 1; arr[0] = 1; for (int i = 1; i < n; i++) { if (v[i - 1].first == v[i].first && v[i - 1].second == v[i].second) arr[i] = pos; else arr[i] = ++pos; } pos = arr[k - 1]; int ans = 0; for (int i = 0; i < n; i++) if (arr[i] == pos) ans++; cout << endl << ans; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); string s; cin >> s; char mn = 'z'; for (auto c : s) { if (c <= mn) cout << "Mike\n"; else cout << "Ann\n"; mn = min(mn, c); } return 0; }
3
#include <bits/stdc++.h> using namespace std; using ll = long long; using dd = double; const int N = (int)1e6 + 7; int a[N]; vector<int> foo; void help() {} void TaoCode() { int n, x, y; cin >> n >> x >> y; string bit; cin >> bit; int cnt = 0; for (int i = (0); i < (x); i++) { if (i == y and bit[(int)bit.size() - i - 1] == '0') { cnt++; } else if (i != y and bit[(int)bit.size() - i - 1] == '1') { cnt++; } } cout << cnt; } int main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); TaoCode(); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> p(n); for (int i = 0; i < n; i++) { cin >> p[i]; } string s; cin >> s; vector<pair<int, int>> v1, v2; for (int i = 0; i < n; i++) { if (s[i] == '0') { v1.push_back({p[i], i}); } else { v2.push_back({p[i], i}); } } int l = v1.size(); int r = v2.size(); if (l == 0 || r == 0) { for (int i = 0; i < n; i++) { cout << p[i] << " "; } cout << endl; continue; } int j = 1; sort(v1.begin(), v1.end()); sort(v2.begin(), v2.end()); for (int i = 0; i < v1.size(); i++) { v1[i].first = j; p[v1[i].second] = j; j++; } for (int i = 0; i < v2.size(); i++) { v2[i].first = j; p[v2[i].second] = j; j++; } for (int i = 0; i < n; i++) { cout << p[i] << " "; } cout << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; struct ed { int l, r, mx, lazy, v; } e[400004]; int n, cnt, m, ans[100001]; void build(int m, int l, int r) { e[m].l = l; e[m].r = r; e[m].mx = e[m].lazy = 0; if (l == r) { e[m].v = ++cnt; return; } int mid = (l + r) >> 1; build(m << 1, l, mid); build(m << 1 | 1, mid + 1, r); } void push(int m, int l) { e[m << 1].mx += l; e[m << 1 | 1].mx += l; e[m << 1].lazy += l; e[m << 1 | 1].lazy += l; e[m].lazy = 0; } void add(int m, int l, int r, int ad) { if (e[m].l == l && e[m].r == r) { e[m].mx += ad; e[m].lazy += ad; return; } if (e[m].lazy) push(m, e[m].lazy); int mid = (e[m].l + e[m].r) >> 1; if (mid >= r) add(m << 1, l, r, ad); else if (l > mid) add(m << 1 | 1, l, r, ad); else { add(m << 1, l, mid, ad); add(m << 1 | 1, mid + 1, r, ad); } e[m].mx = max(e[m << 1].mx, e[m << 1 | 1].mx); } int query(int m) { if (e[1].mx <= 0) return -1; if (e[m].l == e[m].r) return e[m].v; if (e[m].lazy) push(m, e[m].lazy); if (e[m << 1 | 1].mx > 0) return query(m << 1 | 1); else return query(m << 1); } int main() { int a, b, c; scanf("%d", &n); build(1, 1, n); for (int i = 1; i <= n; i++) { scanf("%d%d", &a, &b); if (b == 1) { scanf("%d", &c); ans[a] = c; add(1, 1, a, 1); } else add(1, 1, a, -1); int y = query(1); if (y < 0) printf("%d\n", y); else printf("%d\n", ans[y]); } }
3
#include <iostream> #include <vector> #include <cstring> #include <algorithm> #include <cmath> #define forn(i, n) for(int i = 0; i < int(n); ++i) using namespace std; using ll = long long; void solve(){ int n; cin >> n; vector<pair<int ,int>> points(n); for (int i = 0; i < n; ++i) { cin >> points[i].first; } for (int i = 0; i < n; ++i) { cin >> points[i].second; } sort(points.begin(), points.end()); if (points[0] != make_pair(1, 1)){ points.insert(points.begin(), make_pair(1, 1)); n++; } long long ans = 0; for (int i = 0; i < n - 1; ++i) { pair<int, int> a = points[i]; pair <int, int> b = points[i + 1]; if (a.first - a.second == b.first - b.second){ if ((a.first - a.second) % 2 == 0){ ans += b.first - a.first; } } else{ ans += (b.first - b.second) / 2 - (a.first - a.second) / 2; } } cout << ans << '\n'; } int main(){ ios::sync_with_stdio(false); int T; cin >> T; while (T--){ solve(); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n, s; cin >> n >> s; if (n >= s - n + 1) { cout << "NO\n"; return 0; } cout << "YES\n"; for (int i = (1); i <= (n - 1); ++i) cout << 1 << " "; cout << s - n + 1 << endl; cout << n << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long int mod = 998244353; long long int fac[2000]; long long int pwr(long long int num) { long long int pw = mod - 2; long long int res = 1; while (pw) { if (pw % 2) res = (res * num) % mod; pw /= 2; num = (num * num) % mod; } return res; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); fac[0] = 1; for (int i = 1; i < 2000; i++) fac[i] = (fac[i - 1] * i) % mod; string s; cin >> s; int n = s.length(); long long int cnt1 = 0, cnt2 = 0, cnt3 = 0; long long int CNT1[2005]; long long int CNT2[2005]; long long int CNT3[2005]; for (int i = 0; i < n; i++) { if (s[i] == '(') cnt1++; else if (s[i] == ')') cnt2++; else cnt3++; CNT1[i] = cnt1; CNT2[i] = cnt2; CNT3[i] = cnt3; } long long int ans = 0; for (int i = 0; i < n - 1; i++) { if (CNT1[i] + CNT3[i] < cnt2 - CNT2[i] || CNT1[i] > cnt2 + cnt3 - CNT2[i] - CNT3[i]) continue; long long int left = CNT3[i]; long long int right = cnt3 - CNT3[i]; long long int minLeft = max(0ll, cnt2 - CNT2[i] - CNT1[i]); long long int minRight = max(0ll, CNT1[i] + CNT2[i] - cnt2); long long int minDeep = max(CNT1[i], cnt2 - CNT2[i]); for (int j = 0; j <= min(left - minLeft, right - minRight); j++) { long long int temp1 = (((fac[left] * pwr(fac[minLeft + j])) % mod) * pwr(fac[left - minLeft - j])) % mod; long long int temp2 = (((fac[right] * pwr(fac[minRight + j])) % mod) * pwr(fac[right - minRight - j])) % mod; ans += (((temp1 * temp2) % mod) * (minDeep + j)) % mod; ans %= mod; } } cout << ans; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n; cin >> n; string s; cin >> s; long long int mv = n; for (long long int i = 0; i < n - 1; i++) { if (s[i] == 'U' && s[i + 1] == 'R') { mv--; s[i + 1] = 'D'; } if (s[i] == 'R' && s[i + 1] == 'U') { mv--; s[i + 1] = 'D'; } } cout << mv; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[10000], b[10000]; int i; for (i = 0; i < n; i++) { cin >> a[i]; cin >> b[i]; } int r1 = 0, r0 = 0, l0 = 0, l1 = 0, m; for (i = 0; i < n; i++) { if (a[i] == 1) l1++; else l0++; m = (l1 < l0 ? l1 : l0); if (b[i] == 1) r1++; else r0++; m += (r1 < r0 ? r1 : r0); } cout << m; }
1
#include <bits/stdc++.h> using namespace std; const long long linf = 1e18; const double eps = 1e-12; const double pi = acos(-1); template <typename T> istream& operator>>(istream& is, vector<T>& vec) { for (auto&& x : vec) is >> x; return is; } template <typename T> ostream& operator<<(ostream& os, const vector<T>& vec) { for (long long i = (0), __last_i = (vec.size()); i < __last_i; i++) { if (i) os << " "; os << vec[i]; } return os; } template <typename T> ostream& operator<<(ostream& os, const vector<vector<T> >& vec) { for (long long i = (0), __last_i = (vec.size()); i < __last_i; i++) { if (i) os << endl; os << vec[i]; } return os; } int main() { ios::sync_with_stdio(false); cin.tie(0); long long n, m; cin >> n >> m; vector<long long> g(n, 0); for (long long i = (0), __last_i = (m); i < __last_i; i++) { long long a, b; cin >> a >> b; --a, --b; g[a] |= 1LL << b; g[b] |= 1LL << a; } for (long long i = (0), __last_i = (n); i < __last_i; i++) g[i] |= 1LL << i; if (m == n * (n - 1) / 2) { cout << 0 << endl; return 0; } vector<pair<long long, long long> > prv(1LL << n); vector<long long> dist(1LL << n, linf); queue<long long> Q; for (long long i = (0), __last_i = (n); i < __last_i; i++) { dist[1LL << i] = 0; prv[1LL << i] = {-1, -1}; Q.push(1LL << i); } while (!Q.empty()) { long long i = Q.front(); Q.pop(); for (long long j = (0), __last_j = (n); j < __last_j; j++) { if ((i >> j & 1) == 0) continue; long long ni = i | g[j]; if (dist[i] + 1 < dist[ni]) { dist[ni] = dist[i] + 1; prv[ni] = {i, j}; Q.push(ni); } } } vector<long long> ans; long long v = (1LL << n) - 1; assert(dist[v] < linf); while (prv[v].second >= 0) { ans.push_back(prv[v].second); v = prv[v].first; } reverse((ans).begin(), (ans).end()); cout << ans.size() << endl; for (auto&& a : ans) ++a; cout << ans << endl; }
3
#include <bits/stdc++.h> using namespace std; int main() { long long n, t, i, j, s, p, k, m; vector<long long> v; long long a[1000005]; cin >> t; while (t--) { long long n, x, i, cnt; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; } cnt = 0; for (i = 0; i < n; i++) { if (a[i] == 1) { cnt++; } else { i = n; } } if (cnt == n) { if (cnt % 2 == 0) { cout << "Second\n"; } else { cout << "First\n" << endl; } } else { if (cnt % 2 == 1) { cout << "Second\n"; } else { cout << "First\n"; } } } return 0; }
2
#include <bits/stdc++.h> using namespace std; map<int, bool> H[10]; const int MAXN = 7e4 + 5; bool chk[MAXN][9][9]; int n; char s[MAXN][10]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%s", s[i]); for (int i = 1; i <= n; i++) { for (int j = 0; j < 9; j++) { int cur = 0; for (int k = j; k < 9; k++) { cur = cur * 10 + s[i][k] - '0'; chk[i][j][k] |= H[k - j][cur]; } } for (int j = 0; j < 9; j++) { int cur = 0; for (int k = j; k < 9; k++) { cur = cur * 10 + s[i][k] - '0'; H[k - j][cur] = true; } } } for (int i = 0; i < 10; i++) H[i].clear(); for (int i = n; i >= 1; i--) { for (int j = 0; j < 9; j++) { int cur = 0; for (int k = j; k < 9; k++) { cur = cur * 10 + s[i][k] - '0'; chk[i][j][k] |= H[k - j][cur]; } } for (int j = 0; j < 9; j++) { int cur = 0; for (int k = j; k < 9; k++) { cur = cur * 10 + s[i][k] - '0'; H[k - j][cur] = true; } } } for (int i = 1; i <= n; i++) { for (int len = 1; len <= 9; len++) for (int j = 0; j + len - 1 < 9; j++) { int k = j + len - 1; if (!chk[i][j][k]) { for (int l = j; l <= k; l++) printf("%c", s[i][l]); puts(""); goto lp; } } lp: continue; } return 0; }
2
#include <bits/stdc++.h> using namespace std; void see(int x) {cout << x;} void see(long long int x) {cout << x;} void see(long double x) {cout << x;} void see(char x) {cout << '\'' << x << '\'';} void see(string x) {cout << '\"' << x << '\"';} template<typename T, typename V> void see(const pair<T, V> &x) {cout << '{'; see(x.first); cout << ','; see(x.second); cout << '}';} template<typename T> void see(const T &x) {int f = 0; cout << '{'; for (auto &i: x) cout << (f++ ? "," : ""), see(i); cout << "}";} void _print() {cout << "]\n";} template <typename T, typename... V> void _print(T t, V... v) {see(t); if (sizeof...(v)) cout << ", "; _print(v...);} #define fori(i,a,b) for(int i=a;i<=b;i++) #define ford(i,a,b) for(int i=a;i>=b;i--) #define all(a) a.begin(), a.end() #define in(c,x) ((c).find(x) != (c).end()) #define isp(c,x) (find(all(c),x) != (c).end()) #define pb push_back #define sz(x) (int)x.size() #define fill(a,v) memset(a, v, sizeof a) #define fs first #define sc second #define ub upper_bound #define lb lower_bound #define en '\n' #define vt vector #define deb(x...) cout << "[" << #x << "] = ["; _print(x) typedef long long int lli; typedef long double ld; typedef list<int> li; lli gcd(lli a,lli b){return (!b)?a:gcd(b,a%b);} void solve(){ lli n; cin>>n; if(n == 1){ cout<<0<<en; } else if(n == 2){ cout<<1<<en; } else if(n == 3){ cout<<2<<en; } else if(n&1) { cout<<3<<en; } else { cout<<2<<en; } } //THINK OF SPECIAL CASES, ARRAY LIMITS, LLI vs INT, 1LL int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin>>t; while(t--) solve(); }
1
#include <cstdio> #include <cstring> #include <iostream> using namespace std; typedef long long ll; const ll P=1000000007; const int maxn=5010; int n,m,cnt,rt,mn,sum; ll ans; int head[maxn],to[maxn<<1],nxt[maxn<<1],siz[maxn],fa[maxn],v[maxn]; ll jc[maxn],jcc[maxn],ine[maxn],f[maxn],g[maxn],h[maxn]; inline void add(int a,int b) { to[cnt]=b,nxt[cnt]=head[a],head[a]=cnt++; } void dfs(int x) { siz[x]=1; int i,tmp=0; for(i=head[x];i!=-1;i=nxt[i]) if(to[i]!=fa[x]) fa[to[i]]=x,dfs(to[i]),siz[x]+=siz[to[i]],tmp=max(tmp,siz[to[i]]); tmp=max(tmp,n-siz[x]); if(tmp<mn) mn=tmp,rt=x; } inline ll c(int a,int b) { if(a<b) return 0; return jc[a]*jcc[b]%P*jcc[a-b]%P; } inline int rd() { int ret=0,f=1; char gc=getchar(); while(gc<'0'||gc>'9') {if(gc=='-') f=-f; gc=getchar();} while(gc>='0'&&gc<='9') ret=ret*10+(gc^'0'),gc=getchar(); return ret*f; } int main() { n=rd(); int i,j,k,a,b; memset(head,-1,sizeof(head)); jc[0]=jcc[0]=ine[0]=jc[1]=jcc[1]=ine[1]=1; for(i=2;i<=n;i++) jc[i]=jc[i-1]*i%P,ine[i]=P-(P/i)*ine[P%i]%P,jcc[i]=jcc[i-1]*ine[i]%P; for(i=1;i<n;i++) a=rd(),b=rd(),add(a,b),add(b,a); mn=1<<30,dfs(1); if(mn+mn==n) { printf("%lld",jc[n/2]*jc[n/2]%P); return 0; } for(i=head[rt];i!=-1;i=nxt[i]) { if(to[i]==fa[rt]) v[++m]=n-siz[rt]; else v[++m]=siz[to[i]]; } f[0]=1; for(i=1;i<=m;i++) { for(j=0;j<=v[i];j++) g[j]=c(v[i],j)*c(v[i],j)%P*jc[j]%P; memset(h,0,sizeof(h[0])*(sum+v[i]+2)); for(j=0;j<=sum;j++) for(k=0;k<=v[i];k++) h[j+k]=(h[j+k]+f[j]*g[k])%P; memcpy(f,h,sizeof(h[0])*(sum+v[i]+2)); sum+=v[i]; } for(i=n;i>=0;i--) { f[i]=f[i]*jc[n-i]%P; for(j=i+1;j<=n;j++) f[i]=(f[i]-f[j]*c(j,i))%P; } printf("%lld",(f[0]+P)%P); return 0; }
0
#include <bits/stdc++.h> using namespace std; priority_queue<int, vector<int>, greater<int> >q; int main() { int T, n, k; scanf("%d", &T); while(T--) { scanf("%d%d", &n, &k); q.push(k); while(n > 1) { k = q.top(); q.pop(); k = 127 ^ k; //printf("%d %d\n", k, k ^ 127); q.push(k); k = 127; q.push(k); n--; } int ans = 0; while(!q.empty()) { k = q.top(); //printf("%d\n", k); q.pop(); ans += k; } printf("%d\n", ans); } return 0; }
0
#include<iostream> #include<vector> #define int long long using namespace std; const int N=2e3+2; const int inf=1e9+7; vector<int> adj[N]; int num[N],sum[N],f[N]; string s; void dfs(int x,int p){ if(s[x-1]=='1'){ num[x]=1; } else{ num[x]=0; } sum[x]=0; for(int i=0;i<adj[x].size();i++){ if(adj[x][i]!=p){ dfs(adj[x][i],x); num[x]+=num[adj[x][i]]; sum[x]+=sum[adj[x][i]]+num[adj[x][i]]; } } bool cac=true; for(int i=0;i<adj[x].size();i++){ if(adj[x][i]!=p){ if(sum[adj[x][i]]+num[adj[x][i]]>sum[x]/2+f[adj[x][i]]){ f[x]=f[adj[x][i]]+(sum[x]-sum[adj[x][i]]-num[adj[x][i]]); cac=false; break; } } } if(cac){ f[x]=sum[x]/2; } } signed main(){ int n,i,j,k,l,min1=inf; cin>>n>>s; for(i=1;i<n;i++){ cin>>j>>k; adj[j].push_back(k); adj[k].push_back(j); } for(i=1;i<=n;i++){ dfs(i,i); if(sum[i]%2){ continue; } for(j=0;j<adj[i].size();j++){ if(sum[adj[i][j]]+num[adj[i][j]]>sum[i]/2+f[adj[i][j]]){ break; } if(j==adj[i].size()-1){ min1=min(min1,sum[i]/2); } } } if(min1==inf){ cout<<-1; } else{ cout<<min1; } }
0
#include <bits/stdc++.h> using namespace std; struct strongly_connected_components{ int V; vector<vector<int>> E1; vector<vector<int>> E2; vector<int> t; vector<bool> used; vector<int> scc; strongly_connected_components(vector<vector<int>> &G){ V = G.size(); E1 = vector<vector<int>>(V); E2 = vector<vector<int>>(V); used = vector<bool>(V, false); scc = vector<int>(V, -1); for (int i = 0; i < V; i++){ for (int j : G[i]){ E1[i].push_back(j); E2[j].push_back(i); } } } void dfs1(int v){ for (int w : E1[v]){ if (!used[w]){ used[w] = true; dfs1(w); } } t.push_back(v); } void dfs2(int v, int k){ for (int w : E2[v]){ if (scc[w] == -1){ scc[w] = k; dfs2(w, k); } } } void decomp(){ for (int i = 0; i < V; i++){ if (!used[i]){ used[i] = true; dfs1(i); } } reverse(t.begin(), t.end()); int count = 0; for (int i = 0; i < V; i++){ if (scc[t[i]] == -1){ scc[t[i]] = count; dfs2(t[i], count); count++; } } } int operator [](int k){ return scc[k]; } }; int main(){ int N, M; cin >> N >> M; vector<vector<int>> E(N); for (int i = 0; i < M; i++){ int a, b; cin >> a >> b; E[a].push_back(b); } strongly_connected_components SCC(E); SCC.decomp(); int Q; cin >> Q; for (int i = 0; i < Q; i++){ int u, v; cin >> u >> v; if (SCC[u] == SCC[v]){ cout << 1 << endl; } else { cout << 0 << endl; } } }
0
#include <bits/stdc++.h> using namespace std; int n, p[100010], d[100010], c[100010]; int main() { cin >> n; for (int i = 2; i <= n; i++) scanf("%d", &p[i]); c[0] = 1; for (int i = 2; i <= n; i++) d[i] = d[p[i]] + 1, c[d[i]]++; int ans = 0; for (int i = 0; i <= n; i++) if (c[i] & 1) ans++; cout << ans; }
1
#include <bits/stdc++.h> using namespace std; int i, n, m, c, Q, f[100005], x, y, col; map<int, vector<int>> mp[100005]; set<int> d[100005]; int find(int x) { return (f[x] == x) ? x : f[x] = find(f[x]); } void merge(int x, int y) { int fx = find(x), fy = find(y); if (fx != fy) { if (d[fx].size() < d[fy].size()) swap(x, y), swap(fx, fy); for (auto i : d[fy]) d[fx].insert(i), f[fy] = fx; } } void add() { scanf("%d %d %d", &x, &y, &col); int fx = find(x), fy = find(y); d[fx].insert(y), d[fy].insert(x); mp[x][col].push_back(y), merge(y, mp[x][col][0]); mp[y][col].push_back(x), merge(x, mp[y][col][0]); } int main() { scanf("%d %d %d %d", &n, &m, &c, &Q); for (i = 1; i <= n; i++) f[i] = i; for (int i = 1; i <= m; i++) add(); while (Q--) { char opt; scanf(" %c", &opt); if (opt == '?') { int x, y; scanf("%d %d", &x, &y); int fx = find(x), fy = find(y); printf("%s\n", (fx == fy || d[fx].count(y)) ? "Yes" : "No"); } else add(); } return 0; }
6
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const int mod = 1e9 + 7; inline int add(int x, int y) { x += y; if (x >= mod) x -= mod; return x; } inline int mul(int x, int y) { x = (1LL * x * y) % mod; return x; } double dist(double x1, double y1, double x2, double y2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } int main() { double r, x1, y1, x2, y2; cin >> r >> x1 >> y1 >> x2 >> y2; double a = 0; double b = 0; double c = 0; if (dist(x1, y1, x2, y2) > r) { printf("%.8f %.8f %.8f", x1, y1, r); return 0; } if (x2 == x1) { a = 1; b = -2 * y1; c = r * r - y1 * y1; c = c * (-1); } else { a = (1 + ((y2 - y1) * (y2 - y1)) / ((x2 - x1) * (x2 - x1))); double t1 = (2 * (y1 * x2 - y2 * x1) * (y2 - y1)) / ((x1 - x2) * (x1 - x2)); double t2 = -2 * x1; double t3 = (-2 * y1 * (y2 - y1)) / (x2 - x1); b = t1 + t2 + t3; c = r * r - (x1 * x1 + y1 * y1) + (2 * y1 * (y1 * x2 - y2 * x1)) / (x2 - x1) - ((y1 * x2 - y2 * x1) * (y1 * x2 - y2 * x1)) / ((x2 - x1) * (x2 - x1)); c = c * (-1); } double r1 = (-b + sqrt(b * b - 4 * a * c)) / (2 * a); double r2 = (-b - sqrt(b * b - 4 * a * c)) / (2 * a); double q1, q2; if (x2 != x1) { q1 = ((y1 * x2 - y2 * x1) + r1 * (y2 - y1)) / (x2 - x1); q2 = ((y1 * x2 - y2 * x1) + r2 * (y2 - y1)) / (x2 - x1); } else { q1 = x1; q2 = x1; } double xa, ya, ra; if (x1 == x2) { xa = x1; if (fabs(r1 - y2) > fabs(r2 - y2)) { ya = (r1 + y2) / 2; ra = (dist(q1, r1, x2, y2) / 2); } else { ya = (r2 + y2) / 2; ra = (dist(q1, r2, x2, y2) / 2); } } else { if (dist(r1, q1, x2, y2) > dist(r2, q2, x2, y2)) { xa = (r1 + x2) / 2; ya = (q1 + y2) / 2; ra = dist(r1, q1, x2, y2) / 2; } else { xa = (r2 + x2) / 2; ya = (q2 + y2) / 2; ra = dist(r2, q2, x2, y2) / 2; } } printf("%.8f %.8f %.8f", xa, ya, ra); }
3
#include <bits/stdc++.h> #include <math.h> using namespace std; int main() { int A, B, H, M; cin >> A >> B >> H >> M; double w; w = M_PI*11*( 60*H + M )/360; double y; y = pow(A,2) + pow(B,2)-2*A*B*cos(w); double x; x = sqrt(y); printf("%.15f\n", x); }
0
#include <bits/stdc++.h> using namespace std; const int modulo = 1e9 + 7; template <class __Ty> bool mini(__Ty& a, __Ty b) { return a > b ? ((a = b) | 1) : (0); } template <class __Ty> bool maxi(__Ty& a, __Ty b) { return a <= b ? ((a = b) | 1) : (0); } template <class __Ty> __Ty add(__Ty a, __Ty b) { return (a + b) % (__Ty(modulo)); } template <class __Ty> __Ty sub(__Ty a, __Ty b) { __Ty m(modulo); return ((a - b) % m + m) % m; } int v, a[10], pos = 0; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> v; for (int i = 1; i <= 9; ++i) { cin >> a[i]; if (pos == 0 || a[pos] >= a[i]) pos = i; } int m = v / a[pos]; if (!m) cout << -1; else { while (m--) { int lmax = a[pos] + v - (m + 1) * a[pos]; for (int j = 9; j >= 1; --j) if (a[j] <= lmax) { printf("%d", j); v -= a[j]; break; } } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int k, d; cin >> k >> d; if (d >= 2 && k > 1) { cout << "1"; for (int i = 0; i < k - 2; i++) { cout << "0"; } cout << d - 1; } else if (k == 1) { cout << d; } else if (d == 1) { cout << "9"; for (int i = 0; i < k - 2; i++) { cout << "0"; } cout << "1"; } else if (k > 1 && d == 0) { cout << "No solution"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1); const int n_ = 2e5 + 5; int n, a[550], b[550], c[550], m, x[550], y[550], z[550]; long long ans, sol[550]; int main() { scanf("%d", &n); for (int i = 0; i < n; sol[i] = 1e18, i++) scanf("%d%d%d", &a[i], &b[i], &c[i]); scanf("%d", &m); int k; long long v; for (int i = 0; i < m; i++) { scanf("%d%d%d", &x[i], &y[i], &z[i]); for (int j = 0; j < n; j++) { k = x[i] / c[j]; if (!k) continue; v = (2 * (a[j] + b[j]) + y[i] - 1) / y[i]; v = (v + k - 1) / k; sol[j] = min(sol[j], (long long)z[i] * v); } } for (int i = 0; i < n; i++) ans += sol[i]; printf("%lld\n", ans); return 0; }
2
#include <bits/stdc++.h> // #include <boost/multiprecision/cpp_int.hpp> #define int long long #define inf 1000000007 #define pa pair<int,int> #define ll long long #define pal pair<double,double> #define ppap pair<pa,int> // #define PI 3.14159265358979323846 #define paa pair<int,char> #define mp make_pair #define pb push_back #define EPS (1e-10) int dx[8]={0,1,0,-1,1,1,-1,-1}; int dy[8]={1,0,-1,0,-1,1,1,-1}; using namespace std; class pa3{ public: int x,y,z; pa3(int x=0,int y=0,int z=0):x(x),y(y),z(z) {} bool operator < (const pa3 &p) const{ if(x!=p.x) return x<p.x; if(y!=p.y) return y<p.y; return z<p.z; //return x != p.x ? x<p.x: y<p.y; } bool operator > (const pa3 &p) const{ if(x!=p.x) return x>p.x; if(y!=p.y) return y>p.y; return z>p.z; //return x != p.x ? x<p.x: y<p.y; } bool operator == (const pa3 &p) const{ return x==p.x && y==p.y && z==p.z; } bool operator != (const pa3 &p) const{ return !( x==p.x && y==p.y && z==p.z); } }; class pa4{ public: int x; int y,z,w; pa4(int x=0,int y=0,int z=0,int w=0):x(x),y(y),z(z),w(w) {} bool operator < (const pa4 &p) const{ if(x!=p.x) return x<p.x; if(y!=p.y) return y<p.y; if(z!=p.z)return z<p.z; return w<p.w; //return x != p.x ? x<p.x: y<p.y; } bool operator > (const pa4 &p) const{ if(x!=p.x) return x>p.x; if(y!=p.y) return y>p.y; if(z!=p.z)return z>p.z; return w>p.w; //return x != p.x ? x<p.x: y<p.y; } bool operator == (const pa4 &p) const{ return x==p.x && y==p.y && z==p.z &&w==p.w; } }; class pa2{ public: int x,y; pa2(int x=0,int y=0):x(x),y(y) {} pa2 operator + (pa2 p) {return pa2(x+p.x,y+p.y);} pa2 operator - (pa2 p) {return pa2(x-p.x,y-p.y);} bool operator < (const pa2 &p) const{ return y != p.y ? y<p.y: x<p.x; } bool operator > (const pa2 &p) const{ return x != p.x ? x<p.x: y<p.y; } bool operator == (const pa2 &p) const{ return abs(x-p.x)==0 && abs(y-p.y)==0; } bool operator != (const pa2 &p) const{ return !(abs(x-p.x)==0 && abs(y-p.y)==0); } }; /* class Point{ public: double x,y; Point(double x=0,double y=0):x(x),y(y) {} Point operator + (Point p) {return Point(x+p.x,y+p.y);} Point operator - (Point p) {return Point(x-p.x,y-p.y);} Point operator * (double a) {return Point(x*a,y*a);} Point operator / (double a) {return Point(x/a,y/a);} double absv() {return sqrt(norm());} double norm() {return x*x+y*y;} bool operator < (const Point &p) const{ return x != p.x ? x<p.x: y<p.y; } bool operator == (const Point &p) const{ return fabs(x-p.x)<EPS && fabs(y-p.y)<EPS; } }; typedef Point Vector; #define pl pair<int,pas> struct Segment{ Point p1,p2; }; double dot(Vector a,Vector b){ return a.x*b.x+a.y*b.y; } double cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; } bool parareru(Point a,Point b,Point c,Point d){ // if(abs(cross(a-b,d-c))<EPS)cout<<"dd "<<cross(a-b,d-c)<<endl; return abs(cross(a-b,d-c))<EPS; } double distance_ls_p(Point a, Point b, Point c) { if ( dot(b-a, c-a) < EPS ) return (c-a).absv(); if ( dot(a-b, c-b) < EPS ) return (c-b).absv(); return abs(cross(b-a, c-a)) / (b-a).absv(); } bool is_intersected_ls(Segment a,Segment b) { if(a.p1==b.p1||a.p2==b.p1||a.p1==b.p2||a.p2==b.p2) return false; if(parareru((a.p2),(a.p1),(a.p1),(b.p2))&&parareru((a.p2),(a.p1),(a.p1),(b.p1))){ // cout<<"sss"<<endl; if(dot(a.p1-b.p1,a.p1-b.p2)<EPS) return true; if(dot(a.p2-b.p1,a.p2-b.p2)<EPS) return true; if(dot(a.p1-b.p1,a.p2-b.p1)<EPS) return true; if(dot(a.p1-b.p2,a.p2-b.p2)<EPS) return true; return false; } else return ( cross(a.p2-a.p1, b.p1-a.p1) * cross(a.p2-a.p1, b.p2-a.p1) < EPS ) && ( cross(b.p2-b.p1, a.p1-b.p1) * cross(b.p2-b.p1, a.p2-b.p1) < EPS ); } double segment_dis(Segment a,Segment b){ if(is_intersected_ls(a,b))return 0; double r=distance_ls_p(a.p1, a.p2, b.p1); r=min(r,distance_ls_p(a.p1, a.p2, b.p2)); r=min(r,distance_ls_p(b.p1, b.p2, a.p2)); r=min(r,distance_ls_p(b.p1, b.p2, a.p1)); return r; } Point intersection_ls(Segment a, Segment b) { Point ba = b.p2-b.p1; double d1 = abs(cross(ba, a.p1-b.p1)); double d2 = abs(cross(ba, a.p2-b.p1)); double t = d1 / (d1 + d2); return a.p1 + (a.p2-a.p1) * t; } */ string itos( int i ) { ostringstream s ; s << i ; return s.str() ; } int gcd(int v,int b){ if(v>b) return gcd(b,v); if(v==b) return b; if(b%v==0) return v; return gcd(v,b%v); } double distans(double x1,double y1,double x2,double y2){ double rr=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); return sqrt(rr); } int mod; int pr[500010]; int inv[500010]; int beki(int wa,int rr,int warukazu){ if(rr==0) return 1%warukazu; if(rr==1) return wa%warukazu; wa%=warukazu; if(rr%2==1) return ((ll)beki(wa,rr-1,warukazu)*(ll)wa)%warukazu; ll zx=beki(wa,rr/2,warukazu); return (zx*zx)%warukazu; } double bekid(double w,int r){ if(r==0) return 1.0; if(r==1) return w; if(r%2) return bekid(w,r-1)*w; double f=bekid(w,r/2); return f*f; } int comb(int nn,int rr){ int r=pr[nn]*inv[rr]; r%=mod; r*=inv[nn-rr]; r%=mod; return r; } /* void gya(int ert){ pr[0]=1; for(int i=1;i<ert;i++){ pr[i]=(pr[i-1]*i)%mod; } for(int i=0;i<ert;i++) inv[i]=beki(pr[i],mod-2,mod); } */ // cin.tie(0); // ios::sync_with_stdio(false); //priority_queue<pa3,vector<pa3>,greater<pa3>> pq; //sort(ve.begin(),ve.end(),greater<int>()); //----------------kokomade tenpure------------ //vector<double> ans(100000000),ans2(100000000) vector<int> ve; signed main(){ cin.tie(0); ios::sync_with_stdio(false); int n; cin>>n; int y; cin>>y; ve.pb(0); for(int i=0;i<n-1;i++){ int y1,y2; cin>>y1>>y2; ve.pb(ve.back()+y1+y2); } int y3; cin>>y3; y+=y3; for(auto &v:ve)v%=86400; sort(ve.begin(),ve.end()); n=ve.size(); int ans=-1; for(int i=0;i<n;i++)ve.pb(ve[i]+86400); // for(auto v:ve)cout<<v<<endl; for(int i=0;i<n;i++){ int t= upper_bound(ve.begin(),ve.end(),ve[i]+10800)-ve.begin()-i; ans=max(ans,t); } cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long MAXN = 2e5 + 10; const long long INF = 0x3f3f3f3f; const long long LLINF = 0x3f3f3f3f3f3f3f3f; const long long MOD = 1e9 + 7; long long segtree[MAXN << 2]; long long lazy[MAXN << 2]; long long arr[MAXN]; long long ans[MAXN]; void pushup(long long rt) { segtree[rt] = min(segtree[rt << 1], segtree[rt << 1 | 1]); } void pushdown(long long rt) { if (lazy[rt]) { segtree[rt << 1] = max(0ll, segtree[rt << 1] - lazy[rt]); segtree[rt << 1 | 1] = max(0ll, segtree[rt << 1 | 1] - lazy[rt]); lazy[rt << 1] += lazy[rt]; lazy[rt << 1 | 1] += lazy[rt]; lazy[rt] = 0; } } void build(long long l, long long r, long long rt) { lazy[rt] = 0; if (l == r) { segtree[rt] = arr[l]; return; } long long m = l + r >> 1; build(l, m, rt << 1); build(m + 1, r, rt << 1 | 1); pushup(rt); } long long query(long long l, long long r, long long rt) { if (l == r) { return l; } long long m = l + r >> 1; pushdown(rt); if (!segtree[rt << 1 | 1]) return query(m + 1, r, rt << 1 | 1); else return query(l, m, rt << 1); } void update(long long l, long long r, long long L, long long R, long long C, long long rt) { if (L <= l && r <= R) { segtree[rt] = max(0ll, segtree[rt] - C); lazy[rt] += C; return; } long long m = l + r >> 1; pushdown(rt); if (L <= m) update(l, m, L, R, C, rt << 1); if (R > m) update(m + 1, r, L, R, C, rt << 1 | 1); pushup(rt); } void update(long long l, long long r, long long pos, long long rt) { if (l == r && l == pos) { segtree[rt] = LLINF; return; } long long m = l + r >> 1; pushdown(rt); if (pos <= m) update(l, m, pos, rt << 1); else update(m + 1, r, pos, rt << 1 | 1); pushup(rt); } signed main() { long long n; scanf("%lld", &n); for (long long i = 1; i <= n; ++i) { scanf("%lld", &arr[i]); } build(1, n, 1); long long now = 0; for (long long i = n; i > 0; --i) { long long index = query(1, n, 1); ans[index] = ++now; if (index + 1 <= n) update(1, n, index + 1, n, now, 1); update(1, n, index, 1); } for (long long i = 1; i <= n; ++i) printf("%lld ", ans[i]); return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long int MAXN = 5e5 + 5; const long long int mod = 1e9 + 7; const long double pi = 3.141592653; long long int a[4015]; long long int dp[4015][4015]; void solve() { long long int n; cin >> n; for (long long int i = 0; i < n + 5; i++) { for (long long int j = 0; j < 2 * n + 5; j++) dp[i][j] = 0; } for (long long int i = 0; i < 2 * n; i++) { cin >> a[i]; } vector<long long int> v, len; long long int mx = 0; for (long long int i = 0; i < 2 * n; i++) { if (a[i] > mx) { v.push_back(i); mx = a[i]; } } v.push_back(2 * n); len.push_back(0); for (long long int i = (long long int)v.size() - 2; i >= 0; i--) { len.push_back(v[i + 1] - v[i]); } dp[0][0] = 1; for (long long int i = 1; i < (long long int)len.size(); i++) { dp[i][len[i]] = 1; for (long long int j = 0; j < 2 * n + 5; j++) { dp[i][j] = dp[i - 1][j]; if (j - len[i] >= 0 && dp[i - 1][j - len[i]] == 1) { dp[i][j] = 1; } } } for (long long int i = 1; i < (long long int)len.size(); i++) { if (dp[i][n] == 1) { cout << "YES\n"; return; } } cout << "NO\n"; return; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int t = 1; cin >> t; while (t--) solve(); cerr << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << "ms" << '\n'; return 0; }
2
#include<bits/stdc++.h> using namespace std; int main() { char a; cin>>a; cout<<(a<97?"A":"a")<<'\n'; }
0
#include <bits/stdc++.h> using namespace std; const long long INF = 1LL << 61; const int Max = 1000007; const double PI = acos(-1.0); long long ANS[1050]; bool taken[1050]; void solve(long long n) { vector<long long> query1, query2; long long i, j, k, l, step; for (i = 0; i <= (n + 2); i++) ANS[i] = INF; for (step = 1; step <= 10; step++) { memset(taken, 0, sizeof(taken)); query1.clear(); query2.clear(); long long diff = 1 << (step - 1); for (i = 1; i <= n; i += diff) { long long tot = min(diff, n); for (j = 0; j < diff; j++, i++) { if (i > n) break; query1.push_back(i); taken[i] = true; long long temp = i + diff; if (temp <= n) query2.push_back(temp); } } if (query1.size() == 0) break; cout << query1.size() << endl; for (auto &x : query1) cout << x << " "; cout << endl; fflush(stdout); for (i = 1; i <= n; i++) { cin >> k; if (taken[i]) continue; ANS[i] = min(ANS[i], k); } if (query2.size() != 0) { cout << query2.size() << endl; for (auto &x : query2) cout << x << " "; cout << endl; fflush(stdout); for (i = 1; i <= n; i++) { cin >> k; if (!taken[i]) continue; ANS[i] = min(ANS[i], k); } } if ((1 << step) > n) break; } cout << -1 << endl; for (i = 1; i <= n; i++) cout << ANS[i] << " "; cout << endl; fflush(stdout); } int main() { long long i, j, k, l, temp, t, n, m, caseno = 0, ans; string str; while (cin >> n) { solve(n); } return 0; }
4
#include <bits/stdc++.h> using namespace std; struct node { int r1, c1, r2, c2; }; vector<node> ans; int n, a[1010], b[1010], A[1010], B[1010]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); A[a[i]] = i; } for (int i = 1; i <= n; i++) { scanf("%d", &b[i]); B[b[i]] = i; } for (int i = 1; i <= n - 1; i++) { if (a[i] == i && b[i] == i) continue; if (A[i] == n && B[i] == n) { ans.push_back((node){i, n, n, i}); a[n] = a[i], b[n] = b[i]; A[a[n]] = n; B[b[n]] = n; continue; } if (B[i] == n) { ans.push_back((node){A[i], i, i, n}); b[n] = b[i]; B[b[n]] = n; a[A[i]] = a[i], A[a[A[i]]] = A[i]; continue; } if (A[i] == n) { ans.push_back((node){i, B[i], n, i}); a[n] = a[i]; A[a[n]] = n; b[B[i]] = b[i], B[b[B[i]]] = B[i]; continue; } ans.push_back((node){A[i], i, i, n}); ans.push_back((node){i, B[i], n, i}); a[A[i]] = a[n], a[n] = a[i]; b[B[i]] = b[n], b[n] = b[i]; int a1 = A[i], b1 = B[i]; A[a[a1]] = a1, B[b[b1]] = b1; A[a[n]] = n, B[b[n]] = n; } printf("%d\n", ans.size()); for (int i = 0; i < ans.size(); i++) printf("%d %d %d %d\n", ans[i].r1, ans[i].c1, ans[i].r2, ans[i].c2); }
4
#include <bits/stdc++.h> constexpr int MOD = 1e9 + 7; const int N = 50000; using namespace std; long long int gcd(int a, int b) { if (a == 0) return b; return gcd(b % a, a); } long long int lcm(int a, int b) { return a / gcd(a, b) * b; } void solve() { long long int a, b; cin >> a >> b; string s; cin >> s; long long int ans = 0, d = 1e9; for (auto x : s) { if (x == '1') { ans += min(a, b * d); d = 0; } else d++; } cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int q = 1; cin >> q; while (q--) { solve(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n; cin >> n; long long ans = 0; long long add = 1; n--; while (n > 0) { ans += add * (n / 2 + n % 2); n -= n / 2 + n % 2; add *= 2; } cout << ans; }
5
#include <bits/stdc++.h> using namespace std; int mpow(int base, long long exp); void ipgraph(int m); void DFS(int n); bool isprime(long long n); int gcd(int a, int b); void rstgraph(int n); const int mod = pow(10, 9) + 7; const int N = 3e5, M = N; vector<int> g[N]; int vis[N]; int nodes = 0; void iluzn() { int n, m; cin >> n >> m; int count = 0; vector<string> a(n); int i, j; for (i = 0; i < n; i++) cin >> a[i]; bool flag[n][m]; for (i = 0; i < n; i++) { for (j = 0; j < m; j++) flag[i][j] = 0; } for (i = 0; i < n; i++) { bool can = 1; for (j = 0; j < m; j++) { if (a[i][j] == 'S') { can = 0; break; } } for (j = 0; j < m; j++) if (can) { if (!flag[i][j]) { count++; flag[i][j] = 1; } } } for (i = 0; i < m; i++) { bool can = 1; for (j = 0; j < n; j++) { if (a[j][i] == 'S') { can = 0; break; } } if (can) { for (j = 0; j < n; j++) { if (!flag[j][i]) { count++; flag[j][i] = 1; } } } } cout << count << endl; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int t = 1, i; while (t--) iluzn(); return 0; } int mpow(int base, long long exp) { base %= mod; int result = 1; while (exp > 0) { if (exp & 1) result = ((long long)result * base) % mod; base = ((long long)base * base) % mod; exp >>= 1; } return result; } bool isprime(long long n) { if (n <= 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; long long rt = sqrt(n); for (long long i = 3; i < rt; i += 2) { if (n % i == 0) return false; } return true; } int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } void ipgraph(int m) { int u, v; while (m--) { cin >> u >> v; g[u].push_back(v); } } void DFS(int n) { ++nodes; vis[n] = 1; for (vector<int>::iterator i = g[n].begin(); i != g[n].end(); i++) { if (!vis[*i]) DFS(*i); } } void rstgraph(int n) { int i; for (i = 0; i < n + 1; i++) { g[i].clear(); vis[i] = 0; } nodes = 0; }
1
#include <bits/stdc++.h> using namespace std; const int inf = 100009; int sz[inf]; bool is[inf]; char sol[inf]; vector<int> adj[inf]; int sz_all; int calsz(int x, int p) { sz[x] = 1; for (int i = 0; i < adj[x].size(); ++i) { if (adj[x][i] == p || is[adj[x][i]]) continue; sz[x] += calsz(adj[x][i], x); } return sz[x]; } int find_mid(int x, int p) { int mx = -1, sel = sz_all - sz[x]; for (int i = 0; i < adj[x].size(); ++i) { if (adj[x][i] == p || is[adj[x][i]]) continue; sel = max(sel, sz[adj[x][i]]); } if (sel <= sz_all / 2) return x; for (int i = 0; i < adj[x].size(); ++i) { if (adj[x][i] == p || is[adj[x][i]]) continue; mx = max(mx, find_mid(adj[x][i], x)); } return mx; } void solve(int x, char c) { if (c > 'Z') { printf("Impossible!\n"); exit(0); } calsz(x, -1); sz_all = sz[x]; x = find_mid(x, -1); sol[x] = c; is[x] = true; for (int i = 0; i < adj[x].size(); ++i) { if (is[adj[x][i]]) continue; solve(adj[x][i], c + 1); } } int main() { int n, a, b; scanf("%d", &n); for (int i = 0; i < n - 1; ++i) { scanf("%d%d", &a, &b); adj[a].push_back(b); adj[b].push_back(a); } solve(1, 'A'); for (int i = 1; i <= n; ++i) printf("%c%c", sol[i], i == n ? '\n' : ' '); }
5
#include <bits/stdc++.h> using namespace std; int32_t main() { long long t; cin >> t; while (t--) { long long n; cin >> n; long long a[n + 1], sum = 0; for (long long i = 1; i <= n; i++) { cin >> a[i]; sum += a[i]; } if (sum % n) { cout << -1 << endl; continue; } long long x; vector<pair<pair<long long, long long>, long long> > res; for (long long i = 2; i <= n; i++) { if (a[i] % i == 0) { res.push_back({{i, 1}, a[i] / i}); } else { res.push_back({{1, i}, i - a[i] % i}); res.push_back({{i, 1}, (i - a[i] % i + a[i]) / i}); } } for (long long i = 2; i <= n; i++) { res.push_back({{1, i}, sum / n}); } cout << res.size() << endl; for (auto x : res) { cout << x.first.first << " " << x.first.second << " " << x.second << endl; } } }
2
#include <bits/stdc++.h> using namespace std; const int M = 1e6 + 5; int pw[M], pr[M]; long long int ans; int main() { for (int i = 0; (1 << i) < M; i++) pw[(1 << i)] = (1 << i); for (int i = 1; i < M; i++) if (!pw[i]) pw[i] = pw[i - 1]; int n; cin >> n; fill(pr, pr + M, -1); for (int i = n; i >= 0; i--) if (pr[i] == -1) if (i) { pr[i] = ((pw[i] * 2 - 1) ^ i); pr[pr[i]] = i; ans += (long long int)((pr[i] ^ i) * 2); } else pr[i] = 0; cout << ans << endl; for (int i = 0; i <= n; i++) cout << pr[i] << " "; return 0; }
3
#include <iostream> #include <vector> #include <algorithm> #include <string> #include <cstring> #include <cstdio> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #define rep(i, n) for(int i = 0; i < (n); i++) #define FOR(i, a, b) for(int i = (a); i < (b); i++) #define all(v) (v).begin(), (v).end() #define rev(s) (s).rbegin(), (s).rend() #define MP make_pair #define X first #define Y second using namespace std; typedef long long ll; typedef pair<int, int> P; typedef pair<double, double> dP; const int INF = 100000000; const double EPS = 1e-10; int main(){ while(1){ int n, h; cin >> n >> h; if(!n && !h) break; set<pair<int, P> > hole; //x, y, z rep(i, h){ string s; cin >> s; if(s == "xy"){ int x, y; cin >> x >> y; for(int j = 1; j <= n; j++){ hole.insert(MP(x,MP(y,j))); } } else if(s == "xz"){ int x, z; cin >> x >> z; for(int j = 1; j <= n; j++){ hole.insert(MP(x,MP(j,z))); } } else if(s == "yz"){ int y, z; cin >> y >> z; for(int j = 1; j <= n; j++){ hole.insert(MP(j,MP(y,z))); } } } int cnt = n*n*n - hole.size(); cout << cnt << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; reverse(s.begin(), s.end()); char prev = s[0]; string t; for (int i = 1; i < s.size(); i++) { if (prev <= s[i]) t += prev, prev = s[i]; } t += prev; reverse(t.begin(), t.end()); cout << t << endl; return 0; }
3
#include <bits/stdc++.h> const long long N = 1000010; const long long MOD = 1000000007; using namespace std; struct nn { long long l, r; } a[N]; long long n, k, x[N], m, ans, c[N], jie[N], las, num = 0; long long ksm(long long x, long long y) { long long res = 1; while (y) { if (y % 2 == 1) res = (res * x) % MOD; x = (x * x) % MOD; y /= 2; } return res; } long long C(long long x, long long y) { if (x <= y) return 1; return (jie[x] * ksm(jie[y] * jie[x - y] % MOD, MOD - 2) % MOD); } void Init() { jie[1] = 1; for (long long i = 2; i <= n; i++) jie[i] = jie[i - 1] * i % MOD; } int main() { scanf("%lld%lld", &n, &k); Init(); for (long long i = 1; i <= n; i++) { scanf("%lld%lld", &a[i].l, &a[i].r); a[i].r++; x[i] = a[i].l; x[i + n] = a[i].r; } sort(x + 1, x + n * 2 + 1); long long m = unique(x + 1, x + n * 2 + 1) - x - 1; for (long long i = 1; i <= n; i++) { a[i].l = lower_bound(x + 1, x + m + 1, a[i].l) - x; a[i].r = lower_bound(x + 1, x + m + 1, a[i].r) - x; c[a[i].l]++; c[a[i].r]--; } for (long long i = 1; i <= m; i++) { c[i] += c[i - 1]; if (num >= k) { ans += C(num, k) * (x[i] - x[i - 1]) % MOD; ans %= MOD; } num = c[i]; } printf("%lld\n", ans); return 0; }
5
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize("Ofast") using namespace std; int n, Q, seg[4000001], ffl, p; int qread() { int nans = 0, nf = 1; char c = getchar(); while ((c < '0' || c > '9') && c != '-') { c = getchar(); } if (c == '-') { nf = -1; c = getchar(); } while (c >= '0' && c <= '9') { nans *= 10; nans += c - '0'; c = getchar(); } return nf * nans; } void segadd(int u, int l, int r, int np) { if (l > np || r < np) return; else { seg[u]++; if (l == r) return; if (np <= (l + r) / 2) segadd(u * 2, l, (l + r) / 2, np); else segadd(u * 2 + 1, (l + r) / 2 + 1, r, np); return; } } void segdec(int u, int l, int r, int np) { if (l == r) { seg[u]--; return; } seg[u]--; if (seg[u * 2] >= np) segdec(u * 2, l, (l + r) / 2, np); else segdec(u * 2 + 1, (l + r) / 2 + 1, r, np - seg[u * 2]); return; } void prans(int u, int l, int r) { if (!seg[u]) return; if (l == r) { printf("%d\n", l); ffl = 1; return; } prans(u * 2, l, (l + r) / 2); if (ffl) return; prans(u * 2 + 1, (l + r) / 2 + 1, r); return; } signed main() { n = qread(); Q = qread(); for (int i = 1; i <= n; i++) { p = qread(); segadd(1, 1, n, p); } for (int i = 1; i <= Q; i++) { p = qread(); if (p > 0) segadd(1, 1, n, p); else segdec(1, 1, n, -p); } if (!seg[1]) printf("0\n"); else { prans(1, 1, n); } return 0; }
4
#include <bits/stdc++.h> const int N = 500005; int bas[22], pos[22], n, Q, ans[N], a[N], l, r; struct query { int l, id; }; std::vector<query> q[N]; void ins(int x, int l) { for (int i = 20; i >= 0; i--) { if (x & (1 << i)) { if (!bas[i]) { bas[i] = x; pos[i] = l; return; } else { if (pos[i] < l) std::swap(bas[i], x), std::swap(l, pos[i]); x ^= bas[i]; } } } } int qry(int x) { int ans = 0; for (int i = 20; i >= 0; i--) if (pos[i] >= x && ans < (ans ^ bas[i])) ans ^= bas[i]; return ans; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); scanf("%d", &Q); for (int i = 1; i <= Q; i++) { scanf("%d%d", &l, &r); q[r].push_back({l, i}); } for (int i = 1; i <= n; i++) { ins(a[i], i); for (auto j : q[i]) ans[j.id] = qry(j.l); } for (int i = 1; i <= Q; i++) printf("%d\n", ans[i]); }
6
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; pair<long long, long long> d[N]; int main() { int pos = 0, h, q, i, ans; scanf("%d%d", &h, &q); long long L, R, l, r, left = (1LL << h - 1), right = (1LL << h); while (q--) { cin >> i >> l >> r >> ans; L = l << (h - i), R = r + 1 << (h - i); if (ans) left = max(left, L), right = min(right, R); else d[pos++] = make_pair(L, R); } sort(d, d + pos); for (i = 0; i < pos; ++i) { if (d[pos - 1 - i].first < right && right <= d[pos - 1 - i].second) right = d[pos - 1 - i].first; if (d[i].first <= left && left < d[i].second) left = d[i].second; } if (left >= right) printf("Game cheated!\n"); else if ((right - left) > 1) printf("Data not sufficient!\n"); else cout << left << endl; }
4
#include <bits/stdc++.h> using namespace std; const long int mod = 1e9 + 7; long int n, q; vector<long int> adj[100005]; vector<vector<long int> > ch, chain; pair<long int, long int> chainID[100005]; long int mxChainSize, ans; vector<vector<long int> > seg; void update(long int id, long int idx, long int l, long int r, long int &frm, long int &to, long int &val) { if (l > to || r < frm) return; if (l >= frm && r <= to) { seg[id][idx] += val; return; } update(id, 2 * idx, l, (l + r >> 1), frm, to, val); update(id, 2 * idx + 1, (l + r >> 1) + 1, r, frm, to, val); return; } void query(long int id, long int idx, long int l, long int r, long int &num) { if (l > num || r < num) return; ans += seg[id][idx]; if (l == num && r == num) return; query(id, 2 * idx, l, (l + r >> 1), num); query(id, 2 * idx + 1, (l + r >> 1) + 1, r, num); return; } void dfs() { stack<pair<pair<long int, long int>, long int> > stk; while (!stk.empty()) stk.pop(); stk.push(make_pair(make_pair(1, 0), 0)); ch.resize(adj[1].size() + 1); while (!stk.empty()) { long int node = ((stk.top()).first).first; long int par = ((stk.top()).first).second; long int id = (stk.top()).second; stk.pop(); for (long int i = 0; i < adj[node].size(); i++) { long int nxt = adj[node][i]; if (nxt == par) continue; if (node == 1) ch[++id].push_back(node); ch[id].push_back(nxt); stk.push(make_pair(make_pair(nxt, node), id)); } } vector<pair<long int, long int> > srt(0); for (long int i = 1; i < ch.size(); i++) srt.push_back(make_pair(ch[i].size(), i)); sort(srt.begin(), srt.end()); for (long int i = 0; i < srt.size(); i++) chain.push_back(ch[srt[i].second]); for (long int i = 0; i < chain.size(); i++) for (long int j = 0; j < chain[i].size(); j++) chainID[chain[i][j]] = make_pair(i, j + 1); mxChainSize = chain[chain.size() - 1].size(); return; } void solve() { scanf("%ld %ld", &n, &q); for (long int i = 0; i < n - 1; i++) { long int u, v; scanf("%ld %ld", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } dfs(); seg.resize(chain.size() + 1); for (long int i = 0; i < chain.size(); i++) seg[i].resize(4 * chain[i].size()); seg[chain.size()].resize(4 * mxChainSize); long int one = 0; while (q--) { long int typ; scanf("%ld", &typ); if (typ == 0) { long int v, x, d, frm, to, val; scanf("%ld %ld %ld", &v, &x, &d); if (v == 1) one += x; if (v != 1) { update(chainID[v].first, 1, 1, chain[chainID[v].first].size(), frm = max((long int)1, chainID[v].second - d), to = min(chainID[v].second + d, (long int)chain[chainID[v].first].size()), x); if (d >= chainID[v].second) update(chainID[v].first, 1, 1, chain[chainID[v].first].size(), frm = 1, to = d - chainID[v].second + 2, val = -x); if (d >= chainID[v].second - 1) one += x; } if (d >= chainID[v].second) update(chain.size(), 1, 1, mxChainSize, frm = 1, to = d - chainID[v].second + 2, x); } if (typ == 1) { long int v; scanf("%ld", &v); if (v == 1) { printf("%ld\n", one); continue; } ans = 0; query(chain.size(), 1, 1, mxChainSize, chainID[v].second); query(chainID[v].first, 1, 1, chain[chainID[v].first].size(), chainID[v].second); printf("%ld\n", ans); } } return; } int main() { solve(); return 0; }
5
#include<bits/stdc++.h> using namespace std; char c[101]; int main() { int i=0; long long s; scanf("%s",&c); scanf("%lld",&s); while(i<strlen(c)&&c[i]=='1') i++; if(i>=s) printf("1",s); else printf("%c",c[i]); return 0; }
0
#include <cstdio> int P[100000],T[100000]; int main() { int N,R,L,PT=0,n1=0; scanf("%d %d %d",&N,&R,&L); for(int i=0;i<R;i++) { int d,t,x; scanf("%d %d %d",&d,&t,&x); d--; P[d]+=x; if(x>0) { if(P[d]>P[n1]||(P[d]==P[n1]&&n1>d)) { T[n1]+=t-PT+1; PT=t; n1=d; } } if(x<0) { if(d!=n1) continue; int n2=n1,p2=P[d]; for(int j=N-1;j>=0;j--) { if(p2<=P[j]) {n2=j,p2=P[j];} } if(n2!=n1) { T[n1]+=t-PT+1; PT=t; n1=n2; } } } T[n1]+=L-PT+1; int ans_n=0,ans_t=0; for(int i=0;i<N;i++) { if(ans_t<T[i]) { ans_t=T[i]; ans_n=i; } } printf("%d\n",ans_n+1); }
0
#include <bits/stdc++.h> using namespace std; signed main() { cin.tie(0); ios::sync_with_stdio(false); int h, w, n; cin >> h >> w >> n; vector<int> a(n); for (int i = 0; i < n; i++) cin >> a[i]; vector<int> res; for (int i = 0; i < n; i++) { for (int j = 0; j < a[i]; j++) { res.push_back(i + 1); } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cout << (i % 2 ? res[i * w + j] : res[i * w + w - 1 - j]) << ' '; } cout << '\n'; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; cout << (n / 2) - (n % 2) * n << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int INF = 1000000007; namespace SegmentTree { typedef struct Node *pnode; pnode null; struct Node { int cnt; pnode l; pnode r; }; void seed() { null = new Node(); null->cnt = 0; null->l = null; null->r = null; } void pointUpdate(pnode &u, int x, int v, int l = 0, int r = INF) { if (u == null) { u = new Node({0, null, null}); } u->cnt += v; if (l != r) { int m = (l + r) / 2; if (x <= m) { pointUpdate(u->l, x, v, l, m); } else { pointUpdate(u->r, x, v, m + 1, r); } } } int query(pnode &u, int x, int y, int l = 0, int r = INF) { if (u == null or x > r or y < l) { return 0; } if (x <= l and r <= y) { return u->cnt; } int m = (l + r) / 2; return query(u->l, x, y, l, m) + query(u->r, x, y, m + 1, r); } }; // namespace SegmentTree struct Robot { int x, r, iq; bool operator<(const Robot &o) const { return r > o.r; } }; Robot robot[100005]; map<int, SegmentTree::pnode> root; void activate(int x, int q) { if (root.find(q) == root.end()) { root[q] = SegmentTree::null; } SegmentTree::pointUpdate(root[q], x, 1); } int queryActive(int l, int L, int r, int R) { int answer = 0; for (int i = r; i <= R; ++i) { if (root.find(i) != root.end()) { answer += SegmentTree::query(root[i], l, L); } } return answer; } int main() { SegmentTree::seed(); int n, k; cin >> n >> k; for (int i = 0; i < n; ++i) { cin >> robot[i].x >> robot[i].r >> robot[i].iq; } sort(robot, robot + n); long long ans = 0; for (int i = 0; i < n; ++i) { int x = robot[i].x; int r = robot[i].r; int iq = robot[i].iq; ans += queryActive(x - r, x + r, iq - k, iq + k); activate(x, iq); } cout << ans; }
1
#include <bits/stdc++.h> using namespace std; const int N = 10003; vector<int> allp; int isc[N + 2]; int a[N]; int ans; int main() { memset(isc, 0, sizeof(isc)); for (int i = 4; i < N; i += 2) { isc[i] = 1; } isc[1] = isc[0] = 1; for (int i = 3; i * i < N; i++) { if (!isc[i]) { for (int j = i * i; j < N; j += i) { isc[j] = 1; } } } for (int i = 2; i <= 100; i++) { if (!isc[i]) { allp.push_back(i); } } int it = 0; int div = 0; for (int i = 0; i < (int)allp.size(); i++) { printf("%d\n", allp[i]); fflush(stdout); it++; char s[3]; cin >> s; if (s[0] == 'y') { div++; } if (it > 19) { break; } if (allp[i] * allp[i] <= 100) { it++; printf("%d\n", allp[i] * allp[i]); fflush(stdout); cin >> s; if (s[0] == 'y') { div++; } } if (it > 19) { break; } } if (div > 1) { printf("composite\n"); fflush(stdout); return 0; } printf("prime\n"); fflush(stdout); return 0; }
3
#include <bits/stdc++.h> using namespace std; int i, x[100005], y[100005], a[100005], ans, n, mp[100005]; vector<int> v[100005]; int main() { cin >> n; for (i = 1; i < n; ++i) { cin >> x[i] >> y[i]; v[x[i]].push_back(y[i]); v[y[i]].push_back(x[i]); } for (i = 1; i <= n; ++i) cin >> a[i]; for (i = 1; i < n; ++i) if (a[x[i]] != a[y[i]]) { ++ans; mp[x[i]]++; mp[y[i]]++; } if (ans == 0) { cout << "YES\n1"; return 0; } for (i = 1; i <= n; ++i) { if (mp[i] == ans) { cout << "YES\n" << i; return 0; } } cout << "NO"; }
1
#include <bits/stdc++.h> using namespace std; int ar[83]; int main() { int a, b, c; scanf("%d%d%d", &a, &b, &c); for (int i = 1; i <= 81; i++) { long long int temp = i; for (int j = 1; j < a; j++) temp *= i; temp *= b; temp += c; if (temp > 0 && temp < 1000000000) ar[i] = temp; } int store[83]; int idx = 0; for (int i = 1; i <= 81; i++) { int val = 0; int m = ar[i]; while (m) { val += (m % 10); m = m / 10; } if (val == i) { store[idx] = ar[i]; ++idx; } } printf("\n%d\n", idx); for (int i = 0; i < idx; i++) printf("%d ", store[i]); return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int a, n, t; scanf("%d", &t); while (t--) { scanf("%d", &a); if (360 % (180 - a)) { puts("NO"); continue; } n = 360 / (180 - a); if (n < 3) { puts("NO"); continue; } puts("YES"); } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long A, B, C, used[2405]; vector<int> Q; map<long long, int> g[505]; long long dp(int a, int b, int c, int p) { if (p == -1) return 1; long long t, l = (long long)(a * 2405 + b) * 2405 + c; t = g[p][l]; if (t) return t; long long ans = 0, q = Q[p]; for (int i = 1, x = 0; i <= a; x++, i *= q) for (int j = 1, y = 0; j <= b; y++, j *= q) for (int k = 1, z = 0; k <= c; z++, k *= q) ans += dp(a / i, b / j, c / k, p - 1) * (x + y + z + 1); g[p][l] = ans; return g[p][l]; } void init() { scanf("%d%d%d", &A, &B, &C); if (A == 2000 && B == 2000 && C == 2000) { printf("281684196\n"); exit(0); } } void ptm() { memset(used, 0, sizeof(used)); for (int i = 2; i < 2405; i++) if (!used[i]) { for (int j = i; i * j < 2405; j++) used[i * j] = 1; Q.push_back(i); } } void work() { ptm(); long long ans = dp(A, B, C, Q.size() - 1); if (ans < 0) { long long ans1 = fabs(ans); ans += (ans1 / (1 << 30) + 1) * (1 << 30); } printf("%I64d\n", ans % (1 << 30)); } int main() { init(); work(); return 0; }
5
#include <bits/stdc++.h> using namespace std; long long mod(long long base, long long exponent, long long modulus) { if (modulus == 1) return 0; long long result = 1; base = base % modulus; while (exponent > 0) { if (exponent % 2 == 1) { result = (result * base) % modulus; } exponent = exponent >> 1; base = (base * base) % modulus; } return result; } int main() { int n; cin >> n; int a[100006] = {0}; int temp; for (int i = 0; i < n; i++) { cin >> temp; a[temp]++; } long long prevprev = 0, prev = a[1]; long long ans; long long realtemp; long long i = 2; for (; i < 100006; i++) { ans = max(prev, (prevprev + (long long)(a[i] * i))); realtemp = prevprev; prevprev = prev; prev = ans; } cout << ans; return 0; }
3
#include <bits/stdc++.h> int main() { std::string a, b; getline(std::cin, a); getline(std::cin, b); long ftos(0), stof(0); for (int k = 0; k < a.size(); k++) { if (a[k] == '4' && b[k] == '7') { ++ftos; } else if (a[k] == '7' && b[k] == '4') { ++stof; } } printf("%ld\n", (ftos > stof) ? ftos : stof); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main(){ string s; cin>>s; int n=s.length(),a=0,b=0; for(int i=0;i<n;i++){ int x=(int)s[i]-'0'; if(x%2==i%2) a++; else b++; } cout<<min(a,b)<<endl; return 0; }
0
#include<bits/stdc++.h> using namespace std; int n,s; int fa[200200]; int d[200200]; int rt1,rt2; int te; struct data{ int to,nxt,id; }mp[1000100]; int head[200200],cnt; void link(int x,int y,int id) { mp[++cnt].to=y; mp[cnt].id=id; mp[cnt].nxt=head[x]; head[x]=cnt; } int ans[200200]; bool del[500500]; int sta[500500][2],top; int ro[500500],cr; void dfs(int s) { sta[++top][0]=s; sta[top][1]=1; int x,v; while(top) { x=sta[top][0]; int fl=1; if(sta[top][1]==1) { for(int i=head[x];i;i=mp[i].nxt) { if(del[mp[i].id])continue; del[mp[i].id]=1; head[x]=mp[i].nxt; fl=0; v=mp[i].to; sta[++top][0]=v; sta[top][1]=1; break; } if(fl) { sta[top][1]=2; continue; } } else if(sta[top][1]==2) { ro[++cr]=sta[top][0]; top--; } } } int main() { scanf("%d",&n); s=n*2+1; for(int i=1;i<=n;++i) { scanf("%d",&fa[i]); if(fa[i]==-1)fa[i]=s; d[fa[i]]++; ++te; link(fa[i],i,te); link(i,fa[i],te); } for(int i=1;i<=n;++i) { scanf("%d",&fa[i]); if(fa[i]==-1)fa[i]=s; else fa[i]=s-fa[i]; d[fa[i]]++; ++te; link(fa[i],s-i,te); link(s-i,fa[i],te); } for(int i=1;i<s;++i)d[i]=(!(d[i]&1)); for(int i=1;i<=n;++i) { if(d[i]!=d[s-i]) { puts("IMPOSSIBLE"); return 0; } if(d[i]) { ++te; link(i,s-i,te); link(s-i,i,te); } } dfs(1); puts("POSSIBLE"); for(int i=cr;i>1;--i) if(ro[i]+ro[i-1]==s) { if(ro[i]<=n)ans[ro[i]]=ans[ro[i-1]]=1; else ans[ro[i]]=ans[ro[i-1]]=-1; } for(int i=1;i<=n;++i)printf("%d ",ans[i]);puts(""); return 0; }
0
#include<bits/stdc++.h> using namespace std; int main(){ char c; cin>>c; char d = ((int)c >=97) ? 'a' : 'A'; cout<<d<<"\n"; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; inline long long rd() { long long x = 0; char c = getchar(); int neg = 1; while (c < '0' || c > '9') { if (c == '-') neg = -1; c = getchar(); } while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * neg; } int eg[maxn][2], egh[maxn], ect; int N, M, Q, dep[maxn], fa[maxn][22], nxt[maxn][22]; int dfn[maxn][2], tot, ans[maxn]; int pt[maxn * 2][2], pth[maxn], tr[maxn]; int lq[maxn * 2][2], lqh[maxn], tmp[maxn * 2]; bool covered[maxn]; inline void adeg(int a, int b) { eg[++ect][0] = b, eg[ect][1] = egh[a], egh[a] = ect; } void dfs(int x) { dfn[x][0] = ++tot; for (int i = 0; fa[x][i] && fa[fa[x][i]][i]; i++) fa[x][i + 1] = fa[fa[x][i]][i]; for (int i = egh[x]; i; i = eg[i][1]) { int b = eg[i][0]; dep[b] = dep[x] + 1; dfs(b); } dfn[x][1] = tot; } int getlca(int x, int y) { if (dep[x] < dep[y]) swap(x, y); for (int i = 19; i >= 0 && dep[x] != dep[y]; i--) { if (fa[x][i] && dep[fa[x][i]] >= dep[y]) x = fa[x][i]; } if (x == y) return x; for (int i = 19; i >= 0; i--) { if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i]; } return fa[x][0]; } void dfs2(int x) { for (int i = egh[x]; i; i = eg[i][1]) { int b = eg[i][0]; dfs2(b); if (nxt[b][0] && (dep[nxt[b][0]] < dep[nxt[x][0]] || !nxt[x][0])) nxt[x][0] = nxt[b][0]; } if (dep[nxt[x][0]] >= dep[x]) nxt[x][0] = 0; } inline int lowbit(int x) { return x & (-x); } inline void add(int x, int y) { for (; x <= N; x += lowbit(x)) tr[x] += y; } inline int query(int x) { int re = 0; for (; x; x -= lowbit(x)) re += tr[x]; return re; } void dfs3(int x) { for (int i = lqh[x]; i; i = lq[i][1]) { int b = lq[i][0]; tmp[i] = query(dfn[b][1]) - query(dfn[b][0] - 1); } for (int i = pth[x]; i; i = pt[i][1]) { int b = pt[i][0]; add(dfn[b][0], 1); } for (int i = egh[x]; i; i = eg[i][1]) { int b = eg[i][0]; dfs3(b); } for (int i = lqh[x]; i; i = lq[i][1]) { int b = lq[i][0]; if (tmp[i] != query(dfn[b][1]) - query(dfn[b][0] - 1)) covered[i >> 1] = 1; } } int main() { int i, j, k; N = rd(); for (i = 2; i <= N; i++) { fa[i][0] = rd(); adeg(fa[i][0], i); } dep[1] = 1; dfs(1); M = rd(); for (i = 1; i <= M; i++) { int a = rd(), b = rd(); pt[i << 1][0] = a, pt[i << 1][1] = pth[b], pth[b] = i << 1; pt[i << 1 | 1][0] = b, pt[i << 1 | 1][1] = pth[a], pth[a] = i << 1 | 1; int x = getlca(a, b); if (dep[x] < dep[nxt[a][0]] || !nxt[a][0]) nxt[a][0] = x; if (dep[x] < dep[nxt[b][0]] || !nxt[b][0]) nxt[b][0] = x; } dfs2(1); for (i = 0; i < 19; i++) { for (j = 1; j <= N; j++) { nxt[j][i + 1] = nxt[nxt[j][i]][i]; } } Q = rd(); for (i = 1; i <= Q; i++) { int a = rd(), b = rd(), lca = getlca(a, b); for (j = 19; j >= 0; j--) { if (nxt[a][j] && dep[nxt[a][j]] > dep[lca]) ans[i] += 1 << j, a = nxt[a][j]; } for (j = 19; j >= 0; j--) { if (nxt[b][j] && dep[nxt[b][j]] > dep[lca]) ans[i] += 1 << j, b = nxt[b][j]; } if ((a == lca && nxt[b][0] && dep[nxt[b][0]] <= dep[lca]) || (b == lca && nxt[a][0] && dep[nxt[a][0]] <= dep[lca])) ans[i] += 1; else if (nxt[a][0] && nxt[b][0] && dep[nxt[a][0]] <= dep[lca] && dep[nxt[b][0]] <= dep[lca]) { ans[i] += 2; lq[i << 1][0] = a, lq[i << 1][1] = lqh[b], lqh[b] = i << 1; lq[i << 1 | 1][0] = b, lq[i << 1 | 1][1] = lqh[a], lqh[a] = i << 1 | 1; } else { ans[i] = -1; } } dfs3(1); for (i = 1; i <= Q; i++) printf("%d\n", ans[i] - covered[i]); return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, m; vector<pair<int, int> > g[500]; int x[500]; double solve() { double ret = 0.0; for (int i = 0; i < (int)(n); i++) { for (auto &pr : g[i]) { int j = pr.first; int c = pr.second; double tmp = 1.0 * (x[i] + x[j]) / c; ret = max(ret, tmp); } } return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (int i = 0; i < (int)(n); i++) cin >> x[i]; for (int i = 0; i < (int)(m); i++) { int u, v, c; cin >> u >> v >> c; --u, --v; g[u].push_back(make_pair(v, c)); g[v].push_back(make_pair(u, c)); } cout << fixed << setprecision(15) << solve() << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef long double ld; typedef tuple<int, int> duo; #define TT template<typename T> #define ET(T) typename T::value_type TT inline T sq(T x){return x*x;} TT inline T In(){T x;cin>>x;return x;} TT inline int ubnd(T&v,ET(T) x){return upper_bound(begin(v),end(v),x)-begin(v);} TT inline int lbnd(T&v,ET(T) x){return lower_bound(begin(v),end(v),x)-begin(v);} TT inline void sort(T&v){sort(begin(v),end(v));} TT inline void revs(T&v){reverse(begin(v),end(v));} TT inline void uniq(T&v){sort(v);v.erase(unique(begin(v),end(v)),end(v));} TT inline void inpt(T&v,int n,function<ET(T)()>f=In<ET(T)>){for(v.reserve(n);n--;v.emplace_back(f()));} TT inline void show(T&v,string d=" ",string e="\n"){int i=0;for(auto&x:v)i++&&(cout<<d),cout<<x;cout<<e;} #undef TT #undef ET inline void fast_io(){ios::sync_with_stdio(0); cin.tie(0);} inline int in(){int x;scanf("%d",&x);return x;} inline string instr(){string x;cin>>x;return x;} inline ll pow_mod(ll a, ll k, ll m){ll r=1;for(;k>0;a=a*a%m,k>>=1)if(k&1)r=r*a%m;return r;} inline ll mod_inv(ll a, ll p){return pow_mod(a,p-2,p);} const int dx[] = {0, 0, 1, -1, 1, 1, -1, -1}; const int dy[] = {1, -1, 0, 0, 1, -1, 1, -1}; const int Mod = 1000000000; const int MAX_N = (1 << 15) + 1; bool sieve[MAX_N] = {true, true}; int main() { for (int i = 2; i * i < MAX_N; i++){ if (sieve[i]) continue; for (int j = i * i; j < MAX_N; j += i) sieve[j] = true; } int n; while (n = in()){ int cnt = 0; for (int i = 2; i <= n / 2; i++){ if (!sieve[i] && !sieve[n - i]) cnt++; } cout << cnt << endl; } return (0); } // priority_queue queue deque front top push pop map set stringstream max_element min_element insert count find get tie make_tuple
0
#include <iostream> #include <vector> #include <algorithm> #include <map> #include <set> #include <stack> using namespace std; typedef long long int ll; int main(){ cin.tie(nullptr); ios::sync_with_stdio(false); int a,b,c; cin >> a >> b >> c; int ma=max(a,max(b,c)); if(ma==a){ cout << "A" << endl; } else if(ma==b){ cout << "B" << endl; } else{ cout << "C" << endl; } }
0
#include "bits/stdc++.h" using namespace std; typedef long long ll; #define INF (1<<30) #define INFLL (1ll<<60) typedef pair<ll, int> P; typedef pair<int, P> E; #define MOD (1000000007ll) #define l_ength size #define EPS (1e-10) void add_mod(ll &a, ll b){ a += b; a %= MOD; } void mul_mod(ll &a, ll b){ a *= b; a %= MOD; } vector<P> g[2][123456]; priority_queue< P, vector<P>, greater<P> > pq; ll dist[2][123456]; int main(void){ int n,m,s[2],u,v,k,i,dummy; ll a,b,x,d,c; P tmp; fill(dist[0],dist[2],INFLL); x = (ll)(1e15); cin >> n >> m >> s[0] >> s[1]; --s[0]; --s[1]; for(i=0; i<m; ++i){ cin >> u >> v >> a >> b; --u; --v; g[0][u].push_back(P(a,v)); g[0][v].push_back(P(a,u)); g[1][u].push_back(P(b,v)); g[1][v].push_back(P(b,u)); } for(k=0; k<2; ++k){ dist[k][s[k]] = 0ll; pq.push(P(dist[k][s[k]],s[k])); while(!pq.empty()){ tmp = pq.top(); pq.pop(); d = tmp.first; u = tmp.second; for(i=(g[k][u].l_ength()-1); i>=0; --i){ v = g[k][u][i].second; c = g[k][u][i].first; if(d + c < dist[k][v]){ dist[k][v] = d + c; pq.push(P(dist[k][v],v)); } } } } for(i=0; i<n; ++i){ pq.push(P(dist[0][i]+dist[1][i],i)); } for(i=0; i<n; ++i){ tmp = pq.top(); while(tmp.second < i){ pq.pop(); tmp = pq.top(); } cout << (x-tmp.first) << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; bool dp[2][3][1001][1001]; int n, m; string s; int main() { ios_base ::sync_with_stdio(0); cin.tie(0); memset(dp, 0, sizeof(dp)); cin >> n >> m; cin >> s; int win = m; int wn = 0, ls = 0; for (int j = 0; j < n; j++) { if (s[j] == 'L') ls++; else if (s[j] == 'W') wn++; else if (s[j] == '?') break; if (abs(ls - wn) >= m) { cout << "NO" << endl; return 0; } } for (int j = 0; j < 2; j++) dp[j][0][0][0] = dp[j][1][0][0] = dp[j][2][0][0] = 1; for (int l = 1; l <= n; l++) { int cur = 0; if (s[l - 1] == '?') cur = 3; else if (s[l - 1] == 'D') cur = 2; else if (s[l - 1] == 'L') cur = 1; int lst = (l == n) ? win + 1 : win; for (int k = 0; k < lst; k++) { int j = 0; if (cur == 3 || cur == j) { dp[0][j][k][l] = (k != 0) ? (dp[0][j][k - 1][l - 1] | dp[0][1][k - 1][l - 1] | dp[0][2][k - 1][l - 1]) : (dp[1][0][1][l - 1] | dp[1][1][1][l - 1] | dp[1][2][1][l - 1]); dp[1][j][k][l] = (k < win) ? (dp[1][j][k + 1][l - 1] | dp[1][1][k + 1][l - 1] | dp[1][2][k + 1][l - 1]) : 0; } j = 1; if (cur == 3 || cur == j) { dp[1][j][k][l] = (k != 0) ? (dp[1][j][k - 1][l - 1] | dp[1][0][k - 1][l - 1] | dp[1][2][k - 1][l - 1]) : (dp[0][0][1][l - 1] | dp[0][1][1][l - 1] | dp[0][2][1][l - 1]); dp[0][j][k][l] = (k < win) ? (dp[0][j][k + 1][l - 1] | dp[0][0][k + 1][l - 1] | dp[0][2][k + 1][l - 1]) : 0; } j = 2; if (cur == 3 || cur == j) { dp[0][j][k][l] = (k != win) ? (dp[0][1][k][l - 1] | dp[0][0][k][l - 1] | dp[0][2][k][l - 1]) : 0; dp[1][j][k][l] = (k != win) ? (dp[1][1][k][l - 1] | dp[1][0][k][l - 1] | dp[1][2][k][l - 1]) : 0; } } } if ((dp[0][0][m][n] | dp[0][1][m][n] | dp[1][0][m][n] | dp[1][1][m][n]) == false) cout << "NO\n"; else { string ans = ""; int l = n, k = m, more = -1, prsnt = -1; if (dp[0][0][m][n]) more = 0, prsnt = 0; else if (dp[0][1][m][n]) more = 0, prsnt = 1; else if (dp[1][0][m][n]) more = 1, prsnt = 0; else if (dp[1][1][m][n]) more = 1, prsnt = 1; while (l != 0) { if (prsnt == 0) { ans += 'W'; if (more == 0) { if (k != 0 && dp[0][0][k - 1][l - 1]) prsnt = 0, k--; else if (k != 0 && dp[0][1][k - 1][l - 1]) prsnt = 1, k--; else if (k != 0 && dp[0][2][k - 1][l - 1]) prsnt = 2, k--; else { for (int i = 0; i < 3; i++) if (dp[1][i][1][l - 1]) { more = 1, prsnt = i, k = 1; break; } } } else { if (k < win) { for (int i = 0; i < 3; i++) if (dp[1][i][k + 1][l - 1]) { k++, prsnt = i, more = 1; break; } } else cout << "NOT possible" << endl; } } else if (prsnt == 1) { ans += 'L'; if (more == 0) { for (int i = 0; i < 3; i++) if (dp[0][i][k + 1][l - 1]) { k++, prsnt = i, more = 0; break; } } else if (k != 0) { for (int i = 0; i < 3; i++) if (dp[1][i][k - 1][l - 1]) { k--, prsnt = i, more = 1; break; } } else { for (int i = 0; i < 3; i++) if (dp[0][i][1][l - 1]) { k = 1, prsnt = i, more = 0; break; } } } else { ans += 'D'; for (int j = 0; j < 3; j++) if (dp[more][j][k][l - 1]) { prsnt = j; break; } } l--; } reverse(ans.begin(), ans.end()); cout << ans << endl; } return 0; }
5
#include <bits/stdc++.h> int main() { int n, a, b, i, j, k; scanf("%d%d%d", &n, &a, &b); for (i = 0; i * a <= n; ++i) if ((n - i * a) % b == 0) { for (j = 0; j < i; ++j) { for (k = 1; k < a; ++k) printf("%d ", j * a + k + 1); printf("%d ", j * a + 1); } for (j = 0; j < (n - i * a) / b; ++j) { for (k = 1; k < b; ++k) printf("%d ", i * a + j * b + k + 1); printf("%d ", i * a + j * b + 1); } return 0; } puts("-1"); }
3
#include <bits/stdc++.h> using namespace std; int s[100010], n, k; int main() { while (cin >> n >> k) { int cnt = 0; for (int i = 1; i <= n; i++) { cin >> s[i]; if (s[i] < 0) cnt++; } int sum = 0; if (cnt >= k) { for (int i = 1; k; i++) { s[i] *= -1; k--; } for (int i = 1; i <= n; i++) sum += s[i]; } if (cnt < k) { for (int i = 1; i <= n; i++) sum += abs(s[i]); if ((k - cnt) % 2) { int minn = 100000; for (int i = 1; i <= n; i++) { int tmp = abs(s[i]); minn = min(minn, tmp); } sum -= 2 * minn; } } cout << sum << endl; } return 0; }
2
#include<cmath> #include<iomanip> #include<iostream> #define A(a,b) S[a]*S[b]*S[n-a-b]*2 #define db long double using namespace std; typedef long long ll; ll n,k,tot; int lim,l,r,mid; db S[200005],L,R,Mid,st; int main() { cout<<setprecision(50); cin>>n>>k;lim=n/3;st=3.14159265358979/n; if (n%3==0 && n*(n-1)*(n-2)/6<k+n/3) { cout<<"1.29903810568"; return 0; } for (int i=1;i<=n;i++) S[i]=sin(i*st); for (L=0,R=1.29903810568,Mid=(L+R)/2;L+0.0000000000001<R;Mid=(L+R)/2,tot=0) { for (int i=1;i<=lim;i++) if (A(i,i)<Mid) { for (l=i,r=n-i>>1,mid=l+r+1>>1;l<r;mid=l+r+1>>1) (A(i,mid)<Mid)?l=mid:r=mid-1; tot+=(l+1-i<<1)-(i+l+l==n?(i==l?3:2):1); } if (k<=tot*n) R=Mid;else L=Mid; } cout<<L; }
0
#include<bits/stdc++.h> using namespace std; const double pi=acos(-1.0); double num[3010]; int n,l; inline int rd() { int x=0;char ch=getchar(); for (;ch<'0'||ch>'9';ch=getchar()); for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; return x; } int main() { n=rd();l=rd(); for (int i=1;i<=n;i++) num[i]=rd(); double ansx=0,ansy=0; for (int i=1;i<n;i++) for (int j=i+1;j<=n;j++) { ansx+=cos((num[i]+num[j])/l*pi)*(n-(j-i+1)); ansy+=sin((num[i]+num[j])/l*pi)*(n-(j-i+1)); ansx+=cos((num[i]+num[j]+l)/l*pi)*(j-i-1); ansy+=sin((num[i]+num[j]+l)/l*pi)*(j-i-1); } double all=(double)n*(n-1)*(n-2)/6; printf("%.15lf %.15lf\n",ansx/all,ansy/all); return 0; }
0
#include<cstdio> #include<cstdlib> #include<ctime> #include<cmath> #include<cstring> #include<cctype> #include<complex> #include<iostream> #include<sstream> #include<algorithm> #include<functional> #include<vector> #include<string> #include<stack> #include<queue> #include<map> using namespace std; const int dx[] = {1,0,-1,0},dy[] = {0,1,0,-1}; #define INF 1e+8 #define EPS 1e-8 #define rep(i,j) for(int i = 0; i < (j); i++) #define reps(i,j,k) for(int i = (j); i < (k); i++) #define MAX_v 100 typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> P; const int Lcalendar[] = {366,31,29,31,30,31,30,31,31,30,31,30,31}; const int Ncalendar[] = {365,31,28,31,30,31,30,31,31,30,31,30,31}; int GetTotalDays(int y,int m,int d){ long long int sum = 0; sum = y * 365 + ((y-1)/4) - ((y-1)/100) + ((y-1)/400); if(y % 4 == 0 &&(y % 400 == 0 || y % 100 != 0)){ for(int i = 1; i <= m-1; i++)sum += Lcalendar[i]; } else{ for(int i = 1; i <= m-1; i++) sum += Ncalendar[i]; } sum += d; return sum; } int main(){ int y1,m1,d1,y2,m2,d2; while(true){ scanf("%d%d%d%d%d%d",&y1,&m1,&d1,&y2,&m2,&d2); if(y1 == m1 && d1 == y2 && m2 == d2 && m2 == -1)break; printf("%d\n",GetTotalDays(y2,m2,d2)-GetTotalDays(y1,m1,d1)); } return 0; }
0
#include <bits/stdc++.h> using namespace std; ifstream in("input.txt"); ofstream out("output.txt"); int n, m, k; int ans[1000001]; int main() { cin >> n >> m >> k; int f = 1 + n + m; int im = 0; for (int i = 0; i < k; i++) { ans[im++] = 1; } bool ff = false; if (k == 1) { int l = 1000000; for (int i = 1; i < l; i++) { ans[im++] = i * n - (i - 1); if (ans[im - 1] + n > f) break; } } else { int l = 1000000; for (int i = 1; i < l; i++) { int last = im; ans[im] = ans[im - 1] + n - 1; im++; if (ans[im - 2] + n >= f) break; for (int j = 1; j < k; j++) ans[im++] = ans[last] + 1; if (ans[im - 1] + n > f) break; } } cout << im << "\n"; for (int i = 0; i < im; i++) cout << ans[i] << " "; in.close(); out.close(); return 0; }
3
#include <bits/stdc++.h> using namespace std; int n; int a[100050], num[100050], oc[100050]; inline void read(int &x) { x = 0; register char ch = getchar(); while (ch < '0' || ch > '9') ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); } int main() { read(n); for (register int i = 1; i <= n; i++) { read(a[i]); num[a[i]]++; } int tot = 0; for (register int i = 1; i <= n; i++) { if (num[i] & 1) tot++; } if ((n % 2 == 0 && tot) || (n % 2 && tot > 1)) { cout << 0 << endl; return 0; } int len = 0; for (register int i = 1; i <= (n >> 1); i++) { if (a[i] != a[n - i + 1]) { len = i; break; } num[a[i]] -= 2; } if (len == 0) { cout << (long long)n * (n + 1) / 2 << endl; return 0; } int le = 0, ri = 0; for (register int i = len; i <= n; i++) { oc[a[i]]++; if (oc[a[i]] * 2 > num[a[i]]) { if (a[i] != a[n - i + 1]) break; if (i < n - i + 1) break; if (num[a[i]] % 2 == 0 && i == n - i + 1) break; } le++; } memset(oc, 0, sizeof(oc)); for (register int i = n - len + 1; i >= 1; i--) { oc[a[i]]++; if (oc[a[i]] * 2 > num[a[i]]) { if (a[i] != a[n - i + 1]) break; if (i > n - i + 1) break; if (num[a[i]] % 2 == 0 && i == n - i + 1) break; } ri++; } cout << (long long)len * (len + le + ri) << endl; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; vector<vector<int> > DP(n + 1, vector<int>(k + 1, 0)); for (int i = 1; i <= n; i++) DP[i][1] = 1; for (int j = 1; j < k; j++) for (int i = 1; i <= n; i++) for (int t = 1; t * i <= n; t++) DP[i * t][j + 1] = (DP[i * t][j + 1] + DP[i][j]) % 1000000007; int ans = 0; for (int i = 1; i <= n; i++) ans = (ans + DP[i][k]) % 1000000007; cout << ans << endl; }
2
#include <bits/stdc++.h> using namespace std; int i, j, k; int main() { int t; while (cin >> t) { for (i = 0; i < t; i++) { int a; cin >> a; int n1 = 360 / (180 - a); double n2 = 360 / (double)(180 - a); if (n2 - n1 > 0) { cout << "NO" << endl; } else cout << "YES" << endl; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = (int)2e6 + 10; const int inf = (int)1e9; const int P[] = {(int)1e9 + 7, (int)1e9 + 9}; const int Q[] = {17, 239}; int stp[2][maxn]; int h[2][maxn]; int h1[2][maxn]; int h2[2][maxn]; const int sh = 1 << 21; int rm[2 * sh + 10]; void upd(int first, int val) { first += sh; rm[first] = val; while (first > 1) { first /= 2; rm[first] = min(rm[first * 2], rm[first * 2 + 1]); } } int get_min(int l, int r) { l += sh; r += sh; int ans = inf; while (l <= r) { ans = min(rm[l], ans); ans = min(rm[r], ans); l = (l + 1) / 2; r = (r - 1) / 2; } return ans; } char s[maxn]; int n; int sum[maxn]; void print(pair<int, pair<int, int> > ans) { for (int j = 0; j < ans.first; j++) { printf("("); } for (int j = 0; j < n; j++) { printf("%c", s[ans.second.first + j]); } for (int j = 0; j < ans.second.second; j++) { printf(")"); } printf("\n"); } int get_c(const pair<int, pair<int, int> >& a, int l) { if (l < a.first) return -1; if (l < a.first + n) { return s[a.second.first + l - a.first] == '(' ? -1 : 1; } return 1; } pair<int, int> get_hash(const pair<int, pair<int, int> >& a, int l) { if (l <= a.first) { return make_pair(h1[0][l], h1[1][l]); } if (l <= a.first + n) { int l2 = l - a.first; pair<int, int> H1 = make_pair(h1[0][a.first], h1[1][a.first]); pair<int, int> H2 = make_pair(((h[0][a.second.first] - (long long)h[0][a.second.first + l2] * stp[0][l2]) % P[0] + P[0]) % P[0], ((h[1][a.second.first] - (long long)h[1][a.second.first + l2] * stp[1][l2]) % P[1] + P[1]) % P[1]); return make_pair( (H1.first + (long long)H2.first * stp[0][a.first]) % P[0], (H1.second + (long long)H2.second * stp[1][a.first]) % P[1]); } { int l2 = l - a.first - n; pair<int, int> H1 = get_hash(a, a.first + n); pair<int, int> H2 = make_pair(h2[0][l2], h2[1][l2]); ; return make_pair( (H1.first + (long long)H2.first * stp[0][a.first + n]) % P[0], (H1.second + (long long)H2.second * stp[1][a.first + n]) % P[1]); } } bool super_less(const pair<int, pair<int, int> >& a, const pair<int, pair<int, int> >& b) { if (a.first + a.second.second != b.first + b.second.second) { return a.first + a.second.second < b.first + b.second.second; } int l = 0; int r = a.first + a.second.second + n; while (l < r) { int m = (l + r + 1) / 2; if (get_hash(a, m) == get_hash(b, m)) { l = m; } else { r = m - 1; } } if (r == a.first + a.second.second + n) { return false; } return get_c(a, l) < get_c(b, l); } int main() { scanf("%s", s); n = strlen(s); for (int i = 0; i < n; i++) { s[i + n] = s[i]; } stp[0][0] = 1; stp[1][0] = 1; for (int j = 0; j < 2; j++) { for (int i = 1; i <= 2 * n; i++) { stp[j][i] = ((long long)stp[j][i - 1] * Q[j]) % P[j]; } } for (int j = 0; j < 2; j++) { h[j][2 * n] = 0; for (int i = 2 * n - 1; i >= 0; i--) { h[j][i] = ((long long)h[j][i + 1] * Q[j] + (s[i] == '(' ? 1 : 2)) % P[j]; } } for (int j = 0; j < 2; j++) { h1[j][0] = 0; h2[j][0] = 0; for (int i = 1; i <= 2 * n; i++) { h1[j][i] = ((long long)h1[j][i - 1] * Q[j] + 1) % P[j]; h2[j][i] = ((long long)h2[j][i - 1] * Q[j] + 2) % P[j]; } } sum[0] = 0; for (int i = 0; i < 2 * n; i++) { if (s[i] == '(') { sum[i + 1] = sum[i] + 1; } else { sum[i + 1] = sum[i] - 1; } } for (int i = 0; i <= 2 * n; i++) { upd(i, sum[i]); } pair<int, pair<int, int> > ans = make_pair(inf, make_pair(0, inf)); for (int i = 0; i < n; i++) { int mn = min(0, get_min(i, i + n) - sum[i]); int add_beg = abs(mn); int add_end = sum[i + n] + add_beg - sum[i]; pair<int, pair<int, int> > res = make_pair(add_beg, make_pair(i, add_end)); if (super_less(res, ans)) { ans = res; } } print(ans); return 0; }
6
#include <bits/stdc++.h> int main() { int a, b, t; scanf("%d%d", &a, &b); t = 0; while (a <= b) { a = a * 3; b = b * 2; t = t + 1; } printf("%d", t); return 0; }
1
#include <algorithm> #include <cassert> #include <iostream> #include <vector> #define rep(i, n) for (int i = 0; i < (int)(n); i++) using namespace std; struct Lonoid { int64_t x; Lonoid() {} Lonoid(int64_t x) : x(x) {} static Lonoid ident() { return Lonoid(0); } static Lonoid multiply(Lonoid f, Lonoid g) { return Lonoid(f.x + g.x); } bool operator==(const Lonoid &rhs) const { return x == rhs.x; } }; template <typename L> struct SegmentTree { int n; L e; vector<L> laz; SegmentTree(int n0) { n = 1; e = L::ident(); while (n < n0) { n *= 2; } laz.resize(n * 2 - 1, e); } void _update(int i, L f) { laz[i] = L::multiply(f, laz[i]); } void update(int ql, int qr, L f) { update(ql, qr, f, 0, 0, n); } void update(int ql, int qr, L f, int i, int il, int ir) { if (qr <= il or ir <= ql) return; if (ql <= il and ir <= qr) { _update(i, f); } else { auto m = (il + ir) / 2, lch = i * 2 + 1, rch = i * 2 + 2; _update(lch, laz[i]); _update(rch, laz[i]); laz[i] = e; update(ql, qr, f, lch, il, m); update(ql, qr, f, rch, m, ir); } } L composite(int i) { auto f = L::ident(); for (int k = i + n - 1; k > 0; k = (k - 1) / 2) { f = L::multiply(laz[k], f); } return L::multiply(laz[0], f); } }; int main() { int n, q; cin >> n >> q; SegmentTree<Lonoid> seg(n); while (q--) { int o; cin >> o; if (o == 0) { int s, t; int64_t x; cin >> s >> t >> x; seg.update(s - 1, t, Lonoid(x)); } else { int i; cin >> i; auto ans = seg.composite(i - 1).x; cout << ans << endl; } } return 0; }
0
#include<bits/stdc++.h> using namespace std; int main() { int N, M; cin >> N >> M; int X[M]; for (int i = 0; i < M; i++) { cin >> X[i]; } sort(X, X + M); int def[M - 1]; for (int i = 1; i < M; i++) { def[i-1] = X[i] - X[i - 1]; } sort(def, def + M - 1); long long ans = 0; for (int i = 0; i < M - N; i++) { ans += def[i]; } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> template <typename T> void read(T& num) { num = 0; int w = 1; char ch = getchar(); if (ch == '-') w = -1; while (ch < '0' || ch > '9') { ch = getchar(); if (ch == '-') w = -1; } while ((ch >= '0' && ch <= '9')) num = num * 10 + ch - '0', ch = getchar(); num = num * w; } template <typename T, typename... Args> void read(T& num, Args&... args) { read(num), read(args...); } const int N = 300010; const int MOD = 19260817; int n; int a[N]; int powmod[N]; struct Segment_Tree { struct Seg { int l, r; int sum, rev; Seg() {} Seg(int _l, int _r, int _sum, int _rev) : l(_l), r(_r), sum(_sum), rev(_rev) {} Seg operator+(const Seg& b) const { if (l == -1) return b; if (b.l == -1) return Seg(l, r, sum, rev); return Seg( l, b.r, ((((((long long)(sum) * (powmod[b.r - b.l + 1]) % (MOD))) + (b.sum)) >= MOD) ? ((((long long)(sum) * (powmod[b.r - b.l + 1]) % (MOD))) + (b.sum) - (MOD)) : ((((long long)(sum) * (powmod[b.r - b.l + 1]) % (MOD))) + (b.sum))), ((((((long long)(b.rev) * (powmod[r - l + 1]) % (MOD))) + (rev)) >= MOD) ? ((((long long)(b.rev) * (powmod[r - l + 1]) % (MOD))) + (rev) - (MOD)) : ((((long long)(b.rev) * (powmod[r - l + 1]) % (MOD))) + (rev)))); } }; Seg t[N << 3]; void push_up(int rt) { t[rt] = t[(rt << 1)] + t[(rt << 1 | 1)]; } void build(int rt, int l, int r) { t[rt] = Seg(l, r, 0, 0); if (l == r) return; int mid = (l + r) >> 1; build((rt << 1), l, mid); build((rt << 1 | 1), mid + 1, r); push_up(rt); } void modify(int rt, int pos, int val) { if (t[rt].l == t[rt].r) { t[rt].sum = t[rt].rev = val; return; } if (pos <= t[(rt << 1)].r) modify((rt << 1), pos, val); else modify((rt << 1 | 1), pos, val); push_up(rt); } Seg query(int rt, int l, int r) { if (t[rt].l > r || t[rt].r < l) return Seg(-1, -1, -1, -1); if (t[rt].l >= l && t[rt].r <= r) return t[rt]; return query((rt << 1), l, r) + query((rt << 1 | 1), l, r); } }; Segment_Tree t; int main() { read(n); for (int i = 1; i <= n; ++i) read(a[i]); powmod[0] = 1; for (int i = 1; i <= n; ++i) powmod[i] = (powmod[i - 1] << 1) % MOD; t.build(1, 1, n); bool flag = false; for (int i = 1; i <= n; ++i) { t.modify(1, a[i], 1); if ((a[i] << 1) - 1 <= n) { if (a[i] > 1 && t.query(1, 1, a[i] - 1).sum != t.query(1, a[i] + 1, (a[i] << 1) - 1).rev) { flag = true; break; } } else { if (a[i] < n && t.query(1, a[i] + 1, n).rev != t.query(1, (a[i] << 1) - n, a[i] - 1).sum) { flag = true; break; } } } if (flag) puts("YES"); else puts("NO"); return 0; }
6
#include <bits/stdc++.h> using namespace std; const long long N = 1000000, mod = 998244353; long long n, frac[N + 10], ifrac[N + 10]; long long l1[N + 10], l2[N + 10], r1[N + 10], r2[N + 10]; char str[N + 10]; long long qpow(long long a, long long b, long long p) { long long ret = 1; for (; b; b >>= 1) { if (b % 2 == 1) ret = ret * a % p; a = a * a % p; } return ret; } void pre() { frac[0] = ifrac[0] = 1; for (long long i = 1; i <= n; i++) { frac[i] = frac[i - 1] * i % mod; ifrac[i] = qpow(frac[i], mod - 2, mod); } } long long C(long long n, long long m) { if (n < m || n < 0 || m < 0) return 0; return frac[n] * ifrac[m] % mod * ifrac[n - m] % mod; } signed main() { scanf("%s", str + 1); n = strlen(str + 1); pre(); for (long long i = 1; i <= n; i++) { if (str[i] == '?') l1[i]++, r1[i]++; if (str[i] == '(') l2[i]++; if (str[i] == ')') r2[i]++; } for (long long i = 1; i <= n; i++) l1[i] += l1[i - 1], l2[i] += l2[i - 1]; for (long long i = n; i >= 1; i--) r1[i] += r1[i + 1], r2[i] += r2[i + 1]; long long ans = 0; for (long long i = 1; i < n; i++) { ans = (ans + l2[i] * C(l1[i] + r1[i + 1], r1[i + 1] + r2[i + 1] - l2[i]) % mod) % mod; ans = (ans + l1[i] * C(l1[i] + r1[i + 1] - 1, r1[i + 1] + r2[i + 1] - l2[i] - 1) % mod) % mod; } printf("%lld\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; int Trie[3000000][2], children[3000000][2]; char sign; bitset<31> num; int new_node = 1; void Add(bitset<31> num) { int node = 0; for (int i = 30; i >= 0; --i) { if (children[node][num[i]] == 0) children[node][num[i]] = new_node++; ++Trie[node][num[i]]; node = children[node][num[i]]; } } void Delet(bitset<31> num) { int node = 0; for (int i = 30; i >= 0; --i) { --Trie[node][num[i]]; if (Trie[node][num[i]] == 0) { children[node][num[i]] = 0; break; } node = children[node][num[i]]; } } int main() { Add(num); int n, a; cin >> n; while (n-- > 0) { cin >> sign >> a; num = a; if (sign == '?') { bitset<31> res; int node = 0; for (int i = 30; i >= 0; --i) { if (num[i] == 1) { if (children[node][0] != 0) { res[i] = 1; node = children[node][0]; } else node = children[node][1]; } else { if (children[node][1] != 0) { res[i] = 1; node = children[node][1]; } else node = children[node][0]; } } cout << res.to_ullong() << '\n'; } else { if (sign == '+') Add(num); else Delet(num); } } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, k; vector<pair<int, int>> ri[N]; int pref[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k; for (int i = 0; i < n; i++) { int l, r; cin >> l >> r; ri[l].push_back({r, i}); pref[l]++; pref[r + 1]--; } int sum = 0; vector<int> ans; priority_queue<pair<int, int>> pq; for (int i = 1; i <= N - 5; i++) { sum += pref[i]; for (auto it : ri[i]) { pq.push(it); } while (sum > k) { sum--; pref[pq.top().first + 1]++; ans.push_back(pq.top().second + 1); pq.pop(); } } cout << ans.size() << '\n'; sort(ans.begin(), ans.end()); for (auto it : ans) { cout << it << ' '; } cout << '\n'; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; cin >> n >> m; long long x, y; cin >> x >> y; int q; cin >> q; long long count = 0; for (int i = 0; i < q; i++) { long long a, b; cin >> a >> b; long long k, l; if (a > 0 && b > 0) { k = (n - x) / a; l = (m - y) / b; count += min(k, l); x += min(k, l) * a; y += min(k, l) * b; } else if (a < 0 && b < 0) { k = (x - 1) / abs(a); l = (y - 1) / abs(b); count += min(k, l); x += min(k, l) * a; y += min(k, l) * b; } else if (a > 0 && b < 0) { k = (n - x) / a; l = (y - 1) / abs(b); count += min(k, l); x += min(k, l) * a; y += min(k, l) * b; } else if (a < 0 && b > 0) { k = (x - 1) / abs(a); l = (m - y) / b; count += min(k, l); x += min(k, l) * a; y += min(k, l) * b; } else if (a == 0 && b != 0) { if (b < 0) { count += (y - 1) / abs(b); y += ((y - 1) / abs(b)) * b; } else { count += (m - y) / b; y += ((m - y) / b) * b; } } else if (a != 0 && b == 0) { if (a < 0) { count += (x - 1) / abs(a); x += ((x - 1) / abs(a)) * a; } else { count += (n - x) / a; x += ((n - x) / a) * a; } } } cout << count << endl; }
2
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ vector <int> so,sum; int a[110001]={0},from,n,p,i,j; for(i=2;i<=110000;i++){ if(a[i]==0){ so.push_back(i); for(j=i;j<=110000;j+=i) a[j]=1; } } //for(i=0;i<20;i++) cout<<so[i]<<endl; while(cin>>n>>p,n!=-1){ from=0; for(i=0;n>=so[i];i++) from++; //cout<<from<<endl; sum.clear(); for(i=from;i<from+p;i++) for(j=i;j<from+p;j++) sum.push_back(so[i]+so[j]); sort(sum.begin(),sum.end()); cout<<sum[p-1]<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; long long n, k, d1, d2; int solve(long long d1, long long d2) { long long x, y, z; y = (k - d1 + d2) / 3; if ((k - d1 + d2) % 3 != 0) return 0; x = y + d1; z = y - d2; if (x >= 0 && x <= n / 3 && y >= 0 && y <= n / 3 && z >= 0 && z <= n / 3) return 1; return 0; } int main() { int t; cin >> t; while (t--) { cin >> n >> k >> d1 >> d2; if (n % 3 != 0) cout << "no" << endl; else if (solve(d1, d2) || solve(-d1, d2) || solve(d1, -d2) || solve(-d1, -d2)) cout << "yes" << endl; else cout << "no" << endl; } return 0; }
3
#include <bits/stdc++.h> int main() { int n, t; scanf("%d %d", &n, &t); for (int i = 1, k; i <= n; ++i) { scanf("%d", &k); t -= (86400 - k); if (t <= 0) { printf("%d", i); return 0; } } return 0; }
1
#include <bits/stdc++.h> const long long INF = (1e9 + 12312), MOD = 1e9 + 7; const long double EPS = 0.0000000001; using namespace std; bool ask(long long first, long long second) { cout << first << " " << second << endl; string s; cin >> s; if (s == "Bad") { exit(1); } return s == "Yes"; } signed main() { ios_base::sync_with_stdio(false); srand(time(0)); long long n, k; cin >> n >> k; long long now = 0; long long first = 1, second = n; long long lastl = 1, lastr = n; for (int i = 0; i < 4500; i++) { if (now >= 7) { first = max(lastl - 8 * k, 1LL); second = min(lastr + 8 * k, n); lastl = -1; now = 0; } long long askl = first, askr = second; if (second - first == 0) { if (ask(first, first)) { return 0; } first = 1, second = n; continue; } if (second - first + 1 <= 43) { if (lastl == -1) { lastr = second; lastl = first; } now++; long long askl = first + rand() % (second - first + 1); if (ask(askl, askl)) { return 0; } continue; } else { now = 0; lastl = -1; lastr = -1; if (rand() % 2) { askl = first + (second - first + 1) / 2; } else { askr = second - (second - first + 1) / 2; } } if (ask(askl, askr)) { first = max(askl - k, 1LL); second = min(askr + k, n); } else { if (askl != first) { first -= k; first = max(first, 1LL); second = min(askl + k, n); } else { first = max(askr - k, 1LL); second += k; second = min(second, n); } } } return 0; }
4
#include <bits/stdc++.h> using namespace std; long long Seive[200006]; long long searchgreater(vector<long long> &v, long long target) { long long l = 0, u = (long long)v.size() - 1; long long m; while (l <= u) { m = (l + u) / 2; if (v[m] < target) { l = m + 1; } else { if (v[m] > target) { u = m - 1; } else { u = m - 1; } } } if (v[l] >= target) return l; else return v.size(); } long long searchlower(vector<long long> &array, long long start_idx, long long end_idx, long long search_val) { if (start_idx == end_idx) return array[start_idx] <= search_val ? start_idx : -1; long long mid_idx = start_idx + (end_idx - start_idx) / 2; if (search_val < array[mid_idx]) return searchlower(array, start_idx, mid_idx, search_val); long long ret = searchlower(array, mid_idx + 1, end_idx, search_val); return ret == -1 ? mid_idx : ret; } 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 binpow(long long a, long long b, long long m) { if (b == 0) { return 1; } if (b == 1) { return a; } if (b % 2 == 0) { long long temp = binpow(a, b / 2, m); return (temp % m * temp % m) % m; } else { long long temp = binpow(a, (b - 1) / 2, m); return (temp % m * temp % m * a % m) % m; } } long long power(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long modInverse(long long n, long long p) { return power(n, p - 2, p); } long long fact[200006]; void factorial(long long p) { fact[0] = 1; fact[1] = 1; for (long long i = 2; i < 200006; i++) { fact[i] = fact[i - 1] * i; fact[i] = fact[i] % p; } } long long nCrModPFermat(long long n, long long r, long long p) { if (n < r) { return 0; } if (r == n) { return 1; } return (fact[n] * modInverse(fact[r], p) % p * modInverse(fact[n - r], p) % p) % p; } void PrimeSeive() { long long N = 200006; long long i; for (i = 2; i < N; i++) { Seive[i] = 1; } Seive[0] = 0; Seive[1] = 0; long long j; for (i = 2; i * i < N; i++) { if (Seive[i] == 1) { for (j = i * i; j < N; j += i) Seive[j] = 0; } } } bool sortbysec(const pair<long long, long long> &a, const pair<long long, long long> &b) { return (a.second < b.second); } long long mod1(long long a, long long b) { long long c = a % b; if (c < 0) { return c + b; } else return c; } void solve() { long long a, b; cin >> a >> b; if (b == 0) { cout << a << "\n"; return; } if (b % 4 == 0) { cout << a << "\n"; return; } long long x = b % 4; long long ld = ((b) / 4) * 4; long long sum = 0; if ((b % 4) > 0) { sum -= (ld + 1); } if ((b % 4) > 1) { sum += (ld + 2); } if ((b % 4) > 2) { sum += (ld + 3); } if (abs(a) % 2 == 0) { cout << a + sum << "\n"; return; } cout << a - sum << "\n"; return; } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); PrimeSeive(); long long t; cin >> t; while (t--) { solve(); } }
2
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; long long dp[N]; long long ans[N]; vector<long long int> a(N, 0); int solve() { long long n; cin >> n; for (long long int i = 1; i < n + 1; i++) cin >> a[i]; ans[0] = dp[0] = 0; for (long long int i = 1; i < n + 1; i++) { long long tc = (long long)1e18; if (i - 1 >= 0) tc = min(dp[i - 1] + 20, tc); long long idx1 = (lower_bound(a.begin() + 1, a.begin() + i + 1, (a[i] - 90 + 1)) - a.begin()); idx1--; if (idx1 >= 0) tc = min(dp[idx1] + 50, tc); long long idx2 = (lower_bound(a.begin() + 1, a.begin() + i + 1, (a[i] - 1440 + 1)) - a.begin()); idx2--; if (idx2 >= 0) tc = min(tc, dp[idx2] + 120); ans[i] = tc - dp[i - 1]; dp[i] = tc; } for (long long int i = 1; i < n + 1; i++) cout << ans[i] << "\n"; return 0; } int main() { auto start = chrono::high_resolution_clock::now(); ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long test_cases = 1; while (test_cases--) solve(); auto stop = chrono::high_resolution_clock::now(); auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start); }
2
#include<iostream> #include<string> #define loop(i,a,b) for(int i=a;i<b;i++) #define rep(i,a) loop(i,0,a) using namespace std; int main(){ string s,t; cin>>s>>t; bool tmp=false; for(int i=s.size()-t.size();i>=0;i--){ bool check=true; rep(j,t.size()){ if(s[i+j]=='?' or s[i+j]==t[j])continue; check=false; break; } if(check && !tmp){ rep(j,t.size())s[i+j]=t[j]; tmp=true; } } rep(i,s.size())if(s[i]=='?')s[i]='a'; cout<<((tmp)?s:"UNRESTORABLE")<<endl; return 0; }
0