solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; vector<int> A; int main() { ios::sync_with_stdio(0); cout.tie(0); cin.tie(0); int k, n; while (cin >> n >> k) { if (3 * k > n or k == 1) { cout << "-1\n"; } else { A.assign(n, -1); int j = 2 * k; int p = 0; for (int i = 0; i <...
7
#include <bits/stdc++.h> using namespace std; string s, s2, s3; int n; map<string, int> my; int main() { cin >> n; for (int i = 0; i < n; ++i) { cin >> s; if (my.find(s) == my.end()) { my[s] = 1; } else { my[s] += 1; } } int r = 0; for (map<string, int>::iterator it = my.begin(); i...
4
#include <bits/stdc++.h> using namespace std; using ll = long long; const int N = 1e1 + 4; vector<int> circle; ll vis[N]; bool bad_Relation[N]; int parent[N]; ll chk[N]; int color[N]; int tim[N]; ll dis[N]; int position[N]; vector<pair<ll, ll>> adj[N]; vector<int> adj1[N]; vector<int> graph[N]; bool has_cycle; int maxd...
6
#include <bits/stdc++.h> using namespace std; void redirect_input() { freopen("./input.txt", "r", stdin); } void redirect_output() { freopen("./output.txt", "w", stdout); } const int inf = 0x3f3f3f3f; const int maxn = 2e5 + 100; int a[2], b[2], c[2]; int d[7], ans[7], temp[7], counter[7]; int math(int* arr) { return ar...
19
#include <bits/stdc++.h> using namespace std; int main() { int t = 1; while (t--) { long long int a, b, c, s = 0; cin >> a >> b >> c; s = a + b + c; if (s % 2) cout << "Impossible"; else { if ((b + a - c) >= 0 and (c + b - a) >= 0 and (a + c - b) >= 0) { cout << (b + a - c) /...
4
#include <bits/stdc++.h> using namespace std; long long int MOD = 1E9 + 7; long long int gcd(long long int a, long long int b) { if (b == 0) return a; else return gcd(b, a % b); } long long int gcd_ex(long long int a, long long int b, long long int& x, long long int& y) { if (b == 0) ...
0
#include <bits/stdc++.h> int main() { long long m, n, a, p; scanf("%lld %lld %lld", &m, &n, &a); if (m % a == 0 && n % a == 0) { p = ((m / a)) * ((n / a)); } else if (m % a == 0 && n % a != 0) { p = ((m / a)) * ((n / a) + 1); } else if (m % a != 0 && n % a == 0) { p = ((n / a)) * ((m / a) + 1); ...
2
#include <bits/stdc++.h> using namespace std; const int N = 2010; const long long mod = 1e9 + 7; int n, ent[N], foi[N], dp[N][N]; long long mult(int a, int b) { long long c = 1ll * a * 1ll * b; c %= mod; return c; } int solve(int tot, int caga) { if (caga < 0 || tot < 0 || tot < caga) return 0; if (tot == 0 &...
12
#include <bits/stdc++.h> using namespace std; namespace { template <class T> void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()) { v.assign(a, vector<T>(b, t)); } template <class F, class T> void convert(const F &f, T &t) { stringstream ss; ss << f; ss >> t; } const int INF = 1010101010; int n; c...
16
#include <bits/stdc++.h> using namespace std; int main() { int t = 1; cin >> t; while (t--) { int n, m; cin >> n >> m; char s[n][m]; bool b[n][m]; for (int i = 0; i < n; i++) for (int j = 0; j < m; ++j) { cin >> s[i][j]; b[i][j] = 0; } for (int i = 0; i < n; ++i...
9
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long N = 2e5 + 1; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); int n; cin >> n; vector<long long> h1(n + 1), h2(n + 1); vector<vector<long long>> dp(3, vector<long long>(n + 1)); for (int i = 1; i <= n; i+...
6
#include <bits/stdc++.h> using namespace std; int main() { long n, ct = 0, l; string s; bool test = true; cin >> n; while (n--) { cin >> l >> s; if (s == "South") { if ((ct + l) > 20000) test = false; ct = (l + ct) % 40000; } else if (s == "North") { if (ct - l < 0) test = false;...
5
#include <bits/stdc++.h> int n, m; int a[200100]; int q[200100][2]; int ers[1001000]; int it[1 << 19]; int mod = 1000000007; int ans[200100]; int last[1001000]; struct qry { int loc; int xs, xd; bool operator<(const qry& r) const { return xd < r.xd; } }; qry ql[200100]; long long int mpow(long long int x, long lo...
17
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int a[N]; int main() { int n; cin >> n; for (int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); int ans = 0; int i = 0, j = 0; while (i < n and j < n) { if (a[i] == a[j]) { j++; continue; } if (j == n) bre...
5
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; const long long N = 100011; void solve() { long long n, m; cin >> n >> m; long long mn = 0; long long c[n + 17][m + 17]; for (long long i = 1; i <= n; i++) { long long mx = 1e9; for (long long j = 1; j <= m; j++) { cin >> c[i...
2
#include <bits/stdc++.h> using namespace std; int n; vector<string> v; int main() { cin >> n; v.resize(n); bool cant = false; for (int i = 0; i < n; i++) { cin >> v[i]; if (i != 0 && v[i].size() < v[i - 1].size()) cant = true; } if (v[0][0] == '?') v[0][0] = '1'; if (cant) return cout << "NO", 0; ...
12
#include <bits/stdc++.h> using namespace std; int n, q, x[200005], at; long long y[19][200005], sum, z[19][200005]; int ta, tb; long long f1(int a, int b, int c, int d) { if (((1 << (c)) * (d)) == a && ((1 << (c)) * (d + 1)) == b) return y[c][d]; c--; d = d * 2 + 1; if (b <= ((1 << (c)) * (d))) return f1(a, b, ...
17
#include <bits/stdc++.h> using namespace std; template <typename T, size_t size> ostream &operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; } template <class T> ostream &operator<<(ostream &os, vector<T> V) { os ...
8
#include <bits/stdc++.h> using namespace std; const int N = 100005; long long n, m, k, a[N]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n >> m >> k; for (int i = 1; i <= m; ++i) cin >> a[i]; int res = 0; for (int i = 1; i <= m;) { int j = i; long long curPage = (a[i] - i) / k;...
6
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; long long n, d, m, ans, VAL; long long a[N], par[N]; vector<long long> v1, v2; void solve(); int32_t main() { cin >> n >> d >> m; for (long long i = 0; i < n; i++) { cin >> a[i]; if (a[i] <= m) { ans += a[i]; VAL += a[i];...
10
#include <bits/stdc++.h> using namespace std; int n, m; int a[1100][1100]; int r[1100], c[1100]; int main() { int i, j, k, o = 1; while (~scanf("%d", &n)) { int ans = 0; memset(r, 0, sizeof(r)); memset(c, 0, sizeof(c)); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) scanf("%d", &a[i][j]);...
8
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { ios_base::sync_with_stdio(false); cin.tie(0); int q; cin >> q; while (q--) { ll k, n, a, b; cin >> k >> n >> a >> b; int lo = 0, hi = n; ll ans = -1; while (lo <= hi) { ll mid = lo + ((hi - lo) >> 1); ...
6
#include <bits/stdc++.h> using namespace std; const int NMAX = 20005; vector<int> order(NMAX); int n, m, t, tSt[NMAX], ans; int getSec(string s) { int sec = (int)(s[s.length() - 1] - '0') + 10 * (s[s.length() - 2] - '0'), mns = (int)(s[4] - '0') + 10 * (s[3] - '0'), hrs = (int)(s[1] - '0') + 10 * (s[0] - ...
13
#include <bits/stdc++.h> using namespace std; const int N = 1 << 20, sgn[] = {1, -1}; int n, m, tot, h[N], d[N], a[N], b[N], out[N], vis[N]; struct edge { int t, n; } e[N << 1]; inline void input() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d%d", a + i, b + i); } inline void add_edge(int x, int y) { ...
17
#include <bits/stdc++.h> using namespace std; const long long OO = (long long)(4e18) + 9; const int MOD = (int)(1e9) + 7; const int oo = 2147483647; const double EPS = 1e-8; const double PI = acos(-1.0); int dx[] = {1, 0, -1, 0, 1, -1, 1, -1}; int dy[] = {0, 1, 0, -1, 1, 1, -1, -1}; string abc = "abcdefghijklmnopqrstuv...
7
#include <bits/stdc++.h> using namespace std; using ll = long long; const ll inf = 1e18; const int N = 50 + 10; int res, n, m; char a[N][N]; int pre[N][N]; ll check(int x1, int y1, int x2, int y2) { return pre[x2][y2] - pre[x1 - 1][y2] - pre[x2][y1 - 1] + pre[x1 - 1][y1 - 1]; } void solve() { cin >> n >> m; for (...
7
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int q, n, k; cin >> q; while (q--) { cin >> n >> k; string s; cin >> s; int ans = n, tmp = 0; vector<int> dp(k, 0), cnt(k, 0); for (int i = 0; i < n; i++) { dp[i...
11
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 5; char s[maxn]; int main() { scanf("%s", s); int i = 0, len = strlen(s), m_num = 0, ans = 0; while (s[i] == 'F') i++; for (; i < len; i++) { if (s[i] == 'M') m_num++; else ans = max(ans + 1, m_num); } printf("%d\n", an...
12
#include <bits/stdc++.h> using namespace std; const long long maxn = 5000 + 100; long long a[maxn], p2[maxn], dp[maxn]; long long n, ans; int main() { cin >> n; for (long long i = 0; i < n; i++) { cin >> a[i]; while (a[i] % 2 == 0) { a[i] /= 2; p2[i]++; } } for (long long i = n - 1; i >=...
16
#include <bits/stdc++.h> using namespace std; namespace IO { const int __S = (1 << 20) + 5; char __buf[__S], *__H, *__T; inline char getc() { if (__H == __T) __T = (__H = __buf) + fread(__buf, 1, __S, stdin); if (__H == __T) return -1; return *__H++; } template <class __I> inline void read(__I &__x) { __x = 0; ...
14
#include <bits/stdc++.h> char s[200]; int main() { gets(s); int i = 0, x, ans, j, y, tmp; y = 0; while (s[i]) { x = (int)s[i]; ans = 0; tmp = x; for (j = 1; j <= 8; ++j) { ans = ans * 2 + tmp % 2; tmp /= 2; } printf("%d\n", (256 + y - ans) % 256); y = ans; ++i; } ...
5
#include <bits/stdc++.h> using namespace std; const int s[] = {0, 1, 2, 0, 2, 1, 0}; int g[3], d_max; int main() { ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr), cin >> g[0] >> g[1] >> g[2]; for (int i = 0; i < 7; ++i) { int h[3] = {}, w = INT_MAX; for (int j = 0; j < 7; ++j) h...
6
#include <bits/stdc++.h> using namespace std; class customIO { public: istream& in; ostream& out; customIO(istream& _in, ostream& _out) : in(_in), out(_out) {} string next() { string s; in >> s; return s; } double nextDouble() { return stod(next()); } int nextInt() { return stoi(next()); } ...
15
#include <bits/stdc++.h> using namespace std; const int size = 47; int sum[size][size]; int good[size][size][size][size]; int dp[size][size][size][size]; int source[size][size]; int main() { int n, m, qs; cin >> n >> m >> qs; for (int i = 1; i < n + 1; ++i) { for (int j = 1; j < m + 1; ++j) { char c; ...
11
#include <bits/stdc++.h> using namespace std; const int maxn = 2e3 + 9; const int MOD = 1000000007; int mod = 999998639; inline long long qpow(long long b, long long e, long long m = MOD) { long long a = 1; for (; e; e >>= 1, b = b * b % m) if (e & 1) a = a * b % m; return a; } int f1[maxn][maxn], f2[maxn][ma...
13
#include <bits/stdc++.h> using namespace std; int prime[3010]; set<int> st[3010]; void Sieve(int n) { for (int i = 2; i <= n; i++) { if (prime[i] == 0) { for (int j = 2 * i; j <= n; j += i) { prime[j] = 1; st[j].insert(i); } } } } int main() { memset(prime, 0, sizeof(prime)); ...
1
#include <bits/stdc++.h> using namespace std; string s; long long cnt, ans; int main() { cin >> s; for (int i = s.length() - 1; i >= 0; i--) { if (s[i] == 'b') cnt++; if (s[i] == 'a') { ans += cnt; ans %= 1000000007; cnt *= 2; cnt %= 1000000007; } } cout << ans; return 0; }...
6
#include <bits/stdc++.h> using namespace std; namespace tam { int tr[1200010][26], pr[1200010], ln[1200010], nw, cnt[1200010][11]; int make() { ++nw; return nw; } void init() { nw = 0; make(); } int add(int c, int lst) { if (tr[lst][c]) { int p = lst, q = tr[lst][c]; if (ln[q] == ln[p] + 1) retu...
9
#include <bits/stdc++.h> using namespace std; int n, k, i, crt; vector<pair<int, int> > ans, v; int main() { scanf("%d %d", &n, &k); for (i = 1; i <= n; ++i) { int x, y; scanf("%d %d", &x, &y); v.push_back({x, 0}); v.push_back({y, 1}); } sort(v.begin(), v.end()); for (auto &it : v) { if (i...
10
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const double eps = 1e-8; const int inf = 0x3f3f3f3f; const int N = 2e5 + 10; int a[N], b[N]; int dp[N]; int main() { int n, m, q, k; ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> q; while (q--) { cin >> n; n <<= 1;...
10
#include <bits/stdc++.h> using std::lower_bound; using std::max; using std::min; using std::random_shuffle; using std::reverse; using std::sort; using std::swap; using std::unique; using std::upper_bound; using std::vector; void open(const char *s) {} void open2(const char *s) {} int rd() { int s = 0, c, b = 0; whi...
12
#include <bits/stdc++.h> using namespace std; string c, s; int ls, lc, a; void atoc(int a, string &c) { while (a) { c = c + (char)((a % 2) + '0'); a /= 2; } } void ctoa(string c, int &a) { int lc = c.size(); int d = 1; a = 0; for (int k = lc - 1; k >= 0; k--) { if (c[k] == '1') { a = (a + ...
5
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 1, 0, -1, -1, 1, 1, -1}; int dy[] = {1, 0, -1, 0, -1, -1, 1, 1}; int kx[] = {-2, -2, -1, 1, 2, 2, 1, -1}; int ky[] = {-1, 1, 2, 2, 1, -1, -2, -2}; inline long long gcd(long long a, long long b) { a = fabs(a); b = fabs(b); while (b) { a = a % b; ...
5
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; } template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; } int q[100000]; int n, h, w; int dp[51][100001]; unsigned solve(int a, int b) { for (int i = 0; i <...
13
#include <bits/stdc++.h> using namespace std; long long win(long long e, long long s) { if (s == e) return 0; if (s + 1 == e) return 1; if (e & 1) return !(s & 1); if (s <= e / 4) return win(e / 4, s); if (s <= e / 2) return 1; return (e - s) & 1; } long long lose(long long e, long long s) { if ((s << 1) ...
19
#include <iostream> using namespace std; int main() { int t; cin>>t; while(t--){ string s; cin>>s; int n=s.length(); int ans=0; for(int i=0; i<n-2; i++){ if(s[i]==s[i+1] && s[i]==s[i+2] && s[i]!='1'){ ans+=2; s[i+1]='1'; ...
5
#include <bits/stdc++.h> using namespace std; int main() { int num, counter = 0, x, y, z; cin >> num; for (int i = 1; i <= num; i++) { cin >> x >> y >> z; if ((x == 1 && y == 1) || (x == 1 && z == 1) || (y == 1 && z == 1) || (x == 1 && y == 1 && z == 1)) { counter++; } } cout << coun...
0
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const int32_t MOD = 1e9 + 7; const long long N = 2e5 + 5; vector<long long> edge[N]; long long a[N]; long long n; long long dp[N], sum[N], nl[N]; void dfs(long long v, long long par) { sum[v] = a[v]; nl[v] = !(edge[v].size()); dp[v] = 0; ...
11
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; vector<string> a(n); string s; for (int i = 0; i < n; i++) { cin >> a[i]; } bool f = true; for (int i = 0; i < n - 1...
5
#include <bits/stdc++.h> using namespace std; int min(int x, int y) { return x < y ? x : y; } int max(int x, int y) { return x > y ? x : y; } const int N = 100005; const int M = 200001; vector<int> G[N]; queue<int> q, ans; int a[N]; int main() { int i, j, k = 1; int n, m, tt; int x, y; while (scanf("%d%d", &n, ...
13
#include <bits/stdc++.h> using namespace std; const int mod = (int)1e9 + 9; const int MAXN = 200 * 1000; int x[MAXN], y[MAXN]; bool del[MAXN]; set<int> may; map<pair<int, int>, int> is; vector<int> ans; int n; int cnt(int x, int y) { int ans = 0; for (int i = -1; i < 2; i++) ans += (int)is.count(make_pair(x + i, y ...
13
#include <bits/stdc++.h> using namespace std; int n, q, x, y, val; unsigned long long dp[10005]; bool obt[10005]; vector<int> vdel[10005], vadd[10005]; vector<int> ans; void updp(int now, int mul) { if (mul > 0) { for (int i : vadd[now]) { for (int j = n; j >= i; j--) { dp[j] += dp[j - i]; i...
14
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; long long a[n], b[n]; for (long long i = 0; i < n; i++) { cin >> a[i]; } for (long long i = 0; i < n; i++) { cin >> b[i]; } long long found = 0; lo...
2
#include <bits/stdc++.h> using namespace std; set<int> outp; int n; void rec(int pos, int value) { if (pos == n) { outp.insert(value); return; } rec(pos + 1, value + 1); rec(pos + 1, value + 5); rec(pos + 1, value + 10); rec(pos + 1, value + 50); } int need[49]; int main() { cin >> n; for (int i...
12
#include <bits/stdc++.h> using namespace std; map<int, int> mp; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; ++i) { int a; scanf("%d", &a); while (mp[a]) { mp.erase(a); ++a; } mp[a] = 1; } printf("%d\n", int(mp.size())); return 0; }
7
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") using namespace std; void solve() { long long n; cin >> n; long long a[n + 1], b[n + 1]; for (long long i = 1; i <= n; i++) cin >> a[i]; string s; cin >> s; for (long long i =...
8
#include <bits/stdc++.h> using namespace std; char _c; bool _f; template <class T> inline void IN(T &x) { x = 0, _f = 0; while (_c = getchar(), !isdigit(_c)) if (_c == '-') _f = 1; while (isdigit(_c)) x = x * 10 + _c - '0', _c = getchar(); if (_f) x = -x; } const int mod = 998244353; inline int mul(int x, i...
26
#include <bits/stdc++.h> using namespace std; int a[20]; int main() { int n, k, t, i; cin >> n >> k >> t; t = n * k * t / 100; for (i = 0; i < n; i++) { cout << min(t, k) << " "; t = t - min(t, k); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 101, SIZE = 105, mod = 1e9 + 7; int n, x, mat[SIZE][SIZE]; int tmp[SIZE][SIZE], ans[SIZE][SIZE]; void mult(int a[SIZE][SIZE], int b[SIZE][SIZE]) { memset(tmp, 0, sizeof tmp); for (int i = 1; i <= N; i++) for (int j = 1; j <= N; j++) for (int k = ...
14
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; const int mod = 998244353; int fp(int x, int y) { if (y == 0) return 1; int z = fp(x, y / 2); if (y % 2) return (z * z * x) % 10; else return (z * z) % 10; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.precision(1...
2
#include <bits/stdc++.h> using namespace std; int n, tmpI, tmpII, tmpIII, l, r; bool flag; int read() { int tot = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') fh = -1; c = getchar(); } while (c >= '0' && c <= '9') { tot = tot * 10 + c - '0'; c = getchar(); } re...
12
#include <bits/stdc++.h> #pragma GCC optimize "-O3" using namespace std; template <typename T> T mo(T x, T y) { x %= y; return x <= 0 ? x + y : x; } const int MX = 840; const int MANY = 1000 * 1000 + 7; const long long C = 1e16; bitset<MANY> bs; long long a[10]; int main() { ios_base::sync_with_stdio(false), cin....
15
#include <bits/stdc++.h> using namespace std; int a[1 << 21], h, g, cur[1 << 21], need[1 << 21]; vector<int> op; int get(int i) { return i < (1 << h) ? a[i] : 0; } void f(int i) { if (get(i) == 0) return; --cur[i]; if (get(i * 2) > get(i * 2 + 1)) { a[i] = get(i * 2); f(i * 2); } else { a[i] = get(i...
16
#include <bits/stdc++.h> using namespace std; const int N = 500005; const long long INF = (1LL << 60) - 1; int a[N]; long long sum[N]; int main() { int n; cin >> n; for (int i = 1; i <= (n + 1) / 2; i++) cin >> a[i]; int x; cin >> x; for (int i = (n + 1) / 2 + 1; i <= n; i++) a[i] = x; for (int i = n; i >...
16
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); if (n > 36) { printf("-1\n"); return 0; } if (n == 0) printf("1\n"); else if (n % 2 == 0) { for (int i = 1; i <= n / 2; i++) printf("8"); printf("\n"); } else { printf("4"); for (int i = 1; i <= n / 2; i++) printf("8...
4
#include <bits/stdc++.h> using namespace std; char a[100010]; int main() { int tc; scanf("%d", &tc); while (tc--) { int n, k; priority_queue<int, vector<int>, greater<int> > pq; scanf("%d %d", &n, &k); scanf("\n%s", a); int cnt = 0, p = 0, ch = 0, i = 0; while (a[i] == 'L') { i++; ...
6
#include <bits/stdc++.h> using namespace std; long long int a[100005]; int main() { long long int n, k; cin >> n >> k; for (long long int i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); long long int mx = 1, ans = a[0], i = 0, j = 0, cnt = 0; for (; j < n - 1;) { if (mx < j - i + 1) { mx = j - i +...
8
#include <bits/stdc++.h> using namespace std; const int max_n = 5, inf = 1111111111; int ans, p[max_n], res[max_n], a[max_n][max_n]; void update(int x, int y) { x = p[x]; y = p[y]; res[x] += a[x][y]; res[y] += a[y][x]; } int main() { for (int i = 0; i < 5; ++i) { p[i] = i; for (int j = 0; j < 5; ++j) ...
4
#include <bits/stdc++.h> const int N = 100100; using namespace std; int d[N][2]; int n, a[N]; int mod = 1e9 + 7; vector<int> v[N]; int main() { ios_base::sync_with_stdio(0); cin >> n; for (int i = 2; i <= n; i++) { int p; cin >> p; v[p + 1].push_back(i); } for (int i = 1; i <= n; i++) cin >> a[i];...
12
#include <bits/stdc++.h> using namespace std; const int INF = 1000000000; const long long INF_LL = 1000000000000000000LL; const double pi = 2 * acos(0.0); template <class T> void smin(T& a, T b) { if (a > b) a = b; } template <class T> void smax(T& a, T b) { if (a < b) a = b; } template <class T> T gcd(T a, T b) { ...
12
#include <bits/stdc++.h> using namespace std; int n, m, si, sj, X, Y; const int N = 2e3 + 7; char mp[N][N]; int dp[N][N]; int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1}; struct Node { int i, j, x; Node(int i, int j, int x) : i(i), j(j), x(x) {} }; void bfs(int si, int sj) { memset(dp, 125, sizeof(dp)); queue<Node> ...
10
#include <bits/stdc++.h> using namespace std; const int maxn = 105, hBit = 7; int n, m, k, p[maxn][hBit]; char line[maxn]; void binary(int n, int* bin) { for (int i = 0; n > 0; i++, n >>= 1) bin[i] = n & 1; } int main() { cin >> n >> m >> k; int inf1 = 0, inf2 = 0; for (int i = 0, t1, t2; i < n; i++) { cin ...
18
#include <bits/stdc++.h> using namespace std; const int INF = (int)1e9; const int maxN = 1 << 17; vector<pair<int, int> > G[maxN]; int l[maxN]; long long cnt[maxN]; int mark[maxN]; int n; int dfs(int v) { if (mark[v]) { return 0; } mark[v] = 1; int res = 1; for (auto p : G[v]) { long long b = dfs(p.fi...
11
#include <bits/stdc++.h> using namespace std; const int MN = 100005, P2 = (1 << 17), MTS = 2 * P2 + 5, inf = 1000000005, mod = 1000000007; const long long INF = 1000000000000000005LL; int ile[MN], pref[MN]; int T[MTS]; void maksuj(int p, int v) { p += P2; while (p) { T[p] = max(T[p], v); p >>= 1; ...
11
#include <bits/stdc++.h> long long n, p; int d, w; inline long long read() { long long x = 0; char ch = getchar(); for (; ch > '9' || ch < '0'; ch = getchar()) ; for (; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); return x; } inline int gcd(int a, int b) { return !b ? a...
12
#include <bits/stdc++.h> using namespace std; int n, m, q; int to[200010], nxt[200010], fst[100010], tot = 1; void add(int u, int v) { to[++tot] = v; nxt[tot] = fst[u]; fst[u] = tot; } int d[100010], bl[100010], cnt; int stk[200010], no[100010], top, bccno; int anc[100010][17], dfn[100010], clk; int dfs(int u) { ...
14
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long mo = 998244353; long long sx, sy, ex, ey, dx[6] = {0, 1, 0, -1, 0, 0}, dy[6] = {1, 0, -1, 0, 0, 0}, m, n, k, dz[6]{0, 0, 0, 0, -1, 1}, sg; long long p, no, v, ans, w; int par[550...
10
#include <bits/stdc++.h> using namespace std; int n, m; map<string, int> nums; vector<string> names; vector<string> ans; int dl[20][20]; int maxx; int maxnum; int main() { scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { string str; cin >> str; nums[str] = i; names.push_back(str); } for (in...
7
#include <bits/stdc++.h> using namespace std; const long long M = 1e9 + 7; const double pi = acos(-1); int longstring(string s) { int n = s.length(); if (n < 2) { return 0; } int len = 0; int i = 1; while (i < n) { if (s[i] == s[len]) { ++len; ++i; } else { i = i - len + 1; ...
5
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string s; cin >> s; map<string, int> m; for (int i = 0; i < n - 1; i++) { string semp; semp += s[i]; semp += s[i + 1]; m[semp]++; } string sol; int mx = INT_MIN; for (auto u : m) if (u.second > mx) { ...
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> wow; for (long long i = 0; i < n; ++i) { int tmp; cin >> tmp; wow.push_back(tmp); } int ans = 0; for (int i = 0; i < n; ++i) { for (int q = 0; q < i; ++q) { if (wow[i] < wow[q]) ans++; } } ...
10
#include <bits/stdc++.h> using namespace std; int main() { map<int, int> mymap; int n, value, total; cin >> n; for (int i = 0; i < n; i++) { cin >> value; mymap[value]++; } map<int, int>::iterator it = mymap.begin(); int a, b; int c, d; if (mymap.size() != 2) { cout << "NO" << endl; re...
2
#include <bits/stdc++.h> using namespace std; int cnt1 = 0, cnt0 = 0, cntq = 0, l; char s[100015]; int f[4]; int solve() { int i; for (i = l - 1; i >= 0; i--) if (s[i] == '0') break; int p1 = i; if (p1 < 0) return 3; for (i = l - 1; i >= 0; i--) if (s[i] == '1') break; int p2 = i; if (p2 < 0) retu...
11
#include <bits/stdc++.h> using namespace std; int n, k, dp[444444][2][2], x, ans, a, b, c, d, wa, la, wc, lc, cr, w; bool f[222222]; void dfs(int l, int r, int node) { if (l + 1 == r) { if (f[l] | f[r]) { dp[node][f[l]][f[r]] = dp[node][f[r]][f[l]] = 1; } else { dp[node][f[l]][f[r]] = 0; } ...
17
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> m >> n; double ans = m; for (int i = m - 1; i > 0; i--) { ans -= pow(double(i) / double(m), n); } cout << ans; }
8
#include <bits/stdc++.h> using namespace std; const int N = 100100; int a[N]; int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); int n, ans, val, b, i, j; cin >> n; ans = 100000; int sum = 0; for (i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; } for (i = n; i < 2 * n; i++)...
4
#include <bits/stdc++.h> using namespace std; int main(int argc, char const* argv[]) { if (argc == 3) { freopen(argv[1], "r", stdin); freopen(argv[2], "w", stdout); } string name[] = { "Washington", "Adams", "Jefferson", "Madison", "Monroe", "Adams", "Jackson", "Van Buren", "Harr...
11
#include <bits/stdc++.h> using namespace std; const int dsize = 1e+5 + 2; long long Num[dsize]; long long Best[dsize]; long long cube(long long x) { return (x * x * x); } long long closest_left_cube(long long n) { long long l = 0, r = 1e+5 + 1; while (r - l > 1) { long long m = (r + l) / 2; if (cube(m) > n)...
14
#include <bits/stdc++.h> using namespace std; long long lol[1000001]; int main() { int k, b, n, t; scanf("%d%d%d%d", &k, &b, &n, &t); lol[0] = 1; int lim = 1; for (int i = 1; i < 1000001; ++i) { lol[i] = lol[i - 1] * k; if (lol[i] > 1000000) break; lim++; } int low = 0, hig = n; while (low !...
9
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; char a[19] = {'A', 'b', 'd', 'H', 'I', 'M', 'O', 'o', 'p', 'q', 'T', 'U', 'V', 'v', 'W', 'w', 'X', 'x', 'Y'}; char b[19] = {'A', 'd', 'b', 'H', 'I', 'M', 'O', 'o', 'q', 'p', 'T', 'U', 'V', 'v', 'W', 'w...
8
#include <bits/stdc++.h> using namespace std; int n, m; long long arr[105][105]; bool vis[105][105]; void zroo() { for (int i = 1; i <= 100; i++) vis[i][1] = (i % 2 == 0); for (int i = 1; i <= 100; i++) for (int j = 2; j <= 100; j++) vis[i][j] = !vis[i][j - 1]; } int main() { int t; scanf("%d", &t); zroo(...
12
#include <bits/stdc++.h> using namespace std; struct fast { fast() { ios_base::sync_with_stdio(0); cin.tie(0); } } cincout; long long arr[400010], cum[400010], sumCum[400010]; long long sum(long long n) { return n * (n + 1) / 2; } long long more(long long rem, long long from, long long n) { if (rem <= 0) ...
11
#include <bits/stdc++.h> using namespace std; long long int n, i, x[100005], h[100005], ans; int main() { cin >> n; for (i = 0; i < n; i++) cin >> x[i] >> h[i]; if (n == 1) { cout << "1" << endl; return 0; } ans = 2; for (i = 1; i < n - 1; i++) { if ((x[i] - h[i]) > x[i - 1]) ans++; el...
7
#include <bits/stdc++.h> using namespace std; const int N = 55; string s[N]; int n, f[N][N][N][N]; int main() { cin >> n; for (int i = 1; i <= n; i++) cin >> s[i], s[i] = ' ' + s[i]; for (int l = 1; l <= n; l++) for (int k = 1; k <= n; k++) for (int i = 1; i <= n - l + 1; i++) for (int j = 1; j ...
15
#include <cstdio> using namespace std; inline int read() { int x = 0; char ch = getchar(); while (ch < '0' || ch > '9') ch = getchar(); while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar(); return x; } int main() { int t = read(); while (t--) { int c1 = read(), c2 = read(),...
1
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define ull unsigned long long const int maxn=2e5+5; const int mod=1e9+7; const int inf=0x3f3f3f3f; typedef pair<string, int> pii; void solve() { int n; cin>>n; string s1,s2; cin>>s1>>s2; if(s1[0]=='0'&&s2[0]=='1') { for(int i=0;i<n;i++) cout<<...
18
#include <bits/stdc++.h> using namespace std; int a[100001]; int main() { int n; cin >> n; if (n % 4 == 0 || (n - 1) % 4 == 0) { if (n % 2 == 1) a[n / 2 + 1] = n / 2 + 1; for (int i = 1; i <= n / 2; i += 2) { a[i] = i + 1; a[i + 1] = n - i + 1; a[n - i + 1] = n - i; a[n - i] = i; ...
6
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; const long long m1 = 1000000123; const long long m2 = 1000000321; inline long long modMul(long long a, long long b) { return ((a % mod) * (b % mod)) % mod; } inline long long modAdd(long long a, long long b) { return ((a % mod) + (b % mod)...
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long arr[n], s = 0; for (int i = 0; i < n; i++) { cin >> arr[i]; s += arr[i]; } sort(arr, arr + n); long long x = s, y = 0; for (int i = 0; i < n - 1; i++) { s += x - y; y += arr[i]; } cout << s; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long int n; cin >> n; long long int ans = 0; long long int count = 1; long long int count4 = 0; while (n > 0) { long long int d = n % 10; if (d == 4) ans += pow(2, count - 1); else if (d == 7) { ans += pow(2, count); ...
3