solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int b[1001][1001]; char b1[1001][1001]; bool stug(int i, int j) { if (b[i - 1][j - 1] && b[i - 1][j] && b[i - 1][j + 1] && b[i][j - 1] && b[i][j + 1] && b[i + 1][j - 1] && b[i + 1][j] && b[i + 1][j + 1]) { return 1; } return 0; } void maqr(int i, int j) { ...
2
#include <bits/stdc++.h> using namespace std; int n; int out(int x) { int l, r; if (x > 0) l = n - x, r = n; else l = 1 - x, r = 1; cout << "?"; for (int i = 1; i <= n; i++) cout << " " << (i != n ? l : r); cout << endl; int ret; cin >> ret; return ret; } int a[105]; int main() { std::ios::s...
4
#include <bits/stdc++.h> using namespace std; int p[100010], n, m; map<pair<int, int>, bool> vis; struct edge { int u, v; } e[20]; int bit[100010], c, du[100010]; int main() { scanf("%d%d", &n, &m); for (int i = 1, u, v; i <= m; i++) { scanf("%d%d", &u, &v); vis[pair<int, int>(u, v)] = vis[pair<int, int>(...
3
#include<bits/stdc++.h> using namespace std; int a[200005]; long long sum1; int main() { int n;cin>>n; for(int i=1;i<=n;i++) { cin>>a[i]; sum1=sum1^a[i]; } for(int i=1;i<=n;i++) { cout<<(sum1^a[i])<<' '; } }
0
#include <iostream> #include <vector> using namespace std; int main(){ int n; cin >> n; vector<int> data(n); int count = 0; for(auto &a:data){ cin >> a; } for(int i = 0; i < n; i++){ for(int j = n - 1; j > i; j--){ if(data[j] < data[j-1]){ swap(data[j],data[j-1]); count++; } } } for(int i ...
0
#include <bits/stdc++.h> const int N = (int)5e5 + 228; using namespace std; int n, nn, m, t[N * 4], pair_b[N]; deque<int> dq; void build() { nn = 1; while (nn < n) nn *= 2; for (int i = 1; i <= n; i++) t[i + nn - 1] = 1; for (int i = nn - 1; i >= 1; i--) t[i] = t[i * 2] + t[i * 2 + 1]; } void upd(int l, int r, ...
5
#include <bits/stdc++.h> using namespace std; int n, m, i, j, k, res, x, y; bool f; map<string, int> M; char str[11], A[17][11]; bool B[17][17]; vector<string> V; int main() { scanf("%d%d", &n, &m); for (i = 0; i < n; i++) { scanf("%s", A[i]); M[A[i]] = i; } for (i = 0; i < m; i++) { scanf("%s", str...
2
#include <bits/stdc++.h> using namespace std; void setprime(bool isprime[], long long n); long long BinarySearch(long long arr[], long long first, long long last, long long search); long long choose(long long n, long long k); void showArray(long long a[], long long n); long long gcd(long long a, ...
1
#include<iostream> #include<algorithm> #include<set> #include<queue> using namespace std; #define rep(i,n) for(int i=0;i<n;i++) #define rp(i,c) for(int i=0;i<(c).size();i++) #define fr(i,c) for(__typeof((c).begin()) i=(c).begin();i!=(c).begin();i++) #define mp make_pair #define pb push_back typedef vector<int> vi; typ...
0
#include <bits/stdc++.h> using namespace std; const long long MOD = 1000 * 1000 * 1000 + 7; const long long INF = 1ll * 1000 * 1000 * 1000 * 1000 * 1000 * 1000 + 7; const long long MOD2 = 998244353; const long long N = 1000 * 100 + 10; const long long N2 = 70; const long double PI = 3.141592653589793; long long gcd(lon...
1
#include <bits/stdc++.h> using namespace std; template <class T> inline bool fs(T &x) { int c = getchar(); int sgn = 1; while (~c && c < '0' || c > '9') { if (c == '-') sgn = -1; c = getchar(); } for (x = 0; ~c && '0' <= c && c <= '9'; c = getchar()) x = x * 10 + c - '0'; x *= sgn; return ~c; } in...
1
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int ans, lens, lent, nxt[maxn], Max[maxn]; char s[maxn], t[maxn]; void Getnxt() { nxt[1] = 0; int j = 0; for (int i = 2; i <= lent; i++) { while (j && t[i] != t[j + 1]) j = nxt[j]; if (t[i] == t[j + 1]) j++; nxt[i] = j; } } int ...
5
#include <bits/stdc++.h> using namespace std; int main() { int x; while (cin >> x) { int flag = 0; for (int i = x; i >= 1; i--) { for (int j = i; j >= 1; j--) { if (i % j == 0) { if (i * j > x && i / j < x) { cout << i << " " << j << endl; flag = 1; ...
1
#include<bits/stdc++.h> using namespace std; #define ll long long int main(){ ll n,x; cin>>n; ll sz[n+5], ssz[n+5]; fill_n(sz,n+5,0); fill_n(ssz,n+5,0); for(int i=0; i<n; i++){ cin>>x; sz[--x]++; } ll mx=-1,sm=0; for(int i=0; i<n; i++){ if(sz[i]>0) ssz[sz[i]]++; sm+=sz[i]; if(mx<sz[i]) mx=sz[i]; }...
0
#include <bits/stdc++.h> using namespace std; int main() { string str; cin >> str; int n = str.size(); int zero = -1, two = -1, five = -1, seven = -1; for (int i = n - 1; i >= 0; i -= 1) { if (str[i] == '0') zero = max(zero, i); else if (str[i] == '2') two = max(two, i); else if (str[i...
5
#include <bits/stdc++.h> using namespace std; map<string, int> notes; int main() { const string mas[12] = {"C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "B", "H"}; for (int i = 0; i < 12; i++) notes[mas[i]] = i; string mas2[3]; for (int i = 0; i < 3; i++) cin >> mas2[i]; ...
1
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 10; const int MOD = 1000000007; int a[N]; long long power(long long a, long long b) { long long ans = 1; for (long long i = 0; i < b; i++) ans *= a; return ans; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n...
3
#include <bits/stdc++.h> using namespace std; const int maxn = 3e5; int n, m, k, q; struct node { int v; int nxt; long long w; node() {} node(int v, long long w) : v(v), w(w) {} } edge[maxn * 2 + 10]; struct Node { int u; int v; long long w; Node() {} Node(int u, int v, long long w) : u(u), v(v), w(...
6
#include <bits/stdc++.h> using namespace std; template <typename T> void maxtt(T& t1, T t2) { t1 = max(t1, t2); } template <typename T> void mintt(T& t1, T t2) { t1 = min(t1, t2); } bool debug = 0; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; string direc = "URDL"; long long ln, lk, lm; void etp(b...
5
#include <bits/stdc++.h> using namespace std; int main() { int s; cin >> s; int a[s]; for (int i = 0; i < s; i++) cin >> a[i]; sort(a, a + s); if (s == 1 || s == 2) { cout << "0" << endl; return 0; } int t = s - 2; for (int i = 1; i < s - 1; i++) { if (a[i] > a[0] && a[i] < a[s - 1]) ...
1
#include <bits/stdc++.h> #pragma GCC optimize("O2,unroll-loops") using namespace std; const int MAXN = 600010; int n, m, k, u, v, x, y, t, a, b; int par[MAXN], col[MAXN]; long long s0[MAXN], s1[MAXN], ans; vector<int> vec[MAXN]; string S; pair<int, int> Getpar(int x) { if (par[x] == x) return {x, 0}; pair<int, int>...
3
#include <bits/stdc++.h> using namespace std; int main() { int n, l, i; string s; cin >> n; while (n--) { cin >> l; cin >> s; for (i = 0; s[i] != '8' && i < l - 1; i++) ; if (i + 11 <= l) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int N; inline int ask(int x) { int l, r; cout << "? " << x + 1 << endl; cin >> l; cout << "? " << x + N / 2 + 1 << endl; cin >> r; if (l == r) cout << "! " << x + 1 << endl, exit(0); return l - r; } int main() { ios::sync_with_stdio(false); cin.tie(0); c...
2
#include <bits/stdc++.h> using namespace std; int a[1010]; int main() { int s, k; cin >> s; for (k = 0; k < s; k++) cin >> a[k]; sort(a, a + s); cout << a[s - 1] - a[0] - s + 1 << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; template <typename T1, typename T2> inline void chkmin(T1 &x, T2 y) { if (x > y) x = y; } template <typename T1, typename T2> inline void chkmax(T1 &x, T2 y) { if (x < y) x = y; } inline int readChar(); template <class T = int> inline T readInt(); template <class T> inl...
3
#include <bits/stdc++.h> using namespace std; const int N = 300010; inline int in() { int x; scanf("%d", &x); return x; } inline long long lin() { long long x; scanf("%I64d", &x); return x; } int n; struct Rank { long long bal, wi; } inp[N]; struct Data { long long bal, wi, diff; Data() {} Data(long...
4
#include<cmath> #include<iostream> using namespace std; #define SQ(x)((x)*(x)) int main(){ int N; cin >> N; while(N){ N--; int x,y,ax,ay; x=y=0; ax=ay=0; for(;;){ int dx,dy; cin >>dx>>dy; x+=dx; y+=dy; //cout << "#" << dx<<" " << dy<<endl; if (dx==0 && dy==0)break; i...
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e3 + 10; int f[maxn]; void init(int n) { for (int i = 1; i <= n; i++) f[i] = i; } int find(int x) { int r = x, i = x, t; while (r != f[r]) r = f[r]; while (i != r) { t = f[i]; f[i] = r; i = t; } return r; } int mix(int x, int y) { ...
3
#include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ll x; cin>>x; ll res=0; res+=x/11*2; x%=11; if(x!=0) res++; if(x>6) res++; cout<<res; }
0
#include <bits/stdc++.h> using namespace std; int main(void) { long long int i, j, k, l, counter = 0, n, x = 0, c, c2, ans, c3 = 0; string str; cin >> str >> n; l = str.size(); c = 0, c2 = 0, ans = 0; for (i = l - 1; i >= 0; i--) { if (str[i] == '0') { c++; if (c == n) { for (j = l -...
2
#include <bits/stdc++.h> #pragma GCC optimize(2) using namespace std; inline bool rd(long long &X) { X = 0; int w = 0; char ch = 0; while (!isdigit(ch)) w |= ch == '-', ch = getchar(); while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); X = w ? -X : X; return 1; } char pd[5]; vector<i...
6
#include <bits/stdc++.h> using namespace std; long long r, c, n, len, l, z, x, y, m, t, k, ans; int a[200005], b[200005]; map<int, int> ind; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> a[i]; ind[a[i]] = i; } for (int i = 0; i < n; i++) { cin >> b[i]; } int ii = 0; if (b[ii] == ...
2
#include <bits/stdc++.h> using namespace std; bool compare(pair<long long, long long> a, pair<long long, long long> b) { if (a.second == b.second) return a.first < b.first; return a.second < b.second; } int main() { long long n, i, j, x, r, y; cin >> n >> r; vector<pair<long long, long long> > pos, neg; whi...
6
#include <bits/stdc++.h> using namespace std; template <class T> inline T min(T &a, T &b) { return a < b ? a : b; } template <class T> inline T max(T &a, T &b) { return a > b ? a : b; } template <class T> void read(T &x) { char ch; while ((ch = getchar()) && !isdigit(ch)) ; x = ch - '0'; while ((ch = ge...
4
#include<bits/stdc++.h> using namespace std; int main(){ int N,n,d; int mxx,mxy,mnx,mny; int dx[4]={-1,0,1,0}; int dy[4]={0,-1,0,1}; while(1){ cin>>N; if(N==0) break; mxx=mxy=mnx=mny=0; vector<int> x(N),y(N); for(int i=1;i<N;i++){ cin>>n>>d; x[i]=x[n]+dx[d]; y[i]=y[n...
0
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 7; const long long inf = (long long)1e18; int memo[32][70][2001]; int v[32]; int m; pair<pair<int, int>, int> bc[32][70][2001]; int g[70][70]; bool dp(int i, int j, int k) { if (j == 0 && k == 0 && i == -1) return 1; if (j == 0 || i == -1) retu...
4
#include<bits/stdc++.h> using namespace std; int n; int main(){ cin>>n; long long sol=1; for(int i=1;i<=n;i++){ sol*=i; sol%=1000000000+7; } cout<<sol; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { string s1; string s2; char str[1000000]; scanf("%s", str); s1.assign(str); scanf("%s", str); s2.assign(str); int len1 = s1.size(), len2 = s2.size(); if (len1 < len2) { string s3(len2 - len1, '0'); s3.append(s1); s1.assign(s3); } ...
1
#include <bits/stdc++.h> using namespace std; int main() { string s; char c; getline(cin, s); int i; for (i = 0; i < s.size();) { if (s[i] == ' ' || s[i] == '\t') { if (s[i - 1] == ' ' || s[i - 1] == '\t') s.erase(i, 1); else i++; } if (isalnum(s[i])) { if (s[i - ...
1
#include <bits/stdc++.h> using namespace std; const long long mxn = 5e5 + 10; const long long mxm = 3e6 + 10; const long long INF = 0x3f3f3f3f; const long long mod = 1e9 + 7; const double eps = 1e-7; const double pii = 3.1415926535898; long long gcd(long long x, long long y) { if (y == 0) return x; else ret...
6
#include <bits/stdc++.h> using namespace std; int n; char s1[2000010], s2[2000010]; int main() { scanf("%d%s%s", &n, s1 + 1, s2 + 1); int c11 = 0, c10 = 0, c01 = 0; for (int i = 1; i <= n * 2; ++i) { if (s1[i] == '1' && s2[i] == '1') ++c11; else if (s1[i] == '1' && s2[i] == '0') ++c10; els...
3
#include<iostream> #include<cmath> #include<algorithm> using namespace std; char c[567890]; int a[567890], dp[567890]; int n; int main() { scanf("%d%s", &n, c+1); for (int i=1;i<=n;++i) { if (c[i]=='0') a[i]=i; else a[i]=a[i-1]; ...
0
#include <bits/stdc++.h> using namespace std; const int INF = INT_MAX / 2; struct edge { int u, w, d; }; struct comp { bool operator()(const edge &a, const edge &b) { return a.d < b.d; } }; int main() { int n, c; scanf("%d %d", &n, &c); vector<vector<int> > t(n + 1, vector<int>(2)); for (int j = 0; j < 2; +...
5
#include <bits/stdc++.h> using namespace std; int main() { cin.tie(0); ios_base::sync_with_stdio(0); int n, m, k; int t; cin >> t; while (t--) { cin >> n >> m >> k; int w = min(m, n / k); if (n / k < m) m -= n / k; else m = 0; int s = m / (k - 1); if (m % (k - 1)) s++; ...
1
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, a[N], ans[N], cnt, x, y; bool Ok = 0; int main() { cin >> n; for (int i = 0; i <= n; i++) { cin >> a[i]; if (i) Ok |= (a[i] > 1 && a[i - 1] > 1); } if (!Ok) { cout << "perfect"; } else { cout << "ambiguous" << endl; ...
3
#include <bits/stdc++.h> using namespace std; long long int MAX = 9223372036854775807; long long int MIN = -9223372036854775807; int main() { long long int n, m; cin >> n >> m; vector<long long int> a(m); for (int i = 0; i < m; i++) { cin >> a[i]; } sort(a.begin(), a.end()); long long int x = a[0], k ...
2
#include <bits/stdc++.h> using namespace std; void solve() { int n; cin >> n; if (n <= 5) { cout << "0" << endl; return; } int ans = 0; for (int i = 6; i <= n; i++) { int c = 0; int t = i; for (long long j = 2; j * j <= t; j++) { if ((t % j) == 0) { c++; while ((t %...
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, t, k, d; cin >> n >> t >> k >> d; long long Tm = 0, Tc = 0, Wn = 0, On = 0; while (Tc < n) { Tm += t; Tc += k; } Wn = Tm; Tm = 0, Tc = 0; while (Tc < n) { Tm += t; ...
1
#include <bits/stdc++.h> #define ll long long #define INF 1000000005 #define MOD 1000000007 #define EPS 1e-10 #define rep(i,n) for(int i=0;i<(int)(n);++i) #define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i) #define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i) #define each(a,b) for(auto (a): (b)) #define all(v) (v).begin...
0
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; long long exponentiation(long long base, long long power) { long long ans = 1; while (power > 0) { if (power % 2 == 1) ans = (ans * base) % mod; base = (base * base) % mod; power /= 2; } return ans % mod; } inline int read_int() ...
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 3005; int a[MAXN]; bool b[MAXN]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> a[i]; b[a[i]] = true; } for (int i = 1; i < MAXN; i++) { if (b[i] == false) { cout << i << endl; break; } } }
1
#include <bits/stdc++.h> using namespace std; int main() { int n, i, x = 0, a[100000]; cin >> n; for (i = 0; i < n; i++) { cin >> a[i]; } sort(a, a + n); if (a[0] != 0) x++; for (i = 1; i < n; i++) { if (a[i] != 0 && a[i] != a[i - 1]) x++; } cout << x << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; template <class T> ostream &operator<<(ostream &str, vector<T> &a) { for (auto &i : a) { str << i << " "; } return str; } template <class T> istream &operator>>(istream &str, vector<T> &a) { for (auto &i : a) { str >> i; } return str; } template <class T...
5
#include <bits/stdc++.h> using namespace std; long long H[100005]; long long P[100005]; long long works(long long t, const int& N, const int& M) { int low = 0; for (int i = 0; i < (N); ++i) { if (H[i] >= P[low]) { int new1 = low, new2 = low, new3 = low; if (t >= H[i] - P[low]) { long long tm...
3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n, k; cin >> n >> k; string s; cin >> s; vector<char> c(k, '?'); bool ok = true; for (int i = 0; i < n; i++) { if (s[i] == '?'...
1
#include <bits/stdc++.h> using namespace std; template<typename T> struct Dinitz{ int n, source, sink; vector<vector<T>> adj; struct edge { int u; T flow, cap; }; vector<edge> edges; Dinitz(int n, int source, int sink): n(n), source(source), sink(sink), adj(n) {} int add_edge(int a, int b, T abcap, T bacap =...
0
#include <iostream> #include <cstring> using namespace std; void yaku(int a,int b){ for(int i=2;i<=a&&i<=b;i++){ if(a%i==0&&b%i==0){ a/=i; b/=i; i=2; } } cout<<a<<'/'<<b<<endl; } int main(void){ char s[9]; cin>>s; int k=10,x=0,l=strlen(s),i; ...
0
#include <bits/stdc++.h> using namespace std; int Q, N, K; vector<pair<int, int>> graph[500005]; long long dp[2][500005]; void dfs(int n, int p, int val = 0) { vector<long long> v; long long tot = 0; for (auto e : graph[n]) { if (e.first != p) { dfs(e.first, n, e.second); tot += dp[0][e.first]; ...
3
#include <algorithm> #include <iostream> int main() { std::string s; std::cin >> s; std::cout << std::count(s.begin(), s.end(), '2') << std::endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long a, b, m; cin >> a >> b >> m; if (a >= m || b >= m) { cout << 0 << endl; return 0; } else if (a < 0 && b < 0) { cout << -1 << endl; return 0; } long long _min = min(a, b), _max = max(a, b); if (_max == 0) { cout << -1 ...
1
#include <iostream> #include <cmath> using namespace std; int ans=0; int main(){ int i,j,k,n; cin>>n; for(i=1;i<=n;i++){ int t,x,y; cin>>t>>x>>y; if((x+y)%2==t%2&&t>=x+y)ans++; } if(ans==n)cout<<"Yes"<<endl; else cout<<"No"<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; bool sortbySec(const pair<string, int> &a, const pair<string, int> &b) { return (a.second > b.second); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t, n, m, tCase = 0, a, b; string myName, fName, lName, s; cin >> myName; cin >> n; map<...
2
#include <bits/stdc++.h> using namespace std; const int MX = 35; struct Point { int x, y; Point() {} Point(int _x, int _y) : x(_x), y(_y) {} bool operator==(const Point &b) const { return x == b.x && y == b.y; } bool operator!=(const Point &b) const { return !(*this == b); } }; int N, M, ip[MX][MX], op[MX][MX...
5
#include <bits/stdc++.h> using namespace std; vector<int> primes; void getPrime(int n) { for (int i = 2; i <= n; i++) { int j; for (j = 2; j * j <= i; j++) if (i % j == 0) break; if (j * j > i) primes.push_back(i); } } int isPrime(int num) { for (int i = 2; i * i <= num; i++) if (num % 2 == ...
3
#include<iostream> using namespace std; bool isPrime (int x) { for(int i=2; i*i <=x; i++) { if(x%i==0) return false; } return true; } int N; int main () { cin >> N; long long int ans = 1; for(int i=2; i<=N; i++) { if(isPrime(i)) { long long int r = 1; ...
0
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 20; int dis[N][8], d[8][8], val[N], n; vector<int> G[8]; char S[N]; queue<int> q; inline void push(int v, int c, int D) { if (dis[v][c] <= D) return; dis[v][c] = D, q.push(v); } void BFS(int c) { dis[n + c][c] = 0; q.push(n + c); while (!q.empt...
5
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0, f = 1; 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 x * f; } int n, k, a, b; long lo...
1
#include <bits/stdc++.h> using namespace std; struct Point { int x, y; Point(int x = 0, int y = 0) : x(x), y(y) {} Point operator+(const Point& p) const { return Point(x + p.x, y + p.y); } Point operator-(const Point& p) const { return Point(x - p.x, y - p.y); } void read() { scanf("%d%d", &x, &y); } } a[2005...
4
#include <bits/stdc++.h> using namespace std; long long n, a, b; long long absolute(long long a, long long b) { return (a - b) * (a - b); } long long vec(long long a, long long b) { return (a - b); } bool check_symmetry(vector<pair<long long, long long>> &arr) { vector<long long> dis, dis1; vector<pair<long long, l...
2
#include <bits/stdc++.h> using namespace std; const double pi = acos(-1.0); template <typename T, typename U> inline void amin(T& x, U y) { if (y < x) x = y; } template <typename T, typename U> inline void amax(T& x, U y) { if (x < y) x = y; } template <typename T> inline void write(T x) { int i = 20; char buf[...
5
#include <bits/stdc++.h> using namespace std; int main() { int x, y, z, maxi; cin >> x >> y >> z; maxi = x + y * z; if (x * (y + z) > maxi) maxi = x * (y + z); if (x * y * z > maxi) maxi = x * y * z; if ((x + y) * z > maxi) maxi = (x + y) * z; if ((x * y) + z > maxi) maxi = (x * y) + z; if (x + y + z > ...
1
#include <bits/stdc++.h> using namespace std; int gcdExtended(int a, int b, int *x, int *y) { if (a == 0) { *x = 0, *y = 1; return b; } int x1, y1; int gcd = gcdExtended(b % a, a, &x1, &y1); *x = y1 - (b / a) * x1; *y = x1; return gcd; } long long int modInverse(int a, int m) { int x, y; int g...
2
#include <bits/stdc++.h> using namespace std; template <typename T> bool chkmin(T &x, T y) { return x > y ? x = y, 1 : 0; } template <typename T> bool chkmax(T &x, T y) { return x < y ? x = y, 1 : 0; } int readint() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f =...
4
#include <bits/stdc++.h> using namespace std; int n, A[20] = {}; bool solve(int i, int m) { if (m == 0) return true; if (i >= n) return false; return (solve(i + 1, m) || solve(i + 1, m - A[i])); } int main() { cin >> n; for (int i = 0; i < n; i++) cin >> A[i]; int q, m; cin >> q; for (int i = 0; i < ...
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string S; cin >> S; map<pair<int, int>, int> M; M[make_pair(0, 0)]++; int currx = 0, curry = 0; for (int i = 0; i < S.size(); ++i) { if (S[i] == 'U') { curry++; M[make_pair(currx, curry)]++; } else if (S[i] =...
1
#include <bits/stdc++.h> using namespace std; int n, m, r; long long cover(int x, int y) { return (min(x, n - r) - max(0, x - r + 1) + 1) * (min(y, m - r) - max(0, y - r + 1) + 1); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int k; cin >> n >> m >> r >> k; priority_queue<pair<long ...
4
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10, mod = 1e9 + 7, L = 20; int a[N]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n; cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; a[n + 1] = 1001; int ans = 0; for (int i = 1; i <= n; i++) f...
1
#include<stdio.h> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int N=1000005; int n,i,j,k,head[N],adj[N*2],nxt[N*2],d[N],f[N]; void dfs(int x,int dad) { f[x]=0; int num=0; for(int y=head[x];y;y=nxt[y]) if(adj[y]!=dad) { dfs(adj[y],x); f[x]+=f[adj[y]]; if(!f[adj[y]]) ...
0
#include <bits/stdc++.h> using namespace std; inline void read(int &x) { char c; int f = 1; while (!isdigit(c = getchar())) if (c == '-') f = -1; x = (c & 15); while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15); x *= f; } const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, -1, 0, 1}; int _, n, m...
6
#include <bits/stdc++.h> using namespace std; int n, brr[2001][2001]; int result(int a, int b, int *arr, int h, int l, int r) { int sum = 0, tum = 0, maxi; if (b == n) return 0; if (brr[a][b] != -1) { return brr[a][b]; } if (brr[a][b] == -1) { if (a >= l && a <= r) { sum++; tum++; } ...
5
#include <bits/stdc++.h> using namespace std; const int32_t INFint = 1e9; const long long INFll = 1e18; int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t; cin >> t; while (t--) { long long n; cin >> n; long long arr[n]; for (long long i = 0; i < n; i++) ...
4
#include<bits/stdc++.h> using namespace std; typedef long long int ll; int main() { ll l,r,d; cin>>l>>r>>d; cout<<r/d-(l-1)/d<<endl; }
0
#include<bits/stdc++.h> using namespace std; typedef long long int ll; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); ll t; cin>>t; while(t--){ ll n,k,res=1; cin>>n>>k; string s; cin>>s; ll i = s.find_first_of('*'); while(true){ ll j = min(n...
2
#include <bits/stdc++.h> using namespace std; int main() { int n; long long sum = 0; string s; cin >> n; cin >> s; for (int i = 0; i < n; i++) { if ((s[i] - '0') % 2 == 0) sum += i + 1; } cout << sum << endl; return 0; }
1
#include <bits/stdc++.h> void read(); using namespace std; int ans = -1; int gcd(int a, int b) { int m = a % b; while (m != 0) { a = b; b = m; m = a % b; } return b; } int prime[100005]; bool isprime[100005]; int cnt = 0; void fun() { memset(isprime, true, sizeof(isprime)); isprime[1] = isprime[...
2
#include <iostream> #include <algorithm> #include <climits> using namespace std; int n; int a[100], b[100]; int memo[100][100]; int recur(int left, int right){ if(memo[left][right] == -1){ int ret = INT_MAX; if(left == right){ ret = 0; } else{ for(int mid = left; mid < right; ++mid){ int cost = re...
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); int n; cin >> n; int c = 0; for (int i = 1; i < n; i++) { for (int j = i; j <= n; j++) { float tmp = sqrt(i * i + j * j); if ((tmp * tmp == (i * i + j * j)) and tmp <= n) c++; } } cout << c; retu...
1
#include <bits/stdc++.h> using namespace std; int n, r, a[1010], n1 = 0, ans = 0, le = 0, n0 = 0, ri = -1, ar[1010]; bool ok(int x) { int t; for (int j = max(0, x - r + 1); j <= min(n - 1, x + r - 1); j++) { t = a[j] - 1; if (t <= 0) return 0; } for (int j = max(0, x - r + 1); j <= min(n - 1, x + r - 1)...
2
#include <bits/stdc++.h> using namespace std; int v[200000]; int p[100]; int qa[200000], qb[200000], qc[200000]; int main() { int n, q, m; cin >> n >> q >> m; for (int i = 0; i < n; i++) cin >> v[i]; for (int i = 0; i < q; i++) cin >> qa[i] >> qb[i] >> qc[i]; for (int i = 0; i < m; i++) cin >> p[i]; for (in...
4
#include <bits/stdc++.h> using namespace std; template <typename T> T sum(T a) { T res = 0; for (T i = 1; i <= a; i++) { res += i; } return res; } template <typename T> void selectSort(T arr[], int len) { for (int i = 0; i < len; i++) { int minIndex = i; for (int j = i + 1; j < len; j++) min...
1
#include <bits/stdc++.h> using namespace std; int on_bit(int x, int pos) { x |= (1 << pos); return x; } int off_bit(int x, int pos) { x &= ~(1 << pos); return x; } bool is_on_bit(int x, int pos) { return ((x & (1 << pos)) != 0); } int flip_bit(int x, int pos) { x ^= (1 << pos); return x; } int lsb(int x) { ...
1
#include <bits/stdc++.h> void solve() { int n; std::cin >> n; std::stack<int> q; int a = 0; int cur = 1; n += n; while (n--) { std::string s; std::cin >> s; if (s[0] == 'r') { if (!q.empty() && q.top() != cur) { while (!q.empty()) q.pop(); ++a; } if (!q.empty(...
3
#include <bits/stdc++.h> using namespace std; int c, d, n, m, k, ans = 2e9; int dp[10001]; int main() { cin >> c >> d >> n >> m >> k; int r = n * m - k; if (r <= 0) { printf("0"); return 0; } for (int i = 0; i < r; i++) dp[i] = 2e9; for (int i = r; i > 0; i--) { if (i - 1 <= 0) ans = min(a...
1
#include<iostream> #include<sstream> #include<vector> #include<set> #include<map> #include<queue> #include<algorithm> #include<numeric> #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<cassert> #define rep(i,n) for(int i=0;i<n;i++) #define all(c) (c).begin(),(c).end() #define mp make_pair ...
0
#include <bits/stdc++.h> using namespace std; int main() { map<char, int> value; value['q'] = value['Q'] = 9; value['r'] = value['R'] = 5; value['n'] = value['N'] = value['b'] = value['B'] = 3; value['p'] = value['P'] = 1; value['k'] = value['K'] = value['.'] = 0; int black = 0; int white = 0; for (in...
1
#include <bits/stdc++.h> using namespace std; int sta[100005]; int getT() { char c; while (1) { c = getchar(); if (c == EOF) return 0; if (c == '<') break; } c = getchar(); return c == '/' ? -1 : 1; } int main() { getT(); vector<int> vec; int num = 1, x, cc = 0; sta[cc++] = 0; while (x =...
2
/// // File: dpl_3_a.cpp // Author: ymiyamoto // // Created on Wed Nov 8 01:32:41 2017 // #include <cstdint> #include <iostream> using namespace std; #define MAX_H 1400 #define MAX_W 1400 static uint32_t arr[MAX_H][MAX_W]; int32_t main() { uint32_t H, W; cin >> H >> W; for (uint32_t i = 0; i < H; i++) { ...
0
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; long long dijkstra(int n, int st, int t, vector<pair<pair<int, int>, int>> e) { vector<vector<pair<int, int>>> g(n); for (auto &now : e) { int u = now.first.first, v = now.first.second, w = now.second; g[u].push_back({v, w}); g[v]...
2
#include <bits/stdc++.h> using namespace std; int main() { int n,m,k; scanf("%d%d%d",&n,&m,&k); int i,j; for(i=0; i<=n; i++) for(j=0; j<=m; j++) if(i*m+j*n-2*i*j==k){ puts("Yes"); return 0; } puts("No"); return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, k, p; int a[150000]; int b[150000]; bool check(int x) { int now = 1; for (int i = 1; i <= k; i++) { if (abs(b[i] - a[now]) > x) continue; else if (abs(b[i] - a[now]) + abs(b[i] - p) <= x) now++; } if (now > n) return true; else r...
1