solution
stringlengths
53
181k
difficulty
int64
0
27
#include <bits/stdc++.h> using namespace std; int main() { long long int t, n, k, i; cin >> t; while (t--) { cin >> n; for (i = 0; i < n; i++) { cin >> k; if (i % 2 == 0) cout << abs(k) << " "; else cout << -1 * abs(k) << " "; } cout << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long sz = 200005; long long mod = 1e9 + 7; long long n, t, m; vector<long long> BIT(sz, 0); void Update(long long in, long long x) { for (long long i = in; i < sz; i += i & (-i)) BIT[i] += 1; } long long Query(long long in) { long long ans = 0; for (long lo...
12
#include <bits/stdc++.h> using namespace std; using cd = complex<double>; const int Inf = 1000000007; const long long mod = 998244353; const double Pi = acos(-1); void Fastio() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); } long long p, k, t, deg = 0; long long a[1005]; void Solve() { cin >> k >...
12
#include <bits/stdc++.h> using namespace std; long long int n, m, a[1000005]; vector<long long int> v[1000005]; map<long long int, long long int> mp; pair<long long int, long long int> e[1000005]; long long int bexpo(long long int x, long long int n, long long int m) { long long int r = 1; while (n > 0) { if (n...
14
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; const double pi = 3.14159265358979323846264338327950288419716939937511; const double eps = 1e-11; char ch_ch_ch[1 << 20]; string gs() { scanf("%s", ch_ch_ch); return string(ch_ch_ch); } string gl() { gets(ch_ch_ch); return ...
15
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; const int OO = 0x3f3f3f3f, P = 100; void g4(void) { ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr); } int main() { g4(); long long int n, br[105], ar[105], b = 0, a = 0, f = 1; cin >> n; for (long long int i = 0...
1
#include <bits/stdc++.h> using namespace std; const long long N = 1010; vector<long long> parent(N, -1); vector<bool> king(N, false); vector<long long> size(N, -1); void make_set(long long v) { parent[v] = v; size[v] = 1; king[v] = false; } long long find_set(long long v) { if (parent[v] == v) return v; retur...
7
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, U b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, U b) { if (a < b) a = b; } template <class T> inline void gn(T &first) { char c, sg = 0; while (c = getchar(), (c > '9' || c ...
17
#include <bits/stdc++.h> using namespace std; int isPrime(int n) { if (n <= 1) return 1; for (int i = 2; i <= ceil(sqrt(n)); i++) { if (n % i == 0) return i; } return 0; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long int i, j, k = 0, n, m, q = 2; cin >> n; ve...
5
#include <bits/stdc++.h> using namespace std; long long GCD(long long x, long long y) { return y ? GCD(y, x % y) : x; } long long Get(long long L, long long R, long long x) { return R / x - (L - 1) / x; } int main() { int T; cin >> T; while (T--) { long long r, b, k; cin >> r >> b >> k; if (r > b) s...
9
#include <bits/stdc++.h> using namespace std; int dp[5][5][210][210]; vector<pair<int, int> > out[5][5][210][210]; void Init() { dp[0][0][0][0] = 1; for (int sum = 0; sum <= 4; ++sum) { for (int l = 0; l <= sum; ++l) { for (int i = 0; i <= 200; ++i) { for (int j = 0; j <= 200; ++j) { int...
14
#include <bits/stdc++.h> using namespace std; const int MAX_N = 109; int as[MAX_N]; int N; long long solve() { long long ans = 0; for (int i = 0; i < N; i++) { ans += (long long)(as[i] - 1) * (i + 1); } ans += N; return ans; } int main() { scanf("%d", &N); for (int i = 0; i < N; i++) { scanf("%d",...
3
#include <bits/stdc++.h> using namespace std; long long fx[4] = {1, -1, 0, 0}; long long fy[4] = {0, 0, 1, -1}; int main() { { long long n; cin >> n; long long visi[n + 2]; memset(visi, 0, sizeof visi); for (long long i = 1; i <= n; i++) { for (long long j = 1; j <= n; j++) { long lo...
1
#include <bits/stdc++.h> using namespace std; long double ccw(pair<long long, long long> a, pair<long long, long long> b, pair<long long, long long> c) { return (long double)a.first * (b.second - c.second) + (long double)b.first * (c.second - a.second) + (long double)c.first * (a.sec...
19
#include <bits/stdc++.h> using namespace std; int main() { int n, m, i, temp; cin >> n >> m; int a[n], b[n]; for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < n; i++) cin >> b[i]; int c[n], d[n]; for (i = 0; i < n; i++) { c[i] = a[i + 1] - a[i]; d[i] = b[i + 1] - b[i]; } c[n - 1] = m - a[n ...
5
#include <bits/stdc++.h> using namespace std; const int MM = 2e5 + 5; int n, sz[MM]; vector<int> adj[MM]; void dfs(int u, int p) { sz[u] = 1; for (int v : adj[u]) if (v != p) { dfs(v, u); sz[u] += sz[v]; } } void dfs1(int u, int p) { for (int v : adj[u]) if (v != p) { if (sz[v] % 2 =...
12
#include <bits/stdc++.h> using namespace std; void solve(); signed main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); solve(); } void solve() { long long n; cin >> n; string s; cin >> s; long long one = 0, zero = 0; for (auto x : s) { if (x == '1') one++; else ze...
1
#include <bits/stdc++.h> using namespace std; using ll = long long; bool ck(vector<int> &degs, vector<int> &vals, int size) { sort(degs.begin(), degs.end()); reverse(degs.begin(), degs.end()); int sum = 0; for (int i = 0; i < size - 1; i++) { sum += degs[i]; if (sum >= vals[i]) { return false; ...
24
#include <bits/stdc++.h> using namespace std; vector<bool> vis[100005]; vector<pair<int, int> > adj[100005]; int dfs(int u) { queue<int> q; for (int j = 0; j < (int)adj[u].size(); ++j) { pair<int, int> where = adj[u][j]; if (!vis[min(u, where.first)][where.second]) { vis[min(u, where.first)][where.sec...
15
#include <bits/stdc++.h> using namespace std; const int maxn = 5000; const int mod = 1000 * 1000 * 1000 + 7; int n; vector<pair<short, pair<short, short> > > srt; pair<short, short> ver[maxn]; short mark[maxn]; short dis[maxn][maxn]; short dist(const pair<short, short> &a, const pair<short, short> &b) { return abs(a....
18
#include <bits/stdc++.h> using namespace std; const int N = 300010; int n, q; int a[N]; int t[N]; struct Query { int x, y, id; } queries[N]; int res[N]; struct ST { int sum[N << 2]; void init(int node, int l, int r) { sum[node] = 0; if (l < r) { int m = l + r >> 1; init(node << 1, l, m); ...
15
#include <bits/stdc++.h> using namespace std; template <class T> void _print(T t) { cerr << t; } template <class T, class V> void _print(pair<T, V> p) { cerr << "{"; _print(p.first); cerr << ","; _print(p.second); cerr << "}"; } template <class T> void _print(vector<T> v) { cerr << "[ "; for (auto i : v...
3
#include <bits/stdc++.h> using namespace std; vector<int> v[1010]; int n, m, x, minv, choose, cnt, tot, a[1010], must[1010], num[1010]; double C[1010][1010], f[1010][1010], tmp; bool prob[1010]; int main() { for (int i = 0; i <= 1000; i++) { C[i][0] = 1; for (int j = 1; j <= i; j++) C[i][j] = C[i - 1][j] + C[...
18
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n; vector<int> g[N]; int d[N], par[N]; bool done[N]; vector<int> nodes; void dfs(int u, int p) { for (int v : g[u]) { if (v != p) { par[v] = u; d[v] = d[u] + 1; dfs(v, u); } } nodes.emplace_back(u); } int main() { ...
13
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, 1, -1, -1, -1, 1, 1}; int dy[] = {1, -1, 0, 0, 1, -1, 1, -1}; const double EPS = 1e-8; const double PI = acos(-1.0); long long gcd(long long a, long long b) { return !b ? a : gcd(b, a % b); } long long lcm(long long a, long long b) { return (a / gcd(a, b))...
0
#include <bits/stdc++.h> using namespace std; long long exp(long long b, long long e) { if (b == 1 || e == 0) return 1; if (e == 1) return b % 1000000007; if (e == 2) return (b * b) % 1000000007; return ((e % 2 == 0) ? exp(exp(b, e / 2), 2) % 1000000007 : (b * (exp(exp(b, e / 2), 2) ...
8
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vs = vector<string>; mt19937_64 rng( (unsigned int)chrono::steady_clock::now().time_since_epoch().count()); int main() ...
10
#include <bits/stdc++.h> using namespace std; int main(void) { int n; cin >> n; int a[n + 1][n + 1], b[n + 1]; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cin >> a[i][j]; } } for (int i = 1; i <= n; i++) { int max = a[i][1]; for (int j = 1; j <= n; j++) { if (ma...
3
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n >> k; int a[n]; for (int i = 0; i < n; i++) cin >> a[i]; int j = 0; int ansl = 0; int ansr = 0; int cnt = 0; for (int i = 0; i < n; i++) { if (j < i) { j = i; cnt = 0; } while (j < n) { int ncnt...
8
#include <bits/stdc++.h> using namespace std; long long to[333], coin[111111], start[333], vis[333], n, t1, t2, t3, t; vector<int> v; int dp[2][111111]; int dp2[303][100001]; const long long mod = 1000000007; int main() { ios_base::sync_with_stdio(0); cin >> n >> t1 >> t; for (int i(0), _n(n); i < _n; ++i) { ...
13
#include <bits/stdc++.h> using namespace std; const long long md = 1e9 + 7; int n; vector<int> gpf; void pfs(int x, vector<int> &res) { if (x == 1) return; if (x == n + 1) { res.push_back(x); return; } int p = gpf[x]; pfs(x / p, res); res.push_back(p); } void fail() { puts("0"); exit(0); } int m...
22
#include <bits/stdc++.h> using namespace std; int main() { ios ::sync_with_stdio(false); long long int n; cin >> n; while (n % 3 == 0) n /= 3; cout << n / 3 + 1 << endl; return 0; }
8
#include <bits/stdc++.h> using namespace std; int a[2005]; int main() { int n, b; int i; cin >> n >> b; for (i = 0; i < n; i++) { cin >> a[i]; } int d, div, sum = 0; int num = 0; int j; int max = -1; for (i = 0; i < n - 1; i++) { for (j = i + 1; j < n; j++) { if (a[i] < a[j]) { ...
6
#include <bits/stdc++.h> const int maxn = 200035; const int maxm = 400035; struct node { int l, r, val; node(int a = 0, int b = 0, int c = 0) : l(a), r(b), val(c) {} bool operator<(node a) const { return l < a.l; } }; struct point { int fa, son, top, tot; } a[maxn]; char opt[13]; int n, m, tim, lst, chain[maxn]...
26
#include <bits/stdc++.h> using namespace std; using namespace std; const int N = 105; const int M = 1005; double f[2][N * M], s[N * M]; int main() { int n, m; scanf("%d%d", &n, &m); if (m == 1) { puts("1"); return 0; } f[0][0] = m - 1; for (int i = 0; i <= n * m; ++i) s[i] = m - 1; int sum = 0; ...
15
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 200; const int mod = 1e9 + 7; int n, m, s, k, a[N]; int l[N], r[N]; int sum[N]; vector<int> g[1505]; int dp[1505][1505]; vector<pair<int, int> > f[1505]; inline int check(int x) { for (int i = 1; i <= n; ++i) sum[i] = sum[i - 1] + (a[i] <= x); for (i...
17
#include <bits/stdc++.h> using namespace std; short int visit[100000]; int main() { ios::sync_with_stdio(0), cin.tie(0); long long n, v, x, y, sum = 0; cin >> n >> v; vector<long long> g[n + 1]; for (int i = 1; i <= v; i++) { cin >> x >> y; g[x].push_back(y); g[y].push_back(x); } if (v != (n -...
5
#include <bits/stdc++.h> using namespace std; int main() { char a[3][3]; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { cin >> a[i][j]; } } for (int i = 0, k = 2; i < 3; i++, k--) { for (int j = 0, l = 2; j < 3; j++, l--) { if (a[i][j] != a[k][l]) { cout << "NO"; ...
0
#include <bits/stdc++.h> using namespace std; long long l, r, f[30]; long long cal(long long r) { int l, s[30]; if (r == 0) return 0; for (l = 0; r != 0; r /= 10) s[++l] = r % 10; if (l == 1) return s[1]; long long ans = 9, p = 0; for (int i = 2; i < l; i++) ans += 9 * f[i - 2]; if (l == 2) { if (s[1]...
7
#include <bits/stdc++.h> using namespace std; int mp[50010][5]; int main(void) { cin.tie(0); ios::sync_with_stdio(0); int t; cin >> t; while (t--) { int n, f = 0; cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < 5; j++) cin >> mp[i][j]; if (i != 0) { int sum = 0; ...
7
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int cnt_n[5]; int cnt_m[5]; cnt_n[0] = 0; cnt_n[1] = 0; cnt_n[2] = 0; cnt_n[3] = 0; cnt_n[4] = 0; cnt_m[0] = 0; cnt_m[1] = 0; cnt_m[2] = 0; cnt_m[3] = 0; cnt_m[4] = 0; for (int i = 1; i <= n; i++) { cnt...
3
#include <bits/stdc++.h> using namespace std; const int cmx = 1e5 + 5; int n, m; int color[cmx]; int valid[cmx]; set<int> c_cnt[cmx]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n >> m; for (int i = 1; i <= n; i++) cin >> color[i], valid[color[i]] = 1; for (int i = 1; i <= m; ...
8
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:256000000") using namespace std; int ans, t, i, n, sum, h; int cnt[20], need[20]; char s[300]; int main() { scanf("%d", &t); for (; t; t /= 10) { ++need[t % 10]; } scanf("%s", s + 1); n = strlen(s + 1); for (i = 1; i <= n; ++i) { ++cnt[s[i] - ...
7
#include<bits/stdc++.h> using namespace std; int T,n; string s; string ans1,ans2; void solve(){ ans1=ans2=""; int num=0; for(int i=0;i<n;i++)if(s[i]=='0')num++; if(num%2==1){ puts("NO");return; } int lst=(n-num)/2; num=0; for(int i=0;i<n;i++){ if(s[i]=='0'){ if(num==0)ans1+="(",ans2+=")"; else ans2+="...
8
#include <bits/stdc++.h> using namespace std; int main() { int n, k, w[110], mn = 1000; cin >> n >> k; for (int i = 0; i < n; i++) cin >> w[i]; for (int i = 0; i < n; i++) if (k % w[i] == 0) mn = min(mn, (k / w[i])); cout << mn << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int FFTMOD = 1007681537; const int INF = (int)1e9; const long long LINF = (long long)1e18; const long double PI = acos((long double)-1); const long double EPS = 1e-9; inline long long gcd(long long a, long long b) { long long r; while...
14
#include <bits/stdc++.h> using namespace std; bool check(int a[], int st, int end) { int l = (end + st) / 2; int a1 = a[st]; int a2 = a[l + 1]; if (a1 == a2) return false; for (int i = st + 1; i <= l; i++) { if (a[i] != a[i - 1]) return false; } for (int i = l + 2; i <= end; i++) { if (a[i] != a[i...
1
#include <bits/stdc++.h> using namespace std; int n; vector<long long> h; long long d; int dp[100030]; const int MIN = -987654321; struct RMQ { int n; vector<int> rangeMin; RMQ(const vector<int>& arr) { n = arr.size(); rangeMin.resize(n * 4); init(arr, 0, n - 1, 1); } int init(const vector<int>& a...
12
#include <bits/stdc++.h> using namespace std; void setIO(const string &name = "") { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); if (name.length()) { freopen((name + ".in").c_str(), "r", stdin); freopen((name + ".out").c_str(), "w", stdout); } } long long n, k; vector<long long> a...
8
#include <bits/stdc++.h> using namespace std; int main() { int a, b, c, d, e, f; int i = 0; scanf("%d %d %d %d %d %d", &a, &b, &c, &d, &e, &f); a += b + f; c += b + d; e += d + f; i = b * b + d * d + f * f; printf("%d", a * a - i); }
8
#include <bits/stdc++.h> using namespace std; int main(int argc, char const *argv[]) { ios::sync_with_stdio(false); cin.tie(nullptr); long long int n, p, k, i, count = 0; vector<long long int> a; cin >> n >> p >> k; for (i = 0; i < n; i++) { long long int temp; cin >> temp; a.push_back(temp); ...
15
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, m; cin >> n >> m; vector<string> g(n); for (auto& s : g) { cin >> s; } vector<int> sum(m, 0); for (int i = 1; i < n; i++) { for (int j = 1; j < m; j++) { if (g[i - 1][j] == 'X...
9
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> inline void chkmin(T1 &x, const T2 &y) { if (x > y) x = y; } template <class T1, class T2> inline void chkmax(T1 &x, const T2 &y) { if (x < y) x = y; } const int MAXN = 4100; int k; int a[MAXN]; int n; int b[MAXN]; int p[MAXN], q[MAXN]; int...
23
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 7; bool used[N]; vector<long long> g[N]; void dfs(long long u) { used[u] = 1; for (long long v : g[u]) if (!used[v]) dfs(v); } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); long long n, m; cin >> n >> m; for (long long...
9
#include <bits/stdc++.h> using namespace std; const int32_t Accepted = 0; template <typename T> void in(T& x) { static char ch; static bool neg; for (ch = neg = 0; ch < '0' || '9' < ch; neg |= ch == '-', ch = getchar()) ; for (x = 0; '0' <= ch && ch <= '9'; (x *= 10) += ch - '0', ch = getchar()) ; x =...
17
#include <bits/stdc++.h> struct sm { int l; int r; int t; int c; }; int main() { int n, m; scanf("%d %d", &n, &m); int priz = 0; sm *sms = new sm[m]; for (int i = 0; i < m; i++) { scanf("%d %d %d %d", &sms[i].l, &sms[i].r, &sms[i].t, &sms[i].c); } for (int i = 1; i <= n; ++i) { int t = -1;...
4
#include <bits/stdc++.h> using namespace std; int main() { long long int n, k; scanf("%lld %lld", &n, &k); vector<pair<long long int, long long int> > coin; long long int arr[n]; for (int i = 0; i < n * 2; i++) { if (i < n) scanf("%lld", &arr[i]); else { long long int x; scanf("%lld"...
6
#include <bits/stdc++.h> using namespace std; int main() { long long x, z, a, b, c, i = 1, y = 0; cin >> x >> z; a = b = c = z; while (1) { if (i % 3 == 1 && a < x) { if (c + b < x) { a = c + b - 1; } else if (c + b == x) a = x - 1; else a = x; i++; y++;...
8
#include <bits/stdc++.h> using namespace std; int a[10005], b[10005], c[10005], mo[10005]; char S[10005], SS[10005], AA[10005], xx[10005], yy[10005]; char BB[10001][101]; map<int, int> mp; map<long long, int> mp1; int main() { int i, j, k, n, t, m, y, cnt, adigit, bdigit; while (scanf("%d", &n) == 1) { for (i =...
5
#include <bits/stdc++.h> using namespace std; int main() { int n, flag; int a[100008]; while (~scanf("%d", &n)) { for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } sort(a, a + n); flag = 0; for (int i = 0; i < n; i++) { if (a[i] % a[0] == 0) { flag = 1; } else { ...
2
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); printf("0 0 %d", n); return 0; }
1
#include <bits/stdc++.h> using namespace std; vector<pair<int, long long> > v[100005]; int vis[100005]; int n; int size[100005]; int dd[100005]; int par[100005]; int cou[100005]; long long brr[100005]; long long arr[100005]; void dfs(int ind, int p) { vis[ind] = 1; int i; int coo = 0; size[ind] = 1; for (i = ...
8
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; ifstream in("test.in"); ofstream out("test.out"); const int DIM = 1e5 + 5; int vx[DIM], vy[DIM], ans[DIM]; long long value(int x,...
15
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 10, MN = 2e6 + 10; void fail() { cout << "IMPOSSIBLE\n"; exit(0); } int l[MN], r[MN], id[MN], L, R, n, m, n1 = INF, n2; vector<int> g[MN]; void dfs(int i, int x) { if (id[i]) { if (id[i] == x) return; fail(); } id[i] = x; for (auto ...
24
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n, arr[10]; cin >> n; string s; cin >> s; for (int i = 1; i < 10; i++) cin >> arr[i]; for (int i = 0; i < n; i++) { int x = (int)s[i] - 48; if (arr[x] <= x) continue; els...
5
#include <bits/stdc++.h> using namespace std; const int maxn = 100010; int a[maxn]; int n; void init() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); } int vis[maxn]; int tot; vector<int> C[maxn]; void dfs(int x) { if (vis[x]) { return; } vis[x] = 1; C[tot].push_back(x); dfs(a[x])...
13
#include <bits/stdc++.h> using namespace std; map<int, int> p; int q[310][2] = { 0, }; void ins(int x, int y) { int r = p[x]; if (r == 0 || y < r) p[x] = y; } int gcd(int x, int y) { for (;;) { int t = x % y; if (t == 0) break; x = y; y = t; } return y; } int main() { int n; scanf("%d"...
11
#include <bits/stdc++.h> using namespace std; int v[100005]; int h[100005]; int n; int step[100005]; bool ok(int mid) { for (int i = 1; i <= n; i++) h[i] = v[i]; int left_pointer = 1; int right_pointer = n; for (int i = 1; i <= n; i++) step[i] = (1 << 30); for (int i = 1; i <= mid; i++) { h[left_pointer] ...
8
#include <bits/stdc++.h> using namespace std; void solve() { int n, k; cin >> n >> k; int ans = n - n % k; ans += min(n % k, k / 2); cout << ans << endl; } int main() { int t; cin >> t; while (t--) solve(); return 0; }
1
#include <bits/stdc++.h> using namespace std; bool check[2012][1000] = {false}; bool tr(int m, int n) { int t = 0; for (int i = 0; i < 4; i++) { if (m - 10 * (m / 10) != n - 10 * (n / 10)) t++; m /= 10; n /= 10; } if (t <= 1) return true; return false; } int main() { int n; cin >> n; vector<...
9
#include <bits/stdc++.h> using namespace std; template <class T> __inline__ __attribute__((always_inline)) T read() { T x = 0, w = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') w = -w; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; return x * w; } template <class T> __in...
23
#include <bits/stdc++.h> using namespace std; const long long infi = 1000000000000000009; int subset_fav[220009 + 10]; int dp[220009 + 10]; int puppy[220009 + 10]; int main() { int a, b, k, t, i, j; scanf("%d%d%d%d", &a, &b, &k, &t); for (i = -k; i <= k; i++) { subset_fav[i + 100309] = 1; } for (i = 2; i ...
14
#include <bits/stdc++.h> using namespace std; const int MAXN = (1 << 20) + 1; int h, g, n, m; long long int S; vector<int> ans; int monti[MAXN]; void elimina(int u, int l) { if (l > h || monti[u] == 0) return; int node = u; int level = l; while (level < h && monti[node] > 0) { int leftNode = node * 2; i...
16
#include <bits/stdc++.h> using namespace std; const int N = 100010; const int INF = 0x3f3f3f3f; const long long mod = 1e9 + 7; long long v[N]; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; cin >> n; for (int i = 1; i <= n; i++) { cin >> v[i]; } bool OK = false; for (long long j = 2...
8
#include <bits/stdc++.h> using namespace std; vector<long long> v; long long f(long long x, long long y, long long i) { auto it = lower_bound(v.begin(), v.end(), x); if (it == v.end()) return 0; auto it1 = upper_bound(v.begin(), v.end(), y); long long p = it1 - v.begin(), q = it - v.begin(); return max(0LL, m...
9
#include <bits/stdc++.h> using namespace std; const long long base = 233; const long long ff = 1e6 + 5; const long long mod = 998244353; const long long inf = 1e9 + 1; long long tr[ff], sr[ff]; void upd(long long now) { tr[now] = tr[(now << 1)] + tr[(now << 1 | 1)]; } void build(long long now, long long l, long long r)...
12
#include <bits/stdc++.h> char a[10][10]; int main() { char ch; int r, c, m = 0, i, j, x; scanf("%d%d", &r, &c); scanf("%c", &ch); for (i = 0; i < r; i++) for (j = 0; j <= c; j++) scanf("%c", &a[i][j]); for (i = 0; i < r; i++) { x = 0; for (j = 0; j < c; j++) { if (a[i][j] == (int)'S') { ...
0
#include <bits/stdc++.h> using namespace std; int main() { int n, m, k = 0, l = 0; char a[1100][600], b[1100][600]; scanf("%d %d", &n, &m); for (int i = 0; i < n; i++) { scanf("%s", a[i]); } for (int i = 0; i < m; i++) { scanf("%s", b[i]); } for (int i = 0; i < n; i++) { for (int j = 0; j < ...
3
#include <bits/stdc++.h> using namespace std; const int inf = 1.01e9; const double eps = 1e-9; const int N = 1e5 + 10; char s[N]; int a[N]; int ac = 0; int main() { int n, k; scanf("%d%d%s", &n, &k, s); for (int i = 0; (i) < (n); ++i) if (s[i] == '0') a[ac++] = i; int res = inf; for (int i = 0; i + k + 1 ...
8
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; const long long int inf = 2e9 + 5; double PI = 3.14159265358979323846; void solve() { long long int n; cin >> n; long long int w; cin >> w; long long int N = 2 * n; long long int a[N]; for (int i = 0; i < N; i++) { cin >>...
7
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); if (n < 10) { printf("%d", 0); } switch (n - 10) { case 0: printf("%d", 0); break; case 1 ... 9: printf("%d", 4); break; case 10: printf("%d", 15); break; case 11: printf("%d", 4); ...
0
#include <bits/stdc++.h> using namespace std; long long n, by; long long a[400005]; map<int, int> mp; long long qpow(long long base, long long x) { long long ans = 1; if (x >= 31) return n; while (x) { if (x & 1) ans = ans * base; base = base * base; x >>= 1; } return ans; } int main() { cin >> ...
8
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5; long long a1, b1, a2, b2; long long dx = 0, dy = 0; int n; char s[maxn + 5]; long long Chk(long long mid) { long long sa = dx * mid + a1; long long sb = dy * mid + b1; long long cnt = mid * n; for (int i = 0; i < n; ++i) { long long sum = a...
11
#include <bits/stdc++.h> using namespace std; long long const MOD = 1000000007; const int M = 2e5 + 10; vector<pair<long long, long long> > v1[M + 10]; vector<long long> v[M + 10]; long long n, m, k; char ara[1000][1000]; bool vis[1000][1000]; long long cost[M + 1]; long long mx = 0; long long p1 = 0, p2 = 0; void inpu...
8
#include <bits/stdc++.h> using namespace std; long long a[100000], d, n, m, r, ans = 0; int main() { cin >> n >> m >> d; n *= m; cin >> a[0]; r = a[0] % d; for (int i = 1; i <= n - 1; i++) { cin >> a[i]; if (a[i] % d != r) { cout << -1; return 0; } } sort(a, a + n); r = a[n / 2];...
6
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n, i, ind = 0; string s, z, p, ans; cin >> n; cin >> s; ans = s; char ch = 'z'; map<string, long long int> m; for (i = 0; i < n; i++) { if (s[i] < ch) ch = s[i]; ...
6
#include <bits/stdc++.h> using namespace std; long long dp[110][110], f[110]; const int mod = 1e9 + 7; vector<int> v[110]; int n, k; void dfs(int x, int pre = 0) { dp[x][0] = dp[x][k + 1] = 1; for (auto y : v[x]) if (y != pre) { dfs(y, x); for (int i = 0; i <= 2 * k; i++) { for (int j = 0; j...
17
#include <bits/stdc++.h> using namespace std; long long cnt1, cnt2, x, y; bool valid(long long mid) { long long equ1 = mid - (mid / x); long long equ2 = mid - (mid / y); long long equ3 = mid - (mid / (x * y)); return equ1 >= cnt1 && equ2 >= cnt2 && equ3 >= cnt1 + cnt2; } int main() { cin >> cnt1 >> cnt2 >> x ...
10
#include <bits/stdc++.h> using namespace std; int main() { long long int t; std::cin >> t; while (t--) { vector<string> s; for (int i = 0; i < 9; i++) { string str; std::cin >> str; s.push_back(str); } int x[] = {0, 3, 6, 1, 4, 7, 2, 5, 8}; int y[] = {0, 1, 2, 3, 4, 5, 6, 7, ...
5
#include <bits/stdc++.h> using namespace std; const int MAX = 100000; int a[MAX], b[MAX]; int n, p, q; double f(double x) { double y = 1; for (int i = 0; i < n; i++) y = min(y, (1.0 - x * b[i]) / a[i]); return y * p + x * q; } int main() { cin >> n >> p >> q; for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; ...
16
#include <bits/stdc++.h> using namespace std; vector<int> adj[100001]; bool visit[100001]; const int mod = 1000000007; long long mod_mult(long long a, long long b) { return ((a % mod) * (b % mod)) % mod; } long long mult(long long x, long long y) { long long prod = 1; while (y > 0) { if (y & 1) prod = mod_mul...
10
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:100000000000,100000000000") const long long INF = 2009000999; const double cp = 2 * asin(1.0); const double eps = 1e-9; const long long mod = 1000000007LL; using namespace std; int n, a, b, ost = -INF; vector<int> wyn; pair<int, int> T[1010]; int main() { scanf...
11
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; long long v[N], pre[2][N]; int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> v[i]; pre[i % 2][i] = pre[i % 2][i - 1] + v[i]; pre[!(i % 2)][i] = pre[!(i % 2)][i...
13
#include <bits/stdc++.h> using namespace std; long long n; struct edge { long long v, w, next; } e[300010 << 1]; long long eid, p[300010 << 1]; void insert(long long u, long long v, long long w) { e[++eid] = {v, w, p[u]}; p[u] = eid; } long long w[300010]; long long ans = -1e18; long long dp[300010]; void dfs(lon...
10
#include <bits/stdc++.h> using namespace std; vector<int> v; int main() { int n, cnt = 0; cin >> n; for (int i = 0; i < n; i++) { char c; cin >> c; cnt++; if (c == '0') break; } cout << cnt; return 0; }
1
#include <bits/stdc++.h> int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int n; std::cin >> n; std::vector<int> a, b; for (int i = 0; i < n; ++i) { int value; std::cin >> value; if (value) { a.push_back(value); } } for (int i = 0; i < n; ++i) { int va...
5
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; int n, a, b; int main() { scanf("%d%d%d", &n, &a, &b); int ac = 0, bc = 0; for (int i = 0; i <= n; i++) { long long tren = (long long)n - ((long long)a * i); if (tren % b == 0 && tren >= 0) { ac = i, bc = (n - (a * i)) / b; ...
8
#include <bits/stdc++.h> using namespace std; int n; int a[100007]; vector<pair<int, int> > v[100007]; long long pref[100007]; int used[100007]; int ans = 0; void dfs(int vertex) { int i; int sz = v[vertex].size(); if (pref[vertex] > a[vertex]) { used[vertex] = 1; } for (i = 0; i < sz; i++) { long lon...
8
#include <bits/stdc++.h> using namespace std; int i, j, k; int main() { string s; cin >> s; int df = 0; for (i = 0; i < s.size(); i++) { if (s[i] != s[0]) df = 1; if (s[i] != s[s.size() - 1 - i]) { cout << s.size() << endl; exit(0); } } cout << (df ? s.size() - 1 : 0) << endl; }
1
#include <bits/stdc++.h> using namespace std; const int MX = 105; int a[MX][MX]; int main(int argc, char *argv[]) { int n, m; cin >> n >> m; int i, j, c; long long before = 0; long long after = 0; while (m--) { cin >> i >> j >> c; a[i][j] = c; before += c; } for (int cnt = (0), _b = (2); cnt...
5