func_code_string
stringlengths
59
71.4k
#include <bits/stdc++.h> using namespace std; int main() { int n, s, sum = 0; cin >> n >> s; int arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr, arr + n); for (int i = 0; i < n - 1; i++) { sum += arr[i]; } if (sum > s) cout << NO << endl; else cout << YES << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; cout << 9 * n << << 8 * n << endl; }
#include <bits/stdc++.h> using namespace std; int main() { long long x, d, count = 0; int n; char a; cin >> n >> x; while (n--) { cin >> a >> d; if (a == + ) { x += d; } else { if (d <= x) x -= d; else count++; } } cout << x << << count; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1010; int ar[MAXN][MAXN], star[MAXN][MAXN]; int main() { int n, m, t; char a; cin >> n >> m >> t; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) { scanf( %c , &a); ar[i][j] = a - 0 ; } for (int i = 2; i < n; i++) for (int j = 2; j < m; j++) if (ar[i][j] && ar[i - 1][j] && ar[i][j - 1] && ar[i + 1][j] && ar[i][j + 1]) star[i][j] = 1; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) star[i][j] += star[i][j - 1] + star[i - 1][j] - star[i - 1][j - 1]; long long tmp, fl, res = 0; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { tmp = 1; for (int k = j - 2; k >= 1; k--) { fl = 1; while (fl && tmp < i - 1) { fl = 0; if (star[i - 1][j - 1] - star[tmp][j - 1] - star[i - 1][k] + star[tmp][k] >= t) tmp++, fl = 1; } res += tmp - 1; } } } cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; long long n, mod; long long fast(long long base, long long pow) { long long res = 1; while (pow) { if (pow % 2) res = ((res % mod) * (base % mod)) % mod; pow /= 2; base = ((base % mod) * (base % mod)) % mod; if (!base) base = mod; } return res; } int main() { cin >> n >> mod; long long res = ((long long)(2 * ((1 - fast(3, n)) / -2.0)) % mod); if (res == -1) cout << mod - 1 << endl; else cout << res << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int _c, n, m; int e[70]; long long p[70][70], V, A, B, C, ans, G, F; void dfs2(const long long b, int x) { if (F / b < b) return; if (x > n) { long long c = F / b; if (G * b + b * c + c * G < ans) ans = G * b + b * c + c * G, A = G, B = b, C = c; return; } for (int E = e[x]; E >= 0; --E) dfs2(b * p[x][E], x + 1); } void dfs(const long long a, int x) { if (V / a / a < a) return; if (x > n) { double T = sqrt(V / a); G = a, F = V / a; if (T * a * 2 + V / a < ans) dfs2(1, 1); return; } int E = e[x]; e[x] = 0; for (; E >= 0; --E, ++e[x]) dfs(a * p[x][E], x + 1); --e[x]; } void work() { scanf( %d , &n); V = 1; for (int i = 1; i <= n; ++i) { scanf( %I64d%d , &p[i][1], &e[i]); p[i][0] = 1; for (int j = 2; j <= e[i]; ++j) p[i][j] = p[i][1] * p[i][j - 1]; V *= p[i][e[i]]; } ans = V * 2 + 1; A = V, B = C = 1; dfs(1, 1); printf( %I64d %I64d %I64d %I64d n , ans << 1, A, B, C); } int main() { for (scanf( %d , &_c); _c--;) work(); return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1000000000 + 7; int main() { string s; long long n; cin >> n; long long a[n]; for (int i = 0; i < n; ++i) cin >> a[i]; long long x = 0, y = n - 1, z = 0; while (x <= y) { if (max(a[x], a[y]) < z) break; else { if (a[x] < a[y] && a[x] > z) { z = a[x]; s += L ; x++; } else if (a[y] > z) { z = a[y]; s += R ; y--; } else { z = a[x]; s += L ; x++; } } } cout << s.size() << endl; cout << s; return 0; }
#include <bits/stdc++.h> using namespace std; int n, s, e; long long dp[5005][5005]; vector<int> x, a, b, c, d; int main() { cin >> n >> s >> e; x.resize(n); a.resize(n); b.resize(n); c.resize(n); d.resize(n); for (int i = 0; i < n; i++) { cin >> x[i]; } for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; } for (int i = 0; i < n; i++) { cin >> c[i]; } for (int i = 0; i < n; i++) { cin >> d[i]; } memset(dp, 0x3f, sizeof(dp)); dp[0][0] = 0; bool iss = 0, ise = 0; for (int i = 0; i < n; i++) { if (i == s - 1) { iss = 1; } if (i == e - 1) { ise = 1; } if (i == n - 1) { long long p = dp[i][0]; if (i == s - 1) { dp[i + 1][0] = min(dp[i + 1][0], p + x[i] + c[i]); } else if (i == e - 1) { dp[i + 1][0] = min(dp[i + 1][0], p + x[i] + a[i]); } else { dp[i + 1][0] = min(dp[i + 1][0], p + 2 * x[i] + a[i] + c[i]); } continue; } for (int j = 0; j <= n - 2; j++) { long long p = dp[i][j]; if (i == s - 1) { dp[i + 1][j] = min(dp[i + 1][j], p - x[i] + d[i]); if (j) dp[i + 1][j - 1] = min(dp[i + 1][j - 1], p + x[i] + c[i]); } else if (i == e - 1) { dp[i + 1][j] = min(dp[i + 1][j], p - x[i] + b[i]); if (j) dp[i + 1][j - 1] = min(dp[i + 1][j - 1], p + x[i] + a[i]); } else { dp[i + 1][j + 1] = min(dp[i + 1][j + 1], p - 2 * x[i] + b[i] + d[i]); if (j) { dp[i + 1][j] = min(dp[i + 1][j], p - x[i] + b[i] + x[i] + c[i]); dp[i + 1][j] = min(dp[i + 1][j], p + x[i] + a[i] - x[i] + d[i]); } if (iss) { dp[i + 1][j] = min(dp[i + 1][j], p + x[i] + a[i] - x[i] + d[i]); } if (ise) { dp[i + 1][j] = min(dp[i + 1][j], p - x[i] + b[i] + x[i] + c[i]); } if (j + iss + ise > 1) { dp[i + 1][j - 1] = min(dp[i + 1][j - 1], p + x[i] + a[i] + x[i] + c[i]); } } } } cout << dp[n][0] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; const ll OO = 1e18; int di[8] = {0, 0, 1, -1, -1, 1, -1, 1}; int dj[8] = {1, -1, 0, 0, 1, 1, -1, -1}; string ys = YES , no = NO ; const long double dgr = acos(-1) / 180, dg = 180 / acos(-1); const int mod = 1e9 + 9, N = 1e5 + 5, M = 2001; ll fast_power(ll base, ll power) { ll ans = 1; while (power > 0) { if (power & 1) ans = (ans * base) % mod; base = (base * base) % mod; power >>= 1; } return ans; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; ll n, m; cin >> n >> m; ll pw_m = fast_power(2, m); ll ans = 1; for (int i = 1; i <= n; i++) { ans = ans * (pw_m - i) % mod; } cout << ans % mod << n ; }
#include <bits/stdc++.h> using namespace std; int many; int s[100010]; int a[100010]; int p[100010]; int run(int o) { int zuo = 1; int you = o - 1; int re = -1; int k; while (zuo <= you) { k = (zuo + you) / 2; if (a[k] < s[o]) { re = k; you = k - 1; } else zuo = k + 1; } return re; } int main() { cin >> many; for (int i = 1; i <= many; i++) scanf( %d , &s[many + 1 - i]); a[1] = s[1]; p[1] = -1; for (int i = 2; i <= many; i++) a[i] = min(a[i - 1], s[i]); for (int i = 2; i <= many; i++) { if (a[i - 1] >= s[i]) p[i] = -1; else p[i] = i - 1 - run(i); } for (int i = 1; i <= many; i++) printf( %d , p[many + 1 - i]); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using pl = pair<ll, ll>; using vll = vector<ll>; using vpl = vector<pl>; using mat = vector<vll>; const ll MAX = 1e5; const ll mod = 1e9 + 7; template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto &ele : v) { cout << ele << ; } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { return os << ( << p.first << , << p.second << ) ; } template <typename T> istream &operator>>(istream &in, vector<T> &v) { for (auto &ele : v) { cin >> ele; } return in; } template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string &s) { return + s + ; } string to_string(const char *s) { return to_string((string)s); } string to_string(bool b) { return (b ? true : false ); } string to_string(vector<bool> v) { bool first = true; string res = { ; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += , ; } first = false; res += to_string(v[i]); } res += } ; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ; for (size_t i = 0; i < N; i++) { res += static_cast<char>( 0 + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = { ; for (const auto &x : v) { if (!first) { res += , ; } first = false; res += to_string(x); } res += } ; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return ( + to_string(p.first) + , + to_string(p.second) + ) ; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return ( + to_string(get<0>(p)) + , + to_string(get<1>(p)) + , + to_string(get<2>(p)) + ) ; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return ( + to_string(get<0>(p)) + , + to_string(get<1>(p)) + , + to_string(get<2>(p)) + , + to_string(get<3>(p)) + ) ; } void debug_out() { cerr << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << << to_string(H); debug_out(T...); } template <typename T> class graph { public: struct edge { int from; int to; T cost; }; vector<edge> edges; vector<vector<int>> g; int n; graph(int _n) : n(_n) { g.resize(n); } virtual int add(int from, int to, T cost) = 0; }; template <typename T> class forest : public graph<T> { public: using graph<T>::edges; using graph<T>::g; using graph<T>::n; forest(int _n) : graph<T>(_n) {} int add(int from, int to, T cost = 1) { assert(0 <= from && from < n && 0 <= to && to < n); int id = (int)edges.size(); assert(id < n - 1); g[from].push_back(id); g[to].push_back(id); edges.push_back({from, to, cost}); return id; } }; template <typename T> class dfs_forest : public forest<T> { public: using forest<T>::edges; using forest<T>::g; using forest<T>::n; vector<int> pv; vector<int> pe; vector<int> order; vector<int> pos; vector<int> end; vector<int> sz; vector<int> root; vector<int> depth; vector<T> dist; dfs_forest(int _n) : forest<T>(_n) {} void init() { pv = vector<int>(n, -1); pe = vector<int>(n, -1); order.clear(); pos = vector<int>(n, -1); end = vector<int>(n, -1); sz = vector<int>(n, 0); root = vector<int>(n, -1); depth = vector<int>(n, -1); dist = vector<T>(n); } void clear() { pv.clear(); pe.clear(); order.clear(); pos.clear(); end.clear(); sz.clear(); root.clear(); depth.clear(); dist.clear(); } private: void do_dfs(int v) { pos[v] = (int)order.size(); order.push_back(v); sz[v] = 1; for (int id : g[v]) { if (id == pe[v]) { continue; } auto &e = edges[id]; int to = e.from ^ e.to ^ v; depth[to] = depth[v] + 1; dist[to] = dist[v] + e.cost; pv[to] = v; pe[to] = id; root[to] = (root[v] != -1 ? root[v] : to); do_dfs(to); sz[v] += sz[to]; } end[v] = (int)order.size() - 1; } void do_dfs_from(int v) { depth[v] = 0; dist[v] = T{}; root[v] = v; pv[v] = pe[v] = -1; do_dfs(v); } public: void dfs(int v, bool clear_order = true) { if (pv.empty()) { init(); } else { if (clear_order) { order.clear(); } } do_dfs_from(v); } void dfs_all() { init(); for (int v = 0; v < n; v++) { if (depth[v] == -1) { do_dfs_from(v); } } assert((int)order.size() == n); } }; void solve() { ll N; cin >> N; dfs_forest<ll> g(N); for (ll i = (0); (1) > 0 ? i < (N - 1) : i >= (N - 1); i += (1)) { ll x, y; cin >> x >> y; x -= 1, y -= 1; g.add(x, y); } g.dfs(0); vll cnt(2, 0); cerr << [ << g.depth << ]: , debug_out(g.depth); for (ll i = (0); (1) > 0 ? i < (N) : i >= (N); i += (1)) { cnt[g.depth[i] % 2] += 1; } cout << min(cnt[0], cnt[1]) - 1 << endl; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll tc = 1; while (tc--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; template <class T> void printVector(T begin, T end) { for (auto i = begin; i != end; i++) { cout << *i << ; } cout << endl; } long long readLL() { long long x; cin >> x; return x; } int a[200]; int b[200]; int main() { int n, k; scanf( %d %d , &n, &k); for (int i = 0; i < n; i++) { scanf( %d , &a[i]); } int res = 0; for (int i = 0; i < k; i++) { for (int j = 0; j < n; j++) { b[j] = a[j]; } for (int j = i; j < n; j += k) { b[j] = 0; } int q = 0, w = 0; for (int j = 0; j < n; j++) { if (b[j] < 0) q++; if (b[j] > 0) w++; } res = max(res, abs(q - w)); } printf( %d n , res); return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2710; const double eps = 1e-9; const double pi = 3.1415926536; int dp[maxn][maxn]; string s; int main(int argc, char const *argv[]) { dp[0][0] = 1; for (int i = 1; i <= 100; i++) { for (int j = 0; j <= (i - 1) * 26; j++) { for (int k = 0; k < 26; k++) { dp[i][j + k] = (dp[i][j + k] + dp[i - 1][j]) % 1000000007; } } } int t; std::cin >> t; while (t--) { std::cin >> s; int len; len = s.size(); int sum = 0; for (int i = 0; i < len; i++) { sum += s[i] - a ; } std::cout << dp[len][sum] - 1 << n ; } return 0; }
#include <bits/stdc++.h> namespace mem { namespace stdval { using i32 = int; using i64 = long long; using u32 = unsigned int; using u64 = unsigned long long; using f32 = float; using f64 = double; using i128 = __int128_t; using u128 = __uint128_t; } // namespace stdval namespace utils { using std::cerr; using std::cin; using std::cout; using std::endl; using std::function; using std::lower_bound; using std::make_pair; using std::make_tuple; using std::max_element; using std::min_element; using std::reverse; using std::shuffle; using std::sort; using std::swap; using std::tie; using std::upper_bound; } // namespace utils namespace io { namespace fastio { const int BUFFER = 1 << 21; char ibuf[BUFFER], *iS, *iT; inline int getc() { if (iS == iT) { iT = (iS = ibuf) + fread(ibuf, 1, BUFFER, stdin); return iS == iT ? EOF : *iS++; } else { return *iS++; } } char obuf[BUFFER], *oS = obuf, *oT = oS + BUFFER - 1; inline void flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; } inline void putc(int x) { *oS++ = x; if (oS == oT) flush(); } struct Flusher { ~Flusher() { flush(); } } flusher; } // namespace fastio using fastio::getc; using fastio::putc; template <class T> inline void readDigit(T &x) { x = getc(); while (!isdigit(x)) x = getc(); } inline int readDigit() { int x; readDigit(x); return x; } template <class T> inline void readAlpha(T &x) { x = getc(); while (!isalpha(x)) x = getc(); } inline int readAlpha() { int x; readAlpha(x); return x; } template <class T> inline void readInt(T &x) { x = 0; bool f = 0; char c = getc(); while (!isdigit(c)) f ^= c == - , c = getc(); while (isdigit(c)) x = x * 10 + c - 0 , c = getc(); if (f) x = -x; } inline void read(int &x) { x = 0; bool f = 0; char c = getc(); while (!isdigit(c)) f ^= c == - , c = getc(); while (isdigit(c)) x = x * 10 + c - 0 , c = getc(); if (f) x = -x; } inline void read(unsigned int &x) { x = 0; bool f = 0; char c = getc(); while (!isdigit(c)) f ^= c == - , c = getc(); while (isdigit(c)) x = x * 10 + c - 0 , c = getc(); if (f) x = -x; } inline void read(long long &x) { x = 0; bool f = 0; char c = getc(); while (!isdigit(c)) f ^= c == - , c = getc(); while (isdigit(c)) x = x * 10 + c - 0 , c = getc(); if (f) x = -x; } inline void read(unsigned long long &x) { x = 0; bool f = 0; char c = getc(); while (!isdigit(c)) f ^= c == - , c = getc(); while (isdigit(c)) x = x * 10 + c - 0 , c = getc(); if (f) x = -x; } inline void read(char &x) { x = getc(); } template <class T = int> inline T read() { T x; read(x); return x; } template <class T, class... Args> inline void read(T &x, Args &...args) { read(x), read(args...); } inline void print(int x) { if (x < 0) putc( - ), x = -x; if (x > 9) print(x / 10); putc( 0 + x % 10); } inline void print(unsigned int x) { if (x < 0) putc( - ), x = -x; if (x > 9) print(x / 10); putc( 0 + x % 10); } inline void print(long long x) { if (x < 0) putc( - ), x = -x; if (x > 9) print(x / 10); putc( 0 + x % 10); } inline void print(unsigned long long x) { if (x < 0) putc( - ), x = -x; if (x > 9) print(x / 10); putc( 0 + x % 10); } inline void print(char x) { putc(x); } inline void print(const char *s) { int len = strlen(s); for (int i = 0; i < len; i++) putc(s[i]); } inline void print(const std::string &s) { for (int i = 0; i < s.length(); i++) putc(s[i]); } template <class T, class... Args> inline void print(const T &x, Args... args) { print(x), print(args...); } template <class... Args> inline void println(Args... args) { print(args...), putchar( n ); } } // namespace io namespace random { const int LuckyNumber = 20040725; std::mt19937 rng(LuckyNumber ^ std::chrono::steady_clock::now().time_since_epoch().count()); std::mt19937_64 rng64( LuckyNumber ^ std::chrono::steady_clock::now().time_since_epoch().count()); template <class T> inline T rand(T l, T r) { return std::uniform_int_distribution<T>(l, r)(rng); } template <class T> inline T rand64(T l, T r) { return std::uniform_int_distribution<T>(l, r)(rng); } } // namespace random namespace modint { template <const int mod> struct Z { int x; inline Z() { x = 0; } inline Z(int t) { x = t; } inline void operator-=(Z a) { (x -= a.x) < 0 && (x += mod); } inline void operator+=(Z a) { (x += a.x) >= mod && (x -= mod); } inline void operator*=(Z a) { x = (long long)x * a.x % mod; } friend inline Z operator*(Z a, Z b) { return (long long)a.x * b.x % mod; } friend inline Z operator-(Z a, Z b) { return ((a.x -= b.x) < 0 && (a.x += mod)), a; } friend inline Z operator+(Z a, Z b) { return ((a.x += b.x) >= mod && (a.x -= mod)), a; } }; template <const int mod> inline Z<mod> finv(Z<mod> x) { if (x.x < 2) return x; return (mod - mod / x.x) * finv(mod % x.x); } template <const int mod> inline Z<mod> fpow(Z<mod> a, int b) { Z<mod> s = 1; for (; b; b >>= 1, a = a * a) if (b & 1) s = s * a; return s; } template <const int mod> inline void init_inverse(int n, Z<mod> *inv) { inv[0] = inv[1] = 1; for (int i = 2; i < n; i++) inv[i] = (mod - mod / i) * inv[mod % i]; } template <const int mod> inline void init_factorial(int n, Z<mod> *fac, Z<mod> *ifac) { fac[0] = 1, init_inverse(n, ifac); for (int i = 1; i < n; i++) fac[i] = fac[i - 1] * i, ifac[i] = ifac[i - 1] * ifac[i]; } } // namespace modint namespace math { using std::max; using std::min; template <class T> inline T abs(T x) { return x < 0 ? -x : x; } template <class T> inline T gcd(T n, T m) { return m ? gcd(m, n % m) : n; } template <class T> inline T lcm(T n, T m) { return n / gcd(n, m) * m; } template <const stdval::u64 p> struct FastDiv { stdval::u64 t, i; inline FastDiv() : t(stdval::u64(-1) / p), i(mul_inv(p)) {} inline bool divide(stdval::u64 n) { return n * i <= t; } inline bool divide(stdval::i64 n) { return stdval::u64(n < 0 ? -n : n) * i <= t; } inline stdval::u64 mul_inv(stdval::u64 n) { stdval::u64 x = n; for (int i = 0; i < 5; ++i) x *= 2 - n * x; return x; } }; template <const stdval::u64 b> struct FastMod { stdval::u64 m; inline FastMod() : m(stdval::u64((stdval::u128(1) << 64) / b)) {} inline stdval::u64 reduce(stdval::u64 a) { stdval::u64 q = (stdval::u64)((stdval::u128(m) * a) >> 64); stdval::u64 r = a - q * b; return r >= b ? r - b : r; } }; } // namespace math namespace container { using std::map; using std::pair; using std::set; using std::tuple; using std::unordered_map; using std::unordered_set; template <class T> struct vector : std::vector<T> { using std::vector<T>::vector; vector() : std::vector<T>() {} vector(const std::vector<T> &plain) : std::vector<T>(plain) {} inline void read(int s = -1) { if (~s) this->resize(s); for (size_t i = 0; i < this->size(); i++) { io::read(this->operator[](i)); } } inline void print(char pt0 = , char pt1 = n ) { for (size_t i = 0; i < this->size(); i++) { io::print(this->operator[](i)); if (i + 1 < this->size()) io::putc(pt0); } io::putc(pt1); } inline void sort() { std::sort(this->begin(), this->end()); } inline void concat(const vector &rhs) { this->insert(this->end(), rhs.begin(), rhs.end()); } inline bool includes(const T &x) const { return std::find(this->begin(), this->end(), x) != this->end(); } inline vector slice(int l, int r) const { if (l > r) return {}; if (r < this->size()) return vector(this->begin() + l, this->begin() + r); vector<int> rsp = (this->begin() + l, this->end()); return rsp.resize(r - l), rsp; } inline void from(const std::set<T> &src) { this->resize(src.size()); auto it = this->begin(); for (const T e : src) *it++ = e; } template <class Function> inline void forEach(Function func) { for (const auto &it : *this) func(it); } template <class R, class Function> inline vector<R> _map(Function func) const { vector<R> res(this->size()); for (size_t i = 0; i < this->size(); i++) res[i] = func(this->operator[](i)); return res; } template <class R> inline vector<R> map(R func(T)) const { return this->_map<R>(func); } template <class R> inline vector<R> map(const std::function<R(T)> &func) const { return this->_map<R>(func); } }; } // namespace container namespace str { using namespace mem::container; struct string : std::string { using std::string::string; string() : std::string( ) {} string(const std::string &plain) : std::string(plain) {} inline string join(const vector<string> &) const; inline string join(const std::vector<string> &) const; template <class T> inline string join(const vector<T> &) const; template <class T> inline string join(const std::vector<T> &) const; vector<string> split(const string &delim) const { if (this->empty()) return {}; char *src = new char[this->length() + 1]; strcpy(src, this->c_str()); char *tar = new char[delim.length() + 1]; strcpy(tar, delim.c_str()); vector<string> rsp; for (char *pos = strtok(src, tar); pos; pos = strtok(nullptr, tar)) rsp.push_back(string(pos)); delete[] src; delete[] tar; return rsp; } template <class... Args> static inline string format(const char *fm, Args... args) { int len = snprintf(nullptr, 0, fm, args...); char *buf = new char[len + 1]; snprintf(buf, len + 1, fm, args...); string str(buf); delete[] buf; return str; } template <class... Args> static inline string format(const string &fm, Args... args) { return format(fm.c_str(), args...); } }; inline string to_string(const float &x) { return std::to_string(x); } inline string to_string(const double &x) { return std::to_string(x); } inline string to_string(const int &x) { return std::to_string(x); } inline string to_string(const unsigned int &x) { return std::to_string(x); } inline string to_string(const long long &x) { return std::to_string(x); } inline string to_string(const unsigned long long &x) { return std::to_string(x); } template <class T> inline string to_string(const vector<T> &vet) { return { + string( , ).join(vet) + } ; } template <class T> inline string to_string(const std::vector<T> &vet) { return to_string(vector<T>(vet.begin(), vet.end())); } template <const int mod> inline string to_string(const mem::modint::Z<mod> &zval) { return std::to_string(zval.x); } inline string string::join(const vector<string> &vet) const { if (!vet.size()) return ; string res = string(vet[0]); for (size_t i = 1; i < vet.size(); i++) { res += *this; res += string(vet[i]); } return res; } inline string string::join(const std::vector<string> &vet) const { if (!vet.size()) return ; string res = string(vet[0]); for (size_t i = 1; i < vet.size(); i++) { res += *this; res += string(vet[i]); } return res; } template <class T> inline string string::join(const vector<T> &vet) const { if (!vet.size()) return ; string res = to_string(vet[0]); for (size_t i = 1; i < vet.size(); i++) { res += *this; res += to_string(vet[i]); } return res; } template <class T> inline string string::join(const std::vector<T> &vet) const { if (!vet.size()) return ; string res = to_string(vet[0]); for (size_t i = 1; i < vet.size(); i++) { res += *this; res += to_string(vet[i]); } return res; } inline void print(const str::string &s) { for (size_t i = 0; i < s.length(); i++) mem::io::putc(s[i]); } template <class... Args> inline void printfm(const char *fm, Args... args) { print(str::string().format(fm, args...)); } template <class... Args> inline void printfm(const string &fm, Args... args) { print(str::string().format(fm, args...)); } template <class... Args> inline void printfm(const std::string &fm, Args... args) { print(str::string().format(fm, args...)); } } // namespace str using namespace io; using namespace str; using namespace math; using namespace utils; using namespace modint; using namespace random; using namespace stdval; using namespace container; } // namespace mem const int N = 5e5 + 10; using namespace mem::io; using namespace mem::container; int n, m, q, p[N], anc[N]; bool mrk[N], vis[N]; vector<int> ele[N]; pair<vector<int>, int> dat[N]; struct cmp { public: inline bool operator()(const int x, const int y) { return p[x] < p[y]; } }; std::priority_queue<int, std::vector<int>, cmp> f[N], g[N]; struct Edge { int u, v; } e[N]; struct Query { int o, x; } y[N]; int find(int x) { if (x == anc[x]) return x; return find(anc[x]); } int pop(int k) { while (f[k].size() && g[k].size()) { if (f[k].top() == g[k].top()) { f[k].pop(), g[k].pop(); } else { int x = f[k].top(); f[k].pop(); return x; } } if (f[k].size()) { int x = f[k].top(); f[k].pop(); return x; } else { return 0; } } pair<vector<int>, int> merge(int u, int v) { u = find(u), v = find(v); if (u == v) return {{}, -1}; if (ele[u].size() > ele[v].size()) std::swap(u, v); ele[v].insert(ele[v].end(), ele[u].begin(), ele[u].end()); anc[u] = v; return {ele[u], u}; } int main() { read(n, m, q); for (int i = 1; i <= n; i++) read(p[i]); for (int i = 1; i <= m; i++) read(e[i].u), read(e[i].v); for (int i = 1; i <= q; i++) { read(y[i].o), read(y[i].x); if (y[i].o == 2) mrk[y[i].x] = true; } for (int i = 1; i <= n; i++) { anc[i] = i; ele[i].push_back(i); } for (int i = 1; i <= m; i++) if (!mrk[i]) { merge(e[i].u, e[i].v); } for (int i = q; i >= 1; i--) { if (y[i].o == 2) { dat[i] = merge(e[y[i].x].u, e[y[i].x].v); } } for (int i = 1; i <= n; i++) { f[find(i)].push(i); } for (int i = 1; i <= q; i++) { if (y[i].o == 1) { int ans = pop(find(y[i].x)); vis[ans] = true; print(p[ans], n ); } else { int src = find(e[y[i].x].u); for (int x : dat[i].first) { anc[x] = dat[i].second; if (!vis[x]) { f[dat[i].second].push(x); g[src].push(x); } } } } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, v, cost, i; cin >> n >> v; if (v >= (n - 1)) cout << n - 1; else { cost = v; for (i = 2; i <= n; i++) { v--; if ((n - i) <= v) { cout << cost; break; } else { cost += i; v++; } } } cout << n ; return 0; }
#include <bits/stdc++.h> using namespace std; #pragma comment(linker, /stack:200000000 ) int t, n, m, j, ans, k, a, b, c, d, e, f, sum, i, sz, row, col, l; string s, s2, s3, s4; deque<pair<int, int> > v; void brainfuck(); int main() { ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL); brainfuck(); return 0; } void brainfuck() { cin >> n >> m >> k; for (i = 1; i <= n; i++) { for (j = 1; i & 1 && j <= m; j++) v.push_back({i, j}); for (j = m; i % 2 == 0 && j >= 1; j--) v.push_back({i, j}); } for (j = 1; j <= k; j++) { if (j != k) a = 2; else a = v.size(); cout << a; while (a--) { cout << << v.front().first << << v.front().second; v.pop_front(); } cout << n ; } }
#include <bits/stdc++.h> using namespace std; int s[18257 + 1]; int main() { memset(s, 0, sizeof(s)); s[1] = 1; for (int i = 2; i <= 18257; i++) { s[i] = (2 * i - 3) * 6 + 6; } int a; scanf( %d , &a); if (a == 1) { cout << s[1] << endl; } else { int sum = 0; for (int i = 1; i <= a; i++) { sum += s[i]; } cout << sum << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAX_N = 210, MAX_K = 1010, MOD = 1e9 + 7; int N, K, arr[MAX_N], dp[MAX_N][MAX_N][MAX_K]; void add(int& a, long long b) { b %= MOD; a = (a + b) % MOD; } int solve() { sort(arr + 1, arr + N + 1); dp[0][0][0] = 1; for (int i = 1; i <= N; i++) { for (int j = 0; j <= i; j++) { for (int k = 0; k <= K; k++) { int v = dp[i - 1][j][k]; int nk = k + j * (arr[i] - arr[i - 1]); if (nk > K) continue; add(dp[i][j][nk], v); add(dp[i][j][nk], 1LL * j * v); add(dp[i][j + 1][nk], v); if (j) add(dp[i][j - 1][nk], 1LL * j * v); } } } int ret = 0; for (int k = 0; k <= K; k++) { add(ret, dp[N][0][k]); } return ret; } void read() { cin >> N >> K; for (int i = 1; i <= N; i++) cin >> arr[i]; } int main() { read(); cout << solve() << endl; return (0); }
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const int mod = 1000000007; const int N = 5e5 + 5; int n, m, q, ans[N]; struct node { int cnt, id; } c[N]; struct ask { int id; long long x; } Q[N]; int st[N << 2]; void update(int o, int l, int r, int pos) { if (l == r) { ++st[o]; return; } int mid = l + r >> 1; if (pos <= mid) update(o << 1, l, mid, pos); else update(o << 1 | 1, mid + 1, r, pos); st[o] = st[o << 1] + st[o << 1 | 1]; } int query(int o, int l, int r, int k) { if (l == r) return l; int mid = l + r >> 1; if (k <= st[o << 1]) return query(o << 1, l, mid, k); else return query(o << 1 | 1, mid + 1, r, k - st[o << 1]); } int main() { scanf( %d%d%d , &n, &m, &q); for (int i = 1; i <= n; i++) { int x; scanf( %d , &x); ++c[x].cnt; } for (int i = 1; i <= m; i++) c[i].id = i; for (int i = 1; i <= q; i++) { long long x; scanf( %lld , &x); Q[i].id = i, Q[i].x = x; } sort(c + 1, c + 1 + m, [](const node& a, const node& b) { return a.cnt == b.cnt ? a.id < b.id : a.cnt < b.cnt; }); sort(Q + 1, Q + 1 + q, [](const ask& a, const ask& b) { return a.x < b.x; }); long long now = n; int j = 1; for (int i = 1; i <= m; i++) { long long nxt = (i == m ? 2e18 : 1ll * (c[i + 1].cnt - c[i].cnt) * i + now); update(1, 1, m, c[i].id); while (j <= q && Q[j].x <= nxt) { long long x = Q[j].x; int tmp = (x - now) % i; if (!tmp) tmp = i; ans[Q[j].id] = query(1, 1, m, tmp); ++j; } now = nxt; } for (int i = 1; i <= q; i++) printf( %d n , ans[i]); return 0; }
#include <bits/stdc++.h> using namespace std; char str[1000005]; int num[1000005]; int main() { int i, j, len, ans; while (scanf( %s , str) != EOF) { len = strlen(str); for (i = 0, j = len - 1; i < j; i++, j--) { swap(str[i], str[j]); } str[len] = 0 ; memset(num, 0, sizeof(num)); ans = 0; for (i = 0; i <= len; i++) { if (str[i] == 1 ) { for (j = i + 1; j <= len; j++) { if (str[j] == 0 ) break; } if (j != i + 1) { num[j] = 1; num[i] = -1; str[j] = 1 ; } else { num[i] = 1; } i = j - 1; } } for (i = 0; i <= len; i++) { if (num[i]) ans++; } printf( %d n , ans); for (i = 0; i <= len; i++) { if (num[i]) { if (num[i] == 1) printf( + ); if (num[i] == -1) printf( - ); printf( 2^%d n , i); } } } return 0; }
#include <bits/stdc++.h> using namespace std; vector<int> z; vector<int> pr; vector<vector<int>> ans; int s, f; long long pow(long long n, long long s) { if (s == 0) return 1; else { if (s % 2 == 1) { return (n * (pow(n, s - 1))) % 1000000007; } if (s % 2 == 0) { long long k = (pow(n, s / 2)) % 1000000007; return (k * k) % 1000000007; } } } void prfix(string s) { for (int i = 1; i < s.size(); i++) { pr[i] = pr[i - 1]; while (pr[i] > 0 && s[i] != s[pr[i]]) { pr[i] = pr[pr[i] - 1]; } if (s[i] == s[pr[i]]) pr[i]++; } } void zet(string s) { int l = 0, r = 0; for (int i = 1; i < s.size(); i++) { if (i <= r) { z[i] = min(z[i - l], r - i + 1); } for (; z[i] + i < s.size() && s[z[i] + i] == s[z[i]]; z[i]++) ; if (i + z[i] - 1 >= r) { l = i; r = i + z[i] - 1; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long double R, x1, y1, x2, y2; cin >> R >> x1 >> y1 >> x2 >> y2; if (sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) >= R) { cout << x1 << << y1 << << R; return 0; } if (x1 == x2 && y1 == y2) { long double y3 = y1 + R / 2; cout << x1 << << y3 << << R / 2; return 0; } long double c = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); long double x4 = (x1 - x2) * (R + c) / 2 / c + x2; long double y4 = (y1 - y2) * (R + c) / 2 / c + y2; cout.precision(10); cout << fixed << x4 << << y4 << << (R + c) / 2; }
#include <bits/stdc++.h> using namespace std; int main() { int i = 0, n; bool ok(false); cin >> n; while (i * (i + 1) / 2 <= n && ok == false) { if (i * (i + 1) / 2 == n) { ok = true; } i++; } (ok == true) ? cout << YES : cout << NO ; return 0; }
#include <bits/stdc++.h> using namespace std; const int INF = (int)1e9 + 7; template <class X, class Y> void minimize(X &x, const Y &y) { if (x > y) x = y; } template <class X, class Y> void maximize(X &x, const Y &y) { if (x < y) x = y; } vector<int> adj[5050]; int nLeaf[5050]; int f[5050][5050][2]; int tmp[5050][2]; int n; void loadTree(void) { scanf( %d , &n); for (int love = 0, _n = (n - 1); love < _n; love = love + 1) { int u, v; scanf( %d%d , &u, &v); adj[u].push_back(v); adj[v].push_back(u); } if (n == 2) { printf( 1 n ); exit(0); } memset(f, 0x3f, sizeof f); } void dfs(int u, int p) { bool leaf = true; for (__typeof((adj[u]).begin()) it = (adj[u]).begin(); it != (adj[u]).end(); it++) if (*it != p) { int v = *it; dfs(v, u); leaf = false; } if (leaf) { nLeaf[u] = 1; for (int i = 0, _n = (2); i < _n; i = i + 1) f[u][i][i] = 0; return; } for (int i = 0, _n = (2); i < _n; i = i + 1) f[u][0][i] = 0; for (__typeof((adj[u]).begin()) it = (adj[u]).begin(); it != (adj[u]).end(); it++) if (*it != p) { int v = *it; for (int i = 0, _n = (nLeaf[u] + 1); i < _n; i = i + 1) for (int j = 0, _n = (2); j < _n; j = j + 1) { tmp[i][j] = f[u][i][j]; f[u][i][j] = INF; } for (int i = 0, _n = (nLeaf[u] + 1); i < _n; i = i + 1) for (int j = 0, _n = (2); j < _n; j = j + 1) for (int k = 0, _n = (nLeaf[v] + 1); k < _n; k = k + 1) for (int l = 0, _n = (2); l < _n; l = l + 1) minimize(f[u][i + k][j], tmp[i][j] + f[v][k][l] + (j != l)); nLeaf[u] += nLeaf[v]; } } void process(void) { for (int i = (1), _b = (n); i <= _b; i = i + 1) if (adj[i].size() > 1) { dfs(i, -1); printf( %d n , min(f[i][nLeaf[i] / 2][0], f[i][nLeaf[i] / 2][1])); return; } assert(false); } int main(void) { loadTree(); process(); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool chkmin(T &x, T y) { return y < x ? x = y, 1 : 0; } template <typename T> inline bool chkmax(T &x, T y) { return x < y ? x = y, 1 : 0; } const int INF = 0x3f3f3f3f; const int N = 1e2 + 10, Node = 2e3 + 10, Edge = Node * Node / 2; int Dis[N][N], p[Node], a[Node], f[Node]; int to[Edge], nex[Edge], beg[Node], flow[Edge], dis[Node], cur[Node]; int S, T, e = 1; inline int read() { int x = 0, flag = 1; char ch = getchar(); while (!isdigit(ch) && ch != - ) ch = getchar(); if (ch == - ) flag = -1, ch = getchar(); while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch - 0 ), ch = getchar(); return x * flag; } inline void add(int x, int y, int z) { to[++e] = y, nex[e] = beg[x], beg[x] = e, flow[e] = z; to[++e] = x, nex[e] = beg[y], beg[y] = e, flow[e] = 0; } inline bool bfs() { queue<int> q; memset(dis, 0, sizeof(dis)); dis[S] = 1; q.push(S); while (!q.empty()) { int x = q.front(); q.pop(); for (int i = beg[x]; i; i = nex[i]) if (flow[i] && !dis[to[i]]) dis[to[i]] = dis[x] + 1, q.push(to[i]); } return dis[T]; } inline int dfs(int x, int f) { if (x == T) return f; int res = f; for (int &i = cur[x]; i; i = nex[i]) if (flow[i] && dis[to[i]] == dis[x] + 1) { int v = dfs(to[i], min(f, flow[i])); flow[i] -= v, flow[i ^ 1] += v, f -= v; if (!f) return res; } return res - f; } int main() { int n = read(), m = read(); memset(dis, 63, sizeof(dis)); for (int i = (1), iend = (m); i <= iend; i++) { int u = read(), v = read(); Dis[u][v] = Dis[v][u] = 1; } for (int i = (1), iend = (n); i <= iend; i++) Dis[i][i] = 0; for (int k = (1), kend = (n); k <= kend; k++) for (int i = (1), iend = (n); i <= iend; i++) for (int j = (1), jend = (n); j <= jend; j++) chkmin(Dis[i][j], Dis[i][k] + Dis[k][j]); int A = read(), B = read(), k = read(), h = read(); S = A + B + 1, T = A + B + 2; for (int i = (1), iend = (A); i <= iend; i++) p[i] = read(), a[i] = read(), f[i] = read(), add(S, i, 1); for (int i = (1), iend = (B); i <= iend; i++) { add(i + A, T, 1); int x = read(), d = read(); for (int j = (1), jend = (A); j <= jend; j++) if (Dis[p[j]][x] <= f[j] && a[j] >= d) add(j, i + A, 1); } int ans = 0; while (bfs()) { memcpy(cur, beg, sizeof(cur)); ans += dfs(S, 1e9); } long long Ans = 1e18; for (int i = (0), iend = (A); i <= iend; i++) chkmin(Ans, 1ll * k * min(A - i, ans) + 1ll * i * h); printf( %lld n , Ans); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; namespace io { const int SIZE = (1 << 21) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1; inline char getc() { return (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); } inline void flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; } inline void putc(char x) { *oS++ = x; if (oS == oT) flush(); } template <class T> inline void read(T &x) { char ch; int f = 1; x = 0; while (isspace(ch = getc())) ; if (ch == - ) ch = getc(), f = -1; do x = x * 10 + ch - 0 ; while (isdigit(ch = getc())); x *= f; } template <class T, class... Args> inline void read(T &x, Args &...args) { read(x); read(args...); } template <class T> inline void write(T x) { static char stk[128]; int top = 0; if (x == 0) { putc( 0 ); return; } if (x < 0) putc( - ), x = -x; while (x) stk[top++] = x % 10, x /= 10; while (top) putc(stk[--top] + 0 ); } template <class T, class... Args> inline void write(T x, Args... args) { write(x); putc( ); write(args...); } inline void space() { putc( ); } inline void endl() { putc( n ); } struct _flush { ~_flush() { flush(); } } __flush; }; // namespace io using io::endl; using io::flush; using io::getc; using io::putc; using io::read; using io::space; using io::write; const int M = 1000000007; inline int add(int x, int y) { return x + y >= M ? x + y - M : x + y; } template <class... Args> inline int add(int x, int y, Args... args) { return add(add(x, y), args...); } inline int sub(int x, int y) { return x - y < 0 ? x - y + M : x - y; } inline int mul(int x, int y) { return 1LL * x * y % M; } template <class... Args> inline int mul(int x, int y, Args... args) { return mul(mul(x, y), args...); } inline void inc(int &x, int y = 1) { x += y; if (x >= M) x -= M; } inline void dec(int &x, int y = 1) { x -= y; if (x < 0) x += M; } inline int power(int x, int y) { int res = 1; for (; y; y >>= 1, x = mul(x, x)) if (y & 1) res = mul(res, x); return res; } inline int inv(int x) { return power(x, M - 2); } const int N = 4005; int _g[N * 2], *g = _g + N; int n, m; struct DP { int a[N * 2]; int *f; DP() { f = a + N; fill(f - m, f + m + 1, 0); f[0] = 1; } void update(int x) { fill(g - m, g + m + 1, 0); for (int i = -m + x; i <= m - x; ++i) g[i - x] = (g[i - x] + f[i]) % M; for (int i = -m + x; i <= m - x; ++i) g[i + x] = (g[i + x] + f[i]) % M; for (int i = -m; i <= m; ++i) f[i] = (f[i] + g[i]) % M; } void select(int x) { fill(g - m, g + m + 1, 0); for (int i = -m + x; i <= m; ++i) g[i - x] = (g[i - x] + f[i]) % M; for (int i = -m; i <= m - x; ++i) g[i + x] = (g[i + x] + f[i]) % M; for (int i = -m; i <= m; ++i) f[i] = g[i]; } void operator=(const DP &r) { for (int i = -m; i <= m; ++i) f[i] = r.f[i]; } int query(int x) { int res = 0; for (int i = 2 - x; i <= x - 2; ++i) inc(res, f[i]); return res; } }; vector<int> G[N]; int a[N]; bool vis[N]; DP f; DP stk[20]; int top = 0; int dfs(int x, int fa) { vis[x] = true; for (int v : G[x]) { if (v == fa) continue; if (v == 1) return 1; return dfs(v, x) + 1; } return 1; } int res = 0; void solve(int l, int r) { if (l == r) { inc(res, f.query(a[l])); return; } stk[top] = f; ++top; int mid = (l + r) >> 1; for (int i = l; i <= mid; ++i) f.update(a[i]); solve(mid + 1, r); f = stk[top - 1]; for (int i = mid + 1; i <= r; ++i) f.update(a[i]); solve(l, mid); --top; } void solve2(int l, int r) { if (l == r) { inc(res, add(f.f[a[l]], f.f[a[l] - 1], f.f[1 - a[l]], f.f[-a[l]])); inc(res, add(f.f[a[l]], f.f[a[l] - 1], f.f[1 - a[l]], f.f[-a[l]])); return; } stk[top] = f; ++top; int mid = (l + r) >> 1; for (int i = l; i <= mid; ++i) f.select(a[i]); solve2(mid + 1, r); f = stk[top - 1]; for (int i = mid + 1; i <= r; ++i) f.select(a[i]); solve2(l, mid); --top; } int main() { read(n, m); for (int i = 1; i <= m; ++i) { int u, v; read(u, v); G[u].push_back(v); G[v].push_back(u); } int cnt = 0; for (int v : G[1]) { if (!vis[v]) a[++cnt] = dfs(v, 1) + 1; } solve(1, cnt); res = mul(res, 2); if ((m & 1) == 0) { f = DP(); for (int i = 1; i <= cnt; ++i) f.select(a[i]); inc(res, f.f[0]); } else { f = DP(); solve2(1, cnt); } write(res), endl(); return 0; }
#include <bits/stdc++.h> using namespace std; char sign[1000][1000], cpy[1000][1000]; int n, m; void tryFill(int i, int j) { for (int ii = i - 1; ii <= i + 1; ii++) { for (int jj = j - 1; jj <= j + 1; jj++) { if (i == ii && j == jj) { continue; } if (sign[ii][jj] == . ) { return; } } } for (int ii = i - 1; ii <= i + 1; ii++) { for (int jj = j - 1; jj <= j + 1; jj++) { if (i == ii && j == jj) { continue; } cpy[ii][jj] = # ; } } } int main() { ios_base::sync_with_stdio(false); cin >> n >> m; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { cin >> sign[i][j]; cpy[i][j] = . ; } } for (int i = 1; i < n - 1; i++) { for (int j = 1; j < m - 1; j++) { tryFill(i, j); } } for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (sign[i][j] != cpy[i][j]) { cout << NO ; return 0; } } } cout << YES ; return 0; }
#include <bits/stdc++.h> using namespace std; template <class T, class T2> inline int chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; } template <class T, class T2> inline int chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; } const int MAXN = (1 << 20); int n; void read() { cin >> n; } vector<pair<int, int> > change_list = { {0, 0}, {0, 1}, {0, 2}, {1, 0}, {1, 3}, {0, 3}, {1, 2}, {2, 0}, {3, 1}, {2, 1}, {1, 1}, {2, 2}, {3, 0}, {3, 3}, {2, 3}, {3, 2}, }; void solve() { if (n % 4 >= 2) { cout << NO << n ; return; } cout << YES << n ; bool odd_flag = n % 2; for (int i = 0; i < (n / 4); i++) { int a = i * 4 + 1, b = i * 4 + 2, c = i * 4 + 3, d = i * 4 + 4; if (odd_flag) cout << a << << n << n ; cout << a << << c << n ; if (odd_flag) cout << c << << n << n ; if (odd_flag) cout << b << << n << n ; cout << b << << d << n ; if (odd_flag) cout << d << << n << n ; cout << a << << b << n ; cout << c << << d << n ; cout << a << << d << n ; cout << b << << c << n ; } for (int i = 0; i < (n / 4); i++) for (int j = i + 1; j < (n / 4); j++) for (auto it : change_list) cout << 4 * i + 1 + it.first << << 4 * j + 1 + it.second << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); read(); solve(); return 0; }
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> pair<T1, T2> operator+(pair<T1, T2> a, pair<T1, T2> b) { return make_pair(a.first + b.first, a.second + b.second); } template <typename T1, typename T2> pair<T1, T2> operator-(pair<T1, T2> a, pair<T1, T2> b) { return make_pair(a.first - b.first, a.second - b.second); } template <typename T1, typename T2> ostream& operator<<(ostream& out, pair<T1, T2> p) { out << p.first << << p.second; return out; } template <typename T1, typename T2> istream& operator>>(istream& in, pair<T1, T2>& p) { in >> p.first >> p.second; return in; } template <typename T> ostream& operator<<(ostream& out, vector<T> v) { for (auto a : v) out << a << ; return out; } template <typename T> istream& operator>>(istream& in, vector<T>& v) { for (auto& a : v) in >> a; return in; } template <typename T> ostream& operator<<(ostream& out, multiset<T> S) { for (auto a : S) out << a << ; return out; } template <typename T> ostream& operator<<(ostream& out, set<T> S) { for (auto a : S) out << a << ; return out; } template <typename T> set<T> operator+(set<T> A, set<T> B) { for (auto b : B) A.insert(b); return A; } int main() { string s, t; cin >> s >> t; int i = 0; for (int j = 0; j < t.size(); j++) { if (s[i] == t[j]) i++; } cout << i + 1 << endl; return 0; }
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, l, k; cin >> n >> l >> k; vector<int> cnt(26, 0); for (int i = 0; i < n * l; ++i) { char c; cin >> c; cnt[c - a ]++; } int last = 0; int cur = 0; vector<string> ans(n, string(l, # )); for (int i = 0; i < l; ++i) { while (!cnt[cur]) cur++; while (cnt[cur] < k - last && k - last > 0) { for (int j = 0; j < cnt[cur]; ++j) { ans[last + j][i] = char( a + cur); } last += cnt[cur]; cur++; } for (int j = 0; j < k - last; ++j) { ans[j + last][i] = char( a + cur); } cnt[cur] -= k - last; } for (int i = 0; i < n; ++i) { for (int j = 0; j < l; ++j) { if (ans[i][j] == # ) { cnt[cur]--; cout << char( a + cur); } else cout << ans[i][j]; while (!cnt[cur]) cur++; } cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); long long int n, i, m, j, f, k; cin >> n; m = n * n; vector<long long int> v[n]; for (i = m; i >= n; i -= n) { if (f == 0) { k = 0; for (j = i; j > (i - n); j--, k++) { v[k].push_back(j); } } else { k = n - 1; for (j = i; j > (i - n); j--, k--) { v[k].push_back(j); } } (f == 0) ? f = 1 : f = 0; } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) cout << v[i][j] << ; cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 15e4 + 4; const int MAXQ = MAXN; const int MAXK = 6 + 2; int n, k, q; int t[MAXN]; set<int, greater<int> > st; int main() { scanf( %d%d%d , &n, &k, &q); for (int i = 0; i < n; ++i) scanf( %d , t + i); while (q--) { int op, id; scanf( %d%d , &op, &id); --id; if (op == 1) { st.insert(t[id]); } else { if (!st.count(t[id])) { puts( NO ); continue; } int cnt = k; bool ok = false; for (auto it : st) { if (it == t[id]) { puts( YES ); ok = true; break; } --cnt; if (ok) break; if (cnt == 0) break; } if (!ok) puts( NO ); } } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( Ofast ) #pragma GCC target( avx,avx2,fma ) #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) using namespace std; using pl = pair<long long, long long>; using vll = vector<long long>; using mll = map<long long, long long>; using vpl = vector<pl>; long long mod = 1000000007; long long modu(long long a, long long b) { long long c = a % b; return (c < 0) ? c + b : c; } long long ipow(long long a, long long b) { long long ans = 1; while (b--) { ans = ans * a; } return ans; } long long getmx(long long i, long long j, long long i1, long long j1, long long i2, long long j2) { long long ans = 0; ans += abs(i - i1) + abs(j - j1); ans += abs(i1 - i2) + abs(j1 - j2); ans += abs(i - i2) + abs(j - j2); return ans; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } bool isleap(long long n) { if (n % 400 == 0) return true; if (n % 4 == 0 && n % 100 != 0) return true; return false; } void solve() { long long n, dif = 0; cin >> n; for (long long i = n + 1;; i++) { if (isleap(i)) { dif += 2; } else { dif++; } if (dif % 7 == 0 && (isleap(i) == isleap(n))) { cout << i << n ; return; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long t = 1; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int main() { long long int n, a, b, i, x, t, rem; cin >> n >> a >> b; for (i = 0; i < n; i++) { cin >> x; t = (x * a) % b; if (t / a >= 1) rem = t / a; else rem = 0; cout << rem << ; } return 0; }
#include <bits/stdc++.h> using namespace std; void scanint(long long int &x); map<long long int, long long int> vv; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); multiset<long long int> ms; long long int n; scanint(n); long long int a[n]; long long int curr = 1; long long int i = 0; for (i = 0; i < n; i++) { scanint(a[i]); if (!vv[a[i]] && a[i] >= 1 && a[i] <= n) vv[a[i]]++; else ms.insert(a[i]); } long long int ans = 0; multiset<long long int>::iterator itr; for (itr = ms.begin(); itr != ms.end(); itr++) { while (vv[curr]) curr++; ans += abs(*itr - curr); vv[curr]++; } cout << ans << endl; return 0; } void scanint(long long int &x) { register int c = getchar(); x = 0; int neg = 0; for (; ((c < 48 || c > 57) && c != - ); c = getchar()) ; if (c == - ) { neg = 1; c = getchar(); } for (; c > 47 && c < 58; c = getchar()) { x = (x << 1) + (x << 3) + c - 48; } if (neg) x = -x; }
#include <bits/stdc++.h> using namespace std; int main() { int n, a, mid, ans = 0; int all[100001]; all[0] = 0; cin >> n; for (int i = 1; i <= n; i++) { cin >> a; all[i] = all[i - 1] + a; } if (all[n] % 2 == 1) { cout << 0; return 0; } mid = all[n] / 2; for (int i = 1; i < n; i++) { if (all[i] == all[n] - all[i]) ans++; } cout << ans; }
#include <bits/stdc++.h> using namespace std; int main() { int t, i; cin >> t; for (int i = 0; i < t; i++) { long long int n; cin >> n; if (n < 4) cout << 4 - n << endl; else { cout << n % 2 << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; inline int add(int x, int y) { x += y; if (x >= mod) x -= mod; return x; } inline int mul(int x, int y) { return ((long long)x) * y % mod; } const int N = 1e6 + 5; int fac[N + 5], ifac[N + 5], p[N + 5]; int Pow(int a, int b) { int x = 1; for (; b > 0; b >>= 1) { if (b & 1) x = mul(x, a); a = mul(a, a); } return x; } int main() { ios_base::sync_with_stdio(0), cin.tie(0); int n, m; cin >> n >> m; fac[0] = ifac[0] = p[0] = 1; for (int i = 1; i < N; ++i) { fac[i] = mul(fac[i - 1], i); ifac[i] = Pow(fac[i], mod - 2); p[i] = mul(p[i - 1], m); } auto comb = [&](int x, int y) { return mul(mul(fac[x], ifac[x - y]), ifac[y]); }; auto f = [&](int x, int y) { if (x == y) return 1; return mul(y, Pow(x, x - y - 1)); }; int ans = 0; for (int k = 1; k < n; ++k) if (m >= k) { int g = mul(mul(comb(m - 1, m - k), comb(n - 2, k - 1)), mul(mul(fac[k - 1], p[n - k - 1]), f(n, k + 1))); ans = add(ans, g); } cout << ans << n ; return 0; }
#include <bits/stdc++.h> using namespace std; long long a[27]; int main() { long long n; string s; cin >> n; cin >> s; long long c = 0, i, d; for (i = 0; i < (2 * n) - 2; i++) { if (s[i] >= a && s[i] <= z ) { d = s[i] - a ; a[d]++; } else { d = s[i] - A ; if (a[d] > 0) { a[d]--; } else { c++; } } } cout << c << endl; }
// Problem: A. Dense Array // Contest: Codeforces - Codeforces Round #702 (Div. 3) // URL: https://codeforces.com/contest/1490/problem/A // Memory Limit: 256 MB // Time Limit: 2000 ms // Powered by CP Editor (https://github.com/cpeditor/cpeditor) #include <bits/stdc++.h> #pragma GCC optimize( Ofast,no-stack-protector,unroll-loops ) #pragma GCC target( sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native ) #define ll long long #define pb push_back #define eb emplace_back #define mp make_pair #define pii pair<ll,ll> #define vi vector<int> #define vii vector<pii> #define mi map<ll,ll> #define mii map<pii,ll> #define all(a) (a).begin(),(a).end() #define fi first #define si second #define sz(x) (ll)x.size() #define endl n #define checkbit(n, b) ((n >> b) & 1) #define mod 1000000007 #define inf 1000000000000000000 #define puts(n) cout<<n; #define um unordered_map<ll,ll> #define rep(i,a,b) for(int i=a;i<b;i++) #define setbits(x) __builtin_popcount(x) #define mset(m,v) memset(m,v,sizeof(m)) #define int ll #define deb(x) cout << #x << = << x << endl #define deb2(x, y) cout << #x << = << x << , << #y << = << y << endl using namespace std; int power(int base, int exp); //======================= // USED UNORDERED_MAP???????? // INT_MAX and LONG_MAX range??? //gcd(x,y)=gcd(x-y,y) //gcd(x,y,z...)=gcd(x,y-x,z-x,...) void solve() { int n; cin>>n; vi arr(n,0); rep(i,0,n) { cin>>arr[i]; } int cnt=0; rep(i,0,n-1) { int x=max(arr[i],arr[i+1]); int y=min(arr[i],arr[i+1]); while(y*2<x) { x=(x+1)/2; cnt++; } } cout<<cnt<<endl; } int32_t main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); srand(chrono::high_resolution_clock::now().time_since_epoch().count()); int t = 1; cin >> t; while(t--) { solve(); } return 0; } int power(int base, int exp) { base %= mod; int result = 1; while (exp > 0) { if (exp & 1) result = ((ll)result * base) % mod; base = ((ll)base * base) % mod; exp >>= 1; } return result; }
#include <bits/stdc++.h> using namespace std; string s; struct SUM { int n; bool pp; } b; vector<SUM> a; bool cmp(SUM u, SUM v) { return u.n < v.n; } int main() { std::ios::sync_with_stdio(false); int i, j, k, m; cin >> m >> s; s = s[0] == - ? s : + + s; for (i = 0; i < s.size();) { j = i + 1; if (s[i + 1] < 0 || s[i + 1] > 9 ) k = 1; else { for (k = 0; s[j] >= 0 && s[j] <= 9 ; ++j) k = k * 10 + s[j] - 0 ; ++j; } if (s[i] == - ) k = -k; b.n = k; b.pp = s[j] != a ; a.push_back(b); i = j + 3; } sort(a.begin(), a.end(), cmp); for (k = 0, i = 0; i < a.size(); ++i) { if (a[i].pp) k += a[i].n * ++m; else k += a[i].n * m++; } cout << k << endl; return 0; }
#include <iostream> #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--){ //int n; cin >> n; string s; cin >> s; string res = ; for (int i=0;i<s.size();i++){ if (i%2==0){ if (s[i]== a ){ res += b ; } else { res += a ; } } else { if (s[i]== z ){ res += y ; } else { res += z ; } } } cout << res << endl; } }
#include <bits/stdc++.h> using namespace std; const long long LIM = 1e5; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cout.precision(10); cout << fixed; long long n, r; cin >> n >> r; vector<long long> L(n); vector<long long> T(n); for (long long i = 0; i < n; i++) cin >> L[i]; for (long long i = 0; i < n; i++) cin >> T[i]; long long cnt = 0; vector<long long> ans; long long curTime = 0; long long timePotion = 0; for (long long i = 0; i < n; i++) { long long l = L[i]; long long t = T[i]; if (timePotion < l) { t -= timePotion; l -= timePotion; curTime += timePotion; if (t < 0) { cout << -1 << n ; return 0; } if (2 * l > t) { long long szz = ans.size(); long long timeNeedPotion = 2 * l - t; if (t - timeNeedPotion < 0) { cout << -1 << n ; return 0; } long long cntMoments = (timeNeedPotion / r) + (timeNeedPotion % r != 0); curTime = curTime + t - timeNeedPotion; cnt += cntMoments; for (long long i = 0; i < cntMoments; i++) { if (ans.size() < LIM) { ans.push_back(curTime + i * r); } else { break; } } curTime += timeNeedPotion; timePotion = r - (timeNeedPotion % r); if (timePotion == r) timePotion -= r; } else { curTime += l * 2; timePotion = 0; } } else { if (l > t) { cout << -1 << n ; return 0; } else { timePotion -= l; curTime += l; } } } cout << cnt << n ; if (cnt <= LIM) { for (long long xx : ans) cout << xx << ; cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; long long Length, X, Y; long long Dp[10000001]; int main(void) { register long long i; cin >> Length >> X >> Y; Dp[1] = X; for (i = 2; i <= Length; i++) { Dp[i] = Dp[i - 1] + X; if (i & 1) { Dp[i] = min(Dp[i], Dp[(i >> 1) + 1] + X + Y); } else { Dp[i] = min(Dp[i], Dp[i >> 1] + Y); } } cout << Dp[Length] << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int i1, i2, a1, a2; cout << ? ; for (int i = 1; i <= k; ++i) cout << i << ; cout << n ; cout.flush(); cin >> i1 >> a1; cout << ? ; for (int i = 1; i <= k + 1; ++i) { if (i == i1) ++i; cout << i << ; } cout << n ; cout.flush(); cin >> i2 >> a2; if (a1 < a2) { swap(i1, i2); swap(a1, a2); } int count = 0, c1 = 0, c2; for (int ch = 1; ch <= k + 10; ++ch) { if (ch == i1 || ch == i2) ++ch; if (ch == i1 || ch == i2) ++ch; ++c1; if (c1 > k - 1) break; cout << ? ; c2 = 0; for (int i = 1; i <= k + 10; ++i) { ++c2; if (c2 > k) break; if (i == i2) ++i; if (i == ch) cout << i2 << ; else cout << i << ; } cout << n ; cout.flush(); int ich, ach; cin >> ich >> ach; if (ich == i1) ++count; } cout << ! << count + 1; cout.flush(); return 0; }
#include <bits/stdc++.h> using namespace std; char s[110]; map<int, bool> mp[3]; bool Check(int x) { if (x < 10) { if (mp[0][x] || mp[1][x] || mp[2][x]) return true; } else if (x < 100) { int a = x / 10, b = x % 10; if (mp[0][a] && mp[1][b]) return true; if (mp[0][a] && mp[2][b]) return true; if ((mp[1][a] && mp[2][b]) || (mp[1][a] && mp[0][b])) return true; if ((mp[2][a] && mp[0][b]) || (mp[2][a] && mp[1][b])) return true; } return false; } int main() { int n; scanf( %d , &n); for (int i = 0; i < n; ++i) { for (int x, j = 0; j < 6; ++j) { scanf( %d , &x); mp[i][x] = true; } } int i; for (i = 1; i < 1000; ++i) { if (!Check(i)) break; } printf( %d n , i - 1); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; int a, b; int s[4010]; cin >> n; memset(s, 0, sizeof(s)); for (int i = 0; i < n; i++) { cin >> a >> b; s[a + b]++; s[a - b + 3000]++; } long long sum = 0; for (int i = 0; i <= 4000; i++) { if (s[i] > 1) sum += (s[i] * (s[i] - 1) / 2); } cout << sum << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long x, y, z; cin >> x >> y >> z; long long xx = x, yy = y; long long g1 = x % z, p1; if ((y + g1) / z > y / z) { y += g1; if (g1 - (y) % z > 0) g1 -= (y) % z; } else g1 = 0; p1 = (x / z) + (y / z); long long g2 = yy % z, p2; if ((xx + g2) / z > xx / z) { xx += g2; if (g2 - (xx) % z > 0) g2 -= (xx) % z; } else g2 = 0; p2 = (xx / z) + (yy / z); if (p1 > p2) cout << p1 << << g1; else if (p2 > p1) cout << p2 << << g2; else { cout << p1 << ; if (g1 < g2) cout << g1; else cout << g2; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long M = 1e6 + 5; const long long mod = 1e9 + 7; void solve() { long long n; cin >> n; long long a = n / 10; long long b = n % 10; long long ans = a + (b == 9); cout << ans; } signed main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long T = 1; cin >> T; while (T-- > 0) { solve(); cout << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; template <typename TP> inline bool rd(TP& r) { r = 0; char tmp = getchar(); while (tmp < 0 || tmp > 9 ) { if (tmp == EOF) return 0; tmp = getchar(); } while ( 0 <= tmp && tmp <= 9 ) { r = (r << 3) + (r << 1) + tmp - 0 ; tmp = getchar(); } return 1; } long long a[110], b[110], n; long long dt[110 * 110]; bool vis[1000100]; long long ab(long long num) { if (num >= 0) return num; return -num; } int main() { int t; rd(t); int tot; int nb; while (t--) { rd(n); for (int i = 1; i <= n; i++) rd(a[i]); tot = 0; for (int i = 1; i <= n; i++) for (int j = 1; j < i; j++) dt[++tot] = ab(a[i] - a[j]); sort(dt + 1, dt + tot + 1); tot = unique(dt + 1, dt + tot + 1) - (dt + 1); memset(vis, 0, sizeof(vis)); nb = 0; for (int i = 1; i <= 1000000 && nb != n; i++) if (!vis[i]) { b[++nb] = i; for (int j = 1; j <= tot && i + dt[j] < 1000100; j++) vis[i + dt[j]] = 1; } if (nb != n) printf( NO n ); else { printf( YES n ); for (int i = 1; i <= n; i++) printf( %I64d , b[i]); printf( n ); } } return 0; }
#include <bits/stdc++.h> #pragma warning(disable : 4996) #pragma comment(linker, /STACK:336777216 ) using namespace std; int IT_MAX = 1 << 17; const long long MOD = 1000000007; const int INF = 0x1f3f3f3f; const long long LL_INF = 1034567890123456789ll; const double PI = acos(-1); const double ERR = 1E-11; multiset<int> Sx; int in[100050]; int main() { int N, K, i, j; scanf( %d %d , &N, &K); for (i = 1; i <= N; i++) scanf( %d , &in[i]); int ans = -1; vector<int> Va; for (i = 1, j = 1; i <= N; i++) { while (j <= N) { if (Sx.empty()) { Sx.insert(in[j++]); continue; } auto it1 = Sx.begin(); auto it2 = Sx.end(); it2--; if (abs(in[j] - *it1) > K || abs(in[j] - *it2) > K) break; Sx.insert(in[j++]); } if (ans < j - i) { ans = j - i; Va.clear(); } if (ans == j - i) Va.push_back(i); Sx.erase(Sx.lower_bound(in[i])); } printf( %d %d n , ans, (int)Va.size()); for (auto it : Va) printf( %d %d n , it, it + ans - 1); return 0; }
#include <bits/stdc++.h> using namespace std; struct info { int l, r, need, onemore, take; }; vector<string> solve(vector<int> a, int n) { vector<string> ans(n + 1, string(n, 0 )); vector<info> state = {{0, n, (n + 1) / 2, ((n + 1) & 1), 0}}; int pos = 0; for (auto i : a) { for (auto &j : state) while (j.need && i) { --i; --j.need; ++j.take; } for (auto &j : state) if (j.onemore && i) { --i; --j.onemore; ++j.take; } int j = 0; while (i > 0) { if (state[j].take != state[j].r - state[j].l + 1) { --i; ++state[j].take; } j = (j + 1) % state.size(); } vector<info> nstate; for (auto &k : state) { for (int p = k.l; p < k.l + k.take; ++p) ans[p][pos] = 1 ; if (k.take) nstate.push_back({k.l, k.l + k.take - 1, 0, 0, 0}); if (k.take < k.r - k.l + 1) nstate.push_back({k.l + k.take, k.r, 0, 0, 0}); } state = nstate; for (auto &k : state) { k.need = (k.r - k.l + 1) / 2; k.onemore = (k.r - k.l + 1) & 1; k.take = 0; } ++pos; } sort(ans.begin(), ans.end()); reverse(ans.begin(), ans.end()); while (!ans.empty() && ans.back() == string(n, 0 )) ans.pop_back(); return ans; } void validate(vector<int> a, int n, vector<string> ans) { for (auto &j : ans) for (int i = 0; i < n; ++i) if (j[i] == 1 ) --a[i]; assert(count(a.begin(), a.end(), 0) == n); for (int i = 1; i < ans.size(); ++i) assert(ans[i - 1] != ans[i]); } int main() { ios_base::sync_with_stdio(0), cin.tie(0); int n; cin >> n; vector<int> a(n); for (auto &i : a) cin >> i; auto ans = solve(a, n); validate(a, n, ans); cout << ans.size() << n ; for (auto i : ans) cout << i << n ; return 0; }
#include <bits/stdc++.h> using namespace std; int cnt[ T + 1]; string p = AGTC ; int main() { int n; cin >> n; string s; cin >> s; for (int i = 0, __l = s.size(); i < __l; ++i) cnt[s[i]]++; int maxi = 0; for (int i = 0, __l = p.size(); i < __l; ++i) maxi = max(maxi, cnt[p[i]]); int x = 0; for (int i = 0, __l = p.size(); i < __l; ++i) x += cnt[p[i]] == maxi; long long ans = 1; for (int i = 0, __l = n; i < __l; ++i) (ans *= x) %= 1000000007; cout << ans << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const long double ESP = 1E-8; long double cal(long double x, long double y, long double z) { return log(x) * pow(y, z); } long double ca(long double x, long double y, long double z) { return y * z * log(x); } int main() { const char* result[12] = { x^y^z , x^z^y , (x^y)^z , (x^z)^y , y^x^z , y^z^x , (y^x)^z , (y^z)^x , z^x^y , z^y^x , (z^x)^y , (z^y)^x }; long double a[20]; long double x, y, z; cin >> x >> y >> z; a[0] = cal(x, y, z); a[1] = cal(x, z, y); a[2] = ca(x, y, z); a[3] = ca(x, z, y); a[4] = cal(y, x, z); a[5] = cal(y, z, x); a[6] = ca(y, x, z); a[7] = ca(y, z, x); a[8] = cal(z, x, y); a[9] = cal(z, y, x); a[10] = ca(z, x, y); a[11] = ca(z, y, x); long double maxn = a[0]; int res = 0; for (int i = 1; i < 12; ++i) { if (maxn < a[i] - ESP) { maxn = a[i]; res = i; } } printf( %s , result[res]); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll MOD = (int)1e9 + 7; ll modAdd(ll a, ll b) { if (a + b >= MOD) return a + b - MOD; else return a + b; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n >> m; vector<int> sweetness(n); for (int& v : sweetness) cin >> v; vector<vector<pair<int, int>>> offs(n); for (int i = 0; i < m; ++i) { int s, h; cin >> s >> h; int a = 0; for (int need = h; a < n; ++a) { if (sweetness[a] == s) --need; if (need == 0) break; } int b = n - 1; for (int need = h; b >= 0; --b) { if (sweetness[b] == s) --need; if (need == 0) break; } offs[s - 1].push_back({a, b}); } vector<pair<int, ll>> res; for (int a = -1; a < n; ++a) { int cou = 0; ll v0 = (a != -1); ll v1 = (a == -1); for (auto& vec : offs) { ll dp[2][2][2]; for (int i = 0; i < 2; ++i) { for (int j = 0; j < 2; ++j) { for (int t = 0; t < 2; ++t) { dp[i][j][t] = 0; } } } dp[0][0][0] = v0; dp[0][0][1] = v1; for (auto pr : vec) { int le = (pr.first <= a) + (pr.first == a); int ri = (pr.second >= a + 1); for (int i = 1; i >= 0; --i) { for (int j = 1; j >= 0; --j) { for (int t = 1; t >= 0; --t) { if (le && i) { if (le == 2 && t == 1) { dp[i][j][t] = modAdd(dp[i][j][t], modAdd(dp[0][j][0], dp[0][j][1])); } else if (le == 1) { dp[i][j][t] = modAdd(dp[i][j][t], dp[0][j][t]); } } if (ri && j) { dp[i][j][t] = modAdd(dp[i][j][t], dp[i][0][t]); } } } } } if (dp[1][1][0] || dp[1][1][1]) { cou += 2; v0 = dp[1][1][0]; v1 = dp[1][1][1]; } else if (dp[1][0][0] || dp[1][0][1] || dp[0][1][0] || dp[0][1][1]) { ++cou; v0 = modAdd(dp[1][0][0], dp[0][1][0]); v1 = modAdd(dp[1][0][1], dp[0][1][1]); } } res.push_back({cou, v1}); } sort(res.begin(), res.end()); ll ans = 0; int mc = res.back().first; while (!res.empty() && res.back().first == mc) { ans = modAdd(ans, res.back().second); res.pop_back(); } cout << mc << << ans << n ; }
#include <bits/stdc++.h> using namespace std; const long long int N = 21; long long int tot[N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long int n; cin >> n; long long int i, j, a[n], b, c, ans = 0; for (i = 0; i < n; i++) { cin >> a[i]; for (j = 0; j < N; j++) { if (a[i] & (1LL << j)) { tot[j]++; } } } for (i = 0; i < n; i++) { long long int cur = 0; for (j = 0; j < N; j++) { if (tot[j]) { cur += (1LL << j); tot[j]--; } } ans += (cur * cur); } cout << ans; return 0; }
#include <bits/stdc++.h> using namespace std; bool test_cases_exist = false; void solve() { long long int i, j, n, m, k, x, y, ans; cin >> n; vector<long long int> v(n); for (auto &z : v) cin >> z; long long int bit = 23; long long int oneall = (1 << 23) - 1; vector<long long int> dp((1 << 23), -1); for (auto z : v) dp[z] = z; for (i = 0; i < (1 << bit); i++) { for (j = 0; j < bit; j++) { if (dp[i] != -1) break; if (i >> j & 1) dp[i] = dp[i ^ 1 << j]; } } for (auto z : v) cout << dp[oneall ^ z] << ; cout << n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int t = 1; if (test_cases_exist) cin >> t; while (t--) solve(); }
#include <bits/stdc++.h> using namespace std; int main() { long long n, m; cin >> n >> m; long long a = 1; long long b = 1; long long c = 1000000007; for (int i = 0; i < n; i++) { b = b * (2 * m - 1); b = b + a; b = b % c; a = a * m; a = a % c; } cout << b; return 0; }
#include <bits/stdc++.h> using namespace std; int dfn[2100], low[2100], tim, dis[2100], n, i, x, y, M, be[2100], m, j, ans, du[2100]; vector<int> E[2100], e[2100], q; void tarjan(int x, int ff) { dfn[x] = low[x] = ++tim; q.push_back(x); for (int i = 0; i < e[x].size(); ++i) if (e[x][i] != ff) { if (!dfn[e[x][i]]) { tarjan(e[x][i], x); low[x] = min(low[x], low[e[x][i]]); } else low[x] = min(low[x], dfn[e[x][i]]); } if (low[x] == dfn[x]) { ++M; for (;;) { int t = q.back(); q.pop_back(); be[t] = M; if (t == x) break; } } } void dfs(int p, int ff, int dis) { dfn[p] = 1; if (dis > y) y = dis, x = p; for (int i = 0; i < E[p].size(); ++i) if (E[p][i] != ff) dfs(E[p][i], p, dis + 1); } int main() { scanf( %d%d , &n, &m); for (i = 1; i <= m; ++i) { scanf( %d%d , &x, &y); e[x].push_back(y); e[y].push_back(x); } for (i = 1; i <= n; ++i) if (!dfn[i]) tarjan(i, 0); for (i = 1; i <= n; ++i) for (j = 0; j < e[i].size(); ++j) if (be[i] != be[e[i][j]]) E[be[i]].push_back(be[e[i][j]]), du[be[i]]++; memset(dfn, 0, sizeof(dfn)); for (i = 1; i <= M; ++i) { if (!dfn[i]) { y = 0; dfs(i, 0, 1); y = 0; dfs(x, 0, 1); if (y == 1) ans--; else ans += y - 2 - 1; } if (du[i] <= 1) ans++; } printf( %d n , n - ans - 1); }
#include <bits/stdc++.h> using namespace std; const int MAXN = 100005; const int INF = 1e9; struct dot { int l, r; }; typedef int node; const int neutro = 0; struct RMQ { int sz; node t[4 * MAXN]; node &operator[](int p) { return t[sz + p]; } void init(int n) { sz = 1 << (32 - __builtin_clz(n)); for (int i = (0); i < (int)(2 * sz); i++) t[i] = neutro; } void updall() { for (int i = sz - 1; i >= 0; i--) { t[i] = (t[2 * i] ^ t[2 * i + 1]); } } node get(int i, int j) { return get(i, j, 1, 0, sz); } node get(int i, int j, int n, int a, int b) { if (j <= a || i >= b) return neutro; if (i <= a && b <= j) return t[n]; int c = (a + b) / 2; return (get(i, j, 2 * n, a, c) ^ get(i, j, 2 * n + 1, c, b)); } void set(int p, node val) { for (p += sz; p > 0 && t[p] != val;) { t[p] = val; p /= 2; val = (t[p * 2] ^ t[p * 2 + 1]); } } } rmq; int n, grundy[MAXN], ans[MAXN]; vector<dot> events[MAXN]; int main() { rmq.init(MAXN); rmq.updall(); for (int i = (1); i < (int)(MAXN); i++) { int s = i; for (int j = i; s < MAXN; j++, s += j) { if (j == i) continue; events[s].push_back({i, j}); } } grundy[1] = grundy[2] = 0; rmq.set(1, 0); rmq.set(2, 0); for (int i = (3); i < (int)(MAXN); i++) { ans[i] = INF; long long bm = 0; for (auto ev : events[i]) { long long v = rmq.get(ev.l, ev.r + 1); if (v < 0) { printf( %d %d %d : %d %d n , i, ev.l, ev.r, grundy[ev.l], grundy[ev.r]); exit(-1); } bm |= (1ll << v); if (v == 0) { if (ev.r - ev.l + 1 < ans[i]) { ans[i] = ev.r - ev.l + 1; } } } for (long long j = 0; j < 60; j++) { if ((bm & (1ll << j)) == 0) { grundy[i] = j; break; } } rmq.set(i, grundy[i]); } while (scanf( %d , &n) == 1) { printf( %d n , grundy[n] == 0 ? -1 : ans[n]); } return 0; }
#include <bits/stdc++.h> using namespace std; map<string, int> m; int dis(string s1, string s2) { return (m[s2] - m[s1] + 12) % 12; } int main() { string s1, s2, s3; m[ C ] = 0; m[ C# ] = 1; m[ D ] = 2; m[ D# ] = 3; m[ E ] = 4; m[ F ] = 5; m[ F# ] = 6; m[ G ] = 7; m[ G# ] = 8; m[ A ] = 9; m[ B ] = 10; m[ H ] = 11; cin >> s1 >> s2 >> s3; if ((dis(s1, s2) == 4 && dis(s2, s3) == 3) || (dis(s1, s3) == 4 && dis(s3, s2) == 3) || (dis(s2, s1) == 4 && dis(s1, s3) == 3) || (dis(s2, s3) == 4 && dis(s3, s1) == 3) || (dis(s3, s1) == 4 && dis(s1, s2) == 3) || (dis(s3, s2) == 4 && dis(s2, s1) == 3)) cout << major n ; else if ((dis(s1, s2) == 3 && dis(s2, s3) == 4) || (dis(s1, s3) == 3 && dis(s3, s2) == 4) || (dis(s2, s1) == 3 && dis(s1, s3) == 4) || (dis(s2, s3) == 3 && dis(s3, s1) == 4) || (dis(s3, s1) == 3 && dis(s1, s2) == 4) || (dis(s3, s2) == 3 && dis(s2, s1) == 4)) cout << minor n ; else cout << strange n ; }
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, v[N], id[N], now[N], p[N]; bool is_lucky(int x) { while (x) { if (x % 10 != 7 && x % 10 != 4) return false; x /= 10; } return true; } void change(int pos1, int pos2) { swap(now[p[pos1]], now[p[pos2]]); swap(p[pos1], p[pos2]); } int main() { scanf( %d , &n); for (int i = 0; i < n; i++) scanf( %d , v + i), p[i] = id[i] = now[i] = i; if (is_sorted(v, v + n)) return printf( 0 n ), 0; sort(id, id + n, [](int a, int b) { return v[a] < v[b]; }); int c = -1; for (int i = 0; i < n; i++) if (is_lucky(v[i])) { c = i; break; } if (c == -1) return printf( -1 n ), 0; vector<pair<int, int>> ans; for (int i = 0; i < n; i++) { if (is_lucky(v[i])) { c = i; break; } } for (int i = 0; i < n; i++) if (p[i] != id[i]) { if (now[c] != i) { ans.emplace_back(now[c], i); change(now[c], i); } if (now[c] != now[id[i]]) { ans.emplace_back(now[c], now[id[i]]); change(now[c], now[id[i]]); } } printf( %d n , (int)ans.size()); for (auto x : ans) printf( %d %d n , x.first + 1, x.second + 1); }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int a, b; cin >> a >> b; if (a == b) { cout << 0 << n ; continue; } if (a - b > 5) { int answ = 0; int cant = (a - b) / 5; answ = answ + cant; a = a - cant * 5; if (a - b == 1 or a - b == 2) answ = answ + 1; else { if (a != b) answ = answ + 2; } cout << answ << n ; continue; } if (b - a > 5) { int answ = 0; int cant = (b - a) / 5; a = a + cant * 5; answ = answ + cant; if (b - a == 1 or b - a == 2) answ = answ + 1; else { if (a != b) answ = answ + 2; } cout << answ << n ; continue; } if (abs(a - b) == 1 or abs(a - b) == 2 or abs(a - b) == 5) cout << 1 << n ; else cout << 2 << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; char a[n + 2][m + 2]; int flag = 0; for (long long int i = 0; i <= n + 1; i++) { for (long long int j = 0; j <= m + 1; j++) { a[i][j] = . ; } } for (long long int i = 1; i <= n; i++) for (long long int j = 1; j <= m; j++) cin >> a[i][j]; for (long long int i = 1; i <= n; i++) { for (long long int j = 1; j <= m; j++) { if (a[i][j] == S ) { if (a[i - 1][j] == W || a[i + 1][j] == W || a[i][j - 1] == W || a[i][j + 1] == W ) { flag = 1; break; } } if (a[i][j] == W ) { if (a[i - 1][j] == . || a[i - 1][j] == D ) a[i - 1][j] = D ; if (a[i + 1][j] == . || a[i + 1][j] == D ) a[i + 1][j] = D ; if (a[i][j - 1] == . || a[i][j - 1] == D ) a[i][j - 1] = D ; if (a[i][j + 1] == . || a[i][j + 1] == D ) a[i][j + 1] = D ; } } if (flag == 1) break; } if (flag == 1) cout << No << n ; else { cout << Yes << n ; for (long long int i = 1; i <= n; i++) { for (long long int j = 1; j <= m; j++) { cout << a[i][j]; } cout << n ; } } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 6; const int maxlog = 20; const long long inf = 1e18; const int mod = 1e9 + 7; const long long base = 307; map<long long, int> cnt1, cnt2; set<int> st; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; int need = (n + 1) / 2; for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; cnt1[x]++; if (y != x) cnt2[y]++; st.insert(x); st.insert(y); } int ans = mod; for (set<int>::iterator it = st.begin(); it != st.end(); it++) { int x = *it; int kol = cnt1[x] + cnt2[x]; if (kol < need) continue; int new_need = need - cnt1[x]; ans = min(ans, new_need); } if (ans == mod) cout << -1; else cout << max(0, ans); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int n, i, c = 0; cin >> n; long long int a[n]; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (i = 0; i < n; i++) { if (a[i] == a[i + 1]) { c++; i++; } } cout << c / 2; return 0; }
#include <bits/stdc++.h> using namespace std; bool prime(int x) { if (x == 1) return false; else if (x == 2) return true; for (int i = 2; i * i <= x; i++) { if (x % i == 0) return false; } return true; } void fun2(int t) { if (t == 1) { cout << * ; return; } cout << * ; return fun2(t - 1); } void fun(int s) { map<int, int> asd; map<int, int>::iterator ir; ir = asd.begin(); ir->first; if (s) { cout << s % 10 << endl; fun(s / 10); } } long long numb(long long i) { if (i == 2 || i == 1) return 1; else { long long n = i, z = 0; while (n > 1) { if (i % n == 0) { z++; } n--; } return z; } } int main() { std::ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); int r1, r2, c1, c2, d1, d2; cin >> r1 >> r2 >> c1 >> c2 >> d1 >> d2; vector<vector<int>> v(2, vector<int>(2)); v[0][0] = (r1 - c2 + d1) / 2; v[0][1] = r1 - v[0][0]; v[1][0] = c1 - v[0][0]; v[1][1] = d1 - v[0][0]; bool b = true; for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for (int h = 0; h < 2; h++) { for (int g = 0; g < 2; g++) { if (i == h && j == g) continue; if (v[i][j] == v[h][g]) { b = false; } } } } } for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { if (v[i][j] == 0 || v[i][j] > 9) b = false; } } if (r1 != v[0][0] + v[0][1] || r2 != v[1][0] + v[1][1] || c1 != v[0][0] + v[1][0] || c2 != v[0][1] + v[1][1] || d1 != v[0][0] + v[1][1] || d2 != v[0][1] + v[1][0]) b = false; if (b) { for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { cout << v[i][j] << ; } cout << endl; } } else { cout << -1 << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; vector<string> s(3e5 + 1); vector<pair<int, int>> p; int n; vector<pair<int, int>> q; int Kr(int kb, int kn) { int ans = 0; for (int i = 0; i < n; i++) { int r1 = q[i].first - kb; int r2 = q[i].second - kn; int r; if (r1 * r2 >= 0) r = max(abs(r1), abs(r2)); else r = abs(r1) + abs(r2); ans = max(ans, r); } return ans; } pair<int, int> F(int kb) { int Fl = 0, Fr = 5e5; if (!kb) Fl++; while (Fr - Fl > 2) { int m1 = Fl + (Fr - Fl) / 3; int m2 = Fr - (Fr - Fl) / 3; if (Kr(kb, m1) >= Kr(kb, m2)) Fl = m1; else Fr = m2; } return min(make_pair(Kr(kb, Fl), Fl), min(make_pair(Kr(kb, Fl + 1), Fl + 1), make_pair(Kr(kb, Fl + 2), Fl + 2))); } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> s[i]; } q.resize(n); for (int i = 0; i < n; i++) { int kb = 0; for (int j = 0; j < s[i].size(); j++) { if (s[i][j] == B ) ++kb; } q[i] = {kb, s[i].size() - kb}; } int l = 0, r = 5e5; while (r - l > 2) { int m1 = l + (r - l) / 3; int m2 = r - (r - l) / 3; if (F(m1) >= F(m2)) l = m1; else r = m2; } auto l0 = F(l); auto l1 = F(l + 1); auto l2 = F(l + 2); if (l0 <= l1 && l0 <= l2) { cout << l0.first << n ; for (int i = 0; i < l; i++) { cout << B ; } for (int i = 0; i < l0.second; i++) { cout << N ; } } else if (l1 <= l0 && l1 <= l2) { cout << l1.first << n ; for (int i = 0; i < l + 1; i++) { cout << B ; } for (int i = 0; i < l1.second; i++) { cout << N ; } } else { cout << l2.first << n ; for (int i = 0; i < l + 2; i++) { cout << B ; } for (int i = 0; i < l2.second; i++) { cout << N ; } } }
#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { long long n; cin >> n; long long sum = n * (n + 1) / 2; if (sum % 2 == 1) cout << 1; else cout << 0; }
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; cout << N << ; for (int i = 1; i < N; i++) cout << i << ; cout << endl; return 0; }
#include <bits/stdc++.h> using namespace std; using namespace std::chrono; template <class e, class t, int N> std::basic_istream<e, t>& operator>>(std::basic_istream<e, t>& in, const e (&literal)[N]) { std::array<e, N - 1> buffer; in >> buffer[0]; if (N > 2) in.read(&buffer[1], N - 2); if (strncmp(&buffer[0], literal, N - 1)) in.setstate(in.rdstate() | std::ios::badbit); return in; } template <class e, class t> std::basic_istream<e, t>& operator>>(std::basic_istream<e, t>& in, const e& literal) { e buffer; in >> buffer; if (buffer != literal) in.setstate(in.rdstate() | std::ios::badbit); return in; } template <class e, class t, int N> std::basic_istream<e, t>& operator>>(std::basic_istream<e, t>& in, e (&carray)[N]) { return std::operator>>(in, carray); } template <class t1, class t2> istream& operator>>(istream& in, pair<t1, t2>& p) { in >> p.first >> p.second; return in; } template <class t> istream& operator>>(istream& in, vector<t>& v) { for (int i = 0; i < v.size(); i++) { in >> v[i]; } return in; } template <class t> ostream& operator<<(ostream& out, vector<t>& a) { for (auto x : a) { out << x << ; } return out; } template <class t1, class t2> ostream& operator<<(ostream& out, map<t1, t2>& a) { for (auto x : a) { out << x << ; } return out; } template <class t> ostream& operator<<(ostream& out, set<t>& a) { for (auto x : a) { out << x << ; } return out; } template <class t1, class t2> ostream& operator<<(ostream& out, const pair<t1, t2>& a) { out << a.first << << a.second; return out; } template <class t1, class t2> pair<t1, t2> operator+(const pair<t2, t1>& a, const pair<t1, t2>& b) { return {a.first + b.first, a.second + b.second}; } template <class t1, class t2> pair<t1, t2> operator-(const pair<t2, t1>& a, const pair<t1, t2>& b) { return {a.first - b.first, a.second - b.second}; } long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return a * (b / gcd(a, b)); } long long fpow(long long b, long long e, long long m = (long long)(1e9 + 7)) { long long r = 1; while (e) { if (e & 1) r = (r * b) % m; e >>= 1; b = (b * b) % m; } return r; } long long n, m; vector<long long> a; long long c[100][100]; long double dp[100][100][100]; void init() { for (long long i = 0; i < (long long)60; i++) for (long long j = 0; j < (long long)60; j++) for (long long k = 0; k < (long long)70; k++) dp[i][j][k] = -2; for (long long i = 0; i < (long long)51; i++) { for (long long j = 0; j < (long long)51; j++) { if (j == 0) c[i][j] = 1; else if (i == 0) c[i][j] = 0; else c[i][j] = c[i - 1][j - 1] + c[i - 1][j]; } } } long double solve(long long i, long long k, long long mx) { if (i == m) return (!k ? mx : 0); long double& r = dp[i][k][mx]; if (r > -1) return r; long double p = 1; r = 0; for (long long j = 0; j < (long long)k + 1; j++) { long long len = (j + a[i] - 1) / a[i]; r += (c[k][j] * p * solve(i + 1, k - j, max(mx, len))); p /= m; } return r; } int32_t main(int32_t argc, char const* argv[]) { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n >> m; a.resize(m); cin >> a; init(); cout << setprecision(15) << solve(0, n, 0) << n ; return 0; }
#include <bits/stdc++.h> using namespace std; bool field[100][100][100]; int n, m, k; bool check(int x, int y, int z) { return (x >= 0 && x < n && y >= 0 && y < m && z >= 0 && z < k && field[x][y][z]); } int main() { cin.sync_with_stdio(false); cout.sync_with_stdio(false); cin >> n >> m >> k; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { for (int x = 0; x < k; x++) { char v; cin >> v; field[i][j][x] = (v == 1 ); } } } int answer = 0; for (int x = 0; x < n; x++) { for (int y = 0; y < m; y++) { for (int z = 0; z < k; z++) { if (check(x, y, z)) { if ((check(x - 1, y, z) && check(x + 1, y, z)) || (check(x, y - 1, z) && check(x, y + 1, z)) || (check(x, y, z - 1) && check(x, y, z + 1))) { answer++; continue; } if (check(x - 1, y, z) && ((check(x, y + 1, z) && !check(x - 1, y + 1, z)) || (check(x, y, z + 1) && !check(x - 1, y, z + 1)))) { answer++; continue; } if (check(x, y - 1, z) && ((check(x + 1, y, z) && !check(x + 1, y - 1, z)) || (check(x, y, z + 1) && !check(x, y - 1, z + 1)))) { answer++; continue; } if (check(x, y, z - 1) && ((check(x, y + 1, z) && !check(x, y + 1, z - 1)) || (check(x + 1, y, z) && !check(x + 1, y, z - 1)))) { answer++; continue; } } } } } cout << answer << n ; }
#include <bits/stdc++.h> using namespace std; const int N = 1005; char g[N][N]; int l[N][N], r[N][N], up[N][N], dn[N][N]; int jx[N][N], jy[N][N]; struct Node { int x, y, len; }; vector<Node> p; int main() { int n, m; scanf( %d%d , &n, &m); for (int i = 1; i <= n; i++) scanf( %s , g[i] + 1); for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (g[i][j] == * ) { l[i][j] = l[i][j - 1] + 1; up[i][j] = up[i - 1][j] + 1; } for (int i = n; i >= 1; i--) for (int j = m; j >= 1; j--) if (g[i][j] == * ) { r[i][j] = r[i][j + 1] + 1; dn[i][j] = dn[i + 1][j] + 1; } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (g[i][j] == * ) { int len = min(min(l[i][j], r[i][j]), min(up[i][j], dn[i][j])); if (len > 1) { Node temp; temp = (Node){i, j, len - 1}; p.push_back(temp); jx[i - len + 1][j]++, jx[i + len][j]--; jy[i][j - len + 1]++, jy[i][j + len]--; } } for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) jx[i][j] += jx[i - 1][j], jy[i][j] += jy[i][j - 1]; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) if (g[i][j] == * && !jx[i][j] && !jy[i][j]) { printf( -1 n ); return 0; } printf( %d n , (int)p.size()); for (int i = 0; i < p.size(); i++) printf( %d %d %d n , p[i].x, p[i].y, p[i].len); return 0; }
#include<bits/stdc++.h> using namespace std; #define M 105 int frq[M]; int main() { int t; scanf( %d , &t); while(t--) { int n, x; scanf( %d %d , &n, &x); memset(frq, 0, sizeof(frq)); for(int i = 1; i <= n; i++) { int p; scanf( %d , &p); frq[p]++; } vector<int> ans; for(int i = 1; i <= 100; i++) { while (frq[i] > 1) { ans.push_back(i); frq[i]--; } } for(int i = 1; i <= 100; i++) { if(frq[i]) { ans.push_back(i); frq[i]--; } } int ald = 0; for(int i = 0; i < n; i++) { ald += ans[i]; if(ald == x) { swap(ans[i], ans[n-1]); break; } } ald = 0; bool flag = true; for(int d: ans) { ald += d; if(ald == x) { flag = false; break; } } if(!flag) printf( NO n ); else { printf( YES n ); for(int d: ans) printf( %d , d); printf( n ); } } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n, i, x, k = 0; cin >> n; vector<int> a; for (i = 0; i < n; i++) { cin >> x; a.push_back(x); } sort(a.begin(), a.end()); for (i = 0; i < n; i++) { if (a[i] % 2 != 0) k++; } int y = min(k, n - k); cout << y << n ; return 0; }
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> #include <vector> using namespace std; # define rep(i,a,b) for(int i=(a); i<=(b); ++i) # define drep(i,a,b) for(int i=(a); i>=(b); --i) typedef long long int_; inline int readint(){ int a = 0; char c = getchar(), f = 1; for(; c< 0 ||c> 9 ; c=getchar()) if(c == - ) f = -f; for(; 0 <=c&&c<= 9 ; c=getchar()) a = (a<<3)+(a<<1)+(c^48); return a*f; } inline void getMin(int &a,const int &b){ if(a > b) a = b; } const int Mod = 998244353; inline void add(int &x,const int &y){ (x += y) >= Mod ? x -= Mod : 0; } inline void mul(int &x,const int &y){ x = int_(x)*y%Mod; } int inv[4] = {0,1,(Mod+1)/2,(Mod+1)/3}; inline int C(const int &n,const int &m){ int res = 1; rep(i,1,m) mul(res,inv[i]); rep(i,0,m-1) mul(res,n-i); return res; } const int MaxN = 1000005; const int infty = (1<<30)-1; int dp[MaxN][4]; // prefix sum int main(){ int n = readint(), x; dp[0][0] = 1; // only one node rep(i,1,n) rep(j,0,3){ rep(k,x=0,2) // at most 2 add(x,dp[i-1][k]); dp[i][j] = C(j+x-1,j); } drep(i,n,1) rep(j,0,3) // get origin value add(dp[i][j],Mod-dp[i-1][j]); int ans = 0; rep(i,1,n) rep(j,2,3) rep(k,0,3-j) add(ans,1ll*dp[i][j]*dp[n-i][k]%Mod); rep(j,0,3) add(ans,dp[n][j]); printf( %d n ,ans); return 0; }
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == - ) f = -1; for (; isdigit(c); c = getchar()) x = x * 10 + c - 0 ; return x * f; } const long long MAXN = 100010; const long long INF = 2147483600; long long N; long long a[MAXN + 1]; int f[63][MAXN + 1]; int p[MAXN + 1], id[MAXN + 1], nwp[MAXN + 1]; int t[2]; long long sta[2][MAXN + 1]; int cnt[2]; inline void Sort(long long x) { t[0] = t[1] = 0; for (long long i = 1; i <= N; i++) { long long d = (a[p[i]] >> x) & 1; sta[d][++t[d]] = p[i]; } for (long long i = 1; i <= t[1]; i++) p[i] = sta[1][i]; for (long long i = 1; i <= t[0]; i++) p[i + t[1]] = sta[0][i]; return; } int main() { N = read(); for (long long i = 1; i <= N; i++) { a[i] = read(); } memset(f, 127, sizeof(f)); for (long long i = 1; i <= N; i++) p[i] = i; Sort(0); f[0][0] = t[0]; f[0][t[1]] = min(f[0][t[1]], t[1]); for (long long i = 1; i <= 62; i++) { for (long long j = 1; j <= N; j++) id[j] = p[j]; Sort(i); cnt[0] = cnt[1] = 0; for (long long j = 1; j <= N; j++) nwp[p[j]] = j; int mx1 = 0, mx0 = 0; for (long long j = 0; j <= N; j++) { if (j) { long long x = (a[id[j]] >> i) & 1; ++cnt[x]; if (x) mx1 = max(mx1, nwp[id[j]]); mx0 = max(mx0, nwp[id[j]]); } if (f[i - 1][j] <= i * N) { f[i][mx1] = min(f[i][mx1], f[i - 1][j] + cnt[1] + t[0] - cnt[0]); f[i][max(t[1], mx0)] = min(f[i][max(t[1], mx0)], f[i - 1][j] + cnt[0] + t[1] - cnt[1]); } } } printf( %d n , f[62][0]); return 0; }
#include <bits/stdc++.h> using namespace std; int x, q; vector<int> adj[205]; bool visited[205]; void dfs(int n) { visited[n] = 1; for (int i = 0; i < adj[n].size(); i++) { if (!visited[adj[n][i]]) { visited[adj[n][i]] = 1; dfs(adj[n][i]); } } } int main() { int n, m; cin >> n >> m; bool flag = 0; for (int i = m + 1; i <= n + m; i++) { cin >> q; while (q--) { cin >> x; if (x) { flag = 1; } adj[i].push_back(x); adj[x].push_back(i); } } if (!flag) { cout << n << endl; return 0; } int ans = -1; for (int i = m + 1; i <= n + m; i++) { if (!visited[i]) { dfs(i); ans++; } } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; string ar[] = { zero , one , two , three , four , five , six , seven , eight , nine , ten , eleven , twelve , thirteen , fourteen , fifteen , sixteen , seventeen , eighteen }; string br[] = { , ten , twenty , thirty , forty , fifty , sixty , seventy , eighty , ninety }; int main() { int q = 1; while (q--) { int n; cin >> n; if (n <= 18) { cout << ar[n]; continue; } if (n < 20) { cout << ar[n % 10] << teen ; continue; } int f = n / 10; if (n % 10 == 0) { cout << br[f]; continue; } cout << br[f] << - << ar[n % 10]; } }
#include <bits/stdc++.h> using ll = long long int; using namespace std; struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; bool printSub(int a[], int sum) { if (sum == 0) return true; if (sum < 0) return false; for (int i = 0; i < 3; ++i) { int rem = sum - a[i]; if (printSub(a, rem)) { cout << a[i] << ; return true; } } } void solve() { int n, ans = 0; cin >> n; int a[n]; for (int i = 0; i < n; ++i) cin >> a[i]; for (int i = 30; i > 0; --i) { int sum = 0; for (int j = 0; j < n; ++j) { if (a[j] > i) { sum = 0; } else sum = max(a[j], sum + a[j]); ans = max(ans, sum - i); } } cout << ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t = 1; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> int col[100002]; int cal[5]; int main() { int n, m, i, j, k; int inp[3]; scanf( %d%d , &n, &m); for (i = 0; i < m; i++) { for (j = 0; j <= 3; j++) { cal[j] = 0; } for (j = 0; j < 3; j++) { scanf( %d , inp + j); cal[col[inp[j]]]++; } k = 0; for (j = 1; j <= 3; j++) { if (cal[j] == 0) { while (col[inp[k]] != 0) { k++; } col[inp[k]] = j; } } } for (i = 1; i <= n; i++) { if (col[i] == 0) { col[i] = 1; } printf( %d , col[i]); } }
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0); cin.tie(0); int n, s; cin >> n >> s; int a[s]; memset(a, 0, sizeof a); for (int i = 0; i < n; i++) { int x, y; cin >> x >> y; a[x - 1] = max(a[x - 1], y); } int t = 0; for (int i = 0; i < s; i++) { t += max(a[s - i - 1] - t, 0); t++; } cout << t << endl; return 0; }
#include <bits/stdc++.h> using namespace std; char grid[105][105]; int dix[] = {-1, 1, 0, 0}, diy[] = {0, 0, -1, 1}, par[105][105], col[105][105], n, m; char ddd[] = UDLR ; vector<int> vec; void dfs(int xx, int yy) { int i, p, q; col[xx][yy] = 1; for (i = 0; i < 4; i++) { p = xx + dix[i]; q = yy + diy[i]; if (p >= 0 && p < n && q >= 0 && q < m && grid[p][q] != * && !col[p][q]) { par[p][q] = i; dfs(p, q); } } return; } int main() { int i, j, k, p, q, x, y; scanf( %d %d , &n, &m); for (i = 0; i < n; i++) scanf( %s , grid[i]); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (grid[i][j] == F ) p = i, q = j; } } dfs(0, 0); while (p || q) { vec.push_back(par[p][q]); y = par[p][q]; p -= dix[y], q -= diy[y]; } reverse(vec.begin(), vec.end()); for (i = 0; i < vec.size(); i++) { printf( %c n , ddd[vec[i]]); fflush(stdout); scanf( %d %d , &x, &y); x--, y--; if (p == x && q == y) { swap(ddd[vec[i]], ddd[vec[i] - 1]); i--; } p = x, q = y; } return 0; }
#include <bits/stdc++.h> bool a[1005][1005]; int main() { int n; scanf( %d , &n); if (n == 3) { return puts( 0 1 0 n0 0 1 n1 0 0 ), 0; } else if (n == 4) { return puts( -1 ), 0; }; int n1 = n - (~n & 1); int i, j; for (i = 1; i <= n1; i++) { a[i][i % n1 + 1] = 1; for (j = 4; j < n1; j += 2) { a[i][(i + j - 2) % n1 + 1] = 1; }; }; if (~n & 1) { a[n][1] = 1; for (i = 5; i <= n; i += 2) a[n][i] = 1; a[3][n] = 1; for (i = 2; i < n; i += 2) a[i][n] = 1; }; for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { putchar(48 + a[i][j]); putchar(32); }; putchar( n ); }; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int q; cin >> q; for (int k = 0; k < q; ++k) { string s, t; cin >> s >> t; vector<bool> a(26), b(26); for (int i = 0; i < s.size(); ++i) { a[s[i] - a ] = true; b[t[i] - a ] = true; } bool flag = false; for (int i = 0; i < 26; ++i) { if (a[i] && b[i]) { flag = true; break; } } if (flag) cout << YES n ; else cout << NO n ; } return 0; }
#include <bits/stdc++.h> using namespace std; struct T { T *l, *r; long long d, sum, mx; bool pushed; void recalc(long long len, long long d) { this->d = d; sum = len * d; mx = max(0ll, sum); } T(long long len, long long d) { l = r = 0; this->d = d; sum = len * d; mx = max(0ll, sum); pushed = 0; } void push(int l, int r) { if (!pushed) return; int m = (l + r) / 2; this->l->recalc(m + 1 - l, d); this->r->recalc(r + 1 - m, d); this->l->pushed = 1; this->r->pushed = 1; pushed = 0; } void upd(int l, int r, int tl, int tr, long long x) { if (r < tl || tr < r) return; if (tl <= l && r <= tr) { d = x; sum = (r - l + 1) * d; mx = max(0ll, sum); pushed = 1; } int m = (l + r) / 2; if (!this->l) this->l = new T(m - l + 1, d); if (!this->r) this->r = new T(r - m + 1, d); push(l, r); this->l->upd(l, m, tl, tr, x); this->r->upd(m + 1, r, tl, tr, x); sum = this->l->sum + this->r->sum; mx = max(this->l->mx, this->l->sum + this->r->mx); } }; int main() { long long n; cin >> n; for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { while (n % i == 0) n /= i; return cout << (n == 1 ? i : 1), 0; } } cout << n; return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5, M = N * 3; int n, i, j, l, r, scnt, s1; bool bb[M]; int w1[M], bbel[M]; unsigned long long w2[M]; struct seg { int l, r, c, bel; bool operator<(const seg& rhs) const { return r < rhs.r; } }; set<seg> S; vector<int> ve[N], in[N], out[N]; void del(seg s) { S.erase(s); out[i].push_back(s.c); } void ins(seg s) { s.c = ++scnt; w1[scnt] = s.r - s.l + 1; w2[scnt] = 1ll * w1[scnt] * s.bel; bbel[scnt] = s.bel; S.insert(s); in[i].push_back(s.c); ve[s.bel].push_back(s.c); } unsigned long long s2, s3, s4, k; void rmv(int x) { if (bb[x]) bb[x] = 0, s1 -= w1[x], s2 -= w2[x]; } void calc(int lm) { s1 = s2 = s3 = s4 = 0; memset(bb + 1, 0, scnt); for (i = 1, j = 0; i <= n; ++i) { for (int x : out[i]) rmv(x); for (int x : in[i]) if (bbel[x] >= j) bb[x] = 1, s1 += w1[x], s2 += w2[x]; for (; s1 > lm || !j; ++j) for (int x : ve[j]) rmv(x); s3 += s2 - 1ll * s1 * (j - 1); s4 += i - j + 1; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k; S.insert((seg){1, 1 << 30, 0, 0}); for (i = 1; i <= n; ++i) { cin >> l >> r; --r; auto it = S.lower_bound((seg){0, l, 0, 0}); if (it->l < l) ins((seg){it->l, l - 1, 0, it->bel}); if (it->r >= r) { seg tmp = *it; del(*it); if (r < tmp.r) tmp.l = r + 1, ins(tmp); } else { for (; it != S.end() && it->r <= r;) del(*it++); if (it != S.end() && it->l <= r) { seg tmp = *it; del(*it); tmp.l = r + 1; ins(tmp); } } ins((seg){l, r, 0, i}); } calc(1 << 30); unsigned long long ts3 = s3; k = s4 - k; int le = 0, ri = 1e9, mid; for (; le < ri;) { mid = le + ri >> 1; calc(mid); if (s4 >= k) ri = mid; else le = mid + 1; } calc(le); cout << ts3 - (s3 - 1ll * (s4 - k) * le) << endl; return 0; }
#include <bits/stdc++.h> using namespace std; int w, h; char g[100][100], d[100][100]; bool use[100][100]; int dx[] = {-1, 0, 1, 0}; int dy[] = {0, -1, 0, 1}; bool is_valid(int y, int x) { return 0 <= y && y < h && 0 <= x && x < w; } bool check() { for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (g[i][j] == B && !use[i][j]) return true; } } return false; } void dfs(int y, int x, int dir, bool can) { if (!is_valid(y, x)) return; if (g[y][x] == W ) return; use[y][x] = true; if (!can) dfs(y + dy[dir], x + dx[dir], dir, can); else { dfs(y + dy[dir], x + dx[dir], dir, can); for (int i = 0; i < 4; i++) { if (i == dir) continue; dfs(y + dy[i], x + dx[i], i, false); } } return; } int main() { cin >> h >> w; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> g[i][j]; d[i][j] = g[i][j]; } } for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { memset(use, 0, sizeof(use)); if (g[i][j] == W ) continue; for (int k = 0; k < 4; k++) { dfs(i, j, k, true); } if (check()) { cout << NO << endl; return 0; } } } cout << YES << endl; return 0; }
#include <bits/stdc++.h> using namespace std; constexpr int MAX_N = 5000; constexpr int INF = numeric_limits<int>::max(); int N; int H[MAX_N + 1]; int DP[MAX_N + 1][MAX_N + 1]; void print(int* ptr, int n) { printf( [ ); while (n > 0) { printf( %d , *ptr); ++ptr; --n; } printf( ] n ); } int main() { cin >> N; if (N < 2) { cout << 0 << endl; return 0; } for (int i = 0; i < N; i++) { cin >> H[i]; } DP[0][0] = H[0]; for (int i = 1; i < N; i++) { if (DP[0][i - 1] == -1) { DP[0][i] = -1; } else { DP[0][i] = (H[i] >= H[i - 1]) ? H[i] : -1; } } if (DP[0][N - 1] != -1) { cout << 0 << endl; return 0; } for (int n = 1; n < N; n++) { DP[n][0] = H[0]; for (int i = 1; i < N; i++) { DP[n][i] = INF; if (DP[n - 1][i] != -1) { DP[n][i] = min(DP[n][i], DP[n - 1][i]); } if (DP[n][i - 1] != -1 && DP[n][i - 1] <= H[i]) { DP[n][i] = min(DP[n][i], H[i]); } if (DP[n - 1][i - 1] != -1) { DP[n][i] = min(DP[n][i], H[i] + DP[n - 1][i - 1]); } int S = H[i]; for (int m = n - 1; m >= 0; m--) { int j = i - (n - m); S += H[j]; if (j < 0 || (j > 0 && DP[m][j - 1] == -1)) break; if ((j == 0) || S >= DP[m][j - 1]) { DP[n][i] = min(DP[n][i], S); break; } } } for (int j = 1; j < N; j++) { DP[n][j] = (DP[n][j] == INF) ? -1 : DP[n][j]; } if (DP[n][N - 1] != -1) { cout << n << endl; return 0; } } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize( O3 ) const double PI = acos(-1.0); using namespace std; const double EPS = 1e-9; const int N = 5e5 + 9; int n, mem[3009][2 * 3009], nxt[3009]; pair<int, int> a[3009]; bool com(pair<int, int> a, pair<int, int> b) { if (a.first < b.first) return 1; else if (a.first == b.first) { if (a.second > b.second) return 1; else return 0; } else return 0; } int dp(int l, int mx) { if (l > n || a[l].first > mx) return 0; int& ret = mem[l][mx]; if (~ret) return ret; ret = dp(l + 1, mx); if (a[l].second <= mx) ret = max(ret, 1 + dp(l + 1, a[l].second) + dp(nxt[l], mx)); return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { cin >> n; map<int, int> mp; for (int i = 1; i <= n; i++) { cin >> a[i].first >> a[i].second; mp[a[i].first]; mp[a[i].second]; } int id = 0; for (auto& p : mp) p.second = ++id; for (int i = 1; i <= n; i++) { a[i].first = mp[a[i].first]; a[i].second = mp[a[i].second]; } sort(a + 1, a + 1 + n, com); for (int i = 1; i <= n; i++) { nxt[i] = n + 1; for (int j = i + 1; j <= n; j++) { if (a[j].first > a[i].second) { nxt[i] = j; break; } } } for (int i = 0; i <= n + 2; i++) for (int j = 0; j <= n * 2 + 9; j++) mem[i][j] = -1; cout << dp(1, n * 2 + 2) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int dis[10001][1001]; int pos[10001]; int g, r, n, m; void bfs() { deque<pair<int, pair<int, int> > > fila; fila.push_front({0, {0, 0}}); while (!fila.empty()) { int v = fila.front().first; int s = fila.front().second.first; int t = fila.front().second.second; fila.pop_front(); if (dis[v][s] != -1) continue; dis[v][s] = t; if (v) { if (s < g and s + (pos[v] - pos[v - 1]) <= g and dis[v - 1][s + (pos[v] - pos[v - 1])] == -1) { fila.push_front({v - 1, {s + (pos[v] - pos[v - 1]), t}}); } } if (v < m - 1) { if (s < g and s + (pos[v + 1] - pos[v]) <= g and dis[v + 1][s + (pos[v + 1] - pos[v])] == -1) { fila.push_front({v + 1, {s + (pos[v + 1] - pos[v]), t}}); } } if (s == g and dis[v][0] == -1) { fila.push_back({v, {0, t + 1}}); } } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); memset(dis, -1, sizeof(dis)); cin >> n >> m; int l[m]; for (int i = 0; i < m; i++) cin >> l[i]; sort(l, l + m); for (int i = 0; i < m; i++) pos[i] = l[i]; cin >> g >> r; bfs(); int res = -1; for (int i = 0; i < m - 1; i++) { if (dis[i][0] == -1) continue; if ((pos[m - 1] - pos[i]) > g) continue; int atual = dis[i][0] * (g + r) + (pos[m - 1] - pos[i]); if (res == -1) res = atual; else res = min(res, atual); } cout << res << endl; }
#include <bits/stdc++.h> using namespace std; const int maxn = 8e4 + 10; const long long mod = 998244353, modg = 3, modi = 332748118; int a[maxn], b[maxn]; int pow_mod(int a, int b) { int ans = 1; while (b) { if (b & 1) ans = (long long)ans * a % mod; a = (long long)a * a % mod, b >>= 1; } return ans; } int add(int a, int b) { a += b; if (a > mod) return a - mod; return a; } int sub(int a, int b) { a -= b; if (a < 0) return a + mod; return a; } class nubmer { public: int rev[maxn], len, pw; long long wt[2][maxn]; void init_0() { int len = 1; while (len <= maxn / 2) len <<= 1; for (int mid = 1; mid < len; mid <<= 1) { long long wn1 = pow_mod(modg, (mod - 1) / (mid << 1)); long long wn2 = pow_mod(modi, (mod - 1) / (mid << 1)); wt[0][mid] = wt[1][mid] = 1; long long wt1 = wn1, wt2 = wn2; for (int j = 1; j <= mid; ++j) { wt[0][mid + j] = wt1; wt1 = wt1 * wn1 % mod; wt[1][mid + j] = wt2; wt2 = wt2 * wn2 % mod; } } } void init(int n) { len = 1; pw = 0; while (len <= n) len <<= 1, pw++; for (int i = 0; i <= len - 1; ++i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (pw - 1)); } void transform(int *a, int flag) { int f = flag == 1 ? 0 : 1; for (int i = 0; i <= len - 1; ++i) if (i < rev[i]) swap(a[i], a[rev[i]]); for (int mid = 1; mid < len; mid <<= 1) { for (int r = mid << 1, j = 0; j < len; j += r) { long long *p = wt[f] + mid; int *p1 = a + j, *p2 = a + j + mid; for (int k = 0; k < mid; ++k, ++p, ++p1, ++p2) { int x = *p1, y = (*p) * (*p2) % mod; *p1 = add(x, y), *p2 = sub(x, y); } } } if (flag == -1) { long long inv = pow_mod(len, mod - 2); for (int i = 0; i <= len; i++) { a[i] = a[i] * inv % mod; if (a[i] < 0) a[i] += mod; } } } } ntt; int n, k; int s1[maxn], s2[maxn]; vector<int> solve(int l, int r) { if (l == r) { return vector<int>{1, s2[l]}; } int mid = (l + r) >> 1; vector<int> x = solve(l, mid), y = solve(mid + 1, r); ntt.init(x.size() + y.size()); for (int i = 0; i <= ntt.len + 5; i++) a[i] = 0, b[i] = 0; for (int i = 0; i < x.size(); i++) a[i] = x[i]; for (int i = 0; i < y.size(); i++) b[i] = y[i]; ntt.transform(a, 1); ntt.transform(b, 1); for (int i = 0; i < ntt.len; i++) a[i] = (long long)a[i] * b[i] % mod; ntt.transform(a, -1); int len = x.size() + y.size(); int mi = min(ntt.len, min(k + 1, len)); vector<int> ans(mi); for (int i = 0; i < mi; i++) { ans[i] = a[i]; } return ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); ntt.init_0(); cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> s1[i]; } int T; cin >> T; vector<int> tmp; while (T--) { int flag; cin >> flag; if (flag == 1) { int q, i, d; cin >> q >> i >> d; for (int j = 1; j <= n; j++) s2[j] = sub(q, s1[j]); s2[i] = sub(q, d); tmp = solve(1, n); cout << tmp[k] << n ; } else { int q, l, r, d; cin >> q >> l >> r >> d; for (int j = 1; j <= n; j++) { if (j >= l && j <= r) s2[j] = sub(sub(q, s1[j]), d); else s2[j] = sub(q, s1[j]); } tmp = solve(1, n); cout << tmp[k] << n ; } } return 0; }
#include <bits/stdc++.h> #pragma comment(linker, /STACK:256000000 ) using namespace std; inline void in(int &x) { scanf( %d , &x); } inline void in(int &x, int &y) { scanf( %d %d , &x, &y); } inline void in(int &x, int &y, int &z) { scanf( %d %d , &x, &y, &z); } inline void out(int x) { printf( %d , x); } inline void outln(int x) { printf( %d n , x); } inline void outln(int x, int y) { printf( %d %d n , x, y); } inline int _abs(int x) { return x > 0 ? x : -x; } const int SIZE = (int)1e7 + 10; int n, m; int a[SIZE], b[SIZE], minp[SIZE]; int pa[SIZE], pb[SIZE]; void precalc() { for (int i = 2; i * i < SIZE; ++i) { if (!minp[i]) { for (int j = i * i; j < SIZE; j += i) { if (!minp[j]) minp[j] = i; } } } for (int i = 2; i < SIZE; ++i) if (!minp[i]) minp[i] = i; } void factor(int *mas, int val) { for (; val != 1; val /= minp[val]) { ++mas[minp[val]]; } } void output(int *a, int *pa, int size) { for (int i = 0; i < (int)(size); i++) { int val = a[i], res = 1; for (; val != 1; val /= minp[val]) { if (pa[minp[val]]) --pa[minp[val]]; else res *= minp[val]; } out(res); } } int main() { precalc(); in(n, m); for (int i = 0; i < (int)(n); i++) { in(a[i]); factor(pa, a[i]); } for (int i = 0; i < (int)(m); i++) { in(b[i]); factor(pb, b[i]); } for (int i = 2; i < SIZE; ++i) { pa[i] = pb[i] = min(pa[i], pb[i]); } outln(n, m); output(a, pa, n); printf( n ); output(b, pb, m); return 0; }
#include <bits/stdc++.h> const int N = 1000005; std::vector<int> e[N]; int a[N], n, m, x, y, r[N], c[N], id[N], cnt, cnt2, s[N], vis[N]; void dfs(int x) { if (vis[x]) return; vis[x] = 1; for (auto u : e[x]) { dfs(u); a[x] |= a[u]; } } int main() { scanf( %d%d , &n, &m); for (int i = 1; i <= m; i++) { scanf( %d%d , &x, &y); e[x].push_back(y); r[y]++; c[x]++; } for (int i = 1; i <= n; i++) if (!c[i]) a[i] = 1 << cnt, id[i] = cnt++; for (int i = 1; i <= n; i++) if (!r[i]) { dfs(i); s[cnt2++] = a[i]; } for (int i = 1; i < (1 << cnt) - 1; i++) { int t = 0, S = 0, t2 = 0; for (int j = 0; j < cnt; j++) if (i & (1 << j)) t++, S |= s[j]; for (int j = 0; j < cnt; j++) if (S & (1 << j)) t2++; if (t2 <= t) { puts( NO ); return 0; } } puts( YES ); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long A, B, n; cin >> A >> B >> n; long long i, j; if (A == 0) { if (B == 0) { cout << 0; } else cout << No solution ; return 0; } if (B % A) { cout << No solution ; return 0; } long long rez = B / A; if (rez == 1) { cout << 1; return 0; } if (B == 0) { cout << 0; return 0; } for (long long i = -1002; i <= 1002; i++) { long long cur = i; for (j = 2; j <= n; j++) { cur *= i; if (abs(cur) > 1000000000) break; } if (cur == rez) { cout << i; return 0; } } cout << No solution ; return 0; }
#include <bits/stdc++.h> using namespace std; void geth(int in, int& out) { cout << in << endl; cin >> out; if (out != 1 && out != -1) { exit(0); } } int main() { ios_base::sync_with_stdio(false); int n, mx; cin >> mx >> n; std::vector<int> pp(n); for (auto&& x : pp) { geth(1, x); } int l = 2; int r = mx; int tr = 0; while (l < r) { int mid = (l + r) / 2, res; geth(mid, res); res *= pp[tr]; if (res > 0) { l = mid + 1; } else if (res < 0) { r = mid - 1; } tr = (tr + 1) % n; } geth(l, r); }
#include <bits/stdc++.h> using namespace std; map<string, int> sz = { { S , 0}, { M , 1}, { L , 2}, { XL , 3}, { XXL , 4}, }; string st[5] = { S , M , L , XL , XXL }; int a[5]; int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cerr.tie(nullptr); for (int i = 0; i < 5; i++) cin >> a[i]; int n; cin >> n; while (n--) { string s; cin >> s; int x = sz[s]; int bi = -1, bv = 123123123; for (int i = 0; i < 5; i++) { if (!a[i]) continue; int v = 2 * abs(i - x) + (i < x); if (v < bv) { bv = v; bi = i; } } a[bi]--; cout << st[bi] << n ; } }
#include<bits/stdc++.h> using namespace std; const int N = 3e5 + 9; const long long inf = 2e18; int a[N], c[N], n, m; long long s1[N], s2[N]; long long f(int mid) { for (int i = mid; i <= n; i++) { if (a[i] < i - mid + 1) return 0; } return 1; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { cin >> n >> m; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= m; i++) { cin >> c[i]; s2[i] = s2[i - 1] + c[i]; } sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) { s1[i] = s1[i - 1] + c[a[i]]; } int l = 1, r = n; int z = n + 1; while (l <= r) { int mid = l + r >> 1; if (f(mid)) { z = mid; r = mid - 1; } else { l = mid + 1; } } long long ans = inf; for (int i = z; i <= n + 1; i++) { ans = min(ans, s1[i - 1] + s2[n - i + 1]); } cout << ans << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n--) { unsigned long long a, b, c; cin >> a >> b >> c; unsigned long long d; d = (a * a + (b + c) * (b + c)); d = sqrt(d) - 1; cout << d << n ; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); ; unsigned long long x, y; cin >> x >> y; if (x < y) { return cout << -1, 0; } if ((x - y) & 1) { return cout << -1, 0; } long long z = (x - y) / 2; if (y & z) { cout << -1; return 0; } long long a = z; long long b = x - z; if (a > b) swap(a, b); cout << a << << b << n ; }
#include <bits/stdc++.h> using namespace std; int N, type[100005]; int Tm, in[100005], out[100005]; int par[100005][20]; bool comp[100005][20][2]; vector<int> G[100005]; void dfs(int nd) { in[nd] = ++Tm; for (int i = 0; i < G[nd].size(); i++) { int ch = G[nd][i]; if (ch == par[nd][0]) continue; comp[ch][0][type[ch]] = 1; dfs(ch); } out[nd] = ++Tm; } int LCA(int u, int v) { if ((in[u] <= in[v] && out[u] >= out[v])) return u; for (int i = 20 - 1; i >= 0; i--) if (!(in[par[u][i]] <= in[v] && out[par[u][i]] >= out[v])) u = par[u][i]; return par[u][0]; } bool get(int u, int lca, int fl) { if ((in[u] <= in[lca] && out[u] >= out[lca])) return false; bool x = true; for (int i = 20 - 1; i >= 0; i--) if (!(in[par[u][i]] <= in[lca] && out[par[u][i]] >= out[lca])) { x = x && comp[u][i][fl]; u = par[u][i]; } x = x && comp[u][0][fl]; return x; } int main() { scanf( %d , &N); for (int i = 1; i <= N; i++) { scanf( %d , &par[i][0]); scanf( %d , &type[i]); if (par[i][0] != -1) G[par[i][0]].push_back(i); } for (int i = 1; i <= N; i++) if (par[i][0] == -1) { par[i][0] = i; dfs(i); } for (int i = 1; i < 20; i++) for (int j = 1; j <= N; j++) { par[j][i] = par[par[j][i - 1]][i - 1]; for (int fl = 0; fl < 2; fl++) comp[j][i][fl] = comp[j][i - 1][fl] && comp[par[j][i - 1]][i - 1][fl]; } int Q; scanf( %d , &Q); for (int i = 0; i < Q; i++) { int tp, u, v; scanf( %d , &tp); scanf( %d , &u); scanf( %d , &v); int lca = LCA(u, v); if (v == lca) { puts( NO ); } else { if (tp == 1) { if (u == lca && get(v, lca, 0)) puts( YES ); else puts( NO ); } else { if (u == lca && get(v, lca, 1)) puts( YES ); else if (u != lca && get(u, lca, 0) && get(v, lca, 1)) puts( YES ); else puts( NO ); } } } }