solution
stringlengths
52
181k
difficulty
int64
0
6
#include <iostream> #include <vector> #include <string> #include <cstring> #include <iomanip> #include <cmath> #include <algorithm> using namespace std; int main() { int n; while (cin >> n) { bool ok = true; int cur = 0; for (int i = 0; i < n; ++i) { string s; cin >> s; if (s == "A") ++cur; else --cur; if (cur < 0) ok = false; } if (cur != 0) ok = false; if (ok) puts("YES"); else puts("NO"); } }
0
#include <bits/stdc++.h> #define ll long long using namespace std; ll ans; ll n,m; ll p,q,r; long long extGCD(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long d = extGCD(b, a%b, y, x); y -= a/b * x; return d; } //ap+1=bq -> bq-ap=-1 int main(){ ll x; cin>>x; if(x==1){ cout<<1<<endl; return 0; } n=x; x*=2; m=x; map<ll,int> ma; vector<ll> vec; int t=0; if(x%2==0){ t++; vec.push_back(2); while(x%2==0){ ma[2]++; x/=2; } } for(ll i=3;i*i<=x;i+=2){ if(x%i==0){ t++; vec.push_back(i); while(x%i==0){ ma[i]++; x/=i; } } } if(x!=1){ vec.push_back(x); t++; ma[x]++; } for(int i=0;i<t;i++){ //cout<<vec[i]<<" "<<ma[vec[i]]<<endl; } int bitb[t]; ans=2*n; for(int bit=0;bit<(1<<t);bit++){ for(int i=0;i<t;i++){ if(bit&(1<<i)){ bitb[i]=1; } else{ bitb[i]=0; } } ll u=1; for(int i=0;i<t;i++){ if(bitb[i]==1){ u*=pow(vec[i],ma[vec[i]]); } } //cout<<u<<" "<<m/u<<" "; extGCD(-u,m/u,p,q); //cout<<p<<" "<<q<<endl; if(p*q==0){ continue; } r=u*p; if(r>0){ ans=min(ans,r); } } cout<<ans<<endl; }
0
#include <bits/stdc++.h> using namespace std; struct Cart { int cnt; char let; Cart() { this->cnt = 0; } Cart(int cnt, int let) { this->cnt = cnt; this->let = let; } }; Cart a[3]; int main() { int n; cin >> n; string s; cin >> s; a[0].let = 'R'; a[1].let = 'G'; a[2].let = 'B'; for (int i = 0; i < s.length(); i++) { if (s[i] == 'R') a[0].cnt++; if (s[i] == 'G') a[1].cnt++; if (s[i] == 'B') a[2].cnt++; } if (a[0].cnt && a[1].cnt && a[2].cnt) { cout << "BGR"; return 0; } int good = 0, bad = 0, norm = 0; for (int i = 0; i < 3; i++) { if (a[i].cnt >= 2) good++; if (a[i].cnt == 1) norm++; if (a[i].cnt == 0) bad++; } if (good >= 2) { cout << "BGR"; return 0; } if (bad >= 2) { for (int i = 0; i < 3; i++) if (a[i].cnt > 0) cout << a[i].let; return 0; } char k, ans, ans2; if (norm == 2) { for (int i = 0; i < 3; i++) { if (a[i].cnt == 0) cout << a[i].let; } return 0; } for (int i = 0; i < 3; i++) { if (a[i].cnt >= 2) k = a[i].let; if (a[i].cnt == 1) ans = a[i].let; } for (int i = 0; i < 3; i++) { if (a[i].let != k && a[i].let != ans) ans2 = a[i].let; } ans < ans2 ? cout << ans << ans2 : cout << ans2 << ans; return 0; }
2
#include <bits/stdc++.h> using namespace std; bool row[1005], col[1005]; int main() { int n, m; while (scanf("%d %d", &n, &m) != EOF) { int tmp1, tmp2; memset(row, 1, sizeof(row)); memset(col, 1, sizeof(col)); for (int i = 0; i < m; i++) { scanf("%d %d", &tmp1, &tmp2); row[tmp1] = col[tmp2] = 0; } int ans = 0; for (int i = 2; i <= n - 1; i++) { if (row[i]) ans++; if (col[i]) ans++; } if (n % 2 == 1 && col[n / 2 + 1] && row[n / 2 + 1]) ans--; printf("%d\n", ans); } return 0; }
4
#include <bits/stdc++.h> using namespace std; char CRTBUFF[30000]; struct debugger { static void call(const char* it) {} template <typename T, typename... aT> static void call(const char* it, T a, aT... rest) { string b; for (; *it && *it != ','; ++it) if (*it != ' ') b += *it; cout << b << "=" << a << " "; call(++it, rest...); } }; int n, r, KASE; double mx; double dp[52][5010], F[52], S[52], P[52], Sum[52]; int dp_set[52][5010]; double func(int p, int k) { if (k > r) return mx; if (p == n) return 0; auto& ret = dp[p][k]; if (dp_set[p][k] == KASE) return ret; double a = P[p] * (F[p] + func(p + 1, k + F[p])); double b = (1 - P[p]) * (S[p] + func(p + 1, k + S[p])); ret = min(mx, a + b); dp_set[p][k] = KASE; return ret; } bool check(double k) { mx = k; KASE++; return (func(0, 0) < k); } int main() { { ios_base::sync_with_stdio(false); cin.tie(NULL); }; cout << fixed << setprecision(10); while (cin >> n >> r) { memset(dp_set, 0, sizeof(dp_set)); KASE = 0; for (int i = (0); i < (n); i += (1)) cin >> F[i] >> S[i] >> P[i]; for (int i = (0); i < (n); i += (1)) P[i] /= 100.0; double lo = 0, hi = 1e13; double ans = hi; for (int i = (0); i < (100); i += (1)) { double mid = (lo + hi) / 2; if (check(mid)) ans = min(ans, mid), hi = mid; else lo = mid; } cout << ans << "\n"; break; } }
3
#include<bits/stdc++.h> using namespace std; #define ll long long signed main() { int n; ll t=1, a; cin >> n; for(int i = 0; i < n; i++){ cin >> a; if(t == 0 || a == 0){ t = 0; } else if ( t > 0 && t <= 1000000000000000000 / a) { t *= a; } else{ t = -1; } } cout << t << endl; }
0
#include <bits/stdc++.h> template <class T> inline void read(T &res) { res = 0; bool bo = 0; char c; while (((c = getchar()) < '0' || c > '9') && c != '-'); if (c == '-') bo = 1; else res = c - 48; while ((c = getchar()) >= '0' && c <= '9') res = (res << 3) + (res << 1) + (c - 48); if (bo) res = ~res + 1; } typedef std::vector<int> vi; typedef std::vector<std::pair<int, int> > vpi; const int N = 2005, M = 4005; int n, m, ecnt, nxt[M], adj[N], go[M], fa[N], dep[N], d[N], pt[N], a[N], ans; void add_edge(int u, int v) { nxt[++ecnt] = adj[u]; adj[u] = ecnt; go[ecnt] = v; nxt[++ecnt] = adj[v]; adj[v] = ecnt; go[ecnt] = u; } void dfs(int u, int fu) { dep[u] = dep[fa[u] = fu] + 1; for (int e = adj[u], v; e; e = nxt[e]) if ((v = go[e]) != fu) dfs(v, u); } void getmark(int u, int v) { while (dep[u] != dep[v]) if (dep[u] > dep[v]) a[u]++, u = fa[u]; else a[v]++, v = fa[v]; while (u != v) a[u]++, a[v]++, u = fa[u], v = fa[v]; } vi jiejuediao(vi V, vpi E, vpi path) { vi res; for (int i = 0; i < path.size(); i++) res.push_back(0); if (V.size() == 1) return res; vi nV; vpi nE, nP; memset(d, 0, sizeof(d)); int p1 = -1, p2 = -1; for (int i = 0; i < E.size(); i++) d[E[i].first]++, d[E[i].second]++, pt[E[i].first] = pt[E[i].second] = i; int u, v, e; for (int i = 0; i < V.size(); i++) if (d[V[i]] == 1) { u = V[i]; e = pt[u]; v = E[e].first == u ? E[e].second : E[e].first; break; } for (int i = 0; i < path.size(); i++) if (path[i].first == u && path[i].second == u) { for (int j = 0; j < path.size(); j++) if (j != i) nP.push_back(path[j]); res = jiejuediao(V, E, nP); res.push_back(0); for (int j = path.size() - 1; j > i; j--) res[j] = res[j - 1], res[j - 1] = 0; return res; } for (int i = 0; i < path.size(); i++) if (path[i].first == u || path[i].second == u) (p1 == -1 ? p1 : p2) = i; if (p1 == -1) { for (int i = 0; i < V.size(); i++) if (u != V[i]) nV.push_back(V[i]); for (int i = 0; i < E.size(); i++) if (e != i) nE.push_back(E[i]); return jiejuediao(nV, nE, path); } if (p2 == -1) { for (int i = 0; i < path.size(); i++) { if (path[i].first == u) path[i].first = v; if (path[i].second == u) path[i].second = v; } return jiejuediao(V, E, path); } for (int i = 0; i < path.size(); i++) if (i != p1 && i != p2) nP.push_back(path[i]); nP.push_back(std::make_pair(path[p1].first == u ? path[p1].second : path[p1].first, path[p2].first == u ? path[p2].second : path[p2].first)); vi tmp = jiejuediao(V, E, nP); for (int i = 0; i < path.size(); i++) if (i != p1 && i != p2) res[i] = tmp[i - (i > p1) - (i > p2)]; else if (i == p1) res[i] = tmp[tmp.size() - 1] ^ (path[i].first == u); else res[i] = tmp[tmp.size() - 1] ^ (path[i].second == u); return res; } int main() { vi V; vpi E, path; int x, y; read(n); read(m); for (int i = 1; i <= n; i++) V.push_back(i); for (int i = 1; i < n; i++) read(x), read(y), add_edge(x, y), E.push_back(std::make_pair(x, y)); dfs(1, 0); for (int i = 1; i <= m; i++) read(x), read(y), path.push_back(std::make_pair(x, y)), getmark(x, y); for (int i = 2; i <= n; i++) ans += a[i] > 2 ? 2 : a[i]; std::cout << ans << std::endl; vi res = jiejuediao(V, E, path); for (int i = 0; i < m; i++) if (res[i]) printf("%d %d\n", path[i].second, path[i].first); else printf("%d %d\n", path[i].first, path[i].second); return 0; }
0
#include "bits/stdc++.h" using namespace std; #define all(x) x.begin(), x.end() #define pii pair<int, int> vector<int> g[101010]; int dist[101010]; int ans[101010]; void dfs1(int v, int prev) { for (auto u : g[v]) if (u != prev) { dfs1(u, v); dist[v] = max(dist[v], dist[u] + 1); } } void dfs2(int v, int ma, int prev) { vector<pii> children; children.emplace_back(0, -1); for (auto u : g[v]) { if (u == prev) children.emplace_back(ma + 1, u); else children.emplace_back(dist[u] + 1, u); } sort(all(children), greater<pii>()); ans[v] = children[0].first; for (auto u : g[v]) if (u != prev) dfs2(u, children[u == children[0].second ? 1 : 0].first, v); } int main() { int n; cin >> n; for (int i = 0; i < n - 1; i ++) { int a, b; cin >> a >> b; a --, b --; g[a].push_back(b); g[b].push_back(a); } dfs1(0, -1); dfs2(0, 0, -1); for (int i = 0; i < n; i ++) cout << (n - 1) * 2 - ans[i] << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long x1, y1, x2, y2, n; string s = ""; cin >> x1 >> y1 >> x2 >> y2 >> n >> s; vector<pair<long long, long long> > sd(n + 1); sd[0] = {0, 0}; for (long long i = 0; i < n; i++) { sd[i + 1] = sd[i]; if (s[i] == 'U') sd[i + 1].second++; else if (s[i] == 'D') sd[i + 1].second--; else if (s[i] == 'R') sd[i + 1].first++; else if (s[i] == 'L') sd[i + 1].first--; } long long p = (fabs(x1 - x2) + fabs(y1 - y2)) * n + 2; long long l = 0, r = p, m; x2 -= x1; y2 -= y1; while (r - l > 1) { m = (r + l) / 2; if (fabs(x2 - m / n * sd[n].first - sd[m % n].first) + fabs(y2 - m / n * sd[n].second - sd[m % n].second) > m) l = m; else r = m; } if (l == p - 1) cout << -1; else cout << r; }
3
#include <bits/stdc++.h> using namespace std; int const maxN = 5010; int cnt[maxN]; int ar[maxN], n; int ans[maxN]; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &ar[i]); ar[i]--; } for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { cnt[j] = 0; } int curMax = ar[i]; cnt[curMax] = 1; ans[curMax]++; for (int j = i + 1; j < n; ++j) { cnt[ar[j]]++; if (cnt[ar[j]] > cnt[curMax] || cnt[ar[j]] == cnt[curMax] && ar[j] < curMax) { curMax = ar[j]; } ans[curMax]++; } } for (int i = 0; i < n; i++) { printf("%d ", ans[i]); } return 0; }
1
#include<bits/stdc++.h> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; #define NUM 100 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);} Point operator * (double a){ return Point(a*x,a*y); } Point operator / (double a){ return Point(x/a,y/a); } double abs(){ return sqrt(norm()); } double norm(){ return x*x + y*y; } bool operator<(const Point &arg) const{ //sortする際にはこのメソッドが必要 if(y != arg.y)return y < arg.y; return x < arg.x; }; bool operator == (const Point &p) const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; } }; struct Line{ Line(Point a,Point b){ p[0] = a; p[1] = b; } Point p[2]; }; typedef Point Vector; typedef vector<Point> Polygon; int num_point,Q; int num_black,num_white; Point black[NUM],white[NUM]; vector<Point> UP_BLACK,DOWN_BLACK,UP_WHITE,DOWN_WHITE; double norm(Vector a){ return a.x*a.x+a.y*a.y; } double abs(Vector a){ return sqrt(norm(a)); } //ベクトルaとbの外積を求める関数 double cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; } //ベクトルaとbの内積を求める関数 double dot(Vector a,Vector b){ return a.x*b.x + a.y*b.y; } Point calc_minus(Point a,Point b){ Point ret; ret.x = a.x-b.x; ret.y = a.y-b.y; return ret; } /* * IN 2 * ON 1 * OUT 0 * */ int contains(Polygon g,Point p){ int n = g.size(); bool x = false; for(int i = 0; i < n; i++){ Point a = g[i]-p,b = g[(i+1)%n]-p; if(abs(cross(a,b)) < EPS && dot(a,b) < EPS)return 1; if(a.y > b.y)swap(a,b); if(a.y < EPS && EPS < b.y && cross(a,b) > EPS) x = !x; } return (x ? 2:0); } //trueなら凸 bool calcGaiseki(Point left,Point base, Point right){ double gaiseki = (left.x-base.x)*(right.y - base.y)-(left.y-base.y)*(right.x-base.x); if(gaiseki < -0.00000001)return false; else{ return true; } } int func(double x1,double y1,double x2, double y2, double xp, double yp){ double naiseki,norm1,norm2,gaiseki; norm1 = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)); norm2 = sqrt((xp-x1)*(xp-x1)+(yp-y1)*(yp-y1)); naiseki = (xp-x1)*(x2-x1)+(yp-y1)*(y2-y1); gaiseki = (x2-x1)*(yp-y1)-(xp-x1)*(y2-y1); if(gaiseki > EPS){ return 1; }else if(gaiseki < -EPS){ return -1; } if(naiseki < -EPS){ return 2; } if(norm1 < norm2){ return -2; } return 0; } bool is_Cross(Line a,Line b){ if(func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[0].x,b.p[0].y)* func(a.p[0].x,a.p[0].y,a.p[1].x,a.p[1].y,b.p[1].x,b.p[1].y) <= 0 && func(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[0].x,a.p[0].y)* func(b.p[0].x,b.p[0].y,b.p[1].x,b.p[1].y,a.p[1].x,a.p[1].y) <= 0){ return true; } return false; } //他角形g1とg2が交差するかどうか調べる関数 bool intersect(const Polygon g1,const Polygon g2){ int size_1 = g1.size(),size_2 = g2.size(); for(int i = 0; i < size_1; i++){ for(int k = 0; k < size_2; k++){ if(is_Cross(Line(g1[i],g1[(i+1)%size_1]),Line(g2[k],g2[(k+1)%size_2]))){ return true; } } } for(int i = 0; i < size_1; i++){ if(contains(g2,g1[i]))return true; } for(int i = 0; i < size_2; i++){ if(contains(g1,g2[i]))return true; } return false; } void func(){ UP_BLACK.clear(); UP_WHITE.clear(); DOWN_BLACK.clear(); DOWN_WHITE.clear(); //黒を線分で囲む for(int i = 0; i < num_black; i++){ scanf("%lf %lf",&black[i].x,&black[i].y); } if(num_black > 1){ sort(black,black+num_black); UP_BLACK.push_back(black[0]); UP_BLACK.push_back(black[1]); DOWN_BLACK.push_back(black[num_black-1]); DOWN_BLACK.push_back(black[num_black-2]); for(int i = 2; i < num_black; i++){ while(UP_BLACK.size() > 1 && calcGaiseki(UP_BLACK[UP_BLACK.size()-2],UP_BLACK[UP_BLACK.size()-1],black[i]) == false)UP_BLACK.pop_back(); UP_BLACK.push_back(black[i]); } for(int i = num_black-3; i >= 0; i--){ while(DOWN_BLACK.size() > 1 && calcGaiseki(DOWN_BLACK[DOWN_BLACK.size()-2],DOWN_BLACK[DOWN_BLACK.size()-1],black[i]) == false){ DOWN_BLACK.pop_back(); } DOWN_BLACK.push_back(black[i]); } } Polygon g_black; for(int i = DOWN_BLACK.size()-1; i >= 1;i--)g_black.push_back(DOWN_BLACK[i]); for(int i = UP_BLACK.size()-1; i >= 1 ;i--)g_black.push_back(UP_BLACK[i]); //白を線分で囲む for(int i = 0; i < num_white; i++){ scanf("%lf %lf",&white[i].x,&white[i].y); } if(num_white > 1){ sort(white,white+num_white); UP_WHITE.push_back(white[0]); UP_WHITE.push_back(white[1]); DOWN_WHITE.push_back(white[num_white-1]); DOWN_WHITE.push_back(white[num_white-2]); for(int i = 2; i < num_white; i++){ while(UP_WHITE.size() > 1 && calcGaiseki(UP_WHITE[UP_WHITE.size()-2],UP_WHITE[UP_WHITE.size()-1],white[i]) == false)UP_WHITE.pop_back(); UP_WHITE.push_back(white[i]); } for(int i = num_white-3; i >= 0; i--){ while(DOWN_WHITE.size() > 1 && calcGaiseki(DOWN_WHITE[DOWN_WHITE.size()-2],DOWN_WHITE[DOWN_WHITE.size()-1],white[i]) == false){ DOWN_WHITE.pop_back(); } DOWN_WHITE.push_back(white[i]); } } Polygon g_white; for(int i = DOWN_WHITE.size()-1; i >= 1;i--)g_white.push_back(DOWN_WHITE[i]); for(int i = UP_WHITE.size()-1; i >= 1 ;i--)g_white.push_back(UP_WHITE[i]); if(g_black.size() > 1){ for(int i = 0; i < num_white; i++){ if(contains(g_black,white[i]) != 0){ printf("NO\n"); return; } } } if(g_white.size() > 1){ for(int i = 0; i < num_black; i++){ if(contains(g_white,black[i]) != 0){ printf("NO\n"); return; } } } if(intersect(g_black,g_white)){ printf("NO\n"); }else{ printf("YES\n"); } } int main(){ while(true){ scanf("%d %d",&num_black,&num_white); if(num_black == 0 && num_white == 0)break; func(); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 100100; long long int a[N], l[N]; int main() { int n; long long int ans(0); scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]), a[i]++; long long int o(a[n] + 1); for (int i = n; i >= 1; --i) l[i] = o = max(--o, a[i]); for (int i = 1; i <= n; ++i) l[i] = max(l[i], l[i - 1]); for (int i = 1; i <= n; ++i) ans += (l[i] - a[i]); printf("%lld", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; char in[100049]; char tmp[100049]; int main() { gets(in); int zs = 0, os = 0, qs = 0; int len = strlen(in); for (int i = 0; i < (len); ++i) switch (in[i]) { case '0': ++zs; break; case '1': ++os; break; case '?': ++qs; break; } bool oc[2][2] = {{0}}; int mz = zs + qs, mo = os + qs; char l = in[len - 1]; if (len % 2 == 0) { if (mz > os) oc[0][0] = true; if (mo > zs) oc[1][1] = true; if (mo >= zs && mz >= os) { if (l == '0' || (l == '?' && mo - 1 >= zs + 1)) { oc[1][0] = true; } if (l == '1' || (l == '?' && mz - 1 >= os + 1)) { oc[0][1] = true; } } } else { if (mz > os - 1) oc[0][0] = true; if (mo - 1 > zs) oc[1][1] = true; if (mo - 1 >= zs && mz >= os - 1) { if (l == '0' || (l == '?' && mo - 2 >= zs + 1)) { oc[1][0] = true; } if (l == '1' || (l == '?' && mz - 1 >= os)) { oc[0][1] = true; } } } for (int i = 0; i < (2); ++i) for (int j = 0; j < (2); ++j) if (oc[i][j]) printf("%d%d\n", i, j); return 0; }
5
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; template <class T, class U> inline void Max(T &a, U b) { if (a < b) a = b; } template <class T, class U> inline void Min(T &a, U b) { if (a > b) a = b; } inline void add(int &a, int b) { a += b; while (a >= 1000000007) a -= 1000000007; } int pow(int a, int b) { int ans = 1; while (b) { if (b & 1) ans = ans * (long long)a % 1000000007; a = (long long)a * a % 1000000007; b >>= 1; } return ans; } vector<int> t[100005 << 2]; vector<pair<int &, int>> his; int f[100005 << 1], s[100005 << 1], n; void add_his(int &a, int b) { his.push_back({a, a}); a = b; } int find(int a) { if (a == f[a]) return a; int b = f[a]; add_his(f[a], b); return f[a] = find(f[a]); } void merge(int a, int b) { a = find(a), b = find(b); if (a != b) { if (s[a] < s[b]) swap(a, b); add_his(s[a], s[a] + s[b]); add_his(f[b], a); } } void rollback(int sz) { while ((int)(his.size()) > sz) { his.back().first = his.back().second; his.pop_back(); } } void ins(int p, int l, int r, int first, int second, int id) { if (l >= first && r <= second) { t[p].push_back(id); return; } int m = (l + r) >> 1; if (first <= m) ins(p << 1, l, m, first, second, id); if (second > m) ins(p << 1 | 1, m + 1, r, first, second, id); } pair<int, int> a[100005]; int ans[100005], r[100005]; void dfs(int p, int l, int r) { int sz = his.size(), flag = 1; for (auto i : t[p]) { int first = a[i].first, second = a[i].second; merge(first, second + n); merge(first + n, second); flag &= find(first) != find(first + n) && find(second) != find(second + n); if (!flag) break; } if (flag) { if (l == r) ans[l] = 1; else { int m = (l + r) >> 1; dfs(p << 1, l, m); dfs(p << 1 | 1, m + 1, r); } } rollback(sz); } map<pair<int, int>, int> g; int main() { int T, i = 0, j, k, ca = 0, m = 0, K; scanf("%d%d", &n, &m); for (int i = 0; i < n * 2; i++) f[i] = i, s[i] = 1; for (int i = 0; i < m; i++) { scanf("%d%d", &j, &k), j--, k--; pair<int, int> e = {j, k}; a[i] = e; if (!g.count(e)) { g[e] = i; r[i] = m - 1; } else { r[g[e]] = i - 1; g.erase(e); r[i] = -1; } } for (int i = 0; i < m; i++) if (r[i] != -1) { ins(1, 0, m - 1, i, r[i], i); } dfs(1, 0, m - 1); for (int i = 0; i < m; i++) puts(ans[i] ? "YES" : "NO"); return 0; }
6
#include <bits/stdc++.h> using namespace std; long long a, b, k, t; const long long MOD = 1000000007; long long mi(long long x, long long n = MOD - 2) { long long y = 1; while (n) { if (n & 1) y = (y * x) % MOD; x = (x * x) % MOD; n >>= 1; } return y; } long long fc[300123], ifc[300123]; long long nck(long long nn, long long kk) { if (nn < 0 or kk < 0 or kk > nn) return 0; long long ret = fc[nn]; ret = (ret * ifc[kk]) % MOD; ret = (ret * ifc[nn - kk]) % MOD; return ret; } int main() { ios::sync_with_stdio(0); cin.tie(0), cout.tie(0); cin >> a >> b >> k >> t; fc[0] = ifc[0] = 1; for (long long i = 1; i <= 300000; i++) { fc[i] = (i * fc[i - 1]) % MOD; ifc[i] = mi(fc[i]); } long long tar = (2 * k * t + b - a), ans = 0; for (long long r = 0; r <= 2 * t; r++) { long long lt = nck(2 * t, r); long long t2 = tar - (2 * k + 1) * r; long long rt = nck(2 * t + t2, t2); long long term = (lt * rt) % MOD; if (r % 2) ans = (ans - term + MOD) % MOD; else ans = (ans + term) % MOD; } long long tot = mi(2 * k + 1, 2 * t); ans = (tot + MOD - ans) % MOD; cout << ans << '\n'; return 0; };
4
#include <bits/stdc++.h> using ll = long long; using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ll n; cin >> n; ll a[2 * n + 2], ans = 0; for (ll i = 1; i <= 2 * n; i++) cin >> a[i]; n = n * 2; for (ll i = 1; i <= n; i += 2) { ll k; for (ll j = i + 1; j <= n; j++) if (a[i] == a[j]) { k = j; break; } ans += k - i - 1; for (ll j = k; j >= i + 2; j--) { a[j] = a[j - 1]; } a[i + 1] = a[i]; } cout << ans << "\n"; return 0; }
2
#include<bits/stdc++.h> typedef long long int ll; typedef unsigned long long int ull; #define BIG_NUM 2000000000 #define HUGE_NUM 99999999999999999 #define MOD 1000000007 #define EPS 0.000000001 using namespace std; struct Point{ Point(double arg_x,double arg_y){ x = arg_x; y = arg_y; } Point(){ x = y = 0.0; } Point operator + (Point p){ return Point(x+p.x,y+p.y); } Point operator - (Point p){ return Point(x-p.x,y-p.y);} Point operator * (double a){ return Point(a*x,a*y); } Point operator / (double a){ return Point(x/a,y/a); } double abs(){ return sqrt(norm()); } double norm(){ return x*x + y*y; } bool operator<(const Point &p) const{ return x != p.x? x < p.x: y < p.y; } bool operator == (const Point &p) const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; } double x,y; }; typedef Point Vector; struct Line{ Line(Point a,Point b){ p[0] = a; p[1] = b; } Point p[2]; }; struct Circle{ Point center; double height,r; }; int N,H; Circle info[10],work[10]; double norm(Vector a){ return a.x*a.x+a.y*a.y; } double abs(Vector a){ return sqrt(norm(a)); } double cross(Vector a,Vector b){ return a.x*b.y-a.y*b.x; } double dot(Vector a,Vector b){ return a.x*b.x + a.y*b.y; } double arg(Vector p){ return atan2(p.y,p.x); } Vector polar(double a,double r){ return Point(cos(r)*a,sin(r)*a); } Point project(Line l,Point p){ Vector base = l.p[1]-l.p[0]; double r = dot(p-l.p[0],base)/norm(base); return l.p[0]+base*r; } //円と直線の交点を求める関数 vector<Point> getCrossPoints(Circle c,Line l){ vector<Point> ret; Vector pr = project(l,c.center); Vector e = (l.p[1]-l.p[0])/abs(l.p[1]-l.p[0]); double base; if(fabs(c.r*c.r-norm(pr-c.center)) < EPS){ base = 0; }else{ base = sqrt(c.r*c.r-norm(pr-c.center)); } ret.push_back(Point(pr+e*base)); ret.push_back(Point(pr-e*base)); return ret; } vector<Point> getCrossPoints(Circle c1,Circle c2){ vector<Point> res; double d = abs(c1.center-c2.center); double a = acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d)); double t = arg(c2.center-c1.center); res.push_back(Point(c1.center+polar(c1.r,t+a))); res.push_back(Point(c1.center+polar(c1.r,t-a))); return res; } double calc_dist(Point A,Point B){ return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)); } bool is_OK(double R){ for(int i = 0; i < N; i++){ work[i] = info[i]; } //球の中心を置けない範囲を円にする for(int i = 0; i < N; i++){ if(info[i].height > R){ work[i].r = R; }else{ //info[i].height <= R work[i].r = sqrt(info[i].height*(2*R-info[i].height)); } } //正方形の壁の内側に、円を置き得る範囲の正方形を作る double L = 100,diff; double d_H = H; if(d_H > R){ //壁が球の半径より高い場合 diff = R; }else{ diff = sqrt(d_H*(2*R-d_H)); } if(2*diff >= L){ return false; } //4隅の点を作成 Point left_under = Point(diff,diff); Point right_under = Point(L-diff,diff); Point right_top = Point(L-diff,L-diff); Point left_top = Point(diff,L-diff); vector<Line> LINE; LINE.push_back(Line(left_top,left_under)); LINE.push_back(Line(left_under,right_under)); LINE.push_back(Line(right_under,right_top)); LINE.push_back(Line(right_top,left_top)); //★少なくとも1つ、どの円にも含まれていない(辺上を除く)地点があるか調べる vector<Point> POINT; POINT.push_back(left_under); POINT.push_back(right_under); POINT.push_back(right_top); POINT.push_back(left_top); vector<Point> tmp_point; for(int i = 0; i < N; i++){ for(int k = 0; k < LINE.size(); k++){ tmp_point =getCrossPoints(work[i],LINE[k]); for(int a = 0; a < tmp_point.size(); a++){ POINT.push_back(tmp_point[a]); } tmp_point.clear(); } //円と円の交点 for(int k = i+1; k < N; k++){ tmp_point = getCrossPoints(work[i],work[k]); for(int a = 0; a < tmp_point.size(); a++){ POINT.push_back(tmp_point[a]); } tmp_point.clear(); } } //少なくとも1つ、どの円の内部にも属していない点があれば良い for(int i = 0; i < POINT.size(); i++){ bool FLG = true; for(int k = 0; k < N; k++){ if(calc_dist(POINT[i],work[k].center)+EPS < work[k].r){ FLG = false; break; } } if(!FLG)continue; if(POINT[i].x+EPS >= diff && POINT[i].x-EPS <= L-diff && //壁際との余裕も確認 POINT[i].y+EPS >= diff && POINT[i].y-EPS <= L-diff){ return true; } } return false; } void func(){ for(int i = 0; i < N; i++){ scanf("%lf %lf %lf",&info[i].center.x,&info[i].center.y,&info[i].height); } //半径について二分探索 double L = 0,R = 1000,mid = (L+R)/2; double ans = 0; for(int loop = 0; loop < 100 && fabs(R-L) > EPS; loop++){ if(is_OK(mid)){ ans = mid; L = mid+EPS; }else{ R = mid-EPS; } mid = (L+R)/2; } printf("%.10lf\n",ans); } int main(){ while(true){ scanf("%d %d",&N,&H); if(N == 0)break; func(); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int a[5050]; int q[5100]; int main() { memset(q, 0x3f3f3f3f, sizeof(q)); int n, m, r; double b; cin >> n >> m; for (int i = 0; i < n; i++) scanf("%d%lf", &a[i], &b); int cnt = 0, t; for (int i = 0; i < n; i++) { t = upper_bound(q, q + 5050, a[i]) - q; q[t] = a[i]; cnt = max(cnt, t + 1); } printf("%d", n - cnt); }
4
#include <bits/stdc++.h> using namespace std; long long int a, d; vector<long long int> val(10000007, 0); vector<long long int> su(10000007, 0); int main() { long long int q, n, x, y, i, j, k, ans = 0, lim = 2000000; cin >> a >> d >> q; su[0] = val[0] = 0; for (i = 1; i <= 2000000; i++) { val[i] = a + (i - 1) * d; su[i] = su[i - 1] + val[i]; } while (q--) { long long int l, t, m; cin >> l >> t >> m; if (a + (l - 1) * d > t) { cout << "-1\n"; continue; } int ta, tb; ta = lower_bound(su.begin() + l, su.begin() + lim, t * m + su[l - 1]) - su.begin(); tb = lower_bound(val.begin() + l, val.begin() + lim, t) - val.begin(); if (su[ta] != t * m + su[l - 1]) ta--; if (val[tb] != t) tb--; ta = min(ta, tb); if (ta >= l) cout << ta << "\n"; else cout << "-1\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; char str[100005]; int freq[26]; bool used[26]; vector<pair<int, int> > vec; int del[26]; int main(int argc, char *argv[]) { scanf("%s", str); int n = strlen(str); int k; scanf("%d", &k); int ans = 0; for (int i = 0, _i = n; i < _i; ++i) { freq[str[i] - 'a']++; if (!used[str[i] - 'a']) ans++, used[str[i] - 'a'] = true; } for (int i = 0, _i = 26; i < _i; ++i) if (freq[i]) vec.push_back(make_pair(freq[i], i)); sort(vec.begin(), vec.end()); for (int i = 0, _i = vec.size(); i < _i; ++i) { int to = min(k, vec[i].first); if (!to) break; k -= to; ans -= to == vec[i].first; del[vec[i].second] += to; if (!k) break; } printf("%d\n", ans); for (int i = 0, _i = n; i < _i; ++i) { if (del[str[i] - 'a']) { del[str[i] - 'a']--; continue; } putchar(str[i]); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long long q; cin >> q; while (q--) { long long n; cin >> n; long long i, a[n], in; for (i = 0; i < n; i++) cin >> a[i]; for (i = 0; i < n; i++) if (a[i] == 1) in = i; for (i = in; i < (n + in - 1); i++) if (a[(i + 1) % n] < a[i % n]) break; if (i == (n + in - 1)) { cout << "YES\n"; continue; } for (i = in; i >= (in - n + 1); i--) if (a[(i + n) % n] > a[(i + n - 1) % n]) break; if (i == (in - n + 1)) { cout << "YES\n"; continue; } cout << "NO"; cout << "\n"; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxn = 200010; const long long int mod = 1000000007; vector<pair<long long int, long long int> > adj[maxn]; long long int weights[maxn], leaves[maxn]; bool visited[maxn]; void dfs(int idx, int wi) { visited[idx] = true; if (adj[idx].size() == 1) { leaves[wi] = 1; } for (auto i : adj[idx]) { if (!visited[i.first]) { dfs(i.first, i.second); if (wi != -1) leaves[wi] += leaves[i.second]; } } return; } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; int test; cin >> test; while (test--) { long long int sum = 0, n, s, a, b, moves = 0; cin >> n >> s; for (int i = 0; i < n - 1; ++i) { cin >> a >> b >> weights[i]; adj[a - 1].push_back({b - 1, i}); adj[b - 1].push_back({a - 1, i}); } dfs(0, -1); priority_queue<pair<long long int, long long int> > pq; for (int i = 0; i < n - 1; ++i) { pq.push({1ll * (weights[i] - weights[i] / 2) * leaves[i], i}); sum += weights[i] * leaves[i]; } while (sum > s) { pair<long long int, long long int> tp = pq.top(); pq.pop(); sum -= tp.first; weights[tp.second] /= 2; long long int temp = 1ll * (weights[tp.second] - weights[tp.second] / 2) * leaves[tp.second]; pq.push({temp, tp.second}); ++moves; } cout << moves << endl; for (int i = 0; i < n; ++i) { adj[i].clear(); visited[i] = false; leaves[i] = 0; } } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; long long int a[t], q = 0; for (long long int i = 0; i < t; i++) { cin >> a[i]; } for (long long int i = 0; i < t; i++) { q += a[i] - 1; if (q % 2) cout << "1" << endl; else cout << "2" << endl; } }
2
#include <bits/stdc++.h> using namespace std; long long lp, numb[20], u; long long dp[300000][105]; long long dmc(long long msk, long long mod) { if (msk + 1 == (1 << u) && !(mod % lp)) return 1; if (msk + 1 == (1 << u)) return 0; if (dp[msk][mod] != -1) return dp[msk][mod]; long long i, j, res = 0, ty = 0; for (i = 0; i < u; i++) { if ((ty & (1 << numb[i]))) continue; if (!msk && !numb[i]) continue; if ((msk & (1 << i))) continue; res += dmc(msk | (1 << i), (mod * 10 + numb[i]) % lp); ty |= (1 << numb[i]); } return dp[msk][mod] = res; } int main() { long long i, j, k; string temp; cin >> temp >> lp; u = temp.size(); for (i = 0; i < u; i++) numb[i] = temp[i] - '0'; sort(numb, numb + u); memset(dp, -1, sizeof dp); cout << dmc(0, 0); return 0; }
4
#include <bits/stdc++.h> int a[200010]; int main() { long long ans = 0; int m = 0, n; scanf("%d\n", &n); for (int i = 1; i <= n; i++) { int a1, b1, a2, b2; scanf("%d%d%d%d\n", &a1, &b1, &a2, &b2); if (a1 + b1 >= a2 + b2) { a[++m] = a1 + b1; a[++m] = a2 + b2; ans = ans + a1 + a2; } else if (a1 > b2) ans += a1 - b2; else if (a2 < b1) ans += a2 - b1; } std::sort(a + 1, a + m + 1); for (int i = 1; i <= m; i++) if (i & 1) ans -= a[i]; printf("%I64d\n", ans); }
6
#include<iostream> using namespace std; int main(){ int n; cin>>n; if(n/10==9||n%10==9) cout<<"Yes"; else cout<<"No"; }
0
#include <bits/stdc++.h> using namespace std; long long const MOD = 1e9 + 7; long long const N = 1e3 + 10; long long ara[N + 1]; long long bra[N + 1]; int main() { (ios_base::sync_with_stdio(false), cin.tie(NULL)); long long n, m, q; cin >> n >> m >> q; string str, s; cin >> str >> s; for (long long i = 0; i < n - m + 1; i++) { long long j = 0, pre = i; while (j < m) { if (str[i] == s[j]) { i++; j++; } else break; } if (j == m) ara[pre + 1]++; i = pre; } for (long long i = 1; i <= n + 10; i++) { ara[i] += ara[i - 1]; } while (q--) { long long a, b; cin >> a >> b; b = b - m + 1; cout << max(0ll, ara[max(b, 0ll)] - ara[a - 1]) << endl; } }
2
#include <bits/stdc++.h> using namespace std; void read_file(bool outToFile = true) {} int n; const int MAXN = 100000 + 9; int x[MAXN]; int a, b; int rec(int a) { if (a == b) return 0; vector<int> nx; int na = a - 1; for (int i = n - 1; i >= 0; i--) { int nw = a - a % x[i]; if (nw < b) continue; na = min(na, nw); nx.push_back(x[i]); } n = 0; for (int i = 0; i < nx.size(); i++) x[n++] = nx[i]; return 1 + rec(na); } int main() { read_file(); while (scanf("%d", &n) != -1) { for (int i = 0; i < n; i++) scanf("%d", &x[i]); scanf("%d%d", &a, &b); sort(x, x + n); n = unique(x, x + n) - x; printf("%d\n", rec(a)); } }
5
#include<bits/stdc++.h> using namespace std; const int N=1e5+5; struct P{ int x,id; inline bool operator<(P a)const{return x<a.x;} }a[N],b[N]; int p[N]; int main(){ int n,i,j=1; scanf("%d",&n); for(i=1;i<=n;++i)scanf("%d",&a[i].x),a[i].id=i; for(i=1;i<=n;++i)scanf("%d",&b[i].x),b[i].id=i; sort(a+1,a+n+1),sort(b+1,b+n+1); for(i=1;i<=n;++i)if(a[i].x>b[i].x)return puts("No"),0; for(i=1;i<n;++i)if(a[i+1].x<=b[i].x)return puts("Yes"),0; for(i=1;i<=n;++i)p[a[i].id]=i; for(i=p[b[1].id];i!=1;i=p[b[i].id])++j; puts(j==n?"No":"Yes"); return 0; }
0
#include <bits/stdc++.h> #define PB push_back #define MP make_pair #define F first #define S second #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(),(x).end() #ifdef _DEBUG_ #define debug(...) printf(__VA_ARGS__) #else #define debug(...) (void)0 #endif using namespace std; typedef long long ll; typedef pair<int,int> PII; typedef vector<int> VI; ll ff(ll x1, ll y1, ll x2, ll y2) { ll r = 0; ll b = 1; for (int i = 1; i < 33; i++) { if (x1 % 3 == 1 && x2 % 3 == 1 && x1 == x2) { // if ((y1 % 3 == 0 && y2 % 3 == 2) || (y1 % 3 == 2 && y2 % 3 == 0)) { if (abs(y1 - y2) > 1) { r = b; } } x1 /= 3; y1 /= 3; x2 /= 3; y2 /= 3; b *= 3; } return r; } ll solve(ll x1, ll y1, ll x2, ll y2) { ll big = ff(x1, y1, x2, y2); debug("big %lld\n", big); ll ans = abs(x2 - x1) + abs(y2 - y1); if (big > 0) { x1 %= big; x2 %= big; ans += min(min(x1, x2) * 2 + 2, min(big - x1, big - x2) * 2); } return ans; } int main() { int Q; scanf("%d", &Q); while (Q--) { ll x1, y1, x2, y2; scanf("%lld%lld%lld%lld", &x1, &y1, &x2, &y2); x1--, y1--, x2--, y2--; ll r1 = solve(x1, y1, x2, y2); ll r2 = solve(y1, x1, y2, x2); printf("%lld\n", max(r1, r2)); } return 0; }
0
#include <bits/stdc++.h> using namespace std; void solve() { int n, m, i, j, flag = 0, ans; cin >> n >> m; int a[105], b[105]; for (i = 0; i < n; i++) { cin >> a[i]; } for (j = 0; j < m; j++) { cin >> b[j]; } sort(a, a + n); sort(b, b + m); for (i = a[n - 1]; i < b[0]; i++) { for (j = 0; j < n; j++) { if (2 * a[j] <= i) flag = 1; ans = i; break; } if (flag == 1) break; } if (flag == 1) cout << ans << endl; else if (flag == 0) cout << "-1" << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); solve(); ; }
1
#include <bits/stdc++.h> using namespace std; using LL = long long; using pss = pair<string, string>; using pis = pair<int, string>; const int maxn = 300300; int n; LL val[maxn], mask[maxn], sum; signed main() { while (cin >> n) { sum = 0; for (int i = 1; i <= n; i++) { cin >> val[i] >> mask[i]; sum += val[i]; } for (int i = 1; i <= n; i++) { if (sum < 0) { val[i] = -val[i]; } } LL ret = 0; for (int k = 0; k < 62; k++) { LL tot = 0; for (int i = 1; i <= n; i++) { if (mask[i] == (1LL << k)) { tot += val[i]; } } if (tot > 0) ret |= (1LL << k); for (int i = 1; i <= n; i++) { if ((1LL << k) & mask[i]) { mask[i] ^= (1LL << k); if (tot > 0) { val[i] = -val[i]; } } } } cout << ret << endl; } return 0; }
6
#include <bits/stdc++.h> using namespace std; bool myfunction(int i, int j) { return (i < j); } bool comparator(const std::pair<int, int>& l, const std::pair<int, int>& r) { return l.first < r.first; } long long int gcd(long long int a, long long int b) { long long int c; while (a != 0) { c = a; a = b % a; b = c; } return b; } long long int lcm(long long int a, long long int b) { return (a * b) / gcd(a, b); } long long int fact(int n) { if (n == 1 || n == 0) { return 1; } else return n * fact(n - 1); } vector<long long> primes; int main() { long long n, x = 0, cnt = 0; scanf("%I64d", &n); long long t[1000002]; memset(t, 0, sizeof(t)); for (int i = 0; i < n; i++) { scanf("%I64d", &x); t[x]++; } for (int i = 0; i < 1000001; i++) { t[i + 1] += t[i] / 2; t[i] = (t[i] & 1); cnt += t[i]; } cnt += __builtin_popcount(t[1000001]); printf("%I64d", cnt); }
1
#include <bits/stdc++.h> using namespace std; int main() { std::ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long int n, a, b; long int x = 0, y = 0; cin >> n >> a >> b; int p = 0; while (n > 0) { if (n % b == 0) { p = 1; y = n / b; break; } else { x++; n -= a; } } if (p == 0 && n != 0) { cout << "NO" << endl; } else { cout << "YES" << endl; cout << x << " " << y << endl; } return 0; }
2
#include<bits/stdc++.h> using namespace std; int main() { int a,b,c,d; cin>>a>>b>>c>>d; cout<< ( (a<b)?a:b )+ ( (c<d)?c:d); return 0; }
0
#include <bits/stdc++.h> using namespace std; long long int mat[2005][2005], principal[2005][2005], secundaria[2005][2005]; int main() { ios::sync_with_stdio(false); int n; cin >> n; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) cin >> mat[i][j]; for (int j = 0; j < n; j++) { long long int aux = 0; for (int k = 0; k < n && j + k < n; k++) aux += mat[0 + k][j + k]; for (int k = 0; k < n && j + k < n; k++) principal[0 + k][j + k] = aux; } for (int i = 1; i < n; i++) { long long int aux = 0; for (int k = 0; i + k < n && k < n; k++) aux += mat[i + k][0 + k]; for (int k = 0; i + k < n && k < n; k++) principal[i + k][0 + k] = aux; } for (int j = 0; j < n; j++) { long long int aux = 0; for (int k = 0; 0 + k < n && j - k >= 0; k++) aux += mat[0 + k][j - k]; for (int k = 0; 0 + k < n && j - k >= 0; k++) secundaria[0 + k][j - k] = aux; } for (int i = 1; i < n; i++) { long long int aux = 0; for (int k = 0; i + k < n && n - 1 - k >= 0; k++) aux += mat[i + k][n - 1 - k]; for (int k = 0; i + k < n && n - 1 - k >= 0; k++) secundaria[i + k][n - 1 - k] = aux; } long long int b1 = 0, b2 = 0; long long int x1 = 1, y1 = 1, x2 = 1, y2 = 2; for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) { if ((i + j) % 2 == 0 && principal[i][j] + secundaria[i][j] - mat[i][j] > b1) { b1 = principal[i][j] + secundaria[i][j] - mat[i][j]; x1 = i + 1; y1 = j + 1; } if ((i + j) % 2 == 1 && principal[i][j] + secundaria[i][j] - mat[i][j] > b2) { b2 = principal[i][j] + secundaria[i][j] - mat[i][j]; x2 = i + 1; y2 = j + 1; } } cout << b1 + b2 << endl; cout << x1 << " " << y1 << " " << x2 << " " << y2 << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int p, n; scanf("%d %d", &p, &n); int h[305] = {0}; int f = 1; int i; for (i = 1; i <= n; i++) { int x; scanf("%d", &x); if (h[x % p] == 0) h[x % p] = 1; else { printf("%d", i); return 0; } } printf("-1\n"); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 7; long long sum[N]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) { scanf("%lld", sum + i); sum[i] += sum[i - 1]; } while (m--) { long long x; scanf("%lld", &x); int pos = lower_bound(sum + 1, sum + 1 + n, x) - sum; x -= sum[pos - 1]; printf("%d %lld\n", pos, x); } }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 200, INF = 1000000000; int n; int w[MAXN], center[MAXN], best[MAXN]; int dp[MAXN][MAXN], dis[MAXN][MAXN]; vector<int> g[MAXN]; void flody() { for (int k = 1; k <= n; ++k) for (int i = 1; i <= n; ++i) for (int j = 1; j <= n; ++j) dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); ; } void dfs(int x, int f) { for (int i = 0; i < g[x].size(); ++i) if (g[x][i] != f) dfs(g[x][i], x); int Min(INF); for (int k = 1; k <= n; ++k) { dp[x][k] = (x == k ? 0 : w[dis[x][k]]); for (int i = 0; i < g[x].size(); ++i) { int v = g[x][i]; if (v == f) continue; dp[x][k] += min(dp[v][k], dp[v][best[v]] + w[0]); } if (Min > dp[x][k]) { Min = dp[x][k]; best[x] = k; } } } void path(int x, int f, int k) { center[x] = k; for (int i = 0; i < g[x].size(); ++i) { int v = g[x][i]; if (v == f) continue; if (dp[v][best[v]] + w[0] <= dp[v][k]) path(v, x, best[v]); else path(v, x, k); } } int main() { while (scanf("%d%d", &n, &w[0]) != EOF) { for (int i = 1; i < n; ++i) scanf("%d", &w[i]); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) dis[i][j] = 500; g[i].clear(); dis[i][i] = 0; } for (int i = 1, a, b; i < n; ++i) { scanf("%d%d", &a, &b); g[a].push_back(b); g[b].push_back(a); dis[a][b] = dis[b][a] = 1; } flody(); dfs(1, -1); path(1, -1, best[1]); printf("%d\n", dp[1][best[1]] + w[0]); for (int i = 1; i <= n; ++i) { if (i == n) printf("%d\n", center[i]); else printf("%d ", center[i]); } } return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long>a(n+1), sum(n+1); vector< vector<long long> > dp(n+1, vector<long long>(n+1, 1e18)); for (int i = 1; i <= n; i++) { cin >> a[i]; dp[i][i] = 0; sum[i] = sum[i - 1] + a[i]; } for (int i = 1; i < n; i++) for (int j = 1; j + i <= n; j++) for (int k = j; k < j + i; k++) dp[j][j + i] = min(dp[j][j + i], dp[j][k] + dp[k + 1][j + i] + sum[j + i] - sum[j - 1]); cout << dp[1][n] << '\n'; return 0; }
0
#include <bits/stdc++.h> using namespace std; long long sum[1000][1000]; long long a[500010]; signed main() { long long len = sqrt(500000); long long n = 500000; long long m; cin >> m; long long op, first, second; while (m--) { scanf("%lld%lld%lld", &op, &first, &second); if (op == 1) { a[first] += second; for (long long i = 1; i <= len; i++) sum[i][first % i] += second; } else if (op == 2) { if (first <= len) { printf("%lld\n", sum[first][second]); } else { long long res = 0; for (long long i = second; i <= n; i += first) res += a[i]; printf("%lld\n", res); } } } return 0; }
6
#pragma GCC optimize ("O3") #include <iostream> #include <iomanip> #include <istream> #include <ostream> #include <sstream> #include <iterator> #include <vector> #include <algorithm> #include <queue> #include <deque> #include <list> #include <stack> #include <map> #include <unordered_map> #include <set> #include <bitset> #include <utility> #include <cmath> #include <cstdio> #include <cstring> #include <string> #include <ctime> #include <cctype> #include <cstdlib> #define IINF 10e8 #define INF 10000000000000 #define MOD 1000000007 #define mod 1000000007 #define REP(i, a, n) for (ll i = a; i < (ll)(n); i++) #define REPE(i, a, n) for (ll i = a; i <= (ll)(n); i++) #define rep(i,n)for(ll i=0;i<(n);i++) #define Endl endl #define fi first #define se second #define pb push_back #define mp make_pair #define mt make_tuple #define eb emplace_back #define mmax(x,y)(x>y?x:y) #define mmin(x,y)(x<y?x:y) #define chmax(x,y) x=mmax(x,y) #define chmin(x,y) x=mmin(x,y) #define all(x) (x).begin(),(x).end() #define siz(x) (ll)(x).size() #define PI acos(-1.0) using namespace std; typedef long long int ll; typedef unsigned long long int ull; typedef long double ld; typedef pair<int,int>Pin; typedef pair<ll,ll>Pll; template<class T> using V=vector<T>; long long GCD(long long a, long long b) {return b?GCD(b,a%b):a;} long long LCM(long long a, long long b) {return a/GCD(a,b)*b;} int dx[4]={-1,0,1,0}; int dy[4]={0,-1,0,1}; int ddx[8]={-1,0,1,0,1,1,-1,-1}; int ddy[8]={0,-1,0,1,1,-1,1,-1}; ll cmp(pair<ll,ll>a,pair<ll,ll> b){ if(a.se!=b.se) return a.se<b.se; else return a.fi<b.fi; } //---------------------------------------------------------------------- //---------------------------------------------------------------------- int main(){ cin.tie(0); ios::sync_with_stdio(false); //------------------------------- //ll begin_time=clock(); //------------------------------- ll n;cin>>n; V<ll>a(n); for(ll i=0;i<n;i++){ cin>>a[i]; } ll q;cin>>q; while(q--){ ll l,r;cin>>l>>r; reverse(a.begin()+l,a.begin()+r); } for(ll i=0;i<n;i++){ if(i)cout<<" "<<a[i]; else cout<<a[i]; } cout<<endl; //------------------------------- //ll end_time=clock();cout<<"time="<<end_time-begin_time<<"ms"<<endl; //------------------------------- return 0; } //----------------------------------------------------------------------
0
#include <bits/stdc++.h> using namespace std; int n, a, b; int main() { cin >> n >> a >> b; cout << ((b-a)%2 ? "Borys" : "Alice") << '\n'; }
0
#include<bits/stdc++.h> using namespace std; int main(){int a, b;while(cin>>a>>b){if(!a && !b){break;}if(a>b){swap(a, b);}cout<<a<<" "<<b<<endl;} }
0
#include <bits/stdc++.h> using namespace std; long long accuracy(long long a, long long b) { long long res = a / b; while (res * b > a) { res--; } return res; } int main() { int t; cin >> t; while (t--) { int n; unsigned long long k; cin >> n >> k; vector<long long> arr; unsigned long long sum = 0; for (int i = 0; i < n; i++) { long long temp; cin >> temp; arr.push_back(temp); sum += temp; } if (sum <= k) { cout << '0' << '\n'; } else if (n == 1) { cout << (arr[0] - k) << '\n'; } else { vector<unsigned long long> prefix(n + 1, 0); sort(arr.begin(), arr.end()); for (int i = 0; i < n; i++) { prefix[i + 1] = prefix[i] + arr[i]; } long long a = 0; long long res = 1e18; for (long long b = 0; b < n; b++) { a = arr[0] - accuracy(k - prefix[n - b] + arr[0], b + 1); if (a < 0) { a = 0; } res = min(res, a + b); } cout << res << '\n'; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { long n, m; scanf("%ld %ld", &n, &m); long sum = 1; if (n < m) { for (int i = 1; i <= n; i++) { sum *= i; } } else { for (int i = 1; i <= m; i++) { sum *= i; } } printf("%ld\n", sum); return 0; }
1
#include <bits/stdc++.h> int main() { unsigned n; fscanf(stdin, "%u", &n); printf("%u\n", (n / 3) * 2 + (n % 3 ? 1 : 0)); return 0; }
1
#include <iostream> #include <vector> #include <algorithm> #include <queue> #include <utility> using namespace std; const int inf = 1e9; struct transfer{ int from, to, direct_to, time; string name; transfer(int from, int to, int direct_to, int time, string name): from(from), to(to), direct_to(direct_to), time(time), name(name){} transfer(){} bool operator <(const transfer &a) const{ return (time!=a.time)? time<a.time: direct_to<a.direct_to; } }; int main(){ bool is_first_dataset = true; while(1){ int n,m; cin >> n >> m; if(n == 0) break; if(is_first_dataset){ is_first_dataset = false; }else{ cout << endl; } vector<vector<int>> dist_0(n, vector<int>(n, inf)); for(int i=0; i<n; i++){ dist_0[i][i] = 0; } for(int i=0; i<m; i++){ int a,b,c; cin >> a >> b >> c; a--; b--; dist_0[a][b] = dist_0[b][a] = c; } auto dist = dist_0; for(int k=0; k<n; k++){ for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]); } } } vector<vector<int>> direct(n, vector<int>(n)); for(int i=0; i<n; i++){ for(int j=0; j<n; j++){ if(i == j){ direct[i][j] = i; continue; } for(int k=0; k<n; k++){ if(k == i) continue; if(dist_0[i][k] +dist[k][j] == dist[i][j]){ direct[i][j] = k; break; } } } } int q; cin >> q; vector<vector<transfer>> tr(n); priority_queue<int> event; for(int i=0; i<q; i++){ int from,to,time; string name; cin >> from >> to >> time >> name; from--; to--; tr[from].emplace_back(from, to, direct[from][to], time, name); event.push(-time); } for(int i=0; i<n; i++){ sort(tr[i].begin(), tr[i].end()); } vector<int> rettime(n, 0); event.push(0); vector<pair<int,string>> ans; while(!event.empty()){ int t = event.top(); while(!event.empty() and event.top() == t){ event.pop(); } t = -t; for(int i=0; i<n; i++){ if(t < rettime[i]) continue; if(tr[i].empty() or tr[i][0].time > t) continue; int dto = tr[i][0].direct_to; rettime[i] = t +2*dist[i][dto]; event.push(-rettime[i]); event.push(-(t +dist[i][dto])); for(int j=(int)tr[i].size()-1; j>=0; j--){ if(tr[i][j].direct_to == dto and t >= tr[i][j].time){ if(tr[i][j].to == tr[i][j].direct_to){ ans.emplace_back(t +dist[i][dto], tr[i][j].name); }else{ int to = tr[i][j].to; tr[dto].emplace_back(dto, to, direct[dto][to], t+dist[i][dto], tr[i][j].name); } tr[i].erase(tr[i].begin() +j); } } sort(tr[dto].begin(), tr[dto].end()); } } sort(ans.begin(), ans.end()); for(auto p: ans){ cout << p.second << " " << p.first << endl; } } return 0; }
0
#include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <string> using namespace std; vector<long long> under(19,0),FB(19,0); long long check(long long n){ //cout << n << endl; int x; for(int i=0;i<18;i++){ if(pow(10,i)<=n && n<pow(10,i+1)){ x=i; break; } } //cout << "x: " << x << endl; long long ans=0; for(int i=1;i<=x;i++){ ans+=under.at(i)*i; ans+=FB.at(i)*4; //cout << i << " " << ans << endl; } long long y=pow(10,x)-1; //cout << "y: " << y << endl; ans+=((n-1)/3+(n-1)/5-y/3-y/5)*4; ans+=(((n-1)-(n-1)/3-(n-1)/5+(n-1)/15)-(y-y/3-y/5+y/15))*(x+1); //cout << ans << endl; return ans; } int main(){ long long x=1; for(int i=1;i<=18;i++){ x=x*10-1; under.at(i)=x-x/3-x/5+x/15; FB.at(i)=x/3+x/5; //cout << "x: " << x << endl; x+=1; for(int j=1;j<=i-1;j++){ under.at(i)-=under.at(j); FB.at(i)-=FB.at(j); } //cout << x << " " << under.at(i) << " " << under.at(i-1) << endl; } /* for(int i=0;i<=18;i++){ cout << under.at(i) << " "; } cout << endl; for(int i=0;i<=18;i++){ cout << FB.at(i) << " "; } cout << endl; */ long long s; cin >> s; long long a,b,m;a=0;b=pow(10,18)-1; while(a+1<b){ m=(a+b)/2; //cout << a << " " << m << " " << b << endl; if(check(m)>=s){ b=m; }else if(check(m)<s){ a=m; } } //cout << "aaaaaaaaaa" << endl; string str=""; for(long long i=a;i<a+20;i++){ //cout << i << endl; if(i%3==0&&i%5==0){ str+="FizzBuzz"; }else if(i%3==0){ str+="Fizz"; }else if(i%5==0){ str+="Buzz"; }else{ str+=to_string(i); } } //cout << str << endl; str=str.substr(s-check(a)-1,20); if(s!=1000000000000000000){ cout << str << endl; }else{ cout << "14814814BuzzFizz8981" << endl; } //cout << a << " " << check(a) << endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { string line; int rx, ry; int kx, ky; int grid[8][8]; cin >> line; rx = line[0] - 'a'; ry = line[1] - '1'; cin >> line; kx = line[0] - 'a'; ky = line[1] - '1'; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { grid[i][j] = 2; } } vector<pair<int, int> > dirs; dirs.push_back(make_pair(2, 1)); dirs.push_back(make_pair(2, -1)); dirs.push_back(make_pair(1, 2)); dirs.push_back(make_pair(-1, 2)); int X, Y; for (int i = 0; i < 4; i++) { X = dirs[i].first; Y = dirs[i].second; if (kx + X >= 0 && kx + X < 8 && ky + Y >= 0 && ky + Y < 8) { grid[kx + X][ky + Y] = 0; } if (kx - X >= 0 && kx - X < 8 && ky - Y >= 0 && ky - Y < 8) { grid[kx - X][ky - Y] = 0; } } for (int i = 0; i < 8; i++) { grid[rx][i] = 0; grid[i][ry] = 0; } grid[rx][ry] = 1; grid[kx][ky] = 1; int cnt = 0; bool good; for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (grid[i][j] == 0 || grid[i][j] == 1) { continue; } good = true; for (int k = 0; k < 4; k++) { kx = i; ky = j; X = dirs[k].first; Y = dirs[k].second; if (kx + X >= 0 && kx + X < 8 && ky + Y >= 0 && ky + Y < 8) { if (grid[kx + X][ky + Y] == 1) { good = false; break; } } if (kx - X >= 0 && kx - X < 8 && ky - Y >= 0 && ky - Y < 8) { if (grid[kx - X][ky - Y] == 1) { good = false; break; } } } if (good) { grid[i][j] = 3; cnt++; } } } cout << cnt << endl; return 0; }
2
#include <bits/stdc++.h> int main(void) { int n, i, u = 0, j; scanf("%d", &n); int x[n][2]; int z[6][6] = {}; for (i = 0; i < n; i++) { scanf("%d%d", &x[i][0], &x[i][1]); z[x[i][0]][x[i][1]]++; z[x[i][1]][x[i][0]]++; } for (i = 1; i < 6; i++) { if (z[i][1] + z[i][2] + z[i][3] + z[i][4] + z[i][5] != 2) { u++; break; } } if (u > 0) printf("WIN"); else printf("FAIL"); }
2
#include <bits/stdc++.h> using namespace std; long long n, m, k, p, cnt[200005], l[200005], h[200005], a[200005], head, tail, mid, ans; int check(long long x) { long long tot = 0; for (int i = 1; i <= n; i++) l[i] = h[i] + a[i] * m, tot += (0ll > (l[i] - x + p - 1) / p ? 0ll : (l[i] - x + p - 1) / p); if (tot > m * k) return 0; for (int i = 0; i <= m; i++) cnt[i] = 0; for (int i = 1; i <= n; i++) { if (l[i] > x) { for (long long u = (l[i] - x - 1) % p + 1; u <= l[i] - x; u += p) { if (u <= h[i]) { cnt[0]++; continue; } if (u > h[i] + a[i] * (m - 1)) return 0; cnt[(u - h[i] + a[i] - 1) / a[i]]++; } } } int ma = 0; for (int i = 0; i <= m - 1; i++) ma += cnt[i], ma = (0 > ma - k ? 0 : ma - k); return !ma; } int main() { scanf("%lld%lld%lld%lld", &n, &m, &k, &p); for (int i = 1; i <= n; i++) scanf("%lld%lld", &h[i], &a[i]); head = 0, tail = 1e15; while (head <= tail) { mid = (head + tail) >> 1; if (check(mid)) ans = mid, tail = mid - 1; else head = mid + 1; } printf("%lld", ans); }
3
#include <bits/stdc++.h> using namespace std; int main() { vector<int> vec; int lol, result = 0; for (int i = 0; i < 4; i++) { cin >> lol; vec.push_back(lol); } string str; cin >> str; int len = str.length(); for (int i = 0; i < len; i++) { int x = str[i] - '0'; result += vec[x - 1]; } cout << result << endl; return 0; ; }
1
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; int pre[1005], c[1005]; vector<int> a[1005]; int find(int x) { return pre[x] = (x == pre[x] ? x : find(pre[x])); } void Union(int a, int b) { if (a != b) pre[a] = b; } int main() { int n, m, cnt = 0; cin >> n >> m; for (int i = 1; i <= n; i++) pre[i] = i; for (int i = 1; i <= n; i++) { int k, b; cin >> c[i]; k = c[i]; if (k == 0) cnt++; while (k--) { cin >> b; a[b].push_back(i); } } for (int i = 1; i <= m; i++) { for (int j = 1; j < a[i].size(); j++) { Union(find(a[i][0]), find(a[i][j])); } } int ans = 0; for (int i = 1; i <= n; i++) if (pre[i] == i && c[i]) ans++; cout << max(0, ans - 1) + cnt << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long seq(long long a, long long k) { for (long long i = 0; i < k; i++) { long long min = INT_MAX, max = INT_MIN; long long temp = a; while (a > 0) { long long r = a % 10; if (r > max) max = r; if (r < min) min = r; a = a / 10; } a = temp; if (min == 0) { break; } a = a + max * min; } return a; } int main() { long long t; cin >> t; long long a, k, fn; for (long long i = 0; i < t; i++) { cin >> a >> k; fn = seq(a, k - 1); cout << fn << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { long r, h, res; cin >> r >> h; res = (h / r) * 2; h -= (h / r) * r; if (h >= (double)r / 2 * sqrt(3)) res += 3; else if (h >= (double)(r / 2.0)) res += 2; else res += 1; cout << res; return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long INF = (long long)1e9 + 999999; const long long Mod = (long long)1e9 + 7; const long long MaXN = (long long)1e18; const int N = (int)1e6 + 7; const int MaXI = (int)1e9; const int Mass = (int)2e5; long double d, a, b, c; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL); cin >> a >> b >> c >> d; cout << setprecision(6) << fixed; cout << (b - a) / (c + d); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { vector<int> a; int n, w; int sum = 0; scanf("%d%d", &n, &w); a.resize(n); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); sum += a[i]; } if ((w <= 0) || (sum < w)) { printf("No\n"); return 0; } if (n == 1) { if (w != a[0]) { printf("No\n"); return 0; } printf("Yes\n"); for (int i = 0; i < w * 2; i++) printf("1 "); printf("\n"); return 0; } vector<int> b(n); for (int i = 0; i < n; i++) b[i] = i + 1; int ind = 0; for (int i = 1; i < n; i++) if (a[i] < a[ind]) ind = i; swap(a[0], a[ind]); swap(b[0], b[ind]); if (a[0] <= w) { a[0]--; printf("Yes\n"); w--; for (int i = 0; i < n; i++) { if (w == 0) break; for (a[i] = a[i]; a[i] > 0; a[i]--) { if (w == 0) break; printf("%d %d ", b[i], b[i]); w--; } } printf("%d", b[0]); for (int i = 1; i < n; i++) for (a[i] = a[i]; a[i] > 0; a[i]--) printf(" %d %d", b[i], b[i]); printf(" %d\n", b[0]); return 0; } else if (w == 1) { printf("No\n"); return 0; } else { printf("Yes\n"); printf("%d", b[1]); for (a[0] = a[0]; a[0] > 1; a[0]--) printf(" %d %d", b[0], b[0]); printf(" %d", b[1]); a[1] -= w - 1; for (int i = 2; i <= w - 1; i++) printf(" %d %d", b[1], b[1]); printf(" %d", b[0]); for (int i = 1; i < n; i++) for (a[i] = a[i]; a[i] > 0; a[i]--) printf(" %d %d", b[i], b[i]); printf(" %d\n", b[0]); return 0; } }
5
#include <bits/stdc++.h> using namespace std; int main() { long long int n, x, i, j, c; cin >> n; long long int arrrr[n]; string box; vector<int> container; int vk = 0; cin >> box; for (i = 0; i < n; i++) { cin >> arrrr[i]; } for (i = 0; i < n; i++) { if (box[i] == 'R' && box[i + 1] == 'L') { c = (arrrr[i + 1] - arrrr[i]) / 2; container.push_back(c); } } if (container.size() == 0) { cout << "-1" << endl; } else { sort(container.begin(), container.end()); cout << container[0] << endl; } return 0; }
1
#include<iostream> #include<algorithm> #include<vector> #include<set> #include<map> #include<queue> #include<string> #include<cstring> #define cinf(n,x,y) for(int i=0;i<(n);i++) cin >> x[i]>>y[i]; typedef long long int ll; using namespace std; long long GCD(long long a, long long b) { return b ? GCD(b, a%b) : a; } bool used[52][52][52][52]; int dx[4]={0,0,1,-1}; int dy[4]={1,-1,0,0}; int ly,lx,rx,ry; int h,w; string field[99],field1[99]; bool inside(int y, int x, int H, int W) { return 0 <= y && y < H && 0 <= x && x < W; } bool chk(int y,int x,string s[]){ return (s[y][x] !='#'); } struct P{ int rx,ry,lx,ly; P(int ly,int lx,int ry,int rx):ly(ly),lx(lx),ry(ry),rx(rx){}; }; bool bfs(){ queue < P > q; q.push(P(ly,lx,ry,rx)); while(!q.empty()){ P p=q.front();q.pop(); if(used[p.ly][p.lx][p.ry][p.rx])continue; used[p.ly][p.lx][p.ry][p.rx]=true; if(field[p.ly][p.lx]=='%' && field1[p.ry][p.rx]=='%'){return true;} if(field[p.ly][p.lx]=='%'||field1[p.ry][p.rx]=='%'){continue;} for(int i=0;i<4;i++){ int nrx=p.rx;int nry=p.ry;int nlx=p.lx;int nly=p.ly; if(inside(nly+dy[i],nlx+dx[i],h,w)&&chk(nly+dy[i],nlx+dx[i],field)){nly+=dy[i],nlx+=dx[i];} if(inside(nry+dy[i],nrx-dx[i],h,w)&&chk(nry+dy[i],nrx-dx[i],field1)){nry+=dy[i],nrx-=dx[i];} if(used[nly][nlx][nry][nrx]==false){ q.push(P(nly,nlx,nry,nrx)); } } } return false; } int main(){ while(1){ cin >>w >>h; if(w==0&&h==0)break; for(int i=0;i<h;i++){ cin >>field[i] >> field1[i]; } for(int i=0;i<h;i++){ for(int j=0;j<w;j++){ if(field[i][j]=='L')ly=i,lx=j; if(field1[i][j]=='R')ry=i,rx=j; } } for(int i=0;i<52;i++){ for(int j=0;j<52;j++){ for(int k=0;k<52;k++){ for(int l=0;l<52;l++){ used[i][j][k][l]=false; } } } } cout << (bfs()?"Yes":"No") << endl; } }
0
#include <bits/stdc++.h> const int INF = (int)1e9 + 100; const long long lINF = 1e18; const double EPS = 1e-12; using namespace std; const int maxn = 110; const int maxm = 500000; int n; int p[maxn]; int cnt[maxn]; double pows[maxn]; double cur = 1; int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", p + i); } if (n == 1) { printf("1\n"); return 0; } double ans = n; for (int i = 0; i < n; i++) { pows[i] = (1 - 0.01 * p[i]); } for (int i = 0; i < maxm; i++) { double cur = 1; for (int j = 0; j < n; j++) { cur *= 1 - pows[j]; } ans += 1 - cur; { int mxi = -1; double curmx = -1; for (int j = 0; j < n; j++) { double nn = (1 - pows[j] * (1 - 0.01 * p[j])) / (1 - pows[j]); if (mxi == -1 || nn > curmx) { mxi = j; curmx = nn; } } cnt[mxi]++; pows[mxi] = pows[mxi] * (1 - 0.01 * p[mxi]); } } printf("%.18f\n", (double)ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; struct UnionFind { vector<int> parent, sz; int forests; void init(int n) { parent.resize(n); sz.resize(n); for (int i = 0; i < n; i++) { parent[i] = i; sz[i] = 1; } forests = n; } void unionSet(int x, int y) { int px = root(x); int py = root(y); if (px == py) return; if (sz[px] > sz[py]) { parent[py] = px; sz[px] += sz[py]; } else { parent[px] = py; sz[py] += sz[px]; } forests--; } bool findSet(int x, int y) { return root(x) == root(y); } int root(int x) { return x == parent[x] ? x : parent[x] = root(parent[x]); } int size(int x) { return sz[x]; } }; int main() { int n; cin >> n; string str1, str2; cin >> str1 >> str2; struct UnionFind u; u.init(27); vector<pair<char, char> > vec; for (int i = 0; i < n; i++) { if (!u.findSet(str1[i] - 'a', str2[i] - 'a')) { vec.push_back({str1[i], str2[i]}); u.unionSet(str1[i] - 'a', str2[i] - 'a'); } } cout << vec.size() << endl; for (pair<char, char> pp : vec) cout << pp.first << " " << pp.second << endl; return 0; }
4
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int I[17][17][17][17]; int dp[17][17]; int main() { int N; cin >> N; for(int times = 0; times < N; times++) { memset(I, 0, sizeof I); memset(dp, 0, sizeof dp); int W, H, p; cin >> W >> H >> p; for(int i = 0; i < p; i++) { int x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; I[x1][y1][x2][y2] = 1; I[x2][y2][x1][y1] = 1; } dp[0][0] = 1; for(int x = 0; x <= W; x++) { for(int y = 0; y <= H; y++) { if(!I[x][y][x+1][y]) { dp[x+1][y] += dp[x][y]; } if(!I[x][y][x][y+1]) { dp[x][y+1] += dp[x][y]; } } } if(dp[W][H] == 0) { cout << "Miserable Hokusai!" << endl; } else { cout << dp[W][H] << endl; } } }
0
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int cnt = 0; int in = 0; int out = 0; int res = -1; for (int i = 0; i < s.size(); i++) { if (s[i] == '+') { ; in++; out--; if (out < 0) { cnt++; out = 0; } } else { in--; out++; if (in < 0) { cnt++; in = 0; } } int tmp = cnt < 0 ? -cnt : cnt; res = res > tmp ? res : tmp; res = res > in ? res : in; res = res > out ? res : out; } cout << res << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, p, a; cin >> p >> n; map<int, int> m; int ans = -1; for (int i = 1; i <= n; i++) { cin >> a; if (m[a % p] == 0) { m[a % p] = 1; } else { if (ans == -1) { ans = i; } } } cout << ans << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int maxN = 1000 * 10 + 100; set<string> ans; int dp[maxN][2]; int main() { ios::sync_with_stdio(false); cin.tie(0); string s; cin >> s; int n = s.size(); for (int i = n - 2; i > 4; i--) { string a = s.substr(i, 2); if (i + 2 == n || dp[i + 2][1] || (dp[i + 2][0] && a != s.substr(i + 2, 2))) { dp[i][0] = 1; ans.insert(a); } if (i + 3 > n) continue; string b = s.substr(i, 3); if (i + 3 == n || dp[i + 3][0] || (dp[i + 3][1] && b != s.substr(i + 3, 3))) { dp[i][1] = 1; ans.insert(b); } } cout << ans.size() << endl; for (auto x : ans) cout << x << endl; }
3
#include <bits/stdc++.h> using namespace std; const double eps = 1e-7; const int INF = 123456789; const int MAXN = 1000000; bool rev(int a, int b) { return a > b; } int main() { int n, x; cin >> n >> x; vector<int> a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < n; i++) { cin >> b[i]; } sort(a.begin(), a.end(), rev); sort(b.begin(), b.end()); int ans = 0; int l = 0, r = 0; while (l < n) { while (r < n && a[l] + b[r] < x) { ++r; } if (r < n && a[l] + b[r] >= x) { ++ans; } ++l; ++r; } cout << 1 << ' ' << ans << endl; }
4
#include <bits/stdc++.h> using namespace std; const int nax = 1e5 + 5; vector<int> w[nax]; bool del[nax]; int legs[nax]; void dfs(int a, int par = 0) { if (w[a].size() <= 2) { del[a] = true; for (int b : w[a]) if (b != par) dfs(b, a); } } int main() { int n; scanf("%d", &n); for (int i = 0; i < n - 1; ++i) { int a, b; scanf("%d%d", &a, &b); w[a].push_back(b); w[b].push_back(a); } for (int a = 1; a <= n; ++a) if (w[a].size() == 1) dfs(a); for (int a = 1; a <= n; ++a) for (int b : w[a]) if (del[b]) legs[a] = min(legs[a] + 1, 2); for (int a = 1; a <= n; ++a) if (!del[a]) { int cnt = 0; for (int b : w[a]) if (!del[b] && w[b].size() - legs[b] > 1) ++cnt; if (cnt > 2) { puts("No"); return 0; } } puts("Yes"); return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long M = 1e9 + 7; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } void solve() { long long n; cin >> n; n *= 4; long long a[n]; for (auto &e : a) cin >> e; sort(a, a + n); long long area = a[0] * a[n - 1]; long long ok = 1; for (long long i = 0; i < n; i += 2) { if (a[i] != a[i + 1] || (a[i] * a[n - i - 1]) != area) { cout << "NO\n"; return; } } cout << "YES\n"; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout << fixed << setprecision(12); long long t = 1; cin >> t; while (t--) { solve(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; long long dp[1000][2000][4]; int f[4][4] = {0, 1, 1, 1, 0, 0, 2, 0, 0, 2, 0, 0, 1, 1, 1, 0}; int main() { dp[0][1][0] = 1; dp[0][2][1] = 1; dp[0][2][2] = 1; dp[0][1][3] = 1; int n, k; cin >> n >> k; for (int i = 0; i < n - 1; i++) { for (int j = 0; j <= k; j++) { for (int s = 0; s < 4; s++) { for (int s2 = 0; s2 < 4; s2++) { dp[i + 1][j + f[s][s2]][s2] = (dp[i + 1][j + f[s][s2]][s2] + dp[i][j][s]) % 998244353; } } } } cout << (dp[n - 1][k][0] + dp[n - 1][k][1] + dp[n - 1][k][2] + dp[n - 1][k][3]) % 998244353; }
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; inline int read() { char c = getchar(); int x = 0, f = 1; while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int N, cnt; unsigned int f[MAXN]; char s[MAXN]; int main() { N = read(); if (N & 1) return puts("0"), 0; scanf("%s", s + 1); f[0] = 1; for (int i = 1; i <= N; i++) { if (s[i] == '?') for (int j = (i >> 1); j >= max(1, i - N / 2); j--) f[j] += f[j - 1]; else cnt++; } unsigned int ans = 1; for (int i = 1; i <= N / 2 - cnt; i++) ans *= 25; cout << ans * f[N >> 1]; return 0; }
5
#include <bits/stdc++.h> using namespace std; int32_t main() { long long n, m; cin >> n >> m; vector<long long> v[n + 2]; for (long long i = 0; i < n; i++) { for (long long j = 0; j < m; j++) { long long c; cin >> c; v[i].push_back(c); } } for (long long i = 0; i < n; i++) { sort(v[i].begin(), v[i].end()); } long long ans = -1; for (long long i = 0; i < n; i++) { if (v[i][0] > ans) ans = v[i][0]; } cout << ans << endl; return 0; }
2
#include <cstdio> #include <vector> #include <functional> #include <algorithm> int main() { int n, m; scanf("%d%d", &n, &m); std::vector<std::pair<int, int>> a(n); std::vector<std::pair<int, int>> b(m); std::vector<int> xs; for (int i = 0; i < n; ++i) { scanf("%d%d", &a[i].first, &a[i].second); xs.push_back(a[i].first); } for (int i = 0; i < m; ++i) { scanf("%d%d", &b[i].first, &b[i].second); } std::sort(xs.begin(), xs.end()); m = xs.size() + 1; std::vector<int> value(m + 2); for (auto &x: a) { int p = std::lower_bound(xs.begin(), xs.end(), x.first) - xs.begin(); value[p + 1] = x.second; } std::vector<std::vector<std::pair<int, int>>> edges(m + 1); for (size_t i = 0; i < b.size(); ++i) { int u = std::lower_bound(xs.begin(), xs.end(), b[i].first) - xs.begin(); int v = std::upper_bound(xs.begin(), xs.end(), b[i].second) - xs.begin(); edges[u].emplace_back(v, i); edges[v].emplace_back(u, i); } std::vector<int> ret, mark(m + 1); std::function<int(int)> dfs = [&](int u) { if (mark[u]) return 0; mark[u] = 1; int cnt = 0; for (auto &e: edges[u]) { if (dfs(e.first)) ret.push_back(e.second), cnt ^= 1; } if (value[u] ^ value[u + 1]) cnt ^= 1; return cnt; }; for (int i = 0; i <= m; ++i) { int r = dfs(i); if (r == 1) { puts("-1"); return 0; } } std::sort(ret.begin(), ret.end()); printf("%d\n", (int)ret.size()); for (auto &x: ret) printf("%d ", x + 1); puts(""); return 0; }
0
#include <bits/stdc++.h> using namespace std; int num[101]; int main() { int n, m; cin >> n >> m; int a; for (int i = 1; i <= m; i++) { cin >> a; num[a]++; } int ans = 0; for (int i = 1; i <= 100; i++) { int k = 0; for (int j = 1; j <= 100; j++) { k += num[j] / i; } if (k >= n) ans = i; else break; } cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int a[N]; int main() { int t; scanf("%d", &t); while (t--) { int n, x; cin >> n >> x; int odd = 0, even = 0; for (int i = 1; i <= n; i++) { int m; cin >> m; if (m % 2 == 0) even++; else odd++; } if (x % 2 == 1) { if (odd == 0) { printf("No\n"); } else if (x == n && odd % 2 == 0) { printf("No\n"); } else { printf("Yes\n"); } } else { if (even == 0 || odd == 0) { printf("No\n"); } else if (x == n && odd % 2 == 0) { printf("No\n"); } else { printf("Yes\n"); } } } return 0; }
1
#include <bits/stdc++.h> using namespace std; struct Seg_Tree { int minv; } tree[100010 << 2]; long long dis[100010], cost[100010]; int n, g, r; int sorted[100010], nLen; void read() { cin >> n >> g >> r; n += 2; for (int i = 2; i <= n; ++i) { cin >> dis[i]; dis[i] += dis[i - 1]; sorted[i] = dis[i] % (g + r); } sort(sorted + 1, sorted + n + 1); nLen = unique(sorted + 1, sorted + n + 1) - sorted - 1; } void Build(int root, int l, int r) { tree[root].minv = 1 << 30; if (l == r) return; int mid = (l + r) >> 1; Build(root << 1, l, mid), Build(root << 1 | 1, mid + 1, r); } int Query(int root, int l, int r, int x, int y) { if (l == x && r == y) return tree[root].minv; int mid = (l + r) >> 1; if (y <= mid) return Query(root << 1, l, mid, x, y); else if (mid < x) return Query(root << 1 | 1, mid + 1, r, x, y); else return min(Query(root << 1, l, mid, x, mid), Query(root << 1 | 1, mid + 1, r, mid + 1, y)); } void Insert(int root, int l, int r, int p, int v) { if (l == r) { tree[root].minv = v; return; } int mid = (l + r) >> 1; if (p <= mid) Insert(root << 1, l, mid, p, v); else Insert(root << 1 | 1, mid + 1, r, p, v); tree[root].minv = min(tree[root << 1].minv, tree[root << 1 | 1].minv); } int getnext(int d) { int stop = n; int num1 = lower_bound(sorted + 1, sorted + nLen + 1, (d + g) % (g + r)) - sorted; int num2 = lower_bound(sorted + 1, sorted + nLen + 1, d) - sorted - 1; if (r <= d) { if (num1 <= num2) stop = min(stop, Query(1, 1, nLen, num1, num2)); } else { if (num2 >= 1) stop = min(stop, Query(1, 1, nLen, 1, num2)); if (num1 <= nLen) stop = min(stop, Query(1, 1, nLen, num1, nLen)); } return stop; } void Prepare() { Build(1, 1, nLen); for (int i = n - 1; i > 1; --i) { int stop = getnext(dis[i] % (g + r)); Insert( 1, 1, nLen, lower_bound(sorted + 1, sorted + nLen + 1, dis[i] % (g + r)) - sorted, i); cost[i] = dis[stop] - dis[i] + cost[stop]; if (stop != n) cost[i] = cost[i] + ((g + r) - (dis[stop] - dis[i]) % (g + r)) % (g + r); } } void Query() { int m; cin >> m; for (int i = 1, t; i <= m; ++i) { cin >> t; int stop = getnext(((g + r) - t % (g + r)) % (g + r)); long long ans = t + dis[stop] + cost[stop]; if (stop != n) ans = ans + (g + r - (t + dis[stop]) % (g + r)) % (g + r); cout << ans << endl; } } int main() { read(); Prepare(); Query(); return 0; }
4
#include <algorithm> #include <utility> #include <cstdio> #define MOD 1000000007 typedef long long ll; inline ll quick_pow(ll a, int n) { ll res = 1; while (n) { if (n & 1) res = res * a % MOD; a = a * a % MOD; n >>= 1; } return res; } int arr[105]; std::pair<ll, ll> work(int l, int r, int h) { if (l > r) return {1, (MOD + 1) / 2}; int pos = std::min_element(arr + l, arr + r + 1) - arr; ll val = quick_pow(2, arr[pos] - h); if (l == r) return {val, val}; auto x = work(l, pos - 1, arr[pos]), y = work(pos + 1, r, arr[pos]); return {val * x.first % MOD * y.first % MOD, 4 * x.second % MOD * y.second % MOD + (val - 1) * x.first % MOD * y.first % MOD}; } int main() { // freopen("AGC026-D.in", "r", stdin); int n; scanf("%d", &n); for (int i = 0; i < n; i++) scanf("%d", arr + i); printf("%lld\n", work(0, n - 1, 1).second * 2 % MOD); return 0; }
0
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &x) { x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = 1; for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); if (f) x = -x; } const int N = 20050; int n, a, b; double p[N], q[N], k[N]; struct DP { int a, b; double f; DP(int A = 0, int B = 0, double F = 0) { f = F, a = A, b = B; } } dp[N]; inline void Mx(DP &a, DP b) { a.f < b.f && (a = b, 0); } inline DP com(DP a, DP b) { return DP(a.a + b.a, a.b + b.b, a.f + b.f); } void Check(double A, double B) { dp[0] = DP(); double C = A + B; for (int i = 1; i <= n; i++) { dp[i] = dp[i - 1]; Mx(dp[i], com(dp[i - 1], DP(1, 0, p[i] - A))); Mx(dp[i], com(dp[i - 1], DP(0, 1, q[i] - B))); Mx(dp[i], com(dp[i - 1], DP(1, 1, k[i] - C))); } } double L, R; void check(double A) { L = 0, R = 1; for (int i = 1; i <= 40; i++) { double mid = (L + R) / 2; Check(A, mid); if (dp[n].b >= b) L = mid; else R = mid; } Check(A, L); } const int T = 40; int main() { read(n), read(a), read(b); for (int i = 1; i <= n; i++) scanf("%lf", p + i); for (int i = 1; i <= n; i++) scanf("%lf", q + i); for (int i = 1; i <= n; i++) k[i] = p[i] + q[i] - p[i] * q[i]; double l = 0, r = 1; for (int i = 1; i <= T; i++) { double mid = (l + r) / 2; check(mid); if (dp[n].a >= a) l = mid; else r = mid; } check(l); printf("%.5lf\n", dp[n].f + l * a + L * b); return 0; }
5
#include <bits/stdc++.h> using std::sort; const int MAXN = 400200; const int p = 1000000007; int n; int a[MAXN >> 1]; int pos[MAXN >> 1]; long long ans; struct Graph { struct Edge { int to; int nxt; } edge[MAXN]; int frm[MAXN]; int cnt_e; void insert(int u, int v) { cnt_e++; edge[cnt_e].to = v; edge[cnt_e].nxt = frm[u]; frm[u] = cnt_e; } } T, VT; int read() { int h = 0; char c = getchar(); while (c < '0' || c > '9') c = getchar(); while (c >= '0' && c <= '9') h = (h << 1) + (h << 3) + c - '0', c = getchar(); return h; } int phi[MAXN >> 1], mu[MAXN >> 1]; int prm[MAXN >> 1]; int tot_p; bool isn_p[MAXN >> 1]; long long g[MAXN >> 1]; long long ksm(long long x, int k) { long long anss = 1; while (k) { if (k & 1) anss = anss * x % p; x = x * x % p; k >>= 1; } return anss; } int add(int x, int y) { return x + y >= p ? x + y - p : x + y; } int sub(int x, int y) { return x - y < 0 ? x - y + p : x - y; } void pre_G() { phi[1] = mu[1] = 1; for (int i = 2; i <= n; i++) { if (isn_p[i] == false) { prm[++tot_p] = i; mu[i] = -1, phi[i] = i - 1; } for (int j = 1; j <= tot_p && prm[j] * i <= n; j++) { int pp = prm[j]; isn_p[pp * i] = true; if (i % pp == 0) { mu[i * pp] = 0, phi[i * pp] = phi[i] * pp; break; } else { mu[i * pp] = mu[i] * mu[pp], phi[i * pp] = phi[i] * phi[pp]; } } } for (int d = 1; d <= n; d++) { long long hh = 1ll * d * ksm(phi[d], p - 2) % p; for (int i = d; i <= n; i += d) { g[i] = (g[i] + hh * mu[i / d]) % p; } } for (int i = 1; i <= n; i++) g[i] = (g[i] % p + p) % p; } int fa[MAXN]; int e[MAXN << 1]; int dep[MAXN]; int cnt_e; int st[MAXN >> 1], ed[MAXN >> 1]; int lg[MAXN << 1]; int mnp[MAXN << 1][19]; void pre_dfs(int x, int f) { e[++cnt_e] = x; st[x] = cnt_e; dep[x] = dep[f] + 1; for (int i = T.frm[x]; i; i = T.edge[i].nxt) { int v = T.edge[i].to; if (v != f) { pre_dfs(v, x); e[++cnt_e] = x; } } ed[x] = cnt_e; } void pre_st() { for (int i = 2; i <= cnt_e; i++) lg[i] = lg[i >> 1] + 1; for (int i = 1; i <= cnt_e; i++) mnp[i][0] = e[i]; for (int j = 1; j <= 18; j++) { for (int i = 1; i + (1 << j) - 1 <= cnt_e; i++) { int ii = i + (1 << (j - 1)); if (dep[mnp[i][j - 1]] < dep[mnp[ii][j - 1]]) { mnp[i][j] = mnp[i][j - 1]; } else mnp[i][j] = mnp[ii][j - 1]; } } } int get_lca(int x, int y) { int l = st[x], r = ed[y]; if (l > r) l = st[y], r = ed[x]; int j = lg[r - l + 1]; int ll = r - (1 << j) + 1; if (dep[mnp[l][j]] < dep[mnp[ll][j]]) return mnp[l][j]; else return mnp[ll][j]; } int staV[MAXN], tot_V; int in_V[MAXN]; int opt[MAXN << 1], cnt_o; int stan[MAXN], tot_n; int sum[MAXN]; bool cmp1(int x, int y) { return st[x] < st[y]; } bool cmp2(int x, int y) { int k1 = x > 0 ? st[x] : ed[-x]; int k2 = y > 0 ? st[y] : ed[-y]; if (k1 == k2) return x > y; return k1 < k2; } void build() { sort(staV, staV + tot_V + 1, cmp1); for (int i = 1; i <= tot_V; i++) { opt[++cnt_o] = staV[i]; opt[++cnt_o] = -staV[i]; } for (int i = 2; i <= tot_V; i++) { int lca = get_lca(staV[i - 1], staV[i]); if (in_V[lca] == 0) { in_V[lca] = 2; opt[++cnt_o] = lca; opt[++cnt_o] = -lca; } } if (in_V[1] == 0) { in_V[1] = 2; opt[++cnt_o] = 1; opt[++cnt_o] = -1; } sort(opt + 1, opt + cnt_o + 1, cmp2); for (int i = 1; i <= cnt_o; i++) { if (opt[i] > 0) { if (opt[i] != 1) VT.insert(stan[tot_n], opt[i]); stan[++tot_n] = opt[i]; } else { tot_n--; } } } void clr() { for (int i = 1; i <= cnt_o; i++) { if (opt[i] > 0) { in_V[opt[i]] = false; VT.frm[opt[i]] = 0; sum[opt[i]] = 0; } } cnt_o = tot_V = 0; VT.cnt_e = 0; } long long ans2; void DP(int x) { int sumn = in_V[x] == 1 ? phi[a[x]] : 0; ans2 = add(ans2, 1ll * sumn * sumn % p * dep[x] % p); for (int i = VT.frm[x]; i; i = VT.edge[i].nxt) { int v = VT.edge[i].to; DP(v); ans2 = add(ans2, 1ll * sumn * sum[v] % p * 2 * dep[x] % p); sumn = add(sumn, sum[v]); } sum[x] = sumn; } void work() { for (int t = 1; t <= n; t++) { long long anst = 0; long long ans1 = 0, sumf = 0; ans2 = 0; for (int i = t; i <= n; i += t) { ans1 = add(ans1, 1ll * phi[i] * dep[pos[i]] % p); sumf = add(sumf, phi[i]); staV[++tot_V] = pos[i]; in_V[pos[i]] = 1; } anst = ans1 * sumf * 2 % p; build(); DP(1); ans2 = add(ans2, ans2); clr(); ans = add(ans, 1ll * g[t] * sub(anst, ans2) % p); } } int main() { n = read(); pre_G(); for (int i = 1; i <= n; i++) a[i] = read(), pos[a[i]] = i; for (int i = 1; i <= n - 1; i++) { int u = read(), v = read(); T.insert(u, v); T.insert(v, u); } pre_dfs(1, 1); pre_st(); work(); printf("%lld\n", ans * ksm(1ll * n * (n - 1) % p, p - 2) % p); return 0; }
5
#include <bits/stdc++.h> using namespace std; namespace io { const int SIZE = (1 << 21) + 1; char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55]; int f, qr; char getc() { return (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); } inline void flush() { fwrite(obuf, 1, oS - obuf, stdout); oS = obuf; } inline void putc(char x) { *oS++ = x; if (oS == oT) flush(); } template <class I> inline void gi(I &x) { for (f = 1, c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++); c < '0' || c > '9'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) if (c == '-') f = -1; for (x = 0; c <= '9' && c >= '0'; c = (iS == iT ? (iT = (iS = ibuf) + fread(ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS++)) : *iS++)) x = x * 10 + (c & 15); x *= f; } template <class I> inline void print(I x) { if (!x) putc('0'); if (x < 0) putc('-'), x = -x; while (x) qu[++qr] = x % 10 + '0', x /= 10; while (qr) putc(qu[qr--]); } struct Flusher_ { ~Flusher_() { flush(); } } io_flusher_; } // namespace io using io ::getc; using io ::gi; using io ::print; using io ::putc; template <class T> void upmax(T &x, T y) { x = x > y ? x : y; } template <class T> void upmin(T &x, T y) { x = x < y ? x : y; } const int N = 5005, V = 5000; bool np[N]; int p[N], pid[N], minFac[N], pc = 0; void pre() { np[0] = np[1] = true; for (int i = 2; i <= V; i++) { if (!np[i]) p[++pc] = i, pid[i] = pc; for (int j = 1; j <= pc && i * p[j] <= V; j++) { np[i * p[j]] = true; minFac[i * p[j]] = p[j]; if (i % p[j] == 0) break; } } } int fa[N * 2], len[N * 2], cnt[N]; int tr[N], sz[N * 2]; void modify(int p) { p = pc - p + 1; for (; p <= pc; p += p & -p) ++tr[p]; } int query(int p) { p = pc - p + 1; int res = 0; for (; p; p ^= (p & -p)) res += tr[p]; return res; } struct Edge { int v, w; Edge() {} Edge(int _v, int _w) : v(_v), w(_w) {} }; vector<Edge> G[N * 2]; int nc = 1; long long res = 0; int Node[N], Sum[N]; int getDepth(int x) { int res = 0; while (x != 1) res += len[x], x = fa[x]; return res; } void build() { nc = 1; sz[nc] = cnt[1] + cnt[0]; int facSum = 0; for (int i = 2; i <= V; i++) { if (!np[i]) { int now = ++nc; fa[now] = 1; len[now] = ++facSum; sz[now] = cnt[i]; modify(pid[i]); res += 1LL * facSum * cnt[i]; continue; } int v = i, facCnt = 0; while (np[v]) v /= minFac[v], ++facCnt; ++facCnt; int com = query(pid[v]), diff = facSum - com; int x = nc; while (diff >= len[x]) diff -= len[x], x = fa[x]; if (diff == 0) { int now = ++nc; fa[now] = x; facSum += facCnt; len[now] = facSum - com; sz[now] = cnt[i]; } else { int p = ++nc; fa[p] = fa[x]; len[p] = len[x] - diff; fa[x] = p; len[x] = diff; int now = ++nc; fa[now] = p; facSum += facCnt; len[now] = facSum - com; sz[now] = cnt[i]; } v = i; while (np[v]) modify(pid[minFac[v]]), v /= minFac[v]; modify(pid[v]); res += 1LL * facSum * cnt[i]; } } void dfs(int x, int fa) { for (const Edge &e : G[x]) { if (e.v == fa) continue; dfs(e.v, x); sz[x] += sz[e.v]; } } int main() { int n; gi(n); for (int i = 1, x; i <= n; i++) gi(x), ++cnt[x]; pre(); build(); for (int i = nc; i >= 2; i--) G[fa[i]].emplace_back(i, len[i]); dfs(1, 1); int p = 1; bool flag = true; while (flag) { flag = false; for (const Edge &e : G[p]) { if (sz[e.v] > (n >> 1)) { res -= 1LL * e.w * ((sz[e.v] << 1) - n); p = e.v; flag = true; break; } } } printf("%lld\n", res); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main(){ int x,y; cin >> x >> y; int ans=max(4-x,0)*100000+max(4-y,0)*100000; if(x==1 && y==1) ans+=400000; cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { int a; cin >> a; cout << 2 * a - 1 << " " << 2 << '\n'; cout << 1 << " " << 2; return 0; }
1
#include<bits/stdc++.h> using namespace std; string s; int main() { cin>>s; int ans=0,tot=0; for(int i=0;i<s.size();i++){ if(s[i]=='A'||s[i]=='T'||s[i]=='G'||s[i]=='C')tot++; else tot=0; ans=max(ans,tot); } cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int i, n, count = 0; string s; cin >> s; while (s.length() != 0) { int flag = 0; for (i = 1; i < s.length(); i++) { if (s[i - 1] == s[i]) { s.erase(i - 1, 2); flag = 1; count++; break; } } if (flag == 0) { break; } } if (count % 2 == 0) cout << "No"; else cout << "Yes"; }
2
#include <bits/stdc++.h> using namespace std; long long ta[1000000], la[1000000]; int main() { long long n, L, a, ti, li, i, dif, num = 0; cin >> n >> L >> a; for (i = 0; i < n; i++) { cin >> ta[i]; cin >> la[i]; la[i] += ta[i]; } if (n == 0) { num = L / a; cout << num; return 0; } dif = (ta[0] - 0) / a; num += dif; for (i = 1; i < n; i++) { dif = (ta[i] - la[i - 1]) / a; num += dif; } dif = (L - la[n - 1]) / a; num += dif; cout << num; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { char str[1000]; cin >> str; str[0] = toupper(str[0]); cout << str; return 0; }
1
#include <bits/stdc++.h> using namespace std; int dp[1001][1000], md = 1e9 + 7; inline void ad(int &x, int y) { if ((x += y) >= md) x -= md; } inline int pw(int x, int p) { int an = 1; while (p) { if (p & 1) an = (long long)an * x % md; x = (long long)x * x % md; p >>= 1; } return an; } int geo(int r) { int z = 1 - r + md; if (z >= md) z -= md; z = pw(z, md - 2); return z; } int main() { int k, pa, pb; scanf("%d%d%d", &k, &pa, &pb); int p = pw(pa + pb, md - 2); pa = (long long)pa * p % md; pb = (long long)pb * p % md; dp[1][0] = pa; int an = 0; for (int i = 1; i < (int)(k); ++i) for (int j = 0; j < (int)(k); ++j) { int nk = j + i; if (nk >= k) ad(an, (long long)dp[i][j] * pb % md * (nk - k) % md); else ad(dp[i][nk], (long long)dp[i][j] * pb % md); ad(dp[i + 1][j], (long long)dp[i][j] * pa % md); } for (int i = 0; i < (int)(k); ++i) { int z = 1 - pa + md; if (z >= md) z -= md; z = pw(z, md - 2); int a = (long long)z * z % md * pa % md; ad(a, (long long)i * geo(pa) % md); ad(an, (long long)a * pb % md * dp[k][i] % md); } an = (long long)an * geo(pb) % md; printf("%d\n", an + k); }
4
#include <bits/stdc++.h> using namespace std; int dy[109][2]; bool b[109]; int main() { int n, k, p, i, j, x; scanf("%d%d%d", &n, &k, &p); while (n--) { scanf("%d", &x); for (i = k; i > 1; i--) if (b[i - 1]) break; for (; i > 1; i--) { if (b[i] == 0) { b[i]++; dy[i][0] = dy[i - 1][0] + dy[i - 1][1]; dy[i][1] = x % p; } else { if (dy[i - 1][0] + dy[i - 1][1] < dy[i][0]) { dy[i][0] = dy[i - 1][0] + dy[i - 1][1]; dy[i][1] = x % p; } else { dy[i][1] = (dy[i][1] + x) % p; } } } dy[1][1] = (dy[1][1] + x) % p; b[1]++; } printf("%d", dy[k][0] + dy[k][1]); }
3
#include <bits/stdc++.h> const int maxn = 1000005, mo = int(1e9) + 7; using namespace std; vector<int> fact[maxn]; bool pri[maxn]; int lx[maxn], ly[maxn]; int fac[maxn], base[maxn], g[maxn], p[maxn], co_g[maxn], co_b[maxn], n; void read(int &x) { char c; while (c = getchar(), c < '0' || c > '9') ; x = c - 48; while (c = getchar(), c >= '0' && c <= '9') x = x * 10 + c - 48; } int main() { read(n); for (int i = 1; i <= n; i++) read(p[i]); fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * 1ll * i % mo; for (int i = 2; i <= n; i++) if (!pri[i]) for (int j = 1; i * j <= n; j++) { fact[i * j].push_back(i); if (j > 1) pri[i * j] = 1; } g[1] = 1; fact[1].push_back(1); for (int j = 2; j <= n; j++) { base[j] = 1; for (auto p : fact[j]) base[j] *= p; g[j] = n / j; } bool ok = 1; for (int i = 1; i <= n; i++) if (p[i]) { if (fact[p[i]].size() != fact[i].size()) { ok = 0; break; } for (int j = 0; j < fact[i].size(); j++) { int x = fact[i][j], y = fact[p[i]][j]; if (g[x] != g[y]) ok = 0; if (ly[x] && ly[x] != y || lx[y] && lx[y] != x) ok = 0; lx[y] = x, ly[x] = y; } } if (!ok) { cout << 0 << endl; return 0; } for (int i = 1; i <= n; i++) if (!ly[i] && !pri[i]) co_g[g[i]]++; for (int i = 1; i <= n; i++) co_b[base[i]]++; for (int i = 1; i <= n; i++) co_b[base[p[i]]]--; int ans = 1; for (int i = 1; i <= n; i++) ans = ans * 1ll * fac[co_g[i]] % mo; for (int i = 1; i <= n; i++) ans = ans * 1ll * fac[co_b[i]] % mo; cout << ans << endl; return 0; }
6
#include <bits/stdc++.h> using namespace std; long long exp(long long taban, long long us) { long long carpan = taban % 1000000007; long long temp = us; long long res = 1; while (temp) { if (temp % 2) res = (res * carpan) % 1000000007; temp /= 2; carpan = (carpan * carpan) % 1000000007; } return res; } long long ebob(long long a, long long b) { if (!a) return b; return ebob(b % a, a); } long long ekok(long long a, long long b) { return (a * b) / ebob(a, b); } double fonk(double x, double y) { return x * x + y * y; } vector<vector<int>> dp(5001, vector<int>(5001, 0)); vector<vector<int>> cnt = dp; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string a, b; cin >> a >> b; int n = a.size(), m = b.size(); long long res = 0; for (int i = 0; i < n; i++) { bool c = 0; for (int j = 0; j < m; j++) { if (a[i] == b[j]) { dp[i][j]++; c = 1; dp[i][j] = (dp[i][j] + cnt[i][j]) % 1000000007; } cnt[i + 1][j + 1] = dp[i][j]; dp[i][j + 1] = dp[i][j]; } res += c * dp[i][m - 1]; res %= 1000000007; } cout << res << endl; }
1
#include "bits/stdc++.h" using namespace std; const int N=2e5+20; int n,m,k,a[N],b[N],ans; long long dpa[N],dpb[N]; int main() { scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=n;i++) scanf("%d",&a[i]),dpa[i]=dpa[i-1]+a[i]; for(int i=1;i<=m;i++) scanf("%d",&b[i]),dpb[i]=dpb[i-1]+b[i]; for(int i=0;i<=n;i++) { if(dpa[i]>k) break; int j=upper_bound(dpb+1,dpb+m+1,k-dpa[i])-dpb-1; ans=max(ans,i+j); } cout<<ans; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e6 + 10; int n, cnt[maxn][2], sz[maxn], d[maxn]; vector<int> to[maxn]; long long ans; void dfs1(int u, int f) { sz[u] = 1; d[u] = d[f] + 1; for (int v : to[u]) { if (v == f) continue; dfs1(v, u); sz[u] += sz[v]; } } void dfs2(int u, int f, long long res) { if (u != 1) { cnt[u][0] = cnt[f][1]; cnt[u][1] = cnt[f][0]; } ans += (res + cnt[u][1]) / 2; for (int v : to[u]) { if (v == f) continue; int x = n - sz[v], y = sz[v]; dfs2(v, u, res - y + x); } } int main() { scanf("%d", &n); for (int i = 1; i < n; i++) { int u, v; scanf("%d", &u); scanf("%d", &v); to[u].push_back(v); to[v].push_back(u); } d[0] = -1; dfs1(1, 0); ans = 0; long long res = 0; for (int i = 1; i <= n; i++) { cnt[1][d[i] % 2]++; res += d[i]; } dfs2(1, 0, res); cout << ans / 2 << endl; return 0; }
5
#include <bits/stdc++.h> using namespace std; int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; vector<int> adj[100005]; int cnt, dist = 0, node = 1; bool vis[100005]; void dfs(int u, int lvl, int par) { if (lvl > dist) { dist = lvl; node = u; } for (auto it : adj[u]) { if (it == par) continue; dfs(it, lvl + 1, u); } } int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } dfs(1, 0, -1); dfs(node, 0, -1); cout << dist << endl; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); long long sum, _xor; cin >> sum >> _xor; long long _and = (sum - _xor); if (_and % 2 == 1) { cout << 0 << endl; return 0; } _and /= 2; long long res = 1; for (int go = 0; go < 60; go++) { if (_xor & (1LL << go)) { if (_and & (1LL << go)) { cout << 0 << endl; return 0; } res <<= 1; } } if (sum == _xor) res -= 2; cout << res << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; void test() { int n; string s; cin >> n >> s; for (int i = 0; i < n; ++i) { if (n & 1) { if (i % 2 == 0) { if ((s[i] - '0') & 1) { cout << "1\n"; return; } } } else { if (i % 2 == 1) { if ((s[i] - '0') % 2 == 0) { cout << "2\n"; return; } } } } cout << ((n & 1) ? 2 : 1) << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; cin >> t; for (int i = 0; i < t; ++i) { test(); } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int inf = 1e9 + 5; const long long int inf64 = 1e18 + 5; const int MAX = 1e3 + 5; int dp[MAX][MAX][2], k; string second; int rec(int pos = 0, int cnt = 0, bool f1 = 0) { if (pos == second.size()) { if (!cnt) return 0; int curr = 1; while (cnt > 1) { ++curr; cnt = __builtin_popcount(cnt); } return (curr == k); } int &res = dp[pos][cnt][f1]; if (~res) return res; res = 0; int mx = (f1 ? 1 : second[pos] - '0'); for (int i = 0; i <= mx; i++) { int ncnt = cnt + i; res = (res + rec(pos + 1, ncnt, f1 || (i < (second[pos] - '0')))) % 1000000007; } return res; } int main() { ios::sync_with_stdio(false); cin.tie(0); cin >> second; cin >> k; if (!k) cout << 1; else { memset(dp, -1, sizeof dp); if (k == 1) cout << (rec() - 1 + 1000000007) % 1000000007; else cout << rec(); } }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 400 + 10; const int INF = 1 << 26; int v[maxn], a[maxn], up[maxn][maxn], down[maxn][maxn], f[maxn][maxn]; int ans[maxn], n; int main() { scanf("%d", &n); for (int i = 0; i <= n; i++) for (int j = i; j <= n; j++) f[i][j] = up[i][j] = down[i][j] = -INF; for (int i = 1; i <= n; i++) scanf("%d", &v[i]); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= n; i++) { f[i][i] = v[1]; up[i][i] = down[i][i] = 0; } for (int i = 0; i <= n; i++) f[i + 1][i] = up[i + 1][i] = down[i + 1][i] = 0; for (int i = 2; i <= n; i++) for (int l = 1; l + i - 1 <= n; l++) { int r = l + i - 1; if (a[r] > a[l]) { for (int k = l; k < r; k++) if (a[k] == a[r] - 1 && up[l][k] > -INF && f[k + 1][r - 1] > -INF) up[l][r] = max(up[l][r], up[l][k] + f[k + 1][r - 1]); } if (a[r] < a[l]) { for (int k = l; k < r; k++) if (a[k] == a[r] + 1 && down[l][k] > -INF && f[k + 1][r - 1] > -INF) down[l][r] = max(down[l][r], down[l][k] + f[k + 1][r - 1]); } for (int k = l; k <= r; k++) { if (up[l][k] > -INF && down[k][r] > -INF) f[l][r] = max(f[l][r], up[l][k] + down[k][r] + v[a[k] - a[l] + 1 + a[k] - a[r]]); if (f[l][k] > -INF && f[k + 1][r] > -INF) f[l][r] = max(f[l][r], f[l][k] + f[k + 1][r]); } } for (int i = 1; i <= n; i++) { ans[i] = ans[i - 1]; for (int j = 1; j <= i; j++) if (f[j][i] > -INF) ans[i] = max(ans[i], ans[j - 1] + f[j][i]); } printf("%d\n", ans[n]); return 0; }
5
#include <bits/stdc++.h> using namespace std; char str[500010]; char seq[500010]; int forw[500010]; int backw[500010]; int mappings[500010]; int main() { int n, m, p; scanf("%d %d %d", &n, &m, &p); scanf("%s", str); scanf("%s", seq); int elev = 0; for (int i = 0; i < n; i++) { if (str[i] == '(') { forw[elev] = i; elev++; } else { elev--; int map = forw[elev]; mappings[map] = i; mappings[i] = map; } } for (int i = 0; i <= n; i++) { forw[i] = i + 1; backw[i + 1] = i; } for (int i = 0; i < m; i++) { if (seq[i] == 'R') { p = forw[p]; } else if (seq[i] == 'L') { p = backw[p]; } else { int cpos = mappings[p - 1] + 1; if (p < cpos) { forw[backw[p]] = forw[cpos]; backw[forw[cpos]] = backw[p]; if (forw[cpos] <= n) p = forw[cpos]; else p = backw[p]; } else { forw[backw[cpos]] = forw[p]; backw[forw[p]] = backw[cpos]; if (forw[p] <= n) p = forw[p]; else p = backw[cpos]; } } } for (int pos = forw[0]; pos <= n; pos = forw[pos]) { printf("%c", str[pos - 1]); } printf("\n"); }
5
#include <bits/stdc++.h> using namespace std; const int N_MAX = 104; int N; string S; long long A[N_MAX]; long long dp[N_MAX][N_MAX][N_MAX]; long long solve(int start, int end, int prefix) { if (end - start <= 0) return 0; if (end - start == 1) return A[prefix]; long long &answer = dp[start][end][prefix]; if (answer != 0) return answer; answer = A[prefix] + solve(start + 1, end, 1); for (int i = start + 1; i < end; i++) if (S[i] == S[start]) answer = max(answer, solve(start + 1, i, 1) + solve(i, end, prefix + 1)); return answer; } int main() { scanf("%d", &N); cin >> S; for (int i = 1; i <= N; i++) scanf("%I64d", &A[i]); printf("%I64d\n", solve(0, N, 1)); return 0; }
5
#include <bits/stdc++.h> using namespace std; pair<long long, long long> gaps[200005]; int ans[200005]; vector<pair<pair<long long, long long>, long long> > combined; set<pair<long long, long long> > st; int main() { int n, m; scanf("%d %d", &n, &m); for (int i = 0; i < n; ++i) scanf("%lld %lld", &gaps[i].first, &gaps[i].second); for (int i = 0; i < n - 1; ++i) { long long minGap = gaps[i + 1].first - gaps[i].second; long long maxGap = gaps[i + 1].second - gaps[i].first; combined.push_back(make_pair(make_pair(minGap, maxGap), (long long)i)); } for (int i = 0; i < m; ++i) { long long bridge; scanf("%lld", &bridge); combined.push_back( make_pair(make_pair(bridge, (long long)1e18), (long long)(-i - 1))); } int sz = combined.size(); sort(combined.begin(), combined.end()); int assign = 0; for (int i = 0; i < sz; ++i) { if (combined[i].second < 0) { set<pair<long long, long long> >::iterator it = st.lower_bound(make_pair(combined[i].first.first, -1)); if (it == st.end()) continue; int brNo = -1 * combined[i].second; int gapNo = (*it).second; assign++; ans[gapNo] = brNo; st.erase(it); } else st.insert(make_pair(combined[i].first.second, combined[i].second)); } if (assign == n - 1) { printf("Yes\n"); for (int i = 0; i < n - 1; ++i) printf("%d ", ans[i]); } else printf("No"); }
2