solution
stringlengths
52
181k
difficulty
int64
0
6
#include <iostream> #include <cstdio> #include <string> #include <vector> #include <stack> #include <algorithm> using namespace std; int calc(const vector<int> &exp) { stack<int> st; for (int i = 0; i < 7; i++) { if (exp[i] == '+') { if (st.empty()) return 0; int a = st.top(); st.pop(); if (st.empty()) return 0; int b = st.top(); st.pop(); st.push(a + b); } else if (exp[i] == '-') { if (st.empty()) return 0; int a = st.top(); st.pop(); if (st.empty()) return 0; int b = st.top(); st.pop(); st.push(b - a); } else if (exp[i] == '*') { if (st.empty()) return 0; int a = st.top(); st.pop(); if (st.empty()) return 0; int b = st.top(); st.pop(); st.push(a * b); } else { st.push(exp[i]); } } return st.top(); } inline bool is_op(char c) { return (c == '+' || c == '-' || c == '*'); } void format(const vector<int> &exp) { // for (int i = 0; i < exp.size(); i++) cout << exp[i] << ' '; // cout << endl; if (is_op(exp[2]) && is_op(exp[5]) && is_op(exp[6])) { // cout << "A\n"; printf("(((%d %c %d) %c (%d %c %d)))\n", exp[0], exp[2], exp[1], exp[6], exp[3], exp[5], exp[4]); } else if (is_op(exp[2]) && is_op(exp[4]) && is_op(exp[6])) { // cout << "B\n"; printf("(((%d %c %d) %c %d) %c %d)\n", exp[0], exp[2], exp[1], exp[4], exp[3], exp[6], exp[5]); } else if (is_op(exp[3]) && is_op(exp[4]) && is_op(exp[6])) { // cout << "C\n"; printf("((%d %c (%d %c %d)) %c %d)\n", exp[0], exp[4], exp[1], exp[3], exp[2], exp[6], exp[5]); } else if (is_op(exp[3]) && is_op(exp[5]) && is_op(exp[6])) { // cout << "D\n"; printf("(%d %c (%d %c %d) %c %d)\n", exp[0], exp[6], exp[1], exp[3], exp[2], exp[5], exp[4]); } else if (is_op(exp[4]) && is_op(exp[5]) && is_op(exp[6])) { // cout << "E\n"; printf("(%d %c (%d %c (%d %c %d)))\n", exp[0], exp[6], exp[1], exp[5], exp[2], exp[4], exp[3]); } else { printf("ERROR"); exit(1); } } int main() { vector<int> n(7); while (cin >> n[0] >> n[1] >> n[2] >> n[3]) { if (!(n[0] || n[1] || n[2] || n[3])) break; string op = "+-*"; bool found = false; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { n[4] = op[i], n[5] = op[j], n[6] = op[k]; sort(n.begin(), n.end()); do { if (calc(n) == 10) { found = true; format(n); goto loop_end; } } while (next_permutation(n.begin(), n.end())); } } } loop_end: if (!found) cout << 0 << endl; } return 0; }
0
#include <stdio.h> #include<math.h> int i; int j; int k=2; int a; int max=1; int main(){ scanf("%d",&i); for(j=2;j<i;j++){ k=2; while(1){ a=pow(j,k); if(a>i)break; if(max<a)max=a; k++; } } printf("%d",max); }
0
#include <bits/stdc++.h> using namespace std; int main() { int i, j, num; while (scanf("%d", &num) != EOF) { if (num & 1) { putchar('1'); for (i = 2; i < num; i += 2) printf(" %d %d", i + 1, i); putchar('\n'); } else { printf("2 1"); for (i = 3; i < num; i += 2) printf(" %d %d", i + 1, i); putchar('\n'); } } return 0; }
3
#include<bits/stdc++.h> #define MAXN 1000000 #define INF 2000000000 using namespace std; int n,V,x[MAXN+5]; int cnt[25]; int l[25][MAXN+5],r[25][MAXN+5]; int f1[MAXN*5+5],f2[MAXN*5+5];//如思路中的定义 bool ans[MAXN+5];//ans记录的是对于某一条线段的答案,不是某个绿洲的答案 void Init() { for(int i=0;i<=MAXN*5+3;i++) f1[i]=0,f2[i]=INF; } int UpFind(int id,int pos)//找l在pos+1的左边的线段,也就是能够向右扩展的最靠右的线段 { pos++; int p=upper_bound(l[id]+1,l[id]+cnt[id]+1,pos)-l[id]; p--; if(p<=0) return pos; return max(r[id][p],pos-1); } int LowFind(int id,int pos)//找r在pos-1的右边的线段,也就是能够向左扩展的最靠左的线段 { pos--; int p=lower_bound(r[id]+1,r[id]+cnt[id]+1,pos)-r[id]; if(p>=cnt[id]+1) return pos; return min(l[id][p],pos+1); } int main() { Init(); scanf("%d %d",&n,&V); int logV=0; for(logV=0;(1<<logV)<=V;logV++);//求出来的实际上是logV+1 for(int i=1;i<=n;i++) scanf("%d",&x[i]); x[n+1]=INF; x[0]=-INF;//便于操作 for(int LG=0;LG<=logV;LG++) { int d=V/(1<<LG); cnt[LG]=1; l[LG][1]=1; //求线段 for(int i=1;i<=n;i++) { r[LG][cnt[LG]]=i; if(x[i+1]-x[i]>d) { cnt[LG]++; l[LG][cnt[LG]]=i+1; } } cnt[LG]--; } if(cnt[0]>logV+1)//特判 { for(int i=1;i<=n;i++) printf("Impossible\n"); return 0; } int all=(1<<(logV+1)); f1[0]=0,f2[0]=n+1;//预处理两个f数组 for(int s=0;s<all;s+=2) for(int i=0;i<=logV;i++) { if(!(s&(1<<i))) continue; f1[s]=max(f1[s],UpFind(i,f1[s-(1<<i)])); f2[s]=min(f2[s],LowFind(i,f2[s-(1<<i)])); } for(int i=1;i<=cnt[0];i++) { int ln=l[0][i],rn=r[0][i]; for(int s1=0;s1<all;s1+=2) { int s2=all-1-s1-1; int lpos=f1[s1]; int rpos=f2[s2]; if(lpos>=ln-1&&rpos<=rn+1) { ans[i]=true; break; } } } int pos=1; for(int i=1;i<=n;i++) { if(ans[pos]==true) printf("Possible\n"); else printf("Impossible\n"); if(x[i+1]-x[i]>V) pos++; } return 0; }
0
#include<bits/stdc++.h> using namespace std; int main() { map <string, int> map; int n,m; cin >> n; while (n--) { string x; cin >> x; map[x]++; } cin >> m; while(m--) { string x; cin >> x; map[x]--; } int best = 0; for (auto p : map){ best = max(best, p.second); } cout << best << endl; }
0
#include <bits/stdc++.h> using namespace std; long long mat[3001][3001]; long long sw[3001][3001]; long long ans = 0; void windowK(int row, int n, int k) { deque<int> d(k); for (int i = 0; i < k; i++) { while (!d.empty() && mat[row][i] >= mat[row][d.back()]) d.pop_back(); d.push_back(i); } sw[row][0] = mat[row][d.front()]; for (int i = k; i < n; i++) { while (!d.empty() && d.front() <= (i - k)) d.pop_front(); while (!d.empty() && mat[row][i] > mat[row][d.back()]) d.pop_back(); d.push_back(i); sw[row][i - k + 1] = mat[row][d.front()]; } } void window2(int col, int n, int k) { deque<int> d(k); for (int i = 0; i < k; i++) { while (!d.empty() && sw[i][col] >= sw[d.back()][col]) d.pop_back(); d.push_back(i); } ans += sw[d.front()][col]; for (int i = k; i < n; i++) { while (!d.empty() && d.front() <= (i - k)) d.pop_front(); while (!d.empty() && sw[i][col] > sw[d.back()][col]) d.pop_back(); d.push_back(i); ans += sw[d.front()][col]; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long n, m, a, b; cin >> n >> m >> a >> b; long long gg, x, y, z; cin >> gg >> x >> y >> z; long long g[n * m + 1]; g[0] = gg; for (int i = 1; i < n * m; i++) g[i] = ((g[i - 1] * x) + y) % z; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) mat[i][j] = -g[i * m + j]; for (int i = 0; i < n; i++) windowK(i, m, b); for (int i = 0; i < m - b + 1; i++) { window2(i, n, a); } ans *= -1; cout << ans; }
5
#include <bits/stdc++.h> using namespace std; using ll=long long; int main(){ ll N, M; cin>>N>>M; if(N>=M/2) cout<<M/2<<endl; else cout<<N+(M-2*N)/4<<endl; }
0
#include <bits/stdc++.h> using namespace std; int b[1001]; int main() { int n; cin >> n; int a[n]; set<int> st; for (int i = 1; i <= n; i++) { cin >> a[i]; b[a[i]]++; st.insert(a[i]); } int mx = 0; for (int i = 1; i <= 1000; i++) mx = max(b[i], mx); cout << mx << " " << st.size(); return 0; }
1
#include <bits/stdc++.h> using namespace std; int N; char buf[64]; map<set<string>, vector<string>> hosts; map<string, set<string>> routes; int main() { scanf("%d", &N); for (int i = 0; i < (N); ++i) { scanf("%s", buf); int hn_end = 7, n = strlen(buf); while (hn_end < n && buf[hn_end] != '/') ++hn_end; string hn(buf, buf + hn_end), route(buf + hn_end, buf + n); routes[hn].insert(route); } for (auto p : routes) hosts[p.second].push_back(p.first); int total = 0; for (auto p : hosts) if (p.second.size() > 1) ++total; printf("%d\n", total); for (auto p : hosts) if (p.second.size() > 1) { for (int i = 0; i < ((int)p.second.size()); ++i) { printf("%s%c", p.second[i].c_str(), " \n"[i == (int)p.second.size() - 1]); } } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n; cin >> n; if (n % 2 == 0) cout << n / 2 - 1; else cout << n / 2; return 0; }
3
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 10; const long long MOD = 1e9 + 7; int main() { string s; while (cin >> s) { long long sum = 0; int len = s.length(); int x = 0; for (int i = len - 1; i >= 0; --i) { if (s[i] == 'b') { x++; } else { sum += x; sum %= MOD; x *= 2; x %= MOD; } } cout << sum << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int p[1010]; int main(){ int n; scanf("%d", &n); for(int i = 0; i < n; i++) scanf("%d", &p[i]); int q; scanf("%d", &q); for(int i = 0; i < q; i++) { int a, b, k; scanf("%d%d%d", &a, &b, &k); printf("%d\n", (int)count(p+a, p+b, k)); } }
0
#include <bits/stdc++.h> using namespace std; const int INF = 2147483647; const long long LLINF = 9223372036854775807LL; const int maxn = 100010; int t[maxn], T[maxn], x[maxn], cost[maxn]; int n, m; long long calc(int i, int cnt) { long long res = (long long)cost[i] * cnt; long long tm = m; long long p = T[i] - t[i]; tm -= p * (cnt - 1); if (tm > p) res += x[i] * tm; return res; } int main() { scanf("%d%d", &n, &m); for (int i = 0; i < n; ++i) scanf("%d%d%d%d", &t[i], &T[i], &x[i], &cost[i]); long long ans = 0; for (int i = 0; i < n; ++i) { if (T[i] - t[i] <= 0) { ans += cost[i] + x[i] * (long long)m; } else { long long best = calc(i, 1); int p = (T[i] - t[i]); best = min(best, calc(i, (m + p - 1) / p)); ans += best; } } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 400000; int n, g, r; int l[MAXN]; long long sum[MAXN]; long long d[MAXN]; long long cycle; int m; long long q[MAXN], code[MAXN]; int minv[MAXN]; void build(int l, int r, int v) { minv[v] = n + 1; if (l != r) build(l, (l + r) >> 1, v << 1), build(((l + r) >> 1) + 1, r, (v << 1) + 1); } void change(int p, int l, int r, int v, int x) { if (l > p || r < p) return; minv[v] = x; if (l != r) change(p, l, (l + r) >> 1, v << 1, x), change(p, ((l + r) >> 1) + 1, r, (v << 1) + 1, x); } int getmin(int L, int R, int l, int r, int v) { if (L > r || l > R) return n + 1; if (L <= l && r <= R) return minv[v]; return min(getmin(L, R, l, (l + r) >> 1, v << 1), getmin(L, R, ((l + r) >> 1) + 1, r, (v << 1) + 1)); } int process(int u, int curt) { int l, r; l = g - curt; r = cycle - 1 - curt; l = (l + code[u]) % cycle; r = (r + code[u]) % cycle; int p1, p2; p1 = lower_bound(q, q + m, l) - q; p2 = lower_bound(q, q + m, r + 1) - q - 1; if (l <= r) { if (p1 > p2) return n + 1; return getmin(p1, p2, 0, m - 1, 1); } int ret = n + 1; if (p1 < m) ret = min(ret, getmin(p1, m - 1, 0, m - 1, 1)); ret = min(ret, getmin(0, p2, 0, m - 1, 1)); return ret; } int main() { scanf("%d%d%d", &n, &g, &r); cycle = g + r; for (int i = 0; i <= n; i++) scanf("%d", l + i); for (int i = 1; i <= n + 1; i++) sum[i] = sum[i - 1] + l[i - 1]; for (int i = 1; i <= n + 1; i++) code[i] = sum[i] % cycle; for (int i = 0; i <= n + 1; i++) q[m++] = code[i]; sort(q, q + m); m = unique(q, q + m) - q; build(0, m - 1, 1); for (int i = n, pos; i > 1; i--) { pos = process(i, 0); d[i] = d[pos] + sum[pos] - sum[i]; if (pos != n + 1) d[i] += cycle - (sum[pos] - sum[i]) % cycle; change(lower_bound(q, q + m, code[i]) - q, 0, m - 1, 1, i); } int test_num; scanf("%d", &test_num); for (int pos, tim; test_num--;) { scanf("%d", &tim); tim += sum[1]; if (tim % cycle >= g) tim += cycle - tim % cycle; pos = process(1, tim % cycle); long long res = d[pos] + sum[pos] - sum[1] + tim; if (pos != n + 1) res += cycle - (sum[pos] - sum[1] + tim) % cycle; printf("%I64d\n", res); } return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 5e5 + 10; int n, a[N]; long long s2[N], s1[N], sum, mi, mx; inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } long long wk() { for (int i = n; i >= 1; i--) s1[i] = s1[i + 1] + a[i]; for (int i = 1; i <= n; i++) s2[i] = s2[i - 1] + a[i]; long long w = 0; sum = 0; for (int i = 1; i <= n; i++) sum += a[i]; for (int i = n; i; i--) { int p = lower_bound(a + i + 1, a + n + 1, i, greater<int>()) - a; long long x = (long long)i * (i - 1) + (long long)(p - i - 1) * i + s1[p]; if (s2[i] - x > i) { puts("-1"); exit(0); } w = max(w, s2[i] - x); } if (sum & 1) { if (w & 1) ; else w++; } else { if (w & 1) w++; } return w; } int main() { n = read(); for (int i = 1; i <= n; i++) a[i] = read(); sort(a + 1, a + n + 1, greater<int>()); mx = wk(); reverse(a + 1, a + n + 1); for (int i = 1; i <= n; i++) a[i] = n - a[i]; mi = n - wk(); if (mx > mi) swap(mx, mi); for (long long i = min(mx, mi); i <= max(mx, mi) && i <= n; i += 2) printf("%I64d ", i); return 0; }
5
#include <bits/stdc++.h> using namespace std; typedef long long ll; #define P pair<ll,ll> #define FOR(I,A,B) for(int I = int(A); I < int(B); ++I) #define FORR(I,A,B) for(ll I = ll((B)-1); I >= ll(A); --I) #define TO(x,t,f) ((x)?(t):(f)) #define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9 #define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v x is sorted #define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v x is sorted #define NUM(x,v) (POSU(x,v)-POSL(x,v)) //x is sorted #define REV(x) (reverse(x.begin(),x.end())) //reverse ll gcd(ll a,ll b){if(a%b==0)return b;return gcd(b,a%b);} ll lcm(ll a,ll b){ll c=gcd(a,b);return ((a/c)*(b/c)*c);} #define NEXTP(x) next_permutation(x.begin(),x.end()) const ll INF=ll(1e16)+ll(7); const ll MOD=1000000007LL; #define out(a) cout<<fixed<<setprecision((a)) class Seg_Tree{ public: vector<double> dat; int n; void initsize(int n0){ int k=1; while(1){ if(n0<=k){ n=k; dat.resize(2*n-1); for(int i=0;i<2*n-1;i++)dat[i]=1.0; break; } k*=2; } } //i banme wo x nisuru void update(int i,double x){ i += n-1; dat[i] = x; while(i>0){ i = (i-1) / 2; dat[i] = dat[i*2+1]*dat[i*2+2]; } } //[a,b) double query0(int a,int b,int k,int l,int r){ if(r<=a || b<=l)return 1.0; if(a<=l && r<=b)return dat[k]; else{ double vl = query0(a,b,k*2+1,l,(l+r)/2); double vr = query0(a,b,k*2+2,(l+r)/2,r); return vl*vr; } } //return min [a,b) double query(int a,int b){ return query0(a,b,0,0,n); } }; int main(){ int N,Q; cin >> N; vector<int> T(N+1,1000000009),A(N); FOR(i,0,N) cin >> T[i] >> A[i]; Seg_Tree sg; sg.initsize(N); FOR(i,0,N){ sg.update(i,(double)(10.0-A[i])/10.0); } cin >> Q; FOR(i,0,Q){ int L,R; cin >> L >> R; int il,ir; il = POSL(T,L); ir = POSU(T,R); double x = 1000000000LL*sg.query(il,ir); out(10) << x << endl; } }
0
#include <bits/stdc++.h> using namespace std; map<int, int> mp; int n, ans[100001][4], cnt = 0; pair<int, int> t[4]; priority_queue<pair<int, int> > q; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> n; for (int i = 1; i <= n; i++) { int x; cin >> x; ++mp[x]; } for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) { q.push({it->second, it->first}); } while (q.size() >= 3) { ++cnt; for (int i = 1; i <= 3; i++) { t[i] = q.top(); q.pop(); ans[cnt][i] = t[i].second; } for (int i = 1; i <= 3; i++) { t[i].first--; if (t[i].first) { q.push(t[i]); } } } cout << cnt << "\n"; for (int i = 1; i <= cnt; i++) { sort(ans[i] + 1, ans[i] + 4); cout << ans[i][3] << " " << ans[i][2] << " " << ans[i][1] << "\n"; } }
3
#include <bits/stdc++.h> using namespace std; long long MOD = 1000000007; typedef struct { int a, b, c; } iii; void mosA(vector<int> a) { for (int i = 0; i < a.size(); i++) printf("%d ", a[i]); printf("\n"); } void mosP(vector<pair<int, int> > a) { for (int i = 0; i < a.size(); i++) printf("%d %d\n", a[i].first, a[i].second); printf("\n"); } long long min_especial(long long a, long long b) { if (a != -1 && b != -1) return min(a, b); else return max(a, b); } bool ord(int a, int b) { return b < a; } long long ans, k; int q, n, m, t, a, b, c, u, f, l, x, z, y, s, d, h, r; template <class T, int SZ> struct Fenwick { T tree[SZ]; void adjust(int p, T v) { for (int i = p; i < SZ; i += (i & -i)) tree[i] += v; } void limpiar() { memset(tree, 0, sizeof(T) * SZ); } T sum(int p) { T s = 0; for (int i = p; i; i -= (i & -i)) s += tree[i]; return s; } }; struct Fenwick<int, 100005> F; int R[100005]; vector<iii> V[100005]; map<int, int> M; int main() { scanf("%d", &n); int pos = 0; int indiceNuevo = 1; for (int i = 0; i < (n); i++) { scanf("%d%d%d", &a, &b, &c); int m = M[c]; if (!m) m = M[c] = indiceNuevo++; if (a == 3) V[m].push_back({a, b, pos++}); else V[m].push_back({a, b, c}); } indiceNuevo--; for (int i = 1; i <= indiceNuevo; i++) { vector<int> v; map<int, int> mapa; for (iii x : V[i]) v.push_back(x.b); sort(v.begin(), v.end()); int ind = 1; for (int k = 0; k < v.size(); k++) { if (k == v.size() || v[k] != v[k + 1]) mapa[v[k]] = ind++; } for (iii& x : V[i]) x.b = mapa[x.b]; } for (int i = 1; i <= indiceNuevo; i++) { F.limpiar(); for (iii x : V[i]) { if (x.a == 1) { F.adjust(x.b, 1); continue; } if (x.a == 2) { F.adjust(x.b, -1); continue; } R[x.c] = F.sum(x.b); } } for (int i = 0; i < (pos); i++) { printf("%d\n", R[i]); } return 0; }
5
#include <bits/stdc++.h> using namespace std; inline int Isdigit(char c) { if (c < '0' || c > '9') return 0; return 1; } template <class T> T read() { register T x = 0, flag = 1; register char ch; while (!Isdigit(ch = getchar())) if (ch == '-') flag = -1; while (Isdigit(ch)) x = x * 10 + (ch & 15), ch = getchar(); return x * flag; } template <class T> inline void write(T x) { if (x < 0) putchar('-'), x = -x; if (x >= 10) write(x / 10); putchar(x % 10 + '0'); } template <class T> inline bool Chkmax(T& x, const T& y) { return x < y ? x = y, true : false; } template <class T> inline bool Chkmin(T& x, const T& y) { return x > y ? x = y, true : false; } const int maxn = 202020; int T, x; string s; int tag[maxn]; int Id(int x) { if (x < 0 || x >= s.size()) return 0; if (tag[x] == 0) return 0; return 1; } int main() { T = read<int>(); while (T--) { cin >> s; x = read<int>(); for (int i = 0; i < s.size(); i++) tag[i] = -1; for (int i = 0; i < s.size(); i++) { if (s[i] == '0') { if (i - x >= 0) tag[i - x] = 0; if (i + x <= s.size() - 1) tag[i + x] = 0; } } int ok = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == '1') { if ((Id(i - x) | Id(x + i)) == 0) ok = 1; } } if (ok == 1) puts("-1"); else { for (int i = 0; i < s.size(); i++) { if (tag[i] == -1) printf("%d", 1); else printf("%d", 0); } puts(""); } } }
3
#include <bits/stdc++.h> using namespace std; int M; int total; int X[200]; int Y[200]; long long c[2000][2000]; long long f[200][2000]; bool caled[200][2000]; const long long moder = 1000000007; long long getF(int nowPosi, int remain) { if (nowPosi == M) { if (remain == 0) return 1; else return 0; } if (caled[nowPosi][remain]) return f[nowPosi][remain]; caled[nowPosi][remain] = true; long long& ret = f[nowPosi][remain]; ret = 0; int candidateFirst = 0; for (int i = nowPosi; i < M; ++i) candidateFirst += X[i]; int candidateSecond = 0; for (int i = 0; i <= nowPosi; ++i) candidateSecond += X[i]; int selected = total - remain; candidateSecond -= selected; if (candidateSecond < 0) candidateSecond = 0; int maxSelected = min(remain, Y[nowPosi]); for (int i = 0; i <= maxSelected; ++i) { long long c1 = c[candidateFirst][X[nowPosi]]; long long c2 = c[candidateSecond][i]; long long tmp = c1 * c2 % moder; ret += tmp * getF(nowPosi + 1, remain - i); ret %= moder; } return ret; } void deal() { memset(caled, false, sizeof(caled)); total = 0; for (int i = 0; i < M; ++i) total += X[i]; cout << getF(0, total) << endl; } void init() { scanf("%d", &M); for (int i = 0; i < M; ++i) scanf("%d", &X[i]); for (int i = 0; i < M; ++i) scanf("%d", &Y[i]); memset(c, 0, sizeof(c)); c[0][0] = 1; c[1][0] = 1; c[1][1] = 1; for (int i = 2; i <= 1000; ++i) { c[i][0] = 1; c[i][i] = 1; for (int j = 1; j < i; ++j) c[i][j] = (c[i - 1][j - 1] + c[i - 1][j]) % moder; } } int main() { init(); deal(); return 0; }
4
#include <bits/stdc++.h> using namespace std; const double PI = 2.0 * acos(0.0); const double EPS = 1e-6; bool is_prime(int x) { for (int i = 2; i * i <= x; i++) if (x % i == 0) return false; return true; } long long gcd(long long a, long long b) { for (long long tmp = a % b; tmp != 0; tmp = a % b) { a = b; b = tmp; } return b; } int main() { int T; scanf("%d", &T); while (T--) { int n; scanf("%d", &n); int p = n, q = n + 1; for (; !is_prime(p); p--) ; for (; !is_prime(q); q++) ; long long upper = 2LL * (1LL + n - p - q) + (long long)p * q; long long lower = 2LL * p * q; long long g = gcd(upper, lower); printf("%I64d/%I64d\n", upper / g, lower / g); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { string s; cin >> s; int n = s.size(); s += '?'; for (int i = 1; i < n; i++) { if (s[i] == s[i - 1]) { for (char j = 'a'; j <= 'z'; j++) { if (j != s[i] && j != s[i + 1]) { s[i] = j; break; } } } } for (auto x : s) { if (x == '?') continue; cout << x; } return 0; }
3
#include <bits/stdc++.h> using namespace std; vector<int> primes; int cnt[5001005]; bool visited[5001005]; void get_primes(long long MAX) { for (long long x = 2; x <= MAX; x++) if (!visited[x]) { primes.push_back(x); cnt[x] = 1; for (long long j = x * 2; j <= MAX; j += x) visited[j] = true; } } int main() { int t; cin >> t; get_primes(5000003); for (int i = 2; i < 5000003; i++) { if (cnt[i] == 1) continue; for (int j = 0; j < primes.size(); j++) { if (i % primes[j] == 0) { cnt[i] = cnt[i / primes[j]] + 1; break; } } } for (int i = 2; i < 5000003; i++) cnt[i] += cnt[i - 1]; int a, b; while (t--) { scanf("%d %d", &a, &b); printf("%d\n", cnt[a] - cnt[b]); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; int x = n * 4; for (int i = 1; i <= n; i++) { x -= 2; cout << x << " "; } cout << endl; } }
1
#include <bits/stdc++.h> using namespace std; vector<vector<int> > adj; vector<bool> visited; int z = 0; void dfs(int v, int c) { z = max(z, c); if (visited[v]) return; visited[v] = true; for (int i = 0; i < adj[v].size(); i++) { int child = adj[v][i]; if (!visited[child]) { dfs(child, c + 1); } } } int main() { int n; cin >> n; adj = vector<vector<int> >(10000); visited = vector<bool>(n + 1); vector<int> qrr(n + 1); for (int i = 1; i <= n; i++) { cin >> qrr[i]; if (qrr[i] != -1) { adj[qrr[i]].push_back(i); } } int m = 0; for (int i = 1; i <= n; i++) { if (qrr[i] == -1) { dfs(i, 1); } } cout << z << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long a, b, ans, ansk, k = 0, cnt; long long gcd(long long a, long long b) { if (b == 0) return a; else gcd(b, a % b); } long long solve(long long x) { long long t = a / x; if (a % x) t++; k = x * t - a; long long bns = (b + k) * (a + k) / gcd(a + k, b + k); return bns; } int main() { cin >> a >> b; if (a == b) { cout << 0 << endl; return 0; } if (a > b) { long long t = a; a = b; b = t; } cnt = b - a; ans = -1; for (long long i = 1; i * i <= cnt; i++) { if (cnt % i == 0) { long long zz = solve(i); if (ans > zz || ans == -1) { ans = zz; ansk = k; } else if (ans == zz && ansk > k) ansk = k; zz = solve(cnt / i); if (ans > zz || ans == -1) { ans = zz; ansk = k; } else if (ans == zz && ansk > k) ansk = k; } } cout << ansk << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; long long n, m, k, X[1000010], L[1000010], R[1000010], Fa[1000010], _2[1000010], tim = 0; map<long long, long long> hsh; vector<long long> V[1000010]; long long getf(long long x) { return Fa[x] == x ? x : Fa[x] = getf(Fa[x]); } signed main() { scanf("%lld%lld%lld", &n, &m, &k); _2[0] = 1; for (long long i = 1; i < 1000010; i++) _2[i] = _2[i - 1] * 2 % 1000000007; for (long long i = 1; i <= n; i++) scanf("%lld", &X[i]); for (long long i = 1; i <= m; i++) { scanf("%lld%lld", &L[i], &R[i]); long long t = X[L[i]] ^ X[R[i]]; if (!hsh.count(t)) hsh[t] = ++tim; V[hsh[t]].push_back(i); } long long ans = (_2[k] - tim) * _2[n] % 1000000007; for (long long i = 1; i <= tim; i++) { vector<long long> Vx; Vx.clear(); long long tot = 0; for (long long j = 0; j < V[i].size(); j++) { long long a = L[V[i][j]], b = R[V[i][j]]; Fa[a] = a; Fa[b] = b; Vx.push_back(a); Vx.push_back(b); } tot = 0; for (long long j = 0; j < V[i].size(); j++) { long long a = L[V[i][j]], b = R[V[i][j]]; if (getf(a) != getf(b)) { tot++; Fa[getf(a)] = getf(b); } } ans = (ans + _2[n - tot]) % 1000000007; } printf("%lld\n", (ans % 1000000007 + 1000000007) % 1000000007); }
3
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, i; cin >> n; int a[26] = {0}; for (i = 0; i < n; i++) { string s; cin >> s; for (char ch : s) { ++a[ch - 'a']; } } int ans = 1; for (int i = 0; i < 26; i++) { if (a[i] % n != 0) { ans = 0; break; } } if (ans) cout << "YES" << endl; else cout << "NO" << endl; } return 0; }
1
#include <bits/stdc++.h> #define rep(i,n) for(int i=0;i<(n);i++) using namespace std; using lint=long long; int main(){ int k,q; scanf("%d%d",&k,&q); vector<lint> d0(k); rep(i,k) scanf("%lld",&d0[i]); rep(_,q){ lint n,x,m; scanf("%lld%lld%lld",&n,&x,&m); vector<lint> d(k); rep(i,k) d[i]=d0[i]%m; lint res0=0; res0+=count(d.begin(),d.end(),0)*((n-1)/k); res0+=count(d.begin(),d.begin()+(n-1)%k,0); lint last=x; last+=accumulate(d.begin(),d.end(),0LL)*((n-1)/k); last+=accumulate(d.begin(),d.begin()+(n-1)%k,0LL); lint res1=last/m-x/m; printf("%lld\n",(n-1)-res0-res1); } return 0; }
0
#include<iostream> #include<cstdio> #include<cstring> #define ll long long #define M 100010 using namespace std; struct point{ int to,next,f; }e[M<<1]; int n,m,num; ll tot1,tot2,ans; int head[M],co[M]; ll cnt[5]; bool flag; bool vis[M]; void add(int from,int to,int f) { e[++num].next=head[from]; e[num].to=to; e[num].f=f; head[from]=num; } void dfs(int x) { vis[x]=true; tot1++; cnt[co[x]]++; for(int i=head[x];i;i=e[i].next) { int to=e[i].to; tot2+=e[i].f==1; if(!vis[to]) { co[to]=(co[x]+e[i].f+3)%3; dfs(to); } else if(co[to]!=(co[x]+e[i].f+3)%3) flag=true; } } int main() { scanf("%d%d",&n,&m); for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); add(x,y,1); add(y,x,-1); } for(int i=1;i<=n;i++) { if(!vis[i]) { flag=false; tot1=tot2=0; cnt[0]=cnt[1]=cnt[2]=0; dfs(i); if(flag) ans+=tot1*tot1; else if(cnt[0]&&cnt[1]&&cnt[2]) ans+=cnt[0]*cnt[1]+cnt[1]*cnt[2]+cnt[2]*cnt[0]; else ans+=tot2; } } printf("%lld",ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; long long spf[100001]; long long gcd(long long a, long long b); long long palindrome(string s); long long modexp(long long a, long long b, long long m); void sieve(); long long ceil(long long a, long long b); vector<long long> getFactorization(long long x); void getZarr(string str, long long Z[]); vector<long long> prefix_function(string s); long long d, n; long long a[2005]; vector<long long> adj[2005]; vector<long long> tree[2005]; bool done[2005] = {false}; long long m[2005]; long long f[2005]; void dfs(long long s, long long e, long long root) { for (auto x : adj[s]) { if (x != e) { if (a[x] >= a[root] && a[x] <= a[root] + d) { if (done[x] == true && a[x] == a[root]) continue; else { tree[s].push_back(x), tree[x].push_back(s); dfs(x, s, root); } } } } } void dfs2(long long s, long long e) { for (auto x : tree[s]) { if (x != e) { dfs2(x, s); m[s] = (m[s] % 1000000007 * (m[x] + 1) % 1000000007) % 1000000007; } } } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> d >> n; for (long long i = 1; i < n + 1; i++) cin >> a[i]; for (long long i = 0; i < n - 1; i++) { long long x, y; cin >> x >> y; adj[x].push_back(y), adj[y].push_back(x); } for (long long i = 1; i < n + 1; i++) { done[i] = true; for (long long j = 0; j < 2005; j++) { m[j] = 1; } for (long long j = 1; j < 2005; j++) { tree[j].clear(); } dfs(i, 0, i); dfs2(i, 0); f[i] = m[i]; } long long ans = 0; for (long long i = 1; i < n + 1; i++) { ans = (ans % 1000000007 + f[i] % 1000000007) % 1000000007; } cout << ans << endl; } long long gcd(long long a, long long b) { if (a == 0) return b; if (b == 0) return a; if (a == b) return a; if (a > b) return gcd(a % b, b); return gcd(a, b % a); } long long palindrome(string s) { long long l = 0; long long h = s.length() - 1; while (h > l) { if (s[l++] != s[h--]) { return 0; } } return 1; } long long modexp(long long a, long long b, long long m) { if (b == 0) return 1; long long temp = modexp(a, b / 2, m); temp = (temp * temp) % m; if (b & 1) return (temp * (a % m)) % m; return temp; } void sieve() { spf[1] = 1; for (long long i = 2; i < 100001; i++) spf[i] = i; for (long long i = 4; i < 100001; i += 2) spf[i] = 2; for (long long i = 3; i * i < 100001; i++) { if (spf[i] == i) { for (long long j = i * i; j < 100001; j += i) if (spf[j] == j) spf[j] = i; } } } vector<long long> getFactorization(long long x) { vector<long long> ret; while (x != 1) { ret.push_back(spf[x]); x = x / spf[x]; } return ret; } long long ceil(long long a, long long b) { return a / b + (a % b != 0); } void getZarr(string str, long long Z[]) { long long n = str.length(); long long L, R, k; L = R = 0; for (long long i = 1; i < n; ++i) { if (i > R) { L = R = i; while (R < n && str[R - L] == str[R]) R++; Z[i] = R - L; R--; } else { k = i - L; if (Z[k] < R - i + 1) Z[i] = Z[k]; else { L = i; while (R < n && str[R - L] == str[R]) R++; Z[i] = R - L; R--; } } } } vector<long long> prefix_function(string s) { long long n = (long long)s.length(); vector<long long> pi(n); for (long long i = 1; i < n; i++) { long long j = pi[i - 1]; while (j > 0 && s[i] != s[j]) j = pi[j - 1]; if (s[i] == s[j]) j++; pi[i] = j; } return pi; }
4
#include<cstdio> #include<algorithm> using namespace std; int Get(int n, int K) { if (n%K == 0)return n / K; if (n < K)return 0; int m = n / K; if(m > K)return Get(n - m - 1, K); int u = n / K*K; int t = n % (m + 1); int nxt = u / (m + 1)*(m + 1) + t; if (nxt > u)nxt -= (m + 1); return Get(nxt, K); } int main() { int T, r = 0, n, K; scanf("%d", &T); while (T--) { scanf("%d%d", &n, &K); r ^= Get(n, K); } if (r)printf("Takahashi\n"); else printf("Aoki\n"); }
0
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") #pragma GCC optimize("unroll-loops") using namespace std; long long MOD = 998244353; double eps = 1e-12; void solve() { int n, k, d; cin >> n >> k >> d; int a[n], total[k + 1]; memset(total, 0, sizeof(total)); for (long long i = 0; i < n; i++) cin >> a[i]; int i = 0, j = 0, ans = INT_MAX, count = 0; while (j < n) { while (j - i + 1 < d && j < n) { total[a[j]]++; if (total[a[j]] == 1) count++; j++; } if (j < n) { total[a[j]]++; if (total[a[j]] == 1) count++; } if (j - i + 1 == d && j <= n - 1) ans = min(count, ans); total[a[i]]--; if (total[a[i]] == 0) count--; i++; j++; } cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; for (int it = 1; it <= t; it++) { solve(); } return 0; }
2
#include <bits/stdc++.h> using namespace std; int A[500000],cont=0; void merge(int left,int mid,int right){ int n1 = mid-left; int n2 = right-mid; int L[n1+1],R[n2+1]; for(int i=0;i<n1;i++) L[i] = A[left+i]; for(int i=0;i<n2;i++) R[i] = A[mid+i]; L[n1] = R[n2] = 1e9+1; int i=0,j=0; for(int k=left;k<right;k++){ if(L[i]<=R[j]) A[k] = L[i++]; else A[k] = R[j++]; cont++; } } void ms(int left,int right){ if(left+1<right){ int mid = (left+right)/2; ms(left,mid); ms(mid,right); merge(left,mid,right); } } int main(){ int n; cin>>n; for(int i=0;i<n;i++) scanf("%d",&A[i]); ms(0,n); for(int i=0;i<n;i++) if(i==0) printf("%d",A[i]); else printf(" %d",A[i]); printf("\n%d\n",cont); return 0; }
0
#include <iostream> class Vector { public: Vector(){} Vector(int x, int y): x(x), y(y) {} int dot(Vector v) { return v.x * x + v.y * y; } int norm() { return x * x + y * y; } Vector operator+(Vector v) { return Vector(v.x + x, v.y + y); } Vector operator-(Vector v) { return Vector(x - v.x, y - v.y); } int x, y; }; class Line { public: Line() {} Line(Vector p1, Vector p2): p1(p1), p2(p2) { direction = p1 - p2; } Vector p1, p2; Vector direction; }; int main() { int q; std::cin >> q; for (int i = 0; i < q; i++) { Vector p0, p1, p2, p3; std::cin >> p0.x >> p0.y >> p1.x >> p1.y >> p2.x >> p2.y >> p3.x >> p3.y; Line l1(p0, p1), l2(p2, p3); int dot = l1.direction.dot(l2.direction); if (dot == 0) { std::cout << 1 << std::endl; } else if (dot * dot == l1.direction.norm() * l2.direction.norm()) { std::cout << 2 << std::endl; } else { std::cout << 0 << std::endl; } } }
0
#include<bits/stdc++.h> using namespace std; int main(void) { long long n,a=0; cin>>n; for(int i=1;i<=n;i++) { if(i%5!=0&&i%3!=0) {a+=i;} } cout<<a; return 0; }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1000006; int a[maxn]; long long dp[maxn]; long long sum[maxn]; multiset<int> st; int main() { int n, c; scanf("%d%d", &n, &c); for (int i = 1; i <= n; i++) { scanf("%d", &a[i]); dp[i] = sum[i] = sum[i - 1] + a[i]; } for (int i = 1; i < c; i++) { st.insert(a[i]); } for (int i = c; i <= n; ++i) { st.insert(a[i]); dp[i] = min(dp[i - 1] + a[i], dp[i - c] + sum[i] - sum[i - c] - *st.begin()); st.erase(st.find(a[i - c + 1])); } printf("%lld\n", dp[n]); }
5
#include<iostream> #include<algorithm> #include<vector> #include<map> #include<set> #include<string> #include<queue> #include<stack> using namespace std; #define MON 1000000007 #define INF (1<<29) typedef long long Int; typedef pair<Int, Int> P; #define max(x, y) ((x)>(y)?(x):(y)) #define min(x, y) ((x)<(y)?(x):(y)) vector<Int> children[108000]; Int parent[108000][20]; Int dep[108000]; Int u, v, w, n, k, c, q; void dfs_dep(Int x){ for(auto c:children[x]){ dep[c] = dep[x]+1; dfs_dep(c); } } int lca(int u, int v, int check = 19){ while(check >= 0 && parent[u][check] == parent[v][check])check--; if(check == -1)return parent[u][0]; return lca(parent[u][check], parent[v][check]); } int ancestor(int u, int height){ for(int check = 19;check >= 0;check--){ if((1<<check) > height)continue; u = parent[u][check]; height -= (1<<check); } return u; } int main(){ cin >> n; for(Int i = 0;i < n;i++){ cin >> k; for(Int j = 0;j < k;j++){ cin >> c; children[i].push_back(c); parent[c][0] = i; } } dfs_dep(0); for(int i = 1;i < 20;i++){ for(int j = 0;j < n;j++){ parent[j][i] = parent[parent[j][i-1]][i-1]; } } cin >> q; while(q--){ cin >> u >> v; if(dep[u] < dep[v])swap(u, v); if(dep[u] != dep[v])u = ancestor(u, dep[u] - dep[v]); if(u == v)cout << u << endl; else cout << lca(u, v) << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; string str; struct node { string tt; int c; }; map<string, int> vis; node nodes[1000008]; int cnt; bool check(int dd, int mm) { if (dd <= 0) return false; if (mm <= 0) return false; if (mm == 2) { if (dd <= 28) return true; else return false; } if (mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12) { if (dd <= 31) return true; else return false; } if (mm == 2 || mm == 4 || mm == 6 || mm == 9 || mm == 11) { if (dd <= 30) return true; else return false; } return false; } void findS(int s) { string tmp; int dd, yy, mm; if (s < 2) return; if (str[s - 1] >= '0' && str[s - 1] <= '9' && str[s - 2] <= '9' && str[s - 2] >= '0') { dd = (int)(str[s - 2] - '0') * 10 + (int)(str[s - 1] - '0'); if (str[s + 1] >= '0' && str[s + 1] <= '9' && str[s + 2] >= '0' && str[s + 2] <= '9') { mm = (str[s + 1] - '0') * 10 + (str[s + 2] - '0'); if (!check(dd, mm)) return; if (str[s + 3] != '-') return; if (str[s + 4] >= '0' && str[s + 4] <= '9' && str[s + 5] >= '0' && str[s + 5] <= '9' && str[s + 6] >= '0' && str[s + 6] <= '9' && str[s + 7] >= '0' && str[s + 7] <= '9') yy = (str[s + 4] - '0') * 1000 + (str[s + 5] - '0') * 100 + (str[s + 6] - '0') * 10 + str[s + 7] - '0'; else return; if (yy < 2013 || yy > 2015) return; tmp.push_back(str[s - 2]); tmp.push_back(str[s - 1]); tmp.push_back(str[s + 1]); tmp.push_back(str[s + 2]); tmp.push_back(str[s + 4]); tmp.push_back(str[s + 5]); tmp.push_back(str[s + 6]); tmp.push_back(str[s + 7]); if (vis[tmp]) { nodes[vis[tmp]].c++; } else { vis[tmp] = ++cnt; nodes[vis[tmp]].c = 1; nodes[vis[tmp]].tt = tmp; } } } } bool cmp(node a, node b) { return a.c > b.c; } int main() { cin >> str; vis.clear(); cnt = 0; for (int i = 0; i < str.length(); i++) { if (str[i] == '-') { findS(i); } } sort(nodes + 1, nodes + 1 + cnt, cmp); cout << nodes[1].tt[0] << nodes[1].tt[1] << "-" << nodes[1].tt[2] << nodes[1].tt[3] << "-" << nodes[1].tt[4] << nodes[1].tt[5] << nodes[1].tt[6] << nodes[1].tt[7] << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; long long n; vector<long long> a, kq; void readinp() { a.clear(); for (long long i = 1; i <= (long long)(sqrt(n)); i++) if (n % i == 0) { a.push_back(i); if (i != n / i) a.push_back(n / i); } sort(a.begin(), a.end()); } void solv() { kq.clear(); for (auto k : a) { long long x = (n - 1) / k; long long s = 0; s += x; s += (x * (x + 1) / 2) * k; kq.push_back(s + 1); } } void printRes() { sort(kq.begin(), kq.end()); for (int i = 0; i < kq.size(); ++i) cout << kq[i] << " "; } int main() { cin >> n; readinp(); solv(); printRes(); return 0; }
3
#include <bits/stdc++.h> using namespace std; const long long N = 20; const long long LCM = 2520; long long f[N][LCM][50]; int tid[LCM + 10]; int b[N]; long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); } long long lcm(long long x, long long y) { return x * y / gcd(x, y); } long long work(int len, long long s, long long now, bool flag) { if (len == 0) return (s % now) == 0; if (flag == false && f[len][s][tid[now]] != -1) return f[len][s][tid[now]]; long long ans = 0; int ed; if (flag == true) ed = b[len]; else ed = 9; for (int i = 0; i <= ed; i++) { long long ns = (s * 10 + i) % LCM; long long nnow; if (i != 0) nnow = lcm(now, (long long)i); else nnow = now; ans += work(len - 1, ns, nnow, flag && i == ed); } if (flag == false) f[len][s][tid[now]] = ans; return ans; } long long solve(long long x) { int len = 0; while (x > 0) { b[++len] = x % 10; x = x / 10; } return work(len, 0, 1, true); } int main() { int test; scanf("%d", &test); memset(f, 255, sizeof(f)); int s = 1; for (int i = 1; i <= LCM; i++) if (LCM % i == 0) tid[i] = s++; while (test--) { long long l, r; scanf("%I64d%I64d", &l, &r); printf("%I64d\n", solve(r) - solve(l - 1)); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); int tux; cin >> tux; int foo = 0, bar = 0, baz = 0, quz = 1; for (int i = 1; i <= tux; ++i) { int pur; cin >> pur; foo += pur; ++bar; if (foo * quz > baz * bar) { baz = foo; quz = bar; } } cout << double(baz) / double(quz) << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { map<char, int> pode; pode['A'] = 1; pode['H'] = 1; pode['I'] = 1; pode['M'] = 1; pode['O'] = 1; pode['T'] = 1; pode['U'] = 1; pode['V'] = 1; pode['W'] = 1; pode['X'] = 1; pode['Y'] = 1; string a; cin >> a; int vai = 1; for (int i = 0; i < a.size(); i++) { if (pode[a[i]] == 0) vai = 0; } for (int i = 0; i < a.size() / 2; i++) { if (a[i] != a[a.size() - i - 1]) vai = 0; } if (vai) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; vector<int> graph[100005]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n; cin >> n; int a, b; for (int i = 0; i < 2 * n; i++) { cin >> a >> b; graph[a].push_back(b); graph[b].push_back(a); } for (int i = 1; i <= n; i++) { if (graph[i].size() != 4) { cout << -1; exit(0); } } vector<int> temp; vector<int> check(n + 1, 0); int done = 0; for (int i = 0; i < 3; i++) { for (int j = i + 1; j < 4; j++) { int num1 = graph[1][i]; int num2 = graph[1][j]; int exist1 = 0, exist2 = 0; for (int k = 0; k < 4; k++) { if (graph[num1][k] == num2) { exist1 = 1; } if (graph[num1][k] == 1) { exist2 = 1; } } if (exist1 && exist2) { int count1 = 0, count2 = 0; int use1 = 0, use2 = 0; for (int k = 0; k < 4; k++) { if (k != i && k != j) { if (use1 == 0) { use1 = graph[1][k]; } else { use2 = graph[1][k]; } } } for (int k = 0; k < 4; k++) { if (graph[use1][k] == num1 || graph[use1][k] == num2) { count1++; } } for (int k = 0; k < 4; k++) { if (graph[use2][k] == num1 || graph[use2][k] == num2) { count2++; } } int see1 = 0, see2 = 0; for (int k = 0; k < 4; k++) { if (graph[use1][k] == num1) { see1++; } } for (int k = 0; k < 4; k++) { if (graph[use2][k] == num1) { see2++; } } if (count1 == 1 && count2 == 1) { temp.push_back(num1); temp.push_back(1); temp.push_back(num2); } else if (see1 ^ see2) { temp.push_back(1); temp.push_back(num1); temp.push_back(num2); } else { temp.push_back(1); temp.push_back(num2); temp.push_back(num1); } check[1] = 1; check[num1] = 1; check[num2] = 1; done = 1; goto finish; } } } finish:; if (done == 0) { cout << -1; exit(0); } while (temp.size() < n) { int siz = temp.size(); int myNum = temp[siz - 1]; int is_it = temp[siz - 2]; int flag = 0; for (int k = 0; k < 4; k++) { if (check[graph[myNum][k]] == 0) { int hero = graph[myNum][k]; for (int m = 0; m < 4; m++) { if (graph[hero][m] == is_it) { temp.push_back(hero); check[hero] = 1; flag = 1; goto again; } } } } again:; if (!flag) { cout << -1; exit(0); } } for (int i = 0; i < n; i++) { cout << temp[i] << " "; } }
3
#include <bits/stdc++.h> using namespace std; int main() { string a, b; cin >> a; bool flag = false; for (int i = 0; i < 5; i++) { cin >> b; if (b[0] == a[0] || b[1] == a[1]) { flag = true; break; } } if (flag) cout << "YES"; else cout << "NO"; return 0; }
1
#include <bits/stdc++.h> using namespace std; class Graph { long long V; list<long long> *adj; long long DFSUtil(long long v, bool visited[], vector<long long> &imp, long long depth); public: Graph(long long V); void addEdge(long long v, long long w); vector<long long> DFS(long long v); }; Graph::Graph(long long V) { this->V = V; adj = new list<long long>[V]; } void Graph::addEdge(long long v, long long w) { adj[v].push_back(w); adj[w].push_back(v); } long long Graph::DFSUtil(long long v, bool visited[], vector<long long> &imp, long long depth) { visited[v] = true; long long count = 1; list<long long>::iterator i; for (i = adj[v].begin(); i != adj[v].end(); ++i) if (!visited[*i]) count += DFSUtil(*i, visited, imp, depth + 1); imp[v] = depth - count; return count; } vector<long long> Graph::DFS(long long v) { bool *visited = new bool[V]; vector<long long> imp(V); for (long long i = 0; i < V; i++) visited[i] = false; long long depth = 1; DFSUtil(v, visited, imp, depth); return imp; } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long n, k; cin >> n >> k; Graph g(n); for (long long i = 0; i < n - 1; i++) { long long u, v; cin >> u >> v; g.addEdge(u - 1, v - 1); } vector<long long> imp; imp = g.DFS(0); sort(imp.begin(), imp.end(), greater<long long>()); long long ans = 0; for (long long i = 0; i < k; i++) { ans += imp[i]; } cout << ans << endl; }
3
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:667772160") using namespace std; struct __isoff { __isoff() { if (0) freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout); srand('C' + 'T' + 'A' + 'C' + 'Y' + 'M' + 'B' + 'A'); } ~__isoff() {} } __osafwf; const unsigned long long p1 = 31; const unsigned long long p2 = 29; const double eps = 1e-8; const double pi = acos(-1.0); const int infi = 1e9 + 7; const long long inf = 1e18 + 7; const long long dd = 7e5 + 7; const long long mod = 1e9 + 7; const long long mod2 = 1e6 + 3; pair<int, int> TT; long long vec(pair<int, int> a, pair<int, int> b, pair<int, int> c) { a.first -= b.first; a.second -= b.second; c.first -= b.first; c.second -= b.second; return a.first * c.second - a.second * c.first; } long long dis(pair<int, int> a, pair<int, int> b) { a.first -= b.first; a.second -= b.second; return a.first * a.first + a.second * a.second; } bool cmp(pair<int, int> a, pair<int, int> b) { int t = vec(a, TT, b); if (t == 0) { return dis(a, TT) < dis(b, TT); } return t < 0; } vector<pair<int, int> > Con(vector<pair<int, int> > G) { int m = G.size(); for (long long i = 0; i < (long long)m; i++) { if (G[i] < G[0]) { swap(G[i], G[0]); } } TT = G[0]; sort(G.begin() + 1, G.end(), cmp); vector<pair<int, int> > E; for (long long i = 0; i < (long long)m; i++) { while (E.size() > 1 && vec(E[E.size() - 2], E[E.size() - 1], G[i]) <= 0) { E.pop_back(); } E.push_back(G[i]); } return E; } int main() { int n; while (cin >> n) { if (n == 0) return 0; vector<vector<int> > T(n + 2, vector<int>(n + 2)); vector<pair<int, int> > Z; for (long long i = 0; i < (long long)n; i++) { for (long long j = 0; j < (long long)n; j++) { char c; scanf(" %c", &c); T[i + 1][j + 1] = c - '0'; if (T[i + 1][j + 1] > 0 && T[i + 1][j + 1] < 4) { T[i + 1][j + 1] = 1; } else { T[i + 1][j + 1] = 0; } } } for (long long i = (long long)1; i < (long long)n + 1; i++) { for (long long j = (long long)1; j < (long long)n + 1; j++) { if (T[i][j]) { Z.push_back(make_pair(i, j)); } } } Z = Con(Z); for (long long i = 0; i < (long long)Z.size(); i++) { T[Z[i].first][Z[i].second] = 2; } reverse(T.begin(), T.end()); vector<pair<int, int> > G; for (long long i = (long long)1; i < (long long)n + 1; i++) { for (long long j = (long long)1; j < (long long)n + 1; j++) { if (T[i][j] == 2) { if (T[i - 1][j] > 0 && T[i][j - 1] > 0 && T[i + 1][j] == 0 && T[i][j + 1] == 0) { G.push_back(make_pair(i, j)); } if (T[i + 1][j] > 0 && T[i][j + 1] > 0 && T[i - 1][j] == 0 && T[i][j - 1] == 0) { G.push_back(make_pair(i + 1, j + 1)); } if (T[i + 1][j] > 0 && T[i][j - 1] > 0 && T[i - 1][j] == 0 && T[i][j + 1] == 0) { G.push_back(make_pair(i + 1, j)); } if (T[i - 1][j] > 0 && T[i][j + 1] > 0 && T[i + 1][j] == 0 && T[i][j - 1] == 0) { G.push_back(make_pair(i, j + 1)); } } } } for (long long i = 0; i < (long long)G.size(); i++) { G[i].first -= 1; G[i].second -= 1; swap(G[i].first, G[i].second); } sort(G.begin(), G.end()); G.resize(unique(G.begin(), G.end()) - G.begin()); auto E = Con(G); cout << E.size() << '\n'; for (long long i = 0; i < (long long)E.size(); i++) { cout << E[i].first << ' ' << E[i].second << '\n'; } } }
2
#include <bits/stdc++.h> using namespace std; double dp[2][5001]; double p[5001]; int ts[5001]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n, t; cin >> n >> t; dp[0][0] = 1; for (int i = 0; i < n; i++) { cin >> p[i] >> ts[i]; p[i] /= 100.0; } double v = 0; for (int i = 1; i <= n; i++) { for (int ii = 0; ii <= t; ii++) { dp[i % 2][ii] = 0; } double lol = pow((1.0 - p[i - 1]), (double)(ts[i - 1] - 1)); double vv = 0; for (int ii = 1; ii <= t; ii++) { if (ii >= ts[i - 1]) { dp[i % 2][ii] += lol * dp[(i - 1) % 2][ii - ts[i - 1]]; vv -= lol * dp[(i - 1) % 2][ii - ts[i - 1]]; } vv += dp[(i - 1) % 2][ii - 1]; dp[i % 2][ii] += vv * p[i - 1]; vv -= vv * p[i - 1]; if (t == ii || i == n) { v += (double)i * dp[i % 2][ii]; } } v += (double)(i - 1) * vv; } cout << setprecision(15) << v << endl; }
2
#include <bits/stdc++.h> using namespace std; const int N = 310; const int INF = 100000000; int n, v[N][N], dp[N * 2][N][N]; bool valid(int x, int y) { if (x <= 0 || x > n || y <= 0 || y > n) return false; return true; } bool same(int x1, int y1, int x2, int y2) { if (x1 == x2 && y1 == y2) return true; return false; } int main() { memset(dp, 0, sizeof(dp)); scanf("%d", &n); for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) scanf("%d", &v[i][j]); dp[0][1][1] = v[1][1]; for (int i = 1; i <= 2 * n - 2; i++) for (int x1 = 1; x1 <= n; x1++) for (int x2 = 1; x2 <= n; x2++) { int y1 = i + 2 - x1; int y2 = i + 2 - x2; int t = 0, ans = -INF; if (!same(x1, y1, x2, y2)) t = v[x1][y1] + v[x2][y2]; else t = v[x1][y1]; if (!valid(x1, y1) || !valid(x2, y2)) continue; if (valid(x1 - 1, y1)) { if (valid(x2 - 1, y2)) ans = max(ans, t + dp[i - 1][x1 - 1][x2 - 1]); if (valid(x2, y2 - 1)) ans = max(ans, t + dp[i - 1][x1 - 1][x2]); } if (valid(x1, y1 - 1)) { if (valid(x2 - 1, y2)) ans = max(ans, t + dp[i - 1][x1][x2 - 1]); if (valid(x2, y2 - 1)) ans = max(ans, t + dp[i - 1][x1][x2]); } dp[i][x1][x2] = ans; } printf("%d\n", dp[2 * n - 2][n][n]); return 0; }
5
#include <cstdio> #include <cstring> #include <algorithm> #define inf 0x3f3f3f3f #define lowbit(x) (x&-x) using namespace std; bool sumv[100005]; void add(int x) { for(;x;x-=lowbit(x)) sumv[x]^=1; } bool sum(int x,int n) { int s=0; for(;x<=n;x+=lowbit(x)) s^=sumv[x]; return s; } inline int check(int x,int y,int z) { if (x%3==0&&y==x-1&&z==x-2) return -(x/3); if (z%3==0&&y==z-1&&x==z-2) return z/3; return inf; } int num[3][100005],a[100005]; int main() { int n; scanf("%d",&n); for(int i=0;i<3;i++) for(int j=1;j<=n;j++) scanf("%d",&num[i][j]); bool s1=0,s2=0; for(int i=1;i<=n;i++) { int t=check(num[0][i],num[1][i],num[2][i]); if (t==inf||((i&1)^(t&1))) { puts("No"); return 0; } a[i]=abs(t); if ((i&1)&&t<0) s1^=1; if ((!(i&1))&&t<0) s2^=1; } bool s3=0,s4=0; for(int i=1;i<=n;i+=2) { s3^=sum(a[i],n); add(a[i]); } memset(sumv,0,sizeof(sumv)); for(int i=2;i<=n;i+=2) { s4^=sum(a[i],n); add(a[i]); } puts((s1==s4&&s2==s3)?"Yes":"No"); return 0; }
0
#include<bits/stdc++.h> using namespace std; int main(){ int k; cin>>k; int a=7%k; for(int i=1;i<=k;i++){ if(a==0){ cout<<i<<endl; return 0; } a=(a*10+7)%k; } cout<<-1<<endl; }
0
#include <bits/stdc++.h> using namespace std; struct node { int id, day; friend bool operator<(node a, node b) { return a.day < b.day; } } s[100005]; int n, m, a[100005], b[100005], sign[100005]; int judge(int mid) { int i, j, tmp; memset(sign, 0, sizeof(sign)); for (i = 1; i <= mid; i++) { if (a[i] == 0) continue; s[a[i]].id = a[i]; s[a[i]].day = i; sign[a[i]] = 1; } sort(s + 1, s + m + 1); tmp = s[0].day = 0; for (i = 1; i <= m; i++) { if (sign[i] == 0) return 0; tmp += (s[i].day - s[i - 1].day - 1); if (tmp < b[s[i].id]) return 0; tmp -= b[s[i].id]; } return 1; } int main() { int i, j, l, r, ans, mid; while (scanf("%d%d", &n, &m) != EOF) { for (i = 1; i <= n; i++) scanf("%d", &a[i]); for (i = 1; i <= m; i++) scanf("%d", &b[i]); ans = -1; l = 1, r = n; while (l <= r) { mid = (l + r) >> 1; if (judge(mid)) { ans = mid; r = mid - 1; } else l = mid + 1; } printf("%d\n", ans); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int a[10005]; int main(){ int n;long long s=0;cin>>n; for(int i=0;i<n;i++)cin>>a[i],s+=a[i]; sort(a,a+n); printf("%d %d %lld\n",a[0],a[n-1],s); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); ; int n, m, max = 0, count = 1; cin >> n; int a[n]; for (long long int x = 0; x < n; x++) cin >> a[x]; cin >> m; int b[m]; for (long long int k = 0; k < m; k++) cin >> b[k]; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) if ((b[j] % a[i] == 0)) { if ((b[j] / a[i]) == max) count++; else if ((b[j] / a[i]) > max) { count = 1; max = b[j] / a[i]; } } cout << count; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5, M = 2e4 + 5, inf = 0x3f3f3f3f, mod = 1e9 + 7; int a[N]; int main() { int t, n, k, z; scanf("%d", &t); while (t--) { scanf("%d%d%d", &n, &k, &z); int sum = 0, ans = 0, mx = 0; k++; for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= k; i++) { sum += a[i]; if (i < n) mx = max(mx, a[i] + a[i + 1]); ans = max(ans, min((k - i) / 2, z) * mx + sum); } printf("%d\n", ans); } return 0; }
2
#define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <cstdio> #include <cstring> #include <ctime> #include <string> #include <vector> #include <algorithm> #include <cmath> #include <sstream> #include <utility> using namespace std; typedef long long ll; typedef vector<int> vi; typedef vector<vi> vvi; typedef pair<int,int> pii; #define all(c) (c).begin(), (c).end() #define loop(i,a,b) for(ll i=a; i<ll(b); i++) #define rep(i,b) loop(i,0,b) #define each(e,c) for(auto&e:c) #define pb push_back #define eb emplace_back #define mp make_pair #define mt make_tuple #define lb lower_bound #define ub upper_bound #ifdef DEBUG #define dump(...) (cerr<<#__VA_ARGS__<<" = "<<(DUMP(),__VA_ARGS__).str()<<" ["<<__LINE__<<"]"<<endl) struct DUMP:ostringstream{template<class T>DUMP &operator,(const T&t){if(this->tellp())*this<<", ";*this<<t;return *this;}}; #else #define dump(...) #endif template<class T> ostream& operator<<(ostream& os, vector<T> const& v){ rep(i,v.size()) os << v[i] << (i+1==(int)v.size()?"":" "); return os; } #include <complex> #include <cstdio> #include <string> #include <iostream> #include <vector> #include <algorithm> using namespace std; #define curr(g,i) g[i] #define next(g,i) g[(i+1)%g.size()] #define self (*this) // 実数 typedef long double R; // 実定数 const R eps = 1e-10; const R inf = 1e12; const R pi = acos(-1); // 点 typedef complex<R> P; // A->B->Cの順に廻るときの角ABCの大きさを[0,2*Pi]で求める R arg(P a, P b, P c) { R th = arg((a - b) / (c - b)); return th > 0 ? th : th+2*pi; } // 点のx座標優先の比較関数 bool comp_x(P const& a, P const& b) { return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b); } // 点のy座標優先の比較関数 bool comp_y(P const& a, P const& b) { return imag(a) != imag(b) ? imag(a) < imag(b) : real(a) < real(b); } // 点の偏角の比較関数 P CENTER = P(); bool comp_arg(P const& a, P const& b) { return arg(a, CENTER, CENTER+(R)1.) < arg(b, CENTER, CENTER+(R)1.); } // 点のデフォルトの比較関数 namespace std { bool operator < (P const& a, P const& b) { return comp_x(a, b); } } // 外積 R cross(P const& a, P const& b) { return imag(conj(a)*b); } // 内積 R dot(P const& a, P const& b) { return real(conj(a)*b); } // 直線 struct L : public vector<P> { L(P const& a = P(), P const& b = P()) { push_back(a); push_back(b); } L(R a, R b, R c){ // ax+by+c=0 if(fabs(a) < eps) self = L(P(0,-c/b),P(1,-c/b)); else if(fabs(b) < eps) self = L(P(-c/a,0),P(-c/a,1)); else self = L(P(-c/a,0), P(0,-c/b)); } P vec() const { return self[1] - self[0]; } }; // 線分 struct S : public vector<P> { // A to B S(P const& a = P(), P const& b = P()) { push_back(a); push_back(b); } L line() const { return L(self[0], self[1]); } P vec() const { return line().vec(); } R len() const { return abs(vec()); } }; // 多角形 typedef vector<P> G; // 円 struct C { P p; R r; C(P const& p = P(), R r = 0) : p(p), r(r) { } }; // A->B->C と巡るときの位置関係を調べる enum { CCW = +1, CW = -1, CAB = +2, ABC = -2, SEG = 0 }; int ccw(P a, P b, P c) { b -= a; c -= a; if (cross(b, c) > 0) return CCW; // a->b->c 反時計回り if (cross(b, c) < 0) return CW; // a->b->c 時計回り if (dot(b, c) < 0) return CAB; // c--a--b 直線上 if (norm(b) < norm(c)) return ABC; // a--b--c 直線上 return SEG; // 線分上にある } // 円の面積 R area(C const& c) { return pi*c.r*c.r; } // 多角形の面積 R area(G const& g) { R res = 0; int n = g.size(); rep(i,n) res += cross(g[i],g[(i+1)%n]); return abs(res)/2; } // ヘロンの公式 R heron(R a, R b, R c) { R s = (a+b+c)/2; return sqrt(s*(s-a)*(s-b)*(s-c)); } P biased_center(P const& a, P const& b, P const& c, R wa, R wb, R wc) { return (wa*a + wb*b + wc*c)/(wa + wb + wc); } // http://www.h6.dion.ne.jp/~ooya/Suugaku/IchiVector5shin.pdf // 重心 P gravity_center(P const& a, P const& b, P const& c) { return biased_center(a,b,c,1,1,1); } // 内心 P inner_center(P const& a, P const& b, P const& c) { R la = abs(b-c), lb = abs(c-a), lc = abs(a-b); return biased_center(a,b,c,la,lb,lc); } // 外心 P circumcenter(P const& a, P const& b, P const& c) { R sa = sin(2*arg(c,a,b)), sb = sin(2*arg(a,b,c)), sc = sin(2*arg(b,c,a)); return biased_center(a,b,c,sa,sb,sc); } // 垂心 P orthocenter(P const& a, P const& b, P const& c) { if(abs(arg(c,a,b))==pi) return a; if(abs(arg(a,b,c))==pi) return b; if(abs(arg(b,c,a))==pi) return c; R ta = tan(arg(c,a,b)), tb = tan(arg(a,b,c)), tc = tan(arg(b,c,a)); return biased_center(a,b,c,ta,tb,tc); } // 傍心(3つのうち頂点Aに対応するものを返す) P excenter(P const& a, P const& b, P const& c) { R sa = sin(arg(c,a,b)), sb = sin(arg(a,b,c)), sc = sin(arg(b,c,a)); return biased_center(a,b,c,-sa,sb,sc); } // 直線と直線の交差判定 bool col(L const& l, L const& m) { return abs(cross(l.vec(), m.vec())) > eps || abs(cross(l.vec(), m[0]-l[0])) < eps; } // 直線と線分の交差判定 bool col(L const& l, S const& s) { return cross(l.vec(), s[0]-l[0])*cross(l.vec(), s[1]-l[0]) < eps; } bool col(S const& s, L const& l) { return col(l, s); } // 直線と点の交差判定 bool col(L const& l, P const& p) { return abs(cross(l[1]-p, l[0]-p)) < eps; } bool col(P const& p, L const& l) { return col(l, p); } // 線分と線分の交差判定 bool col(const S &s, const S &t) { return ccw(s[0],s[1],t[0])*ccw(s[0],s[1],t[1]) <= 0 && ccw(t[0],t[1],s[0])*ccw(t[0],t[1],s[1]) <= 0; } // 線分と点の交差判定 bool col(const S &s, const P &p) { return abs(s[0]-p)+abs(s[1]-p)-abs(s[1]-s[0]) < eps; } // 円と円の交差判定 int col(const C& c1, const C& c2){ R d = abs(c1.p - c2.p), r1 = c1.r, r2 = c2.r; if(r1 + r2 < d) return 0; // 離れている if(abs(r1 + r2 - d) < eps) return 1; // 外接 if(abs(d - abs(r1 - r2)) < eps) return -1; // 内接 if(d < r1 - r2) return +3; // c1 が c2 の中にある if(d < r2 - r1) return -3; // c2 が c1 の中にある return 2; // 2つの交点を持つ } // 点の射影 P proj(L const& l, P const& p) { R t = dot(p-l[0], l.vec()) / norm(l.vec()); return l[0] + t*(l.vec()); } P proj(P const& p, L const& l) { return proj(l, p); } // 点の反射 P refl(L const& l, P const& p) { return p + (R)2 * (proj(l, p) - p); } P refl(P const& p, L const& l) { return refl(l, p); } // 点と点の距離 R dist(P const& p, P const& q) { return abs(p - q); } // 点と直線の距離 R dist(L const& l, P const& p) { return abs(p - proj(l, p)); } R dist(P const& p, L const& l) { return dist(l, p); } // 直線と直線の距離 R dist(L const& l, L const& m) { return col(l, m) ? 0 : dist(l, m[0]); } // 直線と線分の距離 R dist(L const& l, S const& s) { return col(l, s) ? 0 : min(dist(l, s[0]), dist(l, s[1])); } R dist(S const& s, L const& l) { return dist(l, s); } // 線分と点の距離 R dist(S const& s, P const& p) { P const r = proj(s.line(), p); return col(s, r) ? abs(r - p) : min(abs(s[0] - p), abs(s[1] - p)); } R dist(P const& p, S const& s) { return dist(s,p); } // 線分と線分の距離 R dist(S const& s, S const& t) { return col(s, t) ? 0 : min(min(min(dist(s, t[0]), dist(s, t[1])), dist(t, s[0])), dist(t, s[1])); } // 直線と直線の交点 vector<P> hit(L const& l, L const& m) { R A = cross(l.vec(), m.vec()); R B = cross(l.vec(), l[1] - m[0]); if (abs(A) < eps && abs(B) < eps) return {m[0]}; // same line if (abs(A) < eps) return G(); // parallel return {m[0] + B / A * (m.vec())}; } // 線分と線分の交点 vector<P> hit(S const& s, S const& t) { return col(s,t) ? hit(s.line(),t.line()) : G(); } // 直線と線分の交点 vector<P> hit(L const& l, S const& s) { return col(l, s) ? hit(l, s.line()) : G(); } vector<P> hit(S const& s, L const& l) { return hit(l, s); } // Verify : http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1183 // : http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2045 // 円と直線の交点を求める vector<P> hit(C const& c, L const& l){ R d = dist(l,c.p); // 中心と直線の距離 if(fabs(d-c.r) < eps) return { proj(l, c.p) }; // 触れている if(d > c.r) return G(); // 離れている P h = proj(l, c.p); P u = sqrt(c.r*c.r - d*d) * (l.vec()) / abs(l.vec()); return {h+u, h-u}; } // 円と線分の交点を求める vector<P> hit(C const& c, S const& s) { G cps = hit(c,s.line()), res; for(const P& p : cps) if(col(s,p)) res.push_back(p); return res; } // 円と円の交点を求める vector<P> hit(C const& c1, C const& c2) { if(abs(c1.p - c2.p) < 0) return G(); // 中心が同じ int i = col(c1,c2); if(i==0 || abs(i)==3) return G(); // 共通部分なし || 内部 R r1 = c1.r, r2 = c2.r, d = abs(c1.p - c2.p); if(i==1) return { c1.p + (c2.p - c1.p) * r1/d }; // 接する P p = c1.p - c2.p; R A = -2. * p.real(), B = 2 * p.imag(); R C = norm(c1.p) - norm(c2.p) - r1*r1 + r2*r2; return hit(c1, L(A,B,C)); // 2つの交点を持つ } // 円と多角形の交点を求める vector<P> hit(C const& c, G const& g) { vector<P> res; int n = g.size(); rep(i,n) for(P const& p : hit(c, S(g[i],g[(i+1)%n]))) res.push_back(p); return res; } // !!! not verified !!! C min_ball(G const& g){ int n = g.size(); if(n==1) return C(g[0],0); if(n==2) return C((g[0]+g[1])/(R)2, abs((g[0]+g[1])/(R)2-g[0])); P c = (g[0]+g[n/3]+g[n/3*2])/(R)3; R r = 1.0; rep(i,100){ R d = 0; int x = 0; rep(j,n){ if(d < norm(g[j]-c)){ d = norm(g[j]-c); x = j; } } c += (g[x]-c)*r; if(i&2) r *= 0.8; } R d = 0; int x = 0; rep(i,n){ if(d < norm(c-g[i]) + eps){ d = norm(c-g[i]); x = i; } } return C(c,abs(c-g[x])); } int n,m; R r[111], mb[111]; vi ans; bool used[111]; bool chk(int s){ vi b; rep(i,n)if(!used[i]) b.eb(r[i]); vector<R> a; loop(i,s,m) a.eb(mb[i]); sort(all(a)); sort(all(b)); int last = -1; rep(i,a.size()){ bool found = false; loop(j,last+1,b.size()){ if(a[i] < b[j] + eps){ found = true; last = j; break; } } if(!found)return false; } return true; } int main(){ #ifdef LOCAL freopen("in","r",stdin); #endif while(cin >> n >> m){ ans.clear(); rep(i,n) cin >> r[i]; rep(i,m){ int k; cin >> k; G g(k); rep(j,k){ R x,y; cin >> x >> y; g[j] = P(x,y); } mb[i] = min_ball(g).r; } rep(i,m) used[i] = false; bool f = true; rep(i,m){ bool found = false; rep(j,n){ if(used[j]) continue; if(!(mb[i] < r[j] + eps)) continue; used[j] = true; if(chk(i+1)){ ans.push_back(j); found = true; break; } used[j] = false; } if(!found){ f = false; break; } } if(f){ rep(i,m) cout << ans[i]+1 << "\n"; } else cout << "NG" << endl; } }
0
#include <bits/stdc++.h> using namespace std; char a[100005], res[100005]; int main() { int n, m; scanf("%d\n", &n); for (int i = 0; i <= 100000; i++) res[i] = '.'; for (int i1 = 1; i1 <= n; i1++) { if (i1 != n) scanf("%s\n", &a); else scanf("%s", &a); m = strlen(a); for (int i = 0; i < m; i++) { if (a[i] == '?') continue; if (res[i] == '.') res[i] = a[i]; else if (res[i] != a[i]) { res[i] = '?'; } } } for (int i = 0; i < m; i++) { if (res[i] == '.') printf("x"); else printf("%c", res[i]); } printf("\n"); return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; string W = "W"; string B = "B"; int flag; for (int i = 1; i <= n; i++) { if (i % 2 == 0) flag = 2; else flag = 1; for (int j = 1; j <= n; j++) { if (flag == 1) { cout << W; flag = 2; } else { cout << B; flag = 1; } } cout << endl; } return 0; }
2
#include<iostream> using namespace std; int main(){ int sum[6], n; double in; for(int i = 0; i < 6; i++){ sum[i] = 0; } cin >> n; for(int i = 0; i < n; i++){ cin >> in; if(in >= 185){ sum[5]++; }else if(in >= 180){ sum[4]++; }else if(in >= 175){ sum[3]++; }else if(in >= 170){ sum[2]++; }else if(in >= 165){ sum[1]++; }else{ sum[0]++; } } for(int i = 0; i <= 5; i++){ cout << i+1 << ":"; for(int j = 0; j < sum[i]; j++){ cout << "*"; } cout <<endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const int MOD = 1000000007; const int N = 1000005; const double PI = 4 * atan(1); long long a[N]; long long n, x, y, q; long long k; vector<long long> v; long long res = 0; long long dp[N]; long long l, r; long long get(long long curr) { long long pos = upper_bound(v.begin(), v.end(), curr) - v.begin(); long long to_return = x + r - (l + y) + 1; if (v[pos] > 1e18) { return to_return; } long long troll = ((pos == 0) ? 0 : dp[pos - 1]); return to_return - ((res - troll) - (n - 1 - pos) * curr); } int main() { ios::sync_with_stdio(0); cin >> n; x = 0; y = 1e18; for (int i = 0; i < n; i++) { cin >> a[i]; x = max(x, a[i]); y = min(y, a[i]); } sort(a, a + n); res = 0; for (int i = 1; i < n; i++) { if (i != 0 && a[i] == a[i - 1]) continue; v.push_back(a[i] - a[i - 1] - 1); res += a[i] - a[i - 1] - 1; } v.push_back(2 * 1e18); sort((v).begin(), (v).end()); n = v.size(); for (int i = 0; i < n - 1; i++) { if (i == 0) dp[i] = v[i]; else dp[i] = v[i] + dp[i - 1]; } cin >> q; for (int i = 0; i < q; i++) { cin >> l >> r; if (l == r) { cout << n << endl; } else cout << get(r - l) << endl; } return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long int n, z, i, j; cin >> n; z = sqrt(n - 1); long long int x = (z + 1) * (z + 1) - z * z; z = z * z; if (z + x / 2 >= n) { i = n - z; j = sqrt(z) + 1; } else { i = sqrt(z) + 1; j = x - (n - z) + 1; } cout << i << " " << j; cout << "\n"; } }
3
#include <iostream> #include <string> #include <algorithm> #include <functional> #include <vector> #include <utility> #include <cstring> #include <iomanip> #include <numeric> #include <limits> #include <cmath> #include <cassert> using namespace std; using ll = long long; const int INF = 1<<30; const int MOD = (int)1e9 + 7; const int MAX_N = (int)1e5 + 5; #define debug(x) cout << #x << ": " << x << endl signed main(void) { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; int d = INF, ans = 0; for(int i = 0; i <= 10000; i++) { string s = to_string(i), t = to_string(i); reverse(t.begin(), t.end()); if(s == t) { if(abs(stoi(s) - n) < d) { d = abs(stoi(s) - n); ans = i; } } } cout << ans << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long MAXN = 1e2 + 10; const long long INF = 1e9 + 10; vector<long long> dag[MAXN][MAXN]; vector<long long> f[MAXN]; vector<long long> e[MAXN]; vector<long long> in[MAXN]; vector<long long> out[MAXN]; queue<long long> q; long long s[MAXN]; long long t[MAXN]; long long z[MAXN]; long long w[MAXN]; long long sp[MAXN]; long long dist[2][MAXN]; long long n; bool cmp(long long x, long long y) { return (dist[0][x] < dist[0][y]); } void bfs(long long id) { fill(dist[0], dist[0] + MAXN, INF); dist[0][s[id]] = 0; q.push(s[id]); while (!q.empty()) { long long v = q.front(); q.pop(); for (long long i = 0; i < out[v].size(); i++) { if (dist[0][out[v][i]] == INF) { dist[0][out[v][i]] = dist[0][v] + 1; q.push(out[v][i]); } } } fill(dist[1], dist[1] + MAXN, INF); dist[1][t[id]] = 0; q.push(t[id]); while (!q.empty()) { long long v = q.front(); q.pop(); for (long long i = 0; i < in[v].size(); i++) { if (dist[1][in[v][i]] == INF) { dist[1][in[v][i]] = dist[1][v] + 1; q.push(in[v][i]); } } } fill(z, z + MAXN, 0); for (long long i = 1; i <= n; i++) { if (dist[0][i] != INF && dist[1][i] != INF && dist[0][i] + dist[1][i] == dist[0][t[id]]) { if (z[dist[0][i]] == 0) { z[dist[0][i]] = i; } else { z[dist[0][i]] = -1; } e[id].push_back(i); for (long long j = 0; j < out[i].size(); j++) { if (dist[0][out[i][j]] != INF && dist[1][out[i][j]] != INF && dist[0][out[i][j]] + dist[1][out[i][j]] == dist[0][t[id]]) { dag[id][i].push_back(out[i][j]); } } } } for (long long i = 0; i < n; i++) { if (z[i] > 0) { f[id].push_back(z[i]); } } sort(e[id].begin(), e[id].end(), cmp); } int main() { ios_base ::sync_with_stdio(false); cin.tie(0); long long m, a, b; cin >> n >> m >> a >> b; for (long long i = 0; i < m; i++) { long long v, u; cin >> v >> u; out[v].push_back(u); in[u].push_back(v); } long long k; cin >> k; for (long long i = 1; i <= k; i++) { cin >> s[i] >> t[i]; bfs(i); } fill(sp, sp + MAXN, INF); sp[b] = 0; for (long long i = 0; i < n + 2; i++) { for (long long j = 1; j <= k; j++) { fill(w, w + MAXN, -1); for (long long u = (long long)(e[j].size()) - 1; u >= 0; u--) { long long v = e[j][u]; for (long long d = 0; d < dag[j][v].size(); d++) { w[v] = max(w[v], w[dag[j][v][d]]); } if (w[v] == -1) { w[v] = INF; } w[v] = min(w[v], sp[v]); } for (long long u = 0; u < f[j].size(); u++) { sp[f[j][u]] = min(sp[f[j][u]], w[f[j][u]] + 1); } } } if (sp[a] == INF) { cout << -1; } else { cout << sp[a]; } }
5
#include <bits/stdc++.h> using namespace std; int N, A[100], i, j; bool w; string S; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> N; for (i = 0; i < N; i++) { cin >> A[i]; } getline(cin, S); for (j = 0; j < N; j++) { getline(cin, S); if (!w) { for (i = S.length() - 1; i >= 0; i--) { if (S[i] == 'a' || S[i] == 'i' || S[i] == 'u' || S[i] == 'e' || S[i] == 'o' || S[i] == 'y') { A[j]--; } } if (A[j] != 0) { w = 1; } } } if (w) { cout << "NO" << endl; } else cout << "YES" << endl; }
2
#include <bits/stdc++.h> using namespace std; ofstream fo("test.out"); ifstream fi("test.inp"); long long n, m, k, rs, dem; long long a[100005]; int main() { cin >> n >> m >> k; for (int i = 0; i < n; i++) { cin >> a[i]; if (a[i] == 1) { if (m > 0) m--; else rs++; } if (a[i] == 2) { if (k > 0) k--; else { if (m > 0) m--; else rs++; } } } cout << rs; }
1
#include <bits/stdc++.h> using namespace std; #define rep(i, n) for(int i = 0; i < (int)n; ++i) #define FOR(i, a, b) for(int i = a; i < (int)b; ++i) typedef long long ll; const int Inf = 1e9; const double EPS = 1e-9; int main() { cin.tie(nullptr); ios::sync_with_stdio(0); int n, m; cin >> n >> m; int res = 0; vector<int> a(m), b(m), c(m); vector<vector<int> > g(n, vector<int>(n, Inf)); rep (i, n) g[i][i] = 0; rep (i, m) { cin >> a[i] >> b[i] >> c[i]; --a[i], --b[i]; g[a[i]][b[i]] = g[b[i]][a[i]] = c[i]; } rep (k, n) rep (i, n) rep (j, n) g[i][j] = min(g[i][j], g[i][k] + g[k][j]); rep (i, m) { if (g[a[i]][b[i]] < c[i]) res++; } cout << res << endl; return 0; }
0
#include <stdio.h> #define N 100005 int n, m, i, j, k, a, b, f[N], g[N]; int main(){ scanf("%d%d", &n, &m); for(i=1; i<=n; i++){ scanf("%d", &g[i]); } for(i=1; i<=m; i++){ scanf("%d%d", &a, &b); if(g[a]>=g[b] && ++f[b]==1) n--; if(g[b]>=g[a] && ++f[a]==1) n--; } printf("%d\n", n); return 0; }
0
#include <stdio.h> char a[5]; int main(){ scanf("%s",a);if(a[0]==a[1]&&a[1]==a[2]&&a[0]==a[2]) puts("No");else puts("Yes");return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, l; cin >> n >> l; int a[100]; cin >> a[0]; int m, d(l); m = a[0]; for (int i(1); i < n; i++) { cin >> a[i]; if (a[i] > m) m = a[i]; } int s(0), s1; for (int i(l); i <= m; i++) { s1 = 0; for (int j(0); j < n; j++) s1 += a[j] / i; if (s1 * i > s * d) { s = s1; d = i; }; } cout << s * d; cout << "\n"; cin >> s; }
3
#include <bits/stdc++.h> using namespace std; const int mxN = 2e5; int n, a[mxN], k; void solve() { long long n; cin >> n; if (n == 0) { cout << 1 << endl; return; } if (n % 4 == 1) { cout << 8 << endl; } else if (n % 4 == 2) { cout << 4 << endl; } else if (n % 4 == 3) { cout << 2 << endl; } else if (n % 4 == 0) { cout << 6 << endl; } } int main() { ios::sync_with_stdio(0); cin.tie(0); solve(); }
1
#include <bits/stdc++.h> int f[105][105][12], g[105][105][12]; int map[105][105]; int n, m, k; void print(int x, int y, int p) { if (x > n) return; if (x == n) { printf("%d\n", y); return; } if (g[x][y][p] == 1) { print(x + 1, y + 1, (f[x][y][p] - map[x][y]) % k); printf("L"); } else if (g[x][y][p] == 2) { print(x + 1, y - 1, (f[x][y][p] - map[x][y]) % k); printf("R"); } } int main() { int cur, ans, now; int i, j, p; char ch; scanf("%d%d%d", &n, &m, &k); for (i = 1; i <= n; i++) { getchar(); for (j = 1; j <= m; j++) { scanf("%c", &ch); map[i][j] = ch - '0'; } } memset(f, -1, sizeof(f)); k++; for (i = 1; i <= m; i++) f[n + 1][i][0] = 0; for (i = n; i >= 1; i--) { for (j = 1; j <= m; j++) for (p = 0; p < k; p++) { if (f[i + 1][j - 1][p] > -1) { cur = (p + map[i][j]) % k; if (f[i + 1][j - 1][p] + map[i][j] > f[i][j][cur]) { f[i][j][cur] = f[i + 1][j - 1][p] + map[i][j]; g[i][j][cur] = 2; } } if (f[i + 1][j + 1][p] > -1) { cur = (p + map[i][j]) % k; if (f[i + 1][j + 1][p] + map[i][j] > f[i][j][cur]) { f[i][j][cur] = f[i + 1][j + 1][p] + map[i][j]; g[i][j][cur] = 1; } } } } ans = -1; for (i = 1; i <= m; i++) if (f[1][i][0] > ans) { ans = f[1][i][0]; now = i; } printf("%d\n", ans); if (ans != -1) { print(1, now, 0); printf("\n"); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int lenA, lenB, num[200050]; char A[200050], B[200050]; int nUm(int L, int R) { return L ? num[R] - num[L - 1] : num[R]; } int NuM(int L, int R) { return R - L + 1 - nUm(L, R); } long long ans = 0; int main() { int l, r; scanf("%s%s", A, B); lenA = strlen(A); lenB = strlen(B); num[0] = (A[0] == '1'); for (int i = 1; i < lenA; i++) num[i] = num[i - 1] + (A[i] == '1'); for (int i = 0; i < lenB; i++) { r = min(i, lenA - 1); l = max(0, lenA - lenB + i); if (B[i] == '1') ans += NuM(l, r); else ans += nUm(l, r); } cout << ans; return 0; }
2
#include<bits/stdc++.h> using namespace std; #define FIO ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0) #define mod 1000000007 #define endl "\n" #define test ll t; cin>>t; while(t--) typedef long long int ll; map<ll,ll>mp; void solve(vector<ll>&a,ll sz){ ll sum=0; ll minm=mod,maxm=0; for(auto it:a){ sum+=it; maxm=max(maxm,it); minm=min(minm,it); } mp[sum]++; ll mid=(minm+maxm)/2; vector<ll>b,c; for(auto it:a){ if(it>mid){ b.push_back(it); } else{ c.push_back(it); } } ll n=(ll)c.size(); ll m=(ll)b.size(); if( n==0 || m==0){ return; } solve(b,m); solve(c,n); } int main() { FIO; test { ll n,q; cin>>n>>q; mp.clear(); vector<ll>a(n); for(ll i=0;i<n;i++){ cin>>a[i]; } solve(a,n); for(ll i=1;i<=q;i++){ ll x; cin>>x; if(mp.find(x)!=mp.end()){ cout<<"Yes\n"; } else{ cout<<"No\n"; } } } return 0; }
4
#include <iostream> #include <cstdio> #include <cstdlib> #include <math.h> struct Vector { double x, y; }; void Koch(Vector p1, Vector p2, int i) { if (i < 1)return; Vector s, t, u; s.x = (2 * p1.x + p2.x) / 3; s.y = (2 * p1.y + p2.y) / 3; u.x = (p1.x + 2 * p2.x) / 3; u.y = (p1.y + 2 * p2.y) / 3; const double root3 = sqrt(3); t.x = (u.x - s.x) / 2 - (u.y - s.y) * root3 / 2; t.x += s.x; t.y = (u.x - s.x) * root3 / 2 + (u.y - s.y) / 2; t.y += s.y; Koch(p1, s, i - 1); printf("%.8f %.8f\n", s.x, s.y); Koch(s, t, i - 1); printf("%.8f %.8f\n", t.x, t.y); Koch(t, u, i - 1); printf("%.8f %.8f\n", u.x, u.y); Koch(u, p2, i - 1); } int main() { int n; std::cin >> n; printf("%.8f %.8f\n", 0.0, 0.0); Koch({ 0,0 }, { 100,0 }, n); printf("%.8f %.8f\n", 100.0, 0.0); return 0; }
0
#include <bits/stdc++.h> using namespace std; int a[101000], g[101000]; int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = n; i >= 1; i--) g[i] = max(g[i + 1], a[i]); for (int i = 1; i <= n; i++) { if (a[i] > g[i + 1]) printf("0 "); else printf("%d ", g[i + 1] + 1 - a[i]); } printf("\n"); }
2
#include <bits/stdc++.h> using namespace std; char c[105][105]; int xy[4][2] = {-1, 0, 1, 0, 0, -1, 0, 1}; int n, m; void solve(int p, int q) { c[p][q] = '.'; for (int i = 0; i < 4; i++) { int x = p + xy[i][0]; int y = q + xy[i][1]; if (x >= 0 && x < n && y >= 0 && y < m && c[x][y] == 'B') { solve(x, y); } } return; } int main() { cin >> n >> m; for (int i = 0; i < n; i++) { scanf("%s", c[i]); } int ans = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (c[i][j] == 'B') { ans++; solve(i, j); } } } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { int n, k; cin >> n; int p1[n + 5], p2[n + 5], link[n + 5]; stack<int> st; memset(p1, 0, sizeof p1); memset(p2, 0, sizeof p2); for (int i = 1; i < n; ++i) { cin >> k; p1[i] = p1[i - 1] + k; } for (int i = 1; i < n; ++i) { cin >> k; st.push(k); } for (int i = n - 2; i >= 0; --i) { p2[i] = p2[i + 1] + st.top(); st.pop(); } for (int i = 0; i < n; ++i) cin >> link[i]; int arr[n + 5]; for (int i = 0; i < n; ++i) { arr[i] = p1[i] + link[i] + p2[i]; } sort(arr, arr + n); cout << arr[0] + arr[1]; }
2
#include <bits/stdc++.h> const int INF = 0x3f3f3f3f; using namespace std; int main() { int t; cin >> t; while (t--) { int sum; cin >> sum; int threeMax = sum / 3; int sevenMax = sum / 7; bool notFound = true; for (int i = 0; i <= threeMax && notFound; i++) { int remainingAfterThree = sum - i * 3; int fiveMax = remainingAfterThree / 5; for (int j = 0; j <= fiveMax && notFound; j++) { int remainingAfterFive = remainingAfterThree - j * 5; int sevenMax = remainingAfterFive / 7; for (int k = 0; k <= sevenMax && notFound; k++) { if (remainingAfterFive == 7 * k) { cout << i << " " << j << " " << k << endl; notFound = false; } } } } if (notFound) { cout << "-1" << endl; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int i, j, k, n, ans = 0; cin >> n; pair<long long int, long long int> a[n]; long long int b[n]; for (i = 0; i < n; i++) { cin >> a[i].first; a[i].second = i; } sort(a, a + n); j = 1; for (i = 0; i < n; i++) { b[a[i].second] = max(a[i].first, j); j = b[a[i].second] + 1; } for (i = 0; i < n; i++) cout << b[i] << " "; return 0; }
3
#include<bits/stdc++.h> using ll = int_fast64_t; using P = std::pair<ll,ll>; int main(){ int n, m; scanf("%d %d", &n, &m); std::vector<ll> a(n), b(n), c(m), d(m); for(int i=0; i<n; i++) scanf("%ld %ld", &a[i], &b[i]); for(int i=0; i<m; i++) scanf("%ld %ld", &c[i], &d[i]); for(int i=0; i<n; i++){ int mind = 0; for(int j=0; j<m; j++){ ll ne = abs(a[i]-c[j]) + abs(b[i]-d[j]); if(abs(a[i]-c[mind])+abs(b[i]-d[mind]) > ne) mind = j; } printf("%d\n", mind+1); } }
0
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; const int inf = 1e9; vector<int> g[maxn]; vector<pair<int, int> > v; bool vis[maxn]; int n, m, tmp, dp[maxn], pd[maxn], ans = inf, c[maxn]; void dfs(int v) { tmp++; vis[v] = 1; for (auto i : g[v]) if (!vis[i]) dfs(i); } bool luck(int a) { int t = a; while (t > 0) { if (t % 10 != 7 && t % 10 != 4) return 0; t = t / 10; } return 1; } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); cin >> n >> m; for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; g[u].push_back(v); g[v].push_back(u); } for (int i = 1; i <= n; i++) if (!vis[i]) { tmp = 0; dfs(i); c[tmp]++; } for (int i = 1; i <= n; i++) { int help = 0; while (c[i] > 0) { int pw = 1 << help; if (c[i] >= pw) v.push_back(make_pair(pw, i)); else v.push_back(make_pair(c[i], i)); c[i] = c[i] - pw; help++; } } for (int i = 1; i <= n; i++) dp[i] = inf; for (int i = 1; i <= n; i++) pd[i] = inf; for (int i = 0; i < v.size(); i++) { for (int j = 1; j <= n; j++) { if (j >= v[i].first * v[i].second) pd[j] = min(pd[j], dp[j - (v[i].first * v[i].second)] + v[i].first); if (luck(j)) ans = min(ans, pd[j]); } for (int s = 1; s <= n; s++) dp[s] = pd[s]; } if (ans == inf) cout << -1 << '\n'; else cout << ans - 1 << '\n'; return 0; }
5
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, k; cin >> n >> k; long long int Y[n]; long long int x, y; vector<pair<long long int, pair<long long int, long long int> > > X(n); for (int i = 0; i < n; ++i) { cin >> x; X[i].first = x; } for (int i = 0; i < n; ++i) { cin >> x; X[i].second.first = x; X[i].second.second = i; } sort(X.begin(), X.end()); priority_queue<long long int, vector<long long int>, greater<long long int> > A; long long int sum = 0; for (int i = 0; i < X.size(); ++i) { if (A.size() > k) { sum -= A.top(); A.pop(); } A.push(X[i].second.first); sum += X[i].second.first; Y[X[i].second.second] = sum; } for (int i = 0; i < n; ++i) { cout << Y[i] << " "; } cout << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; void print(int n) { cout << n << endl; } int main() { int n, l, sum = 0; cin >> n; string s; s = to_string(n); l = s.size(); for (int i = l; i > 0; i--) sum += pow(2, i - 1); reverse(s.begin(), s.begin() + l); for (int i = 0; i < l; i++) if (s[i] == '7') sum += pow(2, i); print(sum); }
2
#include <bits/stdc++.h> using namespace std; char s[20]; bool judge(char s[]) { int l = strlen(s); int ans = 0; for (int i = 1; i < l; i++) if ((s[i] >= '0' && s[i] <= '9') && (s[i - 1] >= 'A' && s[i - 1] <= 'Z')) ans++; if (ans == 2) return true; return false; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%s", s); if (judge(s)) { int l = strlen(s); int r = 0, c = 0; int rr = 1, cc = 1; int i, j; for (i = l - 1; i >= 0; i--) { if (s[i] >= 'A' && s[i] <= 'Z') break; if (s[i] >= '0' && s[i] <= '9') { c += cc * (s[i] - '0'); cc *= 10; } } for (j = i - 1; j >= 0; j--) { if (s[j] >= 'A' && s[j] <= 'Z') break; if (s[j] >= '0' && s[j] <= '9') { r += rr * (s[j] - '0'); rr *= 10; } } stack<char> st; while (!st.empty()) st.pop(); while (c) { int num = c % 26; if (num == 0) { st.push('Z'); c--; } else st.push(num - 1 + 'A'); c /= 26; } while (!st.empty()) { printf("%c", st.top()); st.pop(); } printf("%d\n", r); } else { int r = 0, c = 0; int rr = 1, cc = 1; int i, j, l = strlen(s); stack<char> st; stack<char> nt; while (!nt.empty()) nt.pop(); while (!st.empty()) st.pop(); for (i = 0; i < l; i++) { if (s[i] >= '0' && s[i] <= '9') break; st.push(s[i]); } for (j = i; j < l; j++) { nt.push(s[j]); } while (!st.empty()) { c += cc * (st.top() - 'A' + 1); cc *= 26; st.pop(); } while (!nt.empty()) { r += rr * (nt.top() - '0'); rr *= 10; nt.pop(); } printf("R%dC%d\n", r, c); } } return 0; }
2
#include <bits/stdc++.h> using namespace std; long long mod = 998244353; long long inf = 1e18 + 1; long long n, m, k, q, W; long long w[2000003]; long long f(long long x) { long long res = 0; for (long long i = 1; i < 11; i++) { long long l = w[i - 1] + 1; long long r = w[i]; if (l > x) break; r = min(x, r); long long d1 = x - l + 1; long long d2 = x - r + 1; res += ((i * (d1 + d2) * (d1 - d2 + 1)) / 2); } return res; } long long bs(long long l, long long r, long long x) { long long an = -1; while (l <= r) { long long mid = (l + r) / 2; long long d = f(mid); if (d >= x) { an = mid; r = mid - 1; continue; } l = mid + 1; } return an; } long long bs1(long long l, long long r, long long x) { long long an = -1; while (l <= r) { long long mid = (l + r) / 2; long long d = f(mid) - f(mid - 1); if (d >= x) { r = mid - 1; an = mid; continue; } l = mid + 1; } return an; } int main() { w[0] = 0; w[1] = 9; for (int i = 2; i < 11; i++) { w[i] = w[i - 1] * 10 + 9; } int q; scanf("%d", &q); while (q--) { long long k; scanf("%lld", &k); long long d1 = bs(1, 1500000000, k); k -= f(d1 - 1); long long fu = bs1(1, d1, k); k -= f(fu - 1) - f(fu - 2); vector<long long> v; while (fu) { v.push_back(fu % 10); fu /= 10; } reverse(v.begin(), v.end()); printf("%lld\n", v[k - 1]); } return 0; }
5
#include <iostream> using namespace std; int main(){ int a,b,i; string s; while(1){ cin>>s; a=0,b=0; if(s[0]=='0')break; for(i=1;i<s.size();i++){ if( s[i]=='A')a++; else b++; } if(a>b)a++; else b++; cout<<a<<" "<<b<<endl; } return 0; }
0
#include<cstdio> int a,b; int main(void){ scanf("%d%d",&a,&b); if(a>b) printf("%d\n",a-b); else puts("0"); return 0; }
0
#include <iostream> #include <string> #include <map> using namespace std; bool valid = true; map<char, int> declar; map< pair<char, int>, int> assign; int number(const string& s, int& p) { int ret = 0; while (isdigit(s[p])) { ret *= 10; ret += (s[p++] - '0'); } return ret; } int expression(const string& s, int& p) { if (isdigit(s[p])) { return number(s, p); } else { char array_name = s[p++]; ++p; int array_index = expression(s, p); map<char, int>::iterator it = declar.find(array_name); if (it != declar.end() && array_index < it->second) { ++p; map< pair<char, int>, int>::iterator jt = assign.find(pair<char, int>(array_name, array_index)); if (jt != assign.end()) return jt->second; else valid = false; } else { valid = false; } } } void assignment(const string& s, int& p) { char array_name = s[p++]; ++p; int array_index = expression(s, p); map<char, int>::iterator it = declar.find(array_name); if (it != declar.end() && array_index < it->second) { p += 2; int value = expression(s, p); assign[pair<char, int>(array_name, array_index)] = value; } else { valid = false; } } void declaration(const string& s, int& p) { char array_name = s[p++]; ++p; int array_size = number(s, p); declar[array_name] = array_size; } int main() { string s; while (cin >> s) { if (s == ".") break; int cnt = 1, ans = 0; valid = true; while (s != ".") { int p = 0; if (valid) { if (s.find_first_of("=") == string::npos) declaration(s, p); else assignment(s, p); if (!valid) ans = cnt; } cin >> s; ++cnt; } declar.clear(); assign.clear(); cout << ans << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e5 + 10, inf = 0x3f3f3f3f3f3f3f3f, mod = 1e9 + 7; int main() { int t; cin >> t; while (t--) { int n; cin >> n; string s, t; cin >> s >> t; int cntsame1 = 0, cntsame0 = 0, cntdiff1 = 0, cntdiff0 = 0; for (long long i = 0; i <= n - 1; ++i) { if (s[i] == t[i]) { if (s[i] == '1') cntsame1++; else cntsame0++; } else { if (s[i] == '1') cntdiff1++; else cntdiff0++; } } int cntdiff = cntdiff0 + cntdiff1, cntsame = cntsame0 + cntsame1; if (cntdiff % 2 == 1 && cntsame % 2 == 0) cout << -1 << endl; else { if (cntsame0 == cntsame1 - 1 && cntdiff1 == cntdiff0) cout << min(cntdiff, cntsame) << endl; else if (cntdiff1 == cntdiff0) cout << cntdiff << endl; else if (cntsame0 == cntsame1 - 1) cout << cntsame << endl; else cout << -1 << endl; } } fflush(stdin); getchar(); return 0; }
3
#include <cmath> #include <functional> #include <fstream> #include <iostream> #include <vector> #include <algorithm> #include <string> #include <set> #include <map> #include <list> #include <time.h> #include <math.h> #include <random> #include <deque> #include <queue> #include <cassert> #include <unordered_map> #include <unordered_set> #include <iomanip> #include <bitset> #include <sstream> #include <chrono> #include <cstring> using namespace std; typedef long long ll; #ifdef iq mt19937 rnd(228); #else mt19937 rnd(chrono::high_resolution_clock::now().time_since_epoch().count()); #endif int s, t; ll ok[18][2]; ll comb[51][51]; ll f[51]; ll ans = 0; void brute(int bit, int sign, ll cands) { if (bit == 18) { ans += f[__builtin_popcountll(cands)] * sign; } else { if (((s >> bit) & 1) == ((t >> bit) & 1)) { brute(bit + 1, sign, cands); } else { brute(bit + 1, sign, cands); brute(bit + 1, -sign, cands & ok[bit][0]); brute(bit + 1, -sign, cands & ok[bit][1]); } } } int main() { #ifdef iq freopen("a.in", "r", stdin); #endif ios::sync_with_stdio(0); cin.tie(0); int n, k; cin >> n >> k; cin >> s >> t; comb[0][0] = 1; for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { if (i) comb[i][j] += comb[i - 1][j]; if (i && j) comb[i][j] += comb[i - 1][j - 1]; } } for (int i = 0; i <= n; i++) { for (int j = 1; j <= k; j++) { f[i] += comb[i][j]; } } vector <int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } if ((t & s) != s) { cout << 0 << '\n'; return 0; } vector <int> x; for (int i = 0; i < n; i++) { if (((a[i] & t) == a[i]) && ((a[i] | s) == a[i])) { x.push_back(a[i]); } } a = x; n = (int) a.size(); for (int i = 0; i < n; i++) { for (int j = 0; j < 18; j++) { ok[j][(a[i] >> j) & 1] |= (1ll << i); } } brute(0, 1, (1ll << n) - 1); cout << ans << endl; }
0
#include <algorithm> #include <cstdio> #include <cstring> const int N = 2000; const int N2 = N * 2 + 1; const int MOD = 998244353; void update(int& x, int a) { x += a; if (x >= MOD) { x -= MOD; } } char s[N + 1]; int dp[2][N2]; int main() { scanf("%s", s); int n = strlen(s); memset(dp, 0, sizeof(dp)); dp[0][0] = 1; int reds = 0, blues = 0; for (int i = 0; i < 2 * n; ++ i) { memset(dp[i + 1 & 1], 0, sizeof(dp[i + 1 & 1])); int b = i < n ? s[i] - '0' : 0; int r = i < n ? 2 - b : 0; for (int j = 0; j <= i; ++ j) { if (dp[i & 1][j]) { if (j + 1 <= reds + std::min(r, 1)) { update(dp[i + 1 & 1][j + 1], dp[i & 1][j]); } if (i - j + 1 <= blues + std::min(b, 1)) { update(dp[i + 1 & 1][j], dp[i & 1][j]); } } } reds += r; blues += b; } int result = 0; for (int j = 0; j <= 2 * n; ++ j) { update(result, dp[2 * n & 1][j]); } printf("%d\n", result); }
0
#include <bits/stdc++.h> using namespace std; int main() { string kata; int panjang = 0; int x, y; cin >> kata; int length = kata.length(); for (int i = 0; i < length; i++) { for (int j = i + 1; j < length; j++) { int x = i; int y = j; int panjangsementara = 0; while (kata[x] == kata[y]) { panjangsementara += 1; x++; y++; } if (panjangsementara > panjang) { panjang = panjangsementara; } } } cout << panjang << endl; }
1
#include <bits/stdc++.h> using namespace std; int i, j, k, m, n, l; double a[100000 + 10], s[100000 + 10], p[100000 + 10]; int main() { while (cin >> n) { double p = 0, ans = 0; for (int i = (1); i <= (n); ++i) { scanf("%lf", &a[i]); ans += a[i] * p * 2 + a[i]; p = (p + 1) * a[i]; } printf("%.10lf\n", ans); } return 0; }
2
#include <bits/stdc++.h> using namespace std; string vowels = "aoeui", r[] = {"aabb", "abab", "abba"}; string cut(string s, int k) { int n = s.size(), i; for (; n--;) { for (i = 0; i < 5; i++) { if (s[n] == vowels[i]) k--; if (k == 0) break; } if (k == 0) break; } if (n < 0) { return ""; } return s.substr(n); } int main() { int n, k, i, j; string s[4]; bool b[3] = {0}; cin >> n >> k; for (i = 0; i < n; i++) { for (j = 0; j < 4; j++) { cin >> s[j]; s[j] = cut(s[j], k); if (s[j] == "") { goto EXIT; } } if (s[0] != s[1] || s[2] != s[3]) b[0] = 1; if (s[0] != s[2] || s[1] != s[3]) b[1] = 1; if (s[0] != s[3] || s[1] != s[2]) b[2] = 1; } if (!b[0] && !b[1]) { cout << "aaaa" << endl; return 0; } for (i = 0; i < 3; i++) { if (b[i] == 0) { cout << r[i] << endl; return 0; } } EXIT:; cout << "NO" << endl; return 0; }
1
#include<iostream> #include<string> #include<cmath> using namespace std; int main(){ int color[8][3] = {{0, 0, 0}, {0, 0, 255}, {0, 255, 0}, {0, 255, 255}, {255, 0, 0}, {255, 0, 255}, {255, 255, 0}, {255, 255, 255}}; double d, f, p[3]; string str, c; while(cin >> str){ if(str[0] == '0') break; d = sqrt(3 * pow(255, 2)); for(int j=0; j<3; j++){ if(str[2*j+1] >= '0' && str[2*j+1] <= '9') p[j] = 16 * (str[2*j+1] - '0'); else p[j] = 16 * (10.0 + str[2*j+1] - 'a'); if(str[2*j+2] >= '0' && str[2*j+2] <= '9') p[j] += str[2*j+2] - '0'; else p[j] += 10.0 + str[2*j+2] - 'a'; } for(int i=0; i<8; i++){ f = sqrt(pow(p[0] - color[i][0], 2) + pow(p[1] - color[i][1], 2) + pow(p[2] - color[i][2], 2)); if(d > f){ d = f; switch(i){ case 0: c = "black"; break; case 1: c = "blue"; break; case 2: c = "lime"; break; case 3: c = "aqua"; break; case 4: c = "red"; break; case 5: c = "fuchsia"; break; case 6: c = "yellow"; break; case 7: c = "white"; break; } } } cout << c << endl; } return 0; }
0
#include <bits/stdc++.h> long long int gcd(long long int a, long long int b) { while ((a) != 0 && (b) != 0) { if ((a) > (b)) (a) %= (b); else (b) %= (a); } return ((a) == 0 ? (b) : (a)); } long long int power(long long int x, long long int n, unsigned long long int m = ULLONG_MAX) { long long int res = 1; x %= m; while (n > 0) { if (n & 1 == 1) res = (res * x) % m; n /= 2; x = (x * x) % m; } return res; } using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); string s, t; cin >> s >> t; int l1 = s.length(), l2 = t.length(), i; int a[26] = {0}, b[26] = {0}; for (i = 0; i <= l1 - 1; ++i) ++a[int(s[i]) - 97]; for (i = 0; i <= l2 - 1; ++i) ++b[int(t[i]) - 97]; bool res = 1; for (i = 0; i <= 25; ++i) { if (b[i] == 0) continue; else if (b[i] > a[i]) { res = 0; break; } } if (res == 0) { cout << "need tree\n"; return 0; } if (l1 == l2 && s != t) { cout << "array\n"; return 0; } else { int j = 0; for (i = 0; i <= l1 - 1; ++i) { if (s[i] == t[j]) ++j; else continue; } if (j == l2) { cout << "automaton\n"; return 0; } else { cout << "both\n"; return 0; } } }
2
#include <bits/stdc++.h> using namespace std; int mat[102][102]; int n; int main() { cin >> n; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cin >> mat[i][j]; } } int res = 0; if (n == 1) { cout << mat[0][0]; return 0; } for (int i = 0; i < n; i++) res += mat[i][i]; for (int i = 0; i < n; i++) res += mat[i][n - 1 - i]; for (int i = 0; i < n; i++) res += mat[i][n / 2]; for (int i = 0; i < n; i++) res += mat[n / 2][i]; res -= 3 * mat[n / 2][n / 2]; cout << res; return 0; }
1
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; int x[200010]={0},y[200010]={0}; int main(){ int i,j,k,m,n; scanf("%d%d",&m,&n); for (i=1;i<=n;i++){ int a,b; scanf("%d%d",&a,&b); if (a==1) x[b]++; if (b==m) y[a]++; } for (i=1;i<=n;i++) if (x[i] && y[i]){ printf("POSSIBLE\n"); return 0; } printf("IMPOSSIBLE\n"); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { string s1, s2; getline(cin, s1); getline(cin, s2); int a1[52] = {0}; for (int i = 0; i < s1.length(); i++) { if (int(s1[i]) < 91) a1[s1[i] - 65]++; else a1[s1[i] - 71]++; } for (int i = 0; i < s2.length(); i++) { if (int(s2[i]) < 91) if (a1[s2[i] - 65] == 0) { cout << "NO" << endl; return 0; } else a1[s2[i] - 65]--; else if (a1[s2[i] - 71] == 0) { cout << "NO" << endl; return 0; } else a1[s2[i] - 71]--; } cout << "YES" << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAXN = 2e5 + 7, Mo = 1e9 + 7; int n, m, q; int a[MAXN]; char ss[MAXN]; long long qm(long long s, long long t) { if (t == 0) return 1; if (t == 1) return s; long long rst = qm(s, t >> 1); rst = rst * rst % Mo; if (t & 1) rst = rst * s % Mo; return rst; } int main() { scanf("%d", &n); scanf("%s", ss); int len = strlen(ss); for (int i = 0; i < len; i++) { if (ss[i] == 'A') a[0]++; if (ss[i] == 'C') a[1]++; if (ss[i] == 'G') a[2]++; if (ss[i] == 'T') a[3]++; } sort(a, a + 4); int r = 3; while (r && a[r - 1] == a[3]) r--; cout << qm(3 - r + 1, n) << endl; return 0; }
1