solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, c; cin >> n >> a >> b >> c; int ans = 0; for (int i = 0; i <= min(a, 2 * n); i += 2) { for (int j = 0; j <= min(b, n - i / 2); ++j) { int k = min(c, (n - i / 2 - j) / 2); if (i / 2 + j + 2 * k == n) ans++; } } cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; string s; cin >> s; if (n == 1) { if (k > 0) { cout << 0 << endl; } else { cout << s << endl; } return 0; } if (s[0] != '1' && k > 0) { s[0] = '1'; k--; } for (int i = 1; i < s.size() && k > 0; i++) { if (s[i] != '0') { s[i] = '0'; k--; } } cout << s << endl; }
2
#include <bits/stdc++.h> using namespace std; const double PI = acos(-1); const long long infinity = (1000000000 + 11); void solve() {} void Task() { int test; cin >> test; for (int i = 1; i <= test; i++) { solve(); } } bool isnum(string str) { if ((str[0] == '0' && str.size() != 1) || str.size() == 0) { return false; } for (int i = 0; i < str.size(); i++) { if (!(str[i] >= '0' && str[i] <= '9')) { return false; } } return true; } int main() { string s; cin >> s; vector<string> str; int x = 0; str.emplace_back(); for (int i = 0; i < s.size(); i++) { if (s[i] == ',' || s[i] == ';') { x++; str.emplace_back(); continue; } str[x].push_back(s[i]); } vector<string> a, b; for (int i = 0; i < str.size(); i++) { if (isnum(str[i])) { a.push_back(str[i]); } else { b.push_back(str[i]); } } if (a.size() == 0) { cout << "-\n"; } else { cout << "\""; for (int i = 0; i < a.size(); i++) { if (i != 0) { cout << ","; } cout << a[i]; } cout << "\"" << "\n"; } if (b.size() == 0) { cout << "-\n"; } else { cout << "\""; for (int i = 0; i < b.size(); i++) { if (i != 0) { cout << ","; } cout << b[i]; } cout << "\"" << "\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, k; vector<int> a; vector<int> b; void solve() { int ans = 0; for (int i = 0; i <= k; i++) { int left = b[i], right = b[i + 1]; if (a[left] > a[right]) { cout << -1; return; } vector<int> lis(0); for (int j = left + 1; j < right; j++) if (a[j] >= a[left] && a[j] <= a[right]) { auto pos = upper_bound(lis.begin(), lis.end(), a[j]); if (pos == lis.end()) lis.push_back(a[j]); else { *pos = a[j]; } } ans += right - left - 1 - lis.size(); } cout << ans; return; } int main() { cin >> n >> k; a.resize(n + 2); b.resize(k + 2); for (int i = 1; i <= n; i++) { cin >> a[i]; a[i] -= i; } a[0] = -2e9; a[n + 1] = 2e9; for (int i = 1; i <= k; i++) cin >> b[i]; b[0] = 0; b[k + 1] = n + 1; ; solve(); ; return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int n, q, a[N], tag[N << 2], mn[N << 2], mx[N << 2]; long long sum[N << 2]; void up(int x) { sum[x] = sum[(x << 1)] + sum[(x << 1 | 1)]; mn[x] = min(mn[(x << 1)], mn[(x << 1 | 1)]); mx[x] = max(mx[(x << 1)], mx[(x << 1 | 1)]); } void cov(int x, int l, int r, int v) { tag[x] = v; sum[x] = 1ll * (r - l + 1) * v; mn[x] = mx[x] = v; } void down(int x, int l, int r) { if (tag[x]) { int mid = (l + r) >> 1; cov((x << 1), l, mid, tag[x]); cov((x << 1 | 1), mid + 1, r, tag[x]); tag[x] = 0; } } void build(int x = 1, int l = 1, int r = n) { if (l == r) { sum[x] = mn[x] = mx[x] = a[l]; return; } int mid = (l + r) >> 1; build((x << 1), l, mid); build((x << 1 | 1), mid + 1, r); up(x); } void chkmax(int L, int R, int v, int x = 1, int l = 1, int r = n) { if (mn[x] >= v) return; if (L <= l && r <= R && mx[x] <= v) return cov(x, l, r, v); down(x, l, r); int mid = (l + r) >> 1; if (L <= mid) chkmax(L, R, v, (x << 1), l, mid); if (R > mid) chkmax(L, R, v, (x << 1 | 1), mid + 1, r); up(x); } long long getsum(int L, int R, int x = 1, int l = 1, int r = n) { if (L > R) return 0; if (L <= l && r <= R) return sum[x]; down(x, l, r); int mid = (l + r) >> 1; long long ret = 0; if (L <= mid) ret += getsum(L, R, (x << 1), l, mid); if (R > mid) ret += getsum(L, R, (x << 1 | 1), mid + 1, r); return ret; } int binary1(int v, int x = 1, int l = 1, int r = n) { if (l == r) return sum[x] <= v ? l : n + 1; down(x, l, r); int mid = (l + r) >> 1; if (mn[(x << 1)] <= v) return binary1(v, (x << 1), l, mid); return binary1(v, (x << 1 | 1), mid + 1, r); } int binary2(long long v, int x = 1, int l = 1, int r = n) { if (l == r) return sum[x] >= v ? l : n + 1; down(x, l, r); int mid = (l + r) >> 1; if (sum[(x << 1)] >= v) return binary2(v, (x << 1), l, mid); return binary2(v - sum[(x << 1)], (x << 1 | 1), mid + 1, r); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> q; for (int i = 1; i <= n; i++) cin >> a[i]; build(); while (q--) { int op, x, y; cin >> op >> x >> y; if (op == 1) chkmax(1, x, y); else { int ans = 0; while (x <= n) { long long pre = getsum(1, x - 1); int t = binary2(pre + y + 1); ans += t - x; y -= getsum(x, t - 1); if (t > n) break; x = binary1(y); } cout << ans << '\n'; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main(){ int A[3]; for(int i=0;i<3;i++) cin>>A[i]; sort(A,A+3); cout<<((A[0]==A[1])?A[2]:A[0])<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; vector<int> v[N]; int n, m, p[N], pre[N]; int f(int x) { return pre[x] == x ? x : pre[x] = f(pre[x]); } signed main() { scanf("%d %d", &n, &m); for (register int i = (1); i <= (m); ++i) pre[i] = i; int ans = n - 1; for (register int i = (1); i <= (n); ++i) { int x; scanf("%d", &x); v[x].push_back(i); p[i] = x; ans -= (i > 1 && p[i] == p[i - 1]); } printf("%d\n", ans); for (register int i = (2); i <= (m); ++i) { int x, y, z; scanf("%d %d", &x, &y); x = f(x); y = f(y); if (v[x].size() > v[y].size()) swap(x, y); for (register int j = (0); j <= ((int)(v[x].size() - 1)); ++j) { z = v[x][j]; ans -= (z > 1 && p[z - 1] == y); ans -= (z < n && p[z + 1] == y); } for (register int j = (0); j <= ((int)(v[x].size() - 1)); ++j) { z = v[x][j]; v[y].push_back(z); p[z] = y; } pre[x] = y; printf("%d\n", ans); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int a[] = {1, 2, 4, 3}; int b[] = {1, 3, 4, 2}; int c[] = {1, 4, 1, 4}; int d[] = {1, 1, 1, 1}; int main() { string s; cin >> s; int n = s.length(); int rem = 0; for (int i = 0; i < n; i++) { rem = (rem * 10 + s[i] - '0') % 4; } int ans = a[rem] + b[rem] + c[rem] + d[rem]; ans %= 5; cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 100000; const int inf = (int)1e9 + 1; const long long big = (long long)1e18 + 1; const int P = 239; const int MOD = (int)1e9 + 7; const int MOD1 = (int)1e9 + 9; const double eps = 1e-9; const double pi = atan2(0, -1); const int ABC = 26; string a[N]; int cnt[N][26]; bool used[26]; set<int> g[26], gr[26]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(20); int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; for (int j = 0; j < (int)a[i].size(); j++) { cnt[i][a[i][j] - 'a']++; used[a[i][j] - 'a'] = true; if (cnt[i][a[i][j] - 'a'] == 2) { cout << "NO\n"; return 0; } } } for (int i = 0; i < n; i++) for (int j = 0; j < (int)a[i].size() - 1; j++) { g[a[i][j] - 'a'].insert(a[i][j + 1] - 'a'); gr[a[i][j + 1] - 'a'].insert(a[i][j] - 'a'); } for (int i = 0; i < 26; i++) if ((int)g[i].size() > 1) { cout << "NO\n"; return 0; } vector<string> res; for (int i = 0; i < 26; i++) if (used[i] && (int)gr[i].size() == 0) { string t; int cur = i; while (true) { used[cur] = false; t += (char)('a' + cur); if ((int)g[cur].size() == 0) break; else { if (used[*g[cur].begin()]) cur = *g[cur].begin(); else { cout << "NO\n"; return 0; } } } res.push_back(t); } for (int i = 0; i < 26; i++) if (used[i]) { cout << "NO\n"; return 0; } sort(res.begin(), res.end()); for (auto it : res) cout << it; cout << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int N; if (!(cin >> N)) return 1; vector<char> used(1001, 0); vector<int> V(N, 0); vector<vector<int> > E(N, vector<int>(N, 0)); for (int i = 0; i < int(3); ++i) V[i] = i; for (int i = 0; i < int(3); ++i) for (int j = 0; j < int(i); ++j) used[E[i][j] = E[j][i] = V[i] + V[j]] = true; for (int i = 3; i < int(N); ++i) { int val = V[i - 1] + 1; for (;;) { bool valid = true; for (int j = 0; j < int(i); ++j) { if (used[V[j] + val]) { valid = false; break; } } if (valid) break; assert(val < 1000); ++val; } V[i] = val; for (int j = 0; j < int(i); ++j) used[E[i][j] = E[j][i] = V[i] + V[j]] = true; } for (int i = 0; i < int(N); ++i) { for (int j = 0; j < int(N); ++j) { if (j > 0) cout << ' '; cout << E[i][j]; } cout << '\n'; } }
4
#include <bits/stdc++.h> using namespace std; int main() { int a, c; cin >> a >> c; int ans = 0; int b[a][c]; vector<int> m1, m2; for (int i = 0; i < a; i++) { int k = 0; for (int t = 0; t < c; t++) { int temp; cin >> temp; b[i][t] = temp; if (temp == 1) { k++; ans++; } if (k == c) { m1.push_back(i); } } } for (int i = 0; i < c; i++) { int k = 0; for (int t = 0; t < a; t++) { if (b[t][i] == 1) { k++; } if (k == a) { m2.push_back(i); } } } if (m1.empty() == true || m2.empty() == true) { if (ans != 0) { cout << "NO"; exit(0); } else { cout << "YES" << endl; for (int i = 0; i < a; i++) { for (int t = 0; t < c; t++) { cout << b[i][t] << " "; } cout << endl; } } } for (int i = 0; i < a; i++) { for (int t = 0; t < c; t++) { if (b[i][t] == 1 && find(m1.begin(), m1.end(), i) == m1.end() && find(m2.begin(), m2.end(), t) == m2.end()) { cout << "NO"; exit(0); } b[i][t] = 0; } } if (m1.empty() != true && m2.empty() != true) { cout << "YES" << endl; for (int i = 0; i < m2.size(); i++) { b[m1[0]][m2[i]] = 1; } for (int i = 0; i < m1.size(); i++) { b[m1[i]][m2[0]] = 1; } for (int i = 0; i < a; i++) { for (int t = 0; t < c; t++) { cout << b[i][t] << " "; } cout << endl; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int i,j,k,n,m,x,y,t,a[100010]; int main(){ scanf("%d",&n);for (i=1;i<=n;i++)scanf("%d",&a[i]); sort(a+1,a+1+n,greater<int>()); while (a[t]>=t)t++;t--; if ((a[t]-t)%2==0){ j=0; for (;a[t+j]>=t;j++); if (j%2==1)printf("Second\n"); else printf("First\n"); } else printf("First\n"); return 0; }
0
#include<iostream> using namespace std; #define int long long signed main(){ int t; cin>>t; while(t--){ int n,m; cin>>n>>m; int a[n+10]; int b[n+10]; for(int i=1;i<=n;++i) cin>>a[i]; for(int i=1;i<n;++i) cin>>b[i]; int mind=10000000000000; int day=0; int money=0; for(int i=1;i<n;++i){ mind=min(mind,day+(m-money+a[i]-1)/a[i]); if(money>=b[i]){ money-=b[i]; ++day; } else{ int tmp=(b[i]-money+a[i]-1)/a[i]; day+=tmp; ++day; money=money+tmp*a[i]-b[i]; } } cout<<min(mind,day+(m-money+a[n]-1)/a[n])<<endl; } }
6
#include <bits/stdc++.h> using namespace std; const int V = 300100; struct Node { int l, r, s; } tr[4 * V]; void init(int id, int l, int r) { tr[id].l = l; tr[id].r = r; tr[id].s = 0; if (l == r) return; int mid = (l + r) / 2; init(id * 2, l, mid); init(id * 2 + 1, mid + 1, r); } void update(int id, int pos, int v) { if (tr[id].l == pos && tr[id].r == pos) { tr[id].s = v; return; } int mid = (tr[id].l + tr[id].r) / 2; if (pos <= mid) update(id * 2, pos, v); else update(id * 2 + 1, pos, v); tr[id].s = tr[id * 2].s + tr[id * 2 + 1].s; } int read(int id, int l, int r) { if (tr[id].l == l && tr[id].r == r) return tr[id].s; int mid = (tr[id].l + tr[id].r) / 2; ; if (r <= mid) return read(id * 2, l, r); else if (l > mid) return read(id * 2 + 1, l, r); else { int ret = 0; ret += read(id * 2, l, mid); ret += read(id * 2 + 1, mid + 1, r); return ret; } } int a[V], pos[V], n, Q, op, x, y; int main() { while (~scanf("%d", &n)) { for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); pos[a[i]] = i; } init(1, 1, n); for (int i = 1; i < n; i++) if (pos[i + 1] > pos[i]) update(1, i, 1); scanf("%d", &Q); while (Q--) { scanf("%d%d%d", &op, &x, &y); if (op == 1) { printf("%d\n", y - x + 1 - read(1, x, y - 1)); } else { swap(pos[a[x]], pos[a[y]]); swap(a[x], a[y]); if (a[x] != 1) { if (pos[a[x] - 1] < x) update(1, a[x] - 1, 1); else update(1, a[x] - 1, 0); } if (a[y] != 1) { if (pos[a[y] - 1] < y) update(1, a[y] - 1, 1); else update(1, a[y] - 1, 0); } if (a[x] != n) { if (x < pos[a[x] + 1]) update(1, a[x], 1); else update(1, a[x], 0); } if (a[y] != n) { if (y < pos[a[y] + 1]) update(1, a[y], 1); else update(1, a[y], 0); } } } } return 0; }
2
#include <bits/stdc++.h> using namespace std; using namespace std; const int maxn = 200010; int n, m, q, cnt, top, tim, now, x, y; int lst[maxn], dfn[maxn], low[maxn]; int ss[maxn], bel[maxn]; int c[maxn], tot; int dep[maxn], fa[maxn][20]; int f[maxn], g[maxn]; bool vis[maxn], ins[maxn]; struct edge { int to, nxt; } e[maxn * 4]; void dire(int u, int v) { e[++cnt].to = v; e[cnt].nxt = lst[u]; lst[u] = cnt; } void adde(int u, int v) { dire(u, v); dire(v, u); } void tarjan(int x, int from) { bel[x] = now; dfn[x] = low[x] = ++tim; ss[++top] = x; ins[x] = 1; for (int i = lst[x]; i; i = e[i].nxt) { if (i == (from ^ 1)) continue; if (!dfn[e[i].to]) { tarjan(e[i].to, i); low[x] = min(low[x], low[e[i].to]); } else if (ins[e[i].to]) low[x] = min(low[x], dfn[e[i].to]); } if (dfn[x] == low[x]) { int y = 0; tot++; while (y != x) { y = ss[top]; top--; c[y] = tot; ins[y] = 0; } } } void dfs(int x) { dep[x] = dep[fa[x][0]] + 1; for (int i = 1; i <= 18; i++) fa[x][i] = fa[fa[x][i - 1]][i - 1]; for (int i = lst[x]; i; i = e[i].nxt) if (e[i].to != fa[x][0]) { fa[e[i].to][0] = x; dfs(e[i].to); } } int get_lca(int x, int y) { if (dep[x] < dep[y]) swap(x, y); for (int i = 18; i >= 0; i--) if (dep[fa[x][i]] >= dep[y]) x = fa[x][i]; if (x == y) return x; for (int i = 18; i >= 0; i--) if (fa[x][i] != fa[y][i]) { x = fa[x][i]; y = fa[y][i]; } return fa[x][0]; } void get(int x) { vis[x] = 1; for (int i = lst[x]; i; i = e[i].nxt) { if (vis[e[i].to]) continue; get(e[i].to); f[x] += f[e[i].to]; g[x] += g[e[i].to]; } } int main() { cnt = 1; cin >> n >> m >> q; for (int i = 1; i <= m; i++) { cin >> x >> y; adde(x, y); } for (int i = 1; i <= n; i++) if (!dfn[i]) now++, tarjan(i, 0); memset(lst, 0, sizeof lst); int tmp = cnt; for (int i = 2; i <= tmp; i += 2) { x = e[i].to, y = e[i ^ 1].to; if (c[x] != c[y]) adde(c[x], c[y]); } for (int i = 1; i <= tot; i++) if (!dep[i]) dfs(i); while (q--) { cin >> x >> y; if (bel[x] != bel[y]) { puts("No"); return 0; } x = c[x], y = c[y]; int w = get_lca(x, y); f[x]++, f[w]--; g[y]++, g[w]--; } for (int i = 1; i <= tot; i++) if (!vis[i]) get(i); for (int i = 1; i <= tot; i++) if (f[i] && g[i]) { puts("No"); return 0; } puts("Yes"); return 0; }
5
#include <bits/stdc++.h> #define INF (2e9) #define FOR(i, a, b) for (ll (i) = (a); (i) < (b); ++(i)) #define REP(i, n) FOR(i, 0, n) using namespace std; template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } return false; } template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } return false; } using ll = long long; using ld = long double; #define ALL(x) (x).begin(), (x).end() template<class T> using vc = vector<T>; using vi = vc<ll>; using vvi = vc<vi>; template<class T> using va = valarray<T>; using vad = va<ld>; using vai = va<ll>; vi Read(ll n) { auto v = vi(n); for (auto i = std::size_t(0); i < n; ++i) { cin >> v[i]; } return v; } template<class T> void Dump(const T& A) { for (const auto& value : A) cout << value << "\n"; } int main() { int n, p; cin >> n >> p; auto v = vc<priority_queue<int>>(n); REP(i, p) { int query, t; cin >> query >> t; if (query == 0) { int x; cin >> x; v[t].push(x); } else if (query == 1) { if (!v[t].empty()) cout << v[t].top() << endl; } else if (query == 2) { if (!v[t].empty()) v[t].pop(); } else cout << "Error: query is not correct\n"; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); int n, m; cin >> n; vector<pair<int, int> > v1(n + 1); for (int i = 1; i <= n; i++) { cin >> v1[i].first >> v1[i].second; } cin >> m; vector<pair<int, int> > v2(m + 1); for (int i = 1; i <= m; i++) { cin >> v2[i].first >> v2[i].second; } int minR1 = 0x3f3f3f3f, maxL1 = -0x3f3f3f3f; int minR2 = 0x3f3f3f3f, maxL2 = -0x3f3f3f3f; for (int i = 1; i <= n; i++) { maxL1 = max(maxL1, v1[i].first); minR1 = min(minR1, v1[i].second); } for (int i = 1; i <= m; i++) { maxL2 = max(maxL2, v2[i].first); minR2 = min(minR2, v2[i].second); } int res = max(maxL2 - minR1, maxL1 - minR2); cout << max(res, 0) << '\n'; return 0; }
2
#include <bits/stdc++.h> #define range(i, a, b) for(int i = (a); i < (b); i++) #define rep(i, a) range(i, 0, a) using namespace std; int main() { int n, k; cin >> n >> k; vector <int> p(n, -1); rep (i, k) { int a, b; cin >> a >> b; p[--a] = --b; } vector <vector <vector <int>>> dp(n, vector <vector <int>> (3, vector <int> (3, 0))); rep (i, 3) rep (j, 3) { dp[0][p[0] == -1 ? i : p[0]][p[1] == -1 ? j : p[1]] = 1; } range (i, 1, n) { if (p[i + 1] != -1) { rep (j, 3) rep (k, 3) if (p[i + 1] != j || j != k) { (dp[i][j][p[i + 1]] += dp[i - 1][k][j]) %= 10000; } } else { rep (j, 3) rep (k, 3) rep (l, 3) if (j != k || k != l) { (dp[i][j][k] += dp[i - 1][l][j]) %= 10000; } } } int cnt = 0; rep (i, 3) rep (j, 3) (cnt += dp[n - 2][i][j]) %= 10000; cout << cnt << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; template <typename Arg1> void __f(const char* name, Arg1&& arg1) { cerr << name << " : " << arg1 << '\n'; } template <typename Arg1, typename... Args> void __f(const char* names, Arg1&& arg1, Args&&... args) { const char* comma = strchr(names + 1, ','); cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...); } const long long maxn = (long long)1e6 + 6; const long double EPS = 1e-9; const long long INF = (long long)1e18 + 18; const long long mod = (long long)1e9 + 7; long long n; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; bool flag = false; if (n & 1) { n--; flag = true; } vector<long long> a(n); for (long long i = 0; i < n; i += 2) { a[i] = i + 1; a[i + 1] = i; } if (flag) cout << "1 "; for (long long i = 0; i < n; i++) { cout << a[i] + 1 + (flag ? 1 : 0) << " "; } cout << '\n'; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int _ = 2e5 + 5, yl = 1e9 + 7; int n, rt, siz[_], ch[3][_], f[_], g[_], d[_], cnt; vector<int> e[_]; long long F(int x); long long G(int x, int y); long long F(int x) { if (x == 0) return 1; if (siz[x] & 1) return 0; if (!g[x]) return siz[x] / 2; if (f[x] != -1) return f[x]; long long ans = 0, y = g[x], lc = ch[0][y], rc = ch[1][y]; for (int i = 0; i <= 1; ++i, swap(lc, rc)) { int p = ch[0][rc], q = ch[1][rc]; if (!q) { if (p) ans += G(lc, p); if (!g[rc] && d[rc] <= d[x]) ans += F(lc) * (d[x] == d[rc] ? 1 : 2); } else { if (!g[p] && d[p] <= d[x]) ans += G(lc, q); if (!g[q] && d[q] <= d[x]) ans += G(lc, p); } } return f[x] = ans % yl; } long long G(int x, int y) { if (x == 0 || y == 0) return F(x + y); if ((siz[x] + siz[y]) & 1) return 0; if (e[x].size() == 2 && e[y].size() == 2) return G(ch[0][x], ch[0][y]); if (e[x].size() == 1 && e[y].size() == 1) return 1; if (e[x].size() == 1 && e[y].size() == 2) return F(ch[0][y]); if (e[x].size() == 2 && e[y].size() == 1) return F(ch[0][x]); return 0; } long long solve() { long long ans = 0, k; memset(f, -1, sizeof(f)); k = ch[2][rt]; ans += G(ch[0][rt], ch[0][k]) * G(ch[1][rt], ch[1][k]) % yl; if (e[k].size() > 1) ans += G(ch[0][rt], ch[1][k]) * G(ch[1][rt], ch[0][k]) % yl; k = ch[1][rt]; ans += G(ch[0][rt], ch[0][k]) * G(ch[2][rt], ch[1][k]) % yl; if (e[k].size() > 1) ans += G(ch[0][rt], ch[1][k]) * G(ch[2][rt], ch[0][k]) % yl; k = ch[0][rt]; ans += G(ch[1][rt], ch[0][k]) * G(ch[2][rt], ch[1][k]) % yl; if (e[k].size() > 1) ans += G(ch[1][rt], ch[1][k]) * G(ch[2][rt], ch[0][k]) % yl; return ans * 4 % yl; } void dfs(int fa, int u) { int s = 0; siz[u] = 1; for (int v : e[u]) { if (v == fa) continue; dfs(u, v); ch[s++][u] = v; siz[u] += siz[v]; g[u] = g[v]; d[u] = d[v] + 1; } if (e[u].size() == 3) g[u] = u, d[u] = 0; } int main() { ios::sync_with_stdio(false); cin >> n; if (n == 1) cout << 2 << endl, exit(0); for (int i = 1; i < (n << 1); ++i) { int u, v; cin >> u >> v; e[u].push_back(v); e[v].push_back(u); } for (int i = 1; i < (n << 1); ++i) { if (e[i].size() > 3) cout << 0 << endl, exit(0); if (e[i].size() == 3) rt = i; } if (rt == 0) cout << (2ll * n * n - n * 2 + 4) % yl << endl, exit(0); dfs(0, rt); cout << solve() << endl; return 0; }
5
#include<iostream> #include<vector> #include<string> #include<algorithm> #include<map> #include<set> #include<utility> #include<cmath> #include<cstring> #include<queue> #include<stack> #include<cstdio> #include<sstream> #include<iomanip> #include<assert.h> #define loop(i,a,b) for(int i=a;i<b;i++) #define rep(i,a) loop(i,0,a) #define pb push_back #define mt make_tuple #define all(in) in.begin(),in.end() #define shosu(x) fixed<<setprecision(x) using namespace std; //kaewasuretyuui typedef long long ll; typedef pair<int,int> pii; typedef vector<int> vi; typedef vector<vi> vvi; typedef vector<pii> vp; typedef vector<vp> vvp; typedef vector<string> vs; typedef vector<double> vd; typedef tuple<ll,ll,ll,ll> tp; typedef vector<tp> vt; typedef vector<vd> vvd; typedef pair<int,pii> pip; typedef vector<pip>vip; const double PI=acos(-1); const double EPS=1e-7; const int inf=1e9; const ll INF=2e18; int dx[]={0,1,0,-1}; int dy[]={1,0,-1,0}; typedef ll Def; class DIJ{ public: struct edge{ Def to,cost; }; vector<vector<edge> >G; Def n,L; // vvi d;//distance Def d[510][50][2]; DIJ(Def size,Def l=0){ n=size; L=l; G=vector<vector<edge> >(n); } void add_edge(Def a,Def b,Def c){ edge e={b,c},ee={a,c}; G[a].pb(e); G[b].pb(ee); } void dij(Def a,Def b,Def c,Def D){ // d=vvi(n,vi(L+1,inf)); rep(i,510)rep(j,50)rep(k,2)d[i][j][k]=INF; d[0][D][0]=0; // d[s][L]=0;//now oktime ->time priority_queue<tp>q; q.push(tp(0,0,D,0));//time now v_0 n-1????????£?????? while(!q.empty()){ Def cost,pos,v,t; tie(cost,pos,v,t)=q.top(); cost*=-1; q.pop(); if(cost>d[pos][v][t])continue; rep(i,G[pos].size()){ edge e=G[pos][i]; Def to=e.to; Def ncost=cost+e.cost*v; Def nv=(a*v+b)%c; Def nt=t||(to==n-1); if(ncost<d[to][nv][nt]){ d[to][nv][nt]=ncost; q.push(tp(-ncost,to,nv,nt)); } } } ll out=INF; rep(i,50)out=min(out,d[0][i][1]); cout<<out<<endl; } }; int main(){ int n,m; cin>>n>>m; DIJ dij(n); rep(i,m){ int a,b,c; cin>>a>>b>>c; a--;b--; dij.add_edge(a,b,c); } int a,b,c,d; cin>>d>>a>>b>>c; dij.dij(a,b,c,d); }
0
#include <iostream> #include <cstring> using namespace std; #define MOD 1000000LL typedef unsigned long long ll; int n; ll dp[1002]; string s[1002]; string utf[4][4] = { {"0xxxxxxx","","",""}, {"110yyyyx","10xxxxxx","",""}, {"1110yyyy","10yxxxxx","10xxxxxx",""}, {"11110yyy","10yyxxxx","10xxxxxx","10xxxxxx"} }; ll check(int idx,int len){ ll res = 1; int xcnt = 0; //ƒrƒbƒgƒpƒ^[ƒ“‚Åy‚̈ʒu‚Å, “ü—Í—ñ‚ªx‚É‚È‚Á‚Ä‚¢‚鐔 int onecnt = 0; //ƒrƒbƒgƒpƒ^[ƒ“‚Åy‚̈ʒu‚Å, “ü—Í—ñ‚ª1‚É‚È‚Á‚Ä‚¢‚鐔 for(int i=0;i<len;i++){ for(int j=0;j<8;j++){ char cu = utf[len-1][i][j]; char cs = s[idx+i][j]; if(cu == '0' || cu == '1'){ if(cs != 'x' && cu != cs) return 0; } else if(cu == 'x'){ if(cs == 'x') res = (res * 2) % MOD; } else{ if(cs == 'x') xcnt++; else if(cs == '1') onecnt++; } } } if(len == 1) return res; if(onecnt==0 && xcnt==0) return 0; if(onecnt == 0) res = (res * ((1<<xcnt) - 1)) % MOD; else res = (res * (1<<xcnt)) % MOD; return res; } int main(void){ while(cin>>n,n){ for(int i=0;i<n;i++) cin>>s[i]; memset(dp,0,sizeof(dp)); dp[0] = 1; for(int i=0;i<n;i++){ for(int j=1;j<=4;j++){ if(i+j > n) break; ll pat = check(i,j); if(pat == 0) continue; dp[i+j] = (dp[i+j] + dp[i] * pat) % MOD; } } cout<<dp[n]<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int test; int n, a, b; int t[200007]; map<int, int> m; map<int, int> nr; vector<int> g[200007], G[200007]; bitset<200007> czy; stack<int> s; int w[200007]; void dfs(int v) { czy[v] = 1; for (int i = 0; i < (G[v].size()); ++i) { int u = G[v][i]; if (!czy[u]) dfs(u); } s.push(v); return; } void ustaw(int v) { czy[v] = 1; w[v] = (w[v ^ 1] == -1); for (int i = 0; i < (g[v].size()); ++i) { int u = g[v][i]; if (!czy[u]) { ustaw(u); } } return; } void solve() { cin >> n >> a >> b; for (int i = 0; i < (n); ++i) { cin >> t[i]; nr[t[i]] = i; m[t[i]] = 1; } for (int i = 0; i < (n); ++i) { if (!m[a - t[i]] && !m[b - t[i]]) { cout << "NO"; return; } } for (int i = 0; i < (n); ++i) { if (m[a - t[i]] && m[b - t[i]]) { g[i * 2].push_back(nr[b - t[i]] * 2); G[nr[b - t[i]] * 2].push_back(i * 2); g[i * 2].push_back(nr[a - t[i]] * 2); G[nr[a - t[i]] * 2].push_back(i * 2); g[i * 2 ^ 1].push_back(nr[a - t[i]] * 2 ^ 1); g[i * 2 ^ 1].push_back(nr[b - t[i]] * 2 ^ 1); G[nr[b - t[i]] * 2 ^ 1].push_back(i * 2 ^ 1); G[nr[a - t[i]] * 2 ^ 1].push_back(i * 2 ^ 1); continue; } if (m[a - t[i]]) { g[i * 2 ^ 1].push_back(nr[a - t[i]] * 2 ^ 1); G[nr[a - t[i]] * 2 ^ 1].push_back(i * 2 ^ 1); g[i * 2].push_back(i * 2 ^ 1); G[i * 2 ^ 1].push_back(i * 2); } if (m[b - t[i]]) { g[i * 2].push_back(nr[b - t[i]] * 2); G[nr[b - t[i]] * 2].push_back(i * 2); g[i * 2 ^ 1].push_back(i * 2); G[i * 2].push_back(i * 2 ^ 1); } } for (int i = 0; i < (2 * n); ++i) { if (!czy[i]) dfs(i); } for (int i = 0; i < (2 * n); ++i) { w[i] = -1; czy[i] = 0; } while (!s.empty()) { int v = s.top(); s.pop(); if (!czy[v]) ustaw(v); } for (int i = 0; i < (2 * n); ++i) { for (int j = 0; j < (g[i].size()); ++j) { if (w[i] && !w[g[i][j]]) { cout << "NO"; return; } } } cout << "YES" << '\n'; for (int i = 0; i < (n); ++i) { cout << w[i * 2] << " "; } return; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.precision(10); cout << fixed; test = 1; while (test--) solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 1e3 + 3; int n, m, dp[N], vis[2][N]; vector<int> adj[2][N]; vector<pair<int, int>> e; void dfs(int u, int dir) { vis[dir][u] = 1; for (int v : adj[dir][u]) if (!vis[dir][v]) dfs(v, dir); } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); cin >> n >> m; for (int i = 0, u, v; i < m; i++) { cin >> u >> v; adj[0][u].push_back(v); adj[1][v].push_back(u); e.push_back({u, v}); } dfs(1, 0), dfs(n, 1); for (int i = 0; i < n - 1; i++) for (auto [u, v] : e) if (vis[0][u] & vis[1][v]) { dp[u] = min(dp[u], dp[v] + 2); dp[v] = min(dp[v], dp[u] - 1); } for (auto [u, v] : e) if (vis[0][u] & vis[1][v] & (dp[u] > dp[v] + 2 || dp[v] > dp[u] - 1)) return cout << "No" << endl, 0; cout << "Yes" << endl; for (auto [u, v] : e) cout << (vis[0][u] & vis[1][v] ? dp[u] - dp[v] : 1) << endl; }
5
#include <bits/stdc++.h> using namespace std; void readfile() { ios_base ::sync_with_stdio(false); cin.tie(0); cout.tie(0); } const long long nmax = (long long)5e5 + 20; long long n; map<long long, long long> has; vector<long long> nums; map<long long, long long> pows; void pre() { long long p = 1; for (long long i = (long long)0; i <= (long long)40; i++) { pows[p] = true; p *= 2; } } bool check(long long len) { vector<vector<long long> > b; b.assign(len + 1, vector<long long>()); if (has[1] < len) return false; vector<long long> odd; map<long long, long long> oddhas; long long p = 1; long long last = len; for (long long i = (long long)1; i <= (long long)40; i++) { last = min(has[p], last); for (long long j = (long long)1; j <= (long long)last; j++) { b[j].push_back(p); } oddhas[p] = has[p] - last; if (oddhas[p] > 0) odd.push_back(p); p *= 2; } for (long long i = (long long)0; i <= (long long)nums.size() - 1; i++) { long long nn = nums[i]; if (pows[nn] == 0) { odd.push_back(nn); oddhas[nn] = has[nn]; } } long long sum = 0; for (long long i = (long long)0; i <= (long long)odd.size() - 1; i++) { sum += oddhas[odd[i]]; } if (sum > len) return false; sort((odd).begin(), (odd).end()); reverse((odd).begin(), (odd).end()); long long beg = 1; for (long long i = (long long)0; i <= (long long)odd.size() - 1; i++) { long long nun = odd[i]; for (long long k = (long long)0; k <= (long long)oddhas[nun] - 1; k++) { long long la = b[beg++].back(); if (nun > la * 2) { return false; } } } return true; } signed main() { readfile(); pre(); cin >> n; for (long long i = (long long)0; i <= (long long)n - 1; i++) { long long a; cin >> a; has[a]++; if (has[a] == 1) { nums.push_back(a); } } long long lo = 1; long long hi = has[1]; long long iter = 35; while (iter--) { long long mid = (lo + hi) / 2; bool ok = check(mid); if (ok) hi = mid; else lo = mid; } long long mnlen = (long long)1e18; for (long long i = (long long)max(1LL, lo - 10); i <= (long long)hi + 10; i++) { if (check(i)) { mnlen = i; break; } } if (mnlen == (long long)1e18) cout << -1 << endl; else { for (long long i = (long long)mnlen; i <= (long long)has[1]; i++) { cout << i << " "; } cout << endl; } return 0; }
5
#include <bits/stdc++.h> const long long mod = 1e9 + 7; using namespace std; void test_case() { int n, m, k; cin >> n >> m >> k; vector<vector<char>> table(n, vector<char>(m, '0')); int horizontal = 0; if (n % 2 == 1) horizontal = m / 2; int max_h = n * (m / 2); if (max_h < k) cout << "NO\n"; else if (k >= horizontal && (k - horizontal) % 2 == 0) { cout << "YES\n"; string alphabet = "abcdefghijklmnopqrstuvwxyz"; for (int i = 0; i < n / 2; i++) { for (int j = 0; j < m; j++) { table[2 * i][j] = alphabet[(j + 2 * i) % 26]; table[2 * i + 1][j] = alphabet[(j + 2 * i) % 26]; } } for (int j = 0; j < horizontal; j++) { table[n - 1][j * 2] = alphabet[(n + j * 2) % 26]; table[n - 1][j * 2 + 1] = alphabet[(n + j * 2) % 26]; } int delta = k - horizontal; int s = 0, e = 0; while (delta) { delta -= 2; table[s * 2][e * 2 + 1] = table[s * 2][e * 2]; table[s * 2 + 1][e * 2] = table[s * 2 + 1][e * 2 + 1]; e++; if (e == m / 2) { s++; e = 0; } } for (auto v : table) { for (auto u : v) cout << u; cout << '\n'; } } else cout << "NO\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); int N; cin >> N; while (N--) { test_case(); } }
4
#include <bits/stdc++.h> using namespace std; int n, k, m; double ans[100006], f[1 << 20 + 4]; double a[100006], eps = 1e-12; int read() { int x = 0, c; while (!isdigit(c = getchar())) ; while (x = x * 10 + c - '0', isdigit(c = getchar())) ; return x; } int main() { n = read(); k = read(); m = n; for (int i = 1; i <= n; i++) scanf("%lf", &a[i]), m -= a[i] < eps; k = min(k, m); f[0] = 1; for (int i = 0; i < (1 << n); i++) { int cnt = 0; double tmp = 1; for (int j = 0; j < n; j++) if (i & (1 << j)) cnt++, tmp -= a[j + 1]; if (cnt > k) continue; if (cnt == k) { for (int j = 0; j < n; j++) if (i & (1 << j)) cnt++, ans[j + 1] += f[i]; continue; } for (int j = 0; j < n; j++) if (!(i & (1 << j))) f[i | (1 << j)] += f[i] * a[j + 1] / tmp; } for (int i = 1; i <= n; i++) printf("%.6f ", ans[i]); return 0; }
5
#include <bits/stdc++.h> using namespace std; inline int read() { int f = 1, x = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = (x << 3) + (x << 1) + (c ^ '0'), c = getchar(); return f * x; } const int N = 2e6 + 520; int n, ans[N][3], tot, cnt; signed main() { n = read(); tot = n / 2; if (n == 5) { puts("1 2 1"); puts("2 4 1"); puts("3 5 1"); puts("4 5 2"); puts("2 3"); puts("1 3"); return 0; } for (register int(i) = (1); (i) <= (tot); ++(i)) ans[++cnt][0] = i, ans[cnt][1] = i + tot, ans[cnt][2] = 1; for (register int j = 1, i = tot + 1; i < n; ++i, ++j) ans[++cnt][0] = i, ans[cnt][1] = i + 1, ans[cnt][2] = 2 * j - 1; for (register int(i) = (1); (i) <= (cnt); ++(i)) printf("%d %d %d\n", ans[i][0], ans[i][1], ans[i][2]); for (register int(i) = (1); (i) <= (tot - 1); ++(i)) printf("%d %d\n", i, i + 1); printf("1 3\n"); return 0; }
3
#include <bits/stdc++.h> using namespace std; void codeforces_463_A() { char c[1001]; scanf("%s", c); for (int i = 0; i < strlen(c); i++) { printf("%c", c[i]); } for (int i = 0; i < strlen(c); i++) { printf("%c", c[strlen(c) - 1 - i]); } } void codeforces_463_B() { int n; cin >> n; int b[1000001]; int a[10][1000001]; for (int j = 1; j < 10; j++) { a[j][0] = 0; } for (int i = 1; i < 10; i++) { for (int j = 1; j < i; j++) { a[i][j] = 0; } for (int j = i; j < 10; j++) { a[i][j] = 1; } b[i] = i; } for (int i = 10; i <= 1000000; i++) { int b1 = 0; int b2 = 0; int b3 = 0; int b4 = 0; int b5 = 0; int b6 = 0; int b7 = 0; b1 = i % 10; b2 = (i % 100) / 10; b3 = (i % 1000) / 100; b4 = (i % 10000) / 1000; b5 = (i % 100000) / 10000; b6 = (i % 1000000) / 100000; b7 = (i % 10000000) / 1000000; if (b1 == 0) { b1 = 1; } if (b2 == 0) { b2 = 1; } if (b3 == 0) { b3 = 1; } if (b4 == 0) { b4 = 1; } if (b5 == 0) { b5 = 1; } if (b6 == 0) { b6 = 1; } if (b7 == 0) { b7 = 1; } b[i] = b[b1 * b2 * b3 * b4 * b5 * b6 * b7]; for (int k = 1; k < 10; k++) { a[k][i] = a[k][i - 1]; } a[b[i]][i]++; } for (int i = 0; i < n; i++) { int l, r, k; cin >> l; cin >> r; cin >> k; printf("%d\n", a[k][r] - a[k][l - 1]); } } void codeforces_463_C() { int n, a, b; cin >> n; cin >> a; cin >> b; int iii = -1; int jjj = 0; for (int i = 0; i <= n / a; i++) { if ((n - i * a + b) % b == 0) { iii = i; jjj = (n - i * a) / b; } } if (iii == -1) { printf("-1"); return; } else { if (a == 1) { for (int i = 0; i < iii * a; i++) { printf("%d ", i + 1); } } else { for (int i = 0; i < iii * a; i++) { if ((i + 1) % a != 0) { printf("%d ", i + 2); } else { printf("%d ", i - a + 2); } } } if (b == 1) { for (int j = iii * a; j < n; j++) { printf("%d ", j + 1); } } else { for (int j = 0; j < n - iii * a; j++) { if ((j + 1) % b != 0) { printf("%d ", j + 2 + iii * a); } else { printf("%d ", j + 2 - b + iii * a); } } } } } void codeforces_463_D() {} int power(int a, unsigned int b) { if (b == 0) { return 1; } int p; p = power(a, b / 2) % (1000000007); p = ((long long)p * (long long)p) % (1000000007); return (b % 2 == 0) ? (long long)p : ((long long)a * (long long)p) % 1000000007; } void codeforces_463_E() { int n, k; cin >> n; cin >> k; int B[k + 1]; int S[k + 1][k + 1]; for (int i = 0; i <= k; i++) { S[i][0] = 0; } S[0][0] = 1; for (int i = 0; i <= k; i++) { for (int j = i + 1; j <= k; j++) { S[i][j] = 0; } } for (int i = 1; i <= k; i++) { for (int j = 1; j <= i; j++) { S[i][j] = ((long long)j * (long long)(S[i - 1][j]) + (long long)(S[i - 1][j - 1])) % 1000000007; } } B[0] = power(2, (unsigned int)n); for (int i = 1; i <= k; i++) { if (B[i - 1] % 2 == 0) { B[i] = B[i - 1] / 2; } else { B[i] = ((long long)(B[i - 1]) + 1000000007) / 2; } B[i] = ((long long)(B[i]) * ((long long)n - i + 1)) % 1000000007; } int ans = 0; for (int i = 0; i <= k; i++) { ans = ((long long)ans + (long long)B[i] * (long long)S[k][i]) % 1000000007; } printf("%d", ans); } void codeforces_463_F() {} void codeforces_463_G() {} int main() { codeforces_463_E(); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 1; int n; char olimp[20]; int main() { scanf("%d\n", &n); while (n--) { scanf("IAO'%s\n", olimp); int len = strlen(olimp); int year = atoi(olimp); int ten = 10, first = 0; for (int i = 1; i < len; i++) { first += ten; ten *= 10; } while (year < 1989 + first) year += ten; printf("%d\n", year); } return 0; }
2
#include <bits/stdc++.h> using namespace std; long long a, b, x, y, z; int main() { cin >> a >> b; cin >> x >> y >> z; long long numY = x * 2; long long numB = y; numY += y; numB += 3 * z; cout << max(0LL, numB - b) + max(0LL, numY - a) << endl; }
1
#include <bits/stdc++.h> using namespace std; int days[20] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, in[10]; int getint(string s, int &now) { int o = 0; for (; now < s.size() && s[now] >= '0' && s[now] <= '9'; now++) o = o * 10 + (s[now] - '0'); return o; } int main() { int n, m, now = 0; bool done = 0; queue<long long> q; string s; getline(cin, s); n = getint(s, now); m = getint(s, ++now); while (getline(cin, s)) { now = 5; for (int i = 0; i < 5; i++, now++) in[i] = getint(s, now); int cnt = 0; for (int i = 1; i < in[0]; cnt += days[i++]) ; long long time = (cnt + in[1] - 1) * 24 * 3600 + in[2] * 3600 + in[3] * 60 + in[4]; while (!q.empty() && q.front() + n <= time) q.pop(); q.push(time); if (!done && q.size() == m) { for (int i = 0; i < 19; i++) cout << s[i]; cout << endl; done = 1; } } if (!done) cout << -1 << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; cin >> m >> n; vector<long long> v; long long x; for (long long i = 0; i < n; i++) { cin >> x; v.push_back(x); } sort(v.begin(), v.end()); long long dp = INT_MAX; for (long long i = 0; i <= n - m; i++) { dp = min(dp, v[i + m - 1] - v[i]); } cout << dp << "\n"; }
1
#include <bits/stdc++.h> using namespace std; int main() { int x[6]; for (int i = 0; i < 6; i++) scanf("%d", x + i); for (int i = 0; i < 3; i++) if (x[i] == x[i + 3]) { puts("YES"); return 0; } puts("NO"); return 0; }
4
#include <bits/stdc++.h> using namespace std; char s[5005]; int nxt[5005][26]; int dp[5005][5005]; const int MOD = 1e9 + 7; void add(int &x, int v) { x += v; if (x >= MOD) x -= MOD; } int main(void) { int n; scanf("%d", &n); scanf("%s", s + 1); int p[26] = {}; for (int i = 0; i < 26; i++) p[i] = n + 1; for (int i = n; i >= 1; i--) { p[s[i] - 'a'] = i; for (int j = 0; j < 26; j++) nxt[i][j] = p[j]; } for (int j = 0; j < 26; j++) nxt[0][j] = p[j]; dp[0][0] = 1; for (int i = 0; i < n; i++) for (int j = 0; j <= n; j++) { if (dp[i][j]) for (int c = 0; c < 26; c++) { add(dp[i + 1][nxt[j][c]], dp[i][j]); } } int ans = 0; for (int j = 1; j <= n; j++) { add(ans, dp[n][j]); } printf("%d\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; long long int powe(long long int x, long long int y) { long long int res = 1; while (y > 0) { if (y & 1) { res = (res * x) % 1000000007; } y = y >> 1; x = (x * x) % 1000000007; } return res % 1000000007; } long long int p[10000001][2], ans[10000001]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int q, t; long long int i = 0, n, m, j, k = 0, x, x_i, y, z; string s; cin >> s; long long int ans = 0, prev = 0; n = s.length(); if (s[n - 1] == '0') { ans = 0; } else { ans = 1; } prev = ans; for (i = n - 2; i >= 0; i--) { if (s[i] == '0') { ans = (2 * prev) % 1000000007; } else { ans = ((2 * prev) % 1000000007 + (powe(2, 2 * (n - i - 1)))) % 1000000007; } prev = ans; } cout << ans % 1000000007 << "\n"; return 0; }
3
#include<bits/stdc++.h> using namespace std; const int N = 5020; const int mod=1e9+7; typedef long long ll; inline int add(int a,int b){a+=b;return a>=mod?a-mod:a;} inline int sub(int a,int b){a-=b;return a<0?a+mod:a;} inline int mul(int a,int b){return 1ll*a*b%mod;} inline int qpow(int a,int b){int ret=1;for(;b;b>>=1,a=mul(a,a))if(b&1)ret=mul(ret,a);return ret;} int g[N][2]; int f[N][2]; int ans=0, n, a, b; int main() { cin >> n >> a >> b; if(a>b)swap(a,b); if(b>1){ f[0][0]=f[0][1]=1; for(int i=1;i<=n;i++){ for(int k=0;k<2;k++){ for(int j=1;j<=i;j++){ if(k==0&&j<a)continue; f[i][k]=add(f[i][k],f[i-j][k^1]); } } } f[0][0]=0; for(int i=1;i<n;i++){ if(i<a)g[i][0]=add(g[i][0],1); for(int j=1;j<min(i,a);j++){ g[i][0]=add(g[i][0], g[i-j][1]); } if(i<b)g[i][1]=add(g[i][1], add(f[i-1][0],f[i-1][1])); for(int j=1;j<min(i,b);j++){ int w=j==1?1:add(f[j-2][0],f[j-2][1]); g[i][1]=add(g[i][1],1ll*g[i-j][0]*w%mod); } if(n-i<b)ans=add(ans, mul(g[i][0], add(f[n-i-1][0],f[n-i-1][1]))); if(n-i<a)ans=add(ans, g[i][1]); } } cout << sub(qpow(2,n), ans) << endl; }
0
#include <bits/stdc++.h> using namespace std; const int N = 100005; long long a[N], s[N]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%I64d", &a[i]); } sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) { s[i] = s[i - 1] + a[i]; } long long mx = 0; for (int i = n - 1; i >= 1; i--) { mx += a[i] * (n - i); } int q, k; scanf("%d", &q); while (q-- > 0) { scanf("%d", &k); long long ans = 0; if (k == 1) ans = mx; else { int t = 1, m = n; long long idx = k; while (m >= idx) { ans += (s[m - 1] - s[m - 1 - idx]) * t; t++; m -= idx; idx *= k; } ans += s[m - 1] * t; } printf("%I64d ", ans); } puts(""); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, v = 0, i; cin >> n; string s; for (i = 0; i < n; i++) { cin >> s; if (s == "++X") { ++v; } else if (s == "X++") { v++; } else if (s == "--X") { --v; } else if (s == "X--") { v--; } } cout << v; }
1
#include <bits/stdc++.h> using namespace std; long long n, l, r, dp[200005][3], p[3]; const long long modu = 1000000007; int main() { scanf("%lld%lld%lld", &n, &l, &r); while (l % 3 != 0) ++p[l % 3], ++l; while (r % 3 != 0) ++p[r % 3], --r; p[0] = p[0] + 1 + (r - l) / 3; p[1] = p[1] + (r - l) / 3; p[2] = p[2] + (r - l) / 3; dp[1][0] = p[0]; dp[1][1] = p[1]; dp[1][2] = p[2]; for (int i = 2; i <= n; ++i) for (int j = 0; j < 3; ++j) if (p[j] > 0) { dp[i][(0 + j) % 3] = (dp[i][(0 + j) % 3] + dp[i - 1][0] * p[j]) % modu; dp[i][(1 + j) % 3] = (dp[i][(1 + j) % 3] + dp[i - 1][1] * p[j]) % modu; dp[i][(2 + j) % 3] = (dp[i][(2 + j) % 3] + dp[i - 1][2] * p[j]) % modu; } printf("%lld", dp[n][0]); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; long long int fast_exp(int b, int p) { if (p == 0) return 1LL; if (p & 1) return (1LL * b * fast_exp(b, p - 1)) % mod; return fast_exp((1LL * b * b) % mod, p / 2); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); string s; cin >> s; long long int c = 0, sol = 0; for (int i = (0); i <= (((int)(s).size()) - 1); ++i) { if (s[i] == 'a') c++; else { sol = (sol + fast_exp(2, c) - 1 + mod) % mod; } } cout << sol << '\n'; return 0; }
2
#include <bits/stdc++.h> using namespace std; void solve() { long long n, a, i; priority_queue<int, vector<int>, greater<int>> p; vector<string> v; string s; cin >> n; while (n--) { cin >> s; if (s != "removeMin") cin >> a; if (s == "insert") { v.push_back(s + " " + to_string(a)); p.push(a); } else if (s == "removeMin") { if (p.empty()) { v.push_back("insert 0"); } else p.pop(); v.push_back(s); } else { while (!p.empty() && a > p.top()) { p.pop(); v.push_back("removeMin"); } if (p.empty() || p.top() != a) { v.push_back("insert " + to_string(a)); p.push(a); } v.push_back(s + " " + to_string(a)); } } cout << v.size() << "\n"; for (i = 0; i < v.size(); ++i) cout << v[i] << "\n"; return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; t = 1; while (t--) { solve(); cout << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 500005; int ar[MAXN]; stack<int> stk; int nid[MAXN]; int nxt[MAXN]; int instk[MAXN]; vector<int> ans; map<int, int> table; int cnt = 0; int n; int find(int pos) { map<int, int> nxt; map<int, int> instk; while (stk.size()) { stk.pop(); } for (int i = pos; i < n; ++i) { map<int, int>::iterator itr = nxt.find(ar[i]); if (itr != nxt.end()) { ans.push_back(itr->second); ans.push_back(ar[i]); ans.push_back(itr->second); ans.push_back(ar[i]); return i; } while (stk.size() && (instk[ar[i]] > 1 || (1 == instk[ar[i]] && stk.top() != ar[i]))) { int u = stk.top(); nxt[u] = ar[i]; --instk[u]; stk.pop(); } stk.push(ar[i]); ++instk[ar[i]]; } return -1; } int main() { ios::sync_with_stdio(false); cin >> n; for (int i = 0; i < n; ++i) { cin >> ar[i]; } int t = -1; while (true) { t = find(t + 1); if (t == -1) { break; } } int len = ans.size(); cout << len << endl; for (int i = 0; i < len; ++i) { cout << ans[i] << (i == len - 1 ? '\n' : ' '); } }
5
#include <bits/stdc++.h> const int N = 300005; struct Node { int l, r, x; } a[N]; int tree[N << 2], lazy[N << 2]; void init() { memset(tree, 0, sizeof(tree)); memset(lazy, -1, sizeof(lazy)); } void pushdown(int now) { if (lazy[now] == -1) return; tree[now << 1] = tree[now << 1 | 1] = lazy[now << 1] = lazy[now << 1 | 1] = lazy[now]; lazy[now] = -1; } void update(int L, int R, int val, int l, int r, int now) { if (L <= l && r <= R) { tree[now] = lazy[now] = val; return; } pushdown(now); int mid = l + r >> 1; if (mid >= L) update(L, R, val, l, mid, now << 1); if (mid + 1 <= R) update(L, R, val, mid + 1, r, now << 1 | 1); } int query(int p, int l, int r, int now) { if (l == r) return tree[now]; pushdown(now); int mid = l + r >> 1; if (p <= mid) return query(p, l, mid, now << 1); else return query(p, mid + 1, r, now << 1 | 1); } int main() { int n, m; while (~scanf("%d%d", &n, &m)) { for (int i = 1; i <= m; ++i) scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].x); init(); for (int i = m; i >= 1; --i) { if (a[i].l <= a[i].x - 1) update(a[i].l, a[i].x - 1, a[i].x, 1, n, 1); if (a[i].x + 1 <= a[i].r) update(a[i].x + 1, a[i].r, a[i].x, 1, n, 1); } for (int i = 1; i <= n; ++i) printf("%d ", query(i, 1, n, 1)); puts(""); } return 0; }
1
#include <bits/stdc++.h> using namespace std; using lli = long long; const lli OO = 1e18; void fast() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); } lli di[8] = {-1, 1, 0, 0, -1, 1, -1, 1}; lli dj[8] = {0, 0, -1, 1, 1, 1, -1, -1}; lli frq[100001 + 7]; string ys = "YES", no = "NO"; vector<vector<lli>> adj; vector<lli> vis; lli mem[101][101][2]; vector<lli> a(100001), b(100001); int main() { string s; cin >> s; a.resize(s.size()); a[s.size() - 1] = 0; for (int i = s.size() - 2; i >= 0; i--) { a[i] = a[i + 1] + (s[i] == s[i + 1]); } lli q; cin >> q; while (q--) { lli l, r; cin >> l >> r; l--, r--; cout << a[l] - a[r] << "\n"; } }
2
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int PR = 3; int gr[105][8192], vgr[105][8192]; int dp[105][8192], vdp[105][8192]; int perm[105][2][105]; int power(int x, int t) { t %= MOD - 1; t += t < 0 ? MOD - 1 : 0; int ret = 1; while (t > 0) { if (t & 1) ret = 1LL * ret * x % MOD; x = 1LL * x * x % MOD; t >>= 1; } return ret; } void fft(int from[], int to[], int dir) { int cur = 0; for (int i = 0; i < (int)(8192); i++) { to[i] = from[cur]; for (int j = 12; j >= 0; j--) { cur ^= 1 << j; if (cur >> j & 1) break; } } for (int d = 2; d <= 8192; d <<= 1) { int dr = power(PR, (MOD - 1) / d * dir); for (int i = 0; i < 8192; i += d) { int cr = 1; for (int j = i; j < i + (d >> 1); j++) { int ev = to[j], od = 1LL * to[j + (d >> 1)] * cr % MOD; to[j] = (ev + od) % MOD; to[j + (d >> 1)] = (ev - od + MOD) % MOD; cr = 1LL * cr * dr % MOD; } } } if (dir == -1) { int invn = power(8192, -1); for (int i = 0; i < (int)(8192); i++) to[i] = 1LL * to[i] * invn % MOD; } } int ret[105]; int main() { for (int i = 2; i <= 100; i++) { gr[i - 1][i * (i + 1) / 2 - 1] = 1; if (i >= 4) gr[i - 1][i] = 1; } for (int i = 1; i <= (int)(99); i++) fft(gr[i], vgr[i], 1); for (int i = 2; i <= 100; i++) { for (int j = 1; j <= (int)(i); j++) perm[i][1][j] = j; if (i >= 4) { for (int j = 1; j <= (int)(i); j++) perm[i][0][j] = j % 2 == 1 ? i / 2 + 1 + j / 2 : j / 2; if (i & 1) swap(perm[i][0][i - 2], perm[i][0][i]); } } dp[0][0] = 1; fft(dp[0], vdp[0], 1); for (int i = 1; i <= (int)(99); i++) { for (int j = 1; j <= (int)(i); j++) for (int k = 0; k < (int)(8192); k++) vdp[i][k] = (vdp[i][k] + 1LL * vgr[j][k] * vdp[i - j][k]) % MOD; fft(vdp[i], dp[i], -1); for (int k = 0; k < (int)(8192); k++) dp[i][k] = dp[i][k] != 0; fft(dp[i], vdp[i], 1); } int T; scanf("%d", &T); while (T--) { int n, m; scanf("%d%d", &n, &m); if (dp[n - 1][m - 1]) { printf("YES\n"); int cn = n - 1, cm = m - 1; int clen = 1; ret[1] = 1; while (cn > 0) { int curn = 0; for (curn = 1; (!gr[curn][curn + 1] || !dp[cn - curn][cm - curn - 1]) && !dp[cn - curn][cm - (curn + 1) * (curn + 2) / 2 + 1]; curn++) ; bool fl = dp[cn - curn][cm - (curn + 1) * (curn + 2) / 2 + 1]; for (int i = 1; i <= (int)(clen); i++) ret[i] += perm[curn + 1][fl][1] - 1; if (fl) reverse(ret + 1, ret + clen + 1); for (int i = clen + 1; i <= clen + curn; i++) ret[i] = perm[curn + 1][fl][i - clen + 1] + (perm[curn + 1][fl][i - clen + 1] > perm[curn + 1][fl][1] ? clen - 1 : 0); clen += curn; cn -= curn; cm -= fl ? (curn + 1) * (curn + 2) / 2 - 1 : curn + 1; } for (int i = 1; i <= (int)(n); i++) printf("%d ", ret[i]); printf("\n"); } else printf("NO\n"); } return 0; }
6
#include <bits/stdc++.h> using namespace std; long long h[100005], a, r, m, s[100005]; int n; long long solve(long long cur) { int l = 1, rr = n; while (l < rr) { int mid = (l + rr) / 2; if (h[mid] < cur) l = mid + 1; else rr = mid; } long long miss = cur * ((long long)l - 1) - s[l - 1]; long long extra = s[n] - s[l - 1] - cur * (long long)(n - l + 1); long long res = 0; if (miss >= extra) { res = extra * m + (miss - extra) * a; } else { res = miss * m + (extra - miss) * r; } return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> a >> r >> m; for (int i = 1; i <= n; i++) cin >> h[i]; sort(h + 1, h + 1 + n); m = min(m, a + r); for (int i = 1; i <= n; i++) s[i] = s[i - 1] + h[i]; long long ans = 1e18; long long cak = ceil(s[n] / (double)n); long long tmp = s[n] / (long long)n; ans = min(ans, solve(cak)); ans = min(ans, solve(tmp)); for (int i = 1; i <= n; i++) ans = min(ans, solve(h[i])); cout << ans << endl; }
5
#include <bits/stdc++.h> int main(void) { int h; scanf("%d", &h); char str[256]; scanf("%s", &str); int x = 0, X = 0; for (int i = 0; i < h; i++) { if (str[i] == 'x') { x++; } else { X++; } } int d = X - x, t = 0; for (int i = 0; i < h; i++) { if (str[i] == 'x') { if (d < 0) { str[i] = 'X'; d += 2; t++; } } else { if (d > 0) { str[i] = 'x'; d -= 2; t++; } } } printf("%i\n%s\n", t, str); }
1
#include<iostream> #include<vector> using namespace std; typedef long long ll; const ll mod = 1e9 + 7; struct BIT{ int siz; vector<int> node; BIT(int n){ siz = n; node.resize(siz+1, 0); } int query(int i){ int ret = 0; while(i > 0){ ret += node[i]; i -= i&-i; } return ret; } void add(int i, int x){ while(i <= siz){ node[i] += x; i += i&-i; } } }; int main(){ int n, r; while(cin >> n, n){ vector<int> a(n); for(int i = 0; i < n; i++) a[i] = i; cin >> r; while(r-- > 0){ int s, t; cin >> s >> t; swap(a[--s], a[--t]); } vector<ll> v(n, 1); for(int i = 1; i < n; i++) v[i] = (v[i-1]*i)%mod; BIT bit(n); ll ans = 0; for(int i = 0; i < n; i++){ ans += (a[i]-bit.query(a[i]))*v[n-1-i]%mod; ans %= mod; bit.add(a[i]+1, 1); } cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> #define rep(i,n) for (int i=0; i<(n); ++i) using namespace std; using ll = long long; using P = pair<int,int>; int cnt[105]; int dp[105][80]; int main(){ int n; cin>>n; rep(i,105)cnt[i]=1; for(int i=1; i<=n; ++i){ int x=i; for(int j=2; j*j<=x; ++j){ while(x%j==0){ x/=j; cnt[j]++; } } if(x!=1)cnt[x]++; } dp[0][1]=1; for(int i=0; i<n; ++i){ for(int j=1; j<=75; ++j){ for(int k=1; k<=cnt[i]; ++k){ if(j*k>75)continue; dp[i+1][j*k]+=dp[i][j]; } } } cout<<dp[n][75]<<endl; }
0
#include <bits/stdc++.h> using namespace std; bool dp[51 * 10001]; int main() { int n, d, x, cnt = 0, sum = 0, w = 0; cin >> n >> d; dp[0] = 1; for (int i = 0; i < n; i++) { cin >> x; for (int j = (sum += x); j >= x; j--) { if (dp[j - x]) dp[j] = true; } } while (true) { int j = w + d; while (!dp[j] && j > w) j--; if (j == w) break; w = j; cnt++; } cout << w << " " << cnt << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int socks = n, ans = 0; for (int i = 1; i <= 1000000; i++) { if (i % m == 0) socks++; if (socks > 0) socks--; if (socks == 0) { ans = i; break; } } cout << ans << "\n"; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { char word[1001]; scanf("%s", &word); if (word[0] >= 'a' && word[0] <= 'z') { word[0] = word[0] - 32; } printf("%s\n", word); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = 1e5 + 5; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long t = 1; while (t--) { long long n; cin >> n; long long p = 2, ans = 0; for (long long i = (1); i <= (n); i++) { ans += p; p <<= 1; } cout << ans << "\n"; } }
3
#include <bits/stdc++.h> using namespace std; int main() { int p[4], a, b, min = 1001; for (int i = 0; i < 4; i++) { cin >> p[i]; if (p[i] < min) { min = p[i]; } } cin >> a >> b; if (min > b) { cout << b - a + 1; return 0; } int c = min - a; if (c > 0) { cout << c; } else { cout << 0; } return 0; }
1
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int sx = 150; int sy = 150; int dx[4] = { 1, 0, -1, 0 }; int dy[4] = { 0, 1, 0, -1 }; /* int dd[7][7][2]; int dt[7][7][2]; int df[7][7][2]; dd[1][2][0] = 3; dd[1][2][1] = 2; dt[1][2][0] = 2; dt[1][2][1] = 3; df[1][2][0] = 6; df[1][2][1] = 2; dd[1][3][0] = 0; dd[1][3][1] = 3; dt[1][3][0] = 2; dt[1][3][1] = 3; df[1][3][0] = 3; df[1][3][1] = 6; dd[1][4][0] = 2; dd[1][4][1] = 1; dt[1][4][0] = 2; dt[1][4][1] = 3; df[1][4][0] = 4; df[1][4][1] = 1; dd[1][5][0] = 1; dd[1][5][1] = 0; dt[1][5][0] = 2; dt[1][5][1] = 3; df[1][5][0] = 3; df[1][5][1] = 6; */ int r[7][7]; r[1][2] = 3; r[1][3] = 5; r[1][4] = 2; r[1][5] = 4; r[2][1] = 4; r[2][3] = 1; r[2][4] = 6; r[2][6] = 3; r[3][1] = 2; r[3][2] = 6; r[3][5] = 1; r[3][6] = 5; r[4][1] = 5; r[4][2] = 1; r[4][5] = 6; r[4][6] = 2; r[5][1] = 3; r[5][3] = 6; r[5][4] = 1; r[5][6] = 4; r[6][2] = 4; r[6][3] = 2; r[6][4] = 5; r[6][5] = 3; while( true ) { long long int n; cin >> n; if ( n == 0 ) break; long long int ue[300][300] = {0}; long long int h[300][300] = {0}; for ( long long int i = 0; i < n; i++ ) { long long int t, f; cin >> t >> f; long long int x = sx; long long int y = sy; while( true ) { vector< pair< int, int > > men; men.push_back( make_pair( -r[t][f], 0 ) ); men.push_back( make_pair( -f, 1 ) ); men.push_back( make_pair( -( 7 - r[t][f] ), 2 ) ); men.push_back( make_pair( -( 7 - f ), 3 ) ); sort( men.begin(), men.end() ); bool flag = false; for ( int j = 0; j < 2; j++ ) { int kx = x + dx[ men[j].second ]; int ky = y + dy[ men[j].second ]; if ( h[kx][ky] < h[x][y] ) { x = kx; y = ky; flag = true; if ( men[j].second == 0 ) { t = 7 - r[t][f]; }else if ( men[j].second == 1 ) { int k = t; t = 7 - f; f = k; }else if ( men[j].second == 2 ) { t = r[t][f]; }else if ( men[j].second == 3 ) { int k = t; t = f; f = 7 - k; } break; } } if ( flag == false ) { h[x][y]++; ue[x][y] = t; break; } } } /* for ( long long int x = sx - 5; x < sx + 5; x++ ) { for ( long long int y = sy - 5; y < sy + 5; y++ ) { cout << ue[x][y] << "[" << h[x][y] << "] "; } cout << endl; } */ long long int ans[7] = {0}; for ( long long int x = 0; x < 300; x++ ) { for ( long long int y = 0; y < 300; y++ ) { ans[ ue[x][y] ]++; } } for ( int i = 1; i <= 6; i++ ) { if ( i > 1 ) cout << " "; cout << ans[i]; } cout << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5, M = 4e6 + 5, _n = 1e6; int n, m, a[N], b[N], lz[M], it[M], q; void Down(int k) { int &tmp = lz[k]; it[((k) << 1)] += tmp; lz[((k) << 1)] += tmp; it[((k) << 1) + 1] += tmp; lz[((k) << 1) + 1] += tmp; tmp = 0; } void update(int k, int l, int r, int i, int x) { if (i < l) return; if (r <= i) { it[k] += x; lz[k] += x; } else { Down(k); int m = (l + r) >> 1; update(((k) << 1), l, m, i, x); update(((k) << 1) + 1, m + 1, r, i, x); it[k] = max(it[((k) << 1)], it[((k) << 1) + 1]); } } int get(int k, int l, int r, int i) { if (r < i) return 0; if (i <= l) return it[k]; Down(k); int m = (l + r) >> 1; return max(get(((k) << 1), l, m, i), get(((k) << 1) + 1, m + 1, r, i)); } int bs() { if (get(1, 1, _n, 1) <= 0) return -1; int l = 1, r = _n, m; while (l < r) { m = (l + r + 1) >> 1; if (get(1, 1, _n, m) > 0) l = m; else r = m - 1; } return r; } int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= n; ++i) scanf("%d", a + i); for (int i = 1; i <= m; ++i) scanf("%d", b + i); for (int i = 1; i <= n; ++i) update(1, 1, _n, a[i], +1); for (int i = 1; i <= m; ++i) update(1, 1, _n, b[i], -1); scanf("%d", &q); while (q--) { int t, i, x; scanf("%d%d%d", &t, &i, &x); if (t == 1) { update(1, 1, _n, a[i], -1); update(1, 1, _n, x, 1); a[i] = x; } else { update(1, 1, _n, b[i], 1); update(1, 1, _n, x, -1); b[i] = x; } printf("%d\n", bs()); } }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 5e2 + 5; const int inf = 1e9; bool table[maxn][maxn]; int sum[maxn][maxn]; int hasStar[maxn][maxn]; int n, m, k; int dx[] = {1, -1, 0, 0}; int dy[] = {0, 0, 1, -1}; int Sum(int r1, int c1, int r2, int c2) { return sum[r2][c2] + sum[r1 - 1][c1 - 1] - sum[r1 - 1][c2] - sum[r2][c1 - 1]; } int main() { cin >> n >> m >> k; for (int i = (int)0; i <= (int)n - 1; i++) { for (int j = (int)0; j <= (int)m - 1; j++) { char c; cin >> c; table[i][j] = (c == '1'); } } for (int i = (int)1; i <= (int)n - 2; i++) { for (int j = (int)1; j <= (int)m - 2; j++) { hasStar[i][j] = table[i][j]; for (int k = (int)0; k <= (int)3; k++) { int tx = i + dx[k]; int ty = j + dy[k]; hasStar[i][j] &= table[tx][ty]; } } } for (int i = (int)1; i <= (int)n - 1; i++) { for (int j = (int)1; j <= (int)m - 1; j++) { sum[i][j] = sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1] + hasStar[i][j]; } } long long res = 0; for (int i1 = (int)0; i1 <= (int)n - 1; i1++) { for (int i2 = (int)i1 + 2; i2 <= (int)n - 1; i2++) { for (int j = (int)0; j <= (int)m - 1; j++) { int lo = j + 2; int hi = m - 1; int ans = m; while (lo <= hi) { int mid = lo + hi; mid >>= 1; if (Sum(i1 + 1, j + 1, i2 - 1, mid - 1) >= k) { ans = mid; hi = mid - 1; } else { lo = mid + 1; } } res += (m - ans); } } } cout << res << endl; }
6
#include <bits/stdc++.h> #pragma GCC optimize("O2") using namespace std; const int N = 3e5 + 10; const long long mod = 1e9 + 7; const long long mod2 = 998244353; const long long inf = 8e18; const int LOG = 22; long long pw(long long a, long long b, long long M) { return (!b ? 1 : (b & 1 ? (a * pw(a * a % M, b / 2, M)) % M : pw(a * a % M, b / 2, M))); } struct node { long long abc, ab, bc, mn, mx; }; long long A[N], lz[4 * N]; node seg[4 * N]; void Merge(int v, int tl, int tr) { if (tl == tr) return; seg[v].abc = max({seg[v << 1].abc, seg[v << 1 | 1].abc, seg[v << 1].ab + seg[v << 1 | 1].mx, seg[v << 1].mx + seg[v << 1 | 1].bc}); seg[v].ab = max({seg[v << 1].ab, seg[v << 1 | 1].ab, seg[v << 1].mx - 2 * seg[v << 1 | 1].mn}); seg[v].bc = max({seg[v << 1].bc, seg[v << 1 | 1].bc, -2 * seg[v << 1].mn + seg[v << 1 | 1].mx}); seg[v].mx = max(seg[v << 1].mx, seg[v << 1 | 1].mx); seg[v].mn = min(seg[v << 1].mn, seg[v << 1 | 1].mn); } void B(int v, int tl, int tr) { if (tl == tr) { seg[v].abc = 0; seg[v].ab = -A[tl]; seg[v].bc = -A[tl]; seg[v].mn = seg[v].mx = A[tl]; return; } int mid = (tl + tr) >> 1; B(v << 1, tl, mid); B(v << 1 | 1, mid + 1, tr); Merge(v, tl, tr); } void second(int v, int tl, int tr) { if (tl == tr) return; long long x = lz[v]; seg[v << 1].ab -= x; seg[v << 1].bc -= x; seg[v << 1].mx += x; seg[v << 1].mn += x; lz[v << 1] += x; seg[v << 1 | 1].ab -= x; seg[v << 1 | 1].bc -= x; seg[v << 1 | 1].mx += x; seg[v << 1 | 1].mn += x; lz[v << 1 | 1] += x; lz[v] = 0; } void U(int v, int tl, int tr, int l, int r, long long x) { second(v, tl, tr); if (l > tr || r < tl) return; if (l <= tl && tr <= r) { seg[v].ab -= x; seg[v].bc -= x; seg[v].mx += x; seg[v].mn += x; lz[v] += x; return; } int mid = (tl + tr) >> 1; U(v << 1, tl, mid, l, r, x); U(v << 1 | 1, mid + 1, tr, l, r, x); Merge(v, tl, tr); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; int n, q; string s; cin >> n >> q >> s; int sz = (int)s.size(); A[1] = 0; for (int i = 0; i < sz; i++) { A[i + 2] = A[i + 1] + (s[i] == '(' ? 1 : -1); } A[sz + 2] = 0; B(1, 1, 2 * n); cout << seg[1].abc << "\n"; while (q--) { int l, r; cin >> l >> r; if (l > r) swap(l, r); if (s[l - 1] == s[r - 1]) continue; else if (s[l - 1] == '(') { U(1, 1, 2 * n, l + 1, r, -2); } else { U(1, 1, 2 * n, l + 1, r, +2); } swap(s[l - 1], s[r - 1]); cout << seg[1].abc << "\n"; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5; long long n; inline void init() {} inline void solve() { init(); string a, b; cin >> a >> b; long long cnt_00 = 0, cnt_10 = 0, cnt_01 = 0, cnt_11 = 0; for (long long i = 0; i < n; i++) { if (a[i] == '0' && b[i] == '0') { cnt_00++; } if (a[i] == '0' && b[i] == '1') { cnt_01++; } if (a[i] == '1' && b[i] == '0') { cnt_10++; } if (a[i] == '1' && b[i] == '1') { cnt_11++; } } long long ans = cnt_10 * (cnt_00 + cnt_01) + cnt_00 * cnt_11; cout << ans << '\n'; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); while (cin >> n) solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 4e5 + 100; const long long mod = 1e9 + 7; bool is_pri[310]; int pri[310]; int cnt = 0; int rtl[maxn << 2], rtr[maxn << 2], rtv[maxn << 2], lazy[maxn << 2]; long long rtn[maxn << 2], lazy1[maxn << 2]; int pre_pri[310]; int qmod(int a, int b) { long long ans = 1; while (b) { if (b & 1) ans = ans * a % mod; b >>= 1; a = 1LL * a * a % mod; } return ans; } void Pri() { for (int i = 2; i <= 300; ++i) { if (!is_pri[i]) { pre_pri[cnt] = qmod(i, mod - 2); pri[cnt++] = i; } for (int j = 0; j < cnt && i * pri[j] <= 300; ++j) { is_pri[i * pri[j]] = 1; if (i % pri[j] == 0) break; } } } void push_down(int p) { if (lazy[p] > 1 || lazy1[p]) { int p1 = p << 1 | 1, p2 = (p << 1) + 2; lazy[p1] = 1LL * lazy[p1] * lazy[p] % mod; lazy[p2] = 1LL * lazy[p2] * lazy[p] % mod; rtv[p1] = 1LL * rtv[p1] * qmod(lazy[p], rtr[p1] - rtl[p1] + 1) % mod; rtv[p2] = 1LL * rtv[p2] * qmod(lazy[p], rtr[p2] - rtl[p2] + 1) % mod; lazy[p] = 1; lazy1[p1] |= lazy1[p]; lazy1[p2] |= lazy1[p]; rtn[p1] |= lazy1[p]; rtn[p2] |= lazy1[p]; lazy1[p] = 0; } } void push_up(int p) { int p1 = p << 1 | 1, p2 = (p << 1) + 2; rtv[p] = 1LL * rtv[p1] * rtv[p2] % mod; rtn[p] = rtn[p1] | rtn[p2]; } void build(int l, int r, int p) { rtl[p] = l, rtr[p] = r; rtv[p] = 1; lazy[p] = 1; rtn[p] = lazy1[p] = 0; if (l >= r) return; build(l, (l + r) / 2, p << 1 | 1); build((l + r) / 2 + 1, r, (p << 1) + 2); } void update(int l, int r, int v, long long v1, int p) { if (rtl[p] >= l && r >= rtr[p]) { lazy[p] = 1LL * lazy[p] * v % mod; lazy1[p] |= v1; rtv[p] = 1LL * rtv[p] * qmod(v, (rtr[p] - rtl[p] + 1)) % mod; rtn[p] |= v1; return; } push_down(p); int mid = (rtr[p] + rtl[p]) / 2; if (l <= mid) { update(l, r, v, v1, p << 1 | 1); } if (r > mid) { update(l, r, v, v1, (p << 1) + 2); } push_up(p); } int query(int l, int r, int p, long long &v1) { if (rtl[p] >= l && rtr[p] <= r) { v1 |= rtn[p]; return rtv[p]; } push_down(p); int mid = (rtr[p] + rtl[p]) / 2; long long ans = 1; if (l <= mid) { ans = ans * query(l, r, p << 1 | 1, v1) % mod; } if (r > mid) { ans = ans * query(l, r, (p << 1) + 2, v1) % mod; } return ans; } int a[maxn]; void insert(int l, int r, int tmp) { int v = 1; long long v1 = 0; for (int i = 0; i < cnt; ++i) { if (pri[i] > tmp) break; if (tmp % pri[i] == 0) { v1 |= 1LL << i; while (tmp % pri[i] == 0) { tmp /= pri[i]; v *= pri[i]; } } } update(l, r, v, v1, 0); } int main() { ios::sync_with_stdio(false); cin.tie(0); Pri(); int n, q; cin >> n >> q; build(0, n - 1, 0); for (int i = 0; i < n; ++i) { cin >> a[i]; insert(i, i, a[i]); } string s; int l, r, tmp; while (q--) { cin >> s; if (s[0] == 'M') { cin >> l >> r >> tmp; --l, --r; insert(l, r, tmp); } else { cin >> l >> r; --l, --r; long long v1 = 0; long long ans = query(l, r, 0, v1); if (v1) { for (int i = 0; i < cnt; ++i) { if ((1LL << i) & v1) { ans = ans * pre_pri[i] % mod * (pri[i] - 1) % mod; } } } cout << ans << endl; } } }
6
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; long long dx[] = {0, 0, -1, 1}; long long dy[] = {1, -1, 0, 0}; long long inf = 1e18; bool check(long long a, long long b) { if (a % b == 0) return 1; return 0; } long long fgcd(long long a, long long b) { if (b == 0) return a; return fgcd(b, a % b); } void solve() { long long n; cin >> n; vector<long long> a(n + 1); long long lc = 2; for (long long i = 1; i <= n; i++) { cin >> a[i]; } if (a[1] % 2 == 0) { cout << "NO\n"; return; } for (long long i = 2; i <= n; i++) { long long gc = fgcd(i + 1, lc); lc = (lc * (i + 1)) / gc; if (check(a[i], lc)) { cout << "NO\n"; return; } } cout << "YES\n"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t_c = 1; cin >> t_c; for (long long t_case = 1; t_case <= t_c; t_case++) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; bool is_vowel(char a) { return (a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u'); } int main() { string str[3]; for (int i = 0; i < 3; i++) getline(cin, str[i]); bool c[3] = {false}; int count = 0; for (int i = 0; i < str[0].size(); i++) { if (is_vowel(str[0][i])) count++; } if (count == 5) c[0] = true; count = 0; for (int i = 0; i < str[1].size(); i++) { if (is_vowel(str[1][i])) count++; } if (count == 7) c[1] = true; count = 0; for (int i = 0; i < str[2].size(); i++) { if (is_vowel(str[2][i])) count++; } if (count == 5) c[2] = true; string ans = "NO"; if (c[0] && c[1] && c[2]) ans = "YES"; cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int a1, a2, a3, b1, b2, b3, c1, c2, c3; cin >> a1 >> a2 >> a3 >> b1 >> b2 >> b3 >> c1 >> c2 >> c3; int sum = (a2 + a3 + b1 + b3 + c1 + c2) / 2; a1 = sum - a2 - a3; b2 = sum - b1 - b3; c3 = sum - c1 - c2; cout << a1 << " " << a2 << " " << a3 << endl << b1 << " " << b2 << " " << b3 << endl << c1 << " " << c2 << " " << c3 << endl; return 0; }
2
#include <bits/stdc++.h> #define int long long using namespace std; void SME(int N, int L, vector<int> a){ deque<int> Q; for(int i=0;i<N;i++){ if( Q.size() && Q.front() <= i - L ) Q.pop_front(); while( Q.size() && a[i] < a[ Q.back() ] ) Q.pop_back(); Q.push_back( i ); if( L <= i + 1 ){ cout << a[ Q.front() ]; if( i != N - 1 ) cout << ' '; } } cout<<endl; } signed main(){ int N, L; cin >> N >> L; vector<int> a; for(int i=0;i<N;i++){ int ai; cin>>ai; a.push_back( ai ); } SME( N, L, a ); return 0; }
0
#include<iostream> #include<cstdio> #include<cstdlib> #include<algorithm> #define N 100010 using namespace std; long long num[N]; int main() { int n; scanf("%d",&n); for(int i=1;i<=n;i++) scanf("%lld",&num[i]); sort(num+1,num+n+1,greater<int>()); for(int i=2;i<=n+1;i++) if(i-1<=num[i-1] && i>num[i]) { int x=i-1; if((num[x]-x)%2==1){puts("First");return 0;} while(num[x+1]+1==i) x++; if((x-i+1)%2==0){puts("Second");return 0;} puts("First"); return 0; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int A[6], Temp, Found = 0, l = 0; for (int i = 0; i < 6; i++) { cin >> A[i]; } Temp = A[0] + A[1] + A[2] + A[3] + A[4] + A[5]; for (int i = 0; i < 3; i++) { for (int j = i + 1; j < 5; j++) { for (int k = j + 1; k < 6; k++) { if ((float)Temp / 2 == (A[i] + A[j] + A[k])) { Found++; } } } } if (Found == 0) cout << "No"; else cout << "Yes"; return 0; }
1
#include <iostream> #include <cmath> using namespace std; #define EPS (1e-10) int p, q, a, n; double lhs; int dfs(int n, int cdnm, int sdnm, double sum) { int res = 0; for (int i = cdnm; i * sdnm <= a; i += 1) { double rhs = sum + 1.0 / i; if (abs(lhs-rhs) < EPS) {res++; continue;} if (lhs < rhs) continue; if (n == 1) continue; res += dfs(n-1, i, sdnm * i, rhs); } return res; } void solve() { int res; lhs = (double)p / (double)q; res = dfs(n, 1, 1, 0); cout<<res<<endl; } int main (int argc, char const* argv[]) { while (cin>>p>>q>>a>>n, n) { solve(); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long maxN = 1e5 + 7, MOD = 1e9 + 9, INF = 1e9 + 7, N = 1007, sqr = 383008016; pair<long long, long long> res[10]; long long n, m; set<pair<long long, long long> > q; vector<pair<long long, long long> > p; long double ans, tmp; long double dis(pair<long long, long long> x, pair<long long, long long> y) { long double res = ((x.first - y.first) * (x.first - y.first)) + ((x.second - y.second) * (x.second - y.second)); return sqrt(res); } void add(long long x, long long y) { if (x >= 0 && x <= n && y >= 0 && y <= m) q.insert(pair<long long, long long>(x, y)); } int main() { cin >> n >> m; add(0, 0); add(n, 0); add(0, 1); add(n - 1, 0); add(1, 0); add(n, 1); add(1, 1); add(n - 1, 1); add(0, m); add(n, m); add(0, m - 1); add(n - 1, m); add(1, m); add(n, m - 1); add(1, m - 1); add(n - 1, m - 1); for (auto it = q.begin(); it != q.end(); it++) p.push_back(*it); for (long long i = 0; i < p.size(); i++) { for (long long j = 0; j < p.size(); j++) { for (long long x = 0; x < p.size(); x++) { for (long long y = 0; y < p.size(); y++) { if (i != j && i != x && i != y && j != x && j != y && x != y) { tmp = dis(p[i], p[j]) + dis(p[j], p[x]) + dis(p[x], p[y]); if (tmp >= ans) { ans = tmp; res[1] = p[i]; res[2] = p[j]; res[3] = p[x]; res[4] = p[y]; } } } } } } for (long long i = 1; i <= 4; i++) cout << res[i].first << " " << res[i].second << '\n'; return cout << "\n", 0; }
2
#include <bits/stdc++.h> using namespace std; map<string, vector<vector<int> > > mp; map<string, int> mp2; int check(string s, vector<string> s2) { int ans = mp[s].size(); for (int i = 0; i < mp[s].size(); i++) { if (mp[s][i].size() != s2.size()) { ans--; continue; } for (int j = 0; j < mp[s][i].size(); j++) if (mp[s][i][j] && mp[s][i][j] != mp2[s2[j]]) { ans--; break; } } return ans; } int main() { string s; int na, nb, nc; cin >> na; getline(cin, s); for (int i = 0; i < na; i++) { getline(cin, s); string x1 = ""; int j = 0; while (s[j] == ' ') j++; while (s[j] != ' ') j++; while (s[j] == ' ') j++; while (s[j] != ' ' && s[j] != '(') x1 += s[j], j++; while (s[j] != '(') j++; j++; vector<int> v; while (s[j] != ')') { if (s[j] == 'i') v.push_back(1), j += 2; if (s[j] == 'd') v.push_back(2), j += 5; if (s[j] == 's') v.push_back(3), j += 5; if (s[j] == 'T') v.push_back(0); j++; } mp[x1].push_back(v); } cin >> nb; for (int i = 0; i < nb; i++) { cin >> s; string s2; cin >> s2; if (s == "int") mp2[s2] = 1; if (s == "double") mp2[s2] = 2; if (s == "string") mp2[s2] = 3; } cin >> nc; getline(cin, s); for (int i = 0; i < nc; i++) { getline(cin, s); int j = 0; string s2 = "", s3 = ""; while (s[j] == ' ') j++; while (s[j] != '(' && s[j] != ' ') { s2 += s[j]; j++; } while (s[j] != '(') j++; vector<string> v; j++; for (j; j < s.size(); j++) { if (s[j] == ',' || s[j] == ')') { v.push_back(s3); s3 = ""; continue; } if (s[j] != ' ') s3 += s[j]; } cout << check(s2, v) << endl; } }
4
#include <cstdio> #include <algorithm> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) int main(){ int q; scanf("%d",&q); rep(tt,q){ int n,cnt=0; scanf("%d",&n); while(true){ int d[7]={},tmp=n,i,mx=-1; for(i=0;tmp>0;i++){ d[i]=tmp%10; tmp/=10; } if(i<=1) break; for(int j=1;j<i;j++){ int l=0,r=0; rep(k,j) l=10*l+d[j-1-k]; rep(k,i-j) r=10*r+d[i-1-k]; mx=max(mx,l*r); } n=mx; cnt++; } printf("%d\n",cnt); } }
0
#include <bits/stdc++.h> using namespace std; template <class c> struct rge { c b, e; }; template <class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template <class c> auto dud(c* x) -> decltype(cerr << *x, 0); template <class c> char dud(...); struct debug { template <class c> debug& operator<<(const c&) { return *this; } }; void test_case() { int n; cin >> n; vector<int> b(n), a(n); for (auto& x : b) cin >> x; a[0] = b[0]; int m = a[0]; for (int i = 1; i < n; i++) { a[i] = m + b[i]; m = max(m, a[i]); } for (auto x : a) { printf("%d ", x); } puts(""); } int main() { ios::sync_with_stdio(false); cin.tie(0); int T = 1; for (int nr = 1; nr <= T; nr++) { test_case(); } }
2
#include <bits/stdc++.h> using namespace std; bool dp[500][5001]; pair<pair<int, int>, int> pr[500][5001]; int d, s; int main() { cin >> d >> s; dp[0][0] = true; pr[0][0] = make_pair(make_pair(-1, -1), -1); queue<pair<int, int> > P; P.push(make_pair(0, 0)); while (!P.empty()) { int i = P.front().first, j = P.front().second; P.pop(); for (int k = 0; k < 10; k++) { int ni = (i * 10 + k) % d; int nj = j + k; if (nj > s) continue; if (!dp[ni][nj]) { dp[ni][nj] = true; pr[ni][nj] = make_pair(make_pair(i, j), k); P.push(make_pair(ni, nj)); } } } if (!dp[0][s]) { cout << -1 << endl; return 0; } string ans = ""; int i = 0, j = s; while (i || j) { ans += pr[i][j].second + '0'; int ni = pr[i][j].first.first; int nj = pr[i][j].first.second; i = ni, j = nj; } for (int i = ans.size(); i--;) cout << ans[i]; cout << endl; }
1
#include <iostream> #include <iomanip> #include <math.h> using namespace std; const double eps = 1e-10; class Point { public: double x, y; Point(double _x = 0., double _y = 0.) : x(_x), y(_y) {} }; istream& operator >> (istream& in, Point& P) { in >> P.x >> P.y; return in; } ostream& operator << (ostream& out, const Point& P) { out << P.x << " " << P.y; return out; } double Dist(const Point& P, const Point& Q) { double dx = P.x - Q.x; double dy = P.y - Q.y; return sqrt(dx * dx + dy * dy); } class Circle { public: Point c; double r; Circle(const Point& _c = Point(), double _r = 0.) : c(_c), r(_r) {} }; istream& operator >> (istream& in, Circle& C) { in >> C.c >> C.r; return in; } int CircStatus(const Circle& C1, const Circle& C2) { double d = Dist(C1.c, C2.c); if (d > C1.r + C2.r + eps) { return 4; } else if (d > C1.r + C2.r -eps) { return 3; } else if (d > fabs(C1.r - C2.r) + eps) { return 2; } else if (d > fabs(C1.r - C2.r) - eps) { return 1; } else { return 0; } } class Vector { public: double x, y; Vector(double _x = 0., double _y = 0.) : x(_x), y(_y) {} Vector(const Point& P) : x(P.x), y(P.y) {} Vector(const Point& St, const Point& End) : x(End.x - St.x), y(End.y - St.y) {} inline double InnerProd(const Vector& V) { return x * V.x + y * V.y; } inline double OuterProd(const Vector& V) { return x * V.y - y * V.x; } inline double Length() { return sqrt((*this).InnerProd(*this)); } }; Vector operator+(const Vector& V1, const Vector& V2) { return Vector(V1.x + V2.x, V1.y + V2.y); } Vector operator-(const Vector& V1, const Vector& V2) { return Vector(V1.x - V2.x, V1.y - V2.y); } Vector operator*(double d, const Vector& V) { return Vector(d * V.x, d * V.y); } Vector operator/(const Vector&V, double d) { return Vector(V.x / d, V.y / d); } bool operator<(const Vector& V1, const Vector& V2) { return V1.x < V2.x || (V1.x == V2.x && V1.y < V2.y); } ostream& operator<<(ostream& out, const Vector V) { out << V.x << " " << V.y; return out; } void PrintCross(const Circle& C, const Point& St, const Point& End) { Vector LineVec(St, End); Vector LineUnitVec = LineVec / LineVec.Length(); Vector RefVec(St, C.c); double ParaDist = LineUnitVec.InnerProd(RefVec); double PerpDist = fabs(LineUnitVec.OuterProd(RefVec)); double ParaDistDiff = sqrt(C.r * C.r - PerpDist * PerpDist); Vector Res1 = St + (ParaDist - ParaDistDiff) * LineUnitVec; Vector Res2 = St + (ParaDist + ParaDistDiff) * LineUnitVec; if (Res2 < Res1) { swap(Res1, Res2); } cout << Res1 << " " << Res2 << endl; } void PrintCrossCircles(const Circle& C1, const Circle& C2) { double d = Dist(C1.c, C2.c); double Cos = (d * d + C1.r * C1.r - C2.r * C2.r) / (2 * d * C1.r); double Sin = sqrt(max(0., 1 - Cos * Cos)); Vector ParaVec(C1.c, C2.c); Vector ParaUnitVec = ParaVec / ParaVec.Length(); Vector PerpUnitVec(ParaUnitVec.y, -ParaUnitVec.x); Vector Res1 = C1.c + C1.r * Cos * ParaUnitVec + C1.r * Sin * PerpUnitVec; Vector Res2 = C1.c + C1.r * Cos * ParaUnitVec - C1.r * Sin * PerpUnitVec; if(Res2 < Res1) { swap(Res1, Res2); } cout << Res1 << " " << Res2 << endl; } Vector TiltVec(const Vector& V, double Theta) { Vector Ret; Ret.x = V.x * cos(Theta) - V.y * sin(Theta); Ret.y = V.x * sin(Theta) + V.y * cos(Theta); return Ret; } void PrintTangentPoints(const Point& P, const Circle& C) { double d = Dist(P, C.c); double l = sqrt(d * d - C.r * C.r); double Theta = asin(C.r / d); Vector RefVec(P, C.c); Vector RefUnitVec = RefVec / RefVec.Length(); Vector Res1 = P + l * TiltVec(RefUnitVec, Theta); Vector Res2 = P + l * TiltVec(RefUnitVec, -Theta); if (Res2 < Res1) { swap(Res1, Res2); } cout << Res1 << endl << Res2 << endl; } int main() { Point P; Circle C; cin >> P >> C; cout << fixed << setprecision(10); PrintTangentPoints(P, C); return 0; }
0
#include <bits/stdc++.h> int main() { long n, i, yes, no, a, b, c, d, e, max = -100000000; char name[10005] = {'\0'}, std[10005] = {'\0'}; scanf("%ld\n", &n); for (i = 1; i <= n; i++) { scanf("%s %ld %ld %ld %ld %ld %ld %ld\n", &name, &yes, &no, &a, &b, &c, &d, &e); if (yes * 100 + no * (-50) + a + b + c + d + e > max) { max = yes * 100 + no * (-50) + a + b + c + d + e; strcpy(std, name); } } printf("%s\n", std); return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long L = 5e18; long long gcd(long long a, long long b, long long *x, long long *y) { if (b == 0LL) { (*x) = 1LL; (*y) = 0LL; return a; } long long x1, y1; long long g = gcd(b, a % b, &x1, &y1); (*x) = y1; (*y) = x1 - (a / b) * y1; return g; } int main() { long long A, B, C; cin >> A >> B >> C; if (A == 0LL and B == 0LL) { if (C == 0LL) cout << "1 1" << endl; else cout << "-1" << endl; } else { long long a = max(abs(A), abs(B)); long long b = min(abs(A), abs(B)); long long x, y; long long g = gcd(a, b, &x, &y); long long c = abs(C); if (c % g != 0LL) cout << "-1" << endl; else { long long fact = (-1LL * C) / g; x *= fact; y *= fact; if (a == abs(A)) { if (A < 0) x *= -1LL; if (B < 0) y *= -1LL; } else { if (A < 0) y *= -1LL; if (B < 0) x *= -1LL; swap(x, y); } if (abs(x) <= L and abs(y) <= L) cout << x << " " << y << endl; else cout << "-1" << endl; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; template <typename item> item abs(item i) { if (i < 0) i = -i; return i; } char *s; int n; int pos; int main() { cin >> n; cin >> pos; s = new char[n + 2]; cin >> s + 1; int cnt = 0; if (n == 1) { cout << 0; return 0; } if (n % 2 == 1 and pos == n / 2 + 1) pos++, cnt++; if (pos > n / 2) pos = n - pos + 1; int last = n / 2, fst = 1; while (fst < n - fst + 1 and s[fst] == s[n - fst + 1]) fst++; while (last > 0 and s[last] == s[n + 1 - last]) last--; if (pos >= fst and pos <= last) { int a = pos - fst, b = last - pos; cnt += 2 * (min(a, b)) + max(a, b); } else if (pos >= fst and pos >= last) cnt += pos - min(fst, last); else if (pos <= last and pos <= fst) cnt += max(fst, last) - pos; for (int i = 1; i <= n / 2; i++) { int cmp = n - i + 1; if (s[i] > s[cmp]) swap(s[i], s[cmp]); int a = abs((int)(s[i] - s[cmp])), b = 'z' - s[cmp] + s[i] - 'a' + 1; cnt += min(a, b); } cout << cnt; }
3
#include <bits/stdc++.h> using namespace std; const long long int p_mod = 9999999999999983; const long double pi = 3.14159265358979323; const long long int N = 1e6 + 9; const long long int mod = 1e9 + 7; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int rand(int l, int r) { uniform_int_distribution<int> uid(l, r); return uid(rng); } long long int powerk(long long int x, long long int y); long long int a[N]; map<long long int, long long int> mp; void solve() { long long int n, k, maxm = 0; cin >> n >> k; for (int i = 1; i <= n; ++i) { cin >> a[i]; maxm = max(maxm, a[i]); } if (k > n) cout << maxm; else { long long int cnt = 0, num = a[1]; for (int i = 2; i <= n; ++i) { if (a[i] == maxm) { num = maxm; break; } if (a[i] > num) { num = a[i]; cnt = 1; } else cnt++; if (cnt == k) break; } cout << num; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int t = 1; while (t--) { solve(); } } long long int powerk(long long int x, long long int y) { if (y == 0) return 1; if (y == 1) return x % mod; if (y & 1) return ((powerk((x * x) % mod, y / 2) % mod) * x) % mod; else return powerk((x * x) % mod, y / 2) % mod; }
2
#include <bits/stdc++.h> using namespace std; int main(void) { int t; cin >> t; while (t) { int n, m, k, value = 0; cin >> n >> m >> k; int pc = n / k; if (pc >= m) cout << m << endl; else { int ar[100] = {0}; int rem = m - pc; for (int i = 0; (i < k - 1 && rem > 0); i++) { ar[i] += 1; rem--; if (i == k - 2) i = -1; } sort(ar, ar + k, greater<int>()); for (int i = 0; i < k - 1; i++) { if (ar[i] == pc) value = 1; } if (value == 0) cout << (pc)-ar[0] << endl; else cout << 0 << endl; } t--; } }
1
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <complex> #include <string> #include <sstream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <functional> #include <iostream> #include <map> #include <set> using namespace std; typedef pair<int,int> P; typedef long long ll; typedef vector<int> vi; typedef vector<ll> vll; #define pu push #define pb push_back #define mp make_pair #define eps 1e-9 #define INF 2000000000 #define sz(x) ((int)(x).size()) #define fi first #define sec second #define SORT(x) sort((x).begin(),(x).end()) #define all(x) (x).begin(),(x).end() #define EQ(a,b) (abs((a)-(b))<EPS) int n,m,p,q,r; struct cards { int from,to; cards(int f,int t) { from=f,to=t; } }; deque<cards> all; deque<cards> split(int k) { deque<cards> sub; for(;;) { cards t=all.front(); all.pop_front(); k -= t.to-t.from+1; if(k==0) { sub.pb(t); break; } if(k<0) { all.push_front(cards(t.to+k+1,t.to)); t.to+=k; sub.pb(t); break; } sub.pb(t); } return sub; } void shuffle(int x,int y) { deque<cards> a=split(x); deque<cards> b=split(y-x); for(int i=0;i<b.size();i++)all.pb(b[i]); for(int i=0;i<a.size();i++)all.pb(a[i]); } int main() { while(1) { cin >> n; if(n==0)break; all.clear(); all.pb(cards(1,n)); cin >> m; cin >> p >> q >> r; for(int i=0;i<m;i++) { int x,y; cin >> x >> y; shuffle(x,y); } int ans=0; split(p-1); deque<cards> res=split(q-p+1); for(int i=0;i<res.size();i++) { if(res[i].from<=r) { if(res[i].to>=r) { ans+=r-res[i].from+1; } else { ans+=res[i].to-res[i].from+1; } } } cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, dist[100100], branch[100100], sz[100100]; vector<int> a[100100]; int *f[100100], fromRoot[100100]; void visit(int x, int par, int idBranch) { branch[x] = idBranch; sz[idBranch]++; for (int i = 0; i < int(a[x].size()); i++) { int y = a[x][i]; if (y == par) continue; dist[y] = dist[x] + 1; visit(y, x, idBranch); } } void add(int f[], int x, int val, int mx) { for (int i = x + 1; i <= mx + 1; i += i & -i) f[i] += val; } int get(int f[], int x) { int res = 0; for (int i = x + 1; i; i -= i & -i) res += f[i]; return res; } int main() { int Q, x, y, type, v, d; cin >> n >> Q; for (int i = 1; i < n; i++) { scanf("%d%d", &x, &y); a[x].push_back(y); a[y].push_back(x); } for (int i = 0; i < int(a[1].size()); i++) { dist[a[1][i]] = 1; visit(a[1][i], 1, i); f[i] = new int[sz[i] + 5]; for (int j = 0; j <= sz[i] + 1; j++) f[i][j] = 0; } while (Q--) { scanf("%d%d", &type, &v); if (type) { int ans = get(fromRoot, dist[v]); if (v != 1) ans += get(f[branch[v]], dist[v]); printf("%d\n", ans); } else { scanf("%d%d", &x, &d); if (v != 1) { int idBranch = branch[v]; int from = max(1, dist[v] - d), to = min(sz[idBranch], dist[v] + d); d -= dist[v]; from = max(from, d + 1); if (from <= to) { add(f[idBranch], from, x, sz[idBranch]); add(f[idBranch], to + 1, -x, sz[idBranch]); } } if (d < 0) continue; add(fromRoot, 0, x, n); add(fromRoot, d + 1, -x, n); } } }
5
#include <bits/stdc++.h> const int mod = 998244353, N = 3e5 + 5; int n, nxt[N << 1], to[N << 1], head[N], cnt, f[N][3]; int read() { int x = 0, f = 1; char s; while ((s = getchar()) > '9' || s < '0') if (s == '-') f = -1; while (s >= '0' && s <= '9') x = (x << 1) + (x << 3) + (s ^ 48), s = getchar(); return x * f; } void addEdge(const int u, const int v) { nxt[++cnt] = head[u], to[cnt] = v, head[u] = cnt; } int inv(int x, int y) { int r = 1; while (y) { if (y & 1) r = 1ll * r * x % mod; x = 1ll * x * x % mod; y >>= 1; } return r; } void DP(const int u, const int fa) { f[u][0] = 0, f[u][1] = f[u][2] = 1; int tmp = 1; for (int i = head[u]; i; i = nxt[i]) if (to[i] ^ fa) { DP(to[i], u); int v = to[i]; f[u][1] = 1ll * f[u][1] * ((f[v][0] * 2 % mod + f[v][2]) % mod) % mod; f[u][2] = 1ll * f[u][2] * (f[v][0] + f[v][2]) % mod; tmp = 1ll * tmp * ((f[v][0] * 2 % mod + f[v][2]) % mod) % mod; } f[u][1] = (f[u][1] - f[u][2] + mod) % mod; for (int i = head[u]; i; i = nxt[i]) if (to[i] ^ fa) f[u][0] = (f[u][0] + 1ll * tmp * (f[to[i]][1] + f[to[i]][2]) % mod * inv((f[to[i]][0] * 2 % mod + f[to[i]][2]) % mod, mod - 2) % mod) % mod; } int main() { int u, v; n = read(); for (int i = 1; i < n; ++i) { u = read(), v = read(); addEdge(u, v), addEdge(v, u); } DP(1, 0); printf("%d\n", (f[1][0] + f[1][2]) % mod); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 100006; struct pi { int x; int y; } pp[maxn], pp2[maxn]; int cmp(pi a, pi b) { return a.x < b.x; } int cmp1(pi a, pi b) { return a.y < b.y; } int main() { int i, j, n, m; cin >> n >> m; for (i = 1; i <= n; i++) { int x, y, x1, y1; scanf("%d%d%d%d", &x, &y, &x1, &y1); pp[i].x = (x + x1); pp[i].y = (y + y1); } sort(pp + 1, pp + 1 + n, cmp); long long s1 = (long long)1 << 61; for (i = 0; i <= m; i++) { for (j = 0; i + j <= m; j++) { int x2 = 0, x1 = 2000000000; int y2 = 0, y1 = 2000000000; int tot = 0; for (int f = 1 + i; f <= n - j; f++) { pp2[++tot] = pp[f]; } sort(pp2 + 1, pp2 + tot + 1, cmp1); int k = m - (i + j); for (int u = 0; u <= k; u++) { x2 = 0, x1 = 2000000000; y2 = 0, y1 = 2000000000; for (int v = 1 + u; v <= tot - (k - u); v++) { x2 = max(x2, pp2[v].x); x1 = min(x1, pp2[v].x); y2 = max(y2, pp2[v].y); y1 = min(y1, pp2[v].y); } if (x2 == x1) x2 = x1 + 1; if (y2 == y1) y2 = y1 + 1; x2 = x2 - x1; if (x2 % 2 == 1) x2 = (x2 + 1) / 2; else x2 = x2 / 2; y2 = y2 - y1; if (y2 % 2 == 1) y2 = (y2 + 1) / 2; else y2 = y2 / 2; s1 = min(s1, (long long)x2 * y2); } } } cout << s1 << endl; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int count = 0; if (a[0] == 0) count++; for (int i = 1; i < n; i++) { if (a[i] == 0) { count++; } else if (a[i] == 2 && a[i - 1] == 2) { count++; a[i] = 0; } else if (a[i] == 1 && a[i - 1] == 1) { count++; a[i] = 0; } else if (a[i] == 3 && a[i - 1] == 2) { a[i] = 1; } else if (a[i] == 3 && a[i - 1] == 1) { a[i] = 2; } } cout << count; }
1
#include <bits/stdc++.h> using namespace std; const int mxN = 1e4 + 2; vector<int> adj[mxN], cnt(mxN); int main() { long long int n, a, b, m, i, j, cn = 0, sm = 0, mx = INT_MIN, mn = INT_MAX, k; string s; cin >> n >> m; for (i = 0; i <= 10000; i++) for (j = i; j <= 10000; j++) { if (__builtin_popcount(i ^ j) == m) { adj[i].push_back(j); if (i != j) adj[j].push_back(i); } } for (i = 0; i < n; i++) cin >> a, cnt[a]++; for (i = 0; i <= 10000; i++) { for (auto j : adj[i]) sm += (long long int)cnt[i] * cnt[j]; } if (!m) sm -= n; cout << sm / 2 << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; string arr[4]; bool go(char** a, int n, int m, int startR, int startC, string s) { for (int i = 0; i < s.length(); ++i) { if (s[i] == '0') { if (arr[0] == "up") --startR; else if (arr[0] == "down") ++startR; else if (arr[0] == "right") ++startC; else --startC; } else if (s[i] == '1') { if (arr[1] == "up") --startR; else if (arr[1] == "down") ++startR; else if (arr[1] == "right") ++startC; else --startC; } else if (s[i] == '2') { if (arr[2] == "up") --startR; else if (arr[2] == "down") ++startR; else if (arr[2] == "right") ++startC; else --startC; } else { if (arr[3] == "up") --startR; else if (arr[3] == "down") ++startR; else if (arr[3] == "right") ++startC; else --startC; } if (startR >= 0 && startR < n && startC >= 0 && startC < m) { if (a[startR][startC] == '#') return 0; else if (a[startR][startC] == 'E') return 1; } else return 0; } return 0; } int main() { int n, m, startR, startC, res = 0; cin >> n >> m; char** a = new char*[n]; for (int i = 0; i < n; ++i) a[i] = new char[m]; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 0; i < n; ++i) { bool found = 0; for (int j = 0; j < m; ++j) { if (a[i][j] == 'S') { startR = i; startC = j; found = 1; } } if (found) break; } string s; cin >> s; string b[4] = {"up", "down", "right", "left"}; for (int i = 0; i < 4; ++i) { arr[0] = b[i]; for (int j = 0; j < 4; ++j) { if (j != i) { arr[1] = b[j]; for (int k = 0; k < 4; ++k) { if (k != i && k != j) { arr[2] = b[k]; for (int l = 0; l < 4; ++l) { if (l != i && l != j && l != k) { arr[3] = b[l]; if (go(a, n, m, startR, startC, s)) { res += 1; } } } } } } } } cout << res; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long int dp[5005][2]; long long int inf = 1000000000000000000; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int n, k; cin >> k >> n; vector<long long int> b; for (long long int i = 0; i < n; i++) { long long int z; cin >> z; b.push_back(z); } sort(b.begin(), b.end()); vector<long long int> a; for (long long int i = 1; i < n; i++) { a.push_back(b[i] - b[i - 1]); } vector<pair<long long int, long long int> > v; for (long long int i = 1; i <= n - 1; i++) { long long int z; z = a[i - 1]; v.push_back(make_pair(z, i)); } sort(v.begin(), v.end()); vector<pair<long long int, long long int> > u; u.push_back(make_pair(0, 0)); for (long long int i = 0; i < min(3 * k, n - 1); i++) { u.push_back(make_pair(v[i].second, v[i].first)); } sort(u.begin(), u.end()); n = min(n - 1, 3 * k); for (long long int i = 0; i <= k; i++) { dp[i][0] = inf; dp[i][1] = inf; } dp[0][0] = 0; dp[0][1] = 0; for (long long int i = 1; i <= n; i++) { long long int dp2[k + 2][2]; for (long long int j = 0; j <= k; j++) { dp2[j][0] = inf; dp2[j][1] = inf; } for (long long int j = 0; j <= k; j++) { if (u[i].first == u[i - 1].first + 1) { dp2[j][0] = min(dp2[j][0], min(dp[j][0], dp[j][1])); if (j) { dp2[j][1] = min(dp2[j][1], dp[j - 1][0] + u[i].second); } } else { dp2[j][0] = min(dp2[j][0], min(dp[j][0], dp[j][1])); if (j) { dp2[j][1] = min(dp2[j][1], min(dp[j - 1][0], dp[j - 1][1]) + u[i].second); } } } for (long long int j = 0; j <= k; j++) { dp[j][0] = dp2[j][0]; dp[j][1] = dp2[j][1]; } } cout << min(dp[k][0], dp[k][1]); }
5
#include <bits/stdc++.h> using namespace std; struct struc { string name; vector<string> cont; vector<int> count; }; int main() { int k, n, m, q, a; struc ms; bool bol, bol2; string s; char s2[3]; scanf("%d%d%d%d\n", &k, &n, &m, &q); vector<struc> art; vector<int> v2; vector<string> v1; pair<string, int> p; vector<pair<string, int> > v3; vector<vector<pair<string, int> > > hh(k, v3), hh2(k, v3); v2.push_back(1); for (int i = 0; i < n; i++) { cin >> s; v1.clear(); v1.push_back(s); ms.name = s; ms.cont = v1; ms.count = v2; art.push_back(ms); } getline(cin, s); for (int i = 0; i < m; i++) { v1.clear(); v2.clear(); getline(cin, s); s += ','; ms.name = s.substr(0, s.find(":")); s.erase(0, s.find(":") + 2); while (s.find(",") < s.size()) { s2[0] = ' '; s2[1] = ' '; s2[2] = ' '; v1.push_back(s.substr(0, s.find(" "))); s.erase(0, s.find(" ") + 1); s.copy(s2, s.find(","), 0); s.erase(0, s.find(",") + 2); v2.push_back(atoi(s2)); } ms.cont = v1; ms.count = v2; art.push_back(ms); } for (int i = 0; i < q; i++) { cin >> a >> s; bol = 0; for (int j = 0; j < hh[a - 1].size(); j++) { if (hh[a - 1][j].first == s) { bol = 1; hh[a - 1][j].second++; break; } } if (!bol) { p.first = s; p.second = 1; hh[a - 1].push_back(p); } for (int l = 0; l < art.size(); l++) { bol = 0; bol2 = 0; hh2 = hh; for (int z = 0; z < art[l].cont.size(); z++) { for (int j = 0; j < hh2[a - 1].size(); j++) { if ((art[l].cont[z] == hh2[a - 1][j].first) && (art[l].count[z] <= hh2[a - 1][j].second)) { hh2[a - 1][j].second -= art[l].count[z]; break; } if (j == hh2[a - 1].size() - 1) bol = 1; } if (bol) { hh2 = hh; bol = 0; bol2 = 1; break; } } hh = hh2; if (!bol2) { bol = 0; for (int j = 0; j < hh[a - 1].size(); j++) { if (hh[a - 1][j].first == art[l].name) { bol = 1; hh[a - 1][j].second++; break; } } if (!bol) { p.first = art[l].name; p.second = 1; hh[a - 1].push_back(p); } } } } for (int i = 0; i < hh.size(); i++) { for (int j = 0; j < hh[i].size(); j++) { if (hh[i][j].second == 0) { hh[i].erase(hh[i].begin() + j); j--; } } sort(hh[i].begin(), hh[i].end()); } for (int i = 0; i < hh.size(); i++) { printf("%d\n", (int)hh[i].size()); for (int j = 0; j < hh[i].size(); j++) { for (int l = 0; l < hh[i][j].first.size(); l++) printf("%c", hh[i][j].first[l]); printf(" %d\n", hh[i][j].second); } } return 0; }
3
#include <bits/stdc++.h> using namespace std; typedef long long ll; int main(){ string S; cin >> S; int len = S.length(); vector<int> v(len); S = S + "X"; for ( int i = 0; i < len; ){ int i0 = i; int R = 0, L = 0; // RR...RLL...L の連続数を調べる while ( S[i] == 'R' ){ R++; i++; } while ( S[i] == 'L' ){ L++; i++; } // RLの境界に集まる v[i0+R-1] = (R+1)/2 + L/2; v[i0+R] = R/2 + (L+1)/2; } for ( auto it : v ) cout << it << " "; cout << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); ; long long int n, m; cin >> n >> m; long long int ans = 0; while (m != n) { if (m < n) m++; else { if (m % 2 == 0) m /= 2; else m++; } ans++; } cout << ans; return 0; }
2
#include <cstdio> using namespace std; int a[100010]; int main() { int N; scanf("%d", &N); for(int i=0;i<N;++i) { scanf("%d", &a[i]); } int ans=0; for(int i=0;i<N;++i) { if (a[a[i]-1]-1==i) ans++; } printf("%d\n",ans/2); return 0; }
0
#include <bits/stdc++.h> using namespace std; const double g = 10.0, eps = 1e-12; const int N = 100000 + 10, maxn = 1000 + 10, inf = 0x3f3f3f3f, INF = 0x3f3f3f3f3f3f3f3f; int a[N], n; int cal(int x) { int ans = 0; for (int i = 0; i < n; i++) { if (a[i] > a[(i - x + n) % n]) ans++; } return ans; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); int l = 0, r = n; while (l < r - 1) { int m = (l + r) >> 1; int m1 = (m + r) >> 1; if (cal(m) < cal(m1)) l = m; else r = m1; } int ans = 0; for (int i = max(0, l - 10); i < min(n, l + 10); i++) ans = max(ans, cal(i)); printf("%d\n", ans); return 0; }
3
#include <stdio.h> #include <algorithm> #include <iostream> #include <stdlib.h> #include <string.h> #define rep(i, n) for(int i=0;i<n;i++) #define FOR(i, j, n) for(int i=j;i<n;i++) int N; char data[1000][1000]; int dp[1000][1000]; int search() { int max = 0; FOR(i, 1, N) { FOR(j, 1, N) { if(data[i][j] == '*') { dp[i][j] = 0; continue; } dp[i][j] = std::min({dp[i][j - 1], dp[i - 1][j - 1], dp[i - 1][j]}) + 1; max = dp[i][j] > max ? dp[i][j] : max; } } return max; } int main() { while(1) { std::cin >> N; if (N == 0) return 0; memset(dp, 0, sizeof(dp)); rep(i, N) { rep(j, N) std::cin >> data[i][j]; dp[i][0] = data[i][0] == '.' ? 1 : 0; dp[0][i] = data[0][i] == '.' ? 1 : 0; } std::cout << search() << std::endl; } return 0; }
0
#include <cstdio> using namespace std; int main(){ int n,x; scanf("%d%d",&n,&x); printf("%d\n",(n+x-1)/x); return 0; }
0
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; typedef long long ll; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll my_exp(ll a, ll b, ll MOD = numeric_limits<ll>::max()) { a %= MOD; return b ? (((b % 2 == 1 ? a : 1) * my_exp(a * a, b / 2, MOD)) % MOD) : 1; } bool is_prime(ll n) { if (n != 2 && n % 2 == 0) return false; else { for (ll i = 3; (i * i) <= n; i += 2) if (n % i == 0) return false; return true; } } int main() { ll n; cin >> n; vector<ll> v(n), d(n), p(n); for (ll i = 0; i < n; ++i) cin >> v[i] >> d[i] >> p[i]; list<ll> con; for (ll i = 0; i < n; ++i) { con.push_back(i); } set<ll> ans; while (!con.empty()) { ll curr = con.front(); con.pop_front(); ans.insert(curr + 1); ll red = v[curr]; for (decltype(con.begin()) it = con.begin(); it != con.end(); it++) { if (red > 0) { p[*it] -= red--; } else { break; } } bool neg = true; while (neg) { bool found = false; ll damage = 0; auto it = con.begin(); while (it != con.end()) { if (p[*it] < 0 && !found) { damage = d[*it]; found = true; it = con.erase(it); } else { p[*it] -= damage; ++it; } } if (!found) { neg = false; } } } cout << ans.size() << endl; for (decltype(ans.begin()) it = ans.begin(); it != ans.end(); it++) { cout << *it << " "; } return (0); }
1
#include <bits/stdc++.h> using namespace std; int main() { long long int n, m, i, count, j, flag; char ch; cin >> n >> m; count = 0; while (n--) { j = 0; while (j < m) { i = 0; flag = 1; while (i < 2) { cin >> ch; if (ch == '1' && flag) { flag = 0; count++; } i++; } j++; } } cout << count; return 0; }
1
#include <bits/stdc++.h> using namespace std; char p[1100][1100]; bool used[1100][1100], now[1100][1100], f = false; int kl[1100][1100], kol; int n, m, ans; int dfs(int, int); int main() { cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> p[i][j]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { if (!used[i][j] && p[i][j] == 'D') kol = dfs(i, j); if (kol > ans) ans = kol; } if (!f) { if (ans == 0) cout << "Poor Dima!"; else cout << ans; } else cout << "Poor Inna!"; cin >> n; return 0; } int dfs(int x, int y) { if (now[x][y]) f = true; now[x][y] = true; if (f) return 0; if (used[x][y]) { now[x][y] = false; return kl[x][y]; } used[x][y] = true; int k = 0; if (p[x][y] == 'D') { if (p[x][y - 1] == 'I') k = dfs(x, y - 1); if (k > kl[x][y]) kl[x][y] = k; if (p[x - 1][y] == 'I') k = dfs(x - 1, y); if (k > kl[x][y]) kl[x][y] = k; if (p[x + 1][y] == 'I') k = dfs(x + 1, y); if (k > kl[x][y]) kl[x][y] = k; if (p[x][y + 1] == 'I') k = dfs(x, y + 1); if (k > kl[x][y]) kl[x][y] = k; now[x][y] = false; return kl[x][y]; } if (p[x][y] == 'I') { if (p[x][y - 1] == 'M') k = dfs(x, y - 1); if (k > kl[x][y]) kl[x][y] = k; if (p[x - 1][y] == 'M') k = dfs(x - 1, y); if (k > kl[x][y]) kl[x][y] = k; if (p[x + 1][y] == 'M') k = dfs(x + 1, y); if (k > kl[x][y]) kl[x][y] = k; if (p[x][y + 1] == 'M') k = dfs(x, y + 1); if (k > kl[x][y]) kl[x][y] = k; now[x][y] = false; return kl[x][y]; } if (p[x][y] == 'M') { if (p[x][y - 1] == 'A') k = dfs(x, y - 1); if (k > kl[x][y]) kl[x][y] = k; if (p[x - 1][y] == 'A') k = dfs(x - 1, y); if (k > kl[x][y]) kl[x][y] = k; if (p[x + 1][y] == 'A') k = dfs(x + 1, y); if (k > kl[x][y]) kl[x][y] = k; if (p[x][y + 1] == 'A') k = dfs(x, y + 1); if (k > kl[x][y]) kl[x][y] = k; now[x][y] = false; return kl[x][y]; } if (p[x][y] == 'A') { if (p[x][y - 1] == 'D') k = dfs(x, y - 1); if (k > kl[x][y]) kl[x][y] = k; if (p[x - 1][y] == 'D') k = dfs(x - 1, y); if (k > kl[x][y]) kl[x][y] = k; if (p[x + 1][y] == 'D') k = dfs(x + 1, y); if (k > kl[x][y]) kl[x][y] = k; if (p[x][y + 1] == 'D') k = dfs(x, y + 1); if (k > kl[x][y]) kl[x][y] = k; kl[x][y]++; now[x][y] = false; return kl[x][y]; } }
3
#include <bits/stdc++.h> using namespace std; const int M = 3000001; const int P = 1e9 + 7; int n, m, k, b[M], d[M], pre[M], tag[M], maxi, res, sz, SZ, c[M], CT; struct vv { int si, so, res, cnt; } a[M]; bool cmp(vv a, vv b) { return a.si < b.si; } void pd(int now) { d[now * 2] += tag[now]; d[now * 2 + 1] += tag[now]; tag[now * 2] += tag[now]; tag[now * 2 + 1] += tag[now]; tag[now] = 0; } void ins(int now, int l, int r, int x, int k, int s) { if (l == r) { if (d[now] == k) c[now] = (c[now] + s) % P; if (d[now] > k) d[now] = k, c[now] = s; return; } int mid = (l + r) >> 1; if (tag[now]) pd(now); if (x <= mid) ins(now * 2, l, mid, x, k, s); else ins(now * 2 + 1, mid + 1, r, x, k, s); if (d[now * 2] == d[now * 2 + 1]) c[now] = (c[now * 2] + c[now * 2 + 1]) % P; else c[now] = d[now * 2] < d[now * 2 + 1] ? c[now * 2] : c[now * 2 + 1]; c[now] %= P; d[now] = min(d[now * 2], d[now * 2 + 1]); } void ask(int now, int l, int r, int R) { if (r <= R) { if (d[now] == SZ) CT += c[now]; if (d[now] < SZ) SZ = d[now], CT = c[now]; CT %= P; return; } int mid = (l + r) >> 1; pd(now); ask(now * 2, l, mid, R); if (mid < R) ask(now * 2 + 1, mid + 1, r, R); return; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%d%d", &a[i].so, &a[i].si); b[++k] = a[i].si; b[++k] = a[i].so; } sort(b + 1, b + 1 + n + n); m = unique(b + 1, b + 1 + n + n) - b - 1; memset(d, 0x3f, sizeof(d)); for (int i = 1; i <= n; i++) { k = lower_bound(b + 1, b + 1 + m, a[i].si) - b; pre[k] = a[i].si; a[i].si = k; k = lower_bound(b + 1, b + 1 + m, a[i].so) - b; pre[k] = a[i].so; a[i].so = k; maxi = max(maxi, a[i].si); ins(1, 0, m, a[i].so, pre[a[i].si] - pre[a[i].so], 1); } ins(1, 0, m, 0, 0, 1); sort(a + 1, a + 1 + n, cmp); for (int i = 1; i <= n; i++) { SZ = 0x3f3f3f3f; CT = 0; tag[1] += pre[a[i].si] - pre[a[i - 1].si]; ask(1, 0, m, a[i].si); a[i].res = SZ; a[i].cnt = CT; if (a[i].res != pre[a[i].si]) ins(1, 0, m, a[i].so, a[i].res + pre[a[i].si] - pre[a[i].so], CT); } sz = 0x3f3f3f3f; for (int i = 1; i <= n; i++) if (a[i].so > maxi) { if (a[i].res == sz) (res += a[i].cnt) %= P; if (a[i].res < sz) sz = a[i].res, res = a[i].cnt; } printf("%d", res); }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, L, a; while (~scanf("%d%d%d", &n, &L, &a)) { int t, l; int ans = 0; int last = 0; for (int i = 0; i < n; i++) { scanf("%d%d", &t, &l); ans += (t - last) / a; last = t + l; } ans += (L - last) / a; printf("%d\n", ans); } return 0; }
1
#include<bits/stdc++.h> using namespace std ; #define Next( i, x ) for( register int i = head[x]; i; i = e[i].next ) #define rep( i, s, t ) for( register int i = (s); i <= (t); ++ i ) #define drep( i, s, t ) for( register int i = (t); i >= (s); -- i ) #define re register #define int long long int gi() { char cc = getchar() ; int cn = 0, flus = 1 ; while( cc < '0' || cc > '9' ) { if( cc == '-' ) flus = - flus ; cc = getchar() ; } while( cc >= '0' && cc <= '9' ) cn = cn * 10 + cc - '0', cc = getchar() ; return cn * flus ; } const int N = 8e5 + 5 ; const int P = 200003 ; int n, m, R[N], g[N], ord[N], fc[N], f[N], c[N], tt[N], limit, L, num, cnt, ans[N] ; void Init( int x ) { limit = 1, L = 0 ; while( limit <= x ) limit <<= 1, ++ L ; rep( i, 0, limit ) R[i] = ( R[i >> 1] >> 1 ) | ( ( i & 1 ) << ( L - 1 ) ) ; } const double Pi = acos( -1.0 ) ; struct Complex { double x, y ; Complex( double xx = 0, double yy = 0 ) { x = xx, y = yy ; } } F[N], Q[N], C[N], A1[N], A2[N], B1[N], B2[N] ; Complex operator * ( Complex x, Complex y ) { return Complex( x.x * y.x - x.y * y.y, x.x * y.y + x.y * y.x ) ; } Complex operator + ( Complex x, Complex y ) { return Complex( x.x + y.x, x.y + y.y ) ; } Complex operator - ( Complex x, Complex y ) { return Complex( x.x - y.x, x.y - y.y ) ; } Complex operator / ( Complex x, double y ) { return Complex(x.x / y, x.y / y ) ; } void FFT( Complex *a, int type ) { for( int i = 0; i < limit; ++ i ) if( R[i] > i ) swap( a[R[i]], a[i] ) ; for( int k = 1; k < limit; k <<= 1 ) { Complex Dw = Complex( cos( Pi / k ), type * sin( Pi / k ) ); for( int i = 0; i < limit; i += ( k << 1 ) ) { Complex w = Complex( 1, 0 ) ; for( int j = i; j < i + k; ++ j, w = w * Dw ) { Complex Ny = w * a[j + k], Nx = a[j] ; a[j + k] = Nx - Ny, a[j] = Nx + Ny ; } } } if( type != 1 ) rep( i, 0, limit ) a[i] = a[i] / (double)limit ; } signed main() { n = gi() ; rep( i, 1, n ) c[i] = gi() ; int gg = 2, ff = 1 ; cnt = 400 ; for( re int i = 0; i < P - 1; ++ i ) { ord[ff] = i, fc[i] = ff, ff = ff * 2 % P ; } int Ans = 0 ; rep( i, 1, n ) if(c[i]) tt[ord[c[i]]] ++, Ans -= c[i] * c[i] % P ; rep( i, 0, P ) A1[i].x = tt[i] / cnt, A2[i].x = tt[i] % cnt ; rep( i, 0, P ) B1[i].x = tt[i] / cnt, B2[i].x = tt[i] % cnt ; Init( 2 * P + 5 ), FFT( A1, 1 ), FFT( A2, 1 ), FFT( B1, 1 ), FFT( B2, 1 ) ; num = cnt * cnt % P ; rep( i, 0, limit ) F[i] = A1[i] * B1[i], Q[i] = ( A1[i] * B2[i] + A2[i] * B1[i] ), C[i] = A2[i] * B2[i] ; FFT( F, -1 ), FFT( Q, -1 ), FFT( C, -1 ) ; rep( i, 0, limit ) { ans[i] = ( (int)(F[i].x + 0.5) * num + ( (int)(Q[i].x + 0.5) * cnt ) + (int)(C[i].x + 0.5) ) ; Ans += ans[i] * fc[i % (P - 1)] ; } cout << Ans / 2 << endl ; return 0; }
0