solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; char load[55][55] = {0}; bool flag[55][55] = {false}; int cont = 0; int n, m; bool check(int i, int j, int fa_i, int fa_j, char ch) { bool re = false; cont++; if (cont > 4 && flag[i][j] == true && load[i][j] == ch) re = true; else if (load[i][j] == ch && flag[i]...
2
#include <bits/stdc++.h> using namespace std; int main() { double n; cin >> n; vector<double> r1(n); for (int i = 0; i < n; i++) { cin >> r1[i]; } sort(r1.rbegin(), r1.rend()); double m; cin >> m; vector<double> p1(m); for (int i = 0; i < m; i++) { cin >> p1[i]; } sort(p1.rbegin(), p1.re...
2
#include<iostream> using namespace std; struct Node{ int key, priority; Node* right; Node* left; }; Node* right_rotate(Node* t){ Node* s = t->left; t->left = s->right; s->right = t; return s; } Node* left_rotate(Node* t){ Node* s = t->right; t->right = s->left; s->left = t; ret...
0
#include<cstdio> #include<algorithm> #include<cstring> #include<queue> #define ll long long using namespace std; int n,k,tot,pre[400005],nxt[400005],a[400005],l[400005],r[400005],sl[400005],sr[400005]; ll ans; bool flag[400005]; priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >q; int main() ...
0
#include <bits/stdc++.h> using namespace std; int main() { int n, arr[3] = {0}; cin >> n; for (int i = 1; i <= n; i++) { int x; cin >> x; arr[x]++; } if (arr[2] > 0 && arr[1] > 0) { cout << 2 << " "; cout << 1 << " "; arr[2]--, arr[1]--; while (arr[2] > 0) { cout << 2 << " ";...
1
#include<iostream> #include<vector> #include<string> #include<map> using namespace std; int main() { int a; cin >> a; int b = 0; for (int c = 0; c < a; c++) { string d; cin >> d; if (d == "A")b++; else b--; if (b < 0)break; } cout << ((b) ? ("NO") : ("YES")) << endl; }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; using pii = pair<int, int>; const int INF32 = INT_MAX; const ll INF64 = ((ll)INF32 << 29ll); void reflex() { ios_base::sync_with_stdio(false); cin.tie(nullptr), cout.tie(nullptr); } vector<int> a, tag; vector<pii> mn; void push(int u, int l, int r)...
3
#include <bits/stdc++.h> using namespace std; struct Stu { int t, x, pos; void read(int _p) { cin >> t >> x; pos = _p; } } st[1000020]; int n, m; void init() { cin >> n >> m; for (int i = 1; i <= n; ++i) st[i].read(i); } bool operator<(Stu a, Stu b) { return a.t > b.t; } bool cmp2(Stu a, Stu b) { retu...
3
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int,int> P; typedef pair<int,P> P1; typedef pair<P,P> P2; #define pu push #define pb push_back #define mp make_pair #define eps 1e-7 //#define INF 1000000000 #define mod 1000000007 #define fi first #define sc second #define rep(i,x) for(in...
0
#include <bits/stdc++.h> using namespace std; const long long INF = 0x3f3f3f3f3f3f3f3fLL; const int inf = 0x3f3f3f3f; const int maxn = 2e3 + 10; const int mod = 1e9 + 7; const double eps = 1e-6; int di[8][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}, {-1, -1}, {-1, 1}, {1, -1}, {1, 1}}; clock_t _st = clock(...
4
#include <bits/stdc++.h> using namespace std; const long long UNDEF = -1; const long long INF = 1ULL << 58ULL; template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, true : false; } template <typename T> inline bool chkmin(T &a, T b) { return a > b ? a = b, true : false; } pair<long long, long ...
5
#include <bits/stdc++.h> using namespace std; using PII = pair<int,int>; #define MP make_pair #define FF first #define SS second double P, X; int N; vector<PII> G[100010]; double dfs1(int u, int p){ double res = 0.; for(auto e: G[u]){ if(e.FF == p) continue; res += P * (e.SS + dfs1(e.FF, u)); } return ...
0
#include <bits/stdc++.h> inline int read() { int f = 1, x = 0; char ch = getchar(); while (!(ch >= '0' && ch <= '9')) { if (ch == '-') f *= -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return f * x; } int n, x, last[2], len, ans[200000 + 2...
3
#include<cstdio> #include<algorithm> #include<cstring> #define ll long long using namespace std; const ll MOD=1e9+7; int n; char s[200005]; bool flag[200005]; ll ans,num; int main() { scanf("%d",&n); scanf("%s",s+1); s[0]='W'; for(int i=1;i<=2*n;i++) { if((s[i]==s[i-1]&&flag[i-1]==0)||(s[i]!=s[i-1]&&flag[i-1]==1...
0
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 50; const int inf = 0x3f3f3f3f; const int mod = 1e9 + 7; int n; long long ans; int val[maxn]; int head[maxn], nxt[maxn << 1], to[maxn << 1], edge_tot; void add_edge(int u, int v) { nxt[++edge_tot] = head[u]; head[u] = edge_tot; to[edge_tot] = v;...
5
#include <bits/stdc++.h> using namespace std; int cR[3], cG[3], cB[3], s[6]; int main() { int n; cin >> n; string str; cin >> str; for (int k = 0; k < 3; k++) { for (int i = k; i < n; i += 3) { switch (str[i]) { case 'R': cR[k]++; break; case 'G': cG[k]+...
3
#include <bits/stdc++.h> using namespace std; const int maxn = 100100; int n; vector<int> g[maxn]; set<pair<int, int> > s[maxn]; int f[maxn]; vector<int> ans; void pfs(int v) { for (int u : g[v]) { pfs(u); s[v].insert({f[u], u}); f[v] = max(f[v], f[u] + 1); } } void dfs(int v) { cout << v << " "; wh...
6
#include<iostream> #include<algorithm> #include<map> #include<vector> using namespace std; int n; int main() { while(cin>>n,n){ vector<int>a;map<int,int>M; vector<pair<int,pair<int,int> > >I; for(int i=0;i<n;i++) { int m,aa,b;cin>>m>>aa>>b; a.push_back(aa);a.p...
0
#include <bits/stdc++.h> using namespace std; struct Initializer { Initializer() { cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(15); } } initializer; int main() { int n; string a, b; cin >> n >> a >> b; int res = 0; for (int i = 0; i < n; ++i) { res += min((a[i] - b[i...
1
#include <bits/stdc++.h> using namespace std; int main() { int q; scanf("%d", &q); while (q--) { long long n, m, k, len = 0, ans = 0; scanf("%I64d%I64d%I64d", &n, &m, &k); len = max(n, m); if (k < len) printf("-1\n"); else printf("%I64d\n", k - (k - n) % 2 - (k - m) % 2); } }
2
#include <bits/stdc++.h> using namespace std; long long int n, e, a[1000100], l, m, r = 1e13; ; bool ok(long long int x) { long long int sum = 0, y, z, k; for (int i = 0; i < n; ++i) { y = a[i]; z = y >= x; k = (y + 1) / 2; while (k > x) z *= 2, y = k, k = (y + 1) / 2; if (k == x) z *= 2, z = ma...
5
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 10, NIL = -1; int n, m; int to[MAX * 2], nxt[MAX * 2], head[MAX], e; bool vis[MAX]; priority_queue<int, vector<int>, greater<int> > pq; void init_gr() { memset(head, NIL, sizeof(head)); e = 0; } void addedge(int f, int t) { to[e] = t, nxt[e] = head...
4
#include <bits/stdc++.h> using namespace std; int power2(int exp) { if (exp == 0) return 1; else return 2 << exp - 1; } int main() { string s; cin >> s; vector<int> str; for (int i = 0; i < s.size(); i++) { int a = s[i] - '0'; str.push_back(a); } reverse(str.begin(), str.end()); int t ...
2
#include <bits/stdc++.h> using namespace std; bool compTasks(pair<int, int> a, pair<int, int> b) { if (a.first != b.first) { return a.first < b.first; } else { return a.second < b.second; } } int main() { int a; cin >> a; vector<pair<int, int>> tasks; for (int i = 0; i < a; i++) { int c, d; ...
1
#include <bits/stdc++.h> using namespace std; pair<int, int> D[1000000]; int state[1000000]; int n, mx = 0; int main() { scanf("%d", &n); for (int i = 0, size = 0; i < n; ++i, size += 2) { int x, w; scanf("%d%d", &x, &w); D[size].first = x - w; D[size].second = i + 1; D[size + 1].first = x + w; ...
2
#include <bits/stdc++.h> using namespace std; int n, k, i, s = 0, c = 0; int main() { cin >> n >> k; int a[105]; for (i = 0; i < k; i++) { cin >> a[i]; if (a[i] % 2 != 0) { a[i]++; c++; } s += a[i]; } if (s > 8 * n) cout << "NO"; else if (s == 8 * n) { if (k == 4 * n && c...
2
#define _USE_MATH_DEFINES #include <cstdio> #include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <algorithm> #include <cmath> #include <complex> #include <string> #include <vector> #include <list> #include <queue> #include <stack> #include <set> #include <map> #include <bitset> #include...
0
#include <bits/stdc++.h> using namespace std; using ulong = unsigned long; using ll = long long; const int M = 1e9 + 7; int main() { int n; cin >> n; bool coupon = false; for (int i = 0; i < n; i++) { int a; cin >> a; if (coupon) { if (a == 0) { for (; i < n; i++) { cin >> a;...
2
#include <bits/stdc++.h> using namespace std; int main() { char s[100010]; char s1[100010]; scanf("%s", s); int i, j, k; int total = 0; i = 0; j = -1; while (s[i]) { if (j > -1 && s[i] == s1[j]) { i++; total++; j--; } else { s1[++j] = s[i++]; } } if (total % 2 == ...
2
#include <bits/stdc++.h> using namespace std; int main() { int n, p, s, s1 = 0, s2 = 0; cin >> n; for (int i = 0; i < n; ++i) { cin >> p; s = s + p; if (p % 2 == 0) { s1 = s1 + 1; } } s2 = n - s1; if (s % 2 == 0) { cout << s1; } else { cout << s2; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); long long int a, b, ans = 0; cin >> a >> b; ans = ((b * (b - 1)) / 2) % 1000000007; ans *= ((((a * (a + 1)) / 2) % 1000000007) * b + a) % 1000000007; ans = ans % 1000000007; cout << ans << "\n"; return 0; }
3
#include <bits/stdc++.h> const int MAXN = 200007; using namespace std; class Node { public: int maxy; int l, r; int s[2]; Node() {} }; Node t[MAXN << 2]; int op[MAXN][3]; vector<int> pos; vector<int> a; vector<multiset<int>> st; inline int get_mid(int l, int r) { return (l + r) >> 1; } inline void refresh(int ...
4
#include <bits/stdc++.h> #include <math.h> using namespace std; typedef long long ll; typedef unsigned long long ull; # define M_PI 3.14159265358979323846 const int M=1e9+7; long long mod(long long x){ return ((x%M + M)%M); } ...
2
#include <bits/stdc++.h> using namespace std; long long n, s, INI; pair<long long, long long> a[200010]; bool op(long long meta) { long long quant = 0, disponivel = s; for (long long i = n; i >= 1; i--) { long long vez = meta - a[i].first; if (a[i].first >= meta) { quant++; continue; } i...
4
#include <bits/stdc++.h> using namespace std; const int N = 2012; int f[N][N][2]; int n, m, k, flag; int ex, ey; inline int find(int x, int y, int p) { if (y < 0) return y; return f[x][y][p] == y ? y : f[x][y][p] = find(x, f[x][y][p], p); } int dis(int x, int y) { return abs(ex - x) + abs(ey - y); } inline void chk...
1
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (a == 0 || a == b) { return b; } return gcd(b % a, a); } int main() { long long int n; cin >> n; vector<long long int> a(n), b(n); for (long long int i = 0; i < n; i++) { cin >> a[i]; } for (...
4
#include <bits/stdc++.h> using namespace std; const long long M = 1e9 + 7ll; long long n, c, ans = 1, num; struct matrix { long long n, m, mat[6][6]; matrix operator*(const matrix b) const { matrix res = {n, b.m}; memset(res.mat, 0, sizeof(res.mat)); for (long long k = 1; k <= m; k++) for (long lo...
5
#include <bits/stdc++.h> using namespace std; long long denom = 31607; long long powmod(long long a, long long b) { long long res = 1; a %= denom; for (; b; b >>= 1) { if (b & 1) res = res * a % denom; a = a * a % denom; } return res; } int main(int argc, const char* argv[]) { ios::sync_with_stdio(f...
2
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; long long a[maxn]; long long p[maxn]; double me[maxn]; void ce() { long long n, t; double c; cin >> n >> t >> c; double mean = 0; for (long long i = 0; i < n; i++) { scanf("%d", &a[i]); p[i + 1] = a[i]; } for (long long i = 2...
2
#include <bits/stdc++.h> using namespace std; signed main() { long long n, m; cin >> n >> m; vector<string> vec(n); for (long long i = 0; i < n; ++i) cin >> vec[i]; bool ch1 = 0; for (long long i = 0; i < n; ++i) { bool a = 0, b = 0; for (long long j = 0; j < m; ++j) { if (vec[i][j] == '#') { ...
4
#include <bits/stdc++.h> #define N 30 using namespace std; struct dat{ int l,r,par,sib,deg,dep,height,type; dat(){l = r = par = sib = deg = dep = height = type = -1;} }; dat T[30]; int root; char str[3][20] = {"root", "internal node", "leaf"}; void pr(int id){ printf("node %d: parent = %d, sibling = %d, degree ...
0
#include <bits/stdc++.h> using namespace std; typedef __int128 ll; const int n = 2e5+10; const ll sz = 34; ll N; ll A[n][40]; ll dp1[n][40]; ll dp2[n][40]; /* void printDp1(){ for(int i=0; i<N; i++){ for(int j=0; j<sz; j++){ cout << A[i][j] << ":" << dp1[i][j] << " "; } cout << endl; } cout...
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; vector<int> pos, neg; set<int> t1, t2; map<int, int> m; cin >> n; for (int i = 0; i < n; i++) { int x; cin >> x; m[x]++; if (x >= 0) t1.insert(x); else { t2....
3
#include <bits/stdc++.h> using namespace std; long long mex(map<long long, long long> &mp, long long n) { for (int i = 0; i < n; i++) if (!mp[i]) return i; return n; } int main() { int t = 1; cin >> t; while (t--) { long long n; cin >> n; long long arr[n]; vector<int> ans; map<long lon...
4
#include <bits/stdc++.h> using namespace std; int H, W; int A[30], B[30]; string S[50]; set<int> cand[5]; int p12[6], tar; map<pair<int, int>, pair<int, int> > from[21]; int ok(int curv, int c) { int i; for (i = 0; i < H; i++) { int seg = curv / p12[i] % 12; if (seg > A[i]) return 0; if (seg + (W - c) /...
6
#include <bits/stdc++.h> using namespace std; const int SIZE = 1e5 + 2; const int MOD = 1e9 + 7; const unsigned long long INF = (unsigned long long)(-1LL); int n, t, c; int main() { cin >> n >> t >> c; int continuous = 0; int ans = 0; for (int i = 0; i < n; i++) { int d; scanf("%d", &d); if (d <= t)...
2
#include <bits/stdc++.h> using namespace std; const long long N = 3e5 + 7; const long long M = 1e9 + 7; const long long MAXN = 2e18 + 7; const long long Mod = 998244353; long long _, i, j, k, n, m, p, s, T, t, l, r, o, u, v, w, x, y, z, ans, nex, sum, num, len, en, sx, sy, tx, ty, th, ma, mi, mod, cnt, la, op, res,...
6
#include<cstdio> #include<algorithm> using namespace std; typedef long long ll; ll ans=0,sum=0,n,i,a[100005],b[100005]; int main() { scanf("%lld",&n); for(i=1;i<=n;i++)scanf("%lld",&a[i]); for(i=1;i<=n;i++)scanf("%lld",&b[i]),a[i]-=b[i]; sort(a+1,a+n+1); for(i=1;a[i]<0&&i<=n;i++)sum-=a[i],ans++; for(i=n;a[i]>0&&i...
0
#include <bits/stdc++.h> #define cal(a,b) ((a+=b)%=mo) #define cross(a,b,c) ((b-a)*(c-a)) using namespace std; const int N=250,mo=998244353; const double eps=1e-7; int p[N],n,ans; struct data{ int x,y; data operator - (const data &c)const{ return (data){x-c.x,y-c.y}; } int operator * (const data &c)con...
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e2 + 5; int n; long long k; string s; int pre[maxn], lst[maxn]; long long dp[maxn][maxn]; int main() { scanf("%d%lld", &n, &k); cin >> s; s = "!" + s; for (int i = 1; i <= n; i++) { pre[i] = lst[s[i] - 'a']; lst[s[i] - 'a'] = i; } for (...
5
#include <bits/stdc++.h> using namespace std; stack<int> cnt; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; string s; cin >> s; for (int i = 0; i < signed(s.size()); i++) { if (s[i] == '1') cnt.push(i); else if (!cnt.empty()) cnt.pop(); } while (!cnt.empty()...
4
#include <bits/stdc++.h> long long n, m; int main() { while (scanf("%lld", &n) != EOF) { n++; long long ans; if (n % 2 == 0) ans = n / 2; else ans = n; if (n == 1) ans = 0; printf("%lld\n", ans); } }
1
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<ll, ll> P; #define EACH(i,a) for (auto& i : a) #define FOR(i,a,b) for (ll i=(a);i<(b);i++) #define RFOR(i,a,b) for (ll i=(b)-1;i>=(a);i--) #define REP(i,n) for (ll i=0;i<(n);i++) #...
0
#include <bits/stdc++.h> typedef long long mat[4][4], (*pmat)[4]; const int N = 200054; const long long INF = 0x3f3f3f3f3f3f3f3fll; long long n; int len; char s[N]; std::vector<int> all[4]; mat pw[64], cur, B1, B2; pmat fy = *pw; inline void down(long long &x, const long long y) { x > y ? x = y : 0; } namespace SAM { i...
5
#include <bits/stdc++.h> using namespace std; int main() { string n, m; int x = 0; cin >> n >> m; sort(m.rbegin(), m.rend()); for (int i = 0; i < n.size(); i++) { if (n[i] < m[x]) { swap(n[i], m[x]); x++; } } cout << n; return 0; }
2
#include <bits/stdc++.h> using namespace std; bool cmp(int a, int b) { return a > b; } int al[112345], bo[112345]; int main() { int n, k, m; while (~scanf("%d%d%d", &n, &m, &k)) { for (int i = 0; i < n; ++i) { scanf("%d", al + i); } for (int i = 0; i < m; ++i) { scanf("%d", bo + i); } ...
4
#include<iostream> using namespace std; int main() { int c1, c2, c3; int n, max; while (cin>>c1>>c2>>c3) { n = 20-c1-c2; if (n<=3) { cout<<"NO"<<endl; } else { max = n; if (n>=c1) max--; if (n>=c2) max--; if (n>=c3) max--; if ( (double)max / 7.0 >= 0.5 ) cout<<"YES"<<endl; else cout<<"...
0
#include <bits/stdc++.h> using namespace std; long double rhs; bool valid(long double t, long double k, long double b, long double n, long double x) { long double lhs = (t * log2(k)) + log2((k - 1) * x + b); return lhs >= rhs; } int main() { ios::sync_with_stdio(0); cin.tie(0); long long k, b, n, x...
1
#include <bits/stdc++.h> using namespace std; const long long inf = 1000000007; vector<vector<long long> > dop; void prin(vector<vector<long long> > a) { cout << endl << endl; for (long long c = 0; c < a.size(); c++) { for (long long i = 0; i < a[c].size(); i++) cout << a[c][i] << " "; cout << endl; } } v...
6
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int main() { string s; cin >> s; cout << s.substr(0, s.size() - 8); }
0
#include <bits/stdc++.h> #define MAX 10000000000; #define r(i,n) for(int i=0;i<n;i++) using namespace std; int main(){ long long a,b; cin>>a>>b; long long dp[a+1][b+1],a1[a+1],b1[b+1]; r(i,a)cin>>a1[i+1]; r(i,b)cin>>b1[i+1]; r(i,a+1)r(j,b+1)dp[i][j]=MAX; for(int i=1;i<=a;i++) for(int j=1;j<=b;j++){ if(i==1)...
0
#include <bits/stdc++.h> using namespace std; const int MOD = 1e9 + 7; const int N = 2e5 + 11; int a[5][5], b[5][5]; int dp[5][5]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long k; int a1, b1; cin >> k >> a1 >> b1; for (int i = 1; i <= 3; i++) for (int j = 1; j <= 3; j++...
3
#include <bits/stdc++.h> using namespace std; const int N = (int)1e6 + 7; int n, a[N], t, ans, x, y, k; double dist; bool ok = false, nat = false; int main() { cin >> x >> y; dist = sqrt(x * x * 1.0 + y * y * 1.0); while (k * k <= x * x + y * y) { if (k * k == x * x + y * y) nat = true; k++; } k -= 1;...
1
#include <bits/stdc++.h> using namespace std; int k[3], t[3], p[3]; long long wd[3][100005]; int main() { int n, i; scanf("%d%d%d", &k[0], &k[1], &k[2]); scanf("%d%d%d", &t[0], &t[1], &t[2]); scanf("%d", &n); if (n < k[0]) k[0] = n; if (n < k[1]) k[1] = n; if (n < k[2]) k[2] = n; long long c, mx = 0, sv...
2
#include<algorithm> #include<iostream> #include<string> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> using namespace std; int main(){ int i,j,k; int a[22][22],m,n,w,h,x,y,vx,vy; char c; cin>>n; for(i=0;i<n;i++){ cin>>h>>w; memset(a,-1,sizeof(a)); for(j=1;j<h+1;j++){ f...
0
#include<bits/stdc++.h> #define ll long long #define N 505 #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,a,n) for (int i=n;i>=a;i--) #define inf 0x3f3f3f3f #define pb push_back #define mp make_pair #define pii pair<int,int> #define fi first #define se second #define lowbit(i) ((i)&(-i)) #define VI vector<int>...
5
#include <bits/stdc++.h> using namespace std; const long long int N = 1e6 + 1; long long int prime[N]; vector<long long int> p; void precompute() { for (long long int i = 2; i < N; i++) if (!prime[i]) for (long long int j = 2 * i; j < N; j += i) prime[j] = i; } signed main() { ios_base::sync_with_stdio(0)...
1
#include <bits/stdc++.h> using namespace std; const int N = 1000 * 100 + 5; int n, m, k, a[N][15], ans[N], cnt[20], c[N][15]; int main() { scanf("%d %d %d\n", &n, &m, &k); int i, j; for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { scanf("%d", &a[i][j]); } } int x, y; for (i = 1; i <= k; i...
2
#include <bits/stdc++.h> using namespace std; long long mod = 998244353; long long power(long long base, long long exponent, long long modx = mod) { long long ret = 1 % modx; base %= modx; while (exponent) { if (exponent & 1) { ret = (ret * base) % modx; } base = (base * base) % modx; expone...
6
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; int main() { ll t, n, m, t1, x, y; cin >> t; for (int i = 0; i < t; i += 1) { cin >> n >> m; vector<long long> undirCount(n); vector<long long> deg(n); vector<vector<long long>> backEdge(n); vector<vect...
5
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; const int MOD = 1e9 + 7; long long dp[N][2]; void solve() { memset(dp, 0, sizeof(dp)); string a, b; cin >> a >> b; (a == b ? dp[0][1] = 1 : dp[0][0] = 1); int k; cin >> k; long long way_good = 0, way_bad = 0; string s = a; for (int ...
4
#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); } tem...
5
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cout.setf(ios::fixed); cout.precision(20); int n, x; cin >> n >> x; int y = x; long long ans = 0; for (int i = (1); i <= (n); ++i) { int l, r; cin >> l >> r; if (y < l) { ans += l - y; x = y; ...
6
#include <bits/stdc++.h> using namespace std; const int MAXN = 200005; long long a[MAXN]; struct polygon_convex { vector<complex<long long> > p; } res; inline long long cross(const complex<long long>& a, const complex<long long>& b) { return (conj(a) * b).imag(); } long long calc(int i, int x...
5
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC optimize("unroll-loops") #pragma GCC target("sse4") using namespace std; long long a[100005]; long long f[100005], b[100005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long n, k; cin >> n >> k; long long i, j; ...
3
#include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n >> m; int r = 1; bool l = false; while (r <= n) { if (r % 2 == 1) { for (int i = 0; i < m; i++) cout << "#"; cout << endl; } else if (r % 2 == 0 && l == false) { for (int j = 0; j < m - 1; j++) cout << ".";...
1
#include <bits/stdc++.h> using namespace std; const int maxn = 210; struct Edge { int to, cap, rev; Edge(int _to = 0, int _cap = 0, int _rev = 0) : to(_to), cap(_cap), rev(_rev) {} }; vector<Edge> g[maxn]; int level[maxn], iter[maxn]; inline void add_edge(int u, int v, int cap) { g[u].push_back(Edge(v, cap,...
5
#include <bits/stdc++.h> using namespace std; const int maxn = 500010; const int maxm = 1010; const int inf = 1e8; const int mod = 1e9 + 7; int n, m; struct Child { int l; int r; } cd[maxn]; int cnt[maxn]; int main() { int i, j; int k; scanf("%d%d", &n, &m); memset(cnt, 0, sizeof(cnt)); for (i = 1; i <= n...
3
#include <bits/stdc++.h> using namespace std; int n, a, b; string s; int main() { cin >> n >> a >> b >> s; vector<vector<int>> dp(n + 1, vector<int>(n + 1)); vector<int> to(n); vector<int> res(n + 1); for (int i = n - 1; i > -1; i--) { for (int j = 0; j < i; j++) { if (s[i] == s[j]) dp[j][i]...
6
#include <bits/stdc++.h> using namespace std; int main() { int sen; string line; bool Freda, Rainbow; while (cin >> sen) { cin.ignore(); for (int i = 0; i < sen; i++) { Freda = Rainbow = false; getline(cin, line); if (line.size() >= 4 && line.substr(0, 5) == "miao.") Rainbow = true; ...
1
#include <bits/stdc++.h> using namespace std; template <class T> void pr1(T a) { cout << a; cout << '\n'; } template <class T, class T2> void pr2(T a, T2 b) { cout << a << ' ' << b; cout << '\n'; } template <class T, class T2, class T3> void pr3(T a, T2 b, T3 c) { cout << a << ' ' << b << ' ' << c; cout << ...
1
#include <bits/stdc++.h> using namespace std; template <typename T> void print(T &v) { for (auto i = v.begin(); i != v.end(); ++i) { cout << *i << ' '; } cout << endl; } template <typename T> void scan(vector<T> &v) { for (int64_t i = 0; i < v.size(); ++i) { cin >> v[i]; } } int32_t main() { int64_t...
2
#include <bits/stdc++.h> using namespace std; bitset<512> cur, was, is[512]; vector<int> G[512]; vector<pair<int, int> > edges; bool check(int v) { was[v] = cur[v] = true; for (int u : G[v]) if (is[v][u]) if (cur[u]) return true; else if (!was[u] && check(u)) return true; return cu...
4
#include <bits/stdc++.h> using namespace std; string to_string(bool b) { return (b ? "true" : "false"); } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { ...
2
//#define _GRIBCXX_DEBUG #include <bits/stdc++.h> using namespace std; #define int long long signed main() { int n, p; cin >> n >> p; int cnt[2] = {0}; for(int i=0; i<n; ++i) { int a; cin >> a; cnt[a%2]++; } if(cnt[1]) cout << (1ll<<(n-1)) << endl; else cout << (p?0...
0
#include <bits/stdc++.h> using namespace std; const long long N = 1e5; long long mod = 1e9 + 7; string s; int a[2601][101], n; long long cnt; void boost() { ios_base ::sync_with_stdio(0); cin.tie(0), cout.tie(0); } int add(int a, int b) { a += b; if (a >= mod) a -= mod; return a; } void solve() { boost(); ...
3
#include <bits/stdc++.h> std::mt19937 rng( (int)std::chrono::steady_clock::now().time_since_epoch().count()); const int MOD = 998244353; const int me = 19; const int ms = 1 << me; long long fexp(long long x, long long e) { long long ans = 1; x %= MOD; for (; e > 0; e /= 2) { if (e & 1) { ans = ans *...
6
#include <bits/stdc++.h> using namespace std; int n; int sx = 0, sy = 0; vector<int> v[400000 + 10]; int get(int x, int a, int b) { if ((x % 2) == sx) return 0; int l = b - a + 1; if (l <= 0) return 0; if ((a % 2) == sy) return l / 2; else return (l + 1) / 2; } void reset() { for (int i = 0; i <= 40...
4
#include <bits/stdc++.h> using namespace std; long long n; vector<string> f, s; vector<long long> p; int main() { cin.tie(0); ios::sync_with_stdio(0); cin >> n; f.resize(n); s = f; p.resize(n); for (long long i = 0; i < (n); ++i) { cin >> f[i] >> s[i]; if (s[i] < f[i]) swap(s[i], f[i]); } for ...
3
#include <bits/stdc++.h> using namespace std; #define ll long long #define ull unsigned long long #define pb push_back #define mp make_pair #define all(v) v.begin() , v.end() #define gcd(x,y) __gcd(x,y) #define lcm(x,y) ( x / __gcd(x,y) ) * y #define nl ...
1
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); struct P { double x, y; P(double x = 0, double y = 0) : x(x), y(y) {} P operator-(const P& b) { return P(x - b.x, y - b.y); } P operator+(const P& b) { return P(x + b.x, y + b.y); } double operator&(const P& b) { return x * b.x + y * ...
5
#include <bits/stdc++.h> using namespace std; const int maxs = (int)1e6 + 1; char s[maxs]; int tmp[2][3]; int tr[3]; int make_s(int v1, int v2, char operation) { tmp[0][0] = (v1 & 1), tmp[0][1] = !!(v1 & 2), tmp[0][2] = !!(v1 & 4); tmp[1][0] = (v2 & 1), tmp[1][1] = !!(v2 & 2), tmp[1][2] = !!(v2 & 4); switch (oper...
3
#include <bits/stdc++.h> using namespace std; const int MAX = 100100; long long sum(int n) { long long ans = 0; for (int i = 1; i <= n; i++) { ans += i; } return ans; } int main() { long long x, d; long long ans = 0, n, m; cin >> n >> m; long long t1 = sum(n - 1); if (n % 2 == 1) { long long t...
3
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); int x = sqrt(n); if (x * x == n) printf("%d\n", x * 4); else { int dif = n - (x * x); if (dif > x) printf("%d\n", x * 4 + 4); else printf("%d\n", x * 4 + 2); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int t; map<int, int> has; long long fj[55][50], gs[55], bh; long long n, k; const long long S = 31624000; const long long mf = 1e5 + 10; bool is[S]; long long p[2000000]; void init() { for (int i = 2; i < S; i++) { if (!is[i]) p[++p[0]] = i; for (int j = 1; j <= p...
6
#include <bits/stdc++.h> using namespace std; int n, m, k; char a[101][101]; void rec(int x, int y) { a[x][y] = '.'; if (a[x + 1][y] == 'B') rec(x + 1, y); if (a[x][y + 1] == 'B') rec(x, y + 1); if (a[x - 1][y] == 'B') rec(x - 1, y); if (a[x][y - 1] == 'B') rec(x, y - 1); } int main() { ios_base::sync_with_...
4
#include <bits/stdc++.h> using namespace std; int root[1005]; void Init() { for (int i = 0; i < 1005; ++i) root[i] = i; } int Find(int x) { if (root[x] != x) root[x] = Find(root[x]); return root[x]; } void Merge(int x, int y) { int fx = Find(x), fy = Find(y); if (fx != fy) root[fx] = fy; } int n, x, nx; int n...
2
#include <bits/stdc++.h> using namespace std; const long long int mod = 1e9 + 7; const int N = 100005; long long int q, n; string s, t; int32_t main() { ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> q; while (q--) { cin >> n >> s >> t; long long int ans = n; for (long long int i = 0; i ...
5
#include <bits/stdc++.h> using namespace std; string s, u; long long j, i, n, m, ans, cur; int main() { cin >> s; cin >> u; n = u.size(); for (i = 1; i <= n + 1; i++) s = 'A' + s; for (i = 1; i <= n + 1; i++) s += 'A'; m = s.size(); ans = 100000; for (i = 0; i <= m - n; i++) { cur = 0; for (j = ...
3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:16777216") using namespace std; template <class T> inline T sqr(T x) { return x * x; } const double eps = 1e-9; const double pi = acos(-1.0); const int maxn = (int)1e5 + 10; int r[maxn], used[maxn]; int main() { int a, b, m; cin >> a >> b >> m >> r[0]; fo...
2