solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { string s; cin >> s; int n = s.length(); int m = 0; for (int i = 0; s[i]; i++) { if (s[i] == 'N') m++; } if (m == 1) { cout << "NO"; } else cout << "YES"; cout << "\n"; } return 0; }
1
#include<bits/stdc++.h> #define f first #define s second #define mp make_pair #define pi M_PI #define inf 1<<30 #define eps (1e-11) #define equals(a,b) (fabs((a)-(b))<eps) using namespace std; 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 k){ return Point(x*k,y*k);} Point operator/(double k){ return Point(x/k,y/k);} bool operator<(Point p)const{ return (x!=p.x ? x<p.x : y<p.y);} bool operator==(Point p)const{ return fabs(x-p.x)<eps && fabs(y-p.y)<eps;} double abs(){ return sqrt(norm());} double norm(){ return (x*x+y*y);} }; typedef Point Vector; typedef vector<Point> Polygon; class Circle{ public: Point c; double r; Circle(Point c=Point(),double r=0.0):c(c),r(r){} }; double norm(Vector a){ return (a.x*a.x+a.y*a.y);} double abs(Vector a){ return sqrt(norm(a));} double dot(Vector a,Vector b){ return (a.x*b.x+a.y*b.y);} double cross(Vector a,Vector b){ return (a.x*b.y-a.y*b.x);} double arg(Vector p){ return atan2(p.y,p.x);} Vector polar(double a,double r){ return Point(cos(r)*a,sin(r)*a);} pair<Point,Point> getCrossPoints(Circle c1,Circle c2){ double d=abs(c1.c-c2.c); double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(double)(2*c1.r*d)); double t=arg(c2.c-c1.c); return mp(c1.c+polar(c1.r,t+a),c1.c+polar(c1.r,t-a)); } // ??????????????????????????? bool isContain(Circle c1,Circle c2){ if((abs(c1.c-c2.c)+c2.r-c1.r)<eps)return true; return false; } int n; vector<pair<Point,double> > vp; vector<Circle> vc; bool check(Point p){ for(int i=0;i<n;i++){ if(vc[i].r-abs(p-vc[i].c)<-eps)return false; } return true; } bool ok(double mid){ vc.clear(); for(int i=0;i<n;i++){ if(vp[i].s-mid<eps)return false; vc.push_back(Circle(vp[i].f,sqrt(vp[i].s*vp[i].s-mid*mid))); } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(check(vc[i].c))return true; if(check(vc[j].c))return true; } } for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ if(i==j)continue; if((vc[i].r+vc[j].r)-abs(vc[i].c-vc[j].c)<-eps)return false; if(isContain(vc[j],vc[i]))continue; if(isContain(vc[i],vc[j]))continue; pair<Point,Point> pp=getCrossPoints(vc[i],vc[j]); if(check(pp.f))return true; if(check(pp.s))return true; } } return vc.size()==1; } int main() { int a,b,c; while(1){ cin>>n; if(n==0)break; vp.clear(); for(int i=0;i<n;i++){ cin>>a>>b>>c; vp.push_back(mp(Point(a,b),c)); } double l=0,r=1000,mid; for(int i=0;i<100;i++){ mid=(l+r)/2; if(ok(mid))l=mid; else r=mid; } printf("%.10f\n",mid); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5; long long n, t[N], w[N]; struct high { long long t, w; bool operator<(high other) const { return w - t > other.w - other.t; } }; priority_queue<high> phigh; struct leq { long long t, w; bool operator<(leq other) const { return t < other.t; } }; priority_queue<leq> pleq; int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 0; i < n; i++) { cin >> t[i] >> w[i]; if (t[i] > t[0]) phigh.push({t[i], w[i]}); else if (i) pleq.push({t[i], w[i]}); } int ok = 1, ans = phigh.size(); while (ok) { ok = 0; if (!phigh.empty() && t[0] > phigh.top().w - phigh.top().t) { ok++; t[0] -= phigh.top().w - phigh.top().t; t[0]--; phigh.pop(); } while (!pleq.empty() && pleq.top().t > t[0]) { phigh.push({pleq.top().t, pleq.top().w}); pleq.pop(); } ans = min(ans, (int)phigh.size()); } cout << ++ans << "\n"; }
4
#include <bits/stdc++.h> const int inf = 0x3f3f3f3f, Inf = 0x7fffffff; const long long INF = 0x7fffffffffffffff; const double eps = 1e-10; template <typename _Tp> _Tp gcd(const _Tp &a, const _Tp &b) { return (!b) ? a : gcd(b, a % b); } template <typename _Tp> __inline__ __attribute__((always_inline)) _Tp abs(const _Tp &a) { return a >= 0 ? a : -a; } template <typename _Tp> __inline__ __attribute__((always_inline)) _Tp max(const _Tp &a, const _Tp &b) { return a < b ? b : a; } template <typename _Tp> __inline__ __attribute__((always_inline)) _Tp min(const _Tp &a, const _Tp &b) { return a < b ? a : b; } template <typename _Tp> __inline__ __attribute__((always_inline)) void chmax(_Tp &a, const _Tp &b) { (a < b) && (a = b); } template <typename _Tp> __inline__ __attribute__((always_inline)) void chmin(_Tp &a, const _Tp &b) { (b < a) && (a = b); } template <typename _Tp> __inline__ __attribute__((always_inline)) bool _cmp(const _Tp &a, const _Tp &b) { return abs(a - b) <= eps; } template <typename _Tp> __inline__ __attribute__((always_inline)) void read(_Tp &x) { register char ch(getchar()); bool f(false); while (ch < 48 || ch > 57) f |= ch == 45, ch = getchar(); x = ch & 15, ch = getchar(); while (ch >= 48 && ch <= 57) x = (((x << 2) + x) << 1) + (ch & 15), ch = getchar(); if (f) x = -x; } template <typename _Tp, typename... Args> __inline__ __attribute__((always_inline)) void read(_Tp &t, Args &...args) { read(t); read(args...); } __inline__ __attribute__((always_inline)) int read_str(char *s) { register char ch(getchar()); while (ch == ' ' || ch == '\r' || ch == '\n') ch = getchar(); register char *tar = s; *tar = ch, ch = getchar(); while (ch != ' ' && ch != '\r' && ch != '\n' && ch != EOF) *(++tar) = ch, ch = getchar(); return tar - s + 1; } const int N = 800005; struct edge { int v, nxt, w; } c[N << 1]; int front[N], edge_cnt; int dg[N]; int n; int ans[N]; __inline__ __attribute__((always_inline)) void add(int u, int v, int id) { c[++edge_cnt] = (edge){v, front[u], id}, front[u] = edge_cnt; ++dg[u]; } bool vis[N]; void dfs(int x) { for (int i = front[x]; i; i = front[x]) { front[x] = c[i].nxt; if (!vis[c[i].w]) { vis[c[i].w] = true; ans[c[i].w] = i & 1; dfs(c[i].v); } } } int main() { read(n); int x, y; int cnt = 0; for (int i = 1; i <= n; ++i) { read(x, y); ++cnt; add(x, y + 200000, cnt); add(y + 200000, x, cnt); } for (int i = 1; i <= 400000; ++i) { if (dg[i] & 1) { ++cnt; add(400001, i, cnt); add(i, 400001, cnt); } } for (int i = 1; i <= 400001; ++i) { dfs(i); } for (int i = 1; i <= n; ++i) printf("%c", ans[i] ? 'r' : 'b'); printf("\n"); return 0; }
4
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #pragma GCC optimize("-ffloat-store") const long long N = 1e9 + 7; void solve() { long long n; cin >> n; vector<vector<long long> > v(n); vector<long long> p(n); for (long long i = 0; i < n; i++) p[i] = 1; vector<bool> marr(n, false); for (long long i = 0; i < n; i++) { long long k; cin >> k; v[i].resize(k); for (long long j = 0; j < k; j++) cin >> v[i][j]; } for (long long i = 0; i < n; i++) { for (long long j = 0; j < ((long long)(v[i]).size()); j++) { if (p[v[i][j] - 1]) { marr[i] = true; p[v[i][j] - 1] = 0; break; } } } bool f1 = false, f2 = false; long long pr = -1, prin = -1; for (long long i = 0; i < n; i++) if (p[i]) { f1 = true; pr = i + 1; break; } for (long long i = 0; i < n; i++) if (!marr[i]) { f2 = true; prin = i + 1; break; } if (f1 && f2) { cout << "IMPROVE\n" << prin << " " << pr << "\n"; } else cout << "OPTIMAL\n"; } int32_t main(int32_t argc, char** argv) { long long t; cin >> t; while (t--) solve(); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 5; int s[maxn]; priority_queue<int, vector<int>, greater<int> > que; int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &s[i]); que.push(s[i]); } sort(s, s + n); int ans = 0; for (int i = 0; i < n; i++) { while (!que.empty() && que.top() <= s[i]) { que.pop(); } if (que.empty()) break; if (que.top() > s[i]) { ans++; que.pop(); } } printf("%d\n", ans); return 0; }
3
#include <iostream> int getClass(float e) { if (e >= 1.1) return 0; else if (e >= 0.6) return 1; else if (e >= 0.2) return 2; else return 3; } int main() { int L[4] = {0,0,0,0}, R[4] = {0,0,0,0}; float l, r; while (std::cin >> l >> r) { L[getClass(l)]++; R[getClass(r)]++; } for (int i=0; i<4; i++) std::cout << L[i] << " " << R[i] << std::endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int mods = 1e9 + 7; const int maxn = 50 + 10; const int N = 1e5 + 10; const int E = 1e6 + 10; long long n, p, m; struct node { int x, y; node(int x, int y) : x(x), y(y) {} }; vector<node> a; bool good[maxn][maxn]; char h[maxn][maxn]; bool vis[maxn][maxn]; int b[4][2] = {0, 1, 0, -1, 1, 0, -1, 0}; int dfs(int x, int y) { vis[x][y] = 1; int res = true; for (int i = 0; i < 4; i++) { if ((x + b[i][0] > 0 && x + b[i][0] <= n) && (y + b[i][1] > 0 && y + b[i][1] <= m)) { if (vis[x + b[i][0]][y + b[i][1]] || h[x + b[i][0]][y + b[i][1]] == '#') continue; if (h[x + b[i][0]][y + b[i][1]] == 'B') { if (h[x][y] == 'G') { res = false; } else { h[x][y] = '#'; return res; } } else { res = min(res, dfs(x + b[i][0], y + b[i][1])); } } } return res; } int check(int x, int y) { if (h[x][y] == 'G') good[x][y] = 1; if (x == n && y == m) return true; vis[x][y] = 1; int res = 0; for (int i = 0; i < 4; i++) { if ((x + b[i][0] > 0 && x + b[i][0] <= n) && (y + b[i][1] > 0 && y + b[i][1] <= m)) { if (vis[x + b[i][0]][y + b[i][1]] || h[x + b[i][0]][y + b[i][1]] == '#') continue; else { res = max(res, check(x + b[i][0], y + b[i][1])); } } } return res; } int main() { int t; cin >> t; while (t--) { memset(vis, 0, sizeof(vis)); memset(good, 0, sizeof(good)); a.clear(); cin >> n >> m; for (long long i = 1; i <= (n); ++i) { for (long long j = 1; j <= (m); ++j) { cin >> h[i][j]; if (h[i][j] == 'G') { a.push_back(node(i, j)); } } } bool flg = 1; flg = dfs(n, m); if (!flg) { printf("No\n"); continue; } for (int i = 0; i < a.size(); i++) { if (good[a[i].x][a[i].y]) continue; memset(vis, 0, sizeof(vis)); flg = check(a[i].x, a[i].y); if (!flg) break; } if (flg) { printf("Yes\n"); } else printf("No\n"); } }
4
#include <bits/stdc++.h> using namespace std; long long n, arr[1000009], ans, xorr[1000009]; int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> arr[i]; xorr[i + 1] = xorr[i] ^ (i + 1); } for (int i = 1; i <= n; i++) { ans = ans ^ arr[i - 1]; long long t = (n + 1) / i; long long r = (n + 1) % i; if (r) ans = ans ^ xorr[r - 1]; if (t & 1) ans = ans ^ xorr[i - 1]; } cout << ans; }
3
#include <bits/stdc++.h> using namespace std; const long long INF = 1e18; const int inf = INT_MAX; const long long mod = 1e9 + 7; const int pi = acos(-1); const int dx[4] = {0, 0, 1, -1}; const int dy[4] = {1, -1, 0, 0}; const int N = 505; vector<int> g[N]; int c[N], sz[N], d[N], ch[4][4]; bool used[N]; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { cin >> c[i]; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { ch[i][j] = 1; } } ch[1][2] = 1; ch[2][3] = 1; ch[3][1] = 1; ch[1][3] = 2; ch[3][2] = 2; ch[2][1] = 2; for (int i = 1; i <= n; i++) { cin >> sz[i]; for (int j = 1; j <= sz[i]; j++) { int a; cin >> a; g[a].push_back(i); } } int ans = inf; for (int i = 1; i <= 3; i++) { int s = 0; for (int j = 1; j <= n; j++) { used[j] = 0; } for (int j = 1; j <= n; j++) { if (c[j] == i && sz[j] == 0) { s = j; break; } } if (s == 0) { continue; } for (int j = 1; j <= n; j++) { d[j] = sz[j]; } priority_queue<pair<int, int> > q; q.push(make_pair(i, s)); used[s] = 1; int res = 0; int cur = i; while (!q.empty()) { int v = q.top().second; int val = q.top().first; if (cur != val) res += ch[cur][val], cur = val; q.pop(); for (int j = 0; j < g[v].size(); j++) { int to = g[v][j]; d[to]--; } bool w = 0; for (int j = 1; j <= n; j++) { if (!used[j] && d[j] == 0 && c[j] == cur) { q.push(make_pair(c[j], j)); used[j] = 1; w = 1; break; } } if (!w) { int p = cur + 1; if (p == 4) p = 1; bool u = 0; for (int j = 1; j <= n; j++) { if (!used[j] && d[j] == 0 && c[j] == p) { q.push(make_pair(c[j], j)); u = 1; used[j] = 1; break; } } if (!u) { p++; if (p == 4) p = 1; for (int j = 1; j <= n; j++) { if (!used[j] && d[j] == 0 && c[j] == p) { q.push(make_pair(c[j], j)); used[j] = 1; break; } } } } } ans = min(ans, res); } cout << ans + n; }
3
#include <bits/stdc++.h> using namespace std; int dx[] = {0, 0, 1, -1}; int dy[] = {1, -1, 0, 0}; int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1}; int dy8[] = {1, -1, -1, 0, 1, -1, 0, 1}; int kx8[] = {1, 1, 2, 2, -1, -1, -2, -2}; int ky8[] = {2, -2, 1, -1, 2, -2, 1, -1}; long long bigmod(long long a, long long b, long long c) { if (b == 0) return 1 % c; long long x = bigmod(a, b / 2, c); x = (x * x) % c; if (b % 2 == 1) x = (x * a) % c; return x; } long long mod_inverse(long long a, long long mod) { return bigmod(a, mod - 2, mod); } long long a[100005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t, tt = 1; cin >> t; while (t--) { int n, k; cin >> n >> k; bool flg = 0; for (int i = 0; i < (n); i++) { cin >> a[i]; if (a[i] == k) flg = 1; } if (!flg) { cout << "No\n"; continue; } if (n == 1) { cout << "Yes\n"; continue; } if (n == 2) { if (a[0] >= k && a[1] >= k) cout << "Yes\n"; else cout << "No\n"; continue; } for (int i = 0; i + 2 < n; i++) { int cnt = 0; if (a[i] >= k) cnt++; if (a[i + 1] >= k) cnt++; if (a[i + 2] >= k) cnt++; if (cnt >= 2) { flg = 0; break; } } if (flg) cout << "No\n"; else cout << "Yes\n"; } return 0; }
2
/*+lmake * STD = c++14 * DEFINE += WAAUTOMATON */ #include <bits/stdc++.h> using namespace std; using LL=long long; using ULL=unsigned long long; #ifdef WAAUTOMATON #define debug(args...) {dbg,args; cerr<<endl;} #else #define debug(args...) // Just strip off all debug tokens #endif struct debugger { template<typename T> debugger& operator , (const T& v) { cerr<<v<<" "; return *this; } } dbg; #define MAXN 100000 int c[MAXN+10]; bool b[MAXN+10]; void solve(long long N, vector<vector<long long>> a){ int r[2]={0,0},f[2]={0,0}; for(int i=0; i<N; ++i) { c[i]=a[1][i]/3+1; if (c[i]%2==i%2) throw 2; if (a[0][i]==c[i]*3-2 && a[1][i]==c[i]*3-1 && a[2][i]==c[i]*3) { } else if (a[0][i]==c[i]*3 && a[1][i]==c[i]*3-1 && a[2][i]==c[i]*3-2) { r[(i+1)&1]^=1; } else { throw 1; } } memset(b,true,sizeof(b)); for(int i=1; i<=N; ++i) { if (b[i]) { int x=c[i-1]; while(x!=i) { debug(x); b[x]=false; f[i&1]^=1; x=c[x-1]; } } } debug(f[0],f[1]); debug(r[0],r[1]); if (f[0]!=r[1] || f[1]!=r[0]) throw 3; puts("Yes"); } int main(){ #ifdef WAAUTOMATON freopen("in.txt","r",stdin); #endif long long N; scanf("%lld",&N); vector<vector<long long>> a(3-1+1,vector<long long>(N-1+1)); for(int i = 0 ; i <= 3-1 ; i++){ for(int j = 0 ; j <= N-1 ; j++){ scanf("%lld",&a[i][j]); } } try { solve(N, a); } catch(int err) { puts("No"); debug(err); } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 105; const int M = 505; const int MOD = int(1e9) + 7; const int INF = 0x3f3f3f3f; const double EPS = 1e-9; const double PI = acos(-1.0); const int dx[] = {-1, 1, 0, 0}; const int dy[] = {0, 0, -1, 1}; template <class T> inline T Min(T a, T b) { return a < b ? a : b; } template <class T> inline T Max(T a, T b) { return a > b ? a : b; } template <class T> inline T Min(T a, T b, T c) { return min(min(a, b), c); } template <class T> inline T Max(T a, T b, T c) { return max(max(a, b), c); } template <class T> inline T sqr(T a) { return a * a; } template <class T> inline T cub(T a) { return a * a * a; } template <class T> inline T gcd(T a, T b) { return b == 0 ? a : gcd(b, a % b); } template <class T> inline T lcm(T a, T b) { return a * b / gcd(a, b); } int main() { int a, b; while (scanf("%d%d", &a, &b) != EOF) { int i, tot = (a * (a - 1)) / 2; if (tot <= b) { printf("no solution\n"); } else { for (i = 0; i < a; i++) { printf("%d %d\n", 0, i); } } } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int NMAX = 310000, KMAX = 310000; struct Node { int fa; int w; int nSame; bool isForce; bool isSame; }; int read() { int k = 0, f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) { if (c == '-') f = -1; } for (; isdigit(c); c = getchar()) k = k * 10 + c - '0'; return k * f; } int N, K, ans; int initState[NMAX]; vector<int> op[NMAX]; Node nodes[KMAX]; int Find(int x) { int p, w, tmp, tmpW; for (p = x, w = 0; nodes[p].fa >= 0; w ^= nodes[p].w, p = nodes[p].fa) ; while (x != p) { tmp = nodes[x].fa; tmpW = nodes[x].w; nodes[x].fa = p; nodes[x].w = w; x = tmp; w ^= tmpW; } return p; } int CalContrib(int x) { int X = Find(x); if (nodes[X].isForce) { if (nodes[X].isSame) return nodes[X].nSame; else return -nodes[X].fa - nodes[X].nSame; } else return min(nodes[X].nSame, -nodes[X].fa - nodes[X].nSame); } void Force(int x, int v) { int X = Find(x); v ^= nodes[x].w; ans -= CalContrib(X); if (nodes[X].isForce) assert(nodes[X].isSame == (bool)v); else { nodes[X].isForce = true; nodes[X].isSame = (bool)v; } ans += CalContrib(X); } void Union(int x, int y, int w) { int X = Find(x), Y = Find(y); if (X == Y) return; if (nodes[X].fa > nodes[Y].fa) swap(X, Y); ans -= CalContrib(X) + CalContrib(Y); nodes[Y].w = w ^ nodes[x].w ^ nodes[y].w; if (nodes[Y].w) nodes[X].nSame += -nodes[Y].fa - nodes[Y].nSame; else nodes[X].nSame += nodes[Y].nSame; if (nodes[X].isForce && nodes[Y].isForce) { if (nodes[Y].w) assert(nodes[X].isSame != nodes[Y].isSame); else assert(nodes[X].isSame == nodes[Y].isSame); } else if (nodes[X].isForce) { } else if (nodes[Y].isForce) { nodes[X].isForce = true; if (nodes[Y].w) nodes[X].isSame = !nodes[Y].isSame; else nodes[X].isSame = nodes[Y].isSame; } else { } nodes[X].fa += nodes[Y].fa; nodes[Y].fa = X; ans += CalContrib(X); } int main() { scanf("%d %d", &N, &K); for (int i = 1; i <= N; i += 1) { char ch; scanf(" %c", &ch); initState[i] = (int)(ch - '0'); } for (int i = 1; i <= K; i += 1) { int nEle; nEle = read(); for (int j = 1; j <= nEle; j += 1) { int e; e = read(); op[e].push_back(i); } } for (int i = 1; i <= K; i += 1) nodes[i] = (Node){-1, 0, 1, false, false}; for (int i = 1; i <= N; i += 1) { if (op[i].size() == 1) Force(op[i][0], 1 - initState[i]); else if (op[i].size() == 2) Union(op[i][0], op[i][1], 1 - initState[i]); printf("%d\n", ans); } exit(0); }
3
#include <bits/stdc++.h> using namespace std; const long long N = 1e18; long long l, r, k; void solve() { cin >> l >> r >> k; long long i = 1, flag = 1, j; while (i <= r || i < 0) { if (i >= l) { cout << i << " "; flag = 0; } if (N / i < k) break; i = i * k; } if (flag == 1) cout << "-1"; cout << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); }
1
#include<bits/stdc++.h> using namespace std; long long a[605]; vector<int> vv; signed main(){ long long n; cin >> n; for(int i=0;i<n;i++){ cin >> a[i]; } for(int i=0;i<n;i++){ for(int j=1;j<n;j++){ vv.push_back(1); for(int k=1;k<n;k++){ swap(a[k-1],a[k]); } if(a[0]<a[n-1]){ vv.push_back(n-1); swap(a[0],a[n-1]); } } vv.push_back(1); for(int j=1;j<n;j++){ swap(a[j],a[j-1]); } } cout << vv.size()<<endl; for(auto x: vv){ cout << x<<endl; } }
0
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma G++ optimize("O3") using namespace std; const double PI = acos(-1.0); const int MAXN = 3e5 + 55; const int INF = 0x3f3f3f3f; const int NINF = 0x80808080; const long long NLINF = 0x8080808080808080; const long long LINF = 0x3f3f3f3f3f3f3f3f; const long long mo = 1e9 + 7; inline long long fpow(long long a, long long b) { long long s = 1; a %= mo; while (b) { if (b & 1) s = s * a % mo; a = a * a % mo; b >>= 1; } return s; } namespace Fast_IO { const int MAXL((1 << 18) + 1); int iof, iotp; char ioif[MAXL], *ioiS, *ioiT, ioof[MAXL], *iooS = ioof, *iooT = ioof + MAXL - 1, ioc, iost[55]; inline char Getchar() { if (ioiS == ioiT) { ioiS = ioif; ioiT = ioiS + fread(ioif, 1, MAXL, stdin); return (ioiS == ioiT ? EOF : *ioiS++); } else return (*ioiS++); } inline int read() { int x = 0; for (iof = 1, ioc = Getchar(); (ioc < '0' || ioc > '9') && ioc != EOF;) iof = ioc == '-' ? -1 : 1, ioc = Getchar(); if (ioc == EOF) exit(0); for (x = 0; ioc <= '9' && ioc >= '0'; ioc = Getchar()) x = (x << 3) + (x << 1) + (ioc ^ 48); return x * iof; } inline long long read_ll() { long long x = 0; for (iof = 1, ioc = Getchar(); (ioc < '0' || ioc > '9') && ioc != EOF;) iof = ioc == '-' ? -1 : 1, ioc = Getchar(); if (ioc == EOF) exit(0); for (x = 0; ioc <= '9' && ioc >= '0'; ioc = Getchar()) x = (x << 3) + (x << 1) + (ioc ^ 48); return x * iof; } } // namespace Fast_IO using namespace Fast_IO; char s[MAXN]; long long ans[30]; set<int> st; int a[30], b[30]; int has; int main() { scanf("%s", s + 1); int n = strlen(s + 1); st.insert(0); for (int i = 1; i <= n; i++) { int num = s[i] - 'a' + 1; if (a[num] == 0) has++; a[num] = i; for (int j = 1; j <= 26; j++) { if (a[j]) b[j] = a[j]; else b[j] = INF; } sort(b + 1, b + 1 + 26); int now = has; for (int j = 1; j <= has; j++) { ans[now] += b[j] - b[j - 1]; now--; } } printf("%d\n", has); for (int i = 1; i <= has; i++) printf("%lld\n", ans[i]); }
3
#include <bits/stdc++.h> using namespace std; int main() { char a[10001]; int b, k, j, l, i; cin >> a; b = strlen(a); for (i = b, j = b - 2; i < (2 * b - 1); i++, j--) { a[i] = a[j]; } a[i] = '\0'; cout << a; return 0; }
1
#include <iostream> using namespace std; int main() { int n,m,l; long a[100][100]={},b[100][100]={},c[100][100]={}; cin>>n>>m>>l; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ cin>>a[i][j]; } } for(int i=0;i<m;i++){ for(int j=0;j<l;j++){ cin>>b[i][j]; } } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ for(int k=0;k<l;k++){ c[i][k]+=a[i][m-1-j]*b[m-1-j][k]; } } } for(int i=0;i<n;i++){ for(int j=0;j<l;j++){ if(j<l-1){cout<<c[i][j]<<" "; }else{cout<<c[i][j]<<endl;} } } return 0; }
0
#include <bits/stdc++.h> using namespace std; pair<int, int> p[300000 + 100]; int res[300000 + 100]; bool Cmp(pair<int, int> A, pair<int, int> B) { return A.first < B.first; } int main() { ios_base::sync_with_stdio(0); int n, maX = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> p[i].first; p[i].second = i; } sort(p, p + n, Cmp); for (int i = 0; i < n; i++) { maX = max(maX + 1, p[i].first); res[p[i].second] = maX; } for (int i = 0; i < n; i++) cout << res[i] << " "; }
3
#include <bits/stdc++.h> using namespace std; int main(){ string s; cin>>s; if (s.length()==2) cout<<s; else { for (int i=s.length()-1;i>=0;i--) cout<<s[i]; } }
0
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:836777216") using namespace std; int n, m; string ask[10]; int match[10]; char s[40]; map<int, int> cnt; int main(void) { scanf("%d%d", &n, &m); for (int(i) = 0; (i) < (m); (i)++) { scanf("%s", &s); ask[i] = (string)s; scanf("%d", &match[i]); } for (int(i) = 0; (i) < ((1 << (n / 2))); (i)++) { int cur = 0; int mn = 6; for (int(j) = 0; (j) < (m); (j)++) { int ans = 0; for (int(k) = 0; (k) < (n / 2); (k)++) { if (i & (1 << (k))) { if (ask[j][k] == '1') { ans++; } } else { if (ask[j][k] == '0') { ans++; } } } if (ans <= match[j]) { cur *= mn; cur += ans; } else { cur = -1; break; } } if (cur != -1) cnt[cur]++; } long long result = 0; for (int(i) = 0; (i) < ((1 << (n / 2 + n % 2))); (i)++) { int cur = 0; int mn = 6; for (int(j) = 0; (j) < (m); (j)++) { int ans = 0; for (int(k) = 0; (k) < ((n / 2 + n % 2)); (k)++) { if (i & (1 << (k))) { if (ask[j][k + n / 2] == '1') { ans++; } } else { if (ask[j][k + n / 2] == '0') { ans++; } } } if (ans > match[j]) { cur = -1; break; } else { cur *= mn; cur += (match[j] - ans); } } if (cur != -1) { result += (long long)cnt[cur]; } } printf("%lld\n", result); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, m, l; cin >> n >> m >> l; long long a[n]; for (int(i) = (0); (i) < (n); ++(i)) cin >> a[i]; int total = 0; for (int(i) = (0); (i) < (n); ++(i)) { if (a[i] > l && (i == 0 || a[i - 1] <= l)) ++total; } while (m--) { int type; cin >> type; if (!type) cout << total << endl; else { int pos, val; cin >> pos >> val; --pos; if (a[pos] <= l && a[pos] + val > l) { if ((pos == 0 || a[pos - 1] <= l) && (pos == n - 1 || a[pos + 1] <= l)) ++total; else if (pos > 0 && pos < n - 1 && a[pos - 1] > l && a[pos + 1] > l) --total; } a[pos] += val; } } return 0; }
2
#include <bits/stdc++.h> #include <algorithm> using namespace std; int main(){ long long int n,a[200010]; cin >> n ; for(int i=0;i<n;i++) cin >> a[i]; map<int, int> b; for (int i = 0; i < n; i++) b[a[i]]++; int ans = 0; for (auto p: b) { int x = p.first; int n = p.second; if (n < x) ans += n; else ans += n - x; } cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; typedef long long int ll; ll gcd (ll a, ll b){ if (b==0) return a; return gcd(b, a%b); } int main() { ll N,X; scanf("%lld%lld", &N,&X); printf("%lld\n",3*(N-gcd(N,X))); }
0
#include <bits/stdc++.h> using namespace std; int main() { long long k, l; cin >> k >> l; long long c = 0; while (1) { long long rem = l % k; l = l / k; if (rem < k && rem > 0) { cout << "NO"; return 0; } c++; if (l == 1 || l == 0) break; } if (l) { cout << "YES" << endl << c - 1 << endl; } else cout << "NO" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<pair<int, int> > data(n + 1); for (int i = 0; i < n + 1; ++i) cin >> data[i].first >> data[i].second; int ans = 0; for (int i = 0; i < n; ++i) { if (data[i % (n + 1)].first < data[(i + 1) % (n + 1)].first && data[i + 1 % (n + 1)].second < data[(i + 1 + 1) % (n + 1)].second) ans++; if (data[i % (n + 1)].second < data[(i + 1) % (n + 1)].second && data[i + 1 % (n + 1)].first > data[(i + 1 + 1) % (n + 1)].first) ans++; if (data[i % (n + 1)].second > data[(i + 1) % (n + 1)].second && data[i + 1 % (n + 1)].first < data[(i + 1 + 1) % (n + 1)].first) ans++; if (data[i % (n + 1)].first > data[(i + 1) % (n + 1)].first && data[i + 1 % (n + 1)].second > data[(i + 1 + 1) % (n + 1)].second) ans++; } cout << ans; return 0; }
4
#include <bits/stdc++.h> using namespace std; #define MAX 123456 typedef pair<int, int> pii; int main() { int N, L, x; cin >> N >> L; priority_queue<pii> pq; for (int i = 0; i < N; i++) { cin >> x; pq.push(pii(x, i + 1)); } int dp[MAX] = {}; while (!pq.empty()) { pii p = pq.top(); pq.pop(); int f = p.first, s = p.second; dp[s] = max(dp[s-1], dp[s+1]) + (L - f); } cout << *max_element(dp + 1, dp + N + 1) << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int nxt[55], l, k; string a; void getnext(string s) { int j = -1; nxt[0] = -1; for (int i = 1; i < l; i++) { while (j != -1 && s[j + 1] != s[i]) j = nxt[j]; if (s[j + 1] == s[i]) j++; nxt[i] = j; } return; } int main() { scanf("%d%d", &l, &k); cin >> a; getnext(a); int len = l - 1 - nxt[l - 1]; for (int i = 1; i <= k; i++) for (int j = 0; j < len; j++) cout << a[j]; for (int i = len; i < l; i++) cout << a[i]; return 0; }
1
#include <cstdio> int main() { int n,p,sum=0; scanf("%d",&n); for(int i=1,p=n+1; i<=n; i++) { int x; scanf("%d",&x); if(x<p) { sum++; p=x; } } printf("%d",sum); return 0; }
0
#include <bits/stdc++.h> using namespace std; signed main() { long long int t = 1; while (t--) { long long int n, k; cin >> n >> k; vector<long long int> a(k); long long int type, rem = 1e18; for (long long int i = 0; i < k; ++i) { cin >> a[i]; long long int r = n % a[i]; if (rem > r) { rem = r; type = i + 1; } } cout << type << " " << (n / a[type - 1]); } }
2
#include <bits/stdc++.h> using namespace std; const int N = 1e6; const long long mod = 1e18; const int MASK = (1 << 30) - 1; int n, w; long long ans[2]; long long ans1; char s[N]; int Next[N], Next_c[N][26]; struct node { bool tag; int tagv; int sum, v; long long allv; } f[N * 4]; int bz[N]; int tag; vector<pair<int, int> > d; void down(int l, int r, int s) { if (f[s].tag) { f[s].allv = 1ll * f[s].sum * f[s].tagv; f[s].v = f[s].tagv; if (l != r) f[s + s].tag = f[s + s + 1].tag = 1, f[s + s].tagv = f[s + s + 1].tagv = f[s].tagv; f[s].tag = 0; } } void combine(node& a, node b, node c) { a.allv = b.allv + c.allv; a.sum = b.sum + c.sum; } void ins(int l, int r, int s, int ll, int sum) { down(l, r, s); if (r < ll || l > ll) return; if (l == r) { f[s].sum = sum; f[s].allv = 1ll * f[s].v * f[s].sum; return; } ins(l, (l + r) / 2, s + s, ll, sum); ins((l + r) / 2 + 1, r, s + s + 1, ll, sum); combine(f[s], f[s + s], f[s + s + 1]); } void add(int l, int r, int s, int ll, int rr, int v) { down(l, r, s); if (r < ll || rr < l) return; if (ll <= l && r <= rr) { f[s].tag = 1; f[s].tagv = v; down(l, r, s); return; } add(l, (l + r) / 2, s + s, ll, rr, v); add((l + r) / 2 + 1, r, s + s + 1, ll, rr, v); combine(f[s], f[s + s], f[s + s + 1]); } void dfs(int x, int c, int wz) { if (bz[x] == tag) return; bz[x] = tag; ins(0, n, 1, wz - x - 1, 0); for (int i = 0; i < 26; i++) if (Next_c[x][i] && i != c) dfs(Next_c[x][i] - 1, c, wz); } void output() { if (ans[1]) { printf("%lld", ans[1]); for (long long i = mod / 10; i >= 1; i /= 10) printf("%lld", ans[0] / i % 10); } else { printf("%lld\n", ans[0]); } printf("\n"); } int main() { Next[0] = 0; scanf("%d", &n); scanf(" %c %d", &s[0], &w); s[0] -= 'a'; ans[0] = w; ans1 = w; output(); ins(0, n, 1, 0, 1); add(0, n, 1, 0, 0, w); d.push_back(make_pair(0, w)); for (int i = 1, j = 0; i < n; i++) { scanf(" %c %d", &s[i], &w); w = w ^ (ans1 & MASK); s[i] -= 'a'; s[i] = (s[i] + ans[1] * (mod % 26) % 26 + ans[0]) % 26; while (j && s[i] != s[j]) { ins(0, n, 1, i - j, 0); j = Next[j - 1]; } if (s[i] == s[j]) { for (int k = 0; k < 26; k++) { Next_c[i][k] = Next_c[j][k]; } Next_c[i][s[j + 1]] = j + 1; ++tag; if (j) for (int k = 0; k < 26; k++) if (Next_c[j - 1][k] && k != s[i]) dfs(Next_c[j - 1][k] - 1, s[i], i); j++; } if (s[0] == s[i]) { ins(0, n, 1, i, 1); } Next[i] = j; while (d.size() && d[d.size() - 1].second >= w) d.pop_back(); if (!d.size()) { add(0, n, 1, 0, i, w); } else { add(0, n, 1, d[d.size() - 1].first + 1, i, w); } d.push_back(make_pair(i, w)); ans[0] += f[1].allv; ans[1] += ans[0] / mod; ans[0] %= mod; ans1 += f[1].allv; output(); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int inf = 1000000007; const long long INF = 1e18; int mod = 998244353; template <typename T1, typename T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return 1; } return 0; } template <typename T1, typename T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return 1; } return 0; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { return a / gcd(a, b) * b; } template <typename T, typename U> T pow_(T a, U b) { return b ? pow_(a * a, b / 2) * (b % 2 ? a : 1) : 1; } long long modpow(long long a, long long b, long long _mod) { return b ? modpow(a * a % _mod, b / 2, _mod) * (b % 2 ? a : 1) % _mod : 1; } template <class T, class U> ostream& operator<<(ostream& os, const pair<T, U>& p) { os << p.first << " " << p.second; return os; } template <class T> ostream& operator<<(ostream& os, const vector<T>& vec) { for (int i = 0; i < ((int)vec.size()); ++i) { if (i) os << " "; os << vec[i]; } return os; } template <typename T> inline istream& operator>>(istream& is, vector<T>& v) { for (int j = 0; j < ((int)v.size()); ++j) is >> v[j]; return is; } template <class T, class T2> inline void add(T& a, T2 b) { a += b; if (a >= mod) a -= mod; } void solve(); int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cout << fixed << setprecision(10); int T; T = 1; while (T--) solve(); } void solve() { int n; long long k; cin >> n >> k; vector<int> a(n); cin >> a; long long ma = *max_element(a.begin(), a.end()); int ans = 0; vector<int> b(n); auto Get = [&](long long limit) { long long sum = 0; for (int i = 0; i < (n); ++i) { auto binary_search = [&](int ok, int ng) { auto check = [&](int x) { if (a[i] - 3LL * x * (x + 1) - 1 >= limit) return 1; return 0; }; int d = 1; while ((ng - ok >= 0 ? ng - ok : -(ng - ok)) > d) { int mid = (ng + ok) / 2; (check(mid) ? ok : ng) = mid; } return ok; }; b[i] = binary_search(0, a[i] + 1); sum += b[i]; } return sum; }; auto binary_search = [&](long long ok, long long ng) { auto check = [&](long long x) { long long s = Get(x); if (s >= k) return 1; return 0; }; int d = 1; while ((ng - ok >= 0 ? ng - ok : -(ng - ok)) > d) { long long mid = (ng + ok) / 2; (check(mid) ? ok : ng) = mid; } return ok; }; long long L = binary_search(-4e18, 4e18); long long s = Get(L); for (int i = 0; i < (s - k); ++i) { long long mi = INF; int id = -1; for (int j = 0; j < (n); ++j) if (chmin(mi, a[j] - 3LL * b[j] * b[j] + 3 * b[j] - 1)) id = j; b[id]--; } cout << b << '\n'; }
4
#include <bits/stdc++.h> using namespace std; int main() { int k; string n; cin >> n >> k; int num = 0; for (int i = 0; i < n.length(); i++) { if (n[i] == '0') num++; } if (num < k) cout << n.length() - 1; else if (num == k) { int pot = 0, ans = 0; for (int i = n.length() - 1; i >= 0; i--) { if (n[i] == '0') pot++; if (pot == k) break; if (n[i] != '0') ans++; } cout << ans; } else if (num > k) { int pot = 0, ans = 0; for (int i = n.length() - 1; i >= 0; i--) { if (n[i] == '0') pot++; if (pot == k) break; if (n[i] != '0') ans++; } cout << ans; } }
2
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long INF = 1ll << 60; const double PI = acos(-1.0); int n, m; long long pw[19]; long long sum[20]; long long calc(long long x) { if (x == 0) return 0; return 45ll * x * pw[x - 1]; } long long find(long long k) { long long x, y; int num[20]; int m = 0; while (k) { num[m] = k % 10; m++; k /= 10; } long long res = 0; long long cnt = 0; for (int i = 0; i < m; i++) { if (num[i]) { res += calc(i) * num[i]; res += sum[num[i] - 1] * pw[i]; res += num[i] * (cnt + 1); } cnt += num[i] * pw[i]; } return res; } int main() { pw[0] = 1; pw[1] = 10; sum[0] = 0; sum[1] = 1; for (int i = 2; i <= 19; i++) pw[i] = pw[i - 1] * 10, sum[i] = sum[i - 1] + i; long long x, y; long long a; while (~scanf("%I64d", &a)) { long long l = 0, r = 100000000000000000LL; long long ansl, ansr; long long mid; while (l < r) { mid = (l + r) >> 1; if (find(mid) >= a) r = mid; else l = mid + 1; } ansl = 1; ansr = l; x = find(ansl - 1); y = find(ansr); while (y - x != a) { if (y - x > a) ansl++; else ansr++; x = find(ansl - 1); y = find(ansr); } printf("%I64d %I64d\n", ansl, ansr); } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAX = 1e5 + 10; vector<pair<long long, long long> > adj[MAX]; bool vis[MAX]; long long dist[MAX], dd[MAX], ww[MAX]; int Dijkstra(int src, int k) { priority_queue<pair<long long, long long> > pq; int ans = 0; pq.push(pair<long long, long long>(0, src)); dist[src] = 0; for (int i = 0; i < k; i++) pq.push(pair<long long, long long>(-ww[i], -dd[i])); while (pq.size()) { long long d = -pq.top().first; int u = pq.top().second; pq.pop(); if (u < 0) { u *= -1; if (vis[u]) ans++; } if (vis[u]) continue; vis[u] = 1; for (int i = 0; i < adj[u].size(); i++) { int v = adj[u][i].first, w = adj[u][i].second; if (d + w < dist[v]) { dist[v] = d + w; pq.push(pair<long long, long long>(-dist[v], v)); } } } return ans; } void init() { fill(dist, dist + MAX, 1e15); memset(vis, 0, sizeof vis); for (int i = 0; i < MAX; i++) adj[i].clear(); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m, k, u, v, w; init(); cin >> n >> m >> k; for (int i = 0; i < m; i++) { cin >> u >> v >> w; adj[u].push_back(pair<long long, long long>(v, w)); adj[v].push_back(pair<long long, long long>(u, w)); } for (int i = 0; i < k; i++) { cin >> v >> w; dd[i] = v; ww[i] = w; } int ans = Dijkstra(1, k); cout << ans << endl; }
2
#include <bits/stdc++.h> using namespace std; const int MAXN = 2007; int i, j, k, n, m, t, x, y, cnt; bool mp[MAXN][MAXN], mkr[MAXN][MAXN], mkc[MAXN][MAXN]; char c[MAXN]; void changeDiagonalLine(int x, int y) { bool mk; mkr[x][y] = mkr[x][y - 1] ^ mkr[x][y + 1]; mkc[x][y] = mkc[x + 1][y] ^ mkc[x - 1][y]; mk = mkr[x][y] ^ mkc[x][y]; if (mk ^ mp[x][y]) { cnt++; mkr[x][y] ^= 1; mkc[x][y] ^= 1; } } void changeRightTop(int x, int y) { bool mk; mkr[x][y] = mkr[x][y + 1]; mkc[x][y] = mkc[x - 1][y]; mk = mkr[x][y] ^ mkc[x][y]; if (mk ^ mp[x][y]) { cnt++; mkr[x][y] ^= 1; mkc[x][y] ^= 1; } } void changeLeftBottom(int x, int y) { bool mk; mkr[x][y] = mkr[x][y - 1]; mkc[x][y] = mkc[x + 1][y]; mk = mkr[x][y] ^ mkc[x][y]; if (mk ^ mp[x][y]) { cnt++; mkr[x][y] ^= 1; mkc[x][y] ^= 1; } } int main() { while (~scanf("%d", &n)) { memset(mp, 0, sizeof(mp)); memset(mkr, 0, sizeof(mkr)); memset(mkc, 0, sizeof(mkc)); for (i = 1; i <= n; i++) { scanf("%s", &c); for (j = 0; j < n; j++) { mp[i][j + 1] = c[j] - '0'; } } cnt = 0; for (i = n; i > (n + 1) / 2; i--) { changeLeftBottom(i, n + 1 - i); for (j = n + 2 - i; j < i; j++) { changeLeftBottom(i, j); } for (k = i - 1; k > n + 1 - i; k--) { changeLeftBottom(k, n + 1 - i); } } for (i = 1; i <= n / 2; i++) { changeRightTop(i, n + 1 - i); for (j = n - i; j > i; j--) { changeRightTop(i, j); } for (k = i + 1; k < n + 1 - i; k++) { changeRightTop(k, n + 1 - i); } } for (i = 1; i <= n; i++) { changeDiagonalLine(i, i); } cout << cnt << endl; } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int N = 2500 + 5; int n; double sd[N][N][4]; long long px[N], py[N]; double dis(int i, int j) { return sqrt((px[i] - px[j]) * (px[i] - px[j]) + (py[i] - py[j]) * (py[i] - py[j])); } int main() { scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%I64d%I64d", &px[i], &py[i]); } for (int i = 2; i <= n; ++i) { for (int j = 0; j < n; ++j) { sd[j][i][0] = dis(j, (j + 1) % n) + max(sd[(j + 1) % n][i - 1][0], sd[(j + 1) % n][i - 1][1]); sd[j][i][1] = dis(j, (j + i - 1) % n) + max(sd[(j + i - 1) % n][i - 1][2], sd[(j + i - 1) % n][i - 1][3]); sd[j][i][2] = dis(j, (j - 1 + n) % n) + max(sd[(j - 1 + n) % n][i - 1][2], sd[(j - 1 + n) % n][i - 1][3]); sd[j][i][3] = dis(j, (j - i + 1 + n) % n) + max(sd[(j - i + 1 + n) % n][i - 1][0], sd[(j - i + 1 + n) % n][i - 1][1]); } } double ans = 0; for (int i = 0; i < n; ++i) { ans = max(ans, sd[i][n][0]); ans = max(ans, sd[i][n][1]); ans = max(ans, sd[i][n][2]); ans = max(ans, sd[i][n][3]); } printf("%.10f\n", ans); }
5
#include <bits/stdc++.h> using namespace std; int n, m, x, y, Q, b[2000005], dp[2000005], tmp[2000005], fac[2000005], inv[2000005], ans[56][56]; char s[2000005]; inline int ksm(int x, int y) { int ans1 = 1; while (y) { if (y & 1) ans1 = 1ll * ans1 * x % 1000000007; y >>= 1; x = 1ll * x * x % 1000000007; } return ans1; } inline int C(int n, int m) { return 1ll * fac[n] * inv[m] % 1000000007 * inv[n - m] % 1000000007; } inline void solve(int x, int y) { if (x == y) { if (b[x] > m) return; for (int i = 0; i <= m; i++) tmp[i] = dp[i]; if (b[y]) for (int i = b[y]; i <= m; i++) tmp[i] = (tmp[i] - tmp[i - b[y]] + 1000000007) % 1000000007; ans[x][y] = tmp[m]; return; } if (b[x] + b[y] > m) return; for (int i = 0; i <= m; i++) tmp[i] = dp[i]; if (b[x]) for (int i = b[x]; i <= m; i++) tmp[i] = (tmp[i] - tmp[i - b[x]] + 1000000007) % 1000000007; if (b[y]) for (int i = b[y]; i <= m; i++) tmp[i] = (tmp[i] - tmp[i - b[y]] + 1000000007) % 1000000007; ans[x][y] = tmp[m]; } int main() { scanf("%s", s + 1); fac[0] = 1; n = strlen(s + 1); m = n / 2; for (int i = 1; i < 2000005; i++) fac[i] = 1ll * fac[i - 1] * i % 1000000007; inv[2000005 - 1] = ksm(fac[2000005 - 1], 1000000007 - 2); dp[0] = 1; for (int i = 2000005 - 2; ~i; i--) inv[i] = 1ll * inv[i + 1] * (i + 1) % 1000000007; for (int i = 1; i <= n; i++) { if (s[i] >= 'A' && s[i] <= 'Z') b[s[i] - 'A']++; else b[s[i] - 'a' + 26]++; } for (int i = 0; i < 52; i++) if (b[i]) for (int j = m; j >= b[i]; j--) dp[j] = (dp[j] + dp[j - b[i]]) % 1000000007; for (int i = 0; i < 52; i++) for (int j = i; j < 52; j++) solve(i, j); for (int i = 0; i < 52; i++) for (int j = i; j < 52; j++) solve(i, j); int fm = 1ll * fac[m] * fac[m] % 1000000007; for (int i = 0; i < 52; i++) fm = 1ll * fm * inv[b[i]] % 1000000007; scanf("%d", &Q); while (Q--) { scanf("%d%d", &x, &y); int fr, sd; if (s[x] >= 'A' && s[x] <= 'Z') fr = s[x] - 'A'; else fr = s[x] - 'a' + 26; if (s[y] >= 'A' && s[y] <= 'Z') sd = s[y] - 'A'; else sd = s[y] - 'a' + 26; if (fr > sd) swap(fr, sd); printf("%d\n", 1ll * ans[fr][sd] * fm % 1000000007 * 2 % 1000000007); } return 0; }
4
#include <bits/stdc++.h> using namespace std; long long mod = 1000003; vector<vector<int> > p; vector<int> pa; vector<int> lv; vector<vector<int> > adj; void dfs(int u, int parent, int l) { lv[u] = l; for (int j = 0; j < (int)adj[u].size(); j++) { int v = adj[u][j]; if (v != parent) { dfs(v, u, l + 1); } } } int lca(int x, int y) { if (lv[x] < lv[y]) { swap(x, y); } int gap = lv[x] - lv[y]; for (int i = 20; i >= 0; i--) { if ((1 << i) & gap) { x = p[x][i + 1]; } } if (x == y) return x; for (int i = 20; i >= 0; i--) { if (p[x][i] != p[y][i]) { x = p[x][i]; y = p[y][i]; } } return p[x][1]; } int d(int x, int y) { return lv[x] + lv[y] - 2 * lv[lca(x, y)]; } int main() { int n; cin >> n; pa.assign(n + 10, -1); p.assign(n + 10, vector<int>(23, -1)); lv.assign(n + 10, -1); adj.assign(n + 10, vector<int>(0, 0)); for (int i = 1; i < n; i++) { int x; cin >> x; x--; adj[x].push_back(i); adj[i].push_back(x); pa[i] = x; } for (int i = 1; i <= 22; i++) { for (int j = 0; j < n; j++) { if (i == 1) { p[j][i] = pa[j]; } else { if (p[j][i - 1] != -1) p[j][i] = p[p[j][i - 1]][i - 1]; } } } dfs(0, -1, 0); int pu = 0; int pv = 1; int ans = 1; for (int i = 1; i < n; i++) { if (i == 1) { cout << 1 << endl; } else { if (ans < d(pu, i)) { ans = d(pu, i); pv = i; } if (ans < d(i, pv)) { ans = d(i, pv); pu = i; } cout << ans << endl; } } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 5; int a[maxn]; int main() { int T; scanf("%d", &T); while (T--) { int n, k; scanf("%d%d", &n, &k); for (int i = 0; i < n; i++) scanf("%d", &a[i]); sort(a, a + n); int minn = 1e9 + 1; int p1, p2; for (int i = k; i < n; i++) { if (minn > a[i] - a[i - k]) { minn = a[i] - a[i - k]; p1 = a[i - k]; p2 = a[i]; } } printf("%d\n", (p1 + p2) / 2); } return 0; }
3
#include <bits/stdc++.h> template <class T> inline void chkmax(T &x, const T &y) { if (x < y) x = y; } template <class T> inline void chkmin(T &x, const T &y) { if (x > y) x = y; } template <class T> inline void read(T &x) { char c; int f = 1; x = 0; while (((c = getchar()) < '0' || c > '9') && c != '-') ; if (c == '-') f = -1; else x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0'; x *= f; } int outn; char out[(int)2e7]; template <class T> inline void write(T x) { if (x < 0) out[outn++] = '-', x = -x; if (x) { static int tmpn; static char tmp[20]; tmpn = 0; while (x) tmp[tmpn++] = x % 10 + '0', x /= 10; while (tmpn) out[outn++] = tmp[--tmpn]; } else out[outn++] = '0'; } const int N = 1e5; const double lim = 1.8 * CLOCKS_PER_SEC; const double eps = 1e-8; int n, k; struct pot { double x, y; pot() { x = y = 0; } pot(double _x, double _y) : x(_x), y(_y) {} inline friend pot operator+(const pot &p, const pot &q) { return pot(p.x + q.x, p.y + q.y); } inline friend pot operator-(const pot &p, const pot &q) { return pot(p.x - q.x, p.y - q.y); } inline friend double operator*(const pot &p, const pot &q) { return p.x * q.x + p.y * q.y; } inline friend double operator^(const pot &p, const pot &q) { return p.x * q.y - p.y * q.x; } inline friend pot operator*(const pot &p, const double &k) { return pot(p.x * k, p.y * k); } inline friend pot operator/(const pot &p, const double &k) { return pot(p.x / k, p.y / k); } }; struct line { pot p, v; line() {} line(pot _p, pot _v) : p(_p), v(_v) {} line(int a, int b, int c) { if (!b) p = pot((double)-c / a, 0), v = pot(0, 1); else if (!a) p = pot(0, (double)-c / b), v = pot(1, 0); else p = pot(0, (double)-c / b), v = pot(1, (double)-a / b); } } l[N + 9]; std::vector<int> res; std::vector<std::pair<int, int> > ans; inline int getrand(int lim) { return 1ll * rand() * rand() % lim; } inline bool parallel(const line &a, const line &b) { return fabs(a.v ^ b.v) < eps; } inline pot intersection(const line &a, const line &b) { pot w = b.p - a.p; double k = (w ^ a.v) / (a.v ^ b.v); return b.p + b.v * k; } inline bool online(const pot &x, const line &l) { return fabs((l.p - x) ^ l.v) < eps; } void dfs(int k, std::vector<int> res) { if (!(int)(res).size()) { puts("YES"); write((int)(ans).size()), out[outn++] = '\n'; for (int i = 0; i < (int)(ans).size(); ++i) { write(ans[i].first), out[outn++] = ' '; write(ans[i].second), out[outn++] = '\n'; } printf("%s", out); exit(0); } if (!k) return; int p = getrand((int)(res).size()), q = getrand((int)(res).size()); if (p == q) { ans.push_back(std::make_pair(res[p], -1)); res.erase(find(res.begin(), res.end(), res[p])); dfs(k - 1, res); ans.pop_back(); } else { if (parallel(l[res[p]], l[res[q]])) return; ans.push_back(std::make_pair(res[p], res[q])); pot x = intersection(l[res[p]], l[res[q]]); std::vector<int> nex; for (int i = 0; i < (int)(res).size(); ++i) if (!online(x, l[res[i]])) nex.push_back(res[i]); dfs(k - 1, nex); ans.pop_back(); } } int main() { read(n), read(k); for (int i = 1; i <= n; ++i) { int a, b, c; read(a), read(b), read(c); l[i] = line(a, b, c); } for (int i = 1; i <= n; ++i) res.push_back(i); while (clock() < lim) dfs(k, res); puts("NO"); return 0; }
4
#include<bits/stdc++.h> using namespace std; const int maxn=1e5+5; typedef long long ll; ll N[maxn]; int main() { int n;cin>>n;int cnt=0;ll sum=0;ll minn=1e9+1; for(int i=0;i<n;i++) { scanf("%lld",&N[i]);sum+=abs(N[i]); if(N[i]<0) cnt++; minn=min(minn,abs(N[i])); } if(cnt%2==0) cout<<sum<<endl; else cout<<sum-2*minn<<endl; }
0
#include<bits/stdc++.h> using namespace std; int n,ct[1010101],nw[1010101],sum,al; string s; vector<pair<int,int> >v; bool cmp(pair<int,int>a,pair<int,int>b) { return min(a.second,a.first+b.second)>min(b.second,b.first+a.second); } int main() { cin>>n; for(int i=0;i<n;i++) { cin>>s; int a=0; for(int j=0;j<s.size();j++) { if(s[j]=='(') { a++; } else { a--; } nw[i]=min(nw[i],a); } ct[i]=a; sum+=a; v.push_back(make_pair(ct[i],nw[i])); } if(sum!=0) { cout<<"No"<<endl; return 0; } sort(v.begin(),v.end(),cmp); int nn=0; for(int i=0;i<n;i++) { if(nn<abs(v[i].second)) { cout<<"No"<<endl; return 0; } nn+=v[i].first; } cout<<"Yes"<<endl; return 0; }
0
#include <bits/stdc++.h> int compare(const void *a, const void *b) { int ia = *(int *)a; int ib = *(int *)b; return ib - ia; } int main() { static int aa[100000]; int m, n, x, i, ans; scanf("%d", &m); x = 100001; while (m--) { int q; scanf("%d", &q); if (x > q) x = q; } scanf("%d", &n); for (i = 0; i < n; i++) scanf("%d", &aa[i]); qsort(aa, n, sizeof *aa, compare); ans = 0; for (i = 0; i < n; i++) if (i % (x + 2) < x) ans += aa[i]; printf("%d\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { string str; cin >> str; map<char, int> a; char ans = 'a'; for (int i = 0; i < str.size(); i++) { ans = max(ans, str[i]); a[str[i]]++; } for (int j = 0; j < a[ans]; j++) { cout << ans; } cout << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MaxN = 4e5 + 5; const int md = 1e9 + 7; const int Inf = 1e9; int dp[500][500]; int New_dp[500][500]; int Mx[500][500]; int main() { ios_base::sync_with_stdio(0); cin.tie(); cout.tie(); int N, M; cin >> N >> M; for (int i = 0; i < N; i++) { string s; cin >> s; for (int j = 0; j < M; j++) { Mx[i][j] = s[j] - 'a'; } } if (Mx[0][0] != Mx[N - 1][M - 1]) { cout << 0; return 0; } dp[0][N - 1] = 1; int t; if ((N + M) & 1) t = ((N + M - 1) >> 1) - 1; else t = ((N + M - 1) >> 1); for (int i = 0; i < t; i++) { for (int x = 0; x < N; x++) { for (int a = 0; a < N; a++) { int y = i - x; int b = M - 1 - (i - (N - 1 - a)); if (y < 0 || y >= M || b < 0 || b >= M) continue; if (x + 1 < N) { if (a - 1 >= 0 && x + 1 <= a - 1 && y <= b) { if (Mx[x + 1][y] == Mx[a - 1][b]) { New_dp[x + 1][a - 1] += dp[x][a]; if (New_dp[x + 1][a - 1] >= md) New_dp[x + 1][a - 1] -= md; } } if (b - 1 >= 0 && x + 1 <= a && y <= b - 1) { if (Mx[x + 1][y] == Mx[a][b - 1]) { New_dp[x + 1][a] += dp[x][a]; if (New_dp[x + 1][a] >= md) New_dp[x + 1][a] -= md; } } } if (y + 1 < M) { if (a - 1 >= 0 && x <= a - 1 && y + 1 <= b) { if (Mx[x][y + 1] == Mx[a - 1][b]) { New_dp[x][a - 1] += dp[x][a]; if (New_dp[x][a - 1] >= md) New_dp[x][a - 1] -= md; } } if (b - 1 >= 0 && x <= a && y + 1 <= b - 1) { if (Mx[x][y + 1] == Mx[a][b - 1]) { New_dp[x][a] += dp[x][a]; if (New_dp[x][a] >= md) New_dp[x][a] -= md; } } } } } swap(dp, New_dp); memset(New_dp, 0, sizeof(New_dp)); } int Ans = 0; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { Ans += dp[i][j]; if (Ans >= md) Ans -= md; } } cout << Ans << '\n'; return 0; }
5
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void chkmax(T& x, U y) { if (x < y) x = y; } template <typename T, typename U> inline void chkmin(T& x, U y) { if (y < x) x = y; } const int MX = 1111111; int mod = 1000000007; long long ans[MX], fact[MX]; long long inv[MX], f[MX], g[MX]; void init() { int i; inv[1] = 1; for (i = 2; i < MX; i++) inv[i] = inv[mod % i] * (mod - mod / i) % mod; f[0] = g[0] = 1; for (i = 1; i < MX; i++) { f[i] = f[i - 1] * i % mod; g[i] = g[i - 1] * inv[i] % mod; } } inline long long nCm(int n, int m) { if (m < 0 or m > n) return 0; return f[n] * g[m] % mod * g[n - m] % mod; } int main() { int x, n, m, a, q; scanf("%d%d%d%d", &n, &m, &a, &q); int p = a; fact[0] = 1; for (int i = 1; i < MX; i++) { fact[i] = 1LL * fact[i - 1] * a % q; } for (x = 1;; x++) { if (p == 1) break; p = 1LL * p * a % q; } mod = x; init(); for (int i = n - 1; i >= 0; i--) ans[i] = nCm(m, n - 1 - i); ans[n - 1] = 1; for (int i = n - 2; i >= 0; i--) ans[i] = (ans[i + 1] + ans[i]) % mod; for (int i = 0; i < n; i++) { printf("%d%c", (int)fact[ans[i]], i < (n - 1) ? ' ' : '\n'); } return 0; }
6
#include <bits/stdc++.h> using namespace std; void Freopen() { freopen( "title" ".in", "r", stdin); freopen( "title" ".out", "w", stdout); } long long read() { long long g = 0, f = 1; char ch = getchar(); while (ch < '0' || '9' < ch) { if (ch == '-') f = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') { g = g * 10 + ch - '0'; ch = getchar(); } return g * f; } const int N = 1e5 + 5; int du[N], d[N], n, cnt[N]; long long all; long long check(int x) { long long res = n, nu = 1, ans = 0, dep = 1; while (res) { ans += min(res, nu) * dep; dep++; res -= min(res, nu); nu *= x; } return ans; } void solve(int x) { long long res = n, nu = 1, dep = 1, re = all; while (res) { for (int i = (n - res + 1); i <= (n - res + min(res, nu)); i++) d[i] = dep, cnt[d[i]]++, re -= dep; dep++; res -= min(res, nu); nu *= x; } int j = n; dep--; while (re) { ++dep; if (cnt[d[j]] == 1) j--; int t = min(re, dep - d[j]); cnt[d[j]]--; d[j] += t; cnt[d[j]]++; re -= t; j--; } } signed main() { n = read(), all = read(); if (all * 2 > 1ll * n * (n + 1)) { cout << "No\n"; return signed(); } if (all < n * 2 - 1) { cout << "No\n"; return signed(); } int l = 1, r = n - 1, re = 0; while (l <= r) { int mid = l + r >> 1; if (check(mid) <= all) re = mid, r = mid - 1; else l = mid + 1; } cout << "Yes\n"; solve(re); sort(d + 1, d + n + 1); int now = 1; for (int i = (2); i <= (n); i++) { while (d[now] != d[i] - 1 || du[now] == re) now++; cout << now << ' '; du[now]++; } return signed(); }
3
#include <bits/stdc++.h> using namespace std; int main() { int *a, *b; int n; cin >> n; a = new int[n]; b = new int[n]; for (int i = 0; i < n; ++i) { cin >> a[i]; b[i] = 0; } int count = 0; bool eq = false; int t = 0; b[0] = a[0]; for (int i = 0; i < n; ++i) { int st = i; do { ++i; } while (a[i] != a[i - 1] && i < n); if (i - 1 - st > 1) { int t = (i - st - 1) / 2; count = max(count, t); int t1 = st, t2 = i - 1; if (a[t1] == a[t2]) { b[(t1 + t2) / 2] = a[t1]; } while (t1 < t2) { b[t1++] = a[st]; b[t2--] = a[i - 1]; } --i; } else if (i - 1 - st > 0) { --i; b[i] = a[i]; b[i - 1] = a[i - 1]; } else { --i; b[i] = a[i]; } } cout << count << endl; for (int i = 0; i < n; ++i) { cout << b[i] << ' '; } return 0; }
1
//#define __USE_MINGW_ANSI_STDIO 0 #include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int> VI; typedef vector<VI> VVI; typedef vector<ll> VL; typedef vector<VL> VVL; typedef pair<int, int> PII; #define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i) #define REP(i, n) FOR(i, 0, n) #define ALL(x) x.begin(), x.end() #define MP make_pair #define PB push_back #define MOD 1000000007 #define INF (1LL<<30) #define LLINF (1LL<<60) #define PI 3.14159265359 #define EPS 1e-12 //#define int ll int a[15][15]; signed main(void) { while(true) { string s; cin >> s; if(s == "#") break; int y = 0, x = 0; REP(i, s.size()) { int tmp; if('1' <= s[i] && s[i] <= '9') tmp = s[i]-'0'; else if(s[i] == 'b') {tmp = 0; a[y][x] = 1; x++;} else {y++; x=0; continue;} while(tmp--) {a[y][x] = 0; x++;} //cout << i << " " << x << " " << y << endl; } int b, c, d, e; cin >> b >> c >> d >> e; a[b-1][c-1] = 0; a[d-1][e-1] = 1; /*REP(i, y+1) { REP(j, x) { cout << a[i][j] << " "; } cout << endl; }*/ string ans = ""; REP(i, y+1) { int cnt = 0; REP(j, x) { if(a[i][j]) { if(cnt != 0) ans += (char)('0' + cnt); ans += 'b'; cnt = 0; } else { cnt++; } } if(cnt != 0) ans += (char)('0' + cnt); if(i != y) ans += '/'; } cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long t, n, x, y, ans1, ans2; cin >> t; while (t--) { cin >> x >> y >> n; ans1 = (n - (n % x)) + y; ans2 = ans1 - x; if (ans1 <= n) cout << ans1 << endl; else cout << ans2 << endl; } }
1
#include<complex> #include<vector> #include<iostream> #include<stack> #include<cmath> #define sc second #define fr first #define REP(i,n) for(int i = 0; i < (int)(n); ++i) using namespace std; typedef double elem; typedef complex<elem> point, vec; typedef pair<point, point> line, hline, seg, pp; const double eps = 1.0e-6; const double pi = acos(-1.0); const double infty = 1e120; point base(0,0); // ”’l‰‰ŽZ inline elem sq(elem a){ return a*a; } inline elem cq(elem a){ return a*a*a; } // Šp“x•ÏŠ· elem rad(elem deg){ return (deg/180)*pi; } elem deg(elem rad){ return (rad*180)/pi; } // •‚“®¬”“_‚ÌŽô‚¢A‚È‚Ç bool eq(elem a, elem b){ return abs(a-b) < eps; } bool lt(elem a, elem b){ return !eq(a,b) && a < b; } bool leq(elem a, elem b){ return eq(a,b) || a < b; } bool gt(elem a, elem b){ return !eq(a,b) && a > b; } bool geq(elem a, elem b){ return eq(a,b) || a > b; } bool ltz(elem a){ return lt( a, 0 ); } bool gtz(elem a){ return gt( a, 0 ); } bool eqv(vec a, vec b){ return eq( abs(b-a),0); } bool is_zv(vec v){ return eq(abs(v),0); } elem emax(elem a, elem b){ return gt(a,b)?a:b; } elem emin(elem a, elem b){ return lt(a,b)?a:b; } // “_ƒIƒyƒŒ[ƒ^ bool far(point a, point b){ return gtz( abs(b-a) ); } bool near(point a, point b){ return leq( abs(b-a), 0 ); } elem dot(vec a, vec b){ return (a.real() * b.real() + a.imag() * b.imag() ); } elem cross(vec a, vec b){ return ( a.real() * b.imag() - a.imag() * b.real() ); } // a‚©‚çb‚܂ŎžŒv‰ñ‚è‚ÌŠp“xA“àŠpA“]‰ñ“] elem varg(vec a, vec b){ elem ret=arg(a)-arg(b); if(lt(ret,0))ret+=2*pi; if(gt(ret,2*pi))ret-=2*pi; if(eq(ret,2*pi))ret=0; return ret; } elem arg(vec a, vec b){ return acos( dot(a,b) / ( abs(a) * abs(b) ) ); } point rot(point p, elem theta){ return p * polar((elem)1.0, theta); } point rotdeg(point p, elem deg){ return p * polar((elem)1.0, rad(deg)); } point proj(line l, point p){ double t=dot(p-l.first,l.first-l.second)/abs(l.first-l.second); return l.first + t*(l.first-l.second); } point reflect(line l, point p){ return p+2.0*(proj(l,p)-p); } // “ñ“_ŠÔ‹——£A’¼ü‚Æ“_‚̍ŒZ‹——£Aü•ª‚Æ“_‚̍ŒZ‹——£ elem dist(point a, point b){ return abs(a-b); } elem dist_l(line l, point x){ return abs(cross(l.sc-l.fr,x-l.fr)) / abs(l.sc-l.fr); } elem dist_seg(seg s, point x) { if( ltz( dot(s.sc-s.fr,x-s.fr) ) ) return abs(x-s.fr); if( ltz( dot(s.fr-s.sc,x-s.sc) ) ) return abs(x-s.sc); return dist_l(s,x); } // ’PˆÊƒxƒNƒgƒ‹A–@üƒxƒNƒgƒ‹A’PˆÊ–@üƒxƒNƒgƒ‹ vec uvec(vec a){ return a / abs(a); } vec nmr(vec a){ return a * vec(0,-1); } vec nml(vec a){ return a * vec(0,1); } vec unmr(vec a){ return uvec( nmr(a) ); } vec unml(vec a){ return uvec( nml(a) ); } class Dog{ public: point p; elem v; int score; Dog():p(point(0,0)),v(0.0),score(0){} Dog(point p, elem v):p(p),v(v),score(0){} void move(point target, double t){ vec V = target - p; V = t * v * uvec(V); p = p + V; } }; class Frisbee{ public: point p; vec v; Frisbee():p(point(0,0)),v(vec(0,0)){} Frisbee(point p, vec v):p(p),v(v){} }; bool reachable(const Dog &dog, const Frisbee &fris, double &time) { if(near(dog.p,fris.p)){time=0;return true;} vec A=vec(dog.p-fris.p); double a = pow(abs(dog.v),2.0)-pow(abs(fris.v),2.0); double b = dot(A,fris.v); double c = -pow(abs(A),2.0); double D = pow(b,2.0)-a*c; if( eq(a, 0.0) ){time=infty;return false;} if( lt(D, 0.0) ){time=infty;return false;} double t = (-b+sqrt(D))/a; if( lt(t, 0.0) ){ return false; } time = t; return true; } int main(){ while(true){ int N; int M; //cout <<"START" << endl; cin >> N >> M; //cout << N << ' ' << M << endl; if( N == 0 && M == 0 ) break; vector<Dog> dogs; vector<Frisbee> fris; for(int i = 0; i < N; ++i){ elem x,y,v; cin >> x >> y >> v; dogs.push_back( Dog(point(x,y),v) ); } for(int i = 0; i < M; ++i){ elem x,y,vx,vy; cin >> x >> y >> vx >> vy; fris.push_back( Frisbee( point(x,y), vec(vx,vy) ) ); } for(int i = 0; i < M; ++i){ elem min_time = infty; int min_dog = -1; bool reach[16]; elem times[16]; for(int j = 0; j < N; ++j){ elem time; reach[j] = reachable( dogs[j], fris[i], time ); if( reach[j] ){ if( lt(time,min_time) ){ min_time = time; min_dog = j; } } times[j] = time; } dogs[min_dog].score++; for(int j = 0; j < N; ++j){ if( reach[j] ){ point T = ( times[j] * fris[i].v ) + fris[i].p; //cout << times[j] << ' ' << " DISTANCE : " << abs(T-dogs[j].p) << ' ' << dogs[j].p << ' ' << T << endl; dogs[j].move( T, min_time ); //cout << dogs[j].p << endl; } } } for(int i = 0; i < N; ++i){ if( i > 0 ) cout << ' '; cout << dogs[i].score; } cout << endl; //cout << "END" << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int n; int main() { scanf("%d", &n); if (n % 3 == 1) n--; else if (n % 3 == 2) n++; printf("%d %d\n", n / 36, (n % 36) / 3); return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, k, d, di[1000 + 10][1000 + 10]; int main() { scanf("%d%d%d", &n, &k, &d); for (int i = 0; i < n; ++i) { int l = 0; bool f = 0; for (int j = i; j; j /= k) { di[i][++l] = j % k; if (l > d) { f = 1; break; } } if (f) { puts("-1"); return 0; } } for (int i = d; i; --i) for (int j = 0; j < n; ++j) printf("%d%c", di[j][i] + 1, j == n - 1 ? '\n' : ' '); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int mod = (int)1e9 + 7; char str[1010]; int a[30]; int n; bool can(int x) { int y = 0; for (int i = 0; i < 26; i++) y += a[i] / x + (a[i] % x > 0); if (y <= n) return true; return false; } int main() { scanf("%s", str); for (int i = 0; str[i]; i++) a[str[i] - 'a']++; scanf("%d", &n); int l = 1, r = 1000; while (l <= r) { int mid = l + r >> 1; if (can(mid)) r = mid - 1; else l = mid + 1; } if (l == 1001) puts("-1"); else { int L = 0; printf("%d\n", l); for (int i = 0; i < 26; i++) { int len = a[i] / l + (a[i] % l > 0); for (int j = 0; j < len; j++) printf("%c", i + 'a'); L += len; } for (int j = L; L < n; L++) printf("a"); } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; cin >> t; while (t--) { long long n, k; cin >> n >> k; if (n % 2 == k % 2 && k <= floor(sqrt((double)n))) cout << "YES" << endl; else cout << "NO" << endl; } }
1
#include <bits/stdc++.h> using namespace std; int main() { int N; cin >> N; string hpb = "pphbhhphph"; int ichi = N % 10; cout << hpb.at(ichi) << "on" << endl; }
0
#include<iostream> #include<complex> #include<vector> using namespace std; typedef complex<long double> P; typedef vector<P> Polygon; long double cross(P a,P b){ return (conj(a)*b).imag(); } double area(const Polygon &p){ double a=0; for(int i=0;i<p.size();i++){ a+=cross(p[(i+1)%p.size()],p[i]); } return fabs(a)/2; } P intersection(P a1,P a2,P b1,P b2){ P a=a2-a1; P b=b2-b1; return a1+a*cross(b,b1-a1)/cross(b,a); } Polygon cut(P a,P b,const Polygon &p){ int n=p.size(); vector<double> c(n); for(int i=0;i<n;i++){ c[i]=cross(b-a,p[i]-a); } Polygon r; for(int i=0;i<n;i++){ if(c[i]>=0){ r.push_back(p[i]); } if(c[i]*c[(i+1)%n]<0){ r.push_back(intersection(a,b,p[i],p[(i+1)%n])); } } return r; } int main(){ for(int N;cin>>N,N;){ Polygon p; for(int i=0;i<N;i++){ double x,y; cin>>x>>y; p.emplace_back(x,y); } double wa=area(p); double l=1e-5,r=acos(0)*2+1e-5; Polygon lp=cut(P(0,0),polar(2.,l),p),rp=cut(P(0,0),polar(2.,r),p); double la,ra; la=area(lp); ra=area(rp); for(int i=0;i<1000;i++){ double m=(l+r)/2; auto mp=cut(P(0,0),polar(2.,m),p); double ma=area(mp); if(ma/wa==.5){ l=m; break; }else if((la/wa-.5)*(ma/wa-.5)>=0){ l=m; la=ma; }else{ r=m; ra=ma; } } cout.precision(15); auto ans=polar(2.,l); cout<<fixed<<ans.real()<<' '<<ans.imag()<<endl; } }
0
#include <bits/stdc++.h> using namespace std; const int N = 3000; long long memo[N][N]; int n; long long vs[N]; vector<long long> arr; long long oo = 1000000000000000LL; long long solve(int idx, int f) { if (idx == n) return 0; long long ret = memo[idx][f]; if (ret != -1) return ret; ret = oo; if (f + 1 < n) ret = min(ret, solve(idx, f + 1)); ret = min(ret, (solve(idx + 1, f) + abs(vs[idx] - arr[f]))); return memo[idx][f] = ret; } int main() { cin >> n; for (int i = 0; i < n; i++) { cin >> vs[i]; vs[i] -= i; arr.push_back(vs[i]); } sort(arr.begin(), arr.end()); for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) memo[i][j] = -1; cout << solve(0, 0) << '\n'; return 0; }
5
#include <bits/stdc++.h> using namespace std; int n, a[3][15]; vector<int> sol; int main() { char sir[15]; scanf("%s", sir + 1); n = strlen(sir + 1); int val, p = 1, i, max_val = 1; for (i = 1; i <= n; ++i) { if (sir[i] == ':') { ++p; continue; } if (sir[i] >= '0' && sir[i] <= '9') val = sir[i] - '0'; if (sir[i] >= 'A' && sir[i] <= 'Z') val = sir[i] - 'A' + 10; if (val > max_val) max_val = val; a[p][++a[p][0]] = val; } val = 0; int inf = 0, ora1 = -1, ora2 = -1; for (int b = max_val + 1;; ++b) { int ora = 0; int min = 0; val = 1; for (int i = a[1][0]; i >= 1; --i) { ora += a[1][i] * val; val *= b; } val = 1; for (int i = a[2][0]; i >= 1; --i) { min += a[2][i] * val; val *= b; } if (ora1 == ora && ora2 == min) { inf = 1; break; } if (ora >= 0 && ora <= 23 && min >= 0 && min <= 59) { sol.push_back(b); } else { if (ora > 23 || min > 59) break; } ora1 = ora; ora2 = min; } if (inf) { printf("-1\n"); return 0; } if (sol.size() == 0) printf("0\n"); else { for (i = 0; i < sol.size(); ++i) { printf("%d ", sol[i]); } } return 0; }
2
#include <iostream> #include <cstdio> using namespace std; int main(){ int fla=0,cou,sum,b; char a; while(1){ sum=0; cou=0; while(1){ scanf("%d%c",&b,&a); if(b>=10)sum+=10; else sum+=b; if(b==1)cou++; if(a=='\n')break; } if(sum==0)break; for(int i=0;i<cou;i++){ if(21-sum>=10)sum+=10; else break; } if(sum>21)cout << "0"<<endl; else cout << sum << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long N = 1e5 + 5; const long long NN = 1e9 + 5; long long n, a[105], cnt, l, r; long long ans = 1, x, y; void Input() { freopen("in.txt", "r", stdin); ; freopen("out.txt", "w", stdout); ; } int main() { ios_base::sync_with_stdio(0); cin >> n; l = 1, r = n; for (int i = 1; i <= n; i++) { cin >> a[i]; if (a[i]) cnt++; } if (cnt == 1) cout << 1; else if (cnt == 0) cout << 0; else { while (!a[l] || !a[r]) { if (!a[l]) l++; if (!a[r]) r--; } for (int i = l; i <= r; i++) { if (a[i] == 1) { cnt = 1; for (int j = i + 1; j <= r; j++) { if (a[j] == 1) break; cnt++; } ans *= cnt; } } cout << ans; } }
2
#include <iostream> #include <string> using namespace std; int main(){ string in[2]; while(1){ cin >> in[0] >> in[1]; if(in[0]=="0" && in[1]=="0") break; int hit=0, bro=0; for(int i=0;i<4;i++){ if(in[0][i]==in[1][i]) hit++; for(int j=0;j<4;j++){ if(in[0][i]==in[1][j]) bro++; } } bro-=hit; cout << hit << ' '<< bro << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long n, s, temp, sum, l, r, m, d; cin >> n >> s; l = 1LL; r = 1e18; while (l <= r) { m = (l + r) / 2; temp = m; sum = 0; while (temp) { d = temp % 10; sum += d; temp /= 10; } if (m - sum >= s) r = m - 1; else l = m + 1; } cout << max(0LL, n - l + 1); return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAXn = 1000 * 100, mod = 1000 * 1000 * 1000 + 7; int a[MAXn + 5], b[MAXn + 5]; int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 0; i < n / k; i++) scanf("%d", &a[i]); for (int i = 0; i < n / k; i++) scanf("%d", &b[i]); int num = 1; for (int i = 0; i < k - 1; i++) num *= 10; long long ans = 1; for (int i = 0; i < n / k; i++) { long long val, t; if (b[i] == 0) val = (num - 1) / a[i] + 1; else { val = (long long)((long long)(b[i] + 1) * num - 1) / a[i]; val -= ((long long)b[i] * num - 1) / a[i]; } t = ((long long)num * 10 - 1) / a[i] + 1; ans = ((long long)ans * (t - val)) % mod; } cout << ans << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; template <typename T> T pow(T a, T b, long long m) { T ans = 1; while (b > 0) { if (b % 2 == 1) ans = (ans * a) % m; b /= 2; a = (a * a) % m; } return ans % m; } template <typename T> T add(T a, T b, long long m) { a += b; while (a >= 0) a -= m; while (a < 0) a += m; return a; } template <typename T> T mul(T a, T b, long long m) { return (a * 1ll * b) % m; } long long n, m; const long long N = 4e5 + 5; long long a[N], vis[N] = {0}, nd[N] = {0}; vector<long long> v[N], cur[N]; long long check(long long mid) { memset(vis, 0, sizeof(vis)); for (long long i = 1; i <= n; i++) nd[i] = a[i]; for (long long i = 1; i <= 2e5; i++) cur[i].clear(); for (long long i = mid; i >= 1; i--) { for (auto it : v[i]) { if (!vis[it]) { vis[it] = 1; cur[i].push_back(it); } } } long long have = 0; for (long long i = 1; i <= mid; i++) { have++; for (auto it : cur[i]) { long long p = min(nd[it], have); nd[it] -= p; have -= p; } } long long rem = 0; for (long long i = 1; i <= n; i++) { rem += nd[i]; } rem *= 2; return (rem <= have); } long long naruto(long long lo, long long hi) { long long mid = 0, ans = 0; while (lo <= hi) { mid = (lo + hi) / 2; if (check(mid)) { ans = mid; hi = mid - 1; } else lo = mid + 1; } return ans; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; cin >> n >> m; for (long long i = 1; i <= n; i++) cin >> a[i]; for (long long i = 1; i <= m; i++) { long long x, y; cin >> x >> y; v[x].push_back(y); } long long ans = naruto(1, 4000); cout << ans << endl; }
6
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool cmax(T &x, T y) { return x < y ? x = y, 1 : 0; } template <typename T> inline bool cmin(T &x, T y) { return y < x ? x = y, 1 : 0; } const int _ = 2e5 + 55, __ = 5e5 + 55; int N; long long a[_], b[_]; char ans[__]; int n, cnt; bool check() { for (int i(1), I(N); i <= I; ++i) if (b[i] <= 0) return 0; return 1; } bool up() { for (int i(1), I(N - 1); i <= I; ++i) if (b[i + 1] <= b[i]) return 0; return 1; } bool down() { for (int i(1), I(N - 1); i <= I; ++i) if (b[i] <= b[i + 1]) return 0; return 1; } int main() { ios::sync_with_stdio(0), cin.tie(0); cin >> N; for (int i(1), I(N); i <= I; ++i) cin >> a[i]; for (int i(1), I(N); i <= I; ++i) cin >> b[i]; if (N == 1) return printf(a[1] == b[1] ? "SMALL\n0\n" : "IMPOSSIBLE\n"), 0; if (N == 2) { long long x(b[1]), y(b[2]), s(0); while (x && y) { if (y == a[1]) swap(x, y); if (x == a[1] && y >= a[2] && (y - a[2]) % x == 0) { s += (y - a[2]) / x, y = a[2]; break; } if (y == a[2]) swap(x, y); if (x == a[2] && y >= a[1] && (y - a[1]) % x == 0) { s += (y - a[1]) / x, y = a[1], swap(x, y); break; } if (x > y) swap(x, y); s += y / x, y %= x; } if (x != a[1] || y != a[2]) return printf("IMPOSSIBLE\n"), 0; else if (s > 2e5) return printf("BIG\n%lld\n", s), 0; } while (check()) { int flg = 1; for (int i(1), I(N); i <= I; ++i) if (a[i] != b[i]) { flg = 0; break; } if (!flg) { flg = 2; for (int i(1), I(N); i <= I; ++i) if (a[i] != b[N - i + 1]) { flg = 0; break; } } if (flg) { if (flg == 2 && cnt <= 2e5) ans[++n] = 'R'; if (cnt <= 2e5) { printf("SMALL\n%d\n", n); for (int i(n), I(1); i >= I; --i) putchar(ans[i]); putchar('\n'); } else printf("BIG\n%d\n", cnt); return 0; } else if (up()) { ++cnt; if (cnt <= 2e5) ans[++n] = 'P'; for (int i(N), I(2); i >= I; --i) b[i] -= b[i - 1]; } else if (down()) { ++cnt; if (cnt <= 2e5) ans[++n] = 'R', ans[++n] = 'P'; reverse(b + 1, b + N + 1); for (int i(N), I(2); i >= I; --i) b[i] -= b[i - 1]; } else break; } printf("IMPOSSIBLE\n"); return 0; }
6
#include<algorithm> #include<iostream> #include<string> #include<cstdlib> #include<cstring> #include<cmath> using namespace std; pair<char,int> fn(string s,int p){ int i,j,k,l; int ln=s.length(); for(i=0;i<ln;){ if('A'<=s[i]&&s[i]<='Z'){ if(p){ p--; i++; }else{ return make_pair(s[i],0); } }else{ for(j=i;'0'<=s[j]&&s[j]<='9';j++); if(s[j]=='('){ l=1; for(k=j+1;l;k++){ if(0){ }else if(s[k]=='('){ l++; }else if(s[k]==')'){ l--; } } string t=s.substr(j+1,k-j-2); l=atoi(s.substr(i,j-i).c_str()); i=k; for(k=0;k<l;k++){ pair<char,int> r=fn(t,p); if(r.first!='\0') return make_pair(r.first,0); p=r.second; } }else{ if(p<atoi(s.substr(i,j-i).c_str())){ return make_pair(s[j],0); }else{ p-=atoi(s.substr(i,j-i).c_str()); i=j+1; } } } } return make_pair('\0',p); } int main(){ int p; string s; while(cin>>s>>p&&(s!="0"||p!=0)){ pair<char,int> r=fn(s,p); cout<<(r.first=='\0'?'0':r.first)<<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1e6 + 5; const long long INF = 1e9 + 5; const long long mod = 1e9 + 7; const double pi = 3.1415926536; int dx[] = {1, -1, 0, 0, 1, 1, -1, -1}; int dy[] = {0, 0, 1, -1, 1, -1, -1, 1}; bool visr[105], visc[105]; int main() { ios::sync_with_stdio(0); cin.tie(0); ios_base::sync_with_stdio(0); ; int n; cin >> n; char arr[n][n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) cin >> arr[i][j]; } vector<pair<int, int> > ans, ans2; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (arr[i][j] == '.' && !visr[i]) { visr[i] = 1; ans.push_back({i + 1, j + 1}); break; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (arr[i][j] == '.' && !visc[j]) { visc[j] = 1; ans2.push_back({i + 1, j + 1}); } } } int cnt1 = 0, cnt2 = 0; for (int i = 0; i < n; i++) { if (visr[i]) cnt1++; if (visc[i]) cnt2++; } if (cnt1 != n && cnt2 != n) return cout << "-1\n", 0; else if (cnt1 == n) for (int i = 0; i < ans.size(); i++) cout << ans[i].first << " " << ans[i].second << endl; else if (cnt2 == n) for (int i = 0; i < ans2.size(); i++) cout << ans2[i].first << " " << ans2[i].second << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 3010; long long ans; int x[N], y[N], pre[N], nxt[N]; struct node { int x, y; bool operator<(const node &z) const { return y == z.y ? x < z.x : y < z.y; } } a[N]; int dfs(int x, int y) { if (y == 1 || !x) return x; return dfs(pre[x], y - 1); } int now, r, c, n, k, tot, f[N]; void del(int x) { nxt[pre[x]] = nxt[x]; pre[nxt[x]] = pre[x]; now -= a[f[pre[x]]].y * (a[x].y - a[pre[x]].y); now += a[f[pre[x]]].y * (a[nxt[x]].y - a[pre[x]].y); now -= a[f[x]].y * (a[nxt[x]].y - a[x].y); for (int i = 1, p = nxt[x]; i < k && p <= tot; i++, p = nxt[p]) { now -= a[f[p]].y * (a[nxt[p]].y - a[p].y); f[p] = pre[f[p]]; now += a[f[p]].y * (a[nxt[p]].y - a[p].y); } } vector<int> v[N]; int main() { scanf("%d%d%d%d", &r, &c, &n, &k); for (int i = 1; i <= n; i++) scanf("%d%d", &x[i], &y[i]); for (int i = 1; i <= r; i++) { now = tot = 0; for (int j = 1; j <= n; j++) if (x[j] >= i) a[++tot] = (node){x[j], y[j]}; sort(a + 1, a + 1 + tot); a[tot + 1].y = c + 1; for (int j = i; j <= r; j++) v[j].clear(); for (int j = 1; j <= tot; j++) { pre[j] = j - 1, nxt[j] = j + 1, f[j] = dfs(j, k); now += a[f[j]].y * (a[j + 1].y - a[j].y); } for (int j = 1; j <= tot; j++) v[a[j].x].push_back(j); ans += now; for (int j = r; j > i; j--) { for (int k = 0; k < v[j].size(); k++) del(v[j][k]); ans += now; } } printf("%lld\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; namespace IO { const int buffer_size = 1e5 + 5; char buf[buffer_size], *S, *T; bool flag_EOF; inline char read_char() { if (S == T) T = (S = buf) + fread(buf, 1, buffer_size, stdin); return S != T ? *(S++) : EOF; } inline int read_int() { int flag = 1; char c = read_char(); while (c < '0' || c > '9') { if (c == EOF) { flag_EOF = true; return 0; } flag = (c == '-' ? -1 : 1); c = read_char(); } int x = 0; while (c >= '0' && c <= '9') { x = x * 10 + (c ^ 48); c = read_char(); } return x * flag; } char st[13]; int _top; void Write(int x) { if (!x) { putchar('0'); return; } if (x < 0) { putchar('-'); x = -x; } while (x) { st[++_top] = x % 10 + '0'; x /= 10; } while (_top > 0) putchar(st[_top--]); } } // namespace IO const int P = 1e9 + 7; inline void add(int &a, int b) { a = a + b - (a + b >= P ? P : 0); } inline void sub(int &a, int b) { a = a - b + (a - b < 0 ? P : 0); } inline void mul(int &a, int b) { a = 1ll * a * b % P; } inline int get_sum(int a, int b) { return a + b - (a + b >= P ? P : 0); } inline int get_dif(int a, int b) { return a - b + (a - b < 0 ? P : 0); } inline int get_pro(int a, int b) { return 1ll * a * b % P; } int A[32], B[32], C[32]; int dp[32][2][2][2], sum[32][2][2][2]; int solve(int x, int y, int k) { if (x < 0 || y < 0 || k < 0) return 0; for (int i = 0; i <= 30; ++i) { A[i] = x & 1, x >>= 1; B[i] = y & 1, y >>= 1; C[i] = k & 1, k >>= 1; } reverse(A, A + 31); reverse(B, B + 31); reverse(C, C + 31); memset(dp, 0, sizeof(dp)); memset(sum, 0, sizeof(sum)); dp[0][1][1][1] = 1; sum[0][1][1][1] = 0; for (int i = 0; i <= 30; ++i) for (int a = 0; a <= 1; ++a) for (int b = 0; b <= 1; ++b) for (int c = 0; c <= 1; ++c) for (int x = 0; x <= 1; ++x) for (int y = 0; y <= 1; ++y) { int z = x ^ y; if (a == 1 && x > A[i]) continue; if (b == 1 && y > B[i]) continue; if (c == 1 && z > C[i]) continue; add(dp[i + 1][a & (A[i] == x)][b & (B[i] == y)][c & (C[i] == z)], dp[i][a][b][c]); add(sum[i + 1][a & (A[i] == x)][b & (B[i] == y)][c & (C[i] == z)], sum[i][a][b][c]); add(sum[i + 1][a & (A[i] == x)][b & (B[i] == y)][c & (C[i] == z)], get_pro(z << (30 - i), dp[i][a][b][c])); } int res = 0; for (int a = 0; a <= 1; ++a) for (int b = 0; b <= 1; ++b) for (int c = 0; c <= 1; ++c) add(res, get_sum(dp[31][a][b][c], sum[31][a][b][c])); return res; } int main() { int q = IO::read_int(); while (q--) { int X1 = IO::read_int() - 1, Y1 = IO::read_int() - 1, X2 = IO::read_int() - 1, Y2 = IO::read_int() - 1, k = IO::read_int() - 1; int ans = get_sum(solve(X1 - 1, Y1 - 1, k), solve(X2, Y2, k)); sub(ans, get_sum(solve(X1 - 1, Y2, k), solve(X2, Y1 - 1, k))); printf("%d\n", ans); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<int> d(n); vector<int> s(n); for (int i = 0; i < n; ++i) cin >> d[i] >> s[i]; int m = 0; for (int i = 0; i < n; ++i) m += d[i]; m /= 2; queue<int> Q; vector<pair<int, int> > answer; for (int i = 0; i < n; ++i) if (d[i] == 1) Q.push(i); while (!Q.empty()) { int v = Q.front(); Q.pop(); if (d[v] != 1) continue; d[v]--; int nextV = s[v]; s[nextV] ^= v; answer.push_back(make_pair(v, nextV)); d[nextV]--; if (d[nextV] == 1) { Q.push(nextV); } } cout << m << "\n"; for (int i = 0; i < answer.size(); ++i) printf("%ld %ld", answer[i].first, answer[i].second), printf("\n"); return 0; }
1
#include <bits/stdc++.h> using namespace std; template <typename _Tp> bool Min(_Tp &a, const _Tp &b) { return b < a ? a = b, true : false; } int main(int argc, char **argv) { int n; while (1 == scanf("%d", &n)) { map<int, int> aaa, bbb; for (int i = 0; i < n; ++i) { int a, b; scanf("%d%d", &a, &b); ++aaa[a]; if (a == b) continue; ++bbb[b]; } int least = (n + 1) / 2, ans = 0x3FFFFFFF; for (map<int, int>::iterator it = aaa.begin(); it != aaa.end(); ++it) { int a = it->second, b = bbb[it->first]; if (a + b >= least) { Min(ans, max(least - a, 0)); } } for (map<int, int>::iterator it = bbb.begin(); it != bbb.end(); ++it) { int b = it->second; if (b >= least) { Min(ans, least); } } if (ans != 0x3FFFFFFF) printf("%d\n", ans); else puts("-1"); } return 0; }
4
#include <bits/stdc++.h> using namespace std; struct bear { int a, l, r; } t[1000005]; int n, top, s[1000005]; bool cmp(bear x, bear y) { return x.a > y.a; } int main() { scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &t[i].a); top = 0; for (int i = 1; i <= n; i++) { if (top == 0 || t[i].a > t[s[top]].a) { t[i].l = i; s[++top] = i; } else { while (top > 0 && t[i].a <= t[s[top]].a) top--; t[i].l = s[top] + 1; s[++top] = i; } } top = 0; for (int i = n; i >= 1; i--) { if (top == 0 || t[i].a > t[s[top]].a) { t[i].r = i; s[++top] = i; } else { while (top > 0 && t[i].a <= t[s[top]].a) top--; t[i].r = s[top] - 1; s[++top] = i; } } sort(t + 1, t + n + 1, cmp); int now = 0; for (int i = 1; i <= n; i++) { if (t[i].l == n + 1) t[i].l = 1; if (t[i].r == -1) t[i].r = n; int dist = t[i].r - t[i].l + 1; if (dist > now) { for (int j = now + 1; j <= dist; j++) printf("%d ", t[i].a); now = dist; } } return 0; }
2
#include <bits/stdc++.h> using namespace std; struct data { long x; long y; }; long a[200005]; struct data b[200005]; bool cmp(long x, long y) { return x < y; } int main() { int t; cin >> t; b[0].x = 0; b[0].y = 0; while (t--) { long n, p, k; cin >> n >> p >> k; for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 1; i <= n; i++) { b[i].x = 0; b[i].y = 0; } sort(a + 1, a + n + 1, cmp); long ans = 0; for (int i = 1; i < k; i++) { if (b[i - 1].x + a[i] <= p) { b[i].x = b[i - 1].x + a[i]; b[i].y = b[i - 1].y + 1; ans = max(b[i].y, ans); } } for (int i = k; i <= n; i++) { int ans1 = 0; if (b[i - k].x + a[i] <= p) { b[i].x = b[i - k].x + a[i]; b[i].y = b[i - k].y + k; ans = max(b[i].y, ans); } else { if (b[i - 1].x + a[i] <= p) { b[i].x = b[i - 1].x + a[i]; b[i].y = b[i - 1].y + 1; ans = max(b[i].y, ans); } } } cout << ans << endl; } }
2
#include <bits/stdc++.h> using namespace std; struct bla { int comeca; int fim; bla() { comeca = 0; fim = 0; } }; vector<bla> v; bool mysort(bla a, bla b) { if (a.comeca != b.comeca) { return a.comeca < b.comeca; } return a.fim < b.fim; } int main() { bool pode; int n, s, e, m; scanf("%d", &n); for (int i = 0; i < n; ++i) { bla w = *(new bla()); v.push_back(w); } for (int i = 0; i < n; ++i) { scanf("%d", &m); v[i].comeca = m; } for (int i = 1; i < n; ++i) { v[i - 1].fim = v[i].comeca; if (v[i - 1].fim < v[i - 1].comeca) { v[i - 1].fim = v[i - 1].comeca; v[i - 1].comeca = v[i].comeca; } } v.pop_back(); sort(v.begin(), v.end(), mysort); for (int i = 1; i < n; ++i) { } pode = false; for (int i = 0; i < n - 1; ++i) { for (int j = i + 1; j < n; ++j) { if (v[j].comeca < v[i].fim) { if (v[j].comeca > v[i].comeca) { if (v[j].fim > v[i].fim) { pode = true; goto final; } } } else { break; } } } final: if (pode) printf("yes\n"); else printf("no\n"); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 2005; const long long inf = 1e18; int n, m, s, t, a[N], x, y, l; vector<pair<int, int> > e[N]; struct nod { long long f; int x; nod(long long u, int v) { f = u; x = v; } bool operator<(const nod &b) const { return f > b.f; } }; priority_queue<nod> A; struct dis { long long f[N]; int d[N], ds, di[N]; void work(int s) { for (int i = 1, _e = n; i <= _e; ++i) f[i] = inf; f[s] = 0; A.push(nod(0, s)); for (; !A.empty();) { nod o = A.top(); A.pop(); if (o.f != f[o.x]) continue; d[++ds] = o.x; for (pair<int, int> i : e[o.x]) if (f[i.first] > o.f + i.second) { f[i.first] = o.f + i.second; A.push(nod(f[i.first], i.first)); } } ds = 1; di[d[1]] = 1; for (int i = 2, _e = n; i <= _e; ++i) di[d[i]] = f[d[i]] == f[d[i - 1]] ? ds : ++ds; } } g[2]; bool bz[N]; struct dp { long long s[N], f[N]; int gs[N], O, p; void init(int o, int x) { memset(bz, 0, sizeof bz); for (int i = 1; i <= n && g[o].di[g[o].d[i]] <= x; ++i) bz[g[o].d[i]] = 1; int j = n, cnt = 0; long long su = 0; for (int i = g[!o].ds, _e = 0; i >= _e; --i) { for (; j && g[!o].di[g[!o].d[j]] >= i; --j) if (!bz[g[!o].d[j]]) ++cnt, su += a[g[!o].d[j]]; s[i] = su; gs[i] = cnt; } O = o; p = g[!o].ds; f[p + 1] = O ? -inf : inf; } void put(int x, long long v) { f[x] = O ? max(f[x + 1], v - s[x + 1]) : min(f[x + 1], v + s[x + 1]); } long long get(int x) { for (; gs[x + 1] > gs[p + 1];) --p; return f[p + 1] + (O ? s[x + 1] : -s[x + 1]); } } h[2][N]; long long f[N][N][2], ans; int main() { ios::sync_with_stdio(false); cin >> n >> m >> s >> t; for (int i = 1, _e = n; i <= _e; ++i) cin >> a[i]; for (int i = 1, _e = m; i <= _e; ++i) cin >> x >> y >> l, e[x].push_back(pair<int, int>(y, l)), e[y].push_back(pair<int, int>(x, l)); g[0].work(s); g[1].work(t); for (int o = 0, _e = 1; o <= _e; ++o) for (int i = 0, _e = g[o].ds; i <= _e; ++i) h[o][i].init(o, i); for (int i = g[0].ds, _e = 0; i >= _e; --i) for (int j = g[1].ds, _e = 0; j >= _e; --j) { f[i][j][0] = h[1][j].get(i); f[i][j][1] = h[0][i].get(j); if (h[0][i].gs[j + 1] == 0) f[i][j][0] = f[i][j][1] = 0; h[1][j].put(i, f[i][j][1]); h[0][i].put(j, f[i][j][0]); } ans = f[0][0][0]; printf(ans > 0 ? "Break a heart" : (ans ? "Cry" : "Flowers")); }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 3e3+10; const int mod = 1e9+7; int n; long long int dp[maxn], psum[maxn]; string s; int main(){ cin >> n; cin >> s; for (int i = 1; i <= n; i++){ psum[i] = 1; } for (int i = 2; i <= n; i++){ for (int j = 1; j <= i; j++){ if (s[i-2] == '>') dp[j] = (psum[n] - psum[j-1]) % mod; else dp[j] = psum[j-1]; } for (int j = 1; j <= n; j++) psum[j] = (psum[j-1] + dp[j]) % mod; } cout << ((psum[n] % mod) + mod) % mod << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; while (n--) { int l, r; scanf("%d %d", &l, &r); if (l >= (r + 1) / 2.0) printf("YES\n"); else printf("NO\n"); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int n; inline void read() { scanf("%d", &n); } int main() { read(); int a = 1, b = n; for (int i = 0; i < n; ++i) { if (i & 1) printf("%d%c", b--, (i + 1 == n) ? '\n' : ' '); else printf("%d%c", a++, (i + 1 == n) ? '\n' : ' '); } return 0; }
3
#include <bits/stdc++.h> using namespace std; using ll = long long; using db = double; using vi = vector<int>; ll n, a, m, d, l; ll t[200000]; int main() { scanf("%lld%lld%lld%lld", &n, &m, &a, &d); for (int i = 1; i <= m; ++i) scanf("%lld", &t[i]); t[++m] = n * a; sort(t + 1, t + m + 1); l = (d / a + 1) * a; ll p = -d; ll ans = 0; for (int i = 1; i <= m; ++i) { while (p + d < t[i]) { if ((p + d) / a + 1 > n) { p = t[i]; ans++; } else { ll tmp = ((p + d) / a + 1) * a; if (tmp >= t[i]) { p = t[i]; ans++; } else { p = tmp; ans++; ll step; if (n * a < tmp) step = (t[i] - tmp) / l; step = min((t[i] - tmp) / l, (n * a - tmp) / l); p = tmp + step * l; ans += step; } } } } cout << ans << endl; }
1
#include <iostream> #include <iomanip> #include <algorithm> #include <map> #include <vector> #include <string> using namespace std; typedef long long ll; const int MAXN = 100100; const int P = (1 << 18); const int MOD = 1e9 + 7; int N, M; vector <pair <int, int> > lr; int x[MAXN], y[MAXN]; ll seg[2*P+100]; map <int, int> mm; void upd (int cloc, ll cval) { cloc += P; while (cloc) { seg[cloc] = (seg[cloc] + cval) % MOD; cloc /= 2; } } ll add (int cloc) { cloc += P; ll res = 0; while (cloc > 1) { if (cloc & 1) res += seg[cloc-1]; cloc /= 2; } return res % MOD; } int main() { ios_base::sync_with_stdio(0); cin >> N >> M; for (int i = 0; i < N; i++) cin >> x[i]; for (int i = 0; i < M; i++) cin >> y[i]; int cy = 0; for (int i = 0; i < N; i++) { while (cy < M && y[cy] < x[i]) cy++; if (cy == 0 || cy >= M) continue; lr.push_back(make_pair(y[cy-1] - x[i], y[cy] - x[i])); } sort (lr.begin(), lr.end()); N = lr.size(); for (int i = 0; i < N; i++) mm[lr[i].second]; M = 0; for (map<int, int>::iterator it = mm.begin(); it != mm.end(); it++) it->second = M++; upd (M, 1); for (int i = 0; i < N; i++) { if (i > 0 && lr[i] == lr[i-1]) continue; int cc = mm[lr[i].second]; ll ctot = (MOD + add (M + 2) - add (cc + 1)) % MOD; upd (cc, ctot); // x <= cc // 0, impossible // 1, stay // x > cc // 0, stay // 1, push all to cc } cout << add (M + 2) << "\n"; }
0
#include <iostream> #include <algorithm> #include <iomanip> #include <map> #include <set> #include <queue> #include <stack> #include <numeric> #include <bitset> #include <cmath> static const int MOD = 1000000007; using ll = long long; using u32 = unsigned; using u64 = unsigned long long; using namespace std; template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208; int main() { vector<int> v(4); for (auto &&i : v) scanf("%d", &i); sort(v.begin(),v.end()); int ans = INF<int>; do { ans = min(ans, abs(v[0]+v[1]-v[2]-v[3])); }while(next_permutation(v.begin(),v.end())); cout << ans << "\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); double m, n, sum = 0, ans; cin >> m >> n; for (long long i = 1; i <= m; i++) { sum = sum + i * (pow(i * 1.0 / m, n) - pow((i - 1) * 1.0 / m, n)); } cout << fixed << setprecision(5) << sum << endl; return 0; }
3
#include<bits/stdc++.h> using namespace std; int x; int main() { cin>>x; cout<<!x<<endl; }
0
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; typedef long long ll; const ll mod = 1e9 + 7; ll x, y, z, rs, rs1, a[N], b[N], ans; multiset <ll> st; struct node { ll a, b, c; } p[N]; bool cmp(node a, node b) { return a.a - a.b < b.a - b.b; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> x >> y >> z; int n = x + y + z; for (int i = 1; i <= n; i++) { cin >> p[i].a >> p[i].b >> p[i].c; rs += p[i].c; } sort(p + 1, p + 1 + n, cmp); for (int i = 1; i <= n; i++) { rs1 += (p[i].b - p[i].c); st.insert(p[i].b - p[i].c); if (i > y) { rs1 -= *st.begin(); st.erase(st.begin()); } a[i] = rs1; } st.clear(); rs1 = 0; for (int i = n; i >= 1; i--) { rs1 += (p[i].a - p[i].c); st.insert(p[i].a - p[i].c); if (n - i >= x) { rs1 -= *st.begin(); st.erase(st.begin()); } b[i] = rs1; } for (int i = y; i <= n - x; i++) { ans = max(ans, rs + b[i + 1] + a[i]); } cout << ans; }
0
#include <bits/stdc++.h> using namespace std; const int twx = 5e4 + 100; const int CNM_SB_GRD_GUN = 13331; const int inf = 0x3f3f3f3f; char buf[1 << 20], *p1, *p2; long long read() { long long sum = 0; long long flag = 1; char c = ((p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++); while (c < '0' || c > '9') { if (c == '-') { flag = -1; } c = ((p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++); } while (c >= '0' && c <= '9') { sum = ((sum * 10) + c - '0'); c = ((p1 == p2) && (p2 = (p1 = buf) + fread(buf, 1, 1 << 20, stdin), p1 == p2) ? EOF : *p1++); } return sum * flag; } int n; char s[twx]; long long Hash[twx]; long long p[twx]; int SBZY[twx]; int qf = 0; int qb = 0; void hhash() { Hash[0] = 0; for (int i = 1; i <= n; i++) { Hash[i] = Hash[i - 1] * CNM_SB_GRD_GUN + s[i]; } } bool mycmp(int x, int y, int z) { return Hash[x + z - 1] - Hash[y + z - 1] == (Hash[x - 1] - Hash[y - 1]) * p[z]; } int lcp(int x, int y) { int l = 1; int r = n - max(x, y) + 1; int m; while (l <= r) { m = l + r >> 1; if (mycmp(x, y, m)) { l = m + 1; } else { r = m - 1; } } return r; } int lcs(int x, int y) { int l = 1; int r = min(x, y); int m; while (l <= r) { m = l + r >> 1; if (mycmp(x - m + 1, y - m + 1, m)) { l = m + 1; } else { r = m - 1; } } return r; } void init() { scanf("%s", s + 1); n = strlen(s + 1); p[0] = 1; for (int i = 1; i <= n; i++) { p[i] = p[i - 1] * CNM_SB_GRD_GUN; } } void work() { hhash(); for (int len = 1; len <= n / 2; len++) { int rig = 0; for (int i = 1, j = 1 + len; j <= n; i += len, j += len) { int l = lcs(i, j), r = lcp(i, j); if (i - l < rig) { l = i - rig; } if (l + r - 1 >= len) { SBZY[qb++] = i - l + 1; rig = SBZY[qb - 1] + len - 1; } } if (qb != qf) { int j = 0; for (int i = 1; i <= n; i++) { if (SBZY[qf] == i) { i += len - 1; qf++; } else { s[++j] = s[i]; } } n = j; hhash(); } } s[n + 1] = 0; } void print() { puts(s + 1); } int main() { init(); work(); print(); return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int m = 0, c = 1, n = 0, a = -1; string s; stack<int> l; cin >> s; for (int i = 0; i < s.length(); i++) { if (s[i] == '(') l.push(i); else if (l.empty()) a = i; else { l.pop(); n = i - (l.empty() ? a : l.top()); if (m < n) m = n, c = 1; else if (m == n) c++; } } cout << m << " " << c << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int n, m, a[100 + 2][100 + 2]; inline int col(int x, int y) { int u = a[x - 1][y], d = a[x + 1][y]; int l = a[x][y - 1], r = a[x][y + 1], ans; for (ans = 1; u == ans || d == ans || l == ans || r == ans; ++ans) ; return ans; } inline bool exist(int x, int y, int c) { if (a[x][y]) return true; if (a[x - 1][y] == c || a[x + 1][y] == c) return true; if (a[x][y - 1] == c || a[x][y + 1] == c) return true; return false; } int main(int argc, char *argv[]) { ios::sync_with_stdio(false); cin >> n >> m; for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) { if (a[i][j]) continue; int c = col(i, j), k; for (k = 1; i + k - 1 <= n && j + k - 1 <= m; ++k) { bool ok = true; for (int x = 1; x <= k; ++x) if (exist(i + k - 1, j + x - 1, c)) ok = false; else if (exist(i + x - 1, j + k - 1, c)) ok = false; if (col(i, j + k - 1) < c) ok = false; if (!ok) break; } for (int x = 1; x < k; ++x) for (int y = 1; y < k; ++y) a[i + x - 1][j + y - 1] = c; } for (int i = 1; i <= n; ++i) { for (int j = 1; j <= m; ++j) cout << (char)(a[i][j] + 'A' - 1); cout << endl; } fclose(stdin); fclose(stdout); return 0; }
5
#include <bits/stdc++.h> using namespace std; char s[2050][2050]; int p[2050][2050]; int r[2050][2050]; int u[2050], d[2050]; int h, w; int main(){ memset(p, 0, sizeof(p)); scanf("%d%d", &h, &w); for(int i=1;i<=h;i++){ scanf("%s", s[i]+1); } for(int i=1;i<h;i++){ for(int j=1;j<w;j++){ int cnt=0; cnt+=(s[i][j]=='.'); cnt+=(s[i+1][j]=='.'); cnt+=(s[i][j+1]=='.'); cnt+=(s[i+1][j+1]=='.'); if(cnt%2==0)p[i][j]=1; } } for(int i=1;i<h;i++){ for(int j=w-1;j>=1;j--){ if(p[i][j]){ r[i][j]=r[i][j+1]+1; } else r[i][j]=0; } } int M=max(h, w); for(int j=1;j<w;j++){ for(int i=1;i<h;i++){ int id=i-1; while(id&&r[id][j]>=r[i][j])id=u[id]; u[i]=id; } for(int i=h-1;i>=1;i--){ int id=i+1; while(id<h&&r[id][j]>=r[i][j])id=d[id]; d[i]=id; } for(int i=1;i<h;i++){ if(r[i][j]==0)continue; M=max(M, (d[i]-u[i])*(r[i][j]+1)); } } printf("%d\n", M); }
0
#include<bits/stdc++.h> using namespace std; using Int = long long; //BEGIN CUT HERE const Int MOD=1e9+9; //<- alert!!! typedef vector<Int> arr; typedef vector<arr> mat; inline arr mul(const mat &a,arr &b,Int mod){ arr res(b.size(),0); for(int i=0;i<(int)b.size();i++) for(int j=0;j<(int)a[i].size();j++) (res[i]+=a[i][j]*b[j])%=mod; return res; } inline mat mul(const mat &a,const mat &b,Int mod){ mat res(a.size(),arr(b[0].size(),0)); for(int i=0;i<(int)a.size();i++) for(int j=0;j<(int)b[0].size();j++) for(int k=0;k<(int)b.size();k++) (res[i][j]+=a[i][k]*b[k][j])%=mod; return res; } mat mat_pow(mat a,Int n,Int mod){ mat res(a); for(int i=0;i<(int)a.size();i++) for(int j=0;j<(int)a[i].size();j++) res[i][j]=(i==j); while(n){ if(n&1) res=mul(a,res,mod); a=mul(a,a,mod); n>>=1; } return res; } //END CUT HERE mat base; mat memo[100]; void init(mat a,Int mod){ base=mat(a); for(int i=0;i<(int)base.size();i++) for(int j=0;j<(int)base.size();j++) base[i][j]=i==j; memo[0]=a; for(int k=1;k<70;k++) memo[k]=mul(memo[k-1],memo[k-1],mod); } inline mat mat_pow2(Int n,Int mod){ mat res(base); int k=0; while(n){ if(n&1) res=mul(memo[k],res,mod); n>>=1; k++; } return res; } typedef pair<Int,Int> P; signed main(){ Int w,h,n,cnt=0; while(cin>>w>>h>>n,w||h||n){ Int x[n],y[n]; for(Int i=0;i<n;i++) cin>>x[i]>>y[i]; P p[n]; for(Int i=0;i<n;i++) p[i]=P(y[i],x[i]); sort(p,p+n); for(Int i=0;i<n;i++) x[i]=p[i].second,y[i]=p[i].first; arr a(w,0); a[0]=1; mat b(w,arr(w,0)); for(Int i=0;i<w;i++){ for(Int j=0;j<w;j++) b[i][j]=0; b[i][i]=1; if(i-1>=0) b[i][i-1]=1; if(i+1<w) b[i][i+1]=1; } init(b,MOD); Int d=1; for(Int k=0;k<n;k++){ if(y[k]==d) continue; a=mul(mat_pow2(y[k]-d-1,MOD),a,MOD); Int j=k; mat c=b; while(j<n&&y[k]==y[j]){ for(Int i=0;i<w;i++) c[x[j]-1][i]=0; j++; } a=mul(c,a,MOD); d=y[k]; } a=mul(mat_pow2(h-d,MOD),a,MOD); cout<<"Case "<<++cnt<<": "<<a[w-1]<<endl; } return 0; } /* verified on 2018/01/17 http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2397 */
0
#include <bits/stdc++.h> const double PI = 3.141592653589793238460; using namespace std; pair<long long int, long long int> ar[7001]; bool comp(pair<long long int, long long int> a, pair<long long int, long long int> b) { return a.first > b.first; } int main() { int n; cin >> n; for (int i = 1; i <= n; i++) cin >> ar[i].first; for (int i = 1; i <= n; i++) cin >> ar[i].second; sort(ar + 1, ar + n + 1, comp); long long int res = 0; int cnt = 0; vector<int> vis(n + 1, 0); for (int i = 1; i <= n; i++) { cnt = 0; int j = i; while (j <= n && ar[j].first == ar[i].first) { cnt++, j++; if (cnt == 2) vis[i] = vis[i + 1] = 1; else if (cnt > 2) vis[j - 1] = 1; } if (cnt == 1) continue; while (j <= n) { if (((ar[j].first ^ ar[i].first) & ar[j].first) == 0) vis[j] = 1; j++; } } for (int i = 1; i <= n; i++) if (vis[i]) res += ar[i].second; cout << res; }
2
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = s.size(), c[26] = {0}, p = 0; for (int i = 0; i < n; i++) { c[s[i] - 'a']++; } for (int i = 0; i < 26; i++) { if (c[i] % 2 != 0) p++; } if ((p - 1) % 2 == 0 || p == 0) cout << "First"; else cout << "Second"; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b, c, C, max1 = 0, max2; cin >> n >> a >> b >> c; for (int i = 0; i <= n; i++) for (int j = 0; j <= n; j++) { C = n - (a * i) - (b * j); if (C % c == 0 && C >= 0) { max1 = max(i + j + C / c, max1); } } cout << max1; }
1
#include <bits/stdc++.h> using namespace std; int n, k, s[100002][2]; int susc = 0, defc = 0, sus[100002], def[100002]; int main() { scanf("%d %d", &n, &k); memset(sus, 0, sizeof(sus)); memset(def, 0, sizeof(def)); for (int i = 1; i <= n; i++) { char c; scanf("\n%c%d", &c, &s[i][0]); if (c == '+') s[i][1] = 1, sus[s[i][0]]++, susc++; else s[i][1] = -1, def[s[i][0]]++, defc++; } bool kk[100002]; int p = 0; memset(kk, 0, sizeof(kk)); for (int i = 1; i <= n; i++) { if ((sus[i] + defc - def[i]) == k) { kk[i] = true; p++; } } if (p == 1) { for (int i = 1; i <= n; i++) { if (s[i][1] == 1) printf("%s\n", kk[s[i][0]] ? "Truth" : "Lie"); else printf("%s\n", !kk[s[i][0]] ? "Truth" : "Lie"); } } else { for (int i = 1; i <= n; i++) { if (s[i][1] == 1) printf("%s\n", kk[s[i][0]] ? "Not defined" : "Lie"); else printf("%s\n", !kk[s[i][0]] ? "Truth" : "Not defined"); } } return 0; }
2
#include<iostream> #include<cstdio> #include<cstring> using namespace std; const long long p=1000000007; int H,W; long long K,cnt1,cnt2,cnt3,s1,s2; char str[1002][1002]; struct matrix{ long long a[2][2]; matrix(int e=true){memset(a,0,sizeof(a));for(int i=0;i<2;++i)a[i][i]=e;} long long* operator[](const int &i){return a[i];} matrix& operator*=(matrix &mtx){ matrix mtx0(false); for(int i=0;i<2;++i)for(int j=0;j<2;++j)for(int k=0;k<2;++k)(mtx0[i][j]+=a[i][k]*mtx[k][j]%p)%=p; return (*this)=mtx0; } matrix& operator^=(long long x){ matrix mtx,mtx0=*this; while(x){if(x&1ll)mtx*=mtx0;mtx0*=mtx0;x>>=1;} return (*this)=mtx; } }; inline long long qpow(long long a,long long x){ long long s=1ll; while(x){if(x&1ll)(s*=a)%=p;(a*=a)%=p;x>>=1;} return s; } int main(){ scanf("%d%d%lld",&H,&W,&K); for(int i=1;i<=H;++i){ scanf("%s",str[i]+1);if(str[i][1]=='#'&&str[i][W]=='#')++cnt1; for(int j=1;j<=W;++j)if(str[i][j]=='#'){++cnt3;s1+=str[i][j]==str[i][j-1],s2+=str[i][j]==str[i-1][j];} } for(int i=1;i<=W;++i)if(str[1][i]=='#'&&str[H][i]=='#')++cnt2; if(cnt1&&cnt2)return puts("1"),0; if(!cnt1&&!cnt2)return printf("%lld",qpow(cnt3,K-1ll)),0; if(!cnt1)swap(cnt1,cnt2),swap(s1,s2); matrix A(false);A[0][0]=cnt3,A[1][0]=s1,A[1][1]=cnt1;A^=K-1ll; printf("%lld",(A[0][0]-A[1][0]+p)%p); }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, a, b; cin >> n >> a >> b; int x, y; int l; l = n / a; int flag = 0; for (x = 0; x <= l; x++) { y = (n - x * a); if (y % b == 0) { cout << "YES" << endl; cout << x << " " << y / b << endl; flag = 1; break; } } if (!flag) { cout << "NO" << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; signed main() { ios::sync_with_stdio(false); long long n, m, s, d; cin >> n >> m >> s >> d; long long a[n]; for (long long i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); vector<pair<string, long long> > v; long long x = a[0] - 1; if (a[0] - 1 >= s) v.push_back({"RUN", a[0] - 1}); else goto NO; for (long long i = 1; i < n; i++) { if (a[i] - a[i - 1] - 2 >= s) { if (a[i - 1] + 1 - x <= d) { v.push_back({"JUMP", a[i - 1] + 1 - x}); v.push_back({"RUN", a[i] - a[i - 1] - 2}); x = a[i] - 1; } else goto NO; } } if (a[n - 1] + 1 - x <= d) { v.push_back({"JUMP", a[n - 1] + 1 - x}); if (a[n - 1] + 1 < m) v.push_back({"RUN", m - a[n - 1] - 1}); goto YES; } else goto NO; NO : { cout << "IMPOSSIBLE"; return 0; } YES : { for (long long i = 0; i < v.size(); i++) cout << v[i].first << " " << v[i].second << endl; } }
4
#include <bits/stdc++.h> using namespace std; bool vis[700][700][30][8]; bool vis2[700][700]; int arr[50]; int n; int dr[8] = {0, 0, 1, 1, 1, -1, -1, -1}; int dc[8] = {1, -1, 0, 1, -1, 1, 0, -1}; pair<int, int> nxt[8]; int all = 0; void DFS(int r, int c, int last, int idx) { for (int i = 1; i <= arr[idx]; i++) { int nr = r + (dr[last] * i); int nc = c + (dc[last] * i); if (!vis2[nr][nc]) all++; vis2[nr][nc] = 1; } if (idx == n - 1) return; int nr = r + (dr[last] * arr[idx]); int nc = c + (dc[last] * arr[idx]); if (!vis[nr][nc][idx + 1][nxt[last].first]) { vis[nr][nc][idx + 1][nxt[last].first] = 1; DFS(nr, nc, nxt[last].first, idx + 1); } if (!vis[nr][nc][idx + 1][nxt[last].second]) { vis[nr][nc][idx + 1][nxt[last].second] = 1; DFS(nr, nc, nxt[last].second, idx + 1); } } int main() { nxt[0] = make_pair(3, 5); nxt[1] = make_pair(4, 7); nxt[2] = make_pair(3, 4); nxt[3] = make_pair(2, 0); nxt[4] = make_pair(1, 2); nxt[5] = make_pair(0, 6); nxt[6] = make_pair(7, 5); nxt[7] = make_pair(1, 6); scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } DFS(350, 350, 0, 0); cout << all << endl; return 0; }
4