func_code_string stringlengths 59 71.4k |
|---|
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); long long T, n, k; cin >> T; for (int i = 0; i < T; i++) { cin >> n >> k; cout << k * 2 << n ; } } |
#include <bits/stdc++.h> using namespace std; template <class C> void mini(C& a4, C b4) { a4 = min(a4, b4); } template <class C> void maxi(C& a4, C b4) { a4 = max(a4, b4); } template <class T1, class T2> ostream& operator<<(ostream& rout, pair<T1, T2> pair) { return rout << ( << pair.first << , << pair.second << ) ; } const int P = 10007; const int MN = 201; vector<int> T[MN][MN]; vector<int> pw; int rev[P]; const int S = 26; int pot(int x, int n) { int res = 1; while (n) { if (n & 1) res = (res * x) % P; x = (x * x) % P; n >>= 1; } return res; } int gauss(vector<vector<int> >& a, vector<int>& res) { int n = a.size(), m = a[0].size() - 1; res.resize(m); for (__typeof((res).begin()) i = ((res).begin()); i != (res).end(); ++i) *i = 0; vector<int> wyb(n, -1); for (int j = 0; j < (m); ++j) { for (int i = 0; i < (n); ++i) if (a[i][j] != 0 && wyb[i] == -1) { wyb[i] = j; if (a[i][j] < 0) a[i][j] += P; int odw = rev[a[i][j]]; for (int jj = 0; jj < (m + 1); ++jj) a[i][jj] = (a[i][jj] * odw) % P; for (int ii = 0; ii < (n); ++ii) if (i != ii && a[ii][j] != 0) { int wsp = a[ii][j]; for (int jj = 0; jj < (m + 1); ++jj) a[ii][jj] = (a[ii][jj] - wsp * a[i][jj]) % P; } break; } } int dim = m - n; for (int i = 0; i < (n); ++i) if (wyb[i] == -1) { dim++; if (a[i][m] != 0) return -1; } else res[wyb[i]] = a[i][m]; for (__typeof((res).begin()) i = ((res).begin()); i != (res).end(); ++i) if (*i < 0) *i += P; return dim; } vector<int> solve(vector<int> vals, int n) { int M = 2 * n + 1; vector<vector<int> > A(M, vector<int>(M + 1, 0)); for (int j = 0; j < (3); ++j) { A[0][j] = 1; for (int _ = 0; _ < (n); ++_) A[0][j] = (A[0][j] * (S - j)) % P; } for (int i = (1); i <= (M - 1); ++i) for (int j = 0; j < (3); ++j) A[i][j] = (A[i - 1][j] * (S - j)) % P; for (int i = 0; i < (M); ++i) { for (int j = (3); j <= (M - 1); ++j) A[i][j] = (A[i][j - 2] * (n + i)) % P; } for (int i = 0; i < (M); ++i) A[i][M] = vals[i + n]; vector<int> res(M); int dim = gauss(A, res); assert(dim != -1); return res; } int main() { ios_base::sync_with_stdio(false); cout << fixed << setprecision(10); for (int i = 0; i < (P); ++i) rev[i] = pot(i, P - 2); string s; int L; cin >> s >> L; int n = s.length(); L += n; bool odd = (L % 2 == 1); L = (L + 1) / 2; int mx = 3 * n + 1; pw = vector<int>(mx); pw[0] = 1; for (int j = (1); j <= (mx - 1); ++j) pw[j] = (pw[j - 1] * S) % P; for (int j = 0; j < (n + 1); ++j) for (int i = (j); i >= (0); --i) { T[i][j] = vector<int>(mx); for (int k = 0; k < (mx); ++k) { if (j == i) T[i][j][k] = pw[k]; else if (j == i + 1) { if (k > 0) T[i][j][k] = (T[i][j][k - 1] * (S - 1) + pw[k - 1]) % P; } else if (j == i + 2 && s[i] == s[j - 1] && k == 1 && odd) T[i][j][k] = 0; else if (k > 0) { if (s[i] == s[j - 1]) T[i][j][k] = (T[i][j][k - 1] * (S - 1) + T[i + 1][j - 1][k - 1]) % P; else T[i][j][k] = (T[i][j][k - 1] * (S - 2) + T[i + 1][j][k - 1] + T[i][j - 1][k - 1]) % P; } } } if (L < n) { cout << T[0][n][L] << endl; return 0; } vector<int> coef = solve(T[0][n], n); int M = 2 * n + 1; vector<int> val(M); for (int i = 0; i < (3); ++i) val[i] = pot(S - i, L); for (int i = (3); i <= (M - 1); ++i) val[i] = ((long long)val[i - 2] * L) % P; long long res = 0; for (int i = 0; i < (M); ++i) res = (res + coef[i] * val[i]) % P; cout << res << endl; return 0; } |
#include bits/stdc++.h #include <cstring> using namespace std; typedef long long ll; #define FastIO cin.sync_with_stdio(false), cin.tie(nullptr) #define pb push_back #define fs first #define sc second #define mkp make_pair #define endl n // IO #define multitest signed __T; cin >> __T; while(__T--) template<typename T> void _read(T& t); template<typename T> void _read(vector<T>& v); template<typename T1, typename T2> void _read(pair<T1, T2>& p); template<typename T> void _read(T& t) { cin >> t; } template<typename T> void _read(vector<T>& v) { for (unsigned _i = 0; _i < v.size(); _i++)_read(v[_i]); } template<typename T1, typename T2> void _read(pair<T1, T2>& p) { _read(p.first); _read(p.second); } void _masterread() {} template<typename T, typename... V> void _masterread(T& t, V& ... v) { _read(t); _masterread(v...); } #define re(...)_masterread(__VA_ARGS__) template<typename T> void _print(T t); template<typename T> void _print(vector<T>& v); template<typename T1, typename T2> void _print(pair<T1, T2>& p); template<typename T> void _print(T t) { cout << t; } template<typename T> void _print(vector<T>& v) { for (unsigned _i = 0; _i < v.size(); _i++)_print(v[_i]), cout << (_i == v.size() - 1 ? : ); } template<typename T1, typename T2> void _print(pair<T1, T2>& p) { _print(p.first); cout << ; _print(p.second); } void _masterprint() { cout << endl; } template<typename T, typename... V> void _masterprint(T t, V... v) { _print(t); if (sizeof...(v))cout << ; _masterprint(v...); } #define pr(...)_masterprint(__VA_ARGS__) // DEBUG // colored output??? template <typename T> // start forward declaration void _debug(T t); template<typename T1, typename T2> void _debug(pair<T1, T2> p); template<typename T> void _debug(vector<T>v); template <typename T> // end forward declaration void _debug(T t) { cerr << t; } template<typename T1, typename T2> void _debug(pair<T1, T2> p) { cerr << { ; _debug(p.first); cerr << , ; _debug(p.second); cerr << } ; } template<typename T> void _debug(vector<T>v) { cerr << ( ; for (unsigned _i = 0; _i < v.size(); _i++)_debug(v[_i]), cerr << (_i == v.size() - 1 ? : , ); cerr << ) ; } void _masterdebug() { cerr << ] n ; } template<typename T, typename... V> void _masterdebug(T t, V... v) { _debug(t); if (sizeof...(v))cerr << , ; _masterdebug(v...); } #define debug(...) cerr<< [ <<#__VA_ARGS__<< ] = [ ;_masterdebug(__VA_ARGS__) #define int long long vector<int> A, D; void rec(int l, int r, int d) { if (l > r) { return; } if (l == r) { D[l] = d; return; } int mx = l; for (int i = l; i <= r; i++) { if (A[i] > A[mx]) { mx = i; } } D[mx] = d; rec(l, mx - 1, d + 1); rec(mx + 1, r, d + 1); } void solve() { int n; re(n); A = vector<int>(n); re(A); D = vector<int>(n); rec(0, n - 1, 0); pr(D); } signed main() { FastIO; int t; cin >> t; while(t--) solve(); } |
#include <bits/stdc++.h> using std::cin; using std::cout; using std::endl; using std::set; using std::swap; using std::vector; void TaskA() { vector<char> first; vector<char> second; char a; int f, s; for (int i = 0; i < 4; i++) { cin >> a; first.push_back(a); if (a == A ) f = i; } swap(first[2], first[3]); if (f == 2) f = 3; else if (f == 3) f = 2; for (int i = 0; i < 4; i++) { cin >> a; second.push_back(a); if (a == A ) s = i; } swap(second[2], second[3]); if (s == 2) s = 3; else if (s == 3) s = 2; if (first[(f + 1) % 4] == X ) f = (f + 1) % 4; if (second[(s + 1) % 4] == X ) s = (s + 1) % 4; if (first[(f + 1) % 4] == second[(s + 1) % 4]) cout << YES ; else cout << NO ; } int main() { TaskA(); return 0; } |
#include <bits/stdc++.h> template <typename T> void MACRO_VAR_Scan(T& t) { std::cin >> t; } template <typename First, typename... Rest> void MACRO_VAR_Scan(First& first, Rest&... rest) { std::cin >> first; MACRO_VAR_Scan(rest...); } template <typename T> void MACRO_VEC_ROW_Init(int n, T& t) { t.resize(n); } template <typename First, typename... Rest> void MACRO_VEC_ROW_Init(int n, First& first, Rest&... rest) { first.resize(n); MACRO_VEC_ROW_Init(n, rest...); } template <typename T> void MACRO_VEC_ROW_Scan(int p, T& t) { std::cin >> t[p]; } template <typename First, typename... Rest> void MACRO_VEC_ROW_Scan(int p, First& first, Rest&... rest) { std::cin >> first[p]; MACRO_VEC_ROW_Scan(p, rest...); } template <typename T> inline T CHMAX(T& a, const T b) { return a = (a < b) ? b : a; } template <typename T> inline T CHMIN(T& a, const T b) { return a = (a > b) ? b : a; } void CHECKTIME(std::function<void()> f) { auto start = std::chrono::system_clock::now(); f(); auto end = std::chrono::system_clock::now(); auto res = std::chrono::duration_cast<std::chrono::nanoseconds>((end - start)) .count(); std::cerr << [Time: << res << ns ( << res / (1.0e9) << s)] n ; } using ll = long long; using ull = unsigned long long; constexpr ll INFINT = 1 << 30; constexpr ll INFINT_LIM = (1LL << 31) - 1; constexpr ll INFLL = 1LL << 60; constexpr ll INFLL_LIM = (1LL << 62) - 1 + (1LL << 62); constexpr double EPS = 1e-7; constexpr ll MOD = 1000000007; constexpr double PI = 3.141592653589793238462643383279; ll dp[16][2][16]; ll dp2[16 * 16 * 16 * 16][16]; ll decode(char c) { if (( 0 <= c && c < 9 + 1)) { return c - 0 ; } return c - a + 10; } std::string decl(std::string s) { if (s == 0 ) return s; ll p = (ll)s.size() - 1; while (true) { if (s[p] == 0 ) { s[p] = f ; --p; continue; } if (s[p] == a ) s[p] = 9 ; else --s[p]; break; } return s; } ll calc(std::string s) { if (s.size() <= 4) { ll t = std::stoi(s, nullptr, 16); return dp2[t][0]; } ll res = 0; ll t = std::stoi(s.substr(s.size() - 4), 0, 16); ll max = 0; for (ll i = 0; i < ll(s.size() - 4); ++i) { CHMAX(max, decode(s[i])); } res += dp2[t][max]; for (ll i = 0; i < ll(16); ++i) for (ll j = 0; j < ll(2); ++j) for (ll k = 0; k < ll(16); ++k) dp[i][j][k] = 0; dp[0][0][0] = 1; for (ll i = 0; i < ll(s.size() - 4); ++i) for (ll j = 0; j < ll(2); ++j) for (ll k = 0; k < ll(16); ++k) { ll sdigit = decode(s[i]); ll lim = j ? 16 : sdigit + 1; for (ll d = 0; d < ll(lim); ++d) { dp[i + 1][j || d < sdigit][std::max(k, d)] += dp[i][j][k]; } } for (ll i = 0; i < ll(16); ++i) { res += dp[s.size() - 4][1][i] * dp2[16 * 16 * 16 * 16 - 1][i]; } return res; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(0); ; ll Q; MACRO_VAR_Scan(Q); ; for (ll i1 = 0; i1 < ll(16); ++i1) for (ll i2 = 0; i2 < ll(16); ++i2) for (ll i3 = 0; i3 < ll(16); ++i3) for (ll i4 = 0; i4 < ll(16); ++i4) for (ll j = 0; j < ll(16); ++j) { ll i = i1; i *= 16; i += i2; i *= 16; i += i3; i *= 16; i += i4; if (i == 0) continue; ll max = std::max({i1, i2, i3, i4, j}); dp2[i][j] = dp2[i - 1][j]; dp2[i][j] += (i >> max) & 1; } for (ll q = 0; q < ll(Q); ++q) { std::string sl, sr; MACRO_VAR_Scan(sl, sr); ; ll l = calc(decl(sl)); ll r = calc(sr); std::cout << r - l; std::cout << n ; ; } return 0; } |
#include<bits/stdc++.h> typedef long long int ll; #define io ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL); #define fr(i,l,r) for(ll i=l;i<r;i++) #define lop ll t;cin>>t;while(t--) #define maxi 1000 #define mod 1000000007 #define f first #define s second ll dp[maxi]; using namespace std; /* a1 + a2 +... +an = k*m long n,k; cin>>n>>k;if(k<n){ k=n/k*k+(n%k!=0)*k; } cout<<k/n+(k%n!=0)<<endl; */ void solve(){ ll n , k ; cin >> n >> k ; if(k < n) k = k*(n/k) + (n%k != 0)*k ; cout << k/n + (k%n != 0) << endl ; } int main(){ io; int t ; cin >> t ; while(t--){ solve() ; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, k, sum = 0; cin >> n >> k; pair<int, int> p[n]; for (int i = 0; i < n; i++) { cin >> p[i].first; p[i].second = i + 1; } sort(p, p + n); for (int i = 0; i < n; i++) { k -= p[sum].first; if (k >= 0) sum++; else break; } cout << sum << endl; for (int i = 0; i < sum; i++) { cout << p[i].second << ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int MAX_N = 59; struct State { char c[MAX_N][MAX_N]; }; int H, W; vector<pair<int, int>> solve(State S) { vector<pair<int, int>> D; for (int t = 1; t <= 100000; t++) { bool flag = false; if (H % 2 == 0) { for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { if (i % 2 == 1 && S.c[i][j] != U ) flag = true; if (i % 2 == 0 && S.c[i][j] != D ) flag = true; } } } else { for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) { if (j % 2 == 1 && S.c[i][j] != L ) flag = true; if (j % 2 == 0 && S.c[i][j] != R ) flag = true; } } } if (flag == false) return D; for (int i = 1; i <= H - 1; i++) { for (int j = 1; j <= W - 1; j++) { if (t % 2 == 1) { if (S.c[i][j] == U && S.c[i][j + 1] == U && S.c[i + 1][j] == D && S.c[i + 1][j + 1] == D ) { S.c[i + 0][j + 0] = L ; S.c[i + 0][j + 1] = R ; S.c[i + 1][j + 0] = L ; S.c[i + 1][j + 1] = R ; D.push_back(make_pair(i, j)); } } if (t % 2 == 0) { if (S.c[i][j] == L && S.c[i][j + 1] == R && S.c[i + 1][j] == L && S.c[i + 1][j + 1] == R ) { S.c[i + 0][j + 0] = U ; S.c[i + 0][j + 1] = U ; S.c[i + 1][j + 0] = D ; S.c[i + 1][j + 1] = D ; D.push_back(make_pair(i, j)); } } } } } return D; } int main() { cin >> H >> W; State T, U; for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) cin >> T.c[i][j]; } vector<pair<int, int>> D1 = solve(T); for (int i = 1; i <= H; i++) { for (int j = 1; j <= W; j++) cin >> U.c[i][j]; } vector<pair<int, int>> D2 = solve(U); vector<pair<int, int>> D3 = D1; for (int i = D2.size() - 1; i >= 0; i--) D3.push_back(D2[i]); cout << D3.size() << endl; for (int i = 0; i < D3.size(); i++) cout << D3[i].first << << D3[i].second << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; const long long inf = (long long)2 * 1e9; const long long MOD = (long long)1e9 + 7; const long long P = 239; const long long MAX_N = 100000; const long long ABC = 26; const long double pi = 3.1415926535897932384626; const long double eps = 1e-6; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.precision(20); long long c, h1, h2, w1, w2; cin >> c >> h1 >> h2 >> w1 >> w2; if (w2 > w1) { swap(h1, h2); swap(w1, w2); } if (w1 * w1 >= c) { long long ans = 0; for (long long i = 0; i * w1 <= c; i++) ans = max(ans, i * h1 + (c - i * w1) / w2 * h2); cout << ans; } else { if (h1 * w2 < h2 * w1) { swap(h1, h2); swap(w1, w2); } long long ans = 0; for (long long i = 0; i < w1 && i * w2 <= c; i++) ans = max(ans, i * h2 + (c - i * w2) / w1 * h1); cout << ans; } return 0; } |
#include <bits/stdc++.h> using namespace std; const long long maxn = 1000006, maxm = 1005; const long long mod = 1000000007; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long powermod(long long a, long long b) { a = a; long long ans = 1; while (b) { if (b % 2) ans = (ans * a); b = b / 2; a = (a * a); } return ans; } long long a[100005]; long long flag[100005]; int main() { long long t; scanf( %lld , &t); while (t--) { long long n, m, i, j, k; scanf( %lld%lld , &n, &k); long long vis1 = 0, vis2 = 0; for (i = 1; i <= n; i++) { scanf( %lld , &a[i]); if (a[i] == k) vis1 = 1; if (i >= 2) { if (a[i] >= k && (a[i - 1] >= k || a[i - 2] >= k)) vis2 = 1; } } if ((vis1 == 1 && vis2 == 1) || (n == 1 && vis1 == 1)) printf( yes n ); else printf( no n ); } } |
#include <bits/stdc++.h> using namespace std; vector<vector<int> > e; vector<long long> val; vector<long long> mp, mn; vector<int> deg; vector<bool> vis; pair<long long, long long> dfs(int x) { vis[x] = true; long long mx = 0, mi = 0; for (int to : e[x]) { if (!vis[to]) { auto p = dfs(to); mx = max(mx, p.first); mi = max(mi, p.second); } } long long d = val[x] + mx - mi; mp[x] = mx; mn[x] = mi; if (d > 0) mi += d; else mx -= d; return make_pair(mx, mi); } int main(int argc, char **argv) { int n; scanf( %d , &n); deg = vector<int>(n); mp = mn = val = vector<long long>(n); vis = vector<bool>(n); e = vector<vector<int> >(n); for (int i = 0; i < n - 1; i++) { int a, b; scanf( %d %d , &a, &b); e[a - 1].push_back(b - 1); e[b - 1].push_back(a - 1); deg[a - 1]++; deg[b - 1]++; } for (int i = 0; i < n; i++) { int t; scanf( %d , &t); val[i] = t; } auto p = dfs(0); cout << p.first + p.second << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; long long N, x, y, awal, centre, nb, a; bool visited[100005]; vector<long long> adj[100005]; vector<int> ans; void dfs(int x) { int s = adj[x].size(); visited[x] = 1; bool cek = 1; for (int i = 0; i < s; i++) { nb = adj[x][i]; if (!visited[nb]) { cek = 0; dfs(nb); } } if (cek) ans.push_back(x); } int main() { cin >> N; for (int i = 1; i < N; i++) { cin >> x >> y; adj[x].push_back(y); adj[y].push_back(x); } for (int i = 1; i <= N; i++) { if (adj[i].size() >= 3) { awal = i; centre++; } } if (centre > 1) cout << No n ; else { if (centre == 0) { dfs(1); cout << Yes n << 1 << n ; for (int i = 1; i <= N; i++) { if (adj[i].size() == 1) { cout << i; a++; if (a == 1) cout << ; } } cout << n ; } if (centre == 1) { dfs(awal); cout << Yes n << ans.size() << n ; for (int i = 0; i < ans.size(); i++) { cout << awal << << ans[i] << n ; } } } } |
#include <bits/stdc++.h> using namespace std; const int N = 100100; const long long INF = 1000 * 1000 * 1000LL * 1000 * 1000; vector<pair<int, int> > adj1[N]; vector<pair<long long, pair<int, int> > > e; set<pair<long long, int> > s; long long d[N]; int p[N], par[N], sz[N], flag(1); void Dijkstra() { while (s.size()) { auto u = *s.begin(); s.erase(u); for (auto v : adj1[u.second]) { long long w = u.first + v.second; if (w < d[v.first]) { s.erase({d[v.first], v.first}); d[v.first] = w; p[v.first] = p[u.second]; s.insert({d[v.first], v.first}); } } } return; } int findpar(int v) { if (v == par[v]) return v; return (par[v] = findpar(par[v])); } bool merge(int u, int v) { u = findpar(u), v = findpar(v); if (u == v) return 0; if (sz[u] < sz[v]) swap(u, v); sz[u] += sz[v]; par[v] = u; return 1; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, m, k, u, v, w; cin >> n >> m; for (int i = 0; i < m; i++) { cin >> u >> v >> w; adj1[--u].push_back({--v, w}); adj1[v].push_back({u, w}); } fill(d, d + n, INF); cin >> k; for (int i = 0; i < k; i++) { cin >> u; d[--u] = 0; if (!u) flag = 0; } for (int i = 0; i < n; i++) { p[i] = i; par[i] = i, sz[i] = 1; s.insert({d[i], i}); } Dijkstra(); for (int i = 0; i < n; i++) for (auto u : adj1[i]) if (p[i] < p[u.first]) { long long w = d[i] + d[u.first] + u.second; e.push_back({w, {p[i], p[u.first]}}); } int cmp = k; long long ans(0), ans2(0); sort(e.begin(), e.end()); for (int i = 0; i < e.size() && cmp > 1; i++) { if (merge(e[i].second.first, e[i].second.second)) { cmp--; ans += e[i].first; } } if (flag) ans += d[0]; cout << ((cmp == 1) ? ans : -1) << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; map<int, int> mp; vector<int> l(n), a(n); for (int i = 0; i < n; i++) cin >> l[i]; vector<int> r(n); for (int i = 0; i < n; i++) { cin >> r[i]; a[i] = l[i] + r[i]; mp[a[i]]++; } int curr = 1; map<int, int> mp2; for (auto it = mp.rbegin(); it != mp.rend(); it++) { mp2[it->first] = curr++; } for (int i = 0; i < n; i++) { a[i] = mp2[a[i]]; } for (int i = 0; i < n; i++) { int cnt1 = 0, cnt2 = 0; for (int j = 0; j < n; j++) { if (j == i) continue; if (a[j] > a[i]) { if (j > i) cnt2++; else cnt1++; } } if (cnt1 != l[i] || cnt2 != r[i]) { cout << NO n ; return 0; } } cout << YES n ; for (auto it : a) cout << it << ; cout << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; void solve(); int main(int argc, char const *argv[]) { solve(); } void solve() { map<int, int> arr; cin >> arr[0] >> arr[1] >> arr[2] >> arr[3]; int total = arr[0] + arr[1] + arr[2] + arr[3]; for (int st = 0; st < 4; st++) if (arr[st]) { vector<int> res; auto t = arr; t[st]--; res.push_back(st); int last = st; for (int i = 0; i < total - 1; i++) { if (t[last - 1]) { t[last - 1]--; res.push_back(last - 1); last--; } else if (t[last + 1]) { t[last + 1]--; res.push_back(last + 1); last++; } else break; } if ((int)res.size() == total) { cout << YES n ; for (int i : res) printf( %d , i); cout << endl; return; } } cout << NO n ; return; } |
#include <bits/stdc++.h> using namespace std; enum { EMPTY, LINE, INF }; int Type(int a, int b, int c) { if (a == 0 && b == 0) if (c == 0) return INF; else return EMPTY; return LINE; } void Exit(int ans) { printf( %d n , ans); exit(0); } int main() { int a1, b1, c1; int a2, b2, c2; scanf( %d%d%d , &a1, &b1, &c1); scanf( %d%d%d , &a2, &b2, &c2); int t1 = Type(a1, b1, c1); int t2 = Type(a2, b2, c2); if (t1 == EMPTY || t2 == EMPTY) Exit(0); if (t1 == INF || t2 == INF) Exit(-1); if (a1 * b2 - a2 * b1 != 0) Exit(1); if (a1 * c2 - a2 * c1 == 0 && b1 * c2 - b2 * c1 == 0) Exit(-1); Exit(0); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); long long i, j, x, n, m, l, r; cin >> n >> m; vector<long long> v; for (i = 0; i < n; i++) { cin >> x; v.push_back(x); } for (i = 0; i < m; i++) { cin >> l >> r >> x; long long c = 0; if (x < l || x > r) { cout << Yes n ; continue; } for (j = l - 1; j < r; j++) { if (v[j] < v[x - 1]) { c++; } } if (c == (x - l)) { cout << Yes n ; } else { cout << No n ; } } } |
#include <bits/stdc++.h> int main() { int n, a, b; while (scanf( %d%d%d , &n, &a, &b) != EOF) { int c[202] = {0}; int t = 1, i, j, k; if (b >= 0) { for (int i = 1; i <= n; i++) c[i] = i; for (int j = n + 1; j <= 2 * n; j++) c[j] = c[j - n]; printf( %d n , c[a + (b % n)]); } if (b < 0) { for (i = 1; i <= n; i++) c[i] = i; for (j = n + 1; j <= 2 * n; j++) c[j] = c[j - n]; printf( %d n , c[a + n - (-b) % n]); } } return 0; } |
#include <bits/stdc++.h> using namespace std; map<pair<long long, long long>, long long> cost; map<long long, int> vis; map<long long, int> freq; int main() { int q; cin >> q; while (q--) { int e; long long v, u; cin >> e >> v >> u; long long tv = v; long long tu = u; if (e == 1) { long long w; cin >> w; if (u == v) continue; while (tv > 0) { freq[tv]++; tv /= 2; } while (tu > 0) { ++freq[tu]; tu /= 2; } tv = v; tu = u; while (freq[tv] == 1 && tv > 1) { cost[make_pair(tv, tv / 2)] += w; tv /= 2; } while (freq[tu] == 1 && tu > 1) { cost[make_pair(tu, tu / 2)] += w; tu /= 2; } } else { long long ans = 0; while (tv > 0) { freq[tv]++; tv /= 2; } while (tu > 0) { ++freq[tu]; tu /= 2; } tv = v; tu = u; while (freq[tv] == 1 && tv > 1) { ans += cost[make_pair(tv, tv / 2)]; tv /= 2; } while (freq[tu] == 1 && tu > 1) { ans += cost[make_pair(tu, tu / 2)]; tu /= 2; } cout << ans << endl; } freq.clear(); } } |
#include <iostream> #include <vector> using namespace std; int l[200005], r[200005]; vector<int> e[200005]; long long int dp[2][200005]; void dfs(int u, int fa) { dp[0][u] = dp[1][u] = 0; for (auto v : e[u]) { if (v == fa) continue; dfs(v, u); dp[0][u] += max(dp[0][v] + abs(l[u] - l[v]), dp[1][v] + abs(l[u] - r[v])); dp[1][u] += max(dp[0][v] + abs(r[u] - l[v]), dp[1][v] + abs(r[u] - r[v])); } } int main(void) { int t; scanf( %d , &t); while (t--) { int n; scanf( %d , &n); for (int i = 1;i <= n;i++) scanf( %d %d , &l[i], &r[i]); for (int i = 1;i <= n;i++) e[i].clear(); for (int i = 0;i < n - 1;i++) { int u, v; scanf( %d %d , &u, &v); e[u].emplace_back(v); e[v].emplace_back(u); } dfs(1, -1); printf( %lld n , max(dp[0][1], dp[1][1])); } } |
#include <bits/stdc++.h> using namespace std; class Solution { public: void solve(std::istream& in, std::ostream& out) { int w, h; in >> w >> h; vector<string> m(h); for (string& s : m) in >> s; for (int i = 0; i < 2 * w; ++i) { for (int j = 0; j < 2 * h; ++j) out << m[j / 2][i / 2]; out << n ; } } }; void solve(std::istream& in, std::ostream& out) { out << std::setprecision(12); Solution solution; solution.solve(in, out); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); istream& in = cin; ostream& out = cout; solve(in, out); return 0; } |
#include <bits/stdc++.h> using namespace std; long double eps = 1e-9, pi = acos(-1.0); struct vec { long double i, x, y; } vectors[100005]; long double cala(const vec& v) { long double ang = acos(v.x / sqrt(v.x * v.x + v.y * v.y)); ang = v.y > -eps ? ang : 2 * pi - ang; return ang; } bool cmp(const vec& v1, const vec& v2) { return cala(v1) < cala(v2); } int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> vectors[i].x >> vectors[i].y, vectors[i].i = i; sort(vectors, vectors + n, cmp); int minp = n - 1; long double minv = abs((long double)2 * pi - (cala(vectors[n - 1]) - cala(vectors[0]))); for (int i = 0; i < n - 1; i++) { if (abs(cala(vectors[i + 1]) - cala(vectors[i])) < minv) minp = i, minv = abs(cala(vectors[i + 1]) - cala(vectors[i])); } if (minp == n - 1) cout << vectors[minp].i + 1 << << vectors[0].i + 1 << endl; else cout << vectors[minp].i + 1 << << vectors[minp + 1].i + 1 << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; if (n <= 30) cout << NO n ; else { cout << YES n ; if (n == 36 || n == 40 || n == 44) cout << 6 10 15 << n - 31 << n ; else cout << 6 10 14 << n - 30 << n ; } } return 0; } |
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; vector<int> p; long long dp1[555555]; long long dp2[555555]; long long fp(long long x) { long long ret = 1; long long mul = 2; while (x) { if (x & 1) { ret *= mul; ret %= mod; } mul *= mul; mul %= mod; x >>= 1; } return ret; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); p.push_back(2); p.push_back(3); p.push_back(5); p.push_back(7); p.push_back(11); p.push_back(13); p.push_back(17); p.push_back(19); p.push_back(23); p.push_back(29); p.push_back(31); p.push_back(37); p.push_back(41); p.push_back(43); p.push_back(47); p.push_back(53); p.push_back(59); p.push_back(61); p.push_back(67); int k = p.size(); int cnt[80] = {}; int n; cin >> n; for (int i = 0; i < n; i++) { int add; cin >> add; cnt[add]++; } dp1[0] = 1; for (int i = 1; i < 70 + 1; i++) { if (!cnt[i]) continue; int shift = 0; int rem = i; for (int j = 0; j < k; j++) { while (rem % p[j] == 0) { shift ^= (1 << j); rem /= p[j]; } } memset(dp2, 0, sizeof(dp2)); ; long long add = fp(cnt[i] - 1); for (int j = 0; j < 1 << k; j++) { dp2[j] += dp1[j] * add % mod; dp2[j] %= mod; dp2[j ^ shift] += dp1[j] * add % mod; dp2[j ^ shift] %= mod; } for (int j = 0; j < 1 << k; j++) { dp1[j] = dp2[j]; } } cout << (dp1[0] + mod - 1) % mod << endl; } |
#include <bits/stdc++.h> #pragma comment(linker, /STACK:1024000000,1024000000 ) using namespace std; int sum[4000005]; int cover[4000005]; int n, m; void up(int rt) { if (cover[rt << 1] == cover[rt << 1 | 1] && cover[rt << 1] != -1 && cover[rt << 1]) cover[rt] = cover[rt << 1]; else cover[rt] = -1; } void down(int rt) { if (cover[rt] == 1) { cover[rt << 1] = cover[rt << 1 | 1] = cover[rt]; if (sum[rt << 1] == 0) sum[rt << 1] = sum[rt]; if (sum[rt << 1 | 1] == 0) sum[rt << 1 | 1] = sum[rt]; } } void build(int l, int r, int rt) { cover[rt] = 0; if (l == r) { cover[rt] = 0; sum[rt] = 0; return; } int m = (l + r) >> 1; build(l, m, rt << 1); build(m + 1, r, rt << 1 | 1); up(rt); } void update(int L, int R, int d, int l, int r, int rt) { if (L <= l && R >= r) { if (cover[rt] == 1) return; if (cover[rt] == 0) { cover[rt] = 1; sum[rt] = d; down(rt); return; } } int m = (l + r) >> 1; if (L <= m) update(L, R, d, l, m, rt << 1); if (R > m) update(L, R, d, m + 1, r, rt << 1 | 1); up(rt); } int query(int p, int l, int r, int rt) { if (l == r) return sum[rt]; int m = (l + r) >> 1; int res = 0; if (p <= m) res += query(p, l, m, rt << 1); if (p > m) res += query(p, m + 1, r, rt << 1 | 1); return res; } int main() { while (~scanf( %d%d , &n, &m)) { build(1, n, 1); for (int i = 0; i < m; i++) { int a, b, w; scanf( %d%d%d , &a, &b, &w); if (w - 1 >= a) update(a, w - 1, w, 1, n, 1); if (w + 1 <= b) update(w + 1, b, w, 1, n, 1); } for (int i = 1; i <= n; i++) printf(i == n ? %d n : %d , query(i, 1, n, 1)); } return 0; } |
#include <bits/stdc++.h> using namespace std; string arr[12] = { C , C# , D , D# , E , F , F# , G , G# , A , B , H }, a, b, c; int ok(int a1, int a2, int a3) { int dis1, dis2; if (a2 > a1) dis1 = a2 - a1; else dis1 = 12 - a1 + a2; if (a3 > a2) dis2 = a3 - a2; else dis2 = 12 - a2 + a3; if (dis1 == 4 && dis2 == 3) return 1; if (dis1 == 3 && dis2 == 4) return 0; return 2; } int main() { cin >> a >> b >> c; int ind1, ind2, ind3; for (int i = 0; i < 12; i++) { if (arr[i] == a) ind1 = i; if (arr[i] == b) ind2 = i; if (arr[i] == c) ind3 = i; } if (ok(ind1, ind2, ind3) == 1 || ok(ind1, ind3, ind2) == 1 || ok(ind2, ind1, ind3) == 1 || ok(ind2, ind3, ind1) == 1 || ok(ind3, ind1, ind2) == 1 || ok(ind3, ind2, ind1) == 1) cout << major << endl; else { if (ok(ind1, ind2, ind3) == 0 || ok(ind1, ind3, ind2) == 0 || ok(ind2, ind1, ind3) == 0 || ok(ind2, ind3, ind1) == 0 || ok(ind3, ind1, ind2) == 0 || ok(ind3, ind2, ind1) == 0) cout << minor << endl; else cout << strange << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; template <typename T> ostream& operator<<(ostream& out, const vector<T>& v) { out << [ ; for (auto vi : v) out << vi << , ; out << ] n ; return out; } const int MAX_N = 1024; const int MOD = 1e9 + 7; struct RSQ { static const int len = 1024; vector<double> a = vector<double>(len * 2 + 1, 0.0); void add(int i, double value, int node = 1, int l = 0, int r = len - 1) { if (l <= i && i <= r) { a[node] += value; if (l < r) { int m = (l + r) / 2; add(i, value, node * 2, l, m); add(i, value, node * 2 + 1, m + 1, r); } } } double sum(int ql, int qr, int node = 1, int l = 0, int r = len - 1) { if (ql <= l && r <= qr) { return a[node]; } else if (qr < l || r < ql) { return 0; } else { int m = (l + r) / 2; return sum(ql, qr, node * 2, l, m) + sum(ql, qr, node * 2 + 1, m + 1, r); } } }; int n; vector<vector<int> > a(MAX_N, vector<int>(MAX_N)); vector<vector<vector<int> > > _len2zero( MAX_N, vector<vector<int> >(MAX_N, vector<int>(8, -1))); const int DX[] = {-1, -1, 0, +1, +1, +1, 0, -1}; const int DY[] = {0, +1, +1, +1, 0, -1, -1, -1}; int len2zero(int x, int y, int dir) { if (!(0 <= x && x < n && 0 <= y && y < n) || a[x][y] == 0) { return 0; } int& res = _len2zero[x][y][dir]; if (res == -1) { res = 1 + len2zero(x + DX[dir], y + DY[dir], dir); } return res; } RSQ sumLine[MAX_N * 2], sumColumn[MAX_N * 2], sumMainD[MAX_N * 2], sumMinorD[MAX_N * 2]; long long solution() { vector<double> ln({-1, log(1.0), log(2.0), log(3.0)}); for (int x = 0; x < n; x++) { for (int y = 0; y < n; y++) { sumLine[x].add(y, ln[a[x][y]]); sumColumn[y].add(x, ln[a[x][y]]); sumMainD[n + x - y].add(x, ln[a[x][y]]); sumMinorD[x + y].add(x, ln[a[x][y]]); } } tuple<double, int, int, int, bool> best; for (int x = 0; x < n; x++) { for (int y = 0; y < n; y++) { int d = 100500; for (int dir : {0, 2, 4, 6}) { d = min(d, len2zero(x, y, dir)); } if (d) d--; double logSum = sumLine[x].sum(y - d, y + d) + sumColumn[y].sum(x - d, x + d) - ln[a[x][y]]; if (logSum > get<0>(best)) { best = make_tuple(logSum, x, y, d, true); } d = 100500; for (int dir : {1, 3, 5, 7}) { d = min(d, len2zero(x, y, dir)); } if (d) d--; logSum = sumMainD[n + x - y].sum(x - d, x + d) + sumMinorD[x + y].sum(x - d, x + d) - ln[a[x][y]]; if (logSum > get<0>(best)) { best = make_tuple(logSum, x, y, d, false); } } } double temp; int x, y, d; bool isNormal; tie(temp, x, y, d, isNormal) = best; long long result = a[x][y]; if (isNormal) { for (int dd = 1; dd <= d; dd++) { for (int dir : {0, 2, 4, 6}) { result *= a[x + DX[dir] * dd][y + DY[dir] * dd]; result %= MOD; } } } else { for (int dd = 1; dd <= d; dd++) { for (int dir : {1, 3, 5, 7}) { result *= a[x + DX[dir] * dd][y + DY[dir] * dd]; result %= MOD; } } } return result; } void readData() { cin >> n; for (int i = 0; i < n; i++) { string s; cin >> s; assert(s.size() == n); for (int j = 0; j < (int)s.size(); j++) { a[i][j] = s[j] - 0 ; } } } int main() { ios::sync_with_stdio(false); readData(); cout << solution() << n ; return 0; } |
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); class domino { public: int data[4]; bool IsEqual(domino b) { for (long long move = 0; move <= 3; move++) { bool f = true; for (long long i = 0; i <= 3; i++) if (data[i] != b.data[(i + move) % 4]) f = false; if (f) return true; } return false; } }; vector<domino> a; int main() { int n; cin >> n; for (long long i = 1; i <= n; i++) { domino temp; string s; cin >> s; temp.data[0] = s[0] - 0 ; temp.data[1] = s[1] - 0 ; cin >> s; temp.data[3] = s[0] - 0 ; temp.data[2] = s[1] - 0 ; bool f = true; if (a.size() == 0) a.push_back(temp); for (long long k = 0; k <= a.size() - 1; k++) if (temp.IsEqual(a[k])) f = false; if (f) a.push_back(temp); if (i != n) cin >> s; } cout << a.size(); } |
#include <bits/stdc++.h> using namespace std; const int dx[] = {-1, -2, -2, -1, 1, 2, 2, 1}; const int dy[] = {-2, -1, 1, 2, 2, 1, -1, -2}; set<pair<int, pair<int, int> > > heap; vector<vector<int> > now; int main() { int n, m; cin >> n >> m; if (n >= 10 && m >= 10) { cout << (m * n + 1) / 2 << endl; return 0; } now.assign(n, vector<int>(m, 0)); for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) { int cnt = 1; for (int k = 0; k < 8; k++) { int nx = i + dx[k]; int ny = j + dy[k]; if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue; cnt++; } heap.insert(make_pair(cnt, make_pair(i, j))); now[i][j] = cnt; } int ans = 0; while (!heap.empty()) { pair<int, pair<int, int> > v = *heap.begin(); heap.erase(heap.begin()); ans++; int x = v.second.first; int y = v.second.second; for (int i = 0; i < 8; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (nx < 0 || nx >= n || ny < 0 || ny >= m) continue; int cnt = now[nx][ny]; if (heap.count(make_pair(cnt, make_pair(nx, ny)))) { heap.erase(make_pair(cnt, make_pair(nx, ny))); for (int j = 0; j < 8; j++) { int x = nx + dx[j]; int y = ny + dy[j]; if (x < 0 || x >= n || y < 0 || y >= m) continue; int cnt = now[x][y]; if (heap.count(make_pair(cnt, make_pair(x, y)))) { heap.erase(make_pair(cnt, make_pair(x, y))); now[x][y]--; cnt--; heap.insert(make_pair(cnt, make_pair(x, y))); } } } } } cout << max(ans, (m * n + 1) / 2) << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; struct node { int x, y; node(int a, int b) { x = a, y = b; } }; int d1[1010][1010]; int main() { int n, t; queue<node> Q; for (int i = 0; i <= 1000; i++) { for (int j = 0; j <= 1000; j++) { d1[i][j] = 5000; } } int a, b, c, d; cin >> n >> a >> b >> c >> d; Q.push(node(a, b)); d1[a][b] = 0; while (!Q.empty()) { node v = Q.front(); Q.pop(); int q = v.x; int w = v.y; if (q == c && w == d) { cout << d1[q][w]; return 0; } if (q + 1 <= n && (w == 0 || w == n) && d1[q + 1][w] > d1[q][w] + 1) { d1[q + 1][w] = d1[q][w] + 1; Q.push(node(q + 1, w)); } if (q - 1 >= 0 && (w == 0 || w == n) && d1[q - 1][w] > d1[q][w] + 1) { d1[q - 1][w] = d1[q][w] + 1; Q.push(node(q - 1, w)); } if (w + 1 <= n && (q == 0 || q == n) && d1[q][w + 1] > d1[q][w] + 1) { d1[q][w + 1] = d1[q][w] + 1; Q.push(node(q, w + 1)); } if (w - 1 >= 0 && (q == 0 || q == n) && d1[q][w - 1] > d1[q][w] + 1) { d1[q][w - 1] = d1[q][w] + 1; Q.push(node(q, w - 1)); } } } |
#include <bits/stdc++.h> using namespace std; int main() { int n, k; scanf( %d %d , &n, &k); bool first = 1; for (int i = n; i >= n - k + 1; --i) { if (first) { printf( %d , i); first = !first; } else printf( %d , i); } for (int i = 1; i <= n - k; ++i) { if (first) { printf( %d , i); first = !first; } else printf( %d , i); } printf( n ); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { size_t rnn; cin >> rnn; size_t rnsize = 2 * rnn; size_t rncnt = rnsize; vector<long> rnvec(rnsize); while (rncnt--) cin >> rnvec[rncnt]; sort(rnvec.begin(), rnvec.end()); long rnsum_fin = 999999; long rnsum = 0; for (size_t rncnt1 = 0; rncnt1 < rnsize; ++rncnt1) for (size_t rncnt2 = 0; rncnt2 < rncnt1; ++rncnt2) { rnsum = 0; for (size_t rncnt3 = 0, rnpos = 0; rncnt3 < rnsize; ++rncnt3) if (rncnt3 != rncnt1 && rncnt3 != rncnt2) { ++rnpos; if (rnpos % 2) rnsum -= rnvec[rncnt3]; else rnsum += rnvec[rncnt3]; } rnsum_fin = min(rnsum, rnsum_fin); } cout << rnsum_fin; } |
#include <bits/stdc++.h> using namespace std; int n, ntot, ieven, rs[6], ord[6 + 1], iord[6 + 1]; vector<int> xs; inline int imake(int i) { return i > 0 ? ord[i] : -ord[-i]; } void iextend_even(int p, vector<int> &vi) { vector<int> vr[2], vg; vr[1] = vi; for (int i = (int)vi.size() - 1; i >= 0; i--) vr[0].push_back(-vi[i]); vr[0][0] = imake(p + 1); vr[1][0] = imake(p + 1); vg = vi; for (int k = 1, l = 0; k < xs[p]; k++, l ^= 1) { vg[vg.size() - k] = imake(-(p + 1)); vg.insert(vg.end() - k, vr[l].begin(), vr[l].end()); } vi = vg; } void iextend_odd(int p, vector<int> &vi) { vector<int> vr[2], vg; vr[1] = vi; for (int i = (int)vi.size() - 1; i >= 0; i--) vr[0].push_back(-vi[i]); int vrz = vr[0][0]; vg = vi; for (int k = 1, m = rs[p] - 1; k < xs[p]; k += 2, m -= 2) { if (m > 0) { vr[0][0] = imake(-(p + 1)); vg[vg.size() - k] = imake(p + 1); vg.insert(vg.end() - k, vr[0].begin(), vr[0].end()); vg.insert(vg.end() - k, imake(p + 1)); vg.insert(vg.end() - k - 1, vr[1].begin(), vr[1].end()); vg.insert(vg.end() - k - 1 - vr[1].size(), imake(-(p + 1))); } else { vr[0][0] = vrz; vg.push_back(imake(p + 1)); vg.insert(vg.end(), vr[0].begin(), vr[0].end()); vg.push_back(imake(p + 1)); vg.insert(vg.end(), vr[1].begin(), vr[1].end()); } } vi = vg; } vector<int> iinit_even() { int p = xs[1]; vector<int> vg, vi; for (int i = 0; i < p - 1; i++) vi.push_back(imake(2)); vi.push_back(imake(1)); for (int i = 0; i < p - 1; i++) vi.push_back(imake(-2)); vi.push_back(imake(-1)); vg = vi; vi[0] = imake(1); for (int i = 1, k = 2; i < xs[0] / 2; i++, k += 2) { vg[vg.size() - k] = imake(-1); vg.insert(vg.end() - k, vi.begin(), vi.end()); } return vg; } vector<int> iinit_odd() { int p = (rs[0] - 1) % 2, q = (rs[1] - 1) % 2, pp = xs[0], qq = xs[1], k, kl, kr, ku, kd; vector<int> vg, vil, virx, viry, viu, vid; if (!(p == 1 && q == 1) && (p == 1 || q == 1)) return vg; for (int i : {-1, -1, 2, 2, 1, -2}) vil.push_back(imake(i)); for (int i : {1, 2, 1, -2, -2, -1}) virx.push_back(imake(i)); for (int i : {1, 1, -2, -2, -1, 2}) viry.push_back(imake(i)); viu.resize(pp * 2); vid.resize(pp * 2); viu[0] = imake(2), viu[1] = imake(2), viu[pp + 1] = imake(-2); vid[0] = imake(-2), vid[1] = imake(-2), vid[pp + 1] = imake(2); for (int i = 2; i < pp + 1; i++) viu[i] = imake(1), vid[i] = imake(-1); for (int i = pp + 2; i < pp * 2; i++) viu[i] = imake(-1), vid[i] = imake(1); if (p == 1 && q == 1) { for (int i : {1, -2, -1, -1, 2, 2, 1, 1}) vg.push_back(imake(i)); p++, q++, kl = 4, kr = 1, ku = 6, kd = 2; } else if (rs[0] == 1 && rs[1] == 1) { for (int i : {2, 2, 1, 1, -2, -2, -1, 2}) vg.push_back(imake(i)); p = q = 2, kl = 0, kr = 4, ku = 2, kd = 6; } else if (rs[0] == 1 && rs[1] != 1) { for (int i : {1, 1, -2, -2, -1, -1, 2, 1}) vg.push_back(imake(i)); p = q = 2, kl = 6, kr = 2, ku = 0, kd = 4; } else if (rs[0] != 1 && rs[1] != 1) { for (int i : {-2, -2, -1, -1, 2, 2, 1, -2}) vg.push_back(imake(i)); p = q = 2, kl = 4, kr = 0, ku = 6, kd = 2; } else if (rs[0] != 1 && rs[1] == 1) { for (int i : {-1, -1, 2, 2, 1, 1, -2, -1}) vg.push_back(imake(i)); p = q = 2, kl = 2, kr = 6, ku = 4, kd = 0; } if (p < rs[0] - 1 && ku == 0) ku = 8; if (rs[0] - 1 < pp - 1 && kd == 0) kd = 8; for (k = p; k < rs[0] - 1; k += 2, kl += 2) { vg[kl] = imake(1); vg.insert(vg.begin() + kl, vil.begin(), vil.end()); ku += 2; kr > kl && (kr += 6); kd > kl && (kd += 6); } if (kr == 1) { for (; k < pp - 1; k += 2, kr += 4) { vg[kr] = imake(-1); vg.insert(vg.begin() + kr, virx.begin(), virx.end()); ku > kr && (ku += 6); kd += 4; } } else { for (; k < pp - 1; k += 2, kr += 2) { vg[kr] = imake(-1); vg.insert(vg.begin() + kr, viry.begin(), viry.end()); ku > kr && (ku += 6); kd += 2; } } for (k = q; k < rs[1] - 1; k += 2, kd += 2) { vg[kd] = imake(2); vg.insert(vg.begin() + kd, vid.begin(), vid.end()); ku > kd && (ku += vid.size()); } for (; k < qq - 1; k += 2, ku += 2) { vg[ku] = imake(-2); vg.insert(vg.begin() + ku, viu.begin(), viu.end()); } return vg; } inline void iapply(int *px, int op) { op < 0 ? px[iord[-op] - 1]-- : px[iord[op] - 1]++; } bool iequal(int *px, int *py) { for (int i = 0; i < n; i++) if (px[i] != py[i]) return false; return true; } void icycle(vector<int> &vi) { int px[n], k; for (int i = 1; i <= n; i++) iord[ord[i]] = i; for (int i = 0; i < n; i++) px[i] = 1; for (k = 0; k < (int)vi.size(); k++) { if (iequal(px, rs)) break; iapply(px, vi[k]); } if (k != 0) { vi.insert(vi.end(), vi.begin(), vi.begin() + k); vi.erase(vi.begin(), vi.begin() + k); } } void iprint(vector<int> &vi) { for (auto i : vi) i > 0 ? (cout << inc << i << endl) : (cout << dec << -i << endl); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; ntot = 1; xs.resize(n); for (int i = 0; i < n; i++) cin >> xs[i], ntot *= xs[i]; for (int i = 0; i < n; i++) cin >> rs[i]; if (n == 1) { if (xs[0] == 2 && rs[0] == 1) { cout << Cycle << endl; cout << inc 1 << endl; cout << dec 1 << endl; } else if (xs[0] == 2 && rs[0] == 2) { cout << Cycle << endl; cout << dec 1 << endl; cout << inc 1 << endl; } else if (rs[0] == 1) { cout << Path << endl; for (int i = 1; i < xs[0]; i++) cout << inc 1 << endl; } else if (rs[0] == xs[0]) { cout << Path << endl; for (int i = 1; i < xs[0]; i++) cout << dec 1 << endl; } else cout << No << endl; } else { for (int i = 1; i <= n; i++) ord[i] = i; for (int i = 0; i < n; i++) if (xs[i] % 2 == 0) { swap(xs[0], xs[i]); swap(rs[0], rs[i]); swap(ord[1], ord[i + 1]); break; } vector<int> vi; if (xs[0] % 2 == 0) { cout << Cycle << endl; vi = iinit_even(); for (int p = 2; p < n; p++) iextend_even(p, vi); if (xs[ieven] % 2 == 0) icycle(vi); } else { for (int i = 1; i < n; i++) if (rs[i] % 2 == 0) { if (rs[0] % 2 != 0) { swap(xs[0], xs[i]); swap(rs[0], rs[i]); swap(ord[1], ord[i + 1]); } else if (rs[1] % 2 != 0) { swap(xs[1], xs[i]); swap(rs[1], rs[i]); swap(ord[2], ord[i + 1]); } else break; } bool ok = true; for (int i = 2; i < n; i++) if (rs[i] % 2 == 0) ok = false; vi = iinit_odd(); if (vi.size() == 0 || !ok) { cout << No << endl; vi.clear(); } else { cout << Path << endl; for (int p = 2; p < n; p++) iextend_odd(p, vi); } } iprint(vi); } return 0; } |
#include <bits/stdc++.h> int main() { int t; scanf( %d , &t); while (t--) { int n, i; scanf( %d , &n); if (n % 4 != 0) printf( NO n ); else { printf( YES n ); int sum1 = 0, sum2 = 0; for (i = 0; i < n / 2; i++) { printf( %d , 2 * (i + 1)); sum1 += 2 * (i + 1); } for (i = 0; i < n / 2 - 1; i++) { printf( %d , 2 * (i + 1) - 1); sum2 += 2 * (i + 1) - 1; } printf( %d n , sum1 - sum2); } } } |
#include <bits/stdc++.h> using namespace std; const int N_ = 105; int N, M, X, Y; int len(int x) { for (int l = 1, v = 7;; l++) { if (v >= x) return l; v *= 7; } } set<long long> S; bool chk[100]; void dfs(int i, long long p, long long q) { if (p >= N || q >= M) return; if (i == X + Y) { S.insert(p * (long long)1e9 + q); return; } for (int d = 0; d < 7; d++) { if (!chk[d]) { chk[d] = true; ((i < X ? p : q) *= 7) += d; dfs(i + 1, p, q); ((i < X ? p : q) -= d) /= 7; chk[d] = false; } } } int main() { scanf( %d %d , &N, &M); X = len(N), Y = len(M); if (X + Y > 7) return 0 & puts( 0 ); dfs(0, 0, 0); printf( %lu n , S.size()); return 0; } |
#include <bits/stdc++.h> using namespace std; const int N = 85; long long fibo[N + 5]; long long dp[N + 5][3][3]; long long arr[N + 5]; void pre() { fibo[0] = 1, fibo[1] = 2; for (int i = 2; i <= N; i++) { fibo[i] = fibo[i - 1] + fibo[i - 2]; } } long long solve(int idx, int cur1, int cur2) { if (idx == -1) { if (cur1 + cur2 != 0) return 0; return 1; } long long &res = dp[idx][cur1][cur2]; if (res != -1) return res; res = 0; if (cur1 + arr[idx] > 2) return 0; if (cur1 + arr[idx] <= 1) res += solve(idx - 1, cur2, 0); if (cur1 + arr[idx] == 1) res += solve(idx - 1, cur2 + 1, 1); if (cur1 + arr[idx] == 2) res += solve(idx - 1, cur2 + 1, 1); return res; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); pre(); int ntc; cin >> ntc; while (ntc--) { long long x; cin >> x; for (int i = N; i >= 0; i--) { arr[i] = 0; if (x < fibo[i]) continue; x -= fibo[i]; arr[i] = 1; } memset(dp, -1, sizeof dp); cout << solve(N, 0, 0) << endl; } } |
#include <bits/stdc++.h> using namespace std; const int N = 100100; const long long MOD = 100030001, H = 47; map<int, long long> d[N]; vector<int> adj[N]; set<pair<long long, int> > s; vector<pair<int, pair<long long, int> > > nei[N]; long long r[N], l[N], num[N], ch[N], h[3 * N], n, sz, ans, o = 10, c = 11; void add(int ind, int val) { auto it = s.lower_bound({ind, val}); if (it == s.end() || it->first != ind) s.insert({ind, val}); else { if (it->second + val != 0) s.insert({ind, it->second + val}); s.erase(it); } return; } void f(int v, int u) { add(d[v][v], -1); add(d[u][u], 1); add(d[v][u], 1); add(d[u][v], -1); return; } void DFS1(int v, int par = 0) { for (int u : adj[v]) { if (u != par) { DFS1(u, v); ch[v] += ch[u]; nei[v].push_back({d[u][v], {ch[u], u}}); } } ch[v]++; long long ans = o; sort(nei[v].begin(), nei[v].end()); for (auto u : nei[v]) { ans = ans * h[2 * u.second.first] % MOD; ans = (ans + u.first) % MOD; } d[v][par] = (ans * H + c) % MOD; return; } void DFS2(int v, int par = 0) { nei[v].clear(); for (int u : adj[v]) { nei[v].push_back({d[u][v], {ch[u], u}}); if (u == par) nei[v].back().second.first = n - ch[v]; } sort(nei[v].begin(), nei[v].end()); l[0] = o, r[nei[v].size() + 1] = c, num[nei[v].size() + 1] = 1; for (int i = 1; i <= nei[v].size(); i++) { auto u = nei[v][i - 1]; l[i] = (l[i - 1] * h[u.second.first * 2] + d[u.second.second][v]) % MOD; } for (int i = nei[v].size(); i > 0; i--) { auto u = nei[v][i - 1]; num[i] = num[i + 1] + u.second.first * 2; r[i] = (r[i + 1] + d[u.second.second][v] * h[num[i + 1]]) % MOD; } for (int i = 0; i < nei[v].size(); i++) { d[v][nei[v][i].second.second] = (l[i] * h[num[i + 2]] + r[i + 2]) % MOD; } for (int u : adj[v]) if (u != par) DFS2(u, v); add(d[v][par], 1); return; } void DFS3(int v, int par = 0) { if (s.size() > sz) { ans = v; sz = s.size(); } for (int u : adj[v]) { if (u != par) { f(v, u); DFS3(u, v); } } f(v, par); return; } int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int u, v; cin >> n; h[0] = 1; for (int i = 0; i <= n * 2 + 10; i++) h[i + 1] = h[i] * H % MOD; for (int i = 1; i < n; i++) { cin >> u >> v; adj[--u].push_back(--v); adj[v].push_back(u); } if (n == 1) { cout << 1 << endl; return 0; } DFS1(0); DFS2(0); DFS3(0); cout << ans + 1 << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; struct MinCostFlow { struct Edge { int t; int f; int c; Edge *next, *rev; Edge(int _t, int _f, int _c, Edge* _next) : t(_t), f(_f), c(_c), next(_next) {} }; vector<Edge*> E; int addV() { E.push_back((Edge*)0); return E.size() - 1; } Edge* makeEdge(int s, int t, int f, int c) { return E[s] = new Edge(t, f, c, E[s]); } void addEdge(int s, int t, int f, int c) { Edge *e1 = makeEdge(s, t, f, c), *e2 = makeEdge(t, s, 0, -c); e1->rev = e2, e2->rev = e1; } pair<int, int> minCostFlow(int vs, int vt, bool maxFlow) { int n = E.size(); int flow = 0; int cost = 0; const int MAX_COST = ~0U >> 1; const int MAX_FLOW = ~0U >> 1; for (;;) { vector<int> dist(n, MAX_COST); vector<int> am(n, 0); vector<Edge*> prev(n); vector<bool> inQ(n, false); queue<int> que; dist[vs] = 0; am[vs] = MAX_FLOW; que.push(vs); inQ[vs] = true; while (!que.empty()) { int u = que.front(); int c = dist[u]; que.pop(); inQ[u] = false; for (Edge* e = E[u]; e; e = e->next) if (e->f > 0) { int nc = c + e->c; if (nc < dist[e->t]) { dist[e->t] = nc; prev[e->t] = e; am[e->t] = min(am[u], e->f); if (!inQ[e->t]) { que.push(e->t); inQ[e->t] = true; } } } } if (maxFlow) { if (dist[vt] == MAX_COST) break; } else { if (dist[vt] >= 0) break; } int by = am[vt]; int u = vt; flow += by; cost += by * dist[vt]; while (u != vs) { Edge* e = prev[u]; e->f -= by; e->rev->f += by; u = e->rev->t; } } return make_pair(flow, cost); } }; const int MAX_N = 100 + 10; int n, m; string how[MAX_N]; int A[MAX_N], B[MAX_N]; int ia[MAX_N], ib[MAX_N]; int notKillAll() { MinCostFlow net; int vs = net.addV(), vt = net.addV(); for (int i = 0; i < n; ++i) { ia[i] = net.addV(); net.addEdge(vs, ia[i], 1, 0); } for (int i = 0; i < m; ++i) { ib[i] = net.addV(); net.addEdge(ib[i], vt, 1, 0); } for (int i = 0; i < n; ++i) { for (int j = 0; j < m; ++j) { if (B[j] > A[i] && how[i] == ATK ) { net.addEdge(ia[i], ib[j], 1, -(B[j] - A[i])); } } } return -net.minCostFlow(vs, vt, false).second; } int killAll() { MinCostFlow net; int vs = net.addV(), vt = net.addV(); for (int i = 0; i < m; ++i) { ia[i] = net.addV(); net.addEdge(vs, ia[i], 1, 0); } for (int i = 0; i < m; ++i) { ib[i] = net.addV(); net.addEdge(ib[i], vt, 1, 0); } for (int i = 0; i < m; ++i) { for (int j = 0; j < m; ++j) { if (i < n) { if (how[i] == ATK ) { if (B[j] >= A[i]) net.addEdge(ia[i], ib[j], 1, -(B[j] - A[i])); } else { if (B[j] > A[i]) net.addEdge(ia[i], ib[j], 1, 0); } } else { net.addEdge(ia[i], ib[j], 1, -B[j]); } } } pair<int, int> ret = net.minCostFlow(vs, vt, true); if (ret.first == m) return -ret.second; return 0; } int main() { cin >> n >> m; for (int i = 0; i < n; ++i) { cin >> how[i] >> A[i]; } for (int i = 0; i < m; ++i) { cin >> B[i]; } int ans = notKillAll(); if (m > n) ans = max(ans, killAll()); cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int n, m; int k; bool debug = false; char mp[170][170]; int l[170], r[170]; int main() { cin >> n >> m; for (int i = 0; i < n; i++) { scanf( %s , mp[i]); l[i] = m; r[i] = -1; for (int j = 0; j < m; j++) if (mp[i][j] == W ) { l[i] = j; break; } for (int j = m - 1; j >= 0; j--) if (mp[i][j] == W ) { r[i] = j; break; } } int ans = 0, pos = 0, dir = 1; for (int i = 0, j = 1; i < n; i = j) { j = i + 1; while (j < n && r[j] == -1) j++; if (r[i] != -1) { if (dir == 1) { ans += r[i] - pos; pos = r[i]; } else { ans += pos - l[i]; pos = l[i]; } } if (j == n) break; else { dir = (j - i) % 2 == 1 ? 1 - dir : dir; int npos; if (dir == 1) { npos = min(l[j], pos); } else { npos = max(r[j], pos); } ans += abs(npos - pos); pos = npos; ans += (j - i); } } cout << ans << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; vector<long long> cnt(26); string s; for (int ss = 0; ss < (int)n; ss++) { cin >> s; for (int i = 0; i < (int)s.size(); i++) s[i] -= a ; int nfst = 0, nlst = 0; while (nfst < s.size() && s[nfst] == s[0]) nfst++; while (nlst < s.size() && s[s.size() - nlst - 1] == s.back()) nlst++; vector<long long> old(cnt); fill(cnt.begin(), cnt.end(), 0); for (int i = 0; i < (int)26; i++) cnt[i] = old[i] > 0; if (nfst == s.size()) cnt[s[0]] = (old[s[0]] + 1) * nfst + old[s[0]]; else if (s[0] == s.back()) { if (old[s[0]]) cnt[s[0]] = nfst + 1 + nlst; } else { if (old[s[0]]) cnt[s[0]] = nfst + 1; if (old[s.back()]) cnt[s.back()] = 1 + nlst; } char cur = s[0]; long long streak = 1; cnt[cur] = max(cnt[cur], streak); for (int i = 1; i < s.size(); i++) { if (s[i] == cur) streak++; else { cur = s[i]; streak = 1; } cnt[cur] = max(cnt[cur], streak); } for (int i = 0; i < (int)26; i++) cnt[i] = min(cnt[i], (long long)1e9); } long long ans = 0; for (int i = 0; i < (int)26; i++) ans = max(ans, cnt[i]); cout << ans << n ; } |
#include <bits/stdc++.h> using namespace std; vector<pair<long long, long long> > v; map<long long, long long> m; int qpow(long long a, long long b, long long m) { long long c = 1; while (b) { if (b & 1) c = c * a % m; b >>= 1; a = a * a % m; } return c; } int main() { ios::sync_with_stdio(false); long long num, n, s, t; cin >> n >> s; if (8 * s / n < 63) { num = pow(2, 8 * s / n); } else { num = 0x7fffffffffffffff; } for (int i = (0); i < (n); ++i) { cin >> t; if (m.count(t) == 0) { v.push_back(make_pair(t, 0)); m[t] = 1; } else { m[t]++; } } int len = v.size(); for (int i = (0); i < (len); ++i) { v[i].second = m[v[i].first]; } sort(v.begin(), v.end()); long long l = -1, r = len; long long ans = 0; while (l < r && len > num) { l++; ans += v[l].second; len--; } long long minn = ans; while (l >= 0) { if (l >= r) { cout << n; return 0; } ans -= v[l].second; len++; l--; while (len > num) { r--; ans += v[r].second; len--; } minn = min(ans, minn); } cout << minn; return 0; } |
#include <bits/stdc++.h> int inp() { char c = getchar(); int neg = 1; while (c < 0 || c > 9 ) { if (c == - ) neg = -1; c = getchar(); } int sum = 0; while (c >= 0 && c <= 9 ) { sum = sum * 10 + c - 0 ; c = getchar(); } return sum * neg; } int n, p; int main() { n = inp(); p = inp(); for (int i = 1; i <= 1000; i++) { if (n - (i * p) <= 0) break; if (__builtin_popcount(n - (i * p)) <= i && i <= (n - (i * p))) { printf( %d n , i); return 0; } } printf( -1 n ); } |
#include <bits/stdc++.h> using namespace std; long long int sum, max_lev; vector<pair<long long int, long long int>> level[5010]; vector<long long int> adj[5010]; vector<pair<long long int, long long int>> store[5010]; vector<pair<pair<long long int, long long int>, long long int>> v; long long int vis[5010]; void bfs(long long int s, long long int lev) { queue<pair<long long int, long long int>> q; q.push(make_pair(s, lev)); vis[s] = 1; while (!q.empty()) { s = q.front().first; lev = q.front().second; for (long long int i = 0; i < store[s].size(); i++) { level[lev].push_back(make_pair(store[s][i].first, store[s][i].second)); sum += store[s][i].first; } q.pop(); max_lev = max(lev, max_lev); for (long long int i = 0; i < adj[s].size(); i++) { if (vis[adj[s][i]] == 0) { vis[adj[s][i]] = 1; q.push(make_pair(adj[s][i], lev + 1)); } } } } bool check(long long int mid, long long int max_cost, long long int tot) { long long int cost = 0, curr = 0, p, rem, i, j; for (i = 0; i < v.size(); i++) { if (v[i].second > mid) continue; p = v[i].first.first * v[i].first.second; if (cost + p > max_cost) { rem = max_cost - cost; p = rem / v[i].first.first; curr += p; break; } curr += v[i].first.second; cost += p; } if (curr >= tot) return true; return false; } long long int bs(long long int low, long long int high, long long int max_cost, long long int tot) { long long int ans = -1; while (low <= high) { long long int mid = (low + high) / 2; if (check(mid, max_cost, tot)) { high = mid - 1; ans = mid; } else { low = mid + 1; } } return ans; } int main() { long long int n, m, a, b, i, w, c, k, p, q, g, r, j; ios::sync_with_stdio(0); cin.tie(0); cin >> n >> m; for (i = 1; i <= m; i++) { cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } cin >> w; for (i = 1; i <= w; i++) { cin >> c >> k >> p; store[c].push_back(make_pair(k, p)); } cin >> q; while (q--) { sum = max_lev = 0; cin >> g >> r >> a; for (i = 0; i <= n; i++) level[i].clear(); v.clear(); memset(vis, 0, sizeof(vis)); bfs(g, 0); if (sum < r) { cout << -1 n ; continue; } for (i = 0; i <= max_lev; i++) { for (j = 0; j < level[i].size(); j++) { v.push_back( make_pair(make_pair(level[i][j].second, level[i][j].first), i)); } } sort(v.begin(), v.end()); cout << bs(0, max_lev, a, r) << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 4000010, maxm = 2010; long long s[maxn], f[maxn], P[maxm], Q[maxm]; int a, b, c, p[maxm], vis[maxm], mu[maxm]; void inc(long long &a, long long b) { a += b; if (a > (1ll << 30)) a -= (1ll << 30); } int main() { scanf( %d%d%d , &a, &b, &c); mu[1] = 1; for (int i = 2; i <= c; i++) { if (!vis[i]) p[++p[0]] = i, mu[i] = -1; for (int j = 1; j <= p[0] && i * p[j] <= c; j++) { vis[i * p[j]] = 1; if (i % p[j] == 0) { mu[i * p[j]] = 0; break; } else mu[i * p[j]] = -mu[i]; } } for (int i = 1; i <= a; i++) for (int j = 1; j <= b; j++) f[i * j]++; for (int i = 1; i <= a * b; i++) for (int j = 1; j <= a * b / i; j++) inc(s[i], f[i * j]); for (int i = 1; i <= c; i++) for (int j = 1; j <= a * b / i; j++) inc(P[i], s[i * j]); for (int i = 1; i <= c; i++) for (int j = 1; j <= i; j++) inc(Q[i], i / j); long long ans = 0; for (int i = 1; i <= c; i++) ans = (ans + mu[i] * P[i] * Q[c / i] % (1ll << 30)) % (1ll << 30); printf( %I64d , (ans + (1ll << 30)) % (1ll << 30)); return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int a; cin >> a; while (a--) { int b, c; cin >> b >> c; int arr1[b], arr2[c]; set<int> check; for (int j = 0; j < b; j++) { cin >> arr1[j]; check.insert(arr1[j]); } int x = -1; for (int j = 0; j < c; j++) { cin >> arr2[j]; if (check.count(arr2[j])) x = arr2[j]; } if (x == -1) cout << NO n ; else { cout << YES n ; cout << 1 << << x << n ; } } } |
#include <bits/stdc++.h> using namespace std; vector<pair<int, int> > g[110]; int seen[110][110]; int c[110]; int ans; void dfs(int s, int end, int color) { if (s == end && c[color] == 0) { c[color] = 1; ans++; return; } for (int i = 0; i < (int)g[s].size(); i++) { if ((color == 0 || color == g[s][i].second) && seen[s][g[s][i].first] == 0 && seen[g[s][i].first][s] == 0) { seen[s][g[s][i].first] = 1; seen[g[s][i].first][s] = 1; dfs(g[s][i].first, end, g[s][i].second); seen[s][g[s][i].first] = 0; seen[g[s][i].first][s] = 0; } } } int main() { int n, m; scanf( %d %d , &n, &m); int temp1, temp2, temp3; for (int i = 0; i < m; i++) { scanf( %d %d %d , &temp1, &temp2, &temp3); g[temp1].push_back(pair<int, int>(temp2, temp3)); g[temp2].push_back(pair<int, int>(temp1, temp3)); } int q; scanf( %d , &q); for (int i = 0; i < q; i++) { scanf( %d %d , &temp1, &temp2); dfs(temp1, temp2, 0); printf( %d n , ans); ans = 0; for (int i = 0; i < 110; i++) for (int j = 0; j < 110; j++) seen[i][j] = 0; for (int i = 0; i < 110; i++) c[i] = 0; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); int n, t, m = 0; cin >> n; vector<int> s; for (int i = 0; i < n; i++) { cin >> t; s.push_back(t); } sort(s.begin(), s.end()); while (true) { t = -1; for (int i = 0; i < n; i++) { if (s[i] != -1 && s[i] >= t + 1) { s[i] = -1; t++; } } if (t == -1) break; m++; } cout << m << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; char a[20][20], ans[20]; int n, m; bool isValid() { for (int i = 2; i <= n; i++) { int c = 0; for (int j = 1; j <= m; j++) c += (ans[j] != a[i][j]); if (c > 1) return false; } return true; } void fun() { cin >> n >> m; for (int i = 1; i <= n; i++) cin >> (a[i] + 1); for (int i = 1; i <= m; i++) ans[i] = a[1][i]; ans[m + 1] = 0 ; if (isValid()) cout << (ans + 1) << n ; else { bool isGood = false; for (int i = 1; i <= m && !isGood; i++) { for (char c = a ; c <= z && !isGood; c++) { if (c != a[1][i]) { ans[i] = c; if (isValid()) { cout << (ans + 1) << n ; isGood = true; } } } ans[i] = a[1][i]; } if (!isGood) cout << -1 n ; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { fun(); } return 0; } |
#include <bits/stdc++.h> using namespace std; template <class T> bool umin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } template <class T> bool umax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } int main() { int n, x = 500; scanf( %d , &n); for (int i = 0; i < 26 and (n or !i); i++) { while (x * (x - 1) / 2 > n) x--; n -= x * (x - 1) / 2; for (int j = 0; j < x; j++) printf( %c , char(i + 97)); } return 0; } |
#include <bits/stdc++.h> using namespace std; char s[100005]; int n, k, cnt[20]; bool a[20][20], mark[1 << 17], tmp[1 << 17]; inline void work(int x, int y) { memset(tmp, 0, sizeof(tmp)); for (int i = 1, j, t; i <= n;) { while (i <= n && s[i] - a != x) i++; j = i + 1; t = 0; while (j <= n && s[j] - a != x && s[j] - a != y) { t |= 1 << s[j] - a ; j++; } if (j > n) break; if (s[j] - a == y) tmp[t] = 1; if (s[j] - a == x) i = j; else i = j + 1; } for (int i = 1; i < 1 << k; i++) { if (!tmp[i]) continue; mark[i] = 1; for (int j = 0; j < k; j++) if (j != x && j != y) tmp[i | 1 << j] = 1; } } bool f[1 << 17]; int main() { cin >> n >> k; scanf( %s , s); for (int i = 0; i < n; i++) cnt[s[i] - a ]++; for (int i = 0; i < k; i++) for (int j = 0; j < k; j++) { cin >> a[i][j]; if (!a[i][j]) work(i, j); } f[0] = 1; for (int i = 0; i < 1 << k; i++) { if (!f[i]) continue; for (int j = 0; j < k; j++) if (!mark[i | 1 << j]) f[i | 1 << j] = 1; } int ans = 0; for (int i = 0; i < 1 << k; i++) { if (!f[i]) continue; int t = 0; for (int j = 0; j < k; j++) if (i & 1 << j) t += cnt[j]; ans = max(ans, t); } cout << n - ans; return 0; } |
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; void add(int &a, int b) { a += b; if (a >= MOD) a -= MOD; } int n, K, C[1003][1003], fac[1003]; int dp[1003][1003][2][2], ans[1003]; int main() { scanf( %d%d , &n, &K); for (int i = 1; i <= n; i++) { C[i][0] = C[i][i] = 1; for (int j = 1; j < i; j++) add(C[i][j], C[i - 1][j]), add(C[i][j], C[i - 1][j - 1]); } fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % MOD; dp[0][0][1][0] = 1; for (int i = 1; i <= n; i++) for (int j = 0; j <= i - 1; j++) for (int k = 0; k < 2; k++) for (int l = 0; l < 2; l++) if (dp[i - 1][j][k][l] > 0) { add(dp[i][j][l][0], dp[i - 1][j][k][l]); if (!k) add(dp[i][j + 1][l][0], dp[i - 1][j][k][l]); if (i < n) add(dp[i][j + 1][l][1], dp[i - 1][j][k][l]); } for (int i = 0; i <= n; i++) { for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) add(ans[i], dp[n][i][j][k]); ans[i] = 1ll * ans[i] * fac[n - i] % MOD; } int cur = -1; for (int i = K + 1; i <= n; i++) { add(ans[K], (1ll * cur * C[i][K] % MOD * ans[i] % MOD + MOD) % MOD); cur *= -1; } printf( %d , ans[K]); } |
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 10; const long long inf = 1e15; const int mod = 1e6; void faster() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); } const long long N = 3000 * 200; int mark[N]; int main() { faster(); int n; cin >> n; int t = 0; vector<int> v[n]; for (int i = 1; i <= n * n; i++) { v[(i + t) % n].push_back(i); if (i % n == 0) t++; } for (int i = 0; i < n; i++) { for (auto c : v[i]) cout << c << ; cout << endl; } } |
#include <bits/stdc++.h> using namespace std; const int maxn = 100000 + 5; const long long mod = 99999997; long long a[maxn], k, n; void dfs(long long l, long long r) { if (l + 1 >= r || !k) return; long long mid = (l + r) / 2; k -= 2; swap(a[mid - 1], a[mid]); dfs(l, mid); dfs(mid, r); } int main() { cin >> n >> k; if (k > 2 * n - 1 || k % 2 == 0) { cout << -1 << endl; return 0; } for (int i = 0; i < n; i++) { a[i] = i + 1; } k--; dfs(0ll, n); for (int i = 0; i < n; i++) cout << a[i] << ; } |
#include <bits/stdc++.h> using namespace std; template <class T> inline void smax(T &x, T y) { x = max((x), (y)); } template <class T> inline void smin(T &x, T y) { x = min((x), (y)); } inline void scanInt(int &x) { register char c = getchar(); x = 0; bool neg = false; while (c < 48 || c > 57) { if (c == - ) neg = true; c = getchar(); } for (; c > 47 && c < 58; c = getchar()) { x = (x << 1) + (x << 3) + c - 48; } if (neg) x *= -1; } int n, m; string origin; string s; bool place_char_start(int inx) { if (inx >= 2) { for (char i = s[inx] + 1; i < a + m; i++) if (i != s[inx - 1] && i != s[inx - 2] && i != s[inx]) { s[inx] = i; return true; } return false; } else if (inx == 1) { for (char i = s[inx] + 1; i < a + m; i++) if (i != s[inx - 1] && i != s[inx]) { s[inx] = i; return true; } return false; } else if (inx == 0) { s[0] = s[0] + 1; if (s[0] >= a + m) return false; return true; } return false; } bool place_char(int inx) { if (inx >= 2) { for (char i = a ; i < a + m; i++) if (i != s[inx - 1] && i != s[inx - 2]) { s[inx] = i; return true; } return false; } else if (inx == 1) { for (char i = a ; i < a + m; i++) if (i != s[inx - 1]) { s[inx] = i; return true; } } else if (inx == 0) { s[0] = a ; return true; } return false; } int main(int argc, const char *argv[]) { scanInt(n); scanInt(m); cin >> s; origin = s; for (int i = (int)s.size() - 1; i >= 0; i--) { s = origin; bool ok = true; ok &= place_char_start(i); for (int j = i + 1; ok && j < s.size(); j++) { if (place_char(j) == false) ok = false; } if (ok) { cout << s; return 0; } } cout << NO ; return 0; } |
#include <bits/stdc++.h> using namespace std; struct event { int x[3], id; event() { x[0] = x[1] = x[2] = id = 0; } event(int xx, int yy, int zz, int idd) { x[0] = xx; x[1] = yy; x[2] = zz; id = idd; } }; int vc = 0; bool operator<(const event &a, const event &b) { if (a.x[vc] == b.x[vc]) return a.id < b.id; return a.x[vc] < b.x[vc]; } int ans[100005], ans_id[100005]; event G[3][200005]; void cdq(int L, int R, int now) { if (L >= R) return; vc = now; if (now == 2) { int last = -1, last_id = -1; for (int i = L; i <= R; ++i) { if (G[now][i].id > 0) { if (last_id != -1) { int &tmp = ans[G[now][i].id]; if (tmp == -1 || tmp < last) { tmp = last; ans_id[G[now][i].id] = last_id; } } } else { last = G[now][i].x[2]; last_id = -G[now][i].id; } } return; } int mid = (L + R) / 2, cnt = 0; cdq(L, mid, now); for (int i = L; i <= mid; ++i) if (G[now][i].id < 0) G[now + 1][++cnt] = G[now][i]; for (int i = mid + 1; i <= R; ++i) if (G[now][i].id > 0) G[now + 1][++cnt] = G[now][i]; vc = now + 1; sort(G[now + 1] + 1, G[now + 1] + cnt + 1); cdq(1, cnt, now + 1); cdq(mid + 1, R, now); } int main() { int n, m, i, j, k, l, cnt = 0; scanf( %d%d , &n, &m); for (i = 1; i <= n; ++i) { int x, y, z; scanf( %d%d%d , &x, &y, &z); G[0][++cnt] = event(x, -y, -z, -i); } for (i = 1; i <= m; ++i) { int x, y, z; scanf( %d%d%d , &x, &y, &z); G[0][++cnt] = event(x, -y, -z, i); } memset(ans, -1, sizeof(ans)); memset(ans_id, -1, sizeof(ans)); vc = 0; sort(G[0] + 1, G[0] + 1 + cnt); cdq(1, cnt, 0); for (i = 1; i <= m; ++i) printf( %d%c , ans_id[i], n [i == m]); } |
#include <bits/stdc++.h> using namespace std; vector<multiset<int> > G; vector<int> d; void dfs(int u, vector<int> &ans) { while (!G[u].empty()) { int v = *G[u].begin(); G[u].erase(G[u].begin()); G[v].erase(G[v].find(u)); dfs(v, ans); } ans.push_back(u); } int main() { int n, m; cin >> n >> m; G.resize(n); d.resize(n, 0); for (int i = 0; i < m; i++) { int u, v; scanf( %d %d , &u, &v); u--; v--; G[u].insert(v); G[v].insert(u); d[u]++; d[v]++; } vector<int> odd; for (int i = 0; i < n; i++) { if (d[i] & 1) odd.push_back(i); } int add = 0; for (int i = 0; i < (int)odd.size() - 1; i += 2) { int u = odd[i], v = odd[i + 1]; G[u].insert(v); G[v].insert(u); add++; } if (odd.size() & 1) { int u = odd[odd.size() - 1]; G[u].insert(u); G[u].insert(u); add++; } if ((m + add) % 2) { G[0].insert(0); G[0].insert(0); add++; } vector<int> ans; dfs(0, ans); cout << m + add << endl; for (int i = 0; i < (int)ans.size() - 1; i++) { if (i & 1) printf( %d %d n , ans[i] + 1, ans[i + 1] + 1); else printf( %d %d n , ans[i + 1] + 1, ans[i] + 1); } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { long long int arr[30] = {0}; unsigned long long sum = 0; long long int a, b, n; cin >> n; for (int i = 0; i < n; i++) { int j = 0; cin >> a; while (a != 0) { b = a % 10; arr[j] += b; arr[j + 1] += b; a /= 10; j += 2; } } for (unsigned long long int i = 0, ten = 1; i < 30; i++, ten = ten * 10) { if (!arr[i]) continue; arr[i] *= n; unsigned long long x = ((arr[i] % 998244353) * (ten % 998244353)) % 998244353; sum = (sum + x) % 998244353; } cout << sum << endl; } |
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int main() { int n; cin >> n; long long int x = 2; double d; long long int p; for (long long int i = 1; i <= n; i++) { long long int B = i * (i + 1) * (i + 1) - x / i; cout << B << n ; x = i * (i + 1); } } |
#include <bits/stdc++.h> using namespace std; string grid[8]; int get_weight(char c) { switch (c) { case q : return 9; case r : return 5; case b : case n : return 3; case p : return 1; default: return 0; } } int main() { ios_base::sync_with_stdio(0); for (int i = 0; i < 8; ++i) cin >> grid[i]; int black = 0, white = 0; for (int i = 0; i < 8; ++i) for (int j = 0; j < 8; ++j) { bool play1 = isupper(grid[i][j]); int score = get_weight(tolower(grid[i][j])); if (play1) black += score; else white += score; } if (black == white) cout << Draw ; else if (black > white) cout << White ; else cout << Black ; return 0; } |
#include <bits/stdc++.h> using namespace std; const int mod = 1000000007; char s[21][100100]; int n, m, ans = 1e9, all, inv, a[100100], bit[1100000], w[1100000], f[1100000]; int power(int a, int k) { int ret = 1; while (k) { if (k & 1) ret = 1ll * ret * a % mod; a = 1ll * a * a % mod; k >>= 1; } return ret; } void FWT(int *a, int t) { int pre, cur = 0; static int f[2][1100000]; for (int i = 0; i <= all; i++) f[0][i] = a[i]; for (int i = 0; i <= n - 1; i++) { pre = cur, cur ^= 1; for (int s = 0; s <= all; s++) if (!(s >> i & 1)) { int l = f[pre][s], r = f[pre][s | (1 << i)]; f[cur][s] = (l + r) % mod; f[cur][s | (1 << i)] = (l + mod - r) % mod; } } if (t) for (int i = 0; i <= all; i++) f[cur][i] = 1ll * f[cur][i] * inv % mod; for (int i = 0; i <= all; i++) a[i] = f[cur][i]; } int main() { scanf( %d%d , &n, &m); all = (1 << n) - 1; inv = power(power(2, mod - 2), n); for (int i = 1; i <= n; i++) scanf( %s , s[i] + 1); for (int i = 1; i <= m; i++) for (int j = 1; j <= n; j++) a[i] = (a[i] << 1) + s[j][i] - 0 ; for (int i = 1; i <= all; i++) bit[i] = bit[i >> 1] + (i & 1); for (int i = 1; i <= m; i++) f[a[i]]++; for (int i = 0; i <= all; i++) w[i] = min(bit[i], n - bit[i]); FWT(f, 0); FWT(w, 0); for (int i = 0; i <= all; i++) f[i] = 1ll * f[i] * w[i] % mod; FWT(f, 1); for (int i = 0; i <= all; i++) ans = min(ans, f[i]); printf( %d n , ans); } |
#include <bits/stdc++.h> using namespace std; const int lim = 1e5 + 5; int main(int argc, char const *argv[]) { int n; cin >> n; std::vector<int> v(n); for (int i = 0; i < n; ++i) { cin >> v[i]; } int mn = INT_MAX, ind1, ind2; for (int i = 0; i < n - 1; ++i) { int dif = abs(v[i] - v[i + 1]); if (mn > dif) { mn = dif; ind1 = i + 1; ind2 = i + 2; } } int dif = abs(v[0] - v[n - 1]); if (mn > dif) { mn = dif; ind1 = 1; ind2 = n; } cout << ind1 << << ind2 << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define endl n const int DIM = 1E5+7; int A[DIM]; int cnt[2][DIM]; int solve() { int n; cin>>n; for(int i = 1;i<=n;++i){ cin>>A[i]; ++cnt[i%2][A[i]]; } sort(A+1,A+1+n); int flag = 0; for(int i = 1;i<=n;++i){ cnt[i%2][A[i]]--; } for(int i = 1;i<=n;++i){ if (cnt[0][A[i]] || cnt[1][A[i]]) flag = 1; cnt[0][A[i]] = cnt[1][A[i]] = 0; } if (flag) cout<< NO n ; else cout<< YES n ; return 1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin>>t; while(t--) assert(solve()); } |
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; string s, t; int lenX, lenY; long long dp[5009][5009]; inline long long go(int x, int y) { if (x < 0 || y < 0) return 0LL; long long &ret = dp[x][y]; if (ret != -1) return ret; ret = 0; if (y > 0) ret = go(x, y - 1); if (ret >= MOD) ret -= MOD; if (s[x] == t[y]) { ret++; if (x > 0 && y > 0) ret = (ret + go(x - 1, y - 1)) % MOD; } return ret; } int main() { memset(dp, -1, sizeof(dp)); cin >> s >> t; lenX = (int)s.length(); lenY = (int)t.length(); long long ans = 0; for (int i = 0; i < lenX; ++i) { ans += go(i, lenY - 1); if (ans >= MOD) ans -= MOD; } printf( %lld n , ans); return 0; } |
#include <bits/stdc++.h> using namespace std; int main(void) { const int MAXOPER = 1e5 + 123; const int MAXN = 1e5 + 123; vector<long long> V; long long sz = 0; static long long sz_after[MAXOPER]; static int oper_tip[MAXOPER]; static int oper_l[MAXOPER]; static int oper_c[MAXOPER]; static int oper_x[MAXOPER]; int n_oper; scanf( %d , &n_oper); for (int i_oper = 0; i_oper < (n_oper); ++i_oper) { int tip; scanf( %d , &tip); oper_tip[i_oper] = tip; if (tip == 1) { int x; scanf( %d , &x); oper_x[i_oper] = x; if ((int)V.size() < MAXN) V.push_back(x); ++sz; } else { int l, c; scanf( %d %d , &l, &c); oper_l[i_oper] = l; oper_c[i_oper] = c; for (int i = 0; i < (c); ++i) { if ((int)V.size() >= MAXN) break; for (int j = 0; j < (l); ++j) { if ((int)V.size() >= MAXN) break; V.push_back(V[j]); } } sz += l * c; } sz_after[i_oper] = sz; } int n_q; scanf( %d , &n_q); for (int i_q = 0; i_q < (n_q); ++i_q) { long long idx; cin >> idx; --idx; int o_idx = upper_bound(sz_after, sz_after + n_oper, idx) - sz_after; long long sol; if (oper_tip[o_idx] == 1) sol = oper_x[o_idx]; else { sol = V[(idx - sz_after[o_idx - 1]) % oper_l[o_idx]]; } cout << sol; if (i_q + 1 < n_q) cout << ; else cout << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, m, count = 0; cin >> n >> m; string arr[n]; for (int i = 0; i < n; i++) { cin >> arr[i]; } for (int i = 0; i < (m - 1); i++) { if (arr[n - 1][i] != R ) count++; } for (int i = 0; i < (n - 1); i++) { if (arr[i][m - 1] != D ) count++; } cout << count << endl; } } |
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; template <typename T> using V = std::vector<T>; struct debugger { template <typename T> debugger &operator,(const T &v) { cerr << v << ; return *this; } } dbg; using namespace std; const double pi = acos(-1); struct SegmentTree { int n; int size; vector<pair<double, double> > dat; vector<double> lazy; int real_size; SegmentTree(int n_) { real_size = n_; n = 1; size = n_; while (n < n_) n *= 2; dat = vector<pair<double, double> >(2 * n - 1); lazy = vector<double>(2 * n - 1); init(0, 0, n); } void init(int k, int l, int r) { if (r - l == 1) { if (l < real_size) { dat[k] = make_pair(1.0, 0.0); } } else { int m = (l + r) / 2; init((k * 2 + 1), l, m); init((k * 2 + 2), m, r); dat[k] = combine(dat[(k * 2 + 1)], dat[(k * 2 + 2)]); } } void update(int a, int b, const double &d) { update(a, b + 1, 0, 0, n, d); } void propagate(int k, int l, int r) { double t = lazy[k]; double x = dat[k].first * cos(t) + dat[k].second * sin(t); double y = -dat[k].first * sin(t) + dat[k].second * cos(t); dat[k] = make_pair(x, y); if ((k * 2 + 2) < lazy.size()) { lazy[(k * 2 + 1)] += lazy[k]; lazy[(k * 2 + 2)] += lazy[k]; } lazy[k] = 0.0; } void update(int a, int b, int k, int l, int r, const double &d) { propagate(k, l, r); if (r <= a || b <= l) return; if (a <= l && r <= b) { lazy[k] = d; propagate(k, l, r); } else { int m = (l + r) / 2; update(a, b, (k * 2 + 1), l, m, d); update(a, b, (k * 2 + 2), m, r, d); dat[k] = combine(dat[(k * 2 + 1)], dat[(k * 2 + 2)]); } } void update(int a, int b, int k, int l, int r, int z) { propagate(k, l, r); if (r <= a || b <= l) return; if (a <= l && r <= b) { double len = sqrt(dat[k].first * dat[k].first + dat[k].second * dat[k].second); dat[k].first *= (len + z) / len; dat[k].second *= (len + z) / len; } else { int m = (l + r) / 2; update(a, b, (k * 2 + 1), l, m, z); update(a, b, (k * 2 + 2), m, r, z); dat[k] = combine(dat[(k * 2 + 1)], dat[(k * 2 + 2)]); } } void update(int a, int z) { update(a, a + 1, 0, 0, n, z); } pair<double, double> query(int a, int b) { return query(a, b + 1, 0, 0, n); } pair<double, double> query(int a, int b, int k, int l, int r) { if (r <= a || b <= l) return make_pair(0.0, 0.0); propagate(k, l, r); if (a <= l && r <= b) { return dat[k]; } else { pair<double, double> vl = query(a, b, (k * 2 + 1), l, (l + r) / 2); pair<double, double> vr = query(a, b, (k * 2 + 2), (l + r) / 2, r); return combine(vl, vr); } } pair<double, double> combine(pair<double, double> vl, pair<double, double> vr) { return make_pair(vl.first + vr.first, vl.second + vr.second); } }; int main() { int N, M; scanf( %d , &N), scanf( %d , &M); SegmentTree st(N + 1); for (int i = 0; i < (M); i++) { int x; scanf( %d , &x); if (x == 2) { int y, z; scanf( %d , &y), scanf( %d , &z); st.update(y, N, z * 1.0 / 360 * 2 * pi); } else { int y, z; scanf( %d , &y), scanf( %d , &z); st.update(y, z); } auto ans = st.query(1, N); printf( %.10f %.10f n , ans.first, ans.second); } } |
#include <bits/stdc++.h> using namespace std; int z[150005]; long long tir[150005]; int find(int a) { if (a != z[a]) { z[a] = find(z[a]); } return z[a]; } void mix(int a, int b) { z[find(a)] = find(b); } int main() { int n, m; scanf( %d%d , &n, &m); for (int i = 1; i <= n; i++) { z[i] = i; tir[i] = 0; } for (int j = 0; j < m; j++) { int a, b; scanf( %d%d , &a, &b); mix(a, b); } for (int i = 1; i <= n; i++) { tir[find(i)]++; } long long sum = 0; for (int i = 1; i <= n; i++) { sum += ((tir[i] * (tir[i] - 1)) / 2); } if (sum == m) puts( YES ); else puts( NO ); return 0; } |
#include <bits/stdc++.h> using namespace std; using vi = vector<long long>; template <typename T> std::istream& operator>>(std::istream& input, std::pair<T, T>& data) { input >> data.first >> data.second; return input; } template <typename T> std::istream& operator>>(std::istream& input, std::vector<T>& data) { for (T& first : data) input >> first; return input; } template <typename T> std::ostream& operator<<(std::ostream& output, const pair<T, T>& data) { output << ( << data.first << , << data.second << ) ; return output; } template <typename T> std::ostream& operator<<(std::ostream& output, const std::vector<T>& data) { for (const T& first : data) output << first << ; return output; } std::ostream& operator<<(std::ostream& output, const __int128& first) { __int128 n = first; if (n == 0) { output << 0 ; return output; } if (n < 0) { n = -n; output << - ; } string s; while (n) { s += 0 + (n % 10); n /= 10; } reverse(s.begin(), s.end()); cout << s; return output; } long long div_up(long long a, long long b) { return a / b + ((a ^ b) > 0 && a % b); } long long div_down(long long a, long long b) { return a / b - ((a ^ b) < 0 && a % b); } long long math_mod(long long a, long long b) { return a - b * div_down(a, b); } template <class T> using V = vector<T>; template <class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; } template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; } long long gcd(long long a, long long b) { while (b) { tie(a, b) = make_pair(b, a % b); } return a; } long long Bit(long long mask, long long bit) { return (mask >> bit) & 1; } template <long long MOD, long long RT> struct mint { static const long long mod = MOD; static constexpr mint rt() { return RT; } long long v; explicit operator long long() const { return v; } mint() { v = 0; } mint(long long _v) { v = (long long)((-MOD < _v && _v < MOD) ? _v : _v % MOD); if (v < 0) v += MOD; } friend bool operator==(const mint& a, const mint& b) { return a.v == b.v; } friend bool operator!=(const mint& a, const mint& b) { return !(a == b); } friend bool operator<(const mint& a, const mint& b) { return a.v < b.v; } friend string ts(mint a) { return to_string(a.v); } mint& operator+=(const mint& m) { if ((v += m.v) >= MOD) v -= MOD; return *this; } mint& operator-=(const mint& m) { if ((v -= m.v) < 0) v += MOD; return *this; } mint& operator*=(const mint& m) { v = (long long)((long long)v * m.v % MOD); return *this; } mint& operator/=(const mint& m) { return (*this) *= inv(m); } friend mint pow(mint a, long long p) { mint ans = 1; assert(p >= 0); for (; p; p /= 2, a *= a) if (p & 1) ans *= a; return ans; } mint& operator^=(const long long& p) { return (*this) = pow(this, p); } friend mint inv(const mint& a) { assert(a.v != 0); return pow(a, MOD - 2); } mint operator-() const { return mint(-v); } mint& operator++() { return *this += 1; } mint& operator--() { return *this -= 1; } friend mint operator+(mint a, const mint& b) { return a += b; } friend mint operator-(mint a, const mint& b) { return a -= b; } friend mint operator*(mint a, const mint& b) { return a *= b; } friend mint operator/(mint a, const mint& b) { return a /= b; } friend mint operator^(mint a, const long long p) { return pow(a, p); } }; const long long MOD = 1e9 + 7; typedef mint<MOD, 5> mi; std::ostream& operator<<(std::ostream& o, const mi& a) { cout << a.v; return o; } vector<vector<mi> > scmb; void genComb(long long SZ) { scmb.assign(SZ, vector<mi>(SZ)); scmb[0][0] = 1; for (long long i = (1); i < (SZ); ++i) for (long long j = (0); j < (i + 1); ++j) scmb[i][j] = scmb[i - 1][j] + (j ? scmb[i - 1][j - 1] : 0); } vector<mi> invs, fac, ifac; void genFac(long long SZ) { invs.resize(SZ), fac.resize(SZ), ifac.resize(SZ); invs[1] = fac[0] = ifac[0] = 1; for (long long i = (2); i < (SZ); ++i) invs[i] = mi(-(long long)MOD / i * (long long)invs[MOD % i]); for (long long i = (1); i < (SZ); ++i) { fac[i] = fac[i - 1] * i; ifac[i] = ifac[i - 1] * invs[i]; } } mi comb(long long a, long long b) { if (a < b || b < 0) return 0; assert(a < fac.size()); return fac[a] * ifac[b] * ifac[a - b]; } const long long N = 507; long long link[N]; const long long K = 1 << 16; void ANDConvolution(mi v[K]) { for (long long step = K; step > 1; step /= 2) { for (long long start = 0; start < K; start += step) { for (long long w = 0; w < step / 2; w++) { v[start + w] += v[start + w + step / 2]; } } } } void inverseANDConvolution(mi v[K]) { for (long long step = K; step > 1; step /= 2) { for (long long start = 0; start < K; start += step) { for (long long w = 0; w < step / 2; w++) { v[start + w] -= v[start + w + step / 2]; } } } } void ORConvolution(mi v[K]) { for (long long step = K; step > 1; step /= 2) { for (long long start = 0; start < K; start += step) { for (long long w = 0; w < step / 2; w++) { v[start + step / 2 + w] += v[start + w]; } } } } void inverseORConvolution(mi v[K]) { for (long long step = K; step > 1; step /= 2) { for (long long start = 0; start < K; start += step) { for (long long w = 0; w < step / 2; w++) { v[start + step / 2 + w] -= v[start + w]; } } } } mi dp[N][1 << 16]; string s; mi A[K], B[K], C[K]; long long ptr = 0; long long solve(long long l, long long r) { long long v = ptr; ptr++; if (l == r) { long long who = -1, add = -1; if (s[l] == a ) { who = 0; add = 1; } else if (s[l] == A ) { who = 0; add = 0; } else if (s[l] == b ) { who = 1; add = 1; } else if (s[l] == B ) { who = 1; add = 0; } else if (s[l] == c ) { who = 2; add = 1; } else if (s[l] == C ) { who = 2; add = 0; } else if (s[l] == d ) { who = 3; add = 1; } else if (s[l] == D ) { who = 3; add = 0; } else { assert(s[l] == ? ); } if (who != -1) { long long foo = 0; for (long long mask = (0); mask < (16); ++mask) { if (Bit(mask, who) ^ add) { foo ^= 1 << mask; } } dp[v][foo] += 1; } else { for (long long who = (0); who < (4); ++who) { for (long long add = (0); add < (2); ++add) { long long foo = 0; for (long long mask = (0); mask < (16); ++mask) { if (Bit(mask, who) ^ add) { foo ^= 1 << mask; } } dp[v][foo] += 1; } } } return v; } long long L = solve(l + 1, link[l] - 1); long long nextb = link[l] + 2; long long R = solve(nextb + 1, link[nextb] - 1); if (s[nextb - 1] != & ) { for (long long i = (0); i < (K); ++i) { A[i] = dp[L][i]; B[i] = dp[R][i]; } ORConvolution(A); ORConvolution(B); for (long long i = 0; i < K; i++) { C[i] = A[i] * B[i]; } inverseORConvolution(C); for (long long i = (0); i < (K); ++i) { dp[v][i] += C[i]; } } if (s[nextb - 1] != | ) { for (long long i = (0); i < (K); ++i) { A[i] = dp[L][i]; B[i] = dp[R][i]; } ANDConvolution(A); ANDConvolution(B); for (long long i = 0; i < K; i++) { C[i] = A[i] * B[i]; } inverseANDConvolution(C); for (long long i = (0); i < (K); ++i) { dp[v][i] += C[i]; } } return v; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> s; long long len = s.size(); vi sk; for (long long i = (0); i < (len); ++i) { if (s[i] == ( ) { sk.push_back(i); } else if (s[i] == ) ) { assert(sk.size()); link[sk.back()] = i; link[i] = sk.back(); sk.pop_back(); } } long long root = solve(0, len - 1); long long q; cin >> q; long long want = 0; long long know = 0; for (long long _ = (0); _ < (q); ++_) { long long bit = 0; for (long long i = (0); i < (4); ++i) { long long first; cin >> first; if (first) { bit ^= 1 << i; } } know ^= 1 << bit; long long res; cin >> res; if (res) { want ^= 1 << bit; } } mi ans = 0; for (long long mask = (0); mask < (1 << 16); ++mask) { bool ok = 1; for (long long bit = (0); bit < (16); ++bit) { if (Bit(know, bit)) { ok &= Bit(want, bit) == Bit(mask, bit); } } if (ok) { ans += dp[root][mask]; } } cout << ans << n ; } |
#include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, t; cin >> n >> t; string s; cin >> s; while (t--) { for (long long j = 1; j < n; j++) { if (s[j - 1] == B && s[j] == G ) { swap(s[j - 1], s[j]); j++; } } } cout << s << endl; ; } |
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; void print(int base) { if (base == 4) cout << 4 * 3 = 12 n12 * 2 = 24 n24 * 1 = 24 << endl; if (base == 5) cout << 5 * 4 = 20 n3 + 2 = 5 n5 - 1 = 4 n20 + 4 = 24 << endl; if (base == 6) cout << 4 * 3 = 12 n12 * 2 = 24 n5 + 1 = 6 n6 - 6 = 0 n24 + 0 = 24 << endl; if (base == 7) cout << 5 * 3 = 15 n15 + 7 = 22 n2 - 1 = 1 n22 * 1 = 22 n6 - 4 = 2 n22 + 2 = 24 << endl; } void solve(int n, int base) { print(base); if (n == base) return; cout << base + 1 << + << base + 4 << = << 2 * base + 5 << n ; cout << base + 2 << + << base + 3 << = << 2 * base + 5 << n ; long long sm1 = 2 * base + 5, sm2 = 2 * base + 5, cs = 0; for (int i = base + 5; i <= n; i += 2) { if (cs) { cout << sm1 << + << i << = << sm1 + i << n ; cout << sm2 << + << i + 1 << = << sm2 + i + 1 << n ; cs = 0; sm1 += i; sm2 += i + 1; } else { cout << sm1 << + << i + 1 << = << sm1 + i + 1 << n ; cout << sm2 << + << i << = << sm2 + i << n ; cs = 1; sm1 += i + 1; sm2 += i; } } cout << sm1 << - << sm2 << = << 0 n ; cout << 24 + 0 = 24 n ; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin >> n; if (n < 4) return puts( NO ), 0; cout << YES << endl; for (int i = 4; i <= 7; ++i) { if ((((n - i) / 2) % 2 == 0) && (n - i) % 2 == 0) solve(n, i); } } |
#include <bits/stdc++.h> using namespace std; int arr[105]; int main() { int n; cin >> n; int pos = 0; int neg = 0; int z = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; if (arr[i] > 0) pos++; else if (arr[i] < 0) neg++; else z++; } int pas = 0; if (n % 2 == 0) pas = n / 2; else pas = (n / 2) + 1; int maxi = pos; if (neg > pos) maxi = neg; if (maxi >= pas && maxi == pos) { cout << 1 << endl; } else if (maxi >= pas && maxi == neg) { cout << -1 << endl; } else { cout << 0 << endl; } return 0; } |
#include <bits/stdc++.h> using namespace std; int a[500][500]; int d[500][500]; long long int ans[500]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { int num; cin >> num; a[i][j] = num; d[i][j] = num; } } int b[n]; for (int i = 0; i < n; i++) { cin >> b[i]; b[i]--; } reverse(b, b + n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int s = 0; s < n; s++) { d[j][s] = min(d[j][s], d[j][b[i]] + d[b[i]][s]); } } for (int j = 0; j <= i; j++) { for (int s = 0; s <= i; s++) { ans[i] += d[b[j]][b[s]]; } } } reverse(ans, ans + n); for (int i = 0; i < n; i++) cout << ans[i] << ; return 0; } |
#include <bits/stdc++.h> using namespace std; struct trie { int s, x[2], w[30]; } t[50005 * 30]; int N, S, a[50005], p[50005]; long long M; void ins(int x) { for (int i = 30 - 1, c, p = 1; c = (x >> i) & 1, i >= 0; i--) { if (!t[p].x[c]) t[p].x[c] = ++S; p = t[p].x[c], t[p].s++; for (int j = 0; j <= i; j++) t[p].w[j] += (x >> j) & 1; } } void init() { scanf( %d%lld , &N, &M), M *= 2, S++; for (int i = 1; i <= N; i++) scanf( %d , &a[i]), ins(a[i]), p[i] = 1; } void doit() { long long ans = 0, W = 0; for (int i = 30 - 1; M && i >= 0; i--) { long long s = 0; for (int j = 1; j <= N; j++) s += t[t[p[j]].x[!(a[j] >> i & 1)]].s; if (s <= M) { for (int j = 1, c, q; c = (a[j] >> i) & 1, q = p[j], j <= N; p[j++] = t[q].x[c]) if (q) { for (int k = 0; k <= i; k++) ans += 1ll * ((a[j] >> k) & 1 ? t[t[q].x[!c]].s - t[t[q].x[!c]].w[k] : t[t[q].x[!c]].w[k]) << k; ans += 1ll * t[t[q].x[!c]].s * W; } M -= s; } else for (int j = 1; j <= N; j++) p[j] = t[p[j]].x[!(a[j] >> i & 1)], W |= 1 << i; } printf( %d n , (ans + M * W) / 2 % 1000000007); } int main() { init(); doit(); return 0; } |
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; int ma[maxn]; int en[maxn]; int add = 0; int main() { std::ios::sync_with_stdio(false); int n, d; cin >> n >> d; int sum = 0; int mx = -40000000; int op = 0; for (int i = 0; i < n; i++) { int in; cin >> in; if (in != 0) { sum += in; mx = max(mx, sum); if (sum > d) { cout << -1 << endl; return 0; } } else { if (sum >= 0) continue; ma[op] = mx; en[op++] = sum; sum = 0; mx = -400000000; } } int cnt = 0; for (int i = 0; i < op; i++) { if (min(add, d - ma[i]) < abs(en[i])) { cnt++; add = d; } else { add = min(add, d - ma[i]) - abs(en[i]); } } cout << cnt << endl; } |
#include <bits/stdc++.h> using namespace std; int main() { vector<vector<int> > field; int x, y; cin >> x >> y; field.resize(x); for (int i = 0; i < x; ++i) field[i].resize(y); for (int i = 0; i < x; ++i) for (int j = 0; j < y; ++j) field[i][j] = 0; int x0, y0; cin >> x0 >> y0; x0 -= 1; y0 -= 1; field[x0][y0] = 1; int count = x * y - 1; string s; cin >> s; cout << 1 ; for (unsigned int i = 0; i < s.size() - 1; ++i) { if (s[i] == U && x0 != 0) { x0 -= 1; if (field[x0][y0] != 1) { field[x0][y0] = 1; --count; cout << 1 ; } else cout << 0 ; } else if (s[i] == D && x0 != x - 1) { x0 += 1; if (field[x0][y0] != 1) { field[x0][y0] = 1; --count; cout << 1 ; } else cout << 0 ; } else if (s[i] == L && y0 != 0) { y0 -= 1; if (field[x0][y0] != 1) { field[x0][y0] = 1; --count; cout << 1 ; } else cout << 0 ; } else if (s[i] == R && y0 != y - 1) { y0 += 1; if (field[x0][y0] != 1) { field[x0][y0] = 1; --count; cout << 1 ; } else cout << 0 ; } else cout << 0 ; } cout << count; return 0; } |
#include <bits/stdc++.h> using namespace std; long long n, a[+100500 * 2], ans; int main() { cin >> n; for (int i = 1; i <= n; i++) { scanf( %I64d , &a[i]); } for (int i = 1; i <= n; i++) { ans += abs(a[i] - a[i - 1]); } printf( %I64d , ans); return 0; ; } |
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> v(n); for (int i = 0; i < n; i++) cin >> v[i]; string str; getline(cin, str); bool isok = true; for (int i = 0; i < n; i++) { getline(cin, str); int x = 0; for (int j = 0; j < str.length(); j++) { if (str[j] == a || str[j] == e || str[j] == i || str[j] == o || str[j] == u || str[j] == y ) x++; } if (x != v[i]) { cout << NO << endl; return 0; } } cout << YES << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; void solve() { long long a, b; cin >> a >> b; vector<string> A; for (long long i = 0; i < a; i++) { string res = ; for (long long j = 0; j < b; j++) { if (i % 4 == 0 || i % 4 == 2) { res += # ; } else if (i % 4 == 1 || i % 4 == 3) { res += . ; } } if (i % 4 == 1) res[res.length() - 1] = # ; else if (i % 4 == 3) res[0] = # ; A.push_back(res); } for (auto i : A) { cout << i << n ; } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long tt(1); while (tt--) { solve(); } } |
#include <bits/stdc++.h> using namespace std; long long pow(long long a, long long b) { if (b == 0) { return 1; } if (b % 2 == 0) { return pow((a % 1000000007) * (a % 1000000007), b / 2) % 1000000007; } else { return (a % 1000000007) * (pow((a % 1000000007) * (a % 1000000007), (b - 1) / 2) % 1000000007); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int i, j, k, n; cin >> n >> k; double x[n], y[n]; for (i = 0; i < n; i++) { cin >> x[i] >> y[i]; } double ans = 0.000000000; for (i = 1; i < n; i++) { ans += sqrt((x[i] - x[i - 1]) * (x[i] - x[i - 1]) + (y[i] - y[i - 1]) * (y[i] - y[i - 1])); } double f = (ans * double(k)) / (50.0000000000); cout << fixed << setprecision(10) << f << n ; } |
#include <bits/stdc++.h> const long long P = 1000000007; long long N, oldA, oldB, oldC, oldD = 1, newA, newB, newC, newD; int main() { std::cin >> N; for (int i = 1; i <= N; ++i) { newA = (oldB + oldC + oldD) % P; newB = (oldA + oldC + oldD) % P; newC = (oldA + oldB + oldD) % P; newD = (oldA + oldB + oldC) % P; oldA = newA; oldB = newB; oldC = newC; oldD = newD; } std::cout << newD; } |
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; long long a[N][26]; int b[N]; long long ans[28]; char s[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n, m; cin >> n >> m; cin >> (s + 1); for (int i = 0; i <= n; i++) { for (int j = 0; j <= 26; j++) { a[i][j] = 0; } } for (int i = 0; i < 26; i++) ans[i] = 0; for (int i = 0; i < m; i++) cin >> b[i]; for (int i = 1; i <= n; i++) { int d = s[i] - a ; a[i][d]++; } for (int i = 0; i < 26; i++) { for (int j = 2; j <= n; j++) { a[j][i] = a[j - 1][i] + a[j][i]; } } for (int i = 0; i < m; i++) { int k = b[i]; for (int j = 0; j < 26; j++) { ans[j] += a[k][j]; } } for (int i = 0; i < 26; i++) { ans[i] += a[n][i]; } for (int i = 0; i < 26; i++) { if (i) cout << ; cout << ans[i]; } cout << endl; } return 0; } |
#pragma GCC optimize( Ofast ) #pragma GCC target( avx,avx2,fma ) #pragma GCC optimization ( O3 ) #pragma GCC optimization ( unroll-loops ) #include<bits/stdc++.h> using namespace std; #define pb push_back #define mp make_pair #define F first #define S second #define lb lower_bound #define ub upper_bound #define sz(x) (int)(x).size() #define all(x) x.begin(), x.end() typedef long long ll; typedef long double ld; typedef complex<ld> cd; typedef pair<int, int> pii; typedef pair<ll,ll> pll; typedef pair<ld,ld> pld; typedef vector<int> vi; typedef vector<ld> vd; typedef vector<ll> vl; typedef vector<pii> vpi; typedef vector<pll> vpl; #define FOR(i, a, b) for (int i=a; i<(b); i++) #define F0R(i, a) for (int i=0; i<(a); i++) #define FORd(i,a,b) for (int i = (b)-1; i >= a; i--) #define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--) #define trav(a,x) for (auto& a : x) mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); ll powmod(ll a,ll b) {ll res=1; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a;a=a*a;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} int n,d; void solve() { int n; string a,b; cin >> n >> a >> b; if(a==b) { cout << b << n ; return ; } if(a[0] == 0 ) { F0R(i,n) cout << 1 ; cout<< n ; return; } if(b.back() == 1 || a == b) { cout << b << n ; return ; } { int l = n-1; while(a[l] == 1 ) --l; a[l]= 1 ; FOR(i,l+1,n) a[i]= 0 ; } if(a != b) b[n-1] = 1 ; { cout << b << n ; return ; } } int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); #ifndef ONLINE_JUDGE freopen( input.txt , r ,stdin); freopen( output.txt , w ,stdout); #endif int t=1,Tc=1; while(t--){ //cout << Case # <<Tc<< : ; Tc++; solve(); } return 0; } |
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) using namespace std; template <typename T1, typename T2> void ckmin(T1 &a, T2 b) { if (a > b) a = b; } template <typename T1, typename T2> void ckmax(T1 &a, T2 b) { if (a < b) a = b; } template <typename T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; } int read() { int x = 0, f = 0; char ch = getchar(); while (!isdigit(ch)) f |= ch == - , ch = getchar(); while (isdigit(ch)) x = 10 * x + ch - 0 , ch = getchar(); return f ? -x : x; } const int mod = 1000000007; const int N = 2005; struct fuck { int x, y, b; } a[N]; int sum[N][N]; int n, m, R, t = 1000; int fac[N], ifac[N]; int qpow(int a, int b = mod - 2) { int res = 1; while (b > 0) { if (b & 1) res = 1ll * res * a % mod; a = 1ll * a * a % mod; b >>= 1; } return res; } void init(int n) { fac[0] = ifac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = 1ll * fac[i - 1] * i % mod; ifac[n] = qpow(fac[n], mod - 2); for (int i = n - 1; i >= 1; i--) ifac[i] = 1ll * ifac[i + 1] * (i + 1) % mod; } int C(int n, int m) { if (n < m || n < 0) return 0; else return 1ll * fac[n] * ifac[n - m] % mod * ifac[m] % mod; } int calc1(int xd1, int yd1, int xd2, int yd2) { return sum[xd2][yd2] - sum[xd1 - 1][yd2] - sum[xd2][yd1 - 1] + sum[xd1 - 1][yd1 - 1]; } int main() { init(2000); n = read(), m = read(), R = read(); for (int i = 1; i <= n; i++) { a[i].x = read(), a[i].y = read(), a[i].b = read(); sum[a[i].x][a[i].y]++; } for (int i = 1; i <= t; i++) { for (int j = 1; j <= t; j++) { sum[i][j] += sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1]; } } long long ans = 0; for (int i = 1; i <= n; i++) { int xd1 = max(1, a[i].x - R), yd1 = max(1, a[i].y - R); int xd2 = min(t, a[i].x + R), yd2 = min(t, a[i].y + R); int in_dot = calc1(xd1, yd1, xd2, yd2); ans = (ans + 1ll * (C(n, m) - C(n - in_dot, m) + mod) * a[i].b % mod * a[i].b % mod) % mod; } for (int i = 1; i <= n; i++) { for (int j = i + 1; j <= n; j++) { int xd1 = max(1, a[i].x - R), yd1 = max(1, a[i].y - R); int xd2 = min(t, a[i].x + R), yd2 = min(t, a[i].y + R); int in_dot = calc1(xd1, yd1, xd2, yd2); int kase = C(n, m); kase = (kase - C(n - in_dot, m) + mod) % mod; int x3 = max(1, a[j].x - R), y3 = max(1, a[j].y - R); int x4 = min(t, a[j].x + R), y4 = min(t, a[j].y + R); int in_dot2 = calc1(x3, y3, x4, y4); kase = (kase - C(n - in_dot2, m) + mod) % mod; int x5 = max(xd1, x3), y5 = max(yd1, y3); int x6 = min(xd2, x4), y6 = min(yd2, y4); int in_dot3 = 0; if (x5 <= x6 && y5 <= y6) { in_dot3 = calc1(x5, y5, x6, y6); } int in = in_dot + in_dot2 - in_dot3; kase = (kase + C(n - in, m)) % mod; ans = (ans + 1ll * 2 * a[i].b % mod * a[j].b % mod * kase % mod) % mod; } } printf( %lld n , (ans % mod + mod) % mod); return 0; } |
#include <bits/stdc++.h> using namespace std; int solve(string second, char cur) { if (second.size() == 1) { return !(second[0] == cur); } int n = second.size(); int check = 0; for (int i = 0; i < n / 2; i++) { check += (second[i] == cur); } int check2 = 0; for (int i = n / 2; i < n; i++) { check2 += (second[i] == cur); } int a = solve(second.substr(n / 2, n / 2), cur + 1) + n / 2 - check; int b = solve(second.substr(0, n / 2), cur + 1) + n / 2 - check2; return min(a, b); } int main() { std::ios::sync_with_stdio(false); int T; cin >> T; int n; string second; while (T--) { cin >> n; cin >> second; cout << solve(second, a ) << n ; } return 0; } |
#include <bits/stdc++.h> using namespace std; long long INF = 0x7FFFFFFFFFFFFFFF; long long binpow(long long a, long long b) { long long res = 1; while (b > 0) { if (b & 1) res = res * a; a = a * a; b >>= 1; } return res; } long long ask(vector<long long>& v) { cout << ? << (long long int)(v).size() << ; for (long long i = 0; i < (long long int)(v).size(); ++i) cout << v[i] << ; cout << n << flush; long long res; cin >> res; return res; } vector<long long> comp(vector<long long>& st, long long n) { vector<bool> pres(n + 1, 0); for (long long i = 0; i < (long long int)(st).size(); ++i) pres[st[i]] = 1; vector<long long> res; for (long long i = 1; i < n + 1; ++i) { if (!pres[i]) res.push_back(i); } return res; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long T; cin >> T; while (T--) { long long n, k; cin >> n >> k; vector<vector<long long> > st(k); for (long long i = 0; i < k; ++i) { long long c; cin >> c; st[i].resize(c); for (long long j = 0; j < c; ++j) cin >> st[i][j]; } vector<long long> a(n); iota((a).begin(), (a).end(), 1); long long mx = ask(a); long long l = 0, r = k - 1; while (l < r) { long long md = (l + r) / 2; a.clear(); for (long long i = 0; i < md + 1; ++i) { for (long long j = 0; j < (long long int)(st[i]).size(); ++j) a.push_back(st[i][j]); } long long m = ask(a); if (m == mx) r = md; else l = md + 1; } vector<long long> ans(k, mx); vector<long long> c = comp(st[l], n); long long m = ask(c); ans[l] = m; cout << ! ; for (long long i = 0; i < k; ++i) cout << ans[i] << ; cout << n << flush; string strl; cin >> strl; } } |
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool smin(T &a, const T &b) { return b < a ? a = b, 1 : 0; } template <typename T> inline bool smax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } const long long N = (long long)1e6 + 10; long long n, ar[N], m, bl[N], br[N], pre[N], fen[N], res[N]; vector<long long> qs[N]; map<long long, long long> last; long long get_xor(long long l, long long r) { if (!l) return pre[r]; return pre[r] ^ pre[l - 1]; } void add(long long i, long long val) { for (; i < N; i += (i & -i)) fen[i] ^= val; } long long get(long long i) { long long ret = 0; for (; i; i ^= (i & -i)) ret ^= fen[i]; return ret; } void add(long long l, long long r, long long val) { add(r, val); if (l > 1) add(l - 1, val); } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; for (long long i = 1; i <= n; ++i) { cin >> ar[i]; if (i) pre[i] = pre[i - 1]; pre[i] ^= ar[i]; } cin >> m; for (long long j = 0; j < m; ++j) { cin >> bl[j] >> br[j]; qs[br[j]].push_back(j); } for (long long j = 1; j <= n; ++j) last[ar[j]] = -1; for (long long i = 1; i <= n; ++i) { long long x = ar[i]; if (last[x] == -1) add(1, i, x); else add(last[x] + 1, i, x); last[x] = i; for (long long p : qs[i]) { res[p] = get_xor(bl[p], i); res[p] ^= get(i); res[p] ^= get(bl[p] - 1); } } for (long long j = 0; j < m; ++j) cout << res[j] << n ; } |
#include <bits/stdc++.h> using namespace std; const int N = 1000005; const int CS = 22; int cmax[N][CS], cmin[N][CS], ans[N]; int lt[CS]; int main() { int n, k; scanf( %d%d , &n, &k); for (int i = 1; i <= n; i++) { scanf( %d , &cmax[i][0]); cmax[i][0] *= 100; } for (int i = 1; i <= n; i++) scanf( %d , &cmin[i][0]); int cs = 1; for (int g = 1; g < n; g <<= 1, cs++) { int gg = g << 1; for (int i = 1; i <= n; i++) { if (i + gg - 1 > n) cmax[i][cs] = cmin[i][cs] = 0; else { cmax[i][cs] = max(cmax[i][cs - 1], cmax[i + g][cs - 1]); cmin[i][cs] = min(cmin[i][cs - 1], cmin[i + g][cs - 1]); } } } lt[0] = 1; for (int i = 1; i < cs; i++) lt[i] = lt[i - 1] << 1; for (int i = 1; i <= n; i++) { ans[i] = 0; int tmax = 0, tmin = 10000000; int mid = i; for (int j = cs - 1; j >= 0; j--) { if (cmax[mid][j] > 0) { int tmax2 = max(tmax, cmax[mid][j]); int tmin2 = min(tmin, cmin[mid][j]); ans[i] = max(ans[i], min(tmax2, tmin2)); if (tmax2 <= tmin2) { tmax = tmax2; tmin = tmin2; if (tmax == tmin) break; mid += lt[j]; if (mid > n) break; } } } } sort(ans + 1, ans + n + 1); double p = 1.0; double res = 0; for (int i = 1; i <= n - k + 1; i++) { if (p < 1e-20) break; double pp = k * 1.0 / (n - i + 1); res += p * pp * ans[i]; p *= (1 - pp); } printf( %.12f , res); return 0; } |
#include <bits/stdc++.h> using namespace std; bool check(string s) { for (int i = 0; i < s.size() / 2; i++) if (s[i] != s[s.size() - 1 - i]) return 0; return 1; } int main() { int jopa; string s; cin >> s; int p = 0; char a = s[0]; for (int i = 0; i < s.size(); i++) if (s[i] == a) p++; if (p >= s.size() - 1) { cout << Impossible ; return 0; } int n = s.size(); s = s + s; for (int i = 1; i < n; i++) { if (i + n - 1 >= s.size()) break; if (check(s.substr(i, n)) && s.substr(i, n) != s.substr(0, n)) { cout << 1; return 0; } } cout << 2; return 0; } |
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; int main() { string s; cin >> s; int k = 0, c = 0; for (int i = s.size() - 1; i >= 0; i--) { if (s[i] == b ) c++; else { k += c; c *= 2; k %= MOD; c %= MOD; } } cout << k << n ; return 0; } |
#include <bits/stdc++.h> long long t1[100], t2[100], n, k, m, i, j, mod[10], o, s; int main() { scanf( %I64d %I64d %I64d , &n, &k, &m); t1[0] = 1; for (i = 0; i < 10; i++) mod[i] = i % k; for (i = 1; i <= n; i++) { for (j = 0; j < 10; j++) { if (j == 0 && (i == n || i == 1)) continue; for (o = 0; o < k; o++) { t2[(o + mod[j]) % k] += t1[o]; } } if (i != 1) { for (j = 1; j < 10; j++) { t2[mod[j]]++; } } s += t2[0]; t2[0] = 0; t1[0] = 0; if (i != n) { if (i == n - 1) s *= 9; else s *= 10; s %= m; } for (j = 1; j < k; j++) { t1[j] = t2[j] % m; t2[j] = 0; } for (j = 0; j < 10; j++) mod[j] = (mod[j] * 10) % k; } s %= m; printf( %I64d n , s); return 0; } |
#include <bits/stdc++.h> using namespace std; signed main() { long long n; cin >> n; long long nu[n]; for (long long i = 0; i < n; ++i) { char a; cin >> a; nu[i] = a - 0 ; } long long f[10]; for (long long i = 0; i < 9; ++i) cin >> f[i + 1]; for (long long i = 0; i < n; ++i) { if (f[nu[i]] > nu[i]) { long long dab = i; for (dab = i; dab < n && f[nu[dab]] >= nu[dab]; ++dab) { nu[dab] = f[nu[dab]]; } break; } } for (long long i = 0; i < n; ++i) { cout << nu[i]; } cout << endl; return 0; } |
#include <bits/stdc++.h> using namespace std; int const maxn = 100100; long long a[maxn]; set<long long> s; int main() { int n, k; long long x1, x2, x3, x4; while (~scanf( %d%d , &n, &k)) { for (int i = 0; i < n; ++i) { scanf( %I64d.%I64d.%I64d.%I64d , &x1, &x2, &x3, &x4); a[i] = (x1 << 24) | (x2 << 16) | (x3 << 8) | x4; } long long ret = -1; for (int i = 31; i >= 0; --i) { long long t = (1LL << 32) - (1LL << i); s.clear(); for (int j = 0; j < n; ++j) { s.insert(t & a[j]); } if (s.size() == k) { ret = t; break; } } if (ret == -1) puts( -1 ); else { printf( %I64d.%I64d.%I64d.%I64d n , ret >> 24, (ret >> 16) & 255, (ret >> 8) & 255, ret & 255); } } return 0; } |
#include <bits/stdc++.h> using namespace std; void solve() { long long n; cin >> n; vector<long long> vec(n, 0); for (long long i = 0; i < n; i++) cin >> vec[i]; long long mn = 10e9 + 1, mx = -1, count = 0; for (long long i = 1; i < n; i++) { if (vec[i] == -1 && vec[i - 1] != -1) { mn = min(mn, vec[i - 1]); mx = max(mx, vec[i - 1]); } } for (long long i = 0; i < n - 1; i++) { if (vec[i] == -1 && vec[i + 1] != -1) { mn = min(mn, vec[i + 1]); mx = max(mx, vec[i + 1]); } } if (mx == -1) { cout << 0 << << 0 << endl; } else { long long k = (mx + mn) / 2; for (long long i = 0; i < n; i++) { if (vec[i] == -1) { vec[i] = k; } } long long dif = 0; for (long long i = 1; i < n; i++) { dif = max(dif, abs(vec[i] - vec[i - 1])); } cout << dif << << k << endl; } } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t-- > 0) { solve(); } return 0; } |
#include <bits/stdc++.h> #define ll long long #define f(i, n) for (i = 0; i < n; ++i) #define all(n) n.begin(), n.end() #define P long(1e9 + 7) #define fastio ios_base::sync_with_stdio(false); cin.tie(NULL) using namespace std; ll solve(vector<vector<ll>>& dp, vector<ll>& s, int l, int r) { if (l == r) return dp[l][r] = 0; if (dp[l][r] == -1) { ll val = s[r] - s[l]; val += min(solve(dp, s, l + 1, r), solve(dp, s, l, r - 1)); return dp[l][r] = val; } return dp[l][r]; } int main() { int n, i, j, k, l, m; cin >> n; vector<ll> s(n, 0); vector<vector<ll>> dp(n, vector<ll>(n, -1)); f(i, n) cin >> s[i]; sort(all(s)); cout << solve(dp, s, 0, n - 1); } |
#include <bits/stdc++.h> template <typename T> void rd(T& x) { long long f = 0, c; while ((c = getchar()) < 48 || 57 < c) f ^= !(c ^ 45); x = (c & 15); while (47 < (c = getchar()) && c < 58) x = x * 10 + (c & 15); if (f) x = -x; } template <typename T> inline void pt(T x) { if (x < 0) x = -x, putchar( - ); if (x > 9) pt(x / 10); putchar(x % 10 + 48); } template <typename T> inline void pt(T x, long long c) { pt(x), putchar(c); } template <typename T> inline T max(const T& x, const T& y) { return x < y ? y : x; } template <typename T> inline T min(const T& x, const T& y) { return x < y ? x : y; } const long long N = 2005, P = 1000000007; long long n, m, f[2][N][N], sum[N][N]; char s[N][N]; long long getsum(long long a, long long b, long long c, long long d) { if (a > c || b > d) return 0; return sum[c][d] - sum[c][b - 1] - sum[a - 1][d] + sum[a - 1][b - 1]; } long long getf0(long long a, long long b, long long c, long long d) { if (a > c || b > d) return 0; return f[0][c][d] - f[0][c][b - 1] - f[0][a - 1][d] + f[0][a - 1][b - 1]; } long long getf1(long long a, long long b, long long c, long long d) { if (a > c || b > d) return 0; return f[1][c][d] - f[1][c][b - 1] - f[1][a - 1][d] + f[1][a - 1][b - 1]; } signed main() { rd(n), rd(m); for (long long i = (1); i <= (n); ++i) scanf( %s , s[i] + 1); if (s[n][m] == R ) { puts( 0 ); return 0; } if (n == 1 && m == 1) { puts( 1 ); return 0; } for (long long i = (1); i <= (n); ++i) for (long long j = (1); j <= (m); ++j) sum[i][j] = sum[i][j - 1] + sum[i - 1][j] - sum[i - 1][j - 1] + (s[i][j] == R ); f[0][1][1] = 1; f[1][1][1] = 1; for (long long i = (1); i <= (n); ++i) for (long long j = (1); j <= (m); ++j) { long long l = 1, r = i, ans = i; while (l <= r) { long long mid = (l + r) >> 1; if (getsum(mid + 1, j, n, j) <= n - i) ans = mid, r = mid - 1; else l = mid + 1; } (f[1][i][j] += getf0(ans, j, i - 1, j)) %= P; l = 1, r = j, ans = j; while (l <= r) { long long mid = (l + r) >> 1; if (getsum(i, mid + 1, i, m) <= m - j) ans = mid, r = mid - 1; else l = mid + 1; } (f[0][i][j] += getf1(i, ans, i, j - 1)) %= P; (f[0][i][j] += f[0][i][j - 1] + f[0][i - 1][j] - f[0][i - 1][j - 1]) %= P; (f[1][i][j] += f[1][i][j - 1] + f[1][i - 1][j] - f[1][i - 1][j - 1]) %= P; } pt(((getf0(n, m, n, m) + getf1(n, m, n, m)) % P + P) % P, n ); return 0; } |
#include <bits/stdc++.h> using namespace std; string arr[55], arr2[55]; vector<pair<int, int> > hasil; int i, j, n, t, tc, r, x, y, k, c; void tuker_lr(int x, int y) { arr[x][y] = U ; arr[x + 1][y] = D ; arr[x][y + 1] = U ; arr[x + 1][y + 1] = D ; } void tuker_ud(int x, int y) { arr[x][y] = L ; arr[x][y + 1] = R ; arr[x + 1][y] = L ; arr[x + 1][y + 1] = R ; } void l_u_ganjil(int x, int y) { if (arr[x + 1][y] == L ) { tuker_lr(x, y); hasil.push_back(make_pair(x, y)); } else if (arr[x + 1][y + 1] == U ) { tuker_ud(x + 1, y); hasil.push_back(make_pair(x + 1, y)); tuker_lr(x, y); hasil.push_back(make_pair(x, y)); } else { l_u_ganjil(x + 1, y + 1); tuker_ud(x + 1, y); hasil.push_back(make_pair(x + 1, y)); tuker_lr(x, y); hasil.push_back(make_pair(x, y)); } } void u_d_ganjil(int x, int y) { if (arr[x][y + 1] == U ) { tuker_ud(x, y); hasil.push_back(make_pair(x, y)); } else if (arr[x][y + 1] == L ) { l_u_ganjil(x, y + 1); tuker_ud(x, y); hasil.push_back(make_pair(x, y)); } else { u_d_ganjil(x - 1, y + 1); l_u_ganjil(x, y + 1); tuker_ud(x, y); hasil.push_back(make_pair(x, y)); } } int main() { scanf( %d %d , &r, &c); for (i = 0; i < r; i++) cin >> arr[i]; for (i = 0; i < r; i++) cin >> arr2[i]; if (c % 2 == 1) { for (j = 0; j < c; j++) for (i = 0; i < r; i++) if (arr[i][j] == L ) l_u_ganjil(i, j); } else { for (j = 0; j < c; j++) for (i = 0; i < r; i++) if (arr[i][j] == U ) u_d_ganjil(i, j); } for (j = 0; j < c; j++) { for (i = 0; i < r; i++) { if (arr[i][j] == arr2[i][j]) continue; else if (arr[i][j] == U ) u_d_ganjil(i, j); else l_u_ganjil(i, j); } } printf( %d n , hasil.size()); for (i = 0; i < hasil.size(); i++) printf( %d %d n , hasil[i].first + 1, hasil[i].second + 1); } |
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, j; cin >> n >> m; for (i = 1; i <= n; i++) { int c = i / 2; if (i % 2 != 0) { for (j = 0; j < m; j++) { cout << # ; } cout << endl; } else if (c % 2 == 0) { cout << # ; for (j = 1; j < m; j++) { cout << . ; } cout << endl; } else if (c % 2 != 0) { for (j = 1; j < m; j++) { cout << . ; } cout << # << endl; } } return 0; } |
#include <bits/stdc++.h> using namespace std; int main() { int n, a = 0, b = 0; int t; for (int i = 0; i < 3; ++i) { cin >> t; a += t; } a = (a + 4) / 5; for (int i = 0; i < 3; ++i) { cin >> t; b += t; } b = (b + 9) / 10; cin >> n; if (a + b > n) printf( NO ); else printf( YES ); return 0; } |
#include <bits/stdc++.h> using namespace std; struct name { int l, r; } bl[200001]; bool cmp(name n, name m) { if (n.r == m.r) return n.l < m.l; return n.r < m.r; } int main() { int n, x, w, ans = 0; scanf( %d , &n); for (int i = 1; i <= n; i++) { scanf( %d%d , &x, &w); bl[i].l = x - w; bl[i].r = x + w; } sort(bl + 1, bl + n + 1, cmp); int cnt = -2e9; for (int i = 1; i <= n; i++) { if (cnt <= bl[i].l) { ans++; cnt = bl[i].r; } } printf( %d , ans); } |
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int maxn = 1e5 + 5; long long qpow(long long a, long long b, long long r) { long long ans = 1, buff = a % r; while (b) { if (b & 1) ans = (ans * buff) % r; buff = (buff * buff) % r; b >>= 1; } return ans; } long long f_inv(long long a, long long p) { return qpow(a, p - 2, p); } string s; long long k, ans = 0; int main() { ios::sync_with_stdio(false); cin >> s >> k; if (s[0] == - || s[0] == + ) s.erase(s.begin()); long long len = s.length(); for (long long i = 0; i < len; i++) { if (s[i] == 0 || s[i] == 5 ) { ans += qpow(2, i, mod); ans %= mod; } } long long x = qpow(2, len, mod); long long y = qpow(x, k, mod); x--, y--; ans = ans * (y * f_inv(x, mod) % mod) % mod; cout << ans << endl; return 0; } |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.