solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int N, total = 0; set<string> S; int main() { cin >> N; for (int i = 0; i < N; i++) { string s1, s2; cin >> s1 >> s2; string A = string(1, s1[0]), B = string(1, s1[1]), C = string(1, s2[0]), D = string(1, s2[1]); if (S.insert(A + B + D + C).se...
1
#include <bits/stdc++.h> using namespace std; long long a[9]; long long dfs(long long vis, long long w) { if (vis > 8) return w; long long res = min(a[vis], w / vis), mins = 1e18; int len = log(840 / vis) / log(2) + 1; for (long long i = res; i >= max(res - len, (long long)0); i--) { mins = min(mins, dfs(vi...
5
#include <bits/stdc++.h> using namespace std; int arr[1000000], n; int main() { long long i, j, k; while (scanf("%d", &n) != EOF) { long long sum = 0; for (i = 0; i < n; i++) { scanf("%d", &arr[i]); sum = sum + arr[i]; } if (n == 1) { cout << arr[0] << endl; continue; } ...
1
#include <iostream> using namespace std; int main(){ long long n = 0, count = 0, last = 0; cin >> n; for(int i = 0;i < n; ++i){ int x; cin >> x; if(x >= last){ last = x; } else{ count += (last - x); } } cout << count; return 0; }
0
#include <bits/stdc++.h> using namespace std; template <typename T> std::ostream& operator<<(std::ostream& str, const std::vector<T>& v) { str << "["; for (auto n : v) str << n << ", "; str << "]"; return str; } template <typename T> std::ostream& operator<<(std::ostream& str, const std::unordered_set<T>& v) { ...
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 100100; struct Hull { long long a[MAXN], b[MAXN]; double x[MAXN]; int head, tail; Hull() : head(1), tail(0) {} long long get(long long xQuery) { if (head > tail) return 0; while (head < tail && x[head + 1] <= xQuery) head++; x[head] = ...
4
#include <bits/stdc++.h> using namespace std; bool debug = 1; int n, m, k; int dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; long long ln, lk, lm; struct Point { long long x, y; Point() {} Point(long long x, long long y) : x(x), y(y) {} Point operator*(double o) const { return Point(x * o, y * o); } Point ope...
4
#include<iostream> #include<string> #include<vector> using namespace std; int main() { int n,i,j,k,s,t,u; int carry = 0; string a, b,d; vector<int> c; cin >> n; for (i = 0; i < n; i++) { c.clear(); cin >> a; cin >> b; while (a.size() > b.size()) { b = "0" + b; } while (b.size() > a.size()) { a =...
0
#include <bits/stdc++.h> using namespace std; inline int tavan(int a, int n, int mod) { int p = 1; while (n > 0) { if (n % 2) { p = p * a; p %= mod; } n >>= 1; a *= a; a %= mod; } return p % mod; } int n, m, d[(1111)][(1111)]; string s[(1111)]; int main() { ios_base::sync_with_...
4
#include <bits/stdc++.h> using namespace std; const double PI = 3.14159265358979323846; const double EPS = 1e-9; const int INF = 2e9; const long long LINF = 1e18; template <class T> const T sqr(const T &x) { return x * x; } template <class T> const T &min(const T &a, const T &b, const T &c) { return min(min(a, b), ...
4
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for (int i = 0; i < n; i++) { int temp; cin >> temp; if (temp % 2 == 0 && temp > 2) cout << "0" << endl; else if (temp % 2 == 1 && temp > 2) cout << "1" << endl; else cout << 4 - temp << endl; } }
1
#include <bits/stdc++.h> using namespace std; const long double pi = 3.14159265359; template <typename T> T abs(T x) { return x > 0 ? x : -x; } template <typename T> T sqr(T x) { return x * x; } const int maxn = 1 << 13; int a[maxn]; int b[maxn]; int main() { for (int i = 0; i < maxn; i++) b[i] = -1; int n; s...
2
#include<iostream> using namespace std; int main() { int n,i; cin>>n; long long ans=1; long long a[n]; for(i=0;i<n;i++){ cin>>a[i]; if(a[i]==0){ cout<<0; return 0; } } for(i=0;i<n;i++){ if(1000000000000000000/ans<a[i]){ cout<<-1; return 0; } ans*=a[i]; } cout<<ans; }
0
#include <bits/stdc++.h> using namespace std; long long int gcd(long long int a, long long int b) { if (b == 0) return a; return gcd(b, a % b); } int minn(int a, int b) { if (a < b) return a; return b; } int a[100005], n; bool ans[32]; int func(int bit = 29, int st = 0, int end = n) { if (bit < 0) return 0; ...
4
#include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #define MAXN 100010 int n, m, a[MAXN], b[MAXN], c[MAXN], p[MAXN]; struct edge { int u, v, w; inline friend bool operator<(const edge &a, const edge &b) { return a.w < b.w; } } e[MAXN]; inline int find(int...
0
#include <bits/stdc++.h> using namespace std; int main(){ int a,b,c; std::cin>>a>>b>>c; std::cout<<max(a,max(b,c))+max(-a,max(-c,-b)); }
0
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int n; cin >> n; vector<string> v; v.reserve(n); for (int i = 0; i < n; i++) { char c[10]; cin >> c; v.emplace_back(c); } unordered_set<string> us; for (int i = n - 1; i >= 0; i--) { if (us.count(v[...
2
#include <bits/stdc++.h> using namespace std; pair<int, int> sz[4]; int ord[4]; char toprint[4]; bool Success() { int i1 = ord[1], i2 = ord[2], i3 = ord[3]; if (sz[i1].second == sz[i2].second && sz[i2].second == sz[i3].second && sz[i1].first + sz[i2].first + sz[i3].first == sz[i1].second) { printf("%d\n",...
4
#include <bits/stdc++.h> using namespace std; void FastIO() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cout.precision(15); } int main() { FastIO(); long long int a, b, i, n, p, l, r, l1, l2, r1, r2, y, x, s, e; double ans = 0.0; vector<pair<long long int, long long int> > v; cin >>...
3
#include <bits/stdc++.h> using namespace std; int n, a[200001], x, y; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> n; for (int i = 1; i <= n; ++i) cin >> a[i]; cin >> x >> y; for (int i = 1; i <= n; ++i) { int cnt1 = 0, cnt2 = 0; for (int j = 1; j <= n; ++j) if (j >= i) ...
1
#include <bits/stdc++.h> using namespace std; int Day12[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int prime100[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103}; template <typename T> inline bool isLeap(T y) { return (y % ...
4
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const long long MAXN = 1e5 + 228; long long Sum(long long a, long long b) { long long c = a + b; if (c >= MOD) { c -= MOD; } return c; } void Add(long long& a, long long b) { a += b; if (a >= MOD) { a -= MOD; } } void Sub...
3
#include <bits/stdc++.h> using namespace std; int cnt[26]; char s[105], t[105], p[105]; void solve() { memset(cnt, 0, sizeof cnt); scanf("%s %s %s", s + 1, t + 1, p + 1); int n = strlen(p + 1); for (int i = 1; i <= n; i++) { cnt[p[i] - 'a']++; } int l1 = strlen(s + 1); int l2 = strlen(t + 1); if (l1...
3
#include <bits/stdc++.h> using namespace std; int q; long long n, a, b; int main() { scanf("%d", &q); while (q--) { scanf("%lld%lld%lld", &n, &a, &b); printf("%lld\n", min(a * n + (b - 2 * a) * 0, a * n + (b - 2 * a) * (n / 2))); } }
1
#include<bits/stdc++.h> using namespace std; int a[300010]; int panduan(int x){ if(a[x]>a[x+1]&&a[x]>a[x-1]) return 1; if(a[x]<a[x+1]&&a[x]<a[x-1]) return 1; return 0; } int main(){ ios::sync_with_stdio(false); int t; cin>>t; while(t--){ int n,res=0,cnt=0; cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; a[n+2]=...
2
#include <bits/stdc++.h> //typedef //-------------------------#include <bits/stdc++.h> const double pi = 3.141592653589793238462643383279; using namespace std; //conversion //------------------------------------------ inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <c...
0
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for (int i = 0; i < (n); i++) typedef long long ll; typedef pair<int, int> P; int main() { int n; cin >> n; int t[100000]; int maxi = -1; rep(i, n) { cin >> t[i]; maxi = max(maxi, t[i]); } vector<int> div; ...
0
#include <bits/stdc++.h> const int mo = 1000000007; int n, m; char a[505][505], a1[505][505]; int f0[505][505], f1[505][505]; inline void add(int &a, int b) { a = (a + b) % mo; } int main() { scanf("%d%d", &n, &m); int i, j, k; for (i = 1; i <= n; i++) { scanf("%s", a[i] + 1); } if (n > m) { for (i = ...
5
#include <bits/stdc++.h> using namespace std; int n, a, b; int main() { cin >> n >> a >> b; b++; if (b > n - a) cout << n - a; else cout << b; }
1
#include <iostream> using namespace std; int main() { int c[9]; for(int i = 0; i < sizeof(c); i++) cin >> c[i]; cout << (c[0]+c[4]+c[8] == c[1]+c[5]+c[6] && c[0]+c[4]+c[8] == c[2]+c[3]+c[7] ? "Yes" : "No") <<endl; }
0
#include<bits/stdc++.h> using namespace std; int main(){ int m,n; cin>>m>>n; int a[1000005],maxn=-1; for(int i=0;i<n;i++)cin>>a[i]; for(int i=0;i<n-1;i++){ maxn=max(a[i+1]-a[i],maxn); } maxn=max(m+a[0]-a[n-1],maxn); cout<<m-maxn; return 0; }
0
#include <bits/stdc++.h> using namespace std; char a[1001][1001]; int b[1001][1001]; bool check[1001][1001]; map<int, int> ans; int n, m, k; int cnt; void dfs(int x, int y, int ptr) { check[x][y] = true; b[x][y] = ptr; for (int i = -1; i < 2; i++) { for (int j = -1; j < 2; j++) { if (abs(i) + abs(j) != ...
4
#include <iostream> #include <vector> #include <cstring> #include <string> #include <algorithm> #include <iomanip> #include <cmath> #include <cassert> #include <map> #include <set> #include <unordered_map> #include <queue> using namespace std; int n; struct S { char f[5][5]; int x, y; }; int conv(S s) { int ret =...
0
#include <iostream> #include <algorithm> #include <vector> #include <queue> #include <map> #include <set> #include <string> #include <cstring> #include <cmath> #include <cstdio> #include <cstdlib> using namespace std; #define MAX_N 40 #define MAX_K 3 #define INF (1<<29) int n; int p[MAX_N+1], t[MAX_N+1]; int dp[MAX_...
0
#include <bits/stdc++.h> using namespace std; int n, x; int a[20000]; bool changed[20000]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n; int result = 0; for (int i = 0; i < n; ++i) { cin >> x; if (x % 2 == 0) { a[i] = x / 2; result += a[i]; continue; } ...
1
#include <bits/stdc++.h> int power(int base, int exp) { int sum = 1; for (int i = 0; i < exp; i++) { sum *= base; } return sum; } double maxV(double a, double b) { if (a < b) return b; else return a; } int main() { int* times = (int*)malloc(sizeof(int) * 5); int* mistakes = (int*)malloc(size...
1
#include <bits/stdc++.h> using namespace std; int main() { long long n, nm; vector<string> a(10); string s = ""; a[2] = "2"; a[3] = "3"; a[4] = "322"; a[5] = "5"; a[6] = "53"; a[7] = "7"; a[8] = "7222"; a[9] = "7332"; cin >> n >> nm; stringstream ss; ss << nm; string str = ss.str(); int ...
1
#include <bits/stdc++.h> #define ll long long int #define ld long double #define mod 1000000007 #define inf 1e18 #define inp(arr,n) for(int i=0;i<n;i++) cin>>arr[i] #define soa(arr,n) sort(arr,arr+n) #define sov(arr) sort(arr.begin(),arr.end()) #define fr(i,a,b) for(int i=a;i<b;i++) #define fre(i,a,b) for(int i=a;i<=b;...
1
#include <bits/stdc++.h> using namespace std; using i64 = long long; using f64 = double; using f80 = long double; using pii = pair<int, int>; using ptx = pair<f64, f64>; template <int MOD> class Num { private: int expow(long base, int e) const { long long ans = 1; for (; e > 0; e /= 2) { if (e % 2) ans...
2
#include <bits/stdc++.h> using namespace std; const long long int inf = 1e18; int n, m, k, x, p; int vals[100005]; vector<int> g[100005]; pair<long long int, long long int> dfs(int src, int par) { pair<long long int, long long int> ret = {0LL, 0LL}; for (int ch : g[src]) { if (ch == par) continue; pair<long...
2
#include <bits/stdc++.h> void fastIO() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); } int main() { fastIO(); int n, k; std::cin >> n >> k; std::vector<int> digits(n); for (int& digit : digits) { char d; std::cin >> d; digit = d - '0'; } std::vector<int> solution(n); fo...
1
#include <bits/stdc++.h> using namespace std; long long pi = 1000000000ll; long long hrr[3100], idt[3100]; string srr[3100]; vector<pair<int, int> > arr; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); ; long long n, a, id, t; cin >> n; for (int i = 1; i <= n; i++) { cin >> s...
3
#include <iostream> #include <algorithm> #include <vector> #define rep(i,n) for(int i = 0 ; i < n ; i++) #define MAX 100 using namespace std; typedef pair<int,int> P; int N,M; int scene[MAX][MAX]; int s[MAX/2][MAX/2],rotate_s[MAX/2][MAX/2][4]; void rotate(){ rep(i,3){ rep(j,M){ rep(k,M){ rotate_s[j][k...
0
#include <bits/stdc++.h> using namespace std; #define EPS (1e-10) #define equals(a,b) (fabs((a)-(b))<EPS) class Point { public: double x,y; Point(double x=0,double y=0):x(x),y(y) {} Point operator + (Point p) {return Point(x+p.x,y+p.y);} Point operator - (Point p) {return Point(x-p.x,y-p.y);} Poin...
0
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int arr[n]; int vis[5001] = {0}; set<int> se; for (int i = 0; i < n; ++i) cin >> arr[i], se.insert(arr[i]); vector<pair<int, int> > v; for (int i = 0; i < n; ++i) { if (...
4
#include <bits/stdc++.h> using namespace std; int best = -2e9; const int P1 = 53; const int P2 = 43; const int MOD1 = 1e9 + 7; const int MOD2 = 1e8 + 7; int best_vertex_cnt; const int maxn = 3e5 + 5; set<pair<int, int> >* S[maxn]; int c[maxn]; vector<int> edges[maxn]; char letter[maxn]; inline void recalc(pair<int, int...
4
#include <bits/stdc++.h> using namespace std; const int maxNum = 200010; const int maxMult = maxNum * 2; struct Edge { int u, v, w; int nextNum; int type, id; } edge[maxMult]; int head[maxNum], totalNums; void setStruct() { totalNums = 0; memset(head, -1, sizeof(head)); } void addEdges(int u, int v, int w, in...
5
#include <bits/stdc++.h> using namespace std; long long bigmod(long long b, long long p, long long md) { if (p == 0) return 1; if (p % 2 == 1) { return ((b % md) * bigmod(b, p - 1, md)) % md; } else { long long y = bigmod(b, p / 2, md); return (y * y) % md; } } int main() { string str; cin >> st...
4
#include <bits/stdc++.h> #define rep(i,n)for(int i=0;i<(n);i++) using namespace std; typedef long long ll; typedef pair<int,int>P; const int INF=0x3f3f3f3f; const ll INFL=0x3f3f3f3f3f3f3f3f; const int MOD=1000000007; ll n,K; ll calc(ll x){ ll sum=0; int cnt=0; while(x){ sum+=x; x>>=1; cnt++; if(cnt==n)brea...
0
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10; const int base = 131; const int p = 7000007; int n, ans, h[N]; bool last[N], f[N], can[7000010]; char s[N]; inline int read() { int s = 0, w = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar();...
6
#include <bits/stdc++.h> using namespace std; int main() { int n, j, c, c2 = 0; cin >> n; if (n > 1000) return -1; int a[1000][3]; for (int i = 0; i < n; i++) { cin >> a[i][0]; cin >> a[i][1]; cin >> a[i][2]; } for (int i = 0; i < n; i++) { c = 0; for (j = 0; j < 3; j++) if (a[i]...
1
#include <bits/stdc++.h> using namespace std; int A[1000100]; int main() { int n; scanf("%d", &n); int ans = 0; for (int i = 0; i < 1000100; ++i) A[i] = 0; for (int i = 0; i < n; ++i) { int x; scanf("%d", &x); A[x]++; } for (int i = 0; i < 1000100 - 1; i++) { if (A[i] % 2 == 1) ans++; ...
3
#include <bits/stdc++.h> using namespace std; int n, m, i, j, f, t, a[1001][1001], b[1001], c[1001]; int main() { cin >> n >> m; for (; n; n--) { cin >> i >> j; a[i][j]++; b[j]++; } for (; m; m--) { cin >> i >> j; if (a[i][j]) { b[j]--; a[i][j]--; f++; } else c[j]...
2
#include <bits/stdc++.h> #define r(i,n) for(int i=0;i<n;i++) using namespace std; int main(){ int n; cin>>n; double a[n],b[n],A=0,B=0,C=0,D=0; r(i,n)cin>>a[i]; r(i,n)cin>>b[i]; r(i,n){ A+=abs(a[i]-b[i]); B+=abs(a[i]-b[i])*abs(a[i]-b[i]); D+=abs(a[i]-b[i])*abs(a[i]-b[i])*abs(a[i]-b[i]); C=ma...
0
#include <iostream> #include <cstdio> #include <cassert> #include <cstring> #include <vector> #include <valarray> #include <array> #include <queue> #include <set> #include <unordered_set> #include <map> #include <unordered_map> #include <algorithm> #include <cmath> #include <complex> #include <random> using namespace ...
0
#include <bits/stdc++.h> using namespace std; long long nod(long long a, long long b) { if (b > 0) return nod(b, a % b); return a; } int main() { long long n, m, x, y, a, b; cin >> n >> m >> x >> y >> a >> b; long long d = nod(a, b); a /= d; b /= d; long long dx, dy; if (n * b < a * m) { dx = n / ...
4
#include <bits/stdc++.h> using namespace std; string Sum(string s) { long long int S = 0; for (int i = 0; i < s.size(); i++) { S += int(s[i] - '0'); } return to_string(S); } int main() { string s; cin >> s; int count = 0; while (int(s.size()) != 1) { count += 1; s = Sum(s); } cout << cou...
2
#include "bits/stdc++.h" using namespace std; typedef unsigned long long ull; int main() { int t;cin>>t; string s[t]; int a[t]; int ans=0; for(int i=0;i<t;i++) { cin>>s[i]>>a[i]; } string str; cin>>str; int itr=0; while(itr<t && s[itr]!=str) { itr++; } itr++; for(;itr<t;itr++) { ans+=a[itr]; } ...
0
#include <iostream> using namespace std; int F[102][102]; int color,gx,gy; bool maze; void DFS1(int Y,int X){ if(F[Y][X]!=color)return; if(Y==gy&&X==gx)maze=true; F[Y][X]=0; DFS1(Y-1,X ); DFS1(Y ,X-1); DFS1(Y ,X+1); DFS1(Y+1,X ); } int main(){ int N,M,sx,sy,n; cin >>...
0
#include <cstdio> const int SIZE = 200005; typedef long long int lli; lli comb1[SIZE]; //H-A+B-1 C i lli comb2[SIZE]; //W-B+A-1 C i lli inv[SIZE]; int N; //p = p / n * n + p % n //inv(n) = -inv(p%n) * (p/n) int main(){ int H, W, A, B; lli ans=0, p=1e9+7; scanf("%d %d %d %d", &H, &W, &A, &B); inv[1] = 1; N = H + ...
0
#include<bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; vector<int> res(n, 0); for (int i=0; i<n; ++i) { res[i] = i+1; } for (int i=1; i<n; i += 2) { int temp = res[i]; ...
1
#include <iostream> #include <deque> #include <algorithm> using namespace std; int max_matching(const deque<int> &aite, const deque<int> &jibun) { int i = 0, j = 0; for (; i < aite.size() && j < jibun.size(); ) { if (aite[i] < jibun[j]) ++i, ++j; else ++j; } return i; } int main() { ...
0
#include <bits/stdc++.h> using namespace std; mt19937 rng((unsigned int) chrono::steady_clock::now().time_since_epoch().count()); int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m, p; cin >> n >> m >> p; vector<long long> a(n); for (int i = 0; i < n; i++) { string foo; cin >> foo;...
4
#include <cstdio> #include <cctype> inline void read(int&x){ char c11=getchar();x=0;while(!isdigit(c11))c11=getchar(); while(isdigit(c11))x=x*10+c11-'0',c11=getchar(); } inline void cmax(int&A,int B){A=A>B?A:B;} const int N=2050; struct Edge{int v,nxt;}a[N+N]; int head[N],n,k,ans,_; int dfs(int x,int las,int dep)...
0
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; int dp[11], c[11][11]; int main() { int n, k; cin >> n >> k; long long ans = 1; for (int i = 0; i < n - k; i++) ans *= n - k, ans %= mod; dp[1] = 1; dp[0] = 1; c[0][0] = 1; for (int i = 1; i < 11; i++) { c[i][0] = 1; for (int...
2
#include<cstdio> #include<algorithm> #include<iostream> #include<cmath> #include<vector> #include<cstdlib> #include<cassert> #include<map> #define _A_ true #define EPS 1e-10 #define COUNTER_CLOCKWISE 1 #define CLOCKWISE -1 #define ONLINE_BACK 2 #define ONLINE_FRONT -2 #define ON_SEGMENT 0 using namespace std; class Po...
0
#include<bits/stdc++.h> #define fi(i,a,b) for(int i = (a); i <= (b); i++) using namespace std; const int N = 1e5 + 5; typedef long long ll; int a[N]; int main(){ // freopen("in.txt", "r", stdin); int t; scanf("%d", &t); while(t--){ int n, k; scanf("%d %d", &n, &k); int tmp = 0;int re, ans = 1654; for(int i ...
4
#include<bits/stdc++.h> using namespace std; #define RI register int int read() { int q=0;char ch=' '; while(ch<'0'||ch>'9') ch=getchar(); while(ch>='0'&&ch<='9') q=q*10+ch-'0',ch=getchar(); return q; } typedef long long LL; const int N=2005; int n,tot,a[N],vis[N],h[N],ne[N<<1],to[N<<1],du[N]; vector<int> orz[N]; i...
0
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = double; using pi = pair<int, int>; using pl = pair<ll, ll>; using pd = pair<ld, ld>; using cd = complex<ld>; using vcd = vector<cd>; using vi = vector<int>; using vl = vector<ll>; using vd = vector<ld>; using vs = vector<string>; using vpi =...
1
#include <bits/stdc++.h> using namespace std; const int Maxn = 100005; int n; int p[Maxn]; bool tk[Maxn]; vector<vector<int> > S[Maxn]; int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &p[i]); for (int i = 1; i <= n; i++) if (!tk[i]) { vector<int> seq; seq.push_back(i); ...
2
#include <bits/stdc++.h> using namespace std; int main() { int a,b,k; cin >> a >> b >> k; vector<int> vec; for(int i=1;i<200;i++) { if(a%i==0&&b%i==0) vec.push_back(i); } cout << vec[vec.size()-k] << endl; }
0
#include <bits/stdc++.h> using namespace std; const int MX = 15; const int INF = 1111111111; int N, M, ans; int tab[MX][MX], deg[MX]; vector<int> con[1 << MX], w[1 << MX]; struct data { int u, val; data(int u = 0, int val = 0) : u(u), val(val) {} bool operator<(const data &D) const { return val > D.val; } }; prio...
4
#include <bits/stdc++.h> using namespace std; using ll = long long; struct point { double x, y; point(double x = 0.0, double y = 0.0) : x(x), y(y) {} point operator+(point v) { return point(x + v.x, y + v.y); } point operator*(double k) { return point(x * k, y * k); } }; double dist(point a, point b) { return...
2
#include <bits/stdc++.h> using namespace std; vector<int> v1; vector<int> v2; int n; int dp[(int)1e5 + 5][5]; bool solve(int i, int cur) { if (i == n - 1) return 1; int &ret = dp[i][cur]; if (~ret) return ret; ret = 0; for (int j = 0; j <= 3; j++) { if (((cur | j) == v1[i]) && ((cur & j) == v2[i])) { ...
2
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 30; const int S = 320; int ff[maxn]; int freq[maxn]; vector<int> big; int perform() { priority_queue<int, vector<int>, greater<int> > Q; int ans = 0; for (int c : big) { if (freq[c] >= S) { Q.push(freq[c]); } } vector<int> temp...
4
#include <bits/stdc++.h> using namespace std; const double eps = 1e-8, pi = acos(-1.0); int gcd(int a, int b) { return b ? gcd(b, a % b) : a; } int lcm(int a, int b) { return a / gcd(a, b) * b; } int CEIL(double z) { return ceil(z - eps); } int FLOOR(double z) { return floor(z + eps); } int dblcmp(double d) { return (d...
1
#include <bits/stdc++.h> using namespace std; const int N = 1009; int s, n; pair<int, int> p[N]; int cnt; int main() { cin >> s >> n; for (int i = 0; i < n; i++) { cin >> p[i].first >> p[i].second; } sort(p, p + n); for (int i = 0; i < n; i++) { if (s > p[i].first) { s += p[i].second; cnt+...
1
#include <bits/stdc++.h> using namespace std; const int N = 4005; struct Hopcroft_Karp { static const int inf = 1e9; int n; vector<int> matchL, matchR, dist; vector<vector<int> > g; Hopcroft_Karp(int n) : n(n), matchL(n + 1), matchR(n + 1), dist(n + 1), g(n + 1) {} void addEdge(int u, int v) { g[u].pu...
3
#include <bits/stdc++.h> using namespace std; inline long long read() { long long nm = 0; bool fh = true; char cw = getchar(); for (; !isdigit(cw); cw = getchar()) fh ^= (cw == '-'); for (; isdigit(cw); cw = getchar()) nm = nm * 10 + (cw - '0'); return fh ? nm : -nm; } char ch[1202000]; int n, m, f[1202000]...
3
#include <algorithm> #include <vector> #include <cfloat> #include <string> #include <cmath> #include <set> #include <cstdlib> #include <map> #include <ctime> #include <iomanip> #include <functional> #include <deque> #include <iostream> #include <cstring> #include <queue> #include <cstdio> #include <stack> #include <cli...
0
#include<bits/stdc++.h> #define rep(i,n) for(int i=0;i<n;i++) using namespace std; int main(){ string a; while(cin>>a){ int b=0,c=0; rep(i,a.size()-2){ if(a[i]=='J'&&a[i+1]=='O'&&a[i+2]=='I')b++; if(a[i]=='I'&&a[i+1]=='O'&&a[i+2]=='I')c++; } cout<<b<<endl<<c<<endl; } return (0); }
0
#include <bits/stdc++.h> using namespace std; long long pos = 0; long long neg = 0; long long ans1 = 1e18; long long ans2 = 0; int a[1000005]; int cnt[2000005]; int tot1 = 0; int tot2 = 0; int n; void update(int i, int p) { if (a[i] - i >= 0) { if (a[i] - n >= 0) { cnt[a[i] - i]--; pos -= a[i] - n; ...
4
#include <bits/stdc++.h> using namespace std; int query(int a, int b) { cout << "? " << a << " " << b << endl; fflush(stdout); int tmp; cin >> tmp; return tmp; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int flag = query(0, 0); int a = 0, b = 0; int tmp; for (...
4
#include <bits/stdc++.h> using namespace std; int main() { long int n; cin >> n; vector<long int> v; for (int i = 0; i < n; i++) { long int temp; cin >> temp; v.push_back(temp); } sort(v.rbegin(), v.rend()); long int last = v[0] + 2; int counter = 0; for (int i = 0; i < n; i++) { if (v...
5
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; vector<pair<int, int> > g[N]; int fa[N], dis[N], mx[N], upmx[N], id[N]; vector<int> d; void dfs(int x) { mx[x] = dis[x]; for (auto u : g[x]) if (u.first != fa[x]) { dis[u.first] = dis[x] + u.second; fa[u.first] = x; dfs(u.fi...
5
#include <bits/stdc++.h> using namespace std; vector<int> e[100000]; int sz[100000]; double ans[100000]; void dfs(int now) { sz[now] = 1; for (int i = 0; i < e[now].size(); ++i) { int next = e[now][i]; dfs(next); sz[now] += sz[next]; } } void calc(int now) { for (int i = 0; i < e[now].size(); ++i) {...
4
#include <bits/stdc++.h> using namespace std; int n, m, l, r, ans(1000000007); int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.setf(ios::fixed); cout.precision(0); cin >> n >> m >> l >> r; if (l == 1 && r == n) ans = 0; if (l == 1) ans = min(ans, abs(m - r) + 1); if (r == n) ans = min(ans, a...
2
#include <bits/stdc++.h> using namespace std ; #define ll long long void FastInputOutput(){ ios_base :: sync_with_stdio( 0 ) ; cin.tie( 0 ) ; cout.tie( 0 ) ; } inline int D(){ int t ; scanf( "%d" , &t ) ; return t ; } inline ll LLD(){ ll t ; scanf( "%lld" , &t ) ; return t ; } ll g...
3
#include <bits/stdc++.h> using namespace std; int dis[200009], hdis[2000009], n, m, s, w[200009], c[200009], e[2][200009]; bool tak[200009]; pair<int, pair<int, int> > sparse[20][200009]; int h[200009]; long long tot = 0; vector<pair<int, pair<int, int> > > graph[200009]; bool check(int a, int b) { while (dis[a]) a =...
6
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 5; const long long Mod = 1e9 + 7; long long n, m, mod, ans, A[N], B[N], C[N], D[N]; inline long long read() { long long ret = 0, f = 0; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = 1; c = getchar(); } while (isdigit...
2
#include <bits/stdc++.h> typedef long long ll; #define FOR(i,a,b) for(ll i=(a);i<(b);i++) #define REP(i,a) FOR(i,0,a) using namespace std; const ll MAX_X=1e3,MAX_Y=1e3,MAX_Z=1e3; ll X,Y,Z,K,A[MAX_X],B[MAX_Y],C[MAX_Z],cmb[MAX_X*MAX_Y],inx[MAX_Z]; int main(){ ios::sync_with_stdio(false); cin.tie(0); cin>>X>>Y>>Z>>K; ...
0
#include<cstdio> #include<vector> #include<cstdlib> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; typedef vector<int> vi; typedef vector<vi> vvi; const int INF=(1<<31)-1; template<class T> struct Interval{ T a,b; Interval(){} Interval(T A,T B):a(A),b(B){} }; template<class T> class RMQ{ int n;...
0
#include <bits/stdc++.h> using namespace std; int N, M; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N >> M; int _f = N / 2, _g = M / 2; for (int x = 1; x <= _f; x++) { int tmp = N - x + 1; for (int i = 0; i < M; i++) { cout << x << ' ' << i + 1 << '\n'; cou...
2
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; int n, k, p[N]; int cnt[N]; int main() { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", p + i); bitset<N> vis; vector<int> v; for (int i = 1; i <= n; i++) { if (vis[i]) continue; int cur = i, cnt = 0; while (!vis...
6
#include <bits/stdc++.h> using namespace std; const int N = 20, M = 4096; int w[N]; int cnt[M]; int ans[M][110]; int main() { int n, m, q; scanf("%d%d%d", &n, &m, &q); for (int i = 0; i < n; i++) scanf("%d", w + i); for (int i = 0; i < m; i++) { char s[15]; scanf("%s", s); bitset<20> t(s); cnt[t...
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 1005; int askQueryA(int x) { cout << "A " << x << '\n'; cout.flush(); cin >> x; return x; } int askQueryB(int x) { cout << "B " << x << '\n'; cout.flush(); cin >> x; return x; } void answerQuery(int x) { cout << "C " << x << '\n'; cout.f...
2
#include <bits/stdc++.h> using namespace std; int main() { double r1,r2; cin >> r1 >> r2; printf("%.10f\n",1/(1/r1+1/r2)); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 100000 + 10; struct node { int v, next, en; } e[2 * N]; int cf[N]; int ans[N]; bool vis[N]; int tot, a, b, n, u, v, k; int head[N], deep[N]; int f[N][32]; void add_edge(int u, int v, int en) { e[tot].en = en; e[tot].v = v; e[tot].next = head[u]; head...
3
#include <bits/stdc++.h> using namespace std; int n, m; int r[100000]; vector<int> cont[100000]; struct UnionFind { vector<int> data; UnionFind(int sz) { data.assign(sz, -1); } bool unite(int x, int y) { x = find(x), y = find(y); if (x == y) return (false); if (data[x] > data[y]) swap(x, y); data[...
5
#include <bits/stdc++.h> const long long maxn = 1e4 + 7; using namespace std; struct _IO { _IO() { ios::sync_with_stdio(0); cin.tie(0); } } _io; inline int read() { int s = 0, w = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '...
2