solution
stringlengths
52
181k
difficulty
int64
0
6
#include <bits/stdc++.h> using namespace std; template <typename T> inline void next_int(T &x) { x = 0; char c; bool neg = false; while (!isdigit(c = getchar())) if (c == '-') neg = true; do x = x * 10 + c - 48; while (isdigit(c = getchar())); if (neg) x = -x; } template <typename T> inline void write_int(T x, char last = 0) { if (x < 0) putchar('-'), x = abs(x); char tmp[20]; int cnt = 0; while (x >= 10) tmp[cnt++] = x % 10 + 48, x /= 10; tmp[cnt] = x + 48; for (int i = (cnt); i >= (0); --i) putchar(tmp[i]); if (last) putchar(last); } const int N = (int)1e6 + 5; int n, Us[N], Ds[N], nu, nd; long long sd[N], su[N]; char s[N]; deque<int> lf, rt; int main() { scanf("%d", &n); scanf("%s", s + 1); for (int i = (n); i >= (1); --i) if (s[i] == 'D') Ds[++nd] = i, sd[nd] = sd[nd - 1] + (long long)i; for (int i = (1); i <= (n); ++i) { if (s[i] == 'U') Us[++nu] = i, su[nu] = su[nu - 1] + (long long)i; int tmp = min(nd, nu); long long ret = (sd[nd] - sd[nd - tmp] - su[nu] + su[nu - tmp]) * 2LL; if (nu < nd) { if (s[i] == 'D') ret = ret - (long long)Ds[nd] + (long long)Ds[nd - tmp]; else ret = ret + (long long)Us[nu] - (long long)Ds[nd - tmp + 1]; } else if (nu > nd) { if (s[i] == 'U') ret = ret + (long long)Us[nu] - (long long)Us[nu - tmp]; else ret = ret - (long long)Ds[nd] + (long long)Us[nu - tmp + 1]; } else { if (s[i] == 'D') ret = ret - (long long)Ds[nd] + (long long)Us[nu - tmp + 1]; else ret = ret + (long long)Us[nu] - (long long)Ds[nd - tmp + 1]; } if (s[i] == 'D') { if (nd > nu) ret += (long long)Ds[nd - tmp]; else ret += (long long)(n - Us[nu - tmp + 1] + 1); } else { if (nu > nd) ret += (long long)(n - Us[nu - tmp] + 1); else ret += (long long)Ds[nd - tmp + 1]; } printf("%I64d ", ret); nd -= (s[i] == 'D'); } return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long INF = 9e18; const long long MOD = 1e9 + 7; const long long mod = 998244353; #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") void fileio() {} const long long N = 1e6; vector<vector<long long> > v(N); void solve() { long long n, m; cin >> n >> m; for (long long i = 0; i < m; i++) { long long x, y; cin >> x >> y; v[x].push_back(y); v[y].push_back(x); } vector<long long> t(n + 1); vector<vector<long long> > c(n + 1); for (long long i = 1; i < n + 1; i++) { cin >> t[i]; c[t[i]].push_back(i); } long long fl = 0; vector<long long> o(n + 1, 0); long long k = 1; for (long long i = 1; i < n + 1; i++) { for (auto x : c[i]) { o[x] = k++; set<long long> s; for (long long j = 1; j < t[x] + 1; j++) s.insert(j); for (auto ch : v[x]) s.erase(t[ch]); if (!(s.size() == 1 && *s.begin() == t[x])) { fl = 1; break; } } } if (fl) { cout << -1 << "\n"; return; } vector<long long> ans(n + 1); for (long long i = 1; i < n + 1; i++) ans[o[i]] = i; for (long long i = 1; i < n + 1; i++) cout << ans[i] << " "; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; fileio(); solve(); }
4
#include <bits/stdc++.h> using namespace std; int main() { long long n, sum = 0, minOdd = INT_MAX, number; cin >> n; while (n--) { cin >> number; if (number % 2 == 1 && number < minOdd) minOdd = number; sum += number; } if (sum % 2 == 1) sum -= minOdd; cout << sum; }
1
#include <bits/stdc++.h> using namespace std; int main(){ int c,q,i,j,z=0,k=1,o=0,m[1001][1001]={0},e[6][2]={0}; for(i=0;i<6;i++){ cin>>q>>c; if(q>c)m[q][c]++; else m[c][q]++; } for(i=0;i<1001;i++){ for(j=0;j<1001;j++){ if(m[i][j]){ if(m[i][j]==4){e[z][0]=i;e[z++][1]=j;e[z][0]=i;e[z++][1]=j;} else if(m[i][j]==6){e[z][0]=i;e[z++][1]=j;e[z][0]=i;e[z++][1]=j;e[z][0]=i;e[z++][1]=j;} else{ e[z][0]=i;e[z++][1]=j;} } } } if(e[0][0]!=e[2][1]||e[1][0]!=e[2][0]||e[0][1]!=e[1][1])k=0; if(k)cout<<"yes"<<endl; else cout<<"no"<<endl; }
0
#include<bits/stdc++.h> using namespace std; #define fr for(i=0;i<n;i++) typedef long long ll; ll inf=1e18; #define pb push_back #define ft first #define sc second int main(){ ll i,j,k=0,l=0,m,n,nx,a,b,x,c=0,mx=0,h=0,ans,o,e,p,d,y,z,u,r; cin>>m; while(m--){ cin>>n>>k; c=1; vector<ll>v; for(i=1;i<=n;i++){ if(i!=k){ v.pb(i); } } if(n!=1){ for(i=0;i<=k;i++){ for(j=i+1;j<=k;j++){ if(v[i]+v[j]==k){ v.erase(v.begin()+i); c++;i--; break; } } } } cout<<n-c<<endl; for(auto i:v){ cout<<i<<" "; } cout<<endl; } }
1
#include <bits/stdc++.h> using namespace std; template <typename T, typename U> inline void smin(T &a, const U &b) { if (a > b) a = b; } template <typename T, typename U> inline void smax(T &a, const U &b) { if (a < b) a = b; } template <typename T> inline void gn(T &first) { char c, sg = 0; while (c = getchar(), (c > '9' || c < '0') && c != '-') ; for ((c == '-' ? sg = 1, c = getchar() : 0), first = 0; c >= '0' && c <= '9'; c = getchar()) first = (first << 1) + (first << 3) + c - '0'; if (sg) first = -first; } template <class T, class T1> inline void gn(T &first, T1 &second) { gn(first); gn(second); } template <class T, class T1, class T2> inline void gn(T &first, T1 &second, T2 &z) { gn(first); gn(second); gn(z); } template <typename T> inline void print(T first) { if (first < 0) { putchar('-'); return print(-first); } if (first < 10) { putchar('0' + first); return; } print(first / 10); putchar(first % 10 + '0'); } template <typename T> inline void println(T first) { print(first), putchar('\n'); } template <typename T> inline void printsp(T first) { print(first), putchar(' '); } template <class T, class T1> inline void print(T first, T1 second) { printsp(first), println(second); } template <class T, class T1, class T2> inline void print(T first, T1 second, T2 z) { printsp(first), printsp(second), println(z); } int power(int a, int b, int m, int ans = 1) { for (; b; b >>= 1, a = (long long)a * a % m) if (b & 1) ans = (long long)ans * a % m; return ans; } int main() { long long k, b, n, t; gn(k, b), gn(n, t); long long s = 1; for (int i = 0; i <= n; i++) { if (s == t) { println(n - i); return 0; } if (s > t) { println(n - i + 1); return 0; } s = s * k + b; } puts("0"); }
1
#include <bits/stdc++.h> using namespace std; 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 * b / gcd(a, b); } long long powe(long long a, long long b, long long mo) { long long ans = 1; while (b > 0) { if (b & 1) ans = (ans * a) % mo; b = b >> 1; a = (a * a) % mo; } return ans; } long long A[1000001], B[1000001]; int main() { long long t, i, n; cin >> n; for (i = 0; i < n; i++) { cin >> A[i]; } for (i = 0; i < n / 2; i += 2) { long long temp = A[n - 1 - i]; A[n - 1 - i] = A[i]; A[i] = temp; } for (i = 0; i < n; i++) { cout << A[i] << " "; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int a[10000]; int main() { int n, s, i, sum; cin >> n >> s; sum = 0; for (i = 0; i < n; i++) cin >> a[i]; sort(a, a + n); for (i = 0; i <= n - 2; i++) sum += a[i]; if (sum <= s) cout << "YES"; else cout << "NO"; }
1
#include <bits/stdc++.h> using namespace std; int main() { int a[3], x[3]; for (int i = 0; i < 3; ++i) cin >> a[i]; for (int i = 0; i < 3; ++i) cin >> x[i]; int cnt1 = 0, cnt2 = 0; for (int i = 0; i < 3; ++i) { if (a[i] > x[i]) cnt1 += (a[i] - x[i]) / 2; else cnt2 += x[i] - a[i]; } if (cnt1 >= cnt2) cout << "Yes" << endl; else cout << "No" << endl; return 0; }
1
#include <bits/stdc++.h> #pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double pi = acos(-1.0), eps = 1e-6; void File() { freopen("D:\\in.txt", "r", stdin); freopen("D:\\out.txt", "w", stdout); } template <class T> inline void read(T &x) { char c = getchar(); x = 0; while (!isdigit(c)) c = getchar(); while (isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); } } const int maxn = 100010; int n, k, p[maxn], sz; char s[maxn]; int get(int x, int low, int high) { return max(p[x] - p[low], p[high] - p[x]); } int f(int L, int R) { int t1, t2; int low = L, high = R; for (int i = 1; i <= 200; i++) { t1 = (low + high) / 2; t2 = (t1 + high) / 2; if (get(t1, L, R) < get(t2, L, R)) high = t2; else low = t1; } return get(t1, L, R); } int main() { scanf("%d%d", &n, &k); scanf("%s", s); for (int i = 0; s[i]; i++) { if (s[i] == '1') continue; sz++; p[sz] = i + 1; } int ans = 200000; for (int i = 1; i <= sz; i++) { if (i + k > sz) break; ans = min(ans, f(i, i + k)); } printf("%d\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; double n, dist, v1, v2, k; int g, n1, k1; int check(double mid) { double tim = 0, x, y; for (int i = 0; i < g; i++) { x = (dist - (v1 * mid)) / (v2 - v1); tim += x; if (i + 1 < g) { y = (x * (v2 - v1)) / (v1 + v2); tim += y; } } if (tim <= mid) return 1; else return 0; } double solve() { double l, h, mid; l = 0, h = dist / v1; while (h - l >= 0.0000005) { mid = (h + l) / 2; if (check(mid) == 1) h = mid; else l = mid; } return l; } int main() { scanf("%d%lf%lf%lf%d", &n1, &dist, &v1, &v2, &k1); g = n1 / k1; n = n1; k = k1; if (n1 % k1 != 0) g++; double ans = solve(); printf("%.6lf", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int INF = 1e9 + 7; int main() { srand(time(0)); int n, st, x, val = 1e9 + 7; scanf("%d%d%d", &n, &st, &x); if (n <= 2000) { int ask = st; int v = val; for (int i = 0; i < min(n, 1999); i++) { printf("? %d\n", ask); fflush(stdout); scanf("%d %d", &v, &ask); if (v >= x && v < val) { val = v; } } if (val == INF) val = -1; printf("! %d\n", val); } else { int ask = st, a = st, ai = st; int v = val, vi = -1; int d = n / 1000; for (int i = 0; i < 1000; i++) { printf("? %d\n", rand() % d + 1 + d * i); fflush(stdout); scanf("%d %d", &v, &ask); if (v >= x && v < val) { val = v; } if (v < x && v > vi) { vi = v; ai = ask; } } for (int i = 0; i < 999; i++) { if (ai == -1) break; printf("? %d\n", ai); fflush(stdout); scanf("%d %d", &v, &ai); if (v >= x && v < val) { val = v; } } if (val == INF) val = -1; printf("! %d\n", val); } }
4
#include <bits/stdc++.h> using namespace std; int n, m, q, x, y, ans; int fa[400002]; int find(int x) { return fa[x] == x ? fa[x] : fa[x] = find(fa[x]); } void unionn(int x, int y) { fa[find(y)] = find(x); } int main() { cin >> n >> m >> q; for (int i = 1; i <= n + m; ++i) fa[i] = i; for (int i = 1; i <= q; ++i) { cin >> x >> y; y += n; unionn(x, y); } for (int i = 1; i <= n + m; ++i) ans += fa[i] == i; cout << ans - 1 << endl; return 0; }
2
#include<cstdio> #include<cstdlib> #include<string> #include<iostream> using namespace std; struct Node{ Node *right, *left, *parent; int key; }; Node *root, *NIL; Node *find(Node *u, int k){ while(u != NIL && k != u->key){ if(k < u->key) u = u->left; else u = u->right; } return u; } void insert(int k){ Node *y = NIL; Node *x = root; Node *z; z = (Node *)malloc(sizeof(Node)); z->key = k; z->left = NIL; z->right = NIL; while(x != NIL){ y = x; if(z->key < x->key) x = x->left; else x = x->right; } z->parent = y; if(y == NIL) root = z; else{ if(z->key < y->key) y->left = z; else y->right = z; } } void inorder(Node *u){ if(u == NIL) return; inorder(u->left); printf(" %d",u->key); inorder(u->right); } void preorder(Node *u){ if(u == NIL) return; printf(" %d",u->key); preorder(u->left); preorder(u->right); } int main(){ int n, i, x; string com; scanf("%d", &n); for ( i = 0; i < n; i++ ){ cin >> com; if( com[0] == 'f' ){ scanf("%d", &x); Node *t = find(root, x); if ( t != NIL ) printf("yes\n"); else printf("no\n"); } else if ( com == "insert"){ scanf("%d", &x); insert(x); } else if ( com == "print"){ inorder(root); printf("\n"); preorder(root); printf("\n"); } } return 0; }
0
#include <bits/stdc++.h> using namespace std; int n; int a[200005 * 2], b[200005]; int cmp(int *a, int *b, int n); void show(int *a) { for (int i = 0; i < n * 2; i++) cout << a[i] << ' '; cout << endl; } int main() { cin >> n; int j = 0; for (int i = 0; i < n; i++) { int t; cin >> t; if (t) a[j++] = t; } j = 0; for (int i = 0; i < n; i++) { int t; cin >> t; if (t) b[j++] = t; } for (int i = 0; i < n - 1; i++) { a[n + i - 1] = a[i]; } for (int i = 0; i < n; i++) { if (a[i] == b[0] && cmp(a + i, b, n - 1)) { cout << "YES\n"; return 0; } } cout << "NO\n"; return 0; } int cmp(int *a, int *b, int n) { for (int i = 0; i < n; i++) { if (*a != *b) return 0; a++; b++; } return 1; }
2
#include <bits/stdc++.h> using namespace std; int main() { int n, s, t; cin >> n >> s >> t; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int count = 0, j = a[s - 1]; while (n--) { if (j == a[t - 1]) { break; } j = a[j - 1]; count++; } if (j != a[t - 1]) { cout << "-1" << endl; } else { cout << count << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 2009; const int mod = 998244353; long long f[maxn], g[maxn], p[maxn][maxn]; int n, m, a, b; long long pr; long long PowerMod(long long x, long long y) { if (x < 0) x += mod; long long res = 1; while (y) { if (y & 1) res = res * x % mod; x = x * x % mod; y >>= 1; } return res; } long long C(long long x) { return x * (x - 1) / 2; } int main() { scanf("%d%d%d", &n, &a, &b); pr = 1ll * a * PowerMod(b, mod - 2) % mod; for (int i = 0; i <= n; i++) { p[i][0] = 1; for (int j = 1; j <= i; j++) p[i][j] = (p[i - 1][j] * PowerMod(1 - pr, j) % mod + p[i - 1][j - 1] * PowerMod(pr, i - j) % mod) % mod; } g[1] = 1; g[2] = 0; for (int i = 3; i <= n; i++) { g[i] = 1; for (int j = 1; j < i; j++) g[i] = (g[i] - g[j] * p[i][j] % mod) % mod; if (g[i] < 0) g[i] += mod; } f[2] = 1; for (int i = 3; i <= n; i++) { for (int j = 1; j < i; j++) if (j != 2) f[i] = (f[i] + (f[i - j] + f[j] + C(j) + 1ll * j * (i - j)) % mod * g[j] % mod * p[i][j] % mod) % mod; f[i] = (f[i] + C(i) * g[i] % mod * p[i][i] % mod) % mod; f[i] = f[i] * PowerMod(1 - g[i] * p[i][i] % mod, mod - 2) % mod; } cout << f[n] << endl; return 0; }
6
#include <iostream> #include <string> using namespace std; char table[12][12]={0}; int main(void){ int sx,sy; while(true){ for(int y=1;y<=8;y++) for(int x=1;x<=8;x++) cin >> table[x][y],table[x][y]-='0'; if(cin.eof()) break; for(int y=1;y<=8;y++) for(int x=1;x<=8;x++) if(table[x][y]) sx=x,sy=y,x=9,y=9; if(table[sx][sy]&table[sx+1][sy]&table[sx][sy+1]&table[sx+1][sy+1]) cout << 'A' << endl; else if(table[sx][sy]&table[sx][sy+1]&table[sx][sy+2]&table[sx][sy+3]) cout << 'B' << endl; else if(table[sx][sy]&table[sx+1][sy]&table[sx+2][sy]&table[sx+3][sy]) cout << 'C' << endl; else if(table[sx][sy]&table[sx-1][sy+1]&table[sx][sy+1]&table[sx-1][sy+2]) cout << 'D' << endl; else if(table[sx][sy]&table[sx+1][sy]&table[sx+1][sy+1]&table[sx+2][sy+1]) cout << 'E' << endl; else if(table[sx][sy]&table[sx][sy+1]&table[sx+1][sy+1]&table[sx+1][sy+2]) cout << 'F' << endl; else if(table[sx][sy]&table[sx+1][sy]&table[sx-1][sy+1]&table[sx][sy+1]) cout << 'G' << endl; } }
0
#include <bits/stdc++.h> using namespace std; inline istream& skip_endl(istream& is) { return is.ignore(numeric_limits<streamsize>::max(), '\n'); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; vector<int> v(n); for (int i = 0; i <= n - 1; i++) { cin >> v[i]; } int p1 = -1, p2 = -1; for (int i = 0; i <= n - 1; i++) { if (v[i] == 1) { p1 = i; break; } } for (int i = n - 1; i >= 0; i--) { if (v[i] == 1) { p2 = i; break; } } if (p1 == p2) { if (p1 == -1) { cout << "0\n"; } else { cout << "1\n"; } } else { int zero_block = 0; long long ways = 1; for (int i = p1 + 1; i <= p2 - 1; i++) { if (v[i] == 0) { zero_block++; } else { ways *= (zero_block + 1); zero_block = 0; } } cout << ways * (zero_block + 1) << "\n"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &x) { x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = 1; for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); if (f) x = -x; } template <typename F> inline void write(F x) { static short st[30]; short tp = 0; if (x < 0) putchar('-'), x = -x; do st[++tp] = x % 10, x /= 10; while (x); while (tp) putchar('0' | st[tp--]); putchar('\n'); } template <typename T> inline void Mx(T &x, T y) { x < y && (x = y, 0); } template <typename T> inline void Mn(T &x, T y) { x > y && (x = y); } const int P = 1e9 + 7; const int N = 50; long long f[N][N][2], bit[N], ans, tp; inline void add(long long &x, long long y) { x += y, x >= P && (x -= P); } long long k; int main() { read(k); while (k) bit[++tp] = k & 1, k >>= 1; f[tp + 1][0][1] = 1; for (int i = tp + 1; i >= 1; i--) { for (int j = 0; j <= tp - i + 1; j++) { add(f[i - 1][j][0], (1ll << j) * f[i][j][0] % P); add(f[i - 1][j + 1][0], f[i][j][0]); long long a = j ? (1ll << (j - 1)) : 1; long long b = j ? (1ll << (j - 1)) : 0; if (bit[i - 1]) { add(f[i - 1][j][0], a * f[i][j][1] % P); add(f[i - 1][j][1], b * f[i][j][1] % P); add(f[i - 1][j + 1][1], f[i][j][1] % P); } else add(f[i - 1][j][1], a * f[i][j][1] % P); } } for (int i = 0; i <= tp; i++) add(ans, f[1][i][0] + f[1][i][1]); write(ans % P); return 0; }
4
#include <bits/stdc++.h> using namespace std; long long mod = 1000000 + 3; long long pp(long long a, long long b) { long long ans = 1; a = a % mod; while (b > 0) { if (b % 2 == 1) ans = ans * a % mod; b = b / 2; a = a * a % mod; } return ans % mod; } int main() { long long i, j, k, m, n, a, b; scanf("%lld%lld", &n, &k); long long temp = 1; int flag = 0; for (int i = 1; i <= n; i++) { temp *= 2; if (temp >= k) { flag = 1; break; } } if (flag == 0) { printf("1 1\n"); return 0; } temp = 0; if (k - 1 >= mod) { long long temp = 0; long long kk = k - 1; long long p = 2; while (p <= kk) { temp = (temp + (k - 1) / p); p *= 2; } long long b = pp(2, n); temp = pp(2, temp) % mod; b = pp(b, k - 1) % mod * pp(temp, mod - 2) % mod; printf("%lld %lld\n", b, b); return 0; } temp = 0; long long kk = k - 1; long long p = 2; while (p <= kk) { temp = (temp + (k - 1) / p); p *= 2; } long long aa = pp(2, n); a = 1; b = 1; for (int i = 1; i <= k - 1; i++) { a = a * (aa - i) % mod; } long long tt = pp(2, temp); a = a * pp(tt, mod - 2) % mod; b = pp(2, n); b = pp(b, k - 1); b = b * pp(tt, mod - 2) % mod; a = b - a; a = (a + mod) % mod; printf("%lld %lld\n", a, b); return 0; }
5
#include<iostream> #include<math.h> #include<float.h> #include<cstdio> #include<vector> using namespace std; #define EPS (1e-10) #define equals(a,b) fabs((a)-(b))<DBL_EPSILON*fmax(1,fmax(fabs(a),fabs(b))) //点のクラス class Point{ public: double x,y; Point(double x=0,double y=0):x(x),y(y){} Point operator + (Point p){return Point(x+p.x,y+p.y);} Point operator - (Point p){return Point(x-p.x,y-p.y);} Point operator * (double a){return Point(a*x,a*y);} Point operator / (double a){return Point(x/a,y/a);} double norm(Point p){return p.x*p.x+p.y*p.y;} double abs(Point p){return sqrt(norm(p));} bool operator < (const Point &p)const{ return x!=p.x?x<p.x:y<p.y; } bool operator == (const Point &p)const{ return equals(x,p.x)&&equals(y,p.y); } }; typedef Point Vector; double norm(Vector v){return v.x*v.x+v.y*v.y;} double abs(Vector v){return sqrt(norm(v));} //円 class Circle{ public: Point c; double r; Circle(Point c=Point(),double r=0.0):c(c),r(r){} }; 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){ //assert(intersect(c1,c2)); double d=abs(c1.c-c2.c); double a=acos((c1.r*c1.r+d*d-c2.r*c2.r)/(2*c1.r*d)); double t=arg(c2.c-c1.c); return make_pair(c1.c+polar(c1.r,t+a),c1.c+polar(c1.r,t-a)); } int main(){ Circle c1,c2; cin>>c1.c.x>>c1.c.y>>c1.r; cin>>c2.c.x>>c2.c.y>>c2.r; pair<Point,Point> pp; pp=getCrossPoints(c1,c2); if(pp.first.x>pp.second.x) swap(pp.first,pp.second); else if(pp.first.x==pp.second.x&&pp.first.y>pp.second.y) swap(pp.first,pp.second); printf("%.10f %.10f %.10f %.10f\n",pp.first.x,pp.first.y,pp.second.x,pp.second.y); return 0; }
0
#include <bits/stdc++.h> using namespace std; vector<int> G[100100]; int deg[100100]; int cop[100100]; int main() { int n, m; scanf("%d%d", &n, &m); for (int i = (0); i < (n); i++) scanf("%d", &cop[i]); for (int i = (0); i < (m); i++) { int a, b; scanf("%d%d", &a, &b); G[b].push_back(a); ++deg[a]; } int result = 0; queue<int> q; for (int i = (0); i < (n); i++) { if (deg[i] == 0) q.push(i); } while (!q.empty()) { bool used = false; queue<int> q2; while (!q.empty()) { int v = q.front(); q.pop(); if (!cop[v]) { for (int u : G[v]) { --deg[u]; if (deg[u] == 0) q.push(u); } } else { q2.push(v); } } while (!q2.empty()) { int v = q2.front(); q2.pop(); if (cop[v]) { used = true; for (int u : G[v]) { --deg[u]; if (deg[u] == 0) q2.push(u); } } else { q.push(v); } } if (used) ++result; } printf("%d\n", result); }
5
#include <bits/stdc++.h> using namespace std; int P; int g1() { double p = rand() / 32767.0, s = 0, g = 1; for (int k = 0;; k++) { if (k) g *= k; double c = pow(P, k) * exp(-P) / g; s += c; if (p <= s) return k; } } int g2() { double p = rand() / 32767.0, s = 0; for (int k = 0; k <= P + P; k++) { s += 1.0 / (2 * P + 1); if (p <= s) return k; } return P + P; } int main() { int v; scanf("%d", &v); while (v--) { vector<int> vs; for (int i = 0; i < 250; i++) { int x; scanf("%d", &x); vs.push_back(x); } double avr = 0, ss = 0; for (int i = 0; i < 250; i++) avr += (double)vs[i]; avr /= 250; int mx = 0; for (int i = 0; i < 250; i++) ss += (vs[i] - avr) * (vs[i] - avr), mx = max(mx, vs[i]); ss /= avr; if (ss < 500) printf("%d\n", int(avr + 0.5)); else printf("%d\n", int(mx / 2.0 + 0.5)); } }
5
#include <bits/stdc++.h> using namespace std; using lld = long long; void query(void) { int n; cin >> n; vector<int> t(n); for (int& i : t) cin >> i; int x = 0; for (int& i : t) x ^= i; if (x == 0) cout << "DRAW\n"; else { int b = 30; while (!(x & (1 << b))) b--; int x = 0, y = 0; for (int& i : t) ((i & (1 << b)) ? x : y)++; cout << (x % 4 == 1 || (x % 4 == 3 && y % 2 == 1) ? "WIN" : "LOSE") << "\n"; } } int main(void) { ios_base::sync_with_stdio(false); cin.tie(nullptr); int q = 1; cin >> q; while (q--) query(); return 0; }
2
#include <iostream> #include <iomanip> #include <string> #include <vector> #include <algorithm> #include <numeric> #include <map> #include <set> #include <cstdio> #include <cstring> #include <cmath> #include <bitset> #include <climits> #define REP(i,n) for (int i=0;i<(n);i++) #define FOR(i,a,b) for (int i=(a);i<(b);i++) #define RREP(i,n) for (int i=(n)-1;i>=0;i--) #define RFOR(i,a,b) for (int i=(a)-1;i>=(b);i--) #define ll long long #define ull unsigned long long int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; const int INF = 1e9; const int MOD = 1e9 + 7; using namespace std; #include <vector> #include <cmath> #include <cstring> const double EPS = 1e-10; #define equals(a,b) (fabs((a) - (b)) < EPS) struct Point{ double x,y; Point(double x = 0.0,double y =0.0):x(x),y(y){} Point operator + (const Point& p){ return Point(x+p.x,y+p.y); } Point operator - (const 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); } double norm(){return x*x+y*y;} double abs(){return sqrt(norm());} bool operator < (const Point& p) const { return x != p.x ? x < p.x : y < p.y; } bool operator == (const Point &p) const{ return fabs(x-p.x) < EPS && fabs(y-p.y) < EPS; } }; typedef Point Vector; 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; } struct Segment{ Point p1,p2; Segment(Point p1,Point p2):p1(p1),p2(p2){} Segment(double a,double b,double c,double d):p1(Point(a,b)),p2(Point(c,d)){} }; typedef Segment Line; class Circle{ public: Point c; double r; Circle(Point c = Point(), double r = 0.0): c(c),r(r){} Circle(double x,double y, double r = 0.0): c(Point(x,y)),r(r){} }; typedef vector<Point> Polygon; bool isOrthgonal(Vector a, Vector b){ return equals(dot(a,b),0.0); } bool isOrthgonal(Point a1, Point a2, Point b1, Point b2){ return isOrthgonal(a1-a2,b1-b2); } bool isOrthgonal(Segment s1, Segment s2){ return equals(dot(s1.p2 - s1.p1, s2.p2 - s2.p1),0.0); } bool isParallel(Vector a,Vector b){ return equals(cross(a,b),0.0); } bool isParallel(Point a1, Point a2, Point b1, Point b2){ return isParallel(a1-a2,b1-b2); } bool isParallel(Segment s1, Segment s2){ return equals(cross(s1.p2 - s1.p1,s2.p2-s2.p1),0.0); } Point project(Segment s,Point p){ Vector base = s.p2 - s.p1; double r = dot(p-s.p1,base)/base.norm(); return s.p1 + base * r; } Point reflect(Segment s, Point p){ return p + (project(s,p) - p) * 2.0; } //????¨???¨????????§?????? static const int COUNTER_CLOCKWISE = 1; static const int CLOCKWISE = -1; static const int ONLINE_BACK = 2; static const int ONLINE_FRONT = -2; static const int ON_SEGMENT = 0; int ccw(Vector a, Vector b){ if(cross(a,b) > EPS)return COUNTER_CLOCKWISE; if(cross(a,b) < -EPS)return CLOCKWISE; if(dot(a,b) < -EPS)return ONLINE_BACK; if(a.norm() < b.norm())return ONLINE_FRONT; return ON_SEGMENT; } int ccw(Point p1,Point p2,Point p3){ Vector a = p2 - p1; Vector b = p3 - p1; if(cross(a,b) > EPS)return COUNTER_CLOCKWISE; if(cross(a,b) < -EPS)return CLOCKWISE; if(dot(a,b) < -EPS)return ONLINE_BACK; if(a.norm() < b.norm())return ONLINE_FRONT; return ON_SEGMENT; } //?????????????????? bool intersect(Point p1, Point p2, Point p3,Point p4){ return ccw(p1,p2,p3)*ccw(p1,p2,p4) <= 0 && ccw(p3,p4,p1)*ccw(p3,p4,p2) <= 0; } bool intersect(Segment s1,Segment s2){ return intersect(s1.p1,s1.p2, s2.p1,s2.p2); } //?????? Point getCrossPoint(Segment s1, Segment s2){ Vector base = s2.p2 - s2.p1; double d1 = abs(cross(base,s1.p1 - s2.p1));//?´??????§?¶???? double d2 = abs(cross(base,s1.p2 - s2.p1)); double t = d1 / (d1+d2); return s1.p1 + (s1.p2 - s1.p1)*t; } //{???,??´???,??????}^2????????¢ double getDistance(Point a,Point b){ return abs(a-b); } double getDistanceLP(Line l, Point p){ return abs(cross(l.p2 - l.p1, p-l.p1) / abs(l.p2 - l.p1)); } double getDistanceSP(Segment s,Point p){ if(dot(s.p2 - s.p1, p-s.p1) < 0.0) return abs(p-s.p1); if(dot(s.p1 - s.p2, p-s.p2) < 0.0) return abs(p-s.p2); return getDistanceLP(s,p); } double getDistance(Segment s1, Segment s2){ if(intersect(s1,s2)) return 0.0; return min(min(getDistanceSP(s1, s2.p1), getDistanceSP(s1, s2.p2)), min(getDistanceSP(s2, s1.p1), getDistanceSP(s2, s1.p2))); } pair<Point, Point> getCrossPoint(Circle c, Line l){ //assert(intersect(c,l)); Vector pr = project(l,c.c); Vector e = (l.p2 - l.p1)/abs(l.p2 - l.p1); double base = sqrt(c.r*c.r - norm(pr - c.c)); return make_pair(pr + e*base, pr - e*base); } int main(){ cin.tie(0); ios::sync_with_stdio(false); int cx,cy,r,q; cin>>cx>>cy>>r>>q; while(q--){ int a,b,c,d;cin>>a>>b>>c>>d; pair<Point,Point> ans = getCrossPoint(Circle(cx,cy,r), Line(a,b,c,d)); if(ans.first.x > ans.second.x)swap(ans.first,ans.second); else if(fabs(ans.first.x -ans.second.x) < EPS && ans.first.y > ans.second.y)swap(ans.first,ans.second); cout << fixed << setprecision(8) << ans.first.x << ' ' << ans.first.y << ' ' << ans.second.x << ' ' << ans.second.y << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; const long long md = 1e9 + 7; const long long MX = 2e5 + 5; const long long INF = 1e18; const long double PI = 4 * atan((long double)1); long long power_md(long long a, long long n) { long long res = 1; while (n) { if (n % 2) res = (res % md * a % md) % md; a = (a % md * a % md) % md; n /= 2; } res %= md; return res; } long long power(long long a, long long n) { long long res = 1; while (n) { if (n % 2) res *= a; a = a * a; n /= 2; } return res; } long long abst(long long a) { return ((a < 0) ? (-1 * a) : (a)); } class cmp_set { public: bool operator()(long long a, long long b) { return a > b; } }; vector<string> vec_splitter(string s) { s += ','; vector<string> res; while (!s.empty()) { res.push_back(s.substr(0, s.find(','))); s = s.substr(s.find(',') + 1); } return res; } void debug_out(vector<string> __attribute__((unused)) args, __attribute__((unused)) long long idx, __attribute__((unused)) long long LINE_NUM) { cerr << endl; } template <typename Head, typename... Tail> void debug_out(vector<string> args, long long idx, long long LINE_NUM, Head H, Tail... T) { if (idx > 0) cerr << ", "; else cerr << "Line(" << LINE_NUM << ") "; stringstream ss; ss << H; cerr << args[idx] << " = " << ss.str(); debug_out(args, idx + 1, LINE_NUM, T...); } long long fun(long long x, string s, long long pref[], long long suf[], string t) { for (long long i = 0; i <= (s.size() - x); i++) { long long l = i; long long r = i + x - 1; long long pf = 0, sf = 0; if (l > 0) pf = pref[l - 1]; if (r < s.size() - 1) sf = suf[r + 1]; if ((pf + sf) >= t.size()) return 1; } return 0; } int32_t main() { ios_base::sync_with_stdio(false), cin.tie(NULL); string s, t; cin >> s >> t; long long pref[(long long)s.size()], suf[(long long)s.size()]; long long j = 0; for (long long i = 0; i < s.size(); i++) { if (j == t.size()) pref[i] = t.size(); else { if (s[i] == t[j]) { if (i == 0) pref[i] = 1; else pref[i] = pref[i - 1] + 1; j++; } else { if (i == 0) pref[i] = 0; else pref[i] = pref[i - 1]; } } } j = t.size() - 1; for (long long i = s.size() - 1; i >= 0; i--) { if (j < 0) suf[i] = t.size(); else { if (s[i] == t[j]) { if (i == s.size() - 1) suf[i] = 1; else suf[i] = suf[i + 1] + 1; j--; } else { if (i == s.size() - 1) suf[i] = 0; else suf[i] = suf[i + 1]; } } } long long lo = 0, hi = s.size(), mid; while (lo < hi) { mid = lo + (hi - lo + 1) / 2; if (fun(mid, s, pref, suf, t)) lo = mid; else hi = mid - 1; } cout << lo << "\n"; return 0; }
4
#include <bits/stdc++.h> using namespace std; int main() { long long x, y, cx[120], cy[120], n, i, l = 0; string s; cin >> x >> y; getline(cin, s); getline(cin, s); n = s.size(); cx[0] = cy[0] = 0; if (x == 0 && y == 0) l = 1; for (i = 0; i < n; i++) { cx[i + 1] = cx[i]; cy[i + 1] = cy[i]; if (s[i] == 'R') cx[i + 1]++; if (s[i] == 'U') cy[i + 1]++; if (s[i] == 'D') cy[i + 1]--; if (s[i] == 'L') cx[i + 1]--; if (x == cx[i] && cy[i] == y) l = 1; } for (i = 0; i <= n; i++) { if (cx[n] == 0 && cy[n] == 0) break; if (cx[n] == 0 && (y - cy[i]) % cy[n] == 0 && (y - cy[i]) / cy[n] > 0 && cx[i] == x) l = 1; if (cx[n] != 0) if ((x - cx[i]) % cx[n] == 0 && cy[n] * ((x - cx[i]) / cx[n]) + cy[i] == y && ((x - cx[i]) / cx[n]) > 0) l = 1; } if (l == 1) cout << "Yes\n"; else cout << "No\n"; }
1
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long n, p, a[200005], f[200005][2][2][2], pw[200005]; long long read() { long long f = 0, w = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') w = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { f = f * 10 + ch - '0'; ch = getchar(); } return f * w; } signed main() { n = read(), p = read(); for (long long i = 1; i <= n; i++) a[i] = read(); pw[0] = 1; for (long long i = 1; i <= n; i++) pw[i] = pw[i - 1] * 2 % mod; f[0][0][0][0] = 1; for (long long i = 0; i < n; i++) { for (long long j = 0; j <= 1; j++) { for (long long k = 0; k <= 1; k++) { for (long long l = 0; l <= 1; l++) { if (!f[i][j][k][l]) continue; if (a[i + 1] != 1) { f[i + 1][j][k][l] = (f[i + 1][j][k][l] + f[i][j][k][l] * ((j == 0) ? 0 : pw[i - 1])) % mod; f[i + 1][j][k | 1][l ^ 1] = (f[i + 1][j][k | 1][l ^ 1] + f[i][j][k][l] * ((j == 0) ? pw[i] : pw[i - 1])) % mod; } if (a[i + 1] != 0) { f[i + 1][j][k][l] = (f[i + 1][j][k][l] + f[i][j][k][l] * ((k == 0) ? 0 : pw[i - 1])) % mod; f[i + 1][j | 1][k][l ^ 1] = (f[i + 1][j | 1][k][l ^ 1] + f[i][j][k][l] * ((k == 0) ? pw[i] : pw[i - 1])) % mod; } } } } } long long ans = 0; for (long long i = 0; i <= 1; i++) { for (long long j = 0; j <= 1; j++) { ans = (ans + f[n][i][j][p]) % mod; } } printf("%lld\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; const long long long_MAX = 9223372036854775807; const int intint_MAX = 2147483647; long long N; long long a[2100000]; int main() { long long i, j, cnt = 0; scanf("%I64d", &N); long long l = 0, r = long_MAX; for (i = 2; i <= 2097151; i++) { a[i] = i * i * i; } while (1) { if (l >= r) break; long long mid = (l + r) / 2; for (j = 2; j <= 2097151 && a[j] <= mid; j++) { cnt += (mid / a[j]); } if (cnt >= N) { r = mid; } else { l = mid + 1; } cnt = 0; } cnt = 0; for (j = 2; j <= 2097151; j++) { cnt += (l / a[j]); } if (cnt == N) { printf("%I64d", l); } else { printf("-1"); } }
3
#include "bits/stdc++.h" #include<unordered_map> #include<unordered_set> #pragma warning(disable:4996) using namespace std; vector<vector<int>>input(int N) { vector<vector<int>>v(N,vector<int>(N)); for (int i = 0; i < N; ++i) { string st;cin>>st; for (int j = 0; j < N; ++j) { v[i][j]=st[j]=='.'; } } return v; } vector<vector<int>>rotate(vector<vector<int>>v) { int N=v.size(); vector<vector<int>>nv(v.size(),vector<int>(v.size())); for (int i = 0; i < N; ++i) { for (int j = 0; j < N; ++j) { nv[i][j]=v[j][N-1-i]; } } return nv; } bool operator<(const vector<int>&l, const vector<int>&r) { for (int i = 0; i < l.size(); ++i) { if(l[i]<r[i])return true; else if(l[i]>r[i])return false; } return false; } struct UnionFind { vector<int> data; UnionFind(int size) : data(size, -1) { } bool unionSet(int x, int y) { x = root(x); y = root(y); if (x != y) { if (data[y] < data[x]) swap(x, y); data[x] += data[y]; data[y] = x; } return x != y; } int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); } int size(int x) { return -data[root(x)]; } }; pair<int, vector<int>>solve(pair<int, vector<int>>p, vector<vector<int>>field) { int dx[] = { -1,0,1,0 }; int dy[] = { 0,1,0,-1 }; const int start_id=p.first; const vector<int> v=p.second; int N=field.size(); UnionFind uf(N*(N+1)+1); for (int i = 0; i < N; ++i) { if (v[i] == start_id) { uf.unionSet(i,N*(N+1)); } for (int j = 0; j < N; ++j) { if (v[i] == v[j]) { uf.unionSet(i,j); } } if (p.second[i]&&field[i][0]) { uf.unionSet(i,i+N); } } for (int y = 0; y < N; ++y) { for (int x = 0; x < N; ++x) { for (int way = 0; way < 4; ++way) { int nx=x+dx[way]; int ny=y+dy[way]; if (nx >= 0 && nx < N&&ny >= 0 && ny < N) { if(field[y][x]&&field[ny][nx])uf.unionSet(x*N+N+y,nx*N+N+ny); } } } } vector<int>right_v(N); const int right_start=uf.root(N*(N+1)); for (int i = 0; i < N; ++i) { right_v[i]=uf.root(N*N+i); } map<int,int>mp; vector<int>right_nv(N); int id=1; for (int i = 0; i < N; ++i) { if (field[i][N - 1]) { if (mp.find(right_v[i]) == mp.end()) { mp[right_v[i]]=id++; } right_nv[i]=mp[right_v[i]]; } else { right_nv[i]=0; } } if (mp.find(right_start) == mp.end()) { return make_pair(-1,right_nv); } else { return make_pair(mp[right_start],right_nv); } } int main() { string st;cin>>st; int ans=0; char now('A'); for (auto ch : st) { if (ch > now) { now=ch; } else { ans++; now=ch; } } cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; long long sta(long long num, long long n, long long t, long long r, long long x) { t += (x - num + (num * 2)) * r; if (t > n) return 0; return 1; } long long bs(long long second, long long e, long long n, long long t, long long r) { long long in, mid, x = e; while (second <= e) { mid = (second + e) / 2; if (sta(mid, n, t, r, x)) { in = mid; second = mid + 1; } else e = mid - 1; } return in; } int main() { long long t, n; scanf("%lld", &t); while (t--) { scanf("%lld", &n); long long sum = 1, ans, total, x, r; for (long long i = 1;; i++) { sum *= 2; if (sum == n) { ans = i; break; } if (sum > n) { ans = i - 1; break; } } printf("%lld", ans), printf("\n"); total = 1, x = 1; for (long long i = 1; i <= ans; i++) { r = ans - i + 1; long long val = bs(0, x, n, total, r); x = x - val + (val * 2); total += (x); printf("%lld", val), printf(" "); } printf("\n"); } }
4
#include <bits/stdc++.h> using namespace std; vector<int> adj[1000001]; vector<int> Com; int SZ; bool vis[1000001]; void dfs(int v) { vis[v] = 1; SZ++; for (int i = (0); i < (adj[v].size()); ++i) if (!vis[adj[v][i]]) dfs(adj[v][i]); } int main(int argc, char **argv) { int N, M, K, C = 0; scanf("%d%d%d", &N, &M, &K); for (int i = (0); i < (M); ++i) { int x, y; scanf("%d%d", &x, &y); --x, --y; adj[x].push_back(y), adj[y].push_back(x); } for (int i = (0); i < (N); ++i) if (!vis[i]) SZ = 0, dfs(i), Com.push_back(min(SZ, K)), C++; sort(Com.begin(), Com.end()); int lo = 0, hi = C - 1, res = 1 << 29; int Q[C]; while (lo <= hi) { for (int i = (0); i < (C); ++i) Q[i] = -Com[i]; make_heap(Q, Q + C); int mi = (hi + lo) / 2; int ptr = C; for (int i = (0); i < (mi); ++i) { int X, Y; pop_heap(Q, Q + ptr); X = Q[ptr - 1]; ptr--; pop_heap(Q, Q + ptr); Q[ptr - 1] = max(Q[ptr - 1] + X, -K); push_heap(Q, Q + ptr); } vector<int> Qp(ptr); for (int i = (ptr - 1); i >= (0); --i) Qp[ptr - 1 - i] = -Q[i]; sort(Qp.begin(), Qp.end()); reverse(Qp.begin(), Qp.end()); long long V = Qp[0]; bool ok = 1; if (Qp[ptr - 1] == 1) { int Sp = ptr; for (int i = (1); i < (Sp); ++i) { V += Qp[i]; V -= 2; if (V < 0) { ok = 0; break; } } } if (ok) res = min(res, mi), hi = mi - 1; else lo = mi + 1; } cout << res << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; long long n, maxx, huifu_bird, huifu_next; long long number[1010]; long long cost[1010]; long long dp[2][10001]; int main() { while (cin >> n >> maxx >> huifu_bird >> huifu_next) { for (int i = 0; i < n; i++) cin >> number[i]; for (int i = 0; i < n; i++) cin >> cost[i]; for (int i = 0; i <= 10000; i++) dp[0][i] = -1; for (int i = 0; i <= 10000; i++) dp[1][i] = -1; for (int i = 0; i <= number[0]; i++) { if (maxx < cost[0] * i) break; long long a = maxx - cost[0] * i + huifu_next; if (a > maxx + huifu_bird * i) a = maxx + huifu_bird * i; dp[0 % 2][i] = a; } for (int i = 1; i < n; i++) { for (int j = 0; j <= 10000; j++) { if (j >= number[i] && dp[(i + 1) % 2][j - number[i]] == -1) break; long long ans = -1; for (int k = 0; k <= number[i]; k++) { if (j - k < 0) break; if (dp[(i + 1) % 2][j - k] >= cost[i] * k) { long long a = dp[(i + 1) % 2][j - k] - cost[i] * k + huifu_next; if (a > maxx + huifu_bird * j) a = maxx + huifu_bird * j; ans = max(a, ans); } } dp[i % 2][j] = ans; } } int ans = 0; for (int i = 10000; i >= 0; i--) { if (dp[(n - 1) % 2][i] != -1) { ans = i; break; } } cout << ans << endl; } return 0; }
5
#include <stdio.h> #include <algorithm> #include <vector> #include <bitset> using namespace std; bitset<16080> p[2]; char S[8080]; int main() { scanf ("%s",S); int d = -1, l = 0, x, s[2] = {0,}; p[1][0] = 1; for (int i=0;;i++){ if (S[i] == 'T' || S[i] == 0){ if (d == -1){ d = 0; p[0][2*l] = 1; } else p[d] |= p[d] << (2*l); s[d] += l; l = 0; d = !d; if (S[i] == 0) break; } else l++; } int X,Y; scanf ("%d %d",&X,&Y); X += s[0], Y += s[1]; if (0 <= X && p[0][X] && 0 <= Y && p[1][Y]) puts("Yes"); else puts("No"); return 0; }
0
#include <bits/stdc++.h> using namespace std; const double pi = 4.0 * atan(1.0); const int N = 5 + 1e5; long long num[N]; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } int main() { int i; long long n, sum = 0, now = 0, ans = 0; scanf("%I64d", &n); for (i = 1; i <= n; i++) { scanf("%I64d", &num[i]); sum += num[i]; } sort(num + 1, num + n + 1); for (int i = 1; i <= n; i++) { ans += i * num[i] - now + sum - now - num[i] - num[i] * (n - i); now += num[i]; } long long r = gcd(ans, n); printf("%I64d %I64d\n", ans / r, n / r); return 0; }
3
#include <bits/stdc++.h> using namespace std; long long i, i1, j, k, k1, t, n, m, res, flag[10], a, b, mt[110][110], s; char c; vector<array<long long, 6>> sl; vector<array<long long, 3>> cl; void mv(long long a, long long b, long long c, long long d, long long e, long long f) { sl.push_back({a, b, c, d, e, f}); mt[a][b] ^= 1; mt[c][d] ^= 1; mt[e][f] ^= 1; } void cl4(vector<long long> st) { long long i, l, x, s = 0; for (i = 1; i <= 4; i++) s += st[i]; if (s == 0) return; if (s == 3) { l = 0; cl.push_back({0, 0, 0}); x = cl.size(); x--; for (i = 1; i <= 4; i++) { if (st[i] == 1) { st[i] ^= 1; cl[x][l] = i; l++; } } } if (s == 2) { flag[1] = 0; l = 0; cl.push_back({0, 0, 0}); x = cl.size(); x--; for (i = 1; i <= 4; i++) { if (st[i] == 0 || (st[i] == 1 && flag[1] == 0)) { st[i] ^= 1; cl[x][l] = i; l++; if (st[i] == 0) flag[1] = 1; } } cl4(st); } if (s == 1) { flag[0] = 0; l = 0; cl.push_back({0, 0, 0}); x = cl.size(); x--; for (i = 1; i <= 4; i++) { if (st[i] == 1 || (st[i] == 0 && flag[0] < 2)) { st[i] ^= 1; cl[x][l] = i; l++; if (st[i] == 1) flag[0]++; } } cl4(st); } if (s == 4) { flag[1] = 0; l = 0; cl.push_back({0, 0, 0}); x = cl.size(); x--; for (i = 1; i <= 4; i++) { if (st[i] == 0 || (st[i] == 1 && flag[1] < 3)) { st[i] ^= 1; cl[x][l] = i; l++; if (st[i] == 0) flag[1]++; } } cl4(st); } } void init(vector<long long> st) { cl.clear(); cl4(st); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> t; while (t--) { cin >> n >> m; sl.clear(); for (i = 1; i <= n; i++) { for (j = 1; j <= m; j++) { cin >> c; if (c == '0') mt[i][j] = 0; else mt[i][j] = 1; } } for (i = 1; i <= n - 2; i++) { for (j = 1; j <= m - 1; j++) { if (mt[i][j] == 1 && mt[i][j + 1] == 1) { mv(i, j, i, j + 1, i + 1, j); } else if (mt[i][j] == 1 && mt[i][j + 1] == 0) { mv(i, j, i + 1, j, i + 1, j + 1); } else if (mt[i][j] == 0 && mt[i][j + 1] == 1) { mv(i, j + 1, i + 1, j, i + 1, j + 1); } } } if (m % 2) { if (mt[n - 1][m] == 1 && mt[n][m] == 1) { mv(n - 1, m, n, m - 1, n, m); } else if (mt[n - 1][m] == 1 && mt[n][m] == 0) { mv(n - 1, m - 1, n - 1, m, n, m - 1); } else if (mt[n - 1][m] == 0 && mt[n][m] == 1) { mv(n - 1, m - 1, n, m - 1, n, m); } } for (j = 1; j <= m - 1; j += 2) { vector<long long> st; vector<array<long long, 2>> fn; st.push_back(0); st.push_back(mt[n - 1][j]); st.push_back(mt[n - 1][j + 1]); st.push_back(mt[n][j]); st.push_back(mt[n][j + 1]); init(st); for (auto u : cl) { fn.clear(); for (i = 0; i < 3; i++) { if (u[i] == 1) fn.push_back({n - 1, j}); else if (u[i] == 2) fn.push_back({n - 1, j + 1}); else if (u[i] == 3) fn.push_back({n, j}); else if (u[i] == 4) fn.push_back({n, j + 1}); } sl.push_back({0, 0, 0, 0, 0, 0}); for (i = 0; i < 3; i++) { s = sl.size(); s--; sl[s][2 * i] = fn[i][0]; sl[s][2 * i + 1] = fn[i][1]; } } } s = sl.size(); cout << s << "\n"; for (auto u : sl) { for (i = 0; i < 6; i++) cout << u[i] << ' '; cout << "\n"; } } return 0; }
1
#include<iostream> #include<string> #include<vector> #include<algorithm> #include<numeric> #include <functional> using namespace std; #define all(i) i.begin(), i.end() #define rep(i,n) for(int i=0;i<n;i++) typedef pair<int, int> p; const int INF = 1e9; int dp[1001][1001]; int main() { int n; while (cin >> n && n) { vector<p> a(n); rep(i, n) { cin >> a[i].first >> a[i].second; } sort(all(a)); reverse(all(a)); int sum = 0; for (int i = 1; i < a.size(); i++) { sum += a[i].first; } int ans = 0; rep(i, n) { ans += a[i].first + a[i].second; } if (sum >= a[0].first) { cout << ans << endl; continue; } rep(i, 1001)rep(j, 1001) { dp[i][j] = -1; } int l = a[0].first - sum; function<int(int, int)> solve = [&](int m, int re) { if (re < 0)return INF; if (dp[m][re] != -1)return dp[m][re]; if (m == a.size()) { return re; } return dp[m][re] = min(solve(m + 1, re - a[m].second), solve(m + 1, re)); }; cout << ans + solve(1, l) << endl; } return 0; }
0
#include <iostream> #include <iomanip> #include <vector> #include <set> #include <algorithm> #include <string> #include <sstream> #define INPUT_FROM_FILE (0) #if INPUT_FROM_FILE #include <fstream> #endif bool solve(const std::vector<std::string> &hlines, int n, int m, int g, int d){ bool ret = false; int cur_p = m - 1; for(int y = 0; y < d; y++){ if(cur_p >= 1 && hlines[y][cur_p - 1] == '1'){ cur_p--; } else if(hlines[y][cur_p] == '1'){ cur_p++; } } ret = (cur_p + 1 == g); return ret; } int main(int argc, char **argv){ int n, m, g, d; std::string buf; #if INPUT_FROM_FILE std::ifstream ifs("test.txt"); //FILE *fp = fopen("test.txt", "r"); #endif #if INPUT_FROM_FILE while(ifs >> n){ #else while(std::cin >> n){ #endif if(n == 0){ break; } std::vector<std::string> hlines; #if INPUT_FROM_FILE ifs >> m >> g >> d; #else std::cin >> m >> g >> d; #endif #if INPUT_FROM_FILE std::getline(ifs, buf); #else std::getline(std::cin, buf); #endif for(int y = 0; y < d; y++){ #if INPUT_FROM_FILE std::getline(ifs, buf); #else std::getline(std::cin, buf); #endif hlines.push_back(buf); } if(solve(hlines, n, m, g, d)){ std::cout << "0" << std::endl; } else{ bool is_solved = false; for(int y = 0; y < d; y++){ for(int x = 0; x < n - 1; x++){ if(hlines[y][x] == '0'){ if((x == 0 || (x > 0 && hlines[y][x - 1] == '0')) && (x == n - 2 || (x <= n - 2 && hlines[y][x + 1] == '0'))){ std::vector<std::string> tmp = hlines; tmp[y][x] = '1'; if(solve(tmp, n, m, g, d)){ is_solved = true; std::cout << y + 1 << " " << x + 1 << std::endl; y = d; break; } } } } } if(!is_solved){ std::cout << "1" << std::endl; } } } #if INPUT_FROM_FILE //fclose(fp); #endif return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { string S; cin >> S; cout << (((int) S.find("7") == -1) ? "No" : "Yes") << "\n"; }
0
#include <bits/stdc++.h> int main() { int n; scanf("%d", &n); int answer = 0; while (n--) { int p, q; scanf("%d %d", &p, &q); answer += (p + 2 <= q); } printf("%d\n", answer); return 0; }
1
#include<math.h> #include<string> #define EPS (1e-10) #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 a) {return Point(x * a, y * a);} Point operator / (double a) {return Point(x / a, y / a);} double abs() {return sqrt(norm());} double norm() {return x * x + y * y;} bool operator < (const Point &p) const { return x != p.x ? x < p.x : y < p.y; } bool operator == (const Point &p) const{ return equals(x, p.x) && equals(y, p.y); } };typedef Point Vector; 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; } int ccw(Point p0, Point p1, Point p2){ Vector a = p1 - p0, b = p2 - p0; if (cross(a, b) > EPS) return 1; if (cross(a, b) < -EPS) return -1; if (dot(a, b) < -EPS) return 2; if (a.norm() < b.norm()) return -2; return 0; } #include<iostream> bool intersection(Point p0, Point p1, Point p2, Point p3){ if (ccw(p0, p1, p2) == 0 || ccw(p0, p1, p3) == 0 || ccw(p2, p3, p0) == 0 || ccw(p2, p3, p1) == 0) return true; if (ccw(p0, p1, p2) * ccw(p0, p1, p3) == -1 && ccw(p2, p3, p0) * ccw(p2, p3, p1) == -1) return true; return false; } int main(){ int q; cin >> q; int x0, y0, x1, y1, x2, y2, x3, y3; Point p0, p1, p2, p3; for (int i = 0; i < q; i++) { cin >> x0 >> y0 >> x1 >> y1 >> x2 >> y2 >> x3 >> y3; p0 = Point(x0, y0); p1 = Point(x1, y1); p2 = Point(x2, y2); p3 = Point(x3, y3); if(intersection(p0, p1, p2, p3)) cout << 1 << endl; else cout << 0 << endl; } }
0
#include <iostream> #include <vector> using namespace std; // struct BIT{ // int n; vector<long long> dat; // BIT(int n_){ init(n_); } // void init(int n_){ n=n_; dat=vector<long long>(n+1,0); } // void add(int k,int a){ k++; while(k<=n){ dat[k]+=a; k+=k&-k; } } //[0,k] long long query(int k){ k++; long long res=0; while(k){ res+=dat[k]; k-=k&-k; } return res; } // }; // int main(){ int N,Q; cin>>N>>Q; BIT bit(N); while(Q--){ int C; cin>>C; if(C==1){ int X,Y; cin>>X>>Y; X--;Y--; cout<<bit.query(Y)-bit.query(X-1)<<endl; }else{ int K,A; cin>>K>>A; K--; bit.add(K,A); } } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<long long> start; vector<long long> end; int min_index = 0; int max_index = 0; for (int i = 0; i < n; i++) { long long a, b; cin >> a >> b; start.push_back(a); end.push_back(b); } long long max = *max_element(end.begin(), end.end()); long long min = *min_element(start.begin(), start.end()); for (int i = 0; i < n; i++) { if (start[i] == min and end[i] == max) { cout << i + 1 << endl; return 0; } } cout << "-1" << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long mod = 1000000007LL; int n, k; int a[300005]; int main() { cin >> n >> k; for (int i = 1; i <= n; ++i) { scanf("%d", &a[i]); } vector<long long> rk; rk.clear(); long long sum = 0LL; for (int i = n; i > 1; --i) { sum += a[i]; rk.push_back(sum); } sum += a[1]; long long ans = sum; sort(rk.begin(), rk.end()); reverse(rk.begin(), rk.end()); for (int i = 0; i < k - 1; ++i) { ans += rk[i]; } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; int n, m; char G[301][301]; int dx[5] = {0, -1, 1, 0, 0}; int dy[5] = {0, 0, 0, -1, 1}; int valid(int i, int j) { if (i < 0 || i >= n || j < 0 || j >= m) return 0; return 1; } int isStar(int i, int j, int r) { for (int k = 0; k < 5; k++) { int x = i + dx[k] * r, y = j + dy[k] * r; if (!valid(x, y) || G[x][y] != '*') return 0; } return 1; } int main() { ios::sync_with_stdio(0); cin.tie(0); int k; cin >> n >> m >> k; for (int i = 0; i < n; i++) for (int j = 0; j < m; j++) cin >> G[i][j]; for (int radius = 1; radius <= 300; radius++) { for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { if (isStar(i, j, radius)) { k--; if (!k) { for (int d = 0; d < 5; d++) cout << i + dx[d] * radius + 1 << " " << j + dy[d] * radius + 1 << endl; return 0; } } } } } cout << -1 << endl; return 0; }
4
#include <iostream> using namespace std; int main() { int a[10001]; a[0] = 0; a[1] = 2; for(int i=2;i<10001;i++) { a[i] = a[i-1] + i; } int n; while(cin >> n) { cout << a[n] << endl; } return 0; }
0
#include <bits/stdc++.h> using namespace std; long long a[18]; long long go(long long n, long long i) { long long r = n / a[i]; n %= a[i]; if (n == 0) return (r * i); return (r * i) + min(i + go(a[i] - n, i - 1), go(n, i - 1)); } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long i, n; for (i = 1; i < 17; i++) a[i] = (a[i - 1] * 10ll) + 1ll; cin >> n; cout << go(n, 16) << "\n"; return 0; }
3
#include <bits/stdc++.h> using namespace std; long long check(long long a, long long b, long long c) { long long z = min(b, c); a += z; b -= z; c -= z; if (b == 0) b = c; if (b % 2 == 1) return 1; return a; } void lemon() { long long a, b, c; scanf("%I64d%I64d%I64d", &a, &b, &c); long long max = check(a, b, c); if (check(b, a, c) > max) max = check(b, a, c); if (check(c, a, b) > max) max = check(c, a, b); printf("%I64d\n", a + b + c - max); } int main() { lemon(); return 0; }
2
#include <bits/stdc++.h> using namespace std; int get(int n, int p) { int res = 0; while (n >= p) res += n / p, n /= p; return res; } template <class T> T power(T N, T P) { return (P == 0) ? 1 : N * power(N, P - 1); } void VC(vector<long long int>& v) { for (int i = 0; i < v.size(); i++) printf("%lld ", v[i]); printf("\n"); } template <typename T> void print(const T& v) { cerr << v << ' '; } template <typename T1, typename... T2> void print(const T1& first, const T2&... rest) { print(first); print(rest...); } long long BigMod(long long int a, long long int b, int M) { long long remainder, answer = 1; remainder = a % M; while (b != 0) { if (b % 2 == 1) answer = (answer * remainder) % M; remainder = (remainder * remainder) % M; b /= 2; } return answer; } bool cmp(int a, int b) { return a > b; } int main() { long long int t, x, y; cin >> t; while (t--) { cin >> x >> y; if (x == y) printf("YES\n"); else { if (x % 2) x--; if (x == y) printf("YES\n"); else if (x == 2) { if (y <= 3) printf("YES\n"); else printf("NO\n"); } else if (x == 0) printf("NO\n"); else printf("YES\n"); } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int calcLen(int a) { int res = 0; while (a) { res++; a /= 10; } return res; } int main() { int len = 0; char c; map<char, int> ar; while ((c = getchar()) != '\n') { ar[c]++; len++; } if (len == 2) { printf("%c\n", getchar()); return 0; } int l = calcLen(len); if (calcLen(len - l) != l) l--; l = len - l; len = l; while (l) { map<char, int>::iterator it = ar.find(l % 10 + '0'); it->second--; if (it->second == 0) ar.erase(it); l /= 10; } char t[1000000]; int lenT = 0; while ((c = getchar()) != '\n') { t[lenT++] = c; map<char, int>::iterator it = ar.find(c); it->second--; if (it->second == 0) ar.erase(it); } if (!ar.size()) { for (int i = 0; i < lenT; ++i) { printf("%c", t[i]); } printf("\n"); return 0; } map<char, int>::iterator it = ar.begin(); if (it->first == '0') { it++; } if (it == ar.end()) { it = ar.begin(); for (int i = 0; i < lenT; ++i) { printf("%c", t[i]); } for (int i = 0; i < it->second; ++i) { printf("0"); } printf("\n"); return 0; } char t1[1000000]; char t2[1000000]; char t3[1000000]; t1[0] = t2[0] = it->first; if (t[0] == '0') { for (int i = 0; i < len; ++i) { t3[i] = '9'; } } else { int cur = 0; for (int i = 0; i < lenT; ++i) t3[cur++] = t[i]; for (map<char, int>::iterator it2 = ar.begin(); it2 != ar.end(); ++it2) { for (int i = 0; i < it2->second; ++i) { t3[cur++] = it2->first; } } } it->second--; if (!it->second) { ar.erase(it); } int cur = 1; for (it = ar.begin(); it != ar.end() && it->first < t[0]; ++it) { for (int j = 0; j < it->second; ++j) { t1[cur++] = it->first; } } for (int i = 0; i < lenT; ++i) t1[cur++] = t[i]; for (; it != ar.end(); ++it) { for (int j = 0; j < it->second; ++j) { t1[cur++] = it->first; } } cur = 1; for (it = ar.begin(); it != ar.end() && it->first <= t[0]; ++it) { for (int j = 0; j < it->second; ++j) { t2[cur++] = it->first; } } for (int i = 0; i < lenT; ++i) t2[cur++] = t[i]; for (; it != ar.end(); ++it) { for (int j = 0; j < it->second; ++j) { t2[cur++] = it->first; } } if (strcmp(t1, t2) < 0) { if (strcmp(t1, t3) < 0) { for (int i = 0; i < len; ++i) { printf("%c", t1[i]); } printf("\n"); } else { for (int i = 0; i < len; ++i) { printf("%c", t3[i]); } printf("\n"); } } else { if (strcmp(t2, t3) < 0) { for (int i = 0; i < len; ++i) { printf("%c", t2[i]); } printf("\n"); } else { for (int i = 0; i < len; ++i) { printf("%c", t3[i]); } printf("\n"); } } }
6
#include<bits/stdc++.h> #define FOR(i,a,b) for(int i=a;i<=b;++i) using namespace std; const int mn=2006; int a[mn],id[mn]; long long f[mn][mn],ans; bool cmp(int x,int y) { return a[x]>a[y]; } int main() { int n; scanf("%d",&n); FOR(i,0,n-1) {scanf("%d",a+i);id[i]=i;} sort(id,id+n,cmp); FOR(i,0,n-1) FOR(j,0,n-i-1) { int k=i+j; f[i+1][j]=max(f[i+1][j],f[i][j]+1ll*a[id[k]]*abs(id[k]-i)); f[i][j+1]=max(f[i][j+1],f[i][j]+1ll*a[id[k]]*abs(id[k]-(n-j-1))); } FOR(i,0,n) ans=max(ans,f[i][n-i]); cout<<ans; }
0
#include <cstdio> int main() { long s; scanf("%d", &s); printf("%d:%d:%d\n", s / 3600, (s % 3600) / 60, s%60); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int oo = 0x3f3f3f3f; const double eps = 1e-9; int M; set<string> names; vector<int> childs[200]; map<string, int> gid; int getID(string in) { names.insert(in); if (gid.find(in) == gid.end()) { childs[M].clear(); gid[in] = M++; } return gid[in]; } long long isIn[200], isOf[200], border[200], spacing[200], hight[200], width[200]; bool vis[200]; void calc(int id) { if (vis[id]) return; vis[id] = true; if (isOf[id] == 0) return; if (int((childs[id]).size()) == 0) { hight[id] = width[id] = 0; return; } if (isOf[id] == 1) { hight[id] = 2 * border[id] + spacing[id] * (int((childs[id]).size()) - 1); width[id] = 2 * border[id]; long long maxW = 0; for (int i = (0); i < (int((childs[id]).size())); i++) { calc(childs[id][i]); maxW = max(maxW, width[childs[id][i]]); hight[id] += hight[childs[id][i]]; } width[id] += maxW; } else { hight[id] = 2 * border[id]; width[id] = 2 * border[id] + spacing[id] * (int((childs[id]).size()) - 1); long long maxH = 0; for (int i = (0); i < (int((childs[id]).size())); i++) { calc(childs[id][i]); maxH = max(maxH, hight[childs[id][i]]); width[id] += width[childs[id][i]]; } hight[id] += maxH; } } int main() { M = 0; names.clear(); gid.clear(); int N; cin >> N; for (int kk = (0); kk < (N); kk++) { string in1, in2; cin >> in1; if (in1 == "Widget") { cin >> in2; for (int i = (0); i < (int((in2).size())); i++) if (in2[i] == '(' || in2[i] == ',' || in2[i] == ')') in2[i] = ' '; istringstream is(in2); is >> in2; int id = getID(in2); is >> width[id] >> hight[id]; isIn[id] = -1; isOf[id] = 0; } else if (in1 == "VBox") { cin >> in1; int id = getID(in1); border[id] = spacing[id] = 0; isIn[id] = -1; isOf[id] = 1; } else if (in1 == "HBox") { cin >> in1; int id = getID(in1); border[id] = spacing[id] = 0; isIn[id] = -1; isOf[id] = 2; } else { for (int i = (0); i < (int((in1).size())); i++) if (in1[i] == '(' || in1[i] == '.' || in1[i] == ')') in1[i] = ' '; istringstream is(in1); is >> in1; int pid = getID(in1); string op; is >> op; if (op == "pack") { is >> in1; int cid = getID(in1); childs[pid].push_back(cid); } else if (op == "set_spacing") { int sp; is >> sp; spacing[pid] = sp; } else { int bo; is >> bo; border[pid] = bo; } } } for (int i = (0); i < (M); i++) vis[i] = false; for (int i = (0); i < (M); i++) calc(i); for (__typeof__((names).begin()) it = (names).begin(); it != (names).end(); it++) { string cur = *it; int id = gid[cur]; cout << cur << " " << width[id] << " " << hight[id] << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int n, m, q; vector<int> a, b, c, d; int ans = 0; void dfs(vector<int> p){ if((int)p.size() == n + 1) { int temp = 0; for(int i = 0; i < q; i++) { if(p[b[i]] - p[a[i]] == c[i]) temp += d[i]; } ans = max(ans, temp); return; } p.push_back(p.back()); while(p.back() <= m) { dfs(p); p.back()++; } } int main(){ scanf("%d%d%d", &n, &m, &q); a = b = c = d = vector<int>(q); for(int i = 0; i < q; i++) scanf("%d%d%d%d", &a[i], &b[i], &c[i], &d[i]); dfs(vector<int> (1, 1)); printf("%d\n", ans); }
0
#include <bits/stdc++.h> const long MAXN = 100000; using namespace std; long dp[MAXN + 5]; long max(long a, long b) { if (a > b) return a; else return b; } int main() { long i, j, n, a[MAXN + 5], m, k; bool ok; scanf("%ld", &n); m = 0; for (i = 0; i < n; i++) { scanf("%ld", &a[i]); dp[a[i]] = dp[a[i] - 1] + 1; m = max(m, dp[a[i]]); } printf("%ld\n", n - m); return 0; }
3
#include<bits/stdc++.h> #define rep(i,n) for(int i=0;i<(int)(n);i++) #define init(a) memset((a),0,sizeof(a)) using namespace std; typedef double D; typedef vector<D> P; const D EPS = 1e-8; int n,m,R; P sp[2020],tp[20],ep; D sr[2020],tb[20],p[20]; vector<int> rm[20]; inline P mul(P p, const D &d){ rep(i,p.size())p[i] *= d; return p; } inline P add(P a, const P &b){ rep(i,a.size())a[i] += b[i]; return a; } inline D dis2(const P &a, const P &b){ D x = a[0]-b[0], y = a[1]-b[1], z = a[2]-b[2]; return x*x+y*y+z*z; } inline D score(const int &i){ return tb[i]/dis2(ep,tp[i]); } inline int intersect(const int &p, const int &c){ P l = ep, r = tp[p]; D r2 = sr[c]*sr[c]; bool f1 = (dis2(sp[c],l)<r2+EPS), f2 = (dis2(sp[c],r)<r2+EPS); if(f1&&f2)return 0; if(f1||f2)return 1; rep(i,30){ P m1 = mul(add(mul(l,2),r),1.0/3), m2 = mul(add(l,mul(r,2)),1.0/3); D d1 = dis2(sp[c],m1), d2 = dis2(sp[c],m2); if(d1<d2)r = m2; else l = m1; } if(dis2(sp[c],l)<r2+EPS)return 1; return 0; } D rec(int d,int num, D sum, vector<int> &use){ if(d==m)return sum; D res = rec(d+1,num,sum,use); vector<int> tmp = use; rep(i,rm[d].size()){ if(!use[rm[d][i]]){ num++; use[rm[d][i]] = 1; } } if(num<=R)res = max(res,rec(d+1,num,sum+p[d],use)); use = tmp; return res; } int main(){ while(scanf("%d%d%d",&n,&m,&R),n){ rep(i,n){ sp[i].resize(3); rep(j,3)scanf("%lf",&sp[i][j]); scanf("%lf",&sr[i]); } rep(i,m){ tp[i].resize(3); rep(j,3)scanf("%lf",&tp[i][j]); scanf("%lf",&tb[i]); } ep.resize(3); rep(i,3)scanf("%lf",&ep[i]); rep(i,m){ rm[i].clear(); rep(j,n){ if(intersect(i,j))rm[i].push_back(j); } } rep(i,m)p[i] = score(i); vector<int> use(n,0); cout << fixed << setprecision(10) << rec(0,0,0,use) << endl; } }
0
#include <bits/stdc++.h> using namespace std; int main() { long int n, i, j, a[100001]; cin >> n; if (n % 2 == 0) { cout << "-1" << endl; } else { for (i = 0; i < n; i++) { cout << i << " "; a[i] = i; } cout << endl; for (i = 1; i < n; i++) { cout << i << " "; a[i - 1] += i; } cout << "0" << endl; for (i = 0; i < n; i++) { cout << a[i] % n << " "; } cout << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; const int mod = 998244353; int power[1000010], a[1000010]; int main() { power[0] = 1; for (int i = 1; i < 1000010; ++i) power[i] = power[i - 1] * 2 % mod; int n, ans = 0; scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); ans = a[n]; for (int i = 1; i < n; ++i) ans = (ans + 1LL * a[i] * power[n - i - 1] % mod * (n + 2 - i) % mod) % mod; printf("%d\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") bool iseven(long long int n) { if ((n & 1) == 0) return true; return false; } void print(long long int t) { cout << t; } void print(int t) { cout << t; } void print(string t) { cout << t; } void print(char t) { cout << t; } void print(double t) { cout << t; } void print(long double t) { cout << t; } template <class T, class V> void print(pair<T, V> p); template <class T> void print(vector<T> v); template <class T> void print(set<T> v); template <class T, class V> void print(map<T, V> v); template <class T> void print(multiset<T> v); template <class T, class V> void print(pair<T, V> p) { cout << "{"; print(p.first); cout << ","; print(p.second); cout << "}"; } template <class T> void print(vector<T> v) { cout << "[ "; for (T i : v) { print(i); cout << " "; } cout << "]"; } template <class T> void print(set<T> v) { cout << "[ "; for (T i : v) { print(i); cout << " "; } cout << "]"; } template <class T> void print(multiset<T> v) { cout << "[ "; for (T i : v) { print(i); cout << " "; } cout << "]"; } template <class T, class V> void print(map<T, V> v) { cout << "[ "; for (auto i : v) { print(i); cout << " "; } cout << "]"; } void solve() { long long int n; cin >> n; bool flag = false; vector<long long int> ans; long long int temp; if (iseven(n)) { n /= 2; temp = n * n; ans.push_back(temp - 1); ans.push_back(temp + 1); } else { temp = n * n; ans.push_back(temp / 2); ans.push_back((temp / 2) + 1); } if (find(ans.begin(), ans.end(), 0) != ans.end()) { cout << -1; exit(0); } for (long long int elem : ans) { cout << elem << " "; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); solve(); return 0; }
3
#include <bits/stdc++.h> using namespace std; long long mod(long long x) { return ((x % 1000000007 + 1000000007) % 1000000007); } long long add(long long a, long long b) { return mod(mod(a) + mod(b)); } long long mul(long long a, long long b) { return mod(mod(a) * mod(b)); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; t = 1; cin >> t; while (t--) { long long n; cin >> n; vector<long long> v(n); for (int i = 0; i < n; i++) cin >> v[i]; long long ans = 0; for (int i = 1; i <= 32; i++) { long long temp = 0; for (long long j = 0; j < n; j++) { int z = log2(v[j]) + 1; if (z == i) temp++; } ans += (temp * (temp - 1)) / 2; } cout << ans << "\n"; } }
2
#include <stdio.h> int main(void) { int n, s = 0; scanf("%d", &n); for (int i = 1; i <= n; i++) { s += i; if (s >= n) { for (int j = 1; j <= i; j++) { if (s - n == j) continue; printf("%d\n", j); } break; } } }
0
#include <bits/stdc++.h> using namespace std; struct IO { char buf[(1 << 20)], *p1, *p2; char pbuf[(1 << 20)], *pp; IO() : p1(buf), p2(buf), pp(pbuf) {} inline char gc() { return getchar(); if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, (1 << 20), stdin); return p1 == p2 ? ' ' : *p1++; } inline bool blank(char ch) { return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t'; } template <class T> inline void read(T &x) { register double tmp = 1; register bool sign = 0; x = 0; register char ch = gc(); for (; !(ch >= '0' && ch <= '9'); ch = gc()) if (ch == '-') sign = 1; for (; (ch >= '0' && ch <= '9'); ch = gc()) x = x * 10 + (ch - '0'); if (ch == '.') for (ch = gc(); (ch >= '0' && ch <= '9'); ch = gc()) tmp /= 10.0, x += tmp * (ch - '0'); if (sign) x = -x; } inline void read(char *s) { register char ch = gc(); for (; blank(ch); ch = gc()) ; for (; !blank(ch); ch = gc()) *s++ = ch; *s = 0; } inline void read(char &c) { for (c = gc(); blank(c); c = gc()) ; } template <class t> inline void write(t x) { if (x < 0) putchar('-'), write(-x); else { if (x > 9) write(x / 10); putchar('0' + x % 10); } } } io; const long long N = 3005; long long n, m, a[N], b[N], f[N][N], g[N], ans; signed main() { io.read(n); for (long long i = (1); i <= (n); i++) { io.read(a[i]); a[i] -= i; b[i] = a[i]; } sort(b + 1, b + n + 1); m = unique(b + 1, b + n + 1) - b - 1; memset(f, 127 / 3, sizeof(f)); for (long long i = (1); i <= (m); i++) { f[1][i] = abs(a[1] - b[i]); if (i == 1) g[i] = f[1][i]; else g[i] = min(g[i - 1], f[1][i]); } for (long long i = (2); i <= (n); i++) for (long long j = (1); j <= (m); j++) { f[i][j] = abs(a[i] - b[j]) + g[j]; if (j == 1) g[j] = f[i][j]; else g[j] = min(g[j - 1], f[i][j]); } ans = 1e18; for (long long i = (1); i <= (m); i++) ans = min(ans, f[n][i]); printf("%lld\n", ans); return 0; }
3
#include <bits/stdc++.h> using namespace std; int c[1410][1410]; int dp[1410][1410]; int main(void) { int h, w; cin >> h >> w; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { cin >> c[i][j]; } } int ans = 0; for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) { if (!i || !j) { dp[i][j] = c[i][j] ? 0 : 1; } else { dp[i][j] = c[i][j] ? 0 : min(dp[i - 1][j - 1], min(dp[i - 1][j], dp[i][j - 1])) + 1; } ans = max(ans, dp[i][j]); } } cout << ans * ans << endl; return 0; }
0
#include<iostream> #include <algorithm> using namespace std; int main() { int t,n,i; cin>>t; while(t--){ cin>>n; int arr[n]; for(i=0;i<n;i++) cin>>arr[i]; sort(arr,arr+n); int mino = (int)1e9; int idx ; idx = upper_bound(arr,arr+n,0)-arr; for(i=1;i<idx;i++){ mino=min(mino,abs(arr[i]-arr[i-1])); } int cnt = idx; if(idx!=n && arr[i]<=mino) cnt++; cout << cnt << "\n"; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m; cin >> n >> m; vector<string> a(n); for (int i = 0; i < n; i++) cin >> a[i]; long long ans = 0; for (int i = 1; i < n - 1; i++) { int w = 0; int wasc1 = -1, wasc2 = -1, wasc = -1; int wasl = -1; for (int j = 0; j < m; j++) { if (a[i - 1][j] == a[i][j]) { w = 0; wasc1 = -1; wasc2 = -1; wasl = -1; wasc = -1; continue; } int len = 1; while (i + len - 1 < n && a[i + len - 1][j] == a[i][j]) len++; len--; int l1 = i - len; int r1 = i - 1; int l2 = i + len; int r2 = i + len + len - 1; if (r2 >= n) { w = 0; wasc1 = -1; wasc2 = -1; wasl = -1; wasc = -1; continue; } if (l1 < 0) { w = 0; wasc1 = -1; wasc2 = -1; wasl = -1; wasc = -1; continue; } int c1 = a[l1][j]; bool ok1 = true; for (int x = l1; x <= r1; x++) if (a[x][j] != c1) { ok1 = false; break; } int c2 = a[l2][j]; bool ok2 = true; for (int x = l2; x <= r2; x++) if (a[x][j] != c2) { ok2 = false; break; } if (!ok1 || !ok2) { w = 0; continue; } if (wasc1 == c1 && wasc2 == c2 && wasl == len && wasc == a[i][j]) { w++; } else { w = 1; wasc1 = c1; wasc2 = c2; wasl = len; wasc = a[i][j]; } ans += w; } } cout << ans; return 0; }
3
#include <bits/stdc++.h> using namespace std; void solve() { long long n, x; cin >> n >> x; bool already = false; vector<long long> a(n, 0); for (long long i = 0; i < n; i++) { cin >> a[i]; if (a[i] == x) already = true; } sort(a.begin(), a.end()); if (a[0] == a[n - 1] && a[0] == x) { cout << 0 << '\n'; } else { long long sum = 0; if (already) { cout << 1 << '\n'; return; } for (long long i = 0; i < n; i++) sum += a[i] - x; if (sum == 0) cout << 1 << '\n'; else cout << 2 << '\n'; } return; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; cin >> t; while (t--) { solve(); } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); }; set<string> wordSet; string str; int n, m, i; cin >> n >> m; for (i = 1; i <= n; i++) { cin >> str; wordSet.insert(str); } int commonCnt = 0; for (i = 1; i <= m; i++) { cin >> str; if (wordSet.count(str) > 0) { commonCnt++; } } int word1 = n - commonCnt + (commonCnt + 1) / 2; int word2 = m - commonCnt + (commonCnt) / 2; if (word1 > word2) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { int h, w; cin >> h >> w; char flag[h][w]; int u = h - 1, d = 0, r = 0, l = w - 1; for (int a = 0; a < h; a++) { for (int b = 0; b < w; b++) { cin >> flag[a][b]; if (flag[a][b] == '*') { u = min(u, a); d = max(d, a); l = min(l, b); r = max(r, b); } } } for (int a = u; a <= d; a++) { for (int b = l; b <= r; b++) { cout << flag[a][b]; } cout << endl; } }
1
#include <bits/stdc++.h> using namespace std; int mark[101]; int main() { int n, l, r, nowl, nowr, ans = 0; cin >> n >> l >> r; for (int i = 1; i < n; i++) { cin >> nowl >> nowr; for (int j = nowl; j < nowr; j++) mark[j] = true; } for (int i = l; i < r; i++) if (!mark[i]) ans++; cout << ans; }
1
#include <bits/stdc++.h> int main() { long long int ch[100000]; char arr[1000000]; char kh[1000000], bh, ag; int cp[400000]; long int a = 0, b, c, d, e = 0, g, i = 0, tep = 0, temp = 0, j = 0, k = 0, l = 0, m = 0, n = 0; scanf("%ld %ld", &a, &d); for (i = 0; i < a; i++) { j = 0; scanf("%ld%ld", &b, &c); j = c > d ? (b - (c - d)) : b; if (i == 0 || j > k) k = j; } printf("%ld\n", k); return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int t, sum; cin >> t; string a; vector<int> index_of_zero, index_of_one; int flag; while (t--) { sum = 0; flag = 0; cin >> a; for (int i = 0; i < a.size(); i++) { if (a[i] == '0') flag = 1; else { flag = 0; break; } } if (flag == 1) { cout << 0 << endl; continue; } flag = 0; for (int i = 0; i < a.size(); i++) { if (a[i] == '0') index_of_zero.push_back(i); else if (a[i] == '1') index_of_one.push_back(i); } for (int i = 0; i < index_of_one.size() - 1; i++) { if (index_of_one[i + 1] == (index_of_one[i] + 1)) flag = 1; else { flag = 0; break; } } if (flag == 1) { cout << 0 << endl; index_of_one.clear(); index_of_zero.clear(); continue; } else { for (int i = 0; i < index_of_one.size() - 1; i++) { if (index_of_one[i + 1] != (index_of_one[i] + 1)) { sum += index_of_one[i + 1] - index_of_one[i] - 1; } } } cout << sum << endl; index_of_one.clear(); index_of_zero.clear(); } }
1
#include<bits/stdc++.h> using namespace std; int main() { int t; cin>>t; while(t--) { int n; cin>>n; string s; cin>>s; int x = s.size(); int flag = 0; if(s.substr(0,4) == "2020") flag = 1; else if(s.substr(x-4,x) == "2020") flag = 1; else { for(int i = 1;i<4;i++) { if(s.substr(0,i) + s.substr(x-4+i,x) == "2020") flag = 1; } } if(flag == 1) cout<<"YES"<<endl; else cout<<"NO"<<endl; } }
2
#include <bits/stdc++.h> using namespace std; long long n, k, A, B; long long dpf(long long x) { if (x < k) return (x - 1) * A; long long r, a; r = x % k; x -= r; a = (x - x / k) * A; return dpf(x / k) + r * A + min(a, B); } int main() { cin >> n >> k >> A >> B; k > 1 ? cout << dpf(n) << "\n" : cout << (n - 1) * A << "\n"; }
2
#include <bits/stdc++.h> using namespace std; const int N = 3e5 + 5, M = 10; int n, m, a[N][M], b[N], t[1 << M], x, y; inline bool check(int mid) { memset(b, 0, sizeof b); memset(t, 0, sizeof t); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) b[i] = (b[i] << 1) | (a[i][j] >= mid); t[b[i]] = i; } for (int i = 1; i < (1 << m); i++) for (int j = 1; j < (1 << m); j++) if ((i | j) == (1 << m) - 1 && t[i] && t[j]) { x = t[i]; y = t[j]; return true; } return false; } int main() { cin >> n >> m; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) cin >> a[i][j]; int lb = -1, rb = 1e9; while (lb < rb) { int mid = lb + rb + 1 >> 1; if (check(mid)) lb = mid; else rb = mid - 1; } cout << x << ' ' << y << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; FILE *in, *out; int N, M, K, PW; int w[15]; int num[5000]; vector<vector<pair<int, int> > > ar(4100, vector<pair<int, int> >(4100, pair<int, int>())); int arson[4100]; int val[4100]; int read() { int i, j, k, t, z, v; fscanf(in, "%d%d%d", &N, &M, &K); for (i = 0; i < N; i++) fscanf(in, "%d", &w[i]); char temp[16]; for (i = 0; i < M; i++) { fscanf(in, "%s", temp); for (j = 0, k = 1, t = 0; j < N; j++) { if (temp[j] == '1') t += k; k *= 2; } num[t]++; } PW = (1 << N); for (i = 0; i < PW; i++) { for (j = 0, k = 1, t = 0; j < N; j++, k *= 2) { if ((i & k)) t += w[j]; } val[i] = t; } for (i = 0; i < PW; i++) { for (k = 0; k < PW; k++) { if (num[k] == 0) continue; v = val[((~(i ^ k)) & ((1 << N) - 1))]; ar[i][arson[i]] = make_pair(v, num[k]); arson[i]++; } sort(ar[i].begin(), ar[i].begin() + arson[i]); for (j = 1; j < arson[i]; j++) { ar[i][j].second += ar[i][j - 1].second; } } for (i = 0; i < K; i++) { fscanf(in, "%s%d", temp, &v); for (j = 0, k = 1, t = 0; j < N; j++) { if (temp[j] == '1') t += k; k *= 2; } int ind = (int)(lower_bound(ar[t].begin(), ar[t].begin() + arson[t], make_pair(v + 1, -1)) - ar[t].begin()); if (ind == 0) printf("%d\n", 0); else printf("%d\n", ar[t][ind - 1].second); } return 0; } int main() { in = stdin; out = stdout; read(); return 0; }
4
#include <bits/stdc++.h> using namespace std; using ll = long long; ll n,a,x,dp[55][2505],ans=0; int main(){ cin>>n>>a; dp[0][0]=1; for(int i=1;i<=n;i++){ cin>>x; for(int j=i-1;j>=0;j--){ for(int k=2500;k>=0;k--){ if(k+x<=2500){ dp[j+1][k+x]+=dp[j][k]; } } } } for(int i=1;i<=n;i++){ ans+=dp[i][a*i]; } cout << ans << endl; }
0
#include <bits/stdc++.h> #define ll long long int #define mp make_pair #define pb push_back #define ff first #define ss second #define mod 1000000007 #define lim 100005 #define nfs_mw ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); using namespace std; int dp[1<<18][2]; void merge(int big[2], int small[2]) { for(int i=0; i<2; i++){ if(small[i]>big[1]) big[1] = small[i]; if(big[0]<big[1]) swap(big[0], big[1]); } } int main() { nfs_mw; int i,j,n,mask,out = 0; cin>>n; for(i=0; i<(1<<n); i++) cin>>dp[i][0]; for(i=0; i<n; i++) for(mask=0; mask<(1<<n); mask++) if(mask&(1<<i)) merge(dp[mask], dp[mask^(1<<i)]); for(i=1; i<(1<<n); i++) cout<<(out = max(out, dp[i][0] + dp[i][1]))<<"\n"; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, k; vector<int> adj[200005]; int dist[200005], sz[200005]; bool convoy[200005]; vector<pair<int, int> > vec; long long ans = 0; void calc(int u, int p, int len) { sz[u] = 1; dist[u] = len; for (auto v : adj[u]) { if (v == p) continue; calc(v, u, len + 1); sz[u] += sz[v]; } vec.push_back(make_pair(dist[u] - sz[u], u)); } void dfs(int u, int p, long long cnt) { if (convoy[u]) ans += 1LL * cnt; else cnt += 1; for (auto v : adj[u]) { if (v == p) continue; dfs(v, u, cnt); } } int main() { scanf("%d %d", &n, &k); for (int i = 1; i < n; i++) { int u, v; scanf("%d %d", &u, &v); adj[u].push_back(v); adj[v].push_back(u); } calc(1, -1, 0); sort(vec.rbegin(), vec.rend()); for (int i = 0; i < k; i++) { int node = vec[i].second; convoy[node] = true; } dfs(1, -1, 0); cout << ans << endl; }
1
#include <iostream> #include <string> #include <cmath> #include<algorithm> #include<stack> #include<queue> #include<map> #include<set> #include<iomanip> #define _USE_MATH_DEFINES #include <math.h> #include <functional> #include<complex> using namespace std; #define rep(i,x) for(ll i=0;i<x;i++) #define repn(i,x) for(ll i=1;i<=x;i++) typedef long long ll; const ll INF = 1e17; const ll MOD = 998244353; const ll MAX = 4000001; const double eps = 1E-18; ll max(ll a, ll b) { if (a > b) { return a; } return b; } ll min(ll a, ll b) { if (a > b) { return b; } return a; } ll gcd(ll a, ll b) { if (b == 0) { return a; } if (a < b) { return gcd(b, a); } return gcd(b, a % b); } ll lcm(ll a, ll b) { return a / gcd(a, b) * b; } struct edge { ll ind; ll fr; ll to; ll d; }; class mint { long long x; public: mint(long long x = 0) : x((x% MOD + MOD) % MOD) {} mint operator-() const { return mint(-x); } mint& operator+=(const mint& a) { if ((x += a.x) >= MOD) x -= MOD; return *this; } mint& operator-=(const mint& a) { if ((x += MOD - a.x) >= MOD) x -= MOD; return *this; } mint& operator*=(const mint& a) { (x *= a.x) %= MOD; return *this; } mint operator+(const mint& a) const { mint res(*this); return res += a; } mint operator-(const mint& a) const { mint res(*this); return res -= a; } mint operator*(const mint& a) const { mint res(*this); return res *= a; } mint pow(ll t) const { if (!t) return 1; mint a = pow(t >> 1); a *= a; if (t & 1) a *= *this; return a; } // for prime MOD mint inv() const { return pow(MOD - 2); } mint& operator/=(const mint& a) { return (*this) *= a.inv(); } mint operator/(const mint& a) const { mint res(*this); return res /= a; } friend ostream& operator<<(ostream& os, const mint& m) { os << m.x; return os; } }; mint pw(mint a, ll b) { if (b == 0) { return 1; } mint ret = pw(a, b>>1); ret *= ret; if (b & 1) { ret *= a; } return ret; } typedef vector<ll> vll; typedef vector<vector<ll>> vvll; typedef vector<vector<vector<ll>>> vvvll; typedef vector<mint> vmint; typedef vector<vector<mint>> vvmint; typedef vector<vector<vector<mint>>> vvvmint; ////////////////////////////////////// vector<mint> frac, ifrac; void finit(ll X) { frac.assign(X + 1, 1); ifrac.assign(X + 1, 1); repn(i, X) { frac[i] = frac[i - 1] * i; ifrac[i] = ifrac[i - 1] / i; } } mint com(ll n, ll m) { if (n < 0 || m < 0 || n < m) { return 0; } return frac[n] * ifrac[m] * ifrac[n - m]; } int main() { ll N, X; cin >> N >> X; mint ans = 0; finit(N); rep(i, N + 1) { rep(j, N+1) { if (i + j > N) { continue; } vll c(3); c[0] = i; c[1] = j; c[2] = N - i - j; ll S = c[1] + 2 * c[2]; mint t = 0; if (S < X) { t += com(c[1] + c[2], c[1]) * com(N, c[0]); } else if((S-X)%2==1) { if (j == 0) { t += com(N, c[0]); } else { ll K = (S - X + 1) / 2; t += com(c[1] + c[2] - 2 * K, c[1]) * com(N, c[0]); } } ans += t; //cout << i << " " << j << " " << c[2] << " " << t << endl; } } cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; struct point { int x; int y; double distance; double prob; point() { x = 0; y = 0; distance = 0; prob = 0; } point(int x1, int y1) { x = x1; y = y1; } double setDistance(point& p) { distance = sqrt(1.0 * ((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y))); return distance; } void setProb(double R) { if (distance <= R) prob = 1; else prob = exp(1 - (distance * distance) / (R * R)); } }; const int MAXN = 110; point points[MAXN]; double dp[MAXN][MAXN]; double dodp(int N, int K, double R) { for (int i = 1; i <= N; i++) points[i].setProb(R); for (int i = 0; i <= N; i++) for (int j = 0; j <= N; j++) dp[i][j] = 0; dp[0][0] = 1; for (int i = 1; i <= N; i++) for (int j = 0; j <= N; j++) { dp[i][j] = (1 - points[i].prob) * dp[i - 1][j]; if (j > 0) dp[i][j] += points[i].prob * dp[i - 1][j - 1]; } double ans = 0; for (int j = K; j <= N; j++) ans += dp[N][j]; return ans; } int main() { int N, K, e; cin >> N >> K >> e; double target = 1 - e / 1000.0; int gx, gy; cin >> gx >> gy; point gzero(gx, gy); double maxR = 0; for (int i = 1; i <= N; i++) { int px, py; cin >> px >> py; points[i] = point(px, py); double cur = points[i].setDistance(gzero); if (cur > maxR) maxR = cur; } double Rr = maxR; double Rl = 0; double R; for (int i = 0; i < 100; i++) { R = (Rl + Rr) / 2.0; double cur = dodp(N, K, R); if (cur - target < 10e-9 && cur >= target) break; else if (cur > target) Rr = R; else Rl = R; } printf("%0.6lf\n", R); return 0; }
4
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, fh = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') fh = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 1) + (x << 3) + ch - '0'; ch = getchar(); } return x * fh; } const long long N = 5e5 + 5; long long a[N], n, pos, sum, q; signed main() { cin >> q; while (q--) { long long op = read(); if (op == 1) { long long x = read(); a[++n] = x; while (pos < n - 1 && a[pos + 1] <= (sum + a[n]) / (pos + 1.0)) sum += a[++pos]; } else printf("%.8lf\n", a[n] - (sum + a[n]) / (pos + 1.0)); } return 0; }
5
#include <iostream> #include <string> using namespace std; class dice{ public: int d[7]; dice(){ //?????????????????????????????? } void east(){ d[0]=d[1],d[1]=d[4],d[4]=d[6],d[6]=d[3],d[3]=d[0]; } void north(){ d[0]=d[5],d[5]=d[1],d[1]=d[2],d[2]=d[6],d[6]=d[0]; } void turn(){ d[0]=d[2],d[2]=d[4],d[4]=d[5],d[5]=d[3],d[3]=d[0]; } }; int main(){ dice D1,D2; int c=0; for(int i=1; i<7; i++)cin >> D1.d[i]; for(int i=1; i<7; i++)cin >> D2.d[i]; for(int i=1; i<7; i++){ if(D1.d[1]==D2.d[1]){ for(int j=0; j<4; j++){ if(D1.d[2]==D2.d[2]){ if( D1.d[3]==D2.d[3] && D1.d[4]==D2.d[4] && D1.d[5]==D2.d[5] && D1.d[6]==D2.d[6] )c++; } D2.turn(); } } if(i%2==1)D2.east(); else D2.north(); } if(c==0)cout << "No" <<endl; else cout << "Yes" <<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); int n; cin >> n; bool valid = true; int first = 1, second = 2; for (int i = 0; i < n; i++) { int a; cin >> a; if (a != first && a != second) { valid = false; } if (first == a) { if (a == 1) { if (second == 2) second = 3; else second = 2; } else if (a == 2) { if (second == 1) second = 3; else second = 1; } else { if (second == 1) second = 2; else second = 1; } } else { if (a == 1) { if (first == 2) first = 3; else first = 2; } else if (a == 2) { if (first == 1) first = 3; else first = 1; } else { if (first == 1) first = 2; else first = 1; } } } if (valid) cout << "YES"; else cout << "NO"; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int N = 2000 + 5; int n, m; struct hash_table { unsigned long long seed; unsigned long long Hash[N], temp[N]; void Set(unsigned long long num) { seed = num; } void work(char *s, int n) { temp[0] = 1; Hash[0] = 0; for (int i = 1; i <= n; i++) temp[i] = temp[i - 1] * seed; for (int i = 1; i <= n; i++) Hash[i] = (Hash[i - 1] * seed + (s[i] - 'a')); } unsigned long long get(int l, int r) { return Hash[r] - Hash[l - 1] * temp[r - l + 1]; } } h; unsigned long long g1[N], g2[N][N]; int main() { while (~scanf("%d%d", &n, &m)) { h.Set(163); for (int i = 1; i <= n; i++) { char s[N]; scanf("%s", s + 1); h.work(s, m); g1[i] = h.get(1, m); } for (int i = 1; i <= m; i++) { char s[N]; scanf("%s", s + 1); h.work(s, n); for (int j = 1; j <= n - m + 1; j++) g2[i][j] = h.get(j, j + m - 1); } int ansi = -1, ansj = -1; for (int i = 1; i <= n - m + 1; i++) { for (int j = 1; j <= n - m + 1; j++) { bool OK = 1; for (int k = 1; k <= m; k++) if (g2[k][j] != g1[i + k - 1]) { OK = 0; break; } if (OK) { ansi = i, ansj = j; break; } } } printf("%d %d\n", ansi, ansj); } return 0; }
1
#include <bits/stdc++.h> using namespace std; long long inf = 2E9 + 100; vector<int> grafo[160000]; int marked[160000]; int main() { memset(marked, 0, sizeof(marked)); int n; int m; cin >> n; cin >> m; int a, b; for (int i = 0; i < m; ++i) { scanf("%d %d", &a, &b); a--; b--; grafo[a].push_back(b); grafo[b].push_back(a); } long long k; for (int i = 0; i < n; ++i) { if (!marked[i]) { k = -1; stack<int> s; s.push(i); set<int> f; while (s.size()) { auto v = s.top(); s.pop(); k++; if (marked[v]) continue; f.insert(v); marked[v] = 1; for (auto u : grafo[v]) { s.push(u); } } long long hh = f.size(); if (k != (hh * (hh - 1))) { printf("NO\n"); return 0; } } } printf("YES\n"); }
2
#include <bits/stdc++.h> using namespace std; int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x; bool check1() { if (!(a == b && b == c && c == d)) return false; if (!(i == j && j == k && k == l)) return false; if (!(o == p && p == s && s == q)) return false; if (!(m == n && n == u && u == w)) return false; if (!(v == x && x == e && e == f)) return false; if (!(g == h && h == r && r == t)) return false; return true; } bool check2() { if (!(m == n && n == r && r == t)) return false; if (!(o == p && p == v && v == x)) return false; if (!(u == w && w == g && g == h)) return false; if (!(q == s && s == e && e == f)) return false; if (!(a == b && b == c && c == d)) return false; if (!(i == j && j == k && k == l)) return false; return true; } bool check3() { if (!(m == n && n == o && o == p)) return false; if (!(e == f && f == g && g == h)) return false; if (!(a == b && b == w && w == x)) return false; if (!(u == v && v == i && i == j)) return false; if (!(k == l && l == s && s == t)) return false; if (!(q == r && r == c && c == d)) return false; return true; } bool check4() { if (!(m == n && n == o && o == p)) return false; if (!(e == f && f == g && g == h)) return false; if (!(a == b && b == s && s == t)) return false; if (!(q == r && r == i && i == j)) return false; if (!(w == x && x == k && k == l)) return false; if (!(u == v && v == c && c == d)) return false; return true; } bool check5() { if (!(q == r && r == s && s == t)) return false; if (!(u == v && v == w && w == x)) return false; if (!(a == c && c == n && n == p)) return false; if (!(m == o && o == j && j == l)) return false; if (!(i == k && k == f && f == h)) return false; if (!(e == g && g == b && b == d)) return false; return true; } bool check6() { if (!(q == r && r == s && s == t)) return false; if (!(u == v && v == w && w == x)) return false; if (!(a == c && c == f && f == h)) return false; if (!(e == g && g == j && j == l)) return false; if (!(i == k && k == n && n == p)) return false; if (!(m == o && o == b && b == d)) return false; return true; } bool check() { if (check1()) return true; if (check2()) return true; if (check3()) return true; if (check4()) return true; if (check5()) return true; if (check6()) return true; return false; } int main() { scanf("%d%d%d%d", &v, &x, &u, &w); scanf("%d%d%d%d", &b, &d, &a, &c); scanf("%d%d%d%d", &r, &t, &q, &s); scanf("%d%d%d%d", &n, &p, &m, &o); scanf("%d%d%d%d", &f, &h, &e, &g); scanf("%d%d%d%d", &j, &l, &i, &k); if (check()) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; double ans, pre, f[55][55], p[55], s; int n, m, a[55]; long long c[55][55]; void init() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; i++) scanf("%d", &a[i]); } void work() { int i, j, k, kk; p[0] = 1; for (i = 1; i <= n; i++) p[i] = p[i - 1] * 1.0 / m; c[0][0] = 1; for (i = 1; i <= n; i++) { c[i][0] = 1; for (j = 1; j <= n; j++) c[i][j] = c[i - 1][j - 1] + c[i - 1][j]; } for (kk = 1; kk <= n; kk++) { for (i = 0; i <= m; i++) for (j = 0; j <= n; j++) f[i][j] = 0; f[0][0] = 1; for (i = 0; i < m; i++) for (j = 0; j <= n; j++) for (k = 0; k <= a[i + 1] * kk && j + k <= n; k++) f[i + 1][j + k] += f[i][j] * p[k] * c[n - j][k]; ans += (f[m][n] - pre) * kk; pre = f[m][n]; } printf("%.20lf\n", ans); } int main() { init(); work(); return 0; }
3
#include <bits/stdc++.h> using namespace std; int tt; int n, r, sum; int main() { scanf("%d", &tt); for (int test = 0; test < tt; ++test) { scanf("%d%d", &n, &r), --r; int sum = 0; for (int i = 0; i < n - 1; ++i) { int x; scanf("%d", &x), --x; sum = (sum + n - x) % n; } printf("%d\n", (sum + r) % n + 1); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int main() { int64_t N,ans=0,flag=0; cin >> N; if(N%2==1){ ans = 0; }else{ for (int64_t i= 10; i <= N;i*=5){ ans+=N/i; } } cout << ans; }
0
#include <bits/stdc++.h> using namespace std; using ld = long double; constexpr ld eps = 1e-8; namespace geometry3d { using std::abs; // important using point = std::array<ld, 3>; std::ostream& operator<<(std::ostream& os, point const& p) { os << '(' << p[0] << ", " << p[1] << ", " << p[2] << ')'; return os; } class segment { public: segment(point p1, point p2) : a(p1), b(p2) {} point a, b; }; class line { public: line(point p1, point p2) : a(p1), b(p2) {} line(segment s) : a(s.a), b(s.b) {} point a, b; }; class sphere { public: sphere() : p({0, 0, 0}), r(0) {} sphere(point p_, ld r_) : p(p_), r(r_) {} point p; ld r; }; point operator-(point const& a, point const& b) { point ret; for(int i=0; i<3; ++i) { ret[i] = a[i] - b[i]; } return ret; } point operator+(point const& a, point const& b) { point ret; for(int i=0; i<3; ++i) { ret[i] = a[i] + b[i]; } return ret; } point operator*(point const& a, ld b) { point ret = a; for(int i=0; i<3; ++i) { ret[i] *= b; } return ret; } point operator*(ld a, point const& b) { return b*a; } bool eq(ld a, ld b) { return (std::abs(a - b) < eps); } point make_point(ld a, ld b, ld c) { return point{{a, b, c}}; } ld dot(point a, point b) { return std::inner_product(std::begin(a), std::end(a), std::begin(b), 0.0); } point cross(point a, point b) { return make_point(a[1]*b[2] - a[2]*b[1], a[2]*b[0] - a[0]*b[2], a[0]*b[1] - a[1]*b[0]); } ld norm(point a) { // |a|^2 return dot(a, a); } ld abs(point a) { // |a| return std::sqrt(dot(a, a)); } // verified point proj(line l, point p) { ld t = dot(p - l.a, l.a - l.b) / norm(l.a - l.b); return l.a + t*(l.a - l.b); } bool isis_sps(sphere const& sp, segment const& s) { auto a = s.a - sp.p; auto b = s.b - sp.p; auto p = proj(line(s), sp.p) - sp.p; if(abs(a) + eps < sp.r && abs(b) + eps < sp.r && abs(p) + eps < sp.r) { // contain return false; } if(abs(p) > sp.r + eps) { return false; } if(abs(a) + eps < sp.r || abs(b) + eps < sp.r) { return true; } if(abs(abs(a - p) + abs(b - p) - abs(b - a)) < eps) { return true; } return false; } } // namespace geometry3d using namespace geometry3d; int main() { int N, M, R; while(cin >> N >> M >> R, N) { vector<sphere> ss(N); for(int i=0; i<N; ++i) { cin >> ss[i].p[0] >> ss[i].p[1] >> ss[i].p[2] >> ss[i].r; } vector<point> tp(M); vector<ld> b(M); for(int i=0; i<M; ++i) { cin >> tp[i][0] >> tp[i][1] >> tp[i][2] >> b[i]; } point E; cin >> E[0] >> E[1] >> E[2]; vector<vector<int>> remove_b(M); for(int i=0; i<M; ++i) { segment s(E, tp[i]); int cnt = 0; for(int j=0; j<N; ++j) { if(isis_sps(ss[j], s)) { cnt++; remove_b[i].push_back(j); } } } ld res = 0; for(int bit=0; bit<(1 << M); ++bit) { ld B = 0; vector<bool> removed(N); for(int i=0; i<M; ++i) { if((bit >> i) & 1) { for(int j=0; j<remove_b[i].size(); ++j) { removed[remove_b[i][j]] = true; } B += b[i] / norm(E - tp[i]); } } if(count(removed.begin(), removed.end(), true) <= R) { res = max(res, B); } } cout << fixed << setprecision(10) << res << endl; } }
0
#include <bits/stdc++.h> using namespace std; using ll = long long; namespace { void test() { ll x; int n; cin >> n >> x; vector<ll> v(n); for (int i = 0; i < n; ++i) cin >> v[i]; sort(v.begin(), v.end()); ll sum = 0; for (auto el : v) sum += el; for (int i = 0; i < n; ++i) { ll cnt = n - i; if (sum >= cnt * x) { return void(cout << (cnt) << endl); } sum -= v[i]; } return void(cout << (0) << endl); } } // namespace int main() { int t; cin >> t; while (t--) test(); return 0; }
2
#include <bits/stdc++.h> using namespace std; const int maxn = 4e6 + 10; int n, p; int inc(int a, int b) { a += b; return a >= p ? a - p : a; } int dec(int a, int b) { a -= b; return a < 0 ? a + p : a; } int mul(int a, int b) { return 1ll * a * b % p; } int f[maxn], c[maxn]; void add(int l, int r, int x) { if (r > n) r = n + 1; c[l] = inc(c[l], x); c[r + 1] = dec(c[r + 1], x); } int main() { scanf("%d%d", &n, &p); f[1] = 1; int cur = 0; for (int i = 1; i <= n; ++i) { cur = inc(cur, c[i]); f[i] = inc(f[i], cur); f[i] = inc(f[i], f[i - 1]); for (int j = 2; 1ll * j * i <= n; ++j) { add(i * j, i * j + j - 1, f[i]); } f[i] = inc(f[i], f[i - 1]); } printf("%d\n", dec(f[n], f[n - 1])); return 0; }
4
#include<stdio.h> int main(){ int n,i,j,c=1,a[100010]; scanf("%d", &n); for(i=1;i<=n;i++)scanf("%d",&a[i]); for(j=a[1];j!=2;c++){ j=a[j]; if(c>n){ printf("-1");return 0; } } printf("%d",c);return 0;}
0
#include <bits/stdc++.h> #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") using namespace std; constexpr int maxl = 21; int step[100000][maxl]; signed main() { std::ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<vector<int>> graph(n); for (int i = 0; i < n - 1; i++) { int a, b; cin >> a >> b; a--; b--; graph[a].push_back(b); graph[b].push_back(a); } vector<int> tin(n), tout(n), depth(n, -1); int time = 0; function<void(int, int)> dfs = [&](int v, int p) { step[v][0] = p; depth[v] = depth[p] + 1; tin[v] = time++; for (int ne : graph[v]) if (ne != p) dfs(ne, v); tout[v] = time++; }; dfs(0, 0); for (int l = 0; l < maxl - 1; l++) for (int v = 0; v < n; v++) step[v][l + 1] = step[step[v][l]][l]; auto anc = [&](int high, int low) { return tin[high] <= tin[low] and tout[low] <= tout[high]; }; auto lca = [&](int u, int v) { if (anc(u, v)) return u; if (anc(v, u)) return v; for (int i = maxl - 1; i >= 0; i--) if (!anc(step[u][i], v)) u = step[u][i]; return step[u][0]; }; auto dist = [&](int u, int v) { return depth[u] + depth[v] - 2 * depth[lca(u, v)]; }; int tt; cin >> tt; while (tt--) { int x, y, a, b, k; cin >> x >> y >> a >> b >> k; x--; y--; a--; b--; vector<int> ds = {dist(a, b), dist(a, x) + dist(y, b) + 1, dist(b, x) + dist(y, a) + 1}; bool gud = false; for (int d : ds) gud |= d % 2 == k % 2 and d <= k; cout << (gud ? "YES" : "NO") << "\n"; } }
5
#include <bits/stdc++.h> using namespace std; int dx[8] = {1, -1, 0, 0, 1, -1, 1, -1}; int dy[8] = {0, 0, -1, 1, 1, -1, -1, 1}; const double EPS = 1e-8; const int N = 1000006; bool vis[N]; int p[N]; int cnt[N], a, t[N], n, pos, ff[3]; vector<int> e[N]; int each = 0; void dfs(int idx) { vis[idx] = 1; for (int i = 0; i < e[idx].size(); i++) { int to = e[idx][i]; if (!vis[to]) { dfs(to); cnt[idx] += cnt[to]; } } cnt[idx] += t[idx]; if (cnt[idx] == each && ff[pos] == -1 && p[idx]) { ff[pos] = idx; } } int main() { scanf("%d", &n); int st = -1; for (int i = 1; i <= n; i++) { scanf("%d%d", &a, &t[i]); each += t[i]; if (!a) { st = i; continue; } p[i] = a; e[i].push_back(a); e[a].push_back(i); } if (st == -1) { st = 1; } if (each % 3 != 0) { puts("-1"); return 0; } each /= 3; pos = 0; ff[0] = ff[1] = -1; dfs(st); memset(vis, 0, sizeof(vis)); memset(cnt, 0, sizeof(cnt)); if (ff[pos] == -1) { puts("-1"); return 0; } vis[ff[0]] = 1; pos++; dfs(st); if (ff[pos] == -1) { puts("-1"); return 0; } memset(vis, 0, sizeof(vis)); memset(cnt, 0, sizeof(cnt)); vis[ff[0]] = 1; vis[ff[1]] = 1; pos++; dfs(st); if (cnt[st] != each) { puts("-1"); return 0; } cout << ff[0] << " " << ff[1] << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { string str; int num = 0; cin >> str; int len = str.length(); for (int i = 0; i < len; i++) { if (str[i] == '4' || str[i] == '7') { num++; } } if (num == 4 || num == 7) cout << "YES" << endl; else cout << "NO" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; const long long M = 1e9 + 7; long long n, k, x, t, y, it; pair<long long, long long> a[2500][2500]; int main() { cin >> n; for (int i = 1; i <= n * 2; i++) { if (i > 1) { a[i][0].first = a[i - 1][1].second; a[i][0].second = max(a[i - 1][1].first + 1, a[i][0].first); } for (int j = 1; j <= i; j++) { if (j + 1 <= i - 1) { a[i][j].second = max(a[i - 1][j - 1].second + a[i - 1][j + 1].first, a[i - 1][j - 1].first + a[i - 1][j + 1].second) + 1; a[i][j].first = (a[i - 1][j - 1].second + a[i - 1][j + 1].second); } else { a[i][j].second = a[i - 1][j - 1].first + 1; a[i][j].first = a[i - 1][j - 1].second; } a[i][j].first %= M; a[i][j].second %= M; } } cout << max(a[n * 2][0].second, a[n * 2][0].first); }
4
#include <bits/stdc++.h> using namespace std; const long long INF = 1e9; const int N = 1000010; int n, m, h, s, t; long long ans, res, point, dis[N]; int cnt = 1, cur[N], head[N]; struct edge { int to, nxt; long long dis; } e[N << 1]; inline void add(int u, int v, long long w) { e[++cnt].to = v; e[cnt].dis = w; e[cnt].nxt = head[u]; head[u] = cnt; e[++cnt].to = u; e[cnt].dis = 0; e[cnt].nxt = head[v]; head[v] = cnt; } inline int bfs() { memset(dis, 0, sizeof(dis)); queue<int> q; q.push(s); dis[s] = 1; cur[s] = head[s]; while (!q.empty()) { int x = q.front(); q.pop(); for (register int i = head[x]; i; i = e[i].nxt) { int v = e[i].to; if (e[i].dis > 0 && !dis[v]) { q.push(v); cur[v] = head[v]; dis[v] = dis[x] + 1; if (v == t) return 1; } } } return 0; } inline int dfs(int x, long long flow) { if (x == t) return flow; long long k, rest = flow; for (register int i = cur[x]; i && flow; i = e[i].nxt) { cur[x] = i; int v = e[i].to; if (e[i].dis && (dis[v] == dis[x] + 1)) { k = dfs(v, min(rest, e[i].dis)); e[i].dis -= k; e[i ^ 1].dis += k; rest -= k; if (rest == 0) break; } } return flow - rest; } inline int idx(int x, int y) { return (x - 1) * (h + 2) + y; } int main() { scanf("%d%d%d", &n, &h, &m); t = N - 10; for (int i = 1; i <= n; i++) ans += h * h; for (register int i = 1; i <= n; i++) for (register int j = 1; j <= h + 1; j++) add(idx(i, j), idx(i, j + 1), h * h - (j - 1) * (j - 1)); point = n * (h + 2); for (register int i = 1; i <= n; i++) { add(s, idx(i, 1), INF); add(idx(i, h + 2), t, INF); } for (int i = 1, l, r, x, c; i <= m; i++) { scanf("%d%d%d%d", &l, &r, &x, &c); if (x == h) continue; ++point; add(point, t, c); for (register int j = l; j <= r; j++) add(idx(j, x + 2), point, INF); } while (bfs()) { res += dfs(s, INF); } printf("%lld", ans - res); return 0; }
1
#include <bits/stdc++.h> using namespace std; const int M = 100000 + 10; int n, a[M], cnt = 0; bool vis[M]; vector<int> Circle[M]; vector<pair<int, int> > ans; int top = 0; bool flag[M]; inline void Pre() { memset(flag, 0, sizeof(flag)); for (int i = 2; i <= 100000; ++i) if (!flag[i]) for (int j = 2; j * i <= 100000; ++j) flag[i * j] = 1; } inline void DFS(int x, int y) { if (!flag[y - x + 1]) { ans.push_back(make_pair(x, y)); return; } for (int i = y - 1; i > x; --i) if (!flag[i - x + 1]) { ans.push_back(make_pair(x, i)); DFS(i, y); ans.push_back(make_pair(x, i)); return; } } int main() { Pre(); scanf("%d", &n); memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); for (int i = 1; i <= n; ++i) if (!vis[i] && a[i] != i) { ++cnt; Circle[cnt].push_back(a[i]); vis[i] = 1; int tmp = a[i]; while (tmp != i) { Circle[cnt].push_back(a[tmp]); vis[tmp] = 1; tmp = a[tmp]; } } for (int i = 1; i <= cnt; ++i) { int x = Circle[i][Circle[i].size() - 1]; for (int j = 0; j < Circle[i].size() - 1; ++j) { int tmp = Circle[i][j]; DFS(x, tmp); } } cout << ans.size() << endl; for (int i = 0; i < ans.size(); ++i) { if (ans[i].first > ans[i].second) swap(ans[i].first, ans[i].second); cout << ans[i].first << " " << ans[i].second << endl; } return 0; }
3