solution
stringlengths
52
181k
difficulty
int64
0
6
// need #include <iostream> #include <algorithm> // data structure #include <bitset> #include <map> #include <queue> #include <set> #include <stack> #include <string> #include <utility> #include <vector> //#include <complex> //#include <deque> #include <valarray> // stream //#include <istream> //#include <sstream> //#include <ostream> #include <fstream> // etc #include <cassert> #include <cmath> #include <functional> #include <iomanip> #include <chrono> #include <random> #include <numeric> // input #define INIT std::ios::sync_with_stdio(false);std::cin.tie(0); #define VAR(type, ...)type __VA_ARGS__;MACRO_VAR_Scan(__VA_ARGS__); template<typename T> void MACRO_VAR_Scan(T& t) { std::cin >> t; } template<typename First, typename...Rest>void MACRO_VAR_Scan(First& first, Rest&...rest) { std::cin >> first; MACRO_VAR_Scan(rest...); } #define VEC_ROW(type, n, ...)std::vector<type> __VA_ARGS__;MACRO_VEC_ROW_Init(n, __VA_ARGS__); for(int i=0; i<n; ++i){MACRO_VEC_ROW_Scan(i, __VA_ARGS__);} template<typename T> void MACRO_VEC_ROW_Init(int n, T& t) { t.resize(n); } template<typename First, typename...Rest>void MACRO_VEC_ROW_Init(int n, First& first, Rest&...rest) { first.resize(n); MACRO_VEC_ROW_Init(n, rest...); } template<typename T> void MACRO_VEC_ROW_Scan(int p, T& t) { std::cin >> t[p]; } template<typename First, typename...Rest>void MACRO_VEC_ROW_Scan(int p, First& first, Rest&...rest) { std::cin >> first[p]; MACRO_VEC_ROW_Scan(p, rest...); } #define VEC(type, c, n) std::vector<type> c(n);for(auto& i:c)std::cin>>i; #define MAT(type, c, m, n) std::vector<std::vector<type>> c(m, std::vector<type>(n));for(auto& r:c)for(auto& i:r)std::cin>>i; // output #define OUT(d) std::cout<<(d); #define FOUT(n, d) std::cout<<std::fixed<<std::setprecision(n)<<(d); #define SOUT(n, c, d) std::cout<<std::setw(n)<<std::setfill(c)<<(d); #define SP std::cout<<" "; #define TAB std::cout<<"\t"; #define BR std::cout<<"\n"; #define SPBR(i, n) std::cout<<(i + 1 == n ? '\n' : ' '); #define ENDL std::cout<<std::endl; #define FLUSH std::cout<<std::flush; #define SHOW(d) {std::cerr << #d << "\t:" << (d) << "\n";} #define SHOWVECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";} #define SHOWVECTOR2(v) {std::cerr << #v << "\t:\n";for(const auto& xxx : v){for(const auto& yyy : xxx){std::cerr << yyy << " ";}std::cerr << "\n";}} #define SHOWQUEUE(a) {auto tmp(a);std::cerr << #a << "\t:";while(!tmp.empty()){std::cerr << tmp.front() << " ";tmp.pop();}std::cerr << "\n";} // utility #define ALL(a) (a).begin(),(a).end() #define FOR(i, a, b) for(int i=(a);i<(b);++i) #define RFOR(i, a, b) for(int i=(b)-1;i>=(a);--i) #define REP(i, n) for(int i=0;i<int(n);++i) #define RREP(i, n) for(int i=int(n)-1;i>=0;--i) #define FORLL(i, a, b) for(ll i=ll(a);i<ll(b);++i) #define RFORLL(i, a, b) for(ll i=ll(b)-1;i>=ll(a);--i) #define REPLL(i, n) for(ll i=0;i<ll(n);++i) #define RREPLL(i, n) for(ll i=ll(n)-1;i>=0;--i) #define IN(a, x, b) (a<=x && x<b) template<typename T> inline T CHMAX(T& a, const T b) { return a = (a < b) ? b : a; } template<typename T> inline T CHMIN(T& a, const T b) { return a = (a > b) ? b : a; } #define EXCEPTION(msg) throw std::string("Exception : " msg " [ in ") + __func__ + " : " + std::to_string(__LINE__) + " lines ]" #define TRY(cond, msg) try {if (cond) EXCEPTION(msg);}catch (std::string s) {std::cerr << s << std::endl;} void CHECKTIME(std::function<void()> f) { auto start = std::chrono::system_clock::now(); f(); auto end = std::chrono::system_clock::now(); auto res = std::chrono::duration_cast<std::chrono::nanoseconds>((end - start)).count(); std::cerr << "[Time:" << res << "ns (" << res / (1.0e9) << "s)]\n"; } // test template<class T> std::vector<std::vector<T>> VV(int n, int m, T init = T()) { return std::vector<std::vector<T>>(n, std::vector<T>(m, init)); } template<typename S, typename T> std::ostream& operator<<(std::ostream& os, std::pair<S, T> p) { os << "(" << p.first << ", " << p.second << ")"; return os; } // type/const #define int ll using ll = long long; using ull = unsigned long long; using ld = long double; using PAIR = std::pair<int, int>; using PAIRLL = std::pair<ll, ll>; constexpr int INFINT = 1 << 30; // 1.07x10^ 9 constexpr int INFINT_LIM = (1LL << 31) - 1; // 2.15x10^ 9 constexpr ll INFLL = 1LL << 60; // 1.15x10^18 constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); // 9.22x10^18 constexpr double EPS = 1e-9; constexpr int MOD = 1000000007; constexpr double PI = 3.141592653589793238462643383279; template<class T, size_t N> void FILL(T(&a)[N], const T& val) { for (auto& x : a) x = val; } template<class ARY, size_t N, size_t M, class T> void FILL(ARY(&a)[N][M], const T& val) { for (auto& b : a) FILL(b, val); } template<class T> void FILL(std::vector<T>& a, const T& val) { for (auto& x : a) x = val; } template<class ARY, class T> void FILL(std::vector<std::vector<ARY>>& a, const T& val) { for (auto& b : a) FILL(b, val); } // ------------>8------------------------------------->8------------ double dp[2][502][502]; // dp[i][j][k] = i区画まで見てj区画選んでいて最後の区画の長さがkの時の総和 signed main() { INIT; VAR(int, n, m); VEC(int, a, n); std::vector<int> c(n + 1, 0); REP(i, n) c[i + 1] = c[i] + a[i]; auto calc = [&](int l, int r) { return 1. * (c[r] - c[l]) / (r - l); }; FILL(dp, -1.); dp[0][0][0] = 0; dp[1][1][1] = a[0]; FOR(i, 1, n) { int cur = i % 2, nex = (i + 1) % 2; FOR(j, 1, i + 1) FOR(k, 1, i + 1) { if (dp[cur][j][k] < 0) continue; // 追加 CHMAX(dp[nex][j + 1][1], dp[cur][j][k] + a[i]); // くっつける CHMAX(dp[nex][j][k + 1], dp[cur][j][k] + calc(i - k, i + 1) - calc(i - k, i)); } FILL(dp[cur], -1.); } double ans = -1; REP(k, n + 1) { CHMAX(ans, dp[n%2][m][k]); } FOUT(12, ans)BR; return 0; }
0
#include <bits/stdc++.h> using namespace std; void test_case() { long long n, m; cin >> n >> m; long long mn = m, mx = m, time = 0, flag = 0; long long t, l, h; for (int i = 0; i < n; i++) { cin >> t >> l >> h; long long d = t - time; mn -= d, mx += d; if (mx < l || mn > h) { flag = 1; } time = t; mn = max(mn, l); mx = min(mx, h); } if (flag) cout << "NO\n"; else cout << "YES\n"; } int main() { int t; cin >> t; while (t--) test_case(); }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); int k[n]; for (int i = 0; i < n; i++) { scanf("%d", &k[i]); } int mi = 1e9; for (int i = 0; i < n; i++) { int t = 15 * k[i]; for (int j = 0, a; j < k[i]; j++) { scanf("%d", &a); t += a * 5; } if (mi > t) { mi = t; } } printf("%d", mi); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long int n, digit = 0, tmp, nine = 0, one = 0, i, ones = 1, res = 0; cin >> n; tmp = n; while (tmp) { digit++; tmp = tmp / 10; } if (digit - 1 > 0) { for (long long int i = 1; i <= digit - 1; i++) { nine = nine + 9; res += nine * ones; one = one + (9 * ones); ones = ones * 10; } } res += (n - one) * digit; cout << res << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; int data[9][9] = {{0, 0, 1, 0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 0, 0, 1, 1, 1}, {1, 1, 0, 0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 1, 1, 0, 0, 1}, {0, 0, 0, 1, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 0, 0, 1, 1, 1}, {1, 1, 0, 0, 0, 0, 0, 0, 1}, {0, 0, 0, 0, 1, 1, 0, 0, 1}, {0, 0, 0, 0, 0, 0, 0, 0, 1}}; void cheng(int a[][9], int b[][9], int c[][9]) { for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) { a[i][j] = 0; for (int k = 0; k < 9; k++) { a[i][j] += ((long long)b[i][k]) * c[k][j] % 1000000007; a[i][j] %= 1000000007; } } } int compute(int K) { if (K < 2) return 0; K -= 1; int g[9][9], now[9][9], tmp[9][9]; for (int i = 0; i < 9; i++) for (int j = 0; j < 9; j++) now[i][j] = i == j; memcpy(g, data, sizeof(g)); while (K) { if (K & 1) { cheng(tmp, now, g); memcpy(now, tmp, sizeof(now)); } K >>= 1; cheng(tmp, g, g); memcpy(g, tmp, sizeof(tmp)); } memcpy(g, data, sizeof(g)); cheng(tmp, g, g); memcpy(g, tmp, sizeof(tmp)); int ans = 0; for (int i = 0; i < 8; i++) ans = (ans + now[i][8]) % 1000000007; return ans; } int solve(int K) { if (K == 0) return 0; if (K == 1) return 4; int g, h; g = compute(K); h = compute((K + 1) / 2); return ((g - h) * 500000004ll + h + 4) % 1000000007; } int main() { int l, r; scanf("%d%d", &l, &r); r = solve(r); l = solve(l - 1); int ans = ((r - l) % 1000000007 + 1000000007) % 1000000007; printf("%d\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, cycle1, cycle2, cur, H[(int)(2e5 + 10)], p[(int)(2e5 + 10)]; bool cift; vector<int> ans[2]; int main() { cin >> n; for (int i = 1; i <= n; i++) scanf("%d", &p[i]); for (int i = 1; i <= n; i++) { if (p[i] == i) cycle1 = i; if (p[p[i]] == i) cycle2 = i; } if (!cycle1 and !cycle2) return puts("NO"), 0; if (cycle1) { puts("YES"); for (int i = 1; i <= n; i++) if (i != cycle1) printf("%d %d\n", cycle1, i); return 0; } H[cycle2] = H[p[cycle2]] = 1; for (int i = 1; i <= n; i++) if (!H[i]) { H[i] = 1; cur = i; cift = true; while (true) { H[cur] = 1; if (cift) ans[0].push_back(cur); else ans[1].push_back(cur); cur = p[cur]; cift = !cift; if (cur == i) break; } if (!cift) return puts("NO"), 0; } puts("YES"); cout << cycle2 << ' ' << p[cycle2] << '\n'; for (__typeof(ans[0].begin()) it = ans[0].begin(); it != ans[0].end(); it++) printf("%d %d\n", cycle2, *it); for (__typeof(ans[1].begin()) it = ans[1].begin(); it != ans[1].end(); it++) printf("%d %d\n", p[cycle2], *it); }
2
#include <bits/stdc++.h> using namespace std; long long f(long long g, long long c1, long long c2, long long ones, long long zeros) { long long p = (ones + zeros) / g; long long r = (ones + zeros) % g; long long sum = 0; for (long long i = 0; i < g; i++) { if (i < r) sum += c1 + c2 * p * p; else sum += c1 + c2 * (p - 1) * (p - 1); } return sum; } int main() { long long n, c1, c2; cin >> n >> c1 >> c2; string s; cin >> s; long long ones = 0, zeros = 0; for (int i = 0; i < n; i++) { if (s[i] == '1') ones++; else zeros++; } long long le = 1, ri = ones; long long ans = c1 + c2 * (n - 1) * (n - 1); while (le + 2 < ri) { long long m1 = le + (ri - le) / 3, m2 = le + 2 * (ri - le) / 3; long long fm1 = f(m1, c1, c2, ones, zeros), fm2 = f(m2, c1, c2, ones, zeros); if (fm1 < fm2) ri = m2 - 1; else if (fm1 > fm2) le = m1 + 1; else { le = m1; ri = m2; } } for (long long k = le; k <= ri; k++) ans = min(ans, f(k, c1, c2, ones, zeros)); cout << ans; return 0; }
1
#include <bits/stdc++.h> using namespace std; class DNashMatrix { vector<vector<char> > grid; vector<vector<pair<int, int> > > graph; int n; bool isIn(int r, int c) { return (r >= 0 && c >= 0 && r < n && c < n); } bool BFS(pair<int, int> src) { int X = src.first; int Y = src.second; if (grid[X][Y] != 'X' && grid[X][Y] != 'N') return false; grid[X][Y] = 'X'; queue<pair<int, int> > Q; Q.push(src); while (!Q.empty()) { int x = Q.front().first; int y = Q.front().second; pair<int, int> top = Q.front(); Q.pop(); x++; if (isIn(x, y)) { if (graph[x][y] == graph[X][Y] && grid[x][y] == 'N') { grid[x][y] = 'U'; Q.push({x, y}); } } x--; x--; if (isIn(x, y)) { if (graph[x][y] == graph[X][Y] && grid[x][y] == 'N') { grid[x][y] = 'D'; Q.push({x, y}); } } x++; y++; if (isIn(x, y)) { if (graph[x][y] == graph[X][Y] && grid[x][y] == 'N') { grid[x][y] = 'L'; Q.push({x, y}); } } y--; y--; if (isIn(x, y)) { if (graph[x][y] == graph[X][Y] && grid[x][y] == 'N') { grid[x][y] = 'R'; Q.push({x, y}); } } } return true; } void BFSinv(pair<int, int> src) { int X = src.first; int Y = src.second; queue<pair<int, int> > Q; Q.push(src); bool flag = true; while (!Q.empty()) { int x = Q.front().first; int y = Q.front().second; pair<int, int> top = Q.front(); Q.pop(); x++; if (isIn(x, y)) { if (graph[x][y] == graph[X][Y] && grid[x][y] == 'N') { if (flag) { grid[x][y] = 'U'; grid[X][Y] = 'D'; flag = false; Q.push({x, y}); } else { grid[x][y] = 'U'; Q.push({x, y}); } } } x--; x--; if (isIn(x, y)) { if (graph[x][y] == graph[X][Y] && grid[x][y] == 'N') { if (flag) { grid[x][y] = 'D'; grid[X][Y] = 'U'; flag = false; Q.push({x, y}); } else { grid[x][y] = 'D'; Q.push({x, y}); } } } x++; y++; if (isIn(x, y)) { if (graph[x][y] == graph[X][Y] && grid[x][y] == 'N') { if (flag) { grid[x][y] = 'L'; grid[X][Y] = 'R'; flag = false; Q.push({x, y}); } else { grid[x][y] = 'L'; Q.push({x, y}); } } } y--; y--; if (isIn(x, y)) { if (graph[x][y] == graph[X][Y] && grid[x][y] == 'N') { if (flag) { grid[x][y] = 'R'; grid[X][Y] = 'L'; flag = false; Q.push({x, y}); } else { grid[x][y] = 'R'; Q.push({x, y}); } } } } } public: void solve(std::istream &in, std::ostream &out) { in >> n; grid.resize(n, vector<char>(n, 'N')); graph.resize(n, vector<pair<int, int> >(n)); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { in >> graph[i][j].first >> graph[i][j].second; graph[i][j].first--; graph[i][j].second--; } } bool flag = true; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (grid[i][j] == 'N') { if (graph[i][j].first == -2) BFSinv({i, j}); else flag &= BFS(graph[i][j]); } } } if (!flag) { out << "INVALID\n"; return; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { flag &= (grid[i][j] != 'N'); } } if (!flag) { out << "INVALID\n"; return; } out << "VALID\n"; for (auto t : grid) { for (char x : t) out << x; out << "\n"; } } }; int main() { ios_base::sync_with_stdio(false); DNashMatrix solver; std::istream &in(std::cin); std::ostream &out(std::cout); solver.solve(in, out); return 0; }
4
#include<bits/stdc++.h> using namespace std; int main(){ string s; while(cin>>s){ if(s.length()==3) reverse(s.begin(),s.end()); cout<<s<<endl; } }
0
#include <iostream> #include <string> using namespace std; int main() { string a, b; int n, t = 0, h = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> a >> b; a == b ? t++, h++ : (a > b ? t += 3 : h += 3); } cout << t << " " << h << endl; }
0
#include <bits/stdc++.h> using namespace std; int a, b, c, d, eq; map<pair<int, int>, int> mp; int main() { cin >> c >> d >> a >> b; if (a == b) eq = 1; int now = 1; while (a != 0 || b != 0) { mp[{a, b}] = now; if (a > 0 && b > 0) a--, b--; else if (a > 0) a--; else b--; now++; } mp[{a, b}] = now; now = 1; int f = c, s = d; while (c > 0 || d > 0) { if (mp[{c, d}] >= now) { cout << "Polycarp"; return 0; } if (eq) { if (c > d) c--; else d--; } else if (c > 0) c--; else d--; now++; } if (mp[{c, d}] >= now) { cout << "Polycarp"; return 0; } now = 1; c = f, d = s; while (c > 0 || d > 0) { if (mp[{c, d}] >= now) { cout << "Polycarp"; return 0; } if (eq) { if (c > d) c--; else d--; } else if (d > 0) d--; else c--; now++; } if (mp[{c, d}] >= now) { cout << "Polycarp"; return 0; } now = 1; c = f, d = s; while (c > 0 || d > 0) { if (mp[{c, d}] >= now) { cout << "Polycarp"; return 0; } if (eq) { if (c >= d) c--; else d--; } else if (c > 0) c--; else d--; now++; } if (mp[{c, d}] >= now) { cout << "Polycarp"; return 0; } now = 1; c = f, d = s; while (c > 0 || d > 0) { if (mp[{c, d}] >= now) { cout << "Polycarp"; return 0; } if (eq) { if (c >= d) c--; else d--; } else if (d > 0) d--; else c--; now++; } if (mp[{c, d}] >= now) { cout << "Polycarp"; return 0; } cout << "Vasiliy"; }
3
#include <bits/stdc++.h> using namespace std; long long n, a[101], d = 0, i, m, z, t = 0; int main() { cin >> n; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (i = 1; i < n; i++) { if (a[i] > a[i - 1]) { d = a[i] - a[i - 1]; m = a[i]; break; } } for (i = n - 2; i >= 0; i--) { if (a[i] < a[i + 1]) { t = a[i + 1] - a[i]; z = a[i]; break; } } if (t == d && z == m) d = d; else if (t == d && z + d == m) { if (d % 2 == 0) d /= 2; } else d = -1; cout << d; }
2
#include <bits/stdc++.h> using namespace std; template <typename... Args> inline void unused_args(Args &&...args) { (void)(sizeof...(args)); } int main() { std::ios_base::sync_with_stdio(false); std::streamsize ss = cout.precision(); unused_args(ss); long long n, k; cin >> n >> k; long long ans = k % n ? k / n + 1 : k / n; cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, a[1007]; bool check() { for (int i = 1; i < n; ++i) if (a[i] && a[i + 1]) return 0; return 1; } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%1d", &a[i]); if (!check()) printf("No"); else { int ok = 1; for (int i = 1; i <= n; ++i) if (!a[i]) { a[i] = 1; if (check()) ok = 0; a[i] = 0; } if (ok) printf("Yes"); else printf("No"); } }
1
#include <bits/stdc++.h> #define io ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define ll long long #define ull unsigned long long #define pii pair<ll, ll> #define piii pair<ll, pair<ll, ll>> #define debug(x) cout<<#x<<" = "<<x<<endl using namespace std; const int N = 1000006; ll t, n, m, q, a, b, x, y, k, ans, arr[N]; ll mod = 1e9 + 7; string s; int main() { io; t = 1; cin >> t; while (t--) { cin >> n >> m >> s; m = min(m, n); while (m--) { string tmp = s; for (int i = 0; i < n; ++i) { if (s[i] == '1') { tmp[i] = '1'; } else { bool lft = (i == 0) || (s[i - 1] == '0'); bool ryt = (i == n - 1) || (s[i + 1] == '0'); if ((lft && !ryt) || (ryt && !lft)) { tmp[i] = '1'; } } } s = tmp; // cout << " : " << s << endl; } // for (auto &x : ans) cout << x; cout << s << endl; } return 0; } // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout);
1
#include <bits/stdc++.h> using namespace std; int arr[101]; bool inside[101]; vector<int> factors[101]; vector<pair<int, int> > edgesIndexes[101]; int pairs[100000], dist[100000]; vector<int> G[100000]; vector<int> L1, L2; void factorize(int n, int index) { for (int i = 2; i * i <= n; i++) { while (n % i == 0) { n /= i; factors[index].push_back(i); } } if (n != 1) factors[index].push_back(n); } int m1[100000], m2[100000]; bool vis[100000]; bool dfs(int u) { if (u < 0) return true; if (vis[u]) return false; else vis[u] = true; for (int v : G[u]) if (dfs(m2[v])) { m1[u] = v; m2[v] = u; return true; } return false; } int matching() { int n1 = L1.size(); int n2 = L2.size(); for (int i = 0; i < n1; i++) m1[L1[i]] = -1; for (int i = 0; i < n2; i++) m2[L2[i]] = -1; bool changed; do { changed = 0; for (int i = 0; i < n1; i++) vis[L1[i]] = false; for (int i = 0; i < n1; i++) if (m1[L1[i]] < 0) changed |= dfs(L1[i]); } while (changed); int siz = 0; for (int i = 0; i < n1; i++) siz += (m1[L1[i]] != -1); return siz; } int main() { memset(pairs, -1, 100000); int n, m; scanf("%d%d", &n, &m); for (int i = 1; i < n + 1; i++) { scanf("%d", &arr[i]); } int lastIndex = 0; for (int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); if (!inside[a]) { inside[a] = true; factorize(arr[a], a); for (int i = 0; i < factors[a].size(); i++) { if (a % 2) L1.push_back(lastIndex); else L2.push_back(lastIndex); edgesIndexes[a].push_back(make_pair(lastIndex++, factors[a][i])); } } if (!inside[b]) { inside[b] = true; factorize(arr[b], b); for (int i = 0; i < factors[b].size(); i++) { if (b % 2) L1.push_back(lastIndex); else L2.push_back(lastIndex); edgesIndexes[b].push_back(make_pair(lastIndex++, factors[b][i])); } } for (int i = 0; i < edgesIndexes[a].size(); i++) { for (int j = 0; j < edgesIndexes[b].size(); j++) { if (edgesIndexes[a][i].second == edgesIndexes[b][j].second) { G[edgesIndexes[a][i].first].push_back(edgesIndexes[b][j].first); G[edgesIndexes[b][j].first].push_back(edgesIndexes[a][i].first); } } } } printf("%d\n", matching()); return 0; }
3
#include <iostream> using namespace std; int main(){ int a,sum=100000,b; cin >>a; for(int i=0;i<a;i++){ b=sum/20; if(b%1000==0)sum+=b; else sum+=(b/1000+1)*1000; } cout <<sum<<endl; return 0; }
0
#include<bits/stdc++.h> using namespace std; typedef long long ll; const int MN = 300010; struct CHT { vector<ll> M, B; void init() { M.clear(); B.clear(); } bool bad(int l1, int l2, int l3) { return (M[l1] - M[l2]) * (B[l3] - B[l1]) <= (M[l1] - M[l3]) * (B[l2] - B[l1]); } void add(ll m, ll b) { M.push_back(m); B.push_back(b); while(M.size() >= 3 && bad(M.size() - 3, M.size() - 2, M.size() - 1)) { M.erase(M.end() - 2); B.erase(B.end() - 2); } if(M.size() == 2 && M[0] == M[1]) { M.erase(M.end() - 2); B.erase(B.end() - 2); } } ll query(ll x) { if(M.size() == 0) return 1e18; int s = 0, e = (int)M.size() - 2, p = 0; while(s <= e) { int m = (s + e)>>1; if((M[m] - M[m + 1]) * x >= B[m + 1] - B[m]) { p = m + 1; s = m + 1; } else e = m - 1; } return M[p] * x + B[p]; } } cht; int N, M; ll T[MN], psum[MN], dp1[MN], dp2[MN], mx[2][MN], ans[MN]; void solve(int l, int r, int x, ll *dp1, ll *dp2) { if(l > r) return; int m = (l + r + x)>>1; solve(l, m - 1, x, dp1, dp2); solve(m + 1, r, x, dp1, dp2); cht.init(); cht.add(2 * (r + 1), - 1LL * (r + 1) * (r + 1) - (r + 1) + psum[r]); for(int i = r; i >= m + 1; i--) { cht.add(2 * i, - 1LL * i * i - i + (i? psum[i - 1] : 0) - dp1[i]); } ll tmx = -1e18; for(int i = l; i <= m; i++) { ll t = 1LL * i * i - i + (i? psum[i - 1] : 0) - cht.query(i) + (i? dp2[i - 1] : 0); tmx = max(tmx, t); mx[x][i] = max(mx[x][i], tmx); } } int main() { scanf("%d", &N); for(int i = 0; i < N; i++) { scanf("%lld", &T[i]); T[i] *= 2; } for(int i = 0; i < N; i++) { psum[i] = T[i]; if(i) psum[i] += psum[i - 1]; } cht.init(); cht.add(2 * N, - 1LL * N * N - N + psum[N - 1]); for(int i = N - 1; i >= 0; i--) { dp1[i] = 1LL * i * i - i + (i? psum[i - 1] : 0) - cht.query(i); dp1[i] = max(dp1[i], dp1[i + 1]); cht.add(2 * i, - 1LL * i * i - i + (i? psum[i - 1] : 0) - dp1[i]); } reverse(T, T + N); for(int i = 0; i < N; i++) { psum[i] = T[i]; if(i) psum[i] += psum[i - 1]; } cht.init(); cht.add(2 * N, - 1LL * N * N - N + psum[N - 1]); for(int i = N - 1; i >= 0; i--) { dp2[i] = 1LL * i * i - i + (i? psum[i - 1] : 0) - cht.query(i); dp2[i] = max(dp2[i], dp2[i + 1]); cht.add(2 * i, - 1LL * i * i - i + (i? psum[i - 1] : 0) - dp2[i]); } for(int i = 0; i < N; i++) { mx[0][i] = mx[1][i] = -1e18; } reverse(dp1, dp1 + N); solve(0, N - 1, 1, dp2, dp1); reverse(T, T + N); reverse(dp1, dp1 + N); reverse(dp2, dp2 + N); for(int i = 0; i < N; i++) { psum[i] = T[i]; if(i) psum[i] += psum[i - 1]; } solve(0, N - 1, 0, dp1, dp2); scanf("%d", &M); for(int i = 0; i < M; i++) { int p, x; scanf("%d %d", &p, &x); p--; x *= 2; ll tmp = (p? dp2[p - 1] : 0) + dp1[p + 1]; tmp = max(tmp, max(mx[0][p], mx[1][N - 1 - p]) + T[p] - x); printf("%lld\n", tmp / 2); } }
0
#include <bits/stdc++.h> using namespace std; int n, m, h, t = -1, o; int main() { cin >> n >> m >> h; n--; h--; for (int i = 0; i < m; i++) { int s; cin >> s; t += s; if (i != h) o += s; } if (t < n) cout << -1.0 << endl; else if (o < n) cout << 1.0 << endl; else { double p = 1.0; for (int i = 0; i < n; i++) p *= (o - i) * 1.0 / (t - i); cout << setprecision(10) << 1.0 - p << endl; } }
2
#include <bits/stdc++.h> using namespace std; int a[105]; int b[105]; int main(void) { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", a + i); } for (int i = 0; i < n; i++) { scanf("%d", b + i); } int ascore = 0, bscore = 0; for (int i = 0; i < n; i++) { if (a[i] == 1 && b[i] == 0) ascore++; else if (a[i] == 0 && b[i] == 1) bscore++; } if (ascore == 0) { printf("%d", -1); return 0; } else { int ans = (bscore + 1) / ascore; if (ans * ascore != bscore + 1) ans++; printf("%d", ans); } }
1
#include <bits/stdc++.h> #define GET_MACRO(_1,_2,_3,_4,_5,_6,_7,_8,NAME,...) NAME #define pr(...) cout<< GET_MACRO(__VA_ARGS__,pr8,pr7,pr6,pr5,pr4,pr3,pr2,pr1)(__VA_ARGS__) <<endl #define pr1(a) (#a)<<"="<<(a)<<" " #define pr2(a,b) pr1(a)<<pr1(b) #define pr3(a,b,c) pr1(a)<<pr2(b,c) #define pr4(a,b,c,d) pr1(a)<<pr3(b,c,d) #define pr5(a,b,c,d,e) pr1(a)<<pr4(b,c,d,e) #define pr6(a,b,c,d,e,f) pr1(a)<<pr5(b,c,d,e,f) #define pr7(a,b,c,d,e,f,g) pr1(a)<<pr6(b,c,d,e,f,g) #define pr8(a,b,c,d,e,f,g,h) pr1(a)<<pr7(b,c,d,e,f,g,h) #define prArr(a) {cerr<<(#a)<<"={";int i=0;for(auto t:(a))cerr<<(i++?", ":"")<<t;cerr<<"}"<<endl;} using namespace std; using Int = long long; using _int = int; using ll = long long; using Double = long double; const Int INF = (1LL<<60)+1e9; // ~ 1.15 * 1e18 const Int mod = (1e9)+7; const Double EPS = 1e-8; const Double PI = 6.0 * asin((Double)0.5); using P = pair<Int,Int>; template<class T> T Max(T &a,T b){return a=max(a,b);} template<class T> T Min(T &a,T b){return a=min(a,b);} template<class T1, class T2> ostream& operator<<(ostream& o,pair<T1,T2> p){return o<<"("<<p.first<<","<<p.second<<")";} template<class T1, class T2, class T3> ostream& operator<<(ostream& o,tuple<T1,T2,T3> t){ return o<<"("<<get<0>(t)<<","<<get<1>(t)<<","<<get<2>(t)<<")";} template<class T1, class T2> istream& operator>>(istream& i,pair<T1,T2> &p){return i>>p.first>>p.second;} template<class T> ostream& operator<<(ostream& o,vector<T> a){Int i=0;for(T t:a)o<<(i++?" ":"")<<t;return o;} template<class T> istream& operator>>(istream& i,vector<T> &a){for(T &t:a)i>>t;return i;} class Lowlink{ public: int V; vector<vector<int> > G; vector<int> ord; //ord[i] := ノードiに訪れた順番 vector<int> low; //low[i] := ノードiから訪れることができる最小のord[j]の値。 vector<int> articulation; int ok; Lowlink():V(-1),ok(false){}; Lowlink(int V):V(V),G(V),ok(false){}; void add_edge(int a,int b){ ok = false; assert(a < V && b < V); assert(a >=0 && b >= 0); G[a].push_back(b); G[b].push_back(a); } void build(int root = 0){ ok = true; ord.clear(); ord.resize(V,-1); low.clear(); low.resize(V,-1); articulation.clear(); articulation.resize(V,0); /*関節点用*/ int cnt = 0; function<int(int,int)> dfs=[&](int pos,int pre){ ord[pos] = low[pos] = cnt++; for(int to:G[pos]) { if(to == pre) continue; if(ord[to] != -1) low[pos] = min(low[pos], ord[to]); else { low[pos] = min(low[pos], dfs(to, pos)); /*関節点用*/ if(pos == root) articulation[pos]++; else articulation[pos] |= ord[pos] <= low[to]; } } return low[pos]; }; dfs(root , -1); articulation[root] = articulation[root] >= 2; /*関節点用*/ } int isBridge(int a,int b){ assert(ok); assert(a < V && b < V); assert(a >= 0 && b >= 0); if(ord[a] > ord[b]) swap(a, b); return ord[a] < low[b]; } int isArticulation(int a){ assert(ok); assert(a >= 0 && a < V); return articulation[a]; } }; template<typename ctype, ctype INF> class Dijkstra{ public: typedef tuple<int,ctype> T; //for edge typedef tuple<ctype,int> T2; //for priority_queue int V; vector<vector<T> > G; vector<vector<T> > rG; Dijkstra():V(-1){} Dijkstra(int V):V(V),G(V),rG(V){} void add_edge(int a,int b,ctype c,int oneway = 0){ assert(a >= 0 && b >= 0); assert(a < V && b < V); G[a].push_back(T(b, c)); rG[b].push_back(T(a, c)); if(!oneway) G[b].push_back(T(a, c)), rG[a].push_back(T(b, c)); } vector<ctype> dijkstra(int start, int rev = 0){ vector<ctype> D(V, INF); vector<int> visited(V,0); priority_queue<T2, vector<T2>, greater<T2> > Q; Q.push(T2(0, start)); D[start] = 0; while(!Q.empty()){ ctype cost; int pos; tie(cost,pos) = Q.top(); Q.pop(); assert(!visited[pos] || D[pos] <= cost); if(visited[pos]++) continue; for(auto t:rev == 0? G[pos]:rG[pos]){ int to = get<0>(t); ctype ncost = cost + get<1>(t); if(D[to] <= ncost) continue; D[to] = ncost; Q.push(T2(ncost, to)); } } return D; } vector<vector<int> > getDAG(int start, int goal){ auto DS = dijkstra(start, 0); auto DT = dijkstra(goal, 1); vector<vector<int> > DAG(V); vector<int> visited(V,0); queue<int> Q; Q.push(start); while(!Q.empty()){ int pos; pos = Q.front(); Q.pop(); if(visited[pos]++) continue; for(auto t:G[pos]){ int to = get<0>(t); ctype cost = get<1>(t); if(DS[pos] + DT[to] + cost != DS[goal]) continue; Q.push(to); DAG[pos].push_back(to); } } return DAG; } }; using T = tuple<int,int,int>; const int start = 0, goal= 1; int n, m; vector<T> edge; vector<Int> DS, DT; Lowlink lowlink; int happy(int i){ int a, b, c; tie(a, b, c) = edge[i]; return DS[goal] > DS[b] + DT[a] + c; } int soso(int i){ int a, b, c; tie(a, b, c) = edge[i]; if(DS[a] + DT[b] + c != DS[goal]) return 1; return !lowlink.isBridge(a, b); } signed main(){ srand((unsigned)time(NULL)); cin.tie(0); ios_base::sync_with_stdio(0); cout << fixed << setprecision(12); cin>>n>>m; edge.resize(m); Dijkstra<Int, INF> dijkstra(n); Dijkstra<Int, INF> dijkstra2(n); for(int i=0;i < m;i++){ Int a, b, c; cin>>a>>b>>c; a--,b--; edge[i] = T(a, b, c); dijkstra.add_edge(a, b, c, 1); dijkstra2.add_edge(b, a, c, 1); } DS = dijkstra.dijkstra(start); DT = dijkstra2.dijkstra(goal); auto G = dijkstra.getDAG(start, goal); lowlink = Lowlink(n); for(int i=0;i<n;i++) for(int to:G[i]) lowlink.add_edge(i, to); lowlink.build(); for(int i=0;i<m;i++){ if(happy(i)) cout<<"HAPPY"<<endl; else if(soso(i)) cout<<"SOSO"<<endl; else cout<<"SAD"<<endl; } return 0; }
0
#include <iostream> #include <cassert> using namespace std; typedef long long ll; int N,cnt[110] = {}; ll p[110],q[110],r[110],b[110],dp[110] = {}; ll gcd(ll n,ll m){ assert(m!=0); if(n%m==0) return m; else return gcd(m,n%m); } void dfs(int n){ if(n==0){ dp[0] = 1; }else{ dfs(r[n]); dfs(b[n]); assert(dp[r[n]]*p[n]!=0 && dp[b[n]]*q[n]!=0); ll g = gcd(dp[r[n]]*p[n],dp[b[n]]*q[n]); dp[n] = (dp[r[n]]*p[n])/g*dp[b[n]]+(dp[b[n]]*q[n])/g*dp[r[n]]; } } int main(){ while(cin >> N && N>0){ int par; for(int i=0;i<=N;i++) cnt[i] = 0; for(int i=1;i<=N;i++){ cin >> p[i] >> q[i] >> r[i] >> b[i]; cnt[r[i]]++; cnt[b[i]]++; } for(int i=1;i<=N;i++) if(cnt[i]==0) par = i; dfs(par); cout << dp[par] << endl; } }
0
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; const int INF = 1e9 + 5; const long long mod = 1e9 + 7; const double pi = 3.1415926536; int dx[] = {1, -1, 0, 0, 1, 1, -1, -1}; int dy[] = {0, 0, 1, -1, 1, -1, -1, 1}; int main() { ios::sync_with_stdio(0); cin.tie(0); ios_base::sync_with_stdio(0); ; int n; cin >> n; map<string, long long> sum, sum2; long long maxi = 0; string s[n]; long long score[n]; for (int i = 0; i < n; i++) { cin >> s[i] >> score[i]; sum[s[i]] += score[i]; } for (auto i : sum) { maxi = max(maxi, i.second); } for (int i = 0; i < n; i++) { sum2[s[i]] += score[i]; if (sum2[s[i]] >= maxi && sum[s[i]] == maxi) return cout << s[i], 0; } return 0; }
1
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; using ld = long double; using pii = pair<int, int>; using pll = pair<ll, ll>; using pci = pair<char, int>; using vi = vector<int>; using vll = vector<ll>; using vpii = vector<pii>; const ll infll = 1e18 + 3; const int maxn = 1e6 + 77; const int nmax = 1e6 + 577; const ll basell = 1e18 + 3; const int mod = 1e9 + 7; const ld eps = 1e-7; const int inf = 1009000999; const int nv = 100505; const int baseint = 1000200013; const ld PI = acos(-1.0); inline bool EQ(ld a, ld b) { return fabs(a - b) < 1e-9; } inline bool IF(int a, int b, int c) { return (a >= b && a <= c); } inline bool IFS(int a, int b, int c) { return (a > b && a < c); } int a[505]; int dp[505][505]; int inline solve() { ios::sync_with_stdio(NULL), cin.tie(NULL), cout.tie(NULL); ; int n; cin >> n; for (int i = 0; i < n; ++i) { cin >> a[i]; } for (int len = 1; len <= n; ++len) { for (int l = 0, r = len - 1; r < n; ++l, ++r) { if (len == 1) { dp[l][r] = 1; } else { dp[l][r] = 1 + dp[l + 1][r]; if (a[l] == a[l + 1]) { dp[l][r] = min(dp[l][r], dp[l + 2][r] + 1); } for (int i = l + 2; i <= r; ++i) { if (a[i] == a[l]) { dp[l][r] = min(dp[l][r], dp[l + 1][i - 1] + dp[i + 1][r]); } } } } } cout << dp[0][n - 1]; return 0; } int32_t main() { solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; struct rectangle { long long c1, r1, c2, r2; pair<long long, long long> get_sp_impose() { long long sum = (c2 - c1 + 1) * (r2 - r1 + 1); long long fc_tmp = sum / 2; long long sum_white, sum_black; if ((c1 + r1) % 2 == 0) { sum_black = fc_tmp; sum_white = sum - sum_black; } else { sum_white = fc_tmp; sum_black = sum - sum_white; } return make_pair(sum_white, sum_black); } }; rectangle get_intersect(rectangle& l, rectangle& r) { long long c1 = max(l.c1, r.c1); long long c2 = min(l.c2, r.c2); long long r1 = max(l.r1, r.r1); long long r2 = min(l.r2, r.r2); if (c1 <= c2 && r1 <= r2) return rectangle{c1, r1, c2, r2}; else return rectangle{-1, -1, -1, -1}; } istream& operator>>(istream& in, rectangle& r) { in >> r.c1 >> r.r1 >> r.c2 >> r.r2; return in; } int main(int argc, char const* argv[]) { ios::sync_with_stdio(false); cin.tie(0); long long T; cin >> T; while (T--) { long long n, m; cin >> n >> m; rectangle rec_white, rec_black; cin >> rec_white >> rec_black; pair<long long, long long> white_imp = rec_white.get_sp_impose(); pair<long long, long long> black_imp = rec_black.get_sp_impose(); rectangle inter = get_intersect(rec_white, rec_black); if (inter.c1 != -1) { pair<long long, long long> inter_imp = inter.get_sp_impose(); white_imp.first -= inter_imp.first; white_imp.second -= inter_imp.second; } long long black_pall = n * m / 2; long long white_pall = n * m - black_pall; black_pall -= white_imp.second; white_pall += white_imp.second; white_pall -= black_imp.first; black_pall += black_imp.first; cout << white_pall << ' ' << black_pall << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int a[maxn], b[maxn]; vector<pair<int, int>> G[maxn]; bool v[maxn], use[maxn]; struct data { int a, b, c; }; vector<data> ans; void dfs(int now, int fa, int edge) { v[now] = true; int nxt = -1, cid = -1; for (auto u : G[now]) if (u.first != fa) { if (!v[u.first]) dfs(u.first, now, u.second); if (use[u.second]) continue; if (nxt == -1) nxt = u.second, cid = u.first; else if (nxt != -1) { ans.emplace_back((data){u.first, now, cid}); use[nxt] = use[u.second] = true; nxt = -1; cid = -1; } } if (nxt != -1) { if (edge && !use[edge] && !use[nxt]) { ans.emplace_back((data){cid, now, fa}); use[edge] = use[nxt] = true; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; if (m & 1) return cout << "No solution" << endl, 0; for (int i = 1; i <= m; ++i) { cin >> a[i] >> b[i]; G[a[i]].emplace_back(b[i], i); G[b[i]].emplace_back(a[i], i); } for (int i = 1; i <= n; ++i) { if (!v[i]) dfs(i, 0, 0); } if (ans.size() < m / 2) return cout << "No solution" << endl, 0; for (auto e : ans) cout << e.a << ' ' << e.b << ' ' << e.c << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007; int other = -1, X; int pre[200010]; int cnt[200010]; long long C, N; bool can() { int cur = C; int last = C; while (cur) { int to = pre[last]; if (other != -1 && other <= cur && other > to) { cur -= other; other = -1; } else { if (to == 0) return false; cur -= to * min(cnt[to], cur / to); last = min(cur, to - 1); } } return true; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cin >> C >> N; for (int i = 0; i < N; i++) cin >> X, cnt[X]++; for (int i = 0; i <= C; i++) { pre[i] = (i ? pre[i - 1] : 0); if (cnt[i]) pre[i] = i; } assert(can()); for (int i = 1; i < C; i++) { other = i; if (!can()) { cout << i << '\n'; return 0; } other = -1; } cout << "Greed is good" << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; const double eps = 1e-9; const int inf = (1 << 31) - 1; int main() { int n; scanf("%d", &n); if (n == 5) cout << "1"; else printf("%d", (n % 3) + 1); return 0; }
4
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <vector> using namespace std; #define MAX_N 1120 #define MAX_K 15 int main(){ bool p[MAX_N+1]; memset(p, true, sizeof(p)); p[0] = p[1] = false; for(int i = 2 ; i*i <= MAX_N ; i++){ if(p[i]){ for(int j = i*i ; j <= MAX_N ; j += i){ p[j] = false; } } } vector<int> prime; for(int i = 0 ; i <= MAX_N ; i++){ if(p[i]) prime.push_back(i); } int dp[MAX_N+1][MAX_K]; memset(dp, 0, sizeof(dp)); dp[0][0] = 1; for(int i = 0 ; i < prime.size() ; i++){ for(int j = MAX_N - prime[i] ; j >= 0 ; j--){ for(int k = 0 ; k <= 13 ; k++){ dp[ j + prime[i] ][k + 1] += dp[j][k]; } } } int n, m; while(cin >> n >> m, n|m){ cout << dp[n][m] << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, e, f, x, y, z; cin >> a >> b >> c >> d >> e >> f; x = a + b; y = b + c; z = a + c; if (x + c == d + e + f || x + d == c + e + f || x + e == c + d + f || x + f == c + d + e || y + d == a + e + f || y + e == a + d + f || y + f == a + e + d || z + d == b + e + f || z + e == b + d + f || z + f == b + d + e) cout << "Yes"; else cout << "No"; }
1
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; int h, w, leaves; vector<char> path; char mapData[10][10]; bool visited[10][10]; int dy[] = {-1, 0, 1, 0}; int dx[] = {0, 1, 0, -1}; const string str = "URDL"; bool dfs(int y, int x, int d){ if(leaves == 0){ path.push_back(d); return true; } for(int i=0;i<4;i++){ if(abs(i-d) == 2) continue; int ny = y; int nx = x; while(1){ ny += dy[i]; nx += dx[i]; if(ny<0 || ny>= h) break; if(nx<0 || nx>= w) break; if(mapData[ny][nx] == 'o' && !visited[ny][nx]){ visited[y][x] = true; --leaves; if(dfs(ny, nx, i)){ path.push_back(i); return true; } visited[y][x] = false; ++leaves; break; } } } return false; } int main(){ while(cin >> h >> w && h|w){ path.clear(); fill(visited[0], visited[10], false); int sy, sx, sd; for(int i=0;i<h;++i){ for(int j=0;j<w;++j){ cin >> mapData[i][j]; if(mapData[i][j] == 'o') ++leaves; if(mapData[i][j] != 'o' && mapData[i][j] != '.'){ sy = i; sx = j; if(mapData[i][j] == 'U') sd = 0; if(mapData[i][j] == 'R') sd = 1; if(mapData[i][j] == 'D') sd = 2; if(mapData[i][j] == 'L') sd = 3; } } } dfs(sy, sx, sd); reverse(path.begin(), path.end()); for(int i=0;i<(int)path.size()-1;++i){ cout << str[path[i]]; } cout << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxt = int(1e7) + 5; int n, m, k, mark[maxt]; vector<int> f; vector<pair<int, int>> s; bool check(int x) { for (int i = 0; i < x; i++) mark[s[i].first]++; int drinks = 0; bool done = 1; for (int i = 0; i < maxt; i++) { drinks += mark[i]; if (mark[i]) { if ((drinks - 1) / k > i) done = false; } } for (int i = 0; i < x; i++) mark[s[i].first]--; return done; } int main(void) { int t; scanf("%d%d%d", &n, &m, &k); s.clear(); s.resize(m); for (int i = 0; i < n; i++) { scanf("%d", &t); mark[t]++; } for (int i = 0; i < m; i++) { scanf("%d", &s[i].first); s[i].second = i; } sort(s.rbegin(), s.rend()); int L = 0, R = m, ans = -1; while (L <= R) { int mid = (L + R) / 2; if (check(mid)) { ans = mid; L = mid + 1; } else { R = mid - 1; } } printf("%d\n", ans); for (int i = 0; i < ans; i++) printf("%d ", s[i].second + 1); printf("\n"); }
4
#include<bits/stdc++.h> using namespace std; int main(){ int64_t x,y,z; cin >> x >> y >> z; cout << z <<" "<<x<<" "<<y; }
0
#include <bits/stdc++.h> using namespace std; template <class T> void max_swap(T& a, const T& b) { a = max(a, b); } template <class T> void min_swap(T& a, const T& b) { a = min(a, b); } template <class T> void uniq(vector<T>& c) { sort(c.begin(), c.end()); c.erase(unique(c.begin(), c.end()), c.end()); } template <class T> string to_s(const T& a) { ostringstream os; os << a; return os.str(); } template <class T> T to_T(const string& s) { istringstream is(s); T res; is >> res; return res; } template <class T, class U> ostream& operator<<(ostream& os, pair<T, U>& p) { os << "( " << p.first << ", " << p.second << " )"; return os; } template <class T> void print(T a, int n, const string& deli = " ", int br = 1) { for (int i = 0; i < n; ++i) { cout << a[i]; if (i + 1 != n) cout << deli; } while (br--) cout << endl; } template <class T> void print(const T& c, const string& deli = " ", int br = 1) { for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it) { cout << *it; if (++it != c.end()) cout << deli; --it; } while (br--) cout << endl; } template <class T> void print2d(T a, int w, int h, int width = -1, int br = 1) { for (int i = 0; i < h; ++i) { for (int j = 0; j < w; ++j) { if (width != -1) cout.width(width); cout << a[i][j] << ' '; } cout << endl; } while (br--) cout << endl; } template <class T> void input(T& a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; } template <class T> void input(T* a, int n) { for (int i = 0; i < n; ++i) cin >> a[i]; } void fast_io() { cin.tie(0); ios::sync_with_stdio(false); } bool valid(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; } const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; const double PI = acos(-1.0); const int mod = ((long long)(1e9)) + 7; int n, t; int num_h, num_s, last_h; string s; int las_pos(int init) { int sw = init; for (int i = 0; i < (int)(n); ++i) { if (s[i] == 'S') ++sw; if (sw >= num_h) return max(i, last_h); } (cout << "init" << ": " << (init) << endl); exit(1); } bool ok(int init) { if (init + num_s < num_h) return false; int last_pos = las_pos(init); int left = -1; int sw = init; int step = 0; for (int i = 0; i < (int)(n); ++i) { ++step; if (s[i] == 'H') { if (left == -1 && sw == 0) { left = i; if (step + 2 * (last_pos - i) <= t) return true; } --sw; } else if (s[i] == 'S') { ++sw; if (left != -1 && sw == 0) { step += 2 * (i - left); left = -1; } } if (i == last_pos) break; } return step <= t; } int main() { cin >> n >> t >> s; num_h = count((s).begin(), (s).end(), 'H'); num_s = count((s).begin(), (s).end(), 'S'); last_h = s.find_last_of('H'); const int imp = num_h + 1; int low = -1, high = imp; while (high - low > 1) { int mid = (low + high) / 2; if (ok(mid)) high = mid; else low = mid; } int res = high == imp ? -1 : high; cout << res << endl; }
2
#include <bits/stdc++.h> using namespace std; long long dp[100005][205]; int n, m, k, b[100005], e[100005], to[100005], pr[100005]; vector<int> fin[100005], st[100005]; signed main() { ios::sync_with_stdio(false); cin.tie(NULL); int n; int m; int f; cin >> n >> m >> f; for (int i = 0; i < f; i++) { cin >> b[i] >> e[i] >> to[i] >> pr[i]; st[b[i]].push_back(i); fin[e[i] + 1].push_back(i); } fill(dp[0], dp[100004] + 205, 1e18); multiset<pair<int, int> > sret; dp[1][0] = 0; for (int i = 1; i <= n; i++) { for (auto it : st[i]) sret.insert({pr[it], to[it]}); for (auto it : fin[i]) sret.erase(sret.find({pr[it], to[it]})); pair<int, int> my = {0, i}; if (!sret.empty()) my = *sret.rbegin(); int pric = my.first; int eda = my.second; for (int j = 0; j <= m; j++) { dp[eda + 1][j] = min(dp[eda + 1][j], dp[i][j] + pric); dp[i + 1][j + 1] = min(dp[i + 1][j + 1], dp[i][j]); } } long long ans = dp[n + 1][0]; for (int i = 0; i <= m; i++) { ans = min(ans, dp[n + 1][i]); } cout << ans; cin >> ans; }
5
#include <bits/stdc++.h> using namespace std; const int MOD = 10000005; const int di[] = {-1, 0, 1, 0, 1, -1, 1, -1}; const int dj[] = {0, 1, 0, -1, 1, -1, -1, 1}; int main() { ios::sync_with_stdio(false), cin.tie(0); string s; cin >> s; vector<int> cnt; vector<char> ltr; for (int i = 0; i < s.length(); i++) { ltr.push_back(s[i]); cnt.push_back(1); int oi = i++; for (; i < s.length() && s[i] == s[oi]; i++) cnt.back()++; i--; } int n; cin >> n; vector<vector<bool> > forb(26, vector<bool>(26)); for (int i = 0; i < n; i++) { string ts; cin >> ts; ts[0] -= 'a', ts[1] -= 'a'; forb[ts[0]][ts[1]] = forb[ts[1]][ts[0]] = true; } int r = 0; for (int i = 0; i < ltr.size() - 1; i++) if (forb[ltr[i] - 'a'][ltr[i + 1] - 'a']) { int p[] = {0, 0}; bool flip = true; p[0] += cnt[i]; while (i < ltr.size() - 1 && forb[ltr[i] - 'a'][ltr[i + 1] - 'a']) p[flip] += cnt[++i], flip = !flip; r += min(p[0], p[1]); i--; } cout << r << endl; }
1
#include <bits/stdc++.h> using namespace std; const int N = 100001; char s[4][N]; int p[4][N], l[4], cut[4][4], ans = INT_MAX; void get_next(int now) { int j = 0; l[now] = strlen(s[now] + 1); for (int i = 2; i <= l[now]; i++) { while (j && s[now][i] != s[now][j + 1]) j = p[now][i]; if (s[now][i] == s[now][j + 1]) j++; p[now][i] = j; } } int KMP(int x, int y) { int j = 0; for (int i = 1; i <= l[x]; i++) { while (j && s[x][i] != s[y][j + 1]) j = p[y][j]; if (s[x][i] == s[y][j + 1]) j++; if (j == l[y]) return -1; } return j; } int solve() { for (int i = 1; i <= 3; i++) for (int j = 1; j <= 3; j++) for (int k = 1; k <= 3; k++) { if (i == j || j == k || i == k) continue; int sum = l[i] + l[j] + l[k] - cut[i][j] - cut[j][k]; if (cut[i][j] >= 0 && cut[j][k] >= 0) ans = min(ans, sum); else { if (cut[i][j] < 0 && cut[i][k] < 0) ans = min(ans, l[i]); else if (cut[i][j] < 0) ans = min(ans, sum + cut[i][j] + cut[j][k] - l[j] - cut[i][k]); if (cut[j][k] < 0) ans = min(ans, sum + cut[j][k] - l[k]); } } return ans; } int main() { scanf("%s%s%s", s[1] + 1, s[2] + 1, s[3] + 1); for (int i = 1; i <= 3; i++) get_next(i); for (int i = 1; i <= 3; i++) for (int j = 1; j <= 3; j++) { if (i == j) continue; cut[i][j] = KMP(i, j); } printf("%d", solve()); return 0; }
5
#include <bits/stdc++.h> using namespace std; template <class T> T inline sqr(T x) { return x * x; } template <class T> inline void relaxMin(T &a, T b) { a = min(a, b); } template <class T> inline void relaxMax(T &a, T b) { a = max(a, b); } string str(int i) { char s[100]; sprintf(s, "%d", i); return string(s); } inline int sign(int x) { return x > 0 ? 1 : (x < 0 ? -1 : 0); } inline int myAbs(int a) { return a > 0 ? a : -a; } const int mlen = (int)2e5 + 10; int n; char s[mlen]; int main() { gets(s); n = strlen(s); int i = 0; while (i < n && s[i] <= 32) i++; while (i < n) { char ch = s[i]; int l; if (ch == ',') l = 1; else if (ch == '.') l = 3; else l = n; while (l > 0 && i < n && isdigit(ch) == isdigit(s[i])) putchar(s[i++]), l--; while (i < n && s[i] <= 32) i++; if (i < n && (ch == ',' || s[i] == '.' || (isdigit(ch) && isdigit(s[i])))) putchar(' '); } puts(""); return 0; }
2
#include <bits/stdc++.h> using namespace std; template <class TF, class TC> struct CostFlow { static const int MAXV = 10000; static constexpr TC INF = 1e9; struct Edge { int v, r; TF f; TC c; Edge(int _v, int _r, TF _f, TC _c) : v(_v), r(_r), f(_f), c(_c) {} }; int n, s, t, pre[MAXV], pre_E[MAXV], inq[MAXV]; TF fl; TC dis[MAXV], cost; vector<Edge> E[MAXV]; CostFlow(int _n, int _s, int _t) : n(_n), s(_s), t(_t), fl(0), cost(0) {} void add_edge(int u, int v, TF f, TC c) { E[u].emplace_back(v, E[v].size(), f, c); E[v].emplace_back(u, E[u].size() - 1, 0, -c); } pair<TF, TC> flow() { while (true) { for (int i = 0; i < n; ++i) { dis[i] = INF; inq[i] = 0; } dis[s] = 0; queue<int> que; que.emplace(s); while (not que.empty()) { int u = que.front(); que.pop(); inq[u] = 0; for (int i = 0; i < E[u].size(); ++i) { int v = E[u][i].v; TC w = E[u][i].c; if (E[u][i].f > 0 and dis[v] > dis[u] + w) { pre[v] = u; pre_E[v] = i; dis[v] = dis[u] + w; if (not inq[v]) { inq[v] = 1; que.emplace(v); } } } } if (dis[t] == INF) break; TF tf = INF; for (int v = t, u, l; v != s; v = u) { u = pre[v]; l = pre_E[v]; tf = min(tf, E[u][l].f); } for (int v = t, u, l; v != s; v = u) { u = pre[v]; l = pre_E[v]; E[u][l].f -= tf; E[v][E[u][l].r].f += tf; } cost += tf * dis[t]; fl += tf; } return {fl, cost}; } }; const int MAXN = 50; int N, K; int C[MAXN][MAXN]; signed main() { ios::sync_with_stdio(0); cin >> N >> K; for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { cin >> C[i][j]; } } int lb = 0, ub = 1e9; while (lb + 1 != ub) { int mid = lb + ub >> 1; CostFlow<int, long long> mcmf(N + 1, N, N - 1); mcmf.add_edge(N, 0, mid, 0); for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) if (C[i][j]) { mcmf.add_edge(i, j, C[i][j], 0); mcmf.add_edge(i, j, mcmf.INF, 1); } } long long flow, cost; tie(flow, cost) = mcmf.flow(); (flow == mid and cost <= K ? lb : ub) = mid; } cout << lb << endl; return 0; }
5
#include <iostream> #include <iomanip> using namespace std; int main(){ double a,b; cin>>a>>b; a=a*b; a=a/3.3057851; cout<<fixed<<setprecision(6)<<a<<endl; }
0
#include<iostream> #include<vector> #include<cmath> #include<complex> #define MAX 21 #define INF 10000 #define EPS 1.0e-10 using namespace std; typedef double elem; typedef complex<elem> point; double dist(const point &a, const point &b){return std::abs(b-a);} bool vis[MAX]; bool bt(const vector<point> &v, double walk, const point &h, const point &d){ bool bCont = false; for(unsigned int i = 0; i < v.size(); ++i){ if( !vis[i] )bCont=true; } if( !bCont )return true; for(unsigned int i = 0; i < v.size(); ++i){ if( !vis[i] ){ double t = walk + dist(h,v[i]); if( t < dist(d, v[i]) ){ vis[i] = true; if( bt( v, t, v[i], d ) ) return true; vis[i] = false; }else{ return false; } } } return false; } int main(){ while(true){ bool bImpossible = false; int n,hx,hy,dx,dy; point hero, daemon; vector< point > vp; scanf("%d%d%d%d%d", &n, &hx, &hy, &dx, &dy); if(!(n||hx||hy||dx||dy))break; hero = complex<elem>(hx,hy); daemon = complex<elem>(dx,dy); for(int i=0;i<n;++i){ bool bInserted = false; int cx,cy;scanf("%d%d", &cx, &cy);point p(cx,cy); if(cx==hx&&cy==hy)continue; for(unsigned int j = 0; j < vp.size() && !bInserted; ++j){ if( dist( daemon, vp[j] ) > dist( daemon, p ) ){ vp.insert( vp.begin()+j, p ); bInserted = true; } } if( !bInserted ){ vp.push_back( p ); } if( dist(daemon,p) <= dist(hero,p) )bImpossible=true; vis[i]=false; } /*for(unsigned int i = 0; i < vp.size(); ++i){ cout<<vp[i]<< ' '; } cout << endl;*/ if( !bImpossible && bt( vp, 0.0, hero, daemon ) ) printf("YES\n"); else printf("NO\n"); } return 0; }
0
#include <iostream> using namespace std; int main() { int K, X; cin >> K >> X; cout << (X <= 500*K ? "Yes" : "No") << endl; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5; vector<int> g[maxn + 5]; int dep[maxn + 5], mx[maxn + 5], son[maxn + 5]; int n, root; priority_queue<pair<int, int> > q; void dfs(int k, int fa) { dep[k] = dep[fa] + 1; son[k] = dep[k]; mx[k] = 0; for (auto u : g[k]) { if (u == fa) continue; dfs(u, k); if (son[u] > son[k]) son[k] = son[u], mx[k] = u; } } int ans[maxn + 5]; int main() { scanf("%d", &n); for (int i = 1; i < n; ++i) { int x, y; scanf("%d%d", &x, &y); g[x].push_back(y), g[y].push_back(x); } dfs(1, 0); root = 1; for (int i = 2; i <= n; ++i) if (dep[i] > dep[root]) root = i; dfs(root, 0); q.push(make_pair(son[root] - dep[root], root)); int now = 1; ans[1] = 1; while (!q.empty()) { pair<int, int> tmp = q.top(); q.pop(); ++now; ans[now] = ans[now - 1] + tmp.first; int u = tmp.second; while (u) { for (auto v : g[u]) if (dep[v] > dep[u] && v != mx[u]) q.push(make_pair(son[v] - dep[v] + 1, v)); u = mx[u]; } } for (int i = now + 1; i <= n; ++i) ans[i] = n; for (int i = 1; i <= n; ++i) printf("%d ", ans[i]); return 0; }
2
#include <bits/stdc++.h> using namespace std; void solve(int n, long long p, long long q, long long r, vector<long long> a) { vector<long long> ll(n), lr(n), sl(n), sr(n); ll[0] = a[0]; sl[0] = a[0]; for (int i = 1; i < n; i++) { ll[i] = max(ll[i - 1], a[i]); sl[i] = min(sl[i - 1], a[i]); } lr[n - 1] = a[n - 1]; sr[n - 1] = a[n - 1]; for (int i = n - 2; i >= 0; i--) { lr[i] = max(lr[i + 1], a[i]); sr[i] = min(sr[i + 1], a[i]); } long long ans = (p + q + r) * a[0]; for (int j = 0; j < n; j++) { long long tmp = q * a[j]; tmp += p * ((p > 0) ? ll[j] : sl[j]); tmp += r * ((r > 0) ? lr[j] : sr[j]); ans = max(tmp, ans); } printf("%lli\n", ans); } int main() { int n, p, q, r; scanf("%d %d %d %d", &n, &p, &q, &r); vector<long long> a(n); for (int i = 0; i < n; i++) scanf("%lli", &a[i]); solve(n, p, q, r, a); }
2
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; const int M = 1000000007; int n, k; string s; int dp[1007][1007][2]; int get(int it, int d, bool zero, bool complete) { if (d > k) d = 1001; if (it == 0) return complete; if (zero && dp[it][d][complete] != -1) return dp[it][d][complete]; long long ret = 0; int from = 0; int to = zero ? 9 : s[n - it] - '0'; for (int i = from; i <= to; i++) { bool nxtZero = zero || (i < to); if (i == 4 || i == 7) { bool nxtComplete = complete || (d <= k); ret += get(it - 1, 1, nxtZero, nxtComplete); } else { ret += get(it - 1, d + 1, nxtZero, complete); } } ret %= M; if (zero) dp[it][d][complete] = ret; return ret; } bool correct() { int prev = -1; for (int i = 0, _n = (n); i < _n; ++i) if (s[i] == '4' || s[i] == '7') { if (prev != -1 && i - prev <= k) return true; prev = i; } return false; } void solution() { int t; cin >> t >> k; memset((dp), (-1), sizeof((dp))); while (t--) { cin >> s; n = (int)((s).size()); int r1 = get(n, 1001, 0, 0); if (correct()) r1--; cin >> s; n = (int)((s).size()); int r2 = get(n, 1001, 0, 0); r2 -= r1; if (r2 < 0) r2 += M; r2 %= M; cout << r2 << endl; } } int main() { solution(); return 0; }
4
#include <bits/stdc++.h> using namespace std; long long int a[200010], b[200010], vis[200010]; vector<long long int> G[200010], ans[2]; long long int Ans; void dfs(long long int x) { vis[x] = 1; for (long long int to : G[x]) { if (!vis[to]) { dfs(to); } } Ans += a[x]; if (b[x] != -1 and a[x] > 0) { a[b[x]] += a[x]; } if (a[x] > 0) { ans[0].push_back(x); } else { ans[1].push_back(x); } } int main() { long long int n; cin >> n; for (long long int i = 0; i < n; i++) cin >> a[i]; for (long long int i = 0; i < n; i++) { cin >> b[i]; if (b[i] != -1) { b[i]--; G[b[i]].push_back(i); } } for (long long int i = 0; i < n; i++) { if (!vis[i]) dfs(i); } cout << Ans << endl; reverse(ans[1].begin(), ans[1].end()); for (long long int i : ans[0]) cout << i + 1 << " "; for (long long int i : ans[1]) cout << i + 1 << " "; cout << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int t; string a, b, c; cin >> t; while (t--) { bool f = 0; cin >> a >> b >> c; for (auto i = 0; i < a.size(); ++i) { if (!(c[i] == a[i] || c[i] == b[i])) { f = 1; break; } } if (f == 1) cout << "NO\n"; else cout << "YES\n"; } }
1
#include <bits/stdc++.h> long long gcd(long long a, long long b) { while (1) { if (a == 0) { return b; } if (b == 0) { return a; } if (a >= b) { a %= b; } else { b %= a; } } } long long search(long long left, long long right, int n, long long b) { int l, r, m; l = 1; r = n; while (l <= r) { m = (l + r) >> 1; if (b * m < left) { l = m + 1; } else if (b * m > right) { r = m - 1; } else { return b * m; } } return -1; } void solve(long long a, long long b) { long long t, i, n, ca, cb, k; int f; if (a == b) { printf("Equal\n"); return; } t = gcd(a, b); a /= t; b /= t; t = a * b; f = 0; if (a > b) { a ^= b; b ^= a; a ^= b; f = 1; } n = a; ca = cb = 0; for (i = 0; i < t; i += a) { if (i + a == t) { cb += (a + 1); continue; } k = search(i, i + a, n, b); if (k == -1) { ca += (a + 1); } else { cb += (k - i + 1); ca += (i + a - k + 1); } } if (ca == cb) { printf("Equal\n"); } else if ((ca < cb && f == 0) || (ca > cb && f == 1)) { printf("Masha\n"); } else { printf("Dasha\n"); } } int main() { int a, b; while (scanf("%d%d", &a, &b) == 2) { solve(a, b); } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long k, n, s, p; int main() { ios_base::sync_with_stdio(false); cin >> k >> n >> s >> p; int t = ceil(k * n * 1.0 / s); int v = ceil(t * 1.0 / k); cout << (long long)ceil(v * k * 1.0 / p); return 0; }
1
#include<cstdio> #include<iostream> #include<vector> #include<string> #include<algorithm> #include<set> #define reps(i,f,n) for(int i=f;i<int(n);i++) #define rep(i,n) reps(i,0,n) #define pb push_back using namespace std; typedef pair<double, string> P; bool solve(){ int n; cin>>n; if(n==0)return false; vector<P> dat; rep(i,n){ string l; int p,a,b,c,d,e,f,s,m; cin>>l>>p>>a>>b>>c>>d>>e>>f>>s>>m; double price = s*f*m -p; double time = a+b+c+(d+e)*m; dat.push_back(P(-price/time, l)); } sort(dat.begin(),dat.end()); rep(i,dat.size()){ cout<<dat[i].second<<endl; } cout<<"#"<<endl; return true; } int main(){ while(solve()); }
0
#include <bits/stdc++.h> using namespace std; long long int N, M, K, Q, R, S[105], EXP[105], choose[105][105], dp[105][105 * 105], tmp; long long int exponentiate(long long int v, long long int p) { if (p == 1) return v; if (p & 1ll) return (v * exponentiate(v, p - 1ll)) % 1000000007; return exponentiate((v * v) % 1000000007, p / 2ll); } int main() { cin >> N >> M >> K; for (int i = 0; i <= N; ++i) for (int j = 0; j <= i; ++j) { if (i == 0 || j == 0 || j == i) choose[i][j] = 1ll; else choose[i][j] = choose[i - 1][j - 1] + choose[i - 1][j]; if (choose[i][j] >= 1000000007) choose[i][j] -= 1000000007; } Q = M / N; R = M % N; for (int i = 0; i <= N; ++i) { S[i] = choose[N][i]; EXP[i] = exponentiate(S[i], Q); } dp[0][0] = 1; for (int i = 1; i <= N; ++i) { dp[i][0] = 1; for (int j = 1; j <= K; ++j) { for (int a = 0; a <= N && j - a >= 0; ++a) { tmp = (EXP[a] * ((i <= R) ? S[a] : 1ll)) % 1000000007; tmp = (dp[i - 1][j - a] * tmp) % 1000000007; dp[i][j] += tmp; if (dp[i][j] >= 1000000007) dp[i][j] -= 1000000007; } } } cout << dp[N][K] << endl; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); long long x[30]; for (long long i = 0; i < 26; i++) { cin >> x[i]; } string s; cin >> s; long long n = s.size(); long long pre[n + 1]; pre[0] = 0; for (long long i = 1; i <= n; i++) { pre[i] = pre[i - 1] + x[s[i - 1] - 'a']; } long long ans = 0; map<pair<long long, char>, long long> cnt; for (long long i = 1; i < n; i++) { if (s[i] == s[i - 1]) ans++; ans += cnt[{pre[i], s[i]}]; cnt[{pre[i], s[i - 1]}]++; } cout << ans << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int N, P[2005], Ans; vector<int> AdjList[2005]; int dfs(int u) { int height = 0; for (auto v : AdjList[u]) height = max(height, dfs(v)); return height + 1; } int main() { scanf("%d", &N); for (int i = 1; i <= N; i++) { scanf("%d", &P[i]); AdjList[P[i]].push_back(i); } for (int i = 1; i <= N; i++) if (P[i] == -1) Ans = max(Ans, dfs(i)); printf("%d", Ans); }
1
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8; const int INF = (1 << 30) - 1; const int MAXN = 100010; int n, q; int A[MAXN], B[MAXN]; int L[MAXN], R[MAXN]; set<pair<int, int> > st; set<pair<int, int> >::iterator it; int main() { scanf("%d%d", &n, &q); for (int i = 1; i <= n; ++i) { scanf("%d", &A[i]); } for (int i = 1; i < n; ++i) { B[i] = abs(A[i + 1] - A[i]); } for (int i = 1; i < n; ++i) { L[i] = 1; R[i] = n - 1; } for (int i = 1; i < n; ++i) { st.insert(make_pair(B[i], i)); it = st.find(make_pair(B[i], i)); vector<pair<int, int> > g; while (1) { if (it == st.begin()) break; it--; if ((*it).first <= B[i]) { R[(*it).second] = i - 1; g.push_back((*it)); } } for (int j = 0; j < g.size(); ++j) st.erase(g[j]); } st.clear(); for (int i = n - 1; i >= 1; --i) { st.insert(make_pair(B[i], i)); it = st.find(make_pair(B[i], i)); vector<pair<int, int> > g; while (1) { if (it == st.begin()) break; it--; if ((*it).first < B[i]) { L[(*it).second] = i + 1; g.push_back((*it)); } } for (int j = 0; j < g.size(); ++j) st.erase(g[j]); } for (int i = 1; i <= q; ++i) { int a, b; scanf("%d%d", &a, &b); long long ans = 0; for (int j = a; j < b; ++j) { int l = max(a, L[j]); int r = min(b - 1, R[j]); long long num; if (l == r) num = 1; else num = 1ll * (r - j + 1) * (j - l + 1); ans += 1ll * B[j] * num; } printf("%I64d\n", ans); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, k; string co[107]; pair<int, int> tab[107]; vector<int> lew[107]; vector<int> pra[107]; long long dp[107][107]; int czy; int u; int main() { cin >> n >> k; for (int i = 1; i <= k; i++) { cin >> tab[i].first >> co[i] >> tab[i].second; if (tab[i].first > tab[i].second) { swap(tab[i].first, tab[i].second); if (co[i][0] == '>') { co[i][0] = '<'; } else { if (co[i][0] == '<') { co[i][0] = '>'; } } } if (tab[i].first == tab[i].second && co[i] != "=" && co[i] != ">=" && co[i] != "<=") { printf("0"); return 0; } lew[tab[i].first].push_back(i); pra[tab[i].second].push_back(i); } for (int i = 1; i < 2 * n; i++) { czy = 1; for (int j = 0; j < lew[i].size(); j++) { u = lew[i][j]; if (tab[u].second == (i + 1) && (co[u] == ">" || co[u] == "<")) { czy = 0; break; } } if (czy) { dp[i][i + 1] = 1; } } for (int kon = 1; kon <= 2 * n; kon++) { for (int pocz = kon; pocz; pocz--) { if ((kon - pocz + 1) & 1) continue; if (pocz > 2) { czy = 1; for (int j = 0; j < lew[pocz - 2].size(); j++) { u = lew[pocz - 2][j]; if (tab[u].second == (pocz - 1) && (co[u] == ">" || co[u] == "<")) { czy = 0; break; } if (tab[u].second >= pocz && tab[u].second <= kon && (co[u] == "=" || co[u] == ">" || co[u] == ">=")) { czy = 0; break; } } for (int j = 0; j < lew[pocz - 1].size(); j++) { u = lew[pocz - 1][j]; if (tab[u].second >= pocz && tab[u].second <= kon && (co[u] == "=" || co[u] == ">" || co[u] == ">=")) { czy = 0; break; } } if (czy) { dp[pocz - 2][kon] += dp[pocz][kon]; } } if (pocz > 1 && kon < 2 * n) { czy = 1; for (int j = 0; j < lew[pocz - 1].size(); j++) { u = lew[pocz - 1][j]; if (tab[u].second == (kon + 1) && (co[u] == ">" || co[u] == "<")) { czy = 0; break; } if (tab[u].second >= pocz && tab[u].second <= kon && (co[u] == "=" || co[u] == ">" || co[u] == ">=")) { czy = 0; break; } } for (int j = 0; j < pra[kon + 1].size(); j++) { u = pra[kon + 1][j]; if (tab[u].first >= pocz && tab[u].first <= kon && (co[u] == "=" || co[u] == "<" || co[u] == "<=")) { czy = 0; break; } } if (czy) { dp[pocz - 1][kon + 1] += dp[pocz][kon]; } } if (kon < 2 * n - 1) { czy = 1; for (int j = 0; j < pra[kon + 2].size(); j++) { u = pra[kon + 2][j]; if (tab[u].first == (kon + 1) && (co[u] == ">" || co[u] == "<")) { czy = 0; break; } if (tab[u].first >= pocz && tab[u].first <= kon && (co[u] == "=" || co[u] == "<" || co[u] == "<=")) { czy = 0; break; } } for (int j = 0; j < pra[kon + 1].size(); j++) { u = pra[kon + 1][j]; if (tab[u].first >= pocz && tab[u].first <= kon && (co[u] == "=" || co[u] == "<" || co[u] == "<=")) { czy = 0; break; } } if (czy) { dp[pocz][kon + 2] += dp[pocz][kon]; } } } } printf("%lld", dp[1][2 * n]); return 0; }
6
#include <bits/stdc++.h> using namespace std; const int mxN = 5e5 + 5; const int mod = 1e9 + 7; const int INF = 1e9; void solve() { int n; cin >> n; vector<array<int, 2>> ans; int xorsum[n]; int degree[n]; queue<int> Q; int used = 0; for (int i = 0; i < n; i++) { cin >> degree[i] >> xorsum[i]; if (degree == 0) { used++; if (xorsum[i] != 0) assert(false); } if (degree[i] == n && xorsum[i] >= n) { assert(false); } if (degree[i] == 1) { Q.push(i); } } while (!Q.empty()) { int v = Q.front(); Q.pop(); if (degree[v] == 0) continue; if (degree[v] != 1) { assert(false); } int u = xorsum[v]; xorsum[v] = 0; degree[v]--; degree[u]--; xorsum[u] ^= v; ans.push_back({v, u}); if (degree[u] == 1) { Q.push(u); } } cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) { cout << ans[i][0] << " " << ans[i][1] << endl; } } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout << setprecision(12) << fixed; solve(); return 0; }
1
//#pragma comment(linker, "/STACK:60777216") #include <cassert> #include<iostream> #include<algorithm> #include<cstdio> #include<complex> #include<vector> #include<cstring> #include<set> #include<map> #include<cmath> #include<queue> #include<string> #include<cstdlib> #include<memory.h> #include<ctime> #include<bitset> #include<fstream> #include<queue> #include<stack> #include<unordered_map> #include<unordered_set> using namespace std; typedef long double ld; typedef long long ll; typedef pair<int,int> pii; typedef pair<int,pii> p3; typedef pair<ll,ll> pl; typedef pair<int,pl> p3l; typedef pair<double,double> pdd; typedef vector<int> vi; typedef vector<ld> vd; #define FOR(i,a,b) for(int i=(a);i<(b);i++) #define REP(i,n) FOR(i,0,n) #define SORT(v) sort((v).begin(),(v).end()) #define UN(v) SORT(v),(v).erase(unique((v).begin(),(v).end()),(v).end()) #define CL(a,b) memset(a,b,sizeof a) #define pb push_back int main(){ #ifdef LocalHost freopen("input.txt","r",stdin); //freopen("output.txt","w",stdout); #endif int tc; cin>>tc; REP(TC,tc){ int n; string s; cin>>n; getline(cin,s); getline(cin,s); string a=s,b=s; int n1=0; REP(i,n)if(s[i]=='1')n1++; bool g=1; if(n1%2==0){ int t=0; REP(i,n)if(s[i]=='1'){ if(t<n1/2){ a[i]='('; b[i]='('; }else{ a[i]=b[i]=')'; } t++; } int s1=0,s2=0; REP(i,n){ if(s[i]=='0'){ if(s1>s2){ a[i]=')'; b[i]='('; }else{ a[i]='('; b[i]=')'; } } if(a[i]=='(')s1++; else s1--; if(b[i]=='(')s2++; else s2--; if(s1<0 || s2<0)g=0; } //cout<<s1<<' '<<s2<<' '<<a<<' '<<b<<endl; if(s1||s2)g=0; }else g=0; if(!g)puts("NO"); else cout<<"YES\n"<<a<<"\n"<<b<<"\n"; } #ifdef LocalHost printf("TIME: %.3lf\n",ld(clock())/CLOCKS_PER_SEC); #endif return 0; }
1
#include <bits/stdc++.h> using namespace std; struct fff { int x, y; } mark[1000010]; int cmpx(fff a, fff b) { return a.x < b.x; } int cmpy(fff a, fff b) { return a.y < b.y; } int n, m; char graph[1010][1010]; int x[1010]; int y[1010]; int main() { scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) scanf("%s", graph[i]); memset(x, 0, sizeof(x)); memset(y, 0, sizeof(y)); double div = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (graph[i][j] == '.') { div++; x[i]++; y[j]++; } double ans = 0; for (int i = 0; i < n; i++) for (int j = i + 1; j < n; j++) ans += 2.0 * fabs(j - i) * x[i] * x[j]; for (int i = 0; i < m; i++) for (int j = i + 1; j < m; j++) ans += 2.0 * fabs(j - i) * y[i] * y[j]; int pos = 0; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if (graph[i][j] == 'X') { mark[pos].x = i; mark[pos].y = j; pos++; } sort(mark, mark + pos, cmpx); for (int i = 0; i < pos; i++) { int up = mark[i].y; int down = m - 1 - mark[i].y; int flag = 0; int j = i + 1; ans += 4.0 * up * down; while (j < pos && mark[j].x == mark[j - 1].x + 1) { if (mark[j].y > mark[j - 1].y) { if (flag == 0) flag = 1; else if (flag == 2) break; } else if (mark[j].y < mark[j - 1].y) { if (flag == 0) flag = 2; else if (flag == 1) break; } int addup = mark[j].y; int adddown = m - 1 - mark[j].y; if (flag == 1) ans += 4.0 * up * adddown; else ans += 4.0 * down * addup; j++; } } sort(mark, mark + pos, cmpy); for (int i = 0; i < pos; i++) { int left = mark[i].x; int right = n - 1 - mark[i].x; int flag = 0; int j = i + 1; ans += 4.0 * left * right; while (j < pos && mark[j].y == mark[j - 1].y + 1) { if (mark[j].x > mark[j - 1].x) { if (flag == 0) flag = 2; else if (flag == 1) break; } else if (mark[j].x < mark[j - 1].x) { if (flag == 0) flag = 1; else if (flag == 2) break; } int addleft = mark[j].x; int addright = n - 1 - mark[j].x; if (flag == 1) ans += 4.0 * right * addleft; else ans += 4.0 * left * addright; j++; } } printf("%.8lf\n", ans / div / div); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; int m, n; char s[maxn]; int main() { scanf("%d%s", &n, s + 1); for (register int i = 1; i <= n; ++i) { m += (s[i] == '(' ? 1 : -1); if (m < -1) { puts("No"); return 0; } } puts(m ? "No" : "Yes"); return 0; }
3
#include <bits/stdc++.h> using namespace std; using ll = long long; const int MOD = 1e9 + 7, N = 2e5 + 10; const int M = 400; int st[N]; int seg[M][M]; int strt[N]; int32_t main() { ios::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; vector<array<int, 2>> a(n); for (int i = 0, x, y; i < n; ++i) { cin >> x >> y; a[i] = {x, y}; } auto update = [&](int x, int y, int inc) { st[x] += inc; st[y + 1] -= inc; }; int ans = 0; for (int i = 0; i < m; ++i) { if (i) st[i] += st[i - 1]; int op, k; cin >> op >> k; --k; auto [x, y] = a[k]; int inc = (op == 1 ? 1 : -1); if (op == 1) { strt[k] = i; } int s = x + y; if (x + y < M) { for (int j = strt[k] + x; j < strt[k] + x + y; ++j) { seg[s][j % s] += inc; } } else { for (int j = strt[k] + x; j < m; j += s) { if (j > i) { update(j, min(j + y - 1, m - 1), inc); } if (j <= i and j + y - 1 >= i) { update(i, min(j + y - 1, m - 1), inc); } } } int ans = st[i]; for (int j = 2; j < M; ++j) { ans += seg[j][i % j]; } cout << ans << '\n'; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int memo[1002][1002]; string s; int Calc(int from, int tarpos) { if (memo[from][tarpos] != -1) { return memo[from][tarpos]; } int i, tb, j; int ret = 0, cur; int afr, ato; for (i = from + tarpos + 1; i < s.size(); i++) { tb = i - tarpos + i - 1 - from; if (tb >= s.size()) break; if (s[tb] != s[from + tarpos]) continue; afr = i; cur = Calc(afr, i - tarpos - from - 1); ret = max(cur, ret); } memo[from][tarpos] = ret + 1; return ret + 1; } int main() { int i, j; int ans = 1, cur; memset(memo, -1, sizeof(memo)); cin >> s; cur = 0; for (i = 0; i < s.size(); i++) { if (s[i] == 'L') cur++; } for (i = 0; i + 1 < s.size(); i++) { cur = Calc(0, i); ans = max(cur, ans); } cout << ans << "\n"; }
2
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ll h, w, k; cin >> h >> w >> k; vector<string> S(h); for (auto &s : S) cin >> s; vector<vector<ll>> ANS(h, vector<ll>(w)); { int now = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if(S[i][j]=='#') { ANS[i][j] = ++now; } } } } for(int i = 0;i<h;i++) { for(int j = 1;j<w;j++) { if(ANS[i][j]==0)ANS[i][j] = ANS[i][j-1]; } for(int j = w-2;j>=0;j--) { if(ANS[i][j]==0)ANS[i][j] = ANS[i][j+1]; } } for(int i = 0;i<w;i++) { for(int j = 1;j<h;j++) { if(ANS[j][i]==0)ANS[j][i] = ANS[j-1][i]; } for(int j = h-2;j>=0;j--) { if(ANS[j][i]==0)ANS[j][i] = ANS[j+1][i]; } } for(int i = 0;i<h;i++) { for(int j = 0;j<w;j++) { if(j)cout << ' '; cout << ANS[i][j]; } cout << endl; } }
0
#include <bits/stdc++.h> using namespace std; long long int p = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; t = 1; cin >> t; while (t--) { long long int p, q; cin >> p >> q; if (p < q) { cout << p << '\n'; ; continue; } if (p >= q && p % q != 0) { cout << p << '\n'; ; continue; } long long int kk = q; long long int ans = 0; vector<pair<long long int, long long int> > v; for (long long int i = 2; i * i <= q; i++) { if (q % i == 0) { long long int count = 0; while (q % i == 0) { count++; q /= i; } v.push_back({i, count}); } } if (q != 1) { v.push_back({q, 1}); } for (int i = 0; i < v.size(); i++) { long long int val = v[i].first; long long int count = v[i].second; long long int k = p; while (k % val == 0) { k /= val; } val = k * pow(val, count - 1); ans = max(ans, val); } cout << ans << '\n'; ; } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long c[100005]; map<long long, long long> mama; int main() { long long n, i, j, k, ans = 0; long long sum = 0, x; scanf("%lld%lld", &n, &k); for (i = 0; i < n; i++) { scanf("%lld", &c[i]); sum += c[i]; } ans += c[0] * c[n - 1]; for (i = 0; i < n - 1; i++) { ans += (c[i] * c[i + 1]); } for (i = 0; i < k; i++) { scanf("%lld", &x); x--; long long temporary = sum; temporary -= c[x]; if (x == 0) { if (!mama[1]) temporary -= c[1]; if (!mama[n - 1]) temporary -= c[n - 1]; } else if (x == n - 1) { if (!mama[n - 2]) temporary -= c[n - 2]; if (!mama[0]) temporary -= c[0]; } else { if (!mama[x - 1]) temporary -= c[x - 1]; if (!mama[x + 1]) temporary -= c[x + 1]; } sum -= c[x]; mama[x]++; ans += temporary * c[x]; } printf("%lld", ans); return 0; }
2
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') f = (c == '-') ? -1 : 1, c = getchar(); while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int n; int a[200010]; int cnt[200010], t = 0; set<int> p, q; int sta[200010], top = 0; bool flag[200010]; inline bool cmp(int a, int b) { return a > b; } int main() { n = read(); for (int i = 1; i <= n; i++) a[i] = read(), cnt[a[i]]++; for (int i = 1; i <= n; i++) if (cnt[i] == 0) p.insert(i), t++; for (int i = 1; i <= n; i++) if (cnt[a[i]] > 1) { if (*p.begin() < a[i] || flag[a[i]]) { cnt[a[i]]--; a[i] = *p.begin(); p.erase(*p.begin()); } else flag[a[i]] = 1; } for (int i = n; i; i--) if (cnt[a[i]] > 1) { cnt[a[i]]--; set<int>::iterator it = p.end(); it--; a[i] = *it; p.erase(*it); } cout << t << endl; for (int i = 1; i <= n; i++) printf("%d ", a[i]); }
4
#include <bits/stdc++.h> using namespace std; int n; int m; int hang[100001]; int lie[100001]; int geshuhang; int geshulie; long long ans; int main() { while (scanf("%d %d", &n, &m) == 2) { int x; int y; ans = (long long)n * n; geshuhang = 0; geshulie = 0; memset(hang, 0, sizeof(hang)); memset(lie, 0, sizeof(lie)); for (int i = 1; i <= m; ++i) { int x; int y; scanf("%d %d", &x, &y); if (!hang[x]) { ans = ans - n + geshulie; hang[x] = 1; geshuhang++; } if (!lie[y]) { ans = ans - n + geshuhang; lie[y] = 1; geshulie++; } printf("%I64u ", ans); } } return 0; }
2
#include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> using namespace std; const int mod=1e9+7; const int maxn=1000; int n,m,k,ans=1,tot=1,tail,cnt,Time; int pre[maxn*2+8],now[maxn+8],son[maxn*2+8]; int fac[maxn*2+8],fact[maxn*2+8],st[maxn+8]; int dfn[maxn+8],low[maxn+8]; bool vis[maxn*2+8],color[maxn+8]; int read() { int x=0,f=1;char ch=getchar(); for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1; for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; return x*f; } void add(int u,int v) { pre[++tot]=now[u]; now[u]=tot; son[tot]=v; } int C(int n,int m){return 1ll*fac[n]*fact[m]%mod*fact[n-m]%mod;} int power(int a,int k) { int sum=1; for (;k;k>>=1,a=1ll*a*a%mod) if (k&1) sum=1ll*sum*a%mod; return sum; } int gcd(int n,int m){return n?gcd(m%n,n):m;} void tarjan(int x) { dfn[x]=low[x]=++Time; for (int p=now[x];p;p=pre[p]) { int child=son[p]; //if (child==fa||dfn[child]>dfn[x]) continue; if (color[p]) continue; color[p]=color[p^1]=1; st[++tail]=p; if (!dfn[child]) { tarjan(child); low[x]=min(low[x],low[child]); if (low[child]>=dfn[x]) { //puts("Link Start"); bool flag=1;int cnt=0,tmp=0; for (int i=tail;st[i+1]!=p;cnt++,i--) if (vis[son[st[i]]]) flag=0; else vis[son[st[i]]]=1; if (flag) { for (int i=1;i<=cnt;i++) tmp=(tmp+power(k,gcd(cnt,i)))%mod; tmp=1ll*tmp*power(cnt,mod-2)%mod; } else tmp=C(cnt+k-1,k-1); ans=1ll*ans*tmp%mod; while(st[tail+1]!=p) vis[son[st[tail]]]=0,tail--; // puts("Over"); } } else low[x]=min(low[x],dfn[child]); } } int main() { n=read(),m=read(),k=read(); fac[0]=fact[0]=1; for (int i=1;i<=m+k;i++) fac[i]=1ll*fac[i-1]*i%mod; fact[m+k]=power(fac[m+k],mod-2); for (int i=m+k-1;i;i--) fact[i]=1ll*fact[i+1]*(i+1)%mod; for (int i=1;i<=m;i++) { int u=read(),v=read(); add(u,v),add(v,u); } for (int i=1;i<=n;i++) if (!dfn[i]) tarjan(i); printf("%d\n",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long maxn = 2e5 + 50; const long long mod = 998244353; const long long base = 1e18; long long a[maxn]; long long b[maxn]; long long pos[maxn]; bool dd[maxn]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); if (fopen("t.inp", "r")) { freopen("test.inp", "r", stdin); freopen("test.out", "w", stdout); } long long t; cin >> t; while (t--) { long long n, k; cin >> n >> k; set<long long> st; for (int i = 1; i <= n; i++) { cin >> a[i]; pos[a[i]] = i; st.insert(i); } long long ans = 1; for (int i = 1; i <= k; i++) { cin >> b[i]; dd[pos[b[i]]] = 1; } for (int i = 1; i <= k; i++) { if (st.count(pos[b[i]])) { st.erase(pos[b[i]]); auto it = st.lower_bound(pos[b[i]]); long long h1 = -1; long long h2 = -1; if (it != st.end()) { long long p = *it; if (!dd[p]) { h2 = p; } } if (it != st.begin()) { it--; long long p = *it; if (!dd[p]) { h1 = p; } } if (h1 == -1 && h2 == -1) { ans = 0; break; } else if (h1 == -1 && h2 != -1) { st.erase(h2); } else if (h1 != -1 && h2 == -1) { st.erase(h1); } else { ans = ans * 2; ans %= mod; st.erase(h1); } st.insert(pos[b[i]]); dd[pos[b[i]]] = 0; } else { ans = 0; break; } } for (int i = 1; i <= n; i++) { dd[i] = 0; } cout << ans << "\n"; } }
2
#include<iostream> #include<cfloat> #include<cassert> #include<cmath> #include<vector> using namespace std; #define EPS (1e-8) #define equals(a, b) (fabs((a) - (b)) < EPS ) #define dle(a, b) (equals(a, b) || a < b ) static const double PI = acos(-1); class Point{ public: double x, y; Point ( double x = 0, double y = 0): x(x), y(y){} Point operator + ( Point p ){ return Point(x + p.x, y + p.y); } Point operator - ( Point p ){ return Point(x - p.x, y - p.y); } Point operator * ( double a ){ return Point(x*a, y*a); } Point operator / ( double a ){ return Point(x/a, y/a); } double abs() { return sqrt(norm());} double norm() { return x*x + y*y; } bool operator < ( const Point &p ) const { return x != p.x ? x < p.x : y < p.y; } bool operator == ( const Point &p ) const { return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; } }; typedef Point Vector; class Segment{ public: Point p1, p2; Segment(Point s = Point(), Point t = Point()): p1(s), p2(t){} }; typedef Segment Line; double norm( Vector a ){ return a.x*a.x + a.y*a.y; } double abs( Vector a ){ return sqrt(norm(a)); } Point polar( double a, double r ){ return Point(cos(r)*a, sin(r)*a);} double getDistance( Vector a, Vector b ){ return abs(a - b); } double dot( Vector a, Vector b ){ return a.x*b.x + a.y*b.y; } double cross( Vector a, Vector b ){ return a.x*b.y - a.y*b.x; } static const int COUNTER_CLOCKWISE = 1; static const int CLOCKWISE = -1; static const int ONLINE_BACK = 2; static const int ONLINE_FRONT = -2; static const int ON_SEGMENT = 0; // EPS can be 0 // need to check for 920, 833, 866 int ccw( Point p0, Point p1, Point p2 ){ Vector a = p1 - p0; Vector b = p2 - p0; if ( cross(a, b) > EPS ) return COUNTER_CLOCKWISE; if ( cross(a, b) < -EPS ) return CLOCKWISE; if ( dot(a, b) < -EPS ) return ONLINE_BACK; if ( norm(a) < norm(b) ) return ONLINE_FRONT; return ON_SEGMENT; } /* static const int COUNTER_CLOCKWISE = 1; static const int CLOCKWISE = -1; static const int ONLINE_BACK = 2; static const int ONLINE_FRONT = -2; static const int ON_SEGMENT = 0; */ void compute(Point p, Vector v, double D){ assert( 0.1 <= p.abs() && p.abs() <= 0.9 ); assert( 0.1 <= v.abs() && v.abs() <= 10.0 ); assert( 0.1 <= D && D <= 50.0 ); Point c = Point(0, 0); int dir = ccw(p, p+v, c); double dist = 0.0; if ( dir == ONLINE_FRONT || dir == ON_SEGMENT ){ dist = getDistance(c, p); } else if ( dir == ONLINE_BACK ){ dist = 2.0 - getDistance(c, p); } else { cout << "impossible" << endl; return; } if ( D < dist ){ cout << "impossible" << endl; } else { assert( D-(1.0e-3) > dist ); printf("%.12lf\n", dist); } } int main(){ Point p; Vector v; double D; while(1){ cin >> D; if ( D == 0 ) break; cin >> p.x >> p.y >> v.x >> v.y; compute(p, v, D); } return 0; }
0
#include<bits/stdc++.h> using namespace std; struct var{ long long to,nxt; }edge[201001]; long long u,v,c[101001],x1,x2,n,m,head[101001],cnt; bool flag; void add(long long u,long long v) { cnt++; edge[cnt].to=v; edge[cnt].nxt=head[u]; head[u]=cnt; } void dfs(long long o,long long cc) { c[o]=cc; if(c[o]==1)x1++; else x2++; for(int i=head[o];i;i=edge[i].nxt) { int v=edge[i].to; if(c[v]) { if(c[v]==c[o]) { flag=true; return ; } }else { dfs(v,3-cc); } } } int main() { scanf("%lld%lld",&n,&m); for(int i=1;i<=m;i++) { scanf("%lld%lld",&u,&v); add(u,v); add(v,u); } dfs(1,1); if(flag) { printf("%lld",n*(n-1)/2-m); }else printf("%lld",x1*x2-m); }
0
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t; cin >> t; while (t--) { long long n; cin >> n; vector<long long> a(n); for (long long i = 0; i < n; i++) cin >> a[i]; priority_queue<pair<long long, long long> > pq; for (long long i = 0; i < n; i++) { pq.push({a[i], i + 1}); } vector<vector<long long> > ans; pair<long long, long long> s = pq.top(); pq.pop(); pair<long long, long long> t = pq.top(); pq.pop(); bool fl = true; while (fl) { if (s.first <= 0 || t.first <= 0) { break; } vector<long long> temp; temp.push_back(s.second); temp.push_back(t.second); ans.push_back(temp); s.first--; t.first--; if (s.first > 0) { pq.push(s); } if (t.first > 0) { pq.push(t); } if (pq.empty()) { break; } if (!pq.empty()) { s = pq.top(); pq.pop(); } if (!pq.empty()) { t = pq.top(); pq.pop(); } } cout << ans.size() << "\n"; if (ans.size() != 0) { for (auto i : ans) { for (auto j : i) { cout << j << " "; } cout << "\n"; } } } return 0; }
4
#include <bits/stdc++.h> using namespace std; const long long linf = 1e18 + 5; const int mod = 1e9 + 7; const int logN = 16 + 1; const int inf = 1e9 + 0; const int N = 1e5 + 5; int n, q, x, l, r; map<int, int> F, h; void update(int x, int y) { for (; x <= n; x += x & -x) F[x] += y; } int query(int x) { int sum = 0; for (; x > 0; x -= x & -x) sum += F[x]; return sum; } int main() { scanf("%d %d", &n, &q); set<pair<int, int>, greater<pair<int, int> > > S; set<pair<int, int> > S2; S.insert(make_pair(n, 1)); S2.insert(make_pair(1, n)); for (int i = 1; i <= q; i++) { scanf("%d", &x); if (x == 0) { scanf("%d %d", &l, &r); printf("%d\n", query(r) - query(l - 1)); } else { if (h[x]) { update(h[x], -1); int left = h[x], right = h[x]; h[x] = 0; set<pair<int, int> >::iterator it = S2.lower_bound( make_pair(left, right)), it2 = it; if (it != S.begin()) --it2; if (it->first == right + 1) { right = it->second; S.erase(S.find(make_pair(it->second - it->first + 1, it->first))); S2.erase(it); } if (it2->second == left - 1) { left = it2->first; S.erase(S.find(make_pair(it2->second - it2->first + 1, it2->first))); S2.erase(it2); } S.insert(make_pair(right - left + 1, left)); S2.insert(make_pair(left, right)); } else { pair<int, int> temp = *S.begin(); S.erase(S.begin()); S2.erase(S2.find(make_pair(temp.second, temp.first + temp.second - 1))); update(h[x] = temp.first / 2 + temp.second, 1); if (temp.second < h[x]) { S.insert(make_pair(h[x] - temp.second, temp.second)); S2.insert(make_pair(temp.second, h[x] - 1)); } if (h[x] < temp.first + temp.second - 1) { S.insert(make_pair(temp.second + temp.first - h[x] - 1, h[x] + 1)); S2.insert(make_pair(h[x] + 1, temp.first + temp.second - 1)); } } } } return 0; }
4
#include <bits/stdc++.h> #pragma GCC optimize(3) using namespace std; bool Finish_read; template <class T> inline void read(T &x) { Finish_read = 0; x = 0; int f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; if (ch == EOF) return; ch = getchar(); } while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); x *= f; Finish_read = 1; } template <class T> inline void print(T x) { if (x / 10 != 0) print(x / 10); putchar(x % 10 + '0'); } template <class T> inline void writeln(T x) { if (x < 0) putchar('-'); x = abs(x); print(x); putchar('\n'); } template <class T> inline void write(T x) { if (x < 0) putchar('-'); x = abs(x); print(x); } const int maxn = 100005, maxnode = 16600005; int mx, my, mz, n, m, k, lx, rx, ly, ry, lz, rz, cnt, lc[maxnode], rc[maxnode], lv[maxnode], rv[maxnode], rt[maxn << 2]; inline bool Open(int x, int y, int z) { return lx <= x && x <= rx && ly <= y && y <= ry && lz <= z && z <= rz; } inline void update(int &o, int l, int r, int y, int z) { if (!o) o = ++cnt, rv[o] = mz + 1; if (lz <= z) rv[o] = min(rv[o], z); if (z <= rz) lv[o] = max(lv[o], z); if (l == r) return; int mid = (l + r) >> 1; if (y <= mid) update(lc[o], l, mid, y, z); else update(rc[o], mid + 1, r, y, z); } inline void modify(int o, int l, int r, int x, int y, int z) { update(rt[o], 1, my, y, z); if (l == r) return; int mid = (l + r) >> 1; if (x <= mid) modify(o << 1, l, mid, x, y, z); else modify(o << 1 | 1, mid + 1, r, x, y, z); } inline bool getans(int o, int l, int r, int yl, int yr, int zl, int zr) { if (!o) return 1; if (yl <= l && r <= yr) return lv[o] < zl && zr < rv[o]; bool res = 1; int mid = (l + r) >> 1; if (yl <= mid) res &= getans(lc[o], l, mid, yl, yr, zl, zr); if (yr > mid) res &= getans(rc[o], mid + 1, r, yl, yr, zl, zr); return res; } inline bool query(int o, int l, int r, int xl, int xr, int yl, int yr, int zl, int zr) { if (xl <= l && r <= xr) return getans(rt[o], 1, my, yl, yr, zl, zr); bool res = 1; int mid = (l + r) >> 1; if (xl <= mid) res &= query(o << 1, l, mid, xl, xr, yl, yr, zl, zr); if (xr > mid) res &= query(o << 1 | 1, mid + 1, r, xl, xr, yl, yr, zl, zr); return res; } inline void solve(int x, int y, int z) { if (Open(x, y, z)) return (void)puts("OPEN"); if (query(1, 1, mx, min(lx, x), max(rx, x), min(ly, y), max(ry, y), min(lz, z), max(rz, z))) return (void)puts("UNKNOWN"); return (void)puts("CLOSED"); } int main() { read(mx), read(my), read(mz), read(n), read(m), read(k); lx = mx, ly = my, lz = mz; for (int i = 1, x, y, z; i <= n; ++i) { read(x), read(y), read(z); lx = min(lx, x), rx = max(rx, x); ly = min(ly, y), ry = max(ry, y); lz = min(lz, z), rz = max(rz, z); } for (int i = 1, x, y, z; i <= m; ++i) { read(x), read(y), read(z); if (Open(x, y, z)) return 0 * puts("INCORRECT"); modify(1, 1, mx, x, y, z); } puts("CORRECT"); for (int i = 1, x, y, z; i <= k; ++i) read(x), read(y), read(z), solve(x, y, z); }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 80; int dp[maxn][maxn][maxn][3]; char str[maxn]; int n; int arr[maxn], cnt[3]; int bef[3][maxn][3]; int main() { scanf("%d%s", &n, str); for (int i = 0; i < n; i++) { if (str[i] == 'V') arr[i] = 0; else if (str[i] == 'K') arr[i] = 1; else arr[i] = 2; for (int j = 0; j < 3; j++) bef[arr[i]][cnt[arr[i]]][j] = cnt[j]; cnt[arr[i]]++; } memset(dp, 0x3f, sizeof(dp)); dp[0][0][0][2] = 0; for (int i = 0; i < n; i++) { for (int s0 = 0; s0 <= min(i, cnt[0]); s0++) for (int s1 = 0; s0 + s1 <= i && s1 <= cnt[1]; s1++) for (int last = 0; last < 3; last++) { int s2 = i - s0 - s1; if (s0 < cnt[0]) { int sbef = max(0, bef[0][s0][1] - s1) + max(0, bef[0][s0][2] - s2); dp[i + 1][s0 + 1][s1][0] = min(dp[i + 1][s0 + 1][s1][0], dp[i][s0][s1][last] + sbef); } if (s1 < cnt[1] && last != 0) { int sbef = max(0, bef[1][s1][0] - s0) + max(0, bef[1][s1][2] - s2); dp[i + 1][s0][s1 + 1][1] = min(dp[i + 1][s0][s1 + 1][1], dp[i][s0][s1][last] + sbef); } if (s2 < cnt[2]) { int sbef = max(0, bef[2][s2][0] - s0) + max(0, bef[2][s2][1] - s1); dp[i + 1][s0][s1][2] = min(dp[i + 1][s0][s1][2], dp[i][s0][s1][last] + sbef); } } } int ans = 1000000000; for (int i = 0; i < 3; i++) ans = min(dp[n][cnt[0]][cnt[1]][i], ans); cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:400000000") double S, a, b, c; double f(double x, double y) { if (x == 0) return 0; else return exp(x * y); } int main() { cin >> S >> a >> b >> c; if (a + b + c < 5e-12) cout << "0 0 0"; else { double d = a + b + c; printf("%.11f %.11f %.11f", S * a / d - 5e-12, S * b / d - 5e-12, S * c / d - 5e-12); } }
2
#include "bits/stdc++.h" using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; const int INF = 1e9; const ll LINF = 1e18; template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; } template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; } template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; } template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; } /* <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2767> 問題文============================================================ ================================================================= 解説============================================================= ================================================================ */ ll solve(){ ll res = 0; ll N; cin >> N; string S; cin >> S; vector<int> alpha; for(int i = 0; i < 26;i++){ int x = (int)count(S.begin(),S.end(),'a'+i); if(x == 0) continue; alpha.push_back(x); } for(int x = 1; x <= 9; x++){ int y = (int)count(alpha.begin(),alpha.end(),x); if(y == 0) continue; if(x == 1){ res += (2*y-1); }else{ if(y == 1){ res += 3*y; }else{ res += (4+2*y-1); } } } sort(alpha.begin(),alpha.end()); alpha.erase(unique(alpha.begin(),alpha.end()),alpha.end()); res += alpha.size()-1; return res; } int main(void) { cin.tie(0); ios_base::sync_with_stdio(false); cout << solve() << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long int n, k, count = 0, temp = 0; cin >> n >> k; while (n != 0) { if (n % k == 0) { n = n / k; count++; } else { temp = n % k; n = n - n % k; count += temp; } } cout << count << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long int power(long long int x, long long int y) { if (y == 0) return 1; else if (y % 2 == 0) return power(x, y / 2) * power(x, y / 2); else return x * power(x, y / 2) * power(x, y / 2); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n; cin >> n; long long int a[n]; unordered_map<long long int, long long int> m; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); long long int p = n / 2; if (a[0] ^ a[p]) cout << "Alice" << endl; else cout << "Bob" << endl; return 0; }
5
#include <bits/stdc++.h> #define N 5005 #define INF 10000000000000LL using namespace std; int n,A,B,p[N],pos[N],cnt[N][N]; long long f[N][N]; int main(){ scanf("%d%d%d",&n,&A,&B); for(int i=1;i<=n;++i){ scanf("%d",&p[i]); pos[p[i]]=i; } for(int i=n;i;--i) for(int j=1;j<=n;++j) if(pos[j]>=i)cnt[i][j]=cnt[i+1][j]+(j<=p[i]); for(int j=n;j;--j) for(int i=n;i;--i) if(!cnt[i][j])f[i][j]=f[i][j+1]; else{ f[i][j]=min(f[i][j+1]+B,f[pos[j]+1][j+1]+1LL*A*(cnt[i][j]-1)); //printf("cnt=%d f[%d][%d]=",cnt[i][j]-1,i,j),cout<<f[i][j]<<endl; } cout<<f[1][1]<<endl; }
0
#include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(false); cin.tie(0); long double x; cin >> x; long double DELTA__ = (long long)1e18; long double o, p; for (long double a = 1; a <= 10; a += 1) for (long double h = 1; h <= 10; h += 1) if (fabs(a * a * h * h / (a * a + 4 * h * h) - x * x) <= DELTA__) { DELTA__ = fabs(a * a * h * h / (a * a + 4 * h * h) - x * x); o = a; p = h; } cout << o << ' ' << p << '\n'; }
5
#include <bits/stdc++.h> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::string fst, snd, message; std::cin >> fst >> snd >> message; std::map<char, char> transform; for (size_t i = 0; i < 26; ++i) { transform[fst[i]] = snd[i]; transform[fst[i] + 'A' - 'a'] = snd[i] + 'A' - 'a'; } for (size_t i = 0; i < 10; ++i) { transform['0' + i] = '0' + i; } std::string correct; for (auto symbol : message) { correct.push_back(transform[symbol]); } std::cout << correct << '\n'; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main(){ long long N,ans=0; cin >> N; for(long long int i = 1;i * i <= N; ++i){ if(N%i==0){ if(N/i>i+1){ ans += N/i-1; } } } cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; int a[200005], b[200005]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; b[i] = 0x7fffffff; } int q = -1; for (int i = 0; i < n; i++) { if (a[i] == 0) { q = i; } if (q != -1) b[i] = i - q; } q = -1; for (int i = n - 1; i >= 0; i--) { if (a[i] == 0) { q = i; } if (q != -1) { b[i] = min(b[i], q - i); } } for (int i = 0; i < n; i++) { cout << b[i] << ' '; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL); ; int tc; cin >> tc; while (tc--) { int a, b, x, y; cin >> a >> b >> x >> y; x++; y++; long long l1 = (x - 1) * b; long long l2 = (a - x) * b; long long l3 = (y - 1) * a; long long l4 = (b - y) * a; cout << max(l1, max(l2, max(l3, l4))) << "\n"; } }
1
#include <bits/stdc++.h> using namespace std; typedef long long int ll; typedef pair<ll, ll> P; typedef vector<ll> VI; typedef vector<VI> VVI; const ll MOD = 1000000007; const ll INF = 2147483647; const ll LINF = 4223372036854775807; #define REP(i, n) for (int i = 0; i < n; i++) #define ALL(v) v.begin(), v.end() int amax=0; struct edge { ll to, cost, time; }; vector<edge> G[200]; vector<P> cd(200); VI d(600000,LINF); void dijkstra(int s) { d[s] = 0; priority_queue<P, vector<P>, greater<P> > que; que.push(P(0, s)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second/amax; if (d[p.second] < p.first) continue; REP(i,G[v].size()) { edge e = G[v][i]; bool ok=1; int have = p.second%amax; for(int j=0;ok;j++){ if(have>=amax){ ok=0; have=amax-1; } if(have>=e.cost){ if(d[e.to*amax+have-e.cost]>d[p.second]+e.time+(cd[v].second*j)){ d[e.to*amax+have-e.cost]=d[p.second]+e.time+(cd[v].second*j); que.push(P(d[e.to*amax+have-e.cost], e.to*amax+have-e.cost)); } } have+=cd[v].first; } } } } int main() { int n, m, s; cin >> n >> m >> s; REP(i,m){ int u, v, a, b; cin >> u >> v >> a >> b; u--; v--; G[u].push_back({v,a,b}); G[v].push_back({u,a,b}); amax=max(amax,a*(n-1)); } REP(i,n){ cin >> cd[i].first >> cd[i].second; } amax++; dijkstra(min(s,amax-1)); for(int i=1;i<n;i++){ ll ans=LINF; REP(j,amax){ ans=min(ans,d[i*amax+j]); } cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int a[1005]; int msk[1005]; int main() { int n; ignore = scanf("%d", &n); for (int i = 0; i < n; i++) { char buf[5]; ignore = scanf(" %d %s", a + i, buf); int m = strlen(buf); for (int j = 0; j < m; j++) { msk[i] |= (1 << (buf[j] - 'A')); } } int mnA = 10000000, mnB = 10000000, mnC = 10000000; for (int i = 0; i < n; i++) { if (msk[i] & 1) { mnA = min(mnA, a[i]); } if (msk[i] & 2) { mnB = min(mnB, a[i]); } if (msk[i] & 4) { mnC = min(mnC, a[i]); } } if (mnA == 10000000 || mnB == 10000000 || mnC == 10000000) { printf("-1"); return 0; } int ans = mnA + mnB + mnC; for (int i = 0; i < n; i++) { if (msk[i] == 7) { ans = min(ans, a[i]); } } for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if ((msk[i] | msk[j]) == 7) { ans = min(ans, a[i] + a[j]); } } } printf("%d", ans); }
2
#include <bits/stdc++.h> using namespace std; int n; int v[2][1000000 + 10]; void solve(int n, int p) { if (n == 0) { v[p][0] = 0; return; } if (!(n & (n + 1))) { for (int i = 0; i <= n; ++i) { v[p][i] = n - i; } } else { int two = 1; while (two * 2 < n + 1) two *= 2; solve(n - two, p ^ 1); for (int i = 0; i <= n - two; ++i) { v[p][i] = two + v[p ^ 1][i]; } int cur = two; for (int i = two; i <= n; ++i) { --cur; v[p][i] = cur; } for (int i = n - two + 1; i < two; ++i) { --cur; v[p][i] = cur; } } } int main() { cin >> n; long long s = 0; for (int i = 0; i <= n; ++i) s += i; cout << s + s << endl; solve(n, 0); for (int i = 0; i <= n; ++i) { printf("%d ", v[0][i]); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int message[100010]; int key[100010]; int main() { int n, m, c; while (cin >> n >> m >> c) { int i, j; for (i = 0; i < n; i++) cin >> message[i]; for (i = 0; i < m; i++) cin >> key[i]; for (i = 0; i <= n - m; i++) { int k = 0; for (j = i; j <= i + m - 1; j++) { message[j] = (message[j] + key[k]) % c; k++; } } for (i = 0; i < n; i++) cout << message[i] << " "; cout << endl; } return 0; }
4
#include<bits/stdc++.h> using namespace std; long long int x[500010]; int mod = 1e9 + 7; int cnt[61]; long long int ok[61]; long long int okn[61]; void solve() { int n; cin>>n; memset(cnt,0,sizeof cnt); memset(ok,0,sizeof ok); memset(okn,0,sizeof okn); for(int i = 0; i < n; i++) { cin>>x[i]; for(int j = 59; j >= 0; j--) { if(x[i] & (1LL << j)) { cnt[j]++; } } } okn[0] = n; for(int j = 0; j <= 59; j++) { if(j > 0) okn[j] = okn[j - 1] * 2 % mod; ok[j] = cnt[j]; if(j >= 30) { ok[j] = (ok[j] << 30) % mod; ok[j] = (ok[j] << (j - 30)) % mod; } else { ok[j] = (ok[j] << j) % mod; } } int ans = 0; for(int i = 0; i < n; i++) { long long int sumand = 0; long long int sumor = 0; for(int j = 59; j >= 0; j--) { if(x[i] & (1LL << j)) { sumand = (sumand + ok[j]); sumor = (sumor + okn[j]); } else { sumor = (sumor + ok[j]); } } sumand %= mod; sumor %= mod; ans = (ans + (sumand * sumor)) % mod; } cout<<ans<<"\n"; } int main() { #ifndef ONLINE_JUDGE freopen("test.inp", "r", stdin); freopen("test.out", "w", stdout); #endif int t; cin>>t; while(t--) solve(); }
5
#include <bits/stdc++.h> using namespace std; int main() { set<int> m; unsigned long long s = 0; set<int>::iterator it; int n, x; cin >> n; while (n--) { cin >> x; if (m.find(x) == m.end()) m.insert(x); else while (m.find(x) != m.end()) x--; if (x != 0) m.insert(x); } for (it = m.begin(); it != m.end(); it++) s += *it; cout << s; }
2
#include <bits/stdc++.h> using namespace std; vector<int> v(1505); int n, m, l, r, k, s; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> v[i]; for (int i = 1; i <= n - 1; i++) { for (int j = i + 1; j <= n; j++) { if (v[j] < v[i]) k++; } } cin >> m; k %= 2; while (m--) { cin >> l >> r; s = (r - l + 1) >> 1; if (s % 2) k ^= 1; if (k % 2) cout << "odd\n"; else cout << "even\n"; } return 0; }
4
#include<cstdio> #include<iostream> #include<cmath> using namespace std; long long n; long long f[400]; int main() { scanf("%d",&n); f[0]=2; for(int i=0;i<=n;i++) f[i]=f[i-1]*2+2; cout<<f[n]<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; char ch[8]; int v[1001]; int n; void umplere(int j, int p) { int i; if (p == 1) { for (i = j - 1; i >= 1; i--) v[i]++; } else { for (i = j + 1; i <= n; i++) v[i]++; } } int main() { int i, m, p, j, maxim1 = 0, x; cin >> n >> m; for (i = 1; i <= m; i++) { cin >> ch + 1; cin >> ch + 1; cin >> ch + 1; if (ch[1] == 'l') p = 1; else p = 0; cin >> ch + 1; cin >> j; umplere(j, p); } for (i = 1; i <= n; i++) { if (v[i] == m) maxim1++; } if (maxim1 > 0) cout << maxim1; if (maxim1 == 0) cout << -1; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int cancollect[5000]; int frombefore[5000]; for (int i = 0; i < 5000; i++) { cancollect[i] = 0; frombefore[i] = 0; } int n, v; cin >> n >> v; for (int i = 0; i < n; i++) { int a, b; cin >> a >> b; cancollect[a] += b; } long long ans = 0; for (int i = 0; i < 4000; i++) { int maxcancollect = cancollect[i] + frombefore[i]; if (v < maxcancollect) { ans += v; maxcancollect -= v; } else { ans += maxcancollect; continue; } frombefore[i + 1] = min(maxcancollect, cancollect[i]); } cout << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long int spf[5000001], cum_sum[5000001] = {0}; vector<long long int> factors; void store() { for (long long int i = 1; i <= 5000000; i++) i % 2 ? spf[i] = i : spf[i] = 2; for (long long int i = 3; i * i <= 5000000; i += 2) { if (spf[i] == i) { for (long long int j = (i * i); j <= 5000000; j += i) { if (spf[j] == j) { spf[j] = i; } } } } } void prime_factorization(long long int n) { while (n != 1) { factors.push_back(spf[n]); n /= spf[n]; } } int main() { long long int q, a, b, sum = 0; store(); cum_sum[1] = 1; for (long long int i = 2; i <= 5000000; i++) { prime_factorization(i); cum_sum[i] = cum_sum[i - 1] + factors.size(); factors.clear(); } scanf("%lld", &q); while (q--) { scanf("%lld %lld", &a, &b); printf("%lld\n", cum_sum[a] - cum_sum[b]); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int get(int n, int m, int a, int b) { int line1 = (a - 1) / m, r1 = m - (a - 1) % m; int line2 = (b - 1) / m, r2 = (b - 1) % m + 1; if (line1 == line2) { return 1; } if (r1 == m && r2 == m) { return 1; } if (line1 + 1 == line2) { return 2; } if (r1 + r2 == m) { return 2; } if (r1 == m || r2 == m) { return 2; } return 3; } int main() { int n, m, a, b; while (cin >> n >> m >> a >> b) { if (b == n && n % m != 0) { b = n = n + m - n % m; } cout << get(n, m, a, b) << endl; } }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 1510; complex<long long> cmpt; bool pcomp(complex<long long> a, complex<long long> b) { a -= cmpt; b -= cmpt; return a.real() * b.imag() > a.imag() * b.real(); } bool comp(const pair<complex<long long>, int> &pa, const pair<complex<long long>, int> &pb) { return pcomp(pa.first, pb.first); } bool compxy(const pair<complex<long long>, int> &a, const pair<complex<long long>, int> &b) { return pair<int, int>(a.first.real(), a.first.imag()) < pair<int, int>(b.first.real(), b.first.imag()); } int N; vector<int> adj[MAXN]; int sub[MAXN], par[MAXN]; pair<complex<long long>, int> A[MAXN]; int ans[MAXN]; int dfsz(int x, int p) { par[x] = p; sub[x] = 1; for (int t : adj[x]) { if (t != p) { sub[x] += dfsz(t, x); } } return sub[x]; } void dfs(int x, vector<pair<complex<long long>, int> > vp) { iter_swap(min_element((vp).begin(), (vp).end(), compxy), vp.rbegin()); pair<complex<long long>, int> anchor = vp.back(); vp.pop_back(); ans[anchor.second] = x; cmpt = anchor.first; sort((vp).begin(), (vp).end(), comp); int psum = 0; for (int t : adj[x]) { if (t == par[x]) { continue; } dfs(t, vector<pair<complex<long long>, int> >(vp.begin() + psum, vp.begin() + psum + sub[t])); psum += sub[t]; } } int main() { if (fopen("input.txt", "r")) { freopen("input.txt", "r", stdin); } scanf("%d", &N); if (N <= 3) { for (int i = 1; i <= N; i++) { printf("%d ", i); } puts(""); return 0; } for (int i = 1, x, y; i < N; i++) { scanf("%d %d", &x, &y); adj[x].push_back(y); adj[y].push_back(x); } for (int i = 1, x, y; i <= N; i++) { scanf("%d %d", &x, &y); A[i] = pair<complex<long long>, int>(complex<long long>(x, y), i); } dfsz(1, 0); dfs(1, vector<pair<complex<long long>, int> >(A + 1, A + N + 1)); for (int i = 1; i <= N; i++) { printf("%d ", ans[i]); } puts(""); }
3
#include<bits/stdc++.h> using namespace std; #define CO const #define IN inline typedef long long int64; template<class T> IN T read(){ T x=0,w=1;char c=getchar(); for(;!isdigit(c);c=getchar())if(c=='-') w=-w; for(;isdigit(c);c=getchar()) x=x*10+c-'0'; return x*w; } template<class T> IN T read(T&x){ return x=read<T>(); } CO int mod=1e9+7; IN int add(int a,int b){ return (a+=b)>=mod?a-mod:a; } IN int mul(int a,int b){ return (int64)a*b%mod; } IN int fpow(int a,int b){ int ans=1; for(;b;b>>=1,a=mul(a,a)) if(b&1) ans=mul(ans,a); return ans; } CO int N=51,O=50; int fac[N],ifac[N]; int F[N][2*N]; int main(){ int n=read<int>(); fac[0]=1; for(int i=1;i<=n;++i) fac[i]=mul(fac[i-1],i); ifac[n]=fpow(fac[n],mod-2); for(int i=n-1;i>=0;--i) ifac[i]=mul(ifac[i+1],i+1); F[0][O+1]=1; for(int i=0;i<=n-1;++i){ if(i==0){ int x=1,y=0; for(int d=-y;d<=x;++d) F[i+x+y][O+d]=add(F[i+x+y][O+d],mul(F[i][O+x-y],mul(ifac[x-d],ifac[y+d]))); continue; } for(int x=0;x<=n-i;++x)for(int y=0;y<=n-i-x;++y)if(x+y>0) for(int d=-y;d<=x;++d) F[i+x+y][O+d]=add(F[i+x+y][O+d],mul(F[i][O+x-y],mul(ifac[x-d],ifac[y+d]))); } int ans=mul(fac[n],F[n][O]); printf("%d\n",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 5; const int dx[] = {-1, 0, 1, 0}; const int dy[] = {0, 1, 0, -1}; int gcd(int m, int n) { if (m < n) { int tmp = m; m = n; n = tmp; } if (n == 0) return m; else return gcd(n, m % n); } int n, g; long long a[100005]; int main() { cin >> n; cin >> a[0]; g = a[0]; for (int i = 1; i < n; i++) { cin >> a[i]; g = gcd(g, a[i]); } for (int i = 0; i < n; i++) { a[i] /= g; } for (int i = 0; i < n; i++) { while (a[i] % 2 == 0) a[i] /= 2; while (a[i] % 3 == 0) a[i] /= 3; if (a[i] != 1) { cout << "No"; return 0; } } cout << "Yes"; return 0; }
3
#include <bits/stdc++.h> using namespace std; template <typename T> inline T abs(T t) { return t < 0 ? -t : t; } const long long modn = 1000000007; inline long long mod(long long x) { return x % modn; } const int N = 312345; int seen[N], second[N], low[N], ok[N], p[N], tempo; vector<pair<int, int> > adj[N]; void dfs(int u, int p, bool di) { ::p[u] = p; seen[u] = true; second[u] = low[u] = tempo++; ok[u] = di; bool dad = false; for (pair<int, int> e : adj[u]) if (!seen[e.first]) { dfs(e.first, u, di || e.second); if (low[e.first] <= second[u] && ok[e.first]) ok[u] = 1; low[u] = min(low[u], low[e.first]); } else if (dad || e.first != p) { low[u] = min(low[u], second[e.first]); if (second[e.first] < second[u] && e.second) ok[u] = 1; } else dad = true; } int main() { int n, m, a, b, c, i; scanf("%d %d", &n, &m); for (i = 0; i < m; i++) { scanf("%d %d %d", &a, &b, &c); a--; b--; adj[a].push_back(pair<int, int>(b, c)); adj[b].push_back(pair<int, int>(a, c)); } scanf("%d %d", &a, &b); a--; b--; dfs(a, a, 0); bool any = ok[b]; while (p[b] != b) { b = p[b]; any |= ok[b]; } if (any) puts("YES"); else puts("NO"); }
5