solution
stringlengths
52
181k
difficulty
int64
0
6
#include<bits/stdc++.h> using namespace std; #define jxc 1000000007 #define MN 100010 int Fac[MN],Ifac[MN],Inv[MN],a[MN],f[MN]; int A(int x,int y){return ((x+y)%jxc+jxc)%jxc;} int D(int x,int y){return ((x-y)%jxc+jxc)%jxc;} int M(int x,int y){return ((1ll*x*y)%jxc+jxc)%jxc;} int C(int x,int y){return x<y?0:M(Fac[x],M(Ifac[y],Ifac[x-y]));} int P(int x,int y){ int res=1; for(;y;y>>=1,x=M(x,x))if(y&1)res=M(res,x); return res; } int main(){ Fac[0]=Inv[1]=Fac[1]=Ifac[1]=Ifac[0]=1; for(int i=2;i<=100005;++i){ Fac[i]=M(Fac[i-1],i); Inv[i]=M(Inv[jxc%i],(jxc-jxc/i)); Ifac[i]=M(Ifac[i-1],Inv[i]); } int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=m;++i)scanf("%d",a+i); f[0]=1<<n; sort(a+1,a+m+1); for(int i=m;i;--i){ for(int j=(1<<n)-1;j;--j){ for(int k=0;k<n;++k){ if(j&(1<<k)){ f[j]=D(f[j],M(f[j-(1<<k)],M(C((1<<n)-a[i]-j+(1<<k),(1<<k)-1),Fac[1<<k]))); } } } }int res=0; for(int i=0;i<1<<n;++i)res=A(res,M(f[i],Fac[(1<<n)-i-1])); cout<<res; return 0; }
0
#include <bits/stdc++.h> using namespace std; int r[1005], l[1005], ans[1005]; int main() { int n, res = 0; cin >> n; for (int(i) = (0); (i) < (n); i++) cin >> l[i]; for (int(i) = (0); (i) < (n); i++) cin >> r[i]; for (int(i) = (0); (i) < (n); i++) res = max(res, l[i] + r[i] + 1); for (int(i) = (0); (i) < (n); i++) ans[i] = res - l[i] - r[i]; for (int(i) = (0); (i) < (n); i++) { int ansl = 0, ansr = 0; for (int(j) = (0); (j) < (n); j++) { if (j > i && ans[j] > ans[i]) ansl++; if (i > j && ans[j] > ans[i]) ansr++; } if (ansl == r[i] && ansr == l[i]) continue; else { cout << "NO\n"; return 0; } } cout << "YES\n"; for (int(i) = (0); (i) < (n); i++) cout << ans[i] << " "; return 0; }
3
#include <bits/stdc++.h> using namespace std; int A[100010]; int main() { int t, ans, i, m, x, y; scanf("%d", &m); int sum2 = 0; for (i = 0; i < m; i++) { scanf("%d", &A[i]); sum2 = sum2 + A[i]; } scanf("%d %d", &x, &y); ans = 0; int sum = 0; for (i = 0; i < m; i++) { sum = sum + A[i]; if (sum >= x && sum <= y && sum2 - sum >= x && sum2 - sum <= y) { if (i + 2 <= m) ans = i + 2; } } printf("%d", ans); }
1
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long a, b, c, d, e, i, j, k, l; cin >> a >> b; if (a <= b) cout << abs(a - b) << endl; else if (a % 2 + b % 2 == 1) cout << 1 << endl; else cout << 0 << endl; } }
1
#include <bits/stdc++.h> double t[5], temp; double x_1, y_1, r1, x2, y2, r2, x3, y3, r3, X = 0.0, Y = 0.0; int f = 0; double check(double dx, double dy) { double ret = 0.0; t[0] = sqrt(((dx - x_1) * (dx - x_1)) + ((dy - y_1) * (dy - y_1))) / r1; t[1] = sqrt(((dx - x2) * (dx - x2)) + ((dy - y2) * (dy - y2))) / r2; t[2] = sqrt(((dx - x3) * (dx - x3)) + ((dy - y3) * (dy - y3))) / r3; ret += ((t[0] - t[1]) * (t[0] - t[1])); ret += ((t[1] - t[2]) * (t[1] - t[2])); ret += ((t[2] - t[0]) * (t[2] - t[0])); return ret; } int main() { double s; scanf("%lf%lf%lf", &x_1, &y_1, &r1); scanf("%lf%lf%lf", &x2, &y2, &r2); scanf("%lf%lf%lf", &x3, &y3, &r3); X = (x2 + x3 + x_1) / 3 + 0.1; Y = (y2 + y3 + y_1) / 3 + 0.1; for (s = 1; s > (1e-6); f = 0) { temp = check(X, Y); if (temp > check(X + s, Y)) X += s, f = 1; else if (temp > check(X - s, Y)) X -= s, f = 1; else if (temp > check(X, Y + s)) Y += s, f = 1; else if (temp > check(X, Y - s)) Y -= s, f = 1; if (!f) s *= 0.7; } if (check(X, Y) < (1e-5)) printf("%.5lf %.5lf\n", X, Y); return 0; }
3
#include <bits/stdc++.h> #pragma GCC optimize("O3") using namespace std; const double EPS = 1e-12, INF = 1e9; pair<double, double> operator+(const pair<double, double> &a, const pair<double, double> &b) { return make_pair(a.first + b.first, a.second + b.second); } pair<double, double> operator-(const pair<double, double> &a, const pair<double, double> &b) { return make_pair(a.first - b.first, a.second - b.second); } pair<double, double> operator*(const pair<double, double> &a, const double &b) { return make_pair(a.first * b, a.second * b); } pair<double, double> operator/(const pair<double, double> &a, const double &b) { return make_pair(a.first / b, a.second / b); } double dot(const pair<double, double> &a, const pair<double, double> &b) { return a.first * b.first + a.second * b.second; } double cross(const pair<double, double> &a, const pair<double, double> &b) { return a.first * b.second - a.second * b.first; } double abs2(const pair<double, double> &a) { return dot(a, a); } double abs(const pair<double, double> &a) { return sqrt(dot(a, a)); } int ori(const pair<double, double> &a, const pair<double, double> &b, const pair<double, double> &c) { double res = cross(b - a, c - a); if (fabs(res) < EPS) return 0; return res > 0 ? 1 : -1; } bool collinearity(const pair<double, double> &p1, const pair<double, double> &p2, const pair<double, double> &p3) { return fabs(cross(p1 - p3, p2 - p3)) < EPS; } bool btw(const pair<double, double> &p1, const pair<double, double> &p2, const pair<double, double> &p3) { if (!collinearity(p1, p2, p3)) return 0; return dot(p1 - p3, p2 - p3) < EPS; } bool seg_intersect(const pair<double, double> &p1, const pair<double, double> &p2, const pair<double, double> &p3, const pair<double, double> &p4) { int a123 = ori(p1, p2, p3); int a124 = ori(p1, p2, p4); int a341 = ori(p3, p4, p1); int a342 = ori(p3, p4, p2); if (a123 == 0 && a124 == 0) return btw(p1, p2, p3) || btw(p1, p2, p4) || btw(p3, p4, p1) || btw(p3, p4, p2); return a123 * a124 <= 0 && a341 * a342 <= 0; } pair<double, double> intersect(const pair<double, double> &p1, const pair<double, double> &p2, const pair<double, double> &p3, const pair<double, double> &p4) { double a123 = cross(p2 - p1, p3 - p1); double a124 = cross(p2 - p1, p4 - p1); return (p4 * a123 - p3 * a124) / (a123 - a124); } bool intri(pair<double, double> p, pair<double, double> p1, pair<double, double> p2, pair<double, double> p3) { double org = fabs(cross(p2 - p1, p3 - p1)), a = fabs(cross(p1 - p, p2 - p)), b = fabs(cross(p1 - p, p3 - p)), c = fabs(cross(p2 - p, p3 - p)); if (fabs(a + b + c - org) < EPS) return 1; return 0; } pair<double, double> arr[3][105]; vector<pair<pair<double, double>, pair<double, double>>> v; void insert(pair<double, double> a, pair<double, double> b, pair<double, double> p1, pair<double, double> p2, pair<double, double> p3) { int flag = 0; pair<pair<double, double>, pair<double, double>> rt; if (intri(a, p1, p2, p3)) if (flag) rt.second = a, flag = 2; else rt.first = a, flag = 1; if (intri(b, p1, p2, p3)) if (flag) rt.second = b, flag = 2; else rt.first = b, flag = 1; if (seg_intersect(a, b, p1, p2)) { auto tmp = intersect(a, b, p1, p2); if (abs2(tmp - a) >= EPS && abs2(tmp - b) >= EPS) if (flag) rt.second = tmp, flag = 2; else rt.first = tmp, flag = 1; } if (seg_intersect(a, b, p3, p2)) { auto tmp = intersect(a, b, p3, p2); if (abs2(tmp - a) >= EPS && abs2(tmp - b) >= EPS) if (flag) rt.second = tmp, flag = 2; else rt.first = tmp, flag = 1; } if (seg_intersect(a, b, p1, p3)) { auto tmp = intersect(a, b, p1, p3); if (abs2(tmp - a) >= EPS && abs2(tmp - b) >= EPS) if (flag) rt.second = tmp, flag = 2; else rt.first = tmp, flag = 1; } if (flag == 2) { if (rt.first > rt.second) swap(rt.first, rt.second); v.push_back(rt); } } double cal() { sort(v.begin(), v.end()); double rt = 0; pair<double, double> nw = make_pair(-INF, -INF); for (pair<pair<double, double>, pair<double, double>> i : v) if (i.first >= nw) rt += abs(i.second - i.first), nw = i.second; else if (i.second > nw) rt += abs(i.second - nw), nw = i.second; return rt; } int main() { ios::sync_with_stdio(0), cin.tie(0), cout.tie(0); double ans = 0; long long n; cin >> n; for (int i = 0; i < n; ++i) cin >> arr[0][i].first >> arr[0][i].second >> arr[1][i].first >> arr[1][i].second >> arr[2][i].first >> arr[2][i].second; for (int i = 0; i < n; ++i) { v.clear(); for (int j = 0; j < n; ++j) if (i != j) insert(arr[0][i], arr[1][i], arr[0][j], arr[1][j], arr[2][j]); ans += abs(arr[0][i] - arr[1][i]) - cal(); v.clear(); for (int j = 0; j < n; ++j) if (i != j) insert(arr[2][i], arr[1][i], arr[0][j], arr[1][j], arr[2][j]); ans += abs(arr[2][i] - arr[1][i]) - cal(); v.clear(); for (int j = 0; j < n; ++j) if (i != j) insert(arr[0][i], arr[2][i], arr[0][j], arr[1][j], arr[2][j]); ans += abs(arr[0][i] - arr[2][i]) - cal(); } cout << fixed << setprecision(10) << ans << "\n"; }
3
#include <iostream> using namespace std; int main() { string s; cin >> s; s.at(3) = '8'; cout << s << endl; }
0
#include <bits/stdc++.h> using namespace std; int n, m, x, y, c; vector<int> vx[100005]; vector<int> v; vector<pair<int, int> > e[100005]; int q; int qx[100005], qy[100005]; int fa[100005]; int ans[100005]; int find(int x) { if (fa[x] != x) fa[x] = find(fa[x]); return fa[x]; } map<pair<int, int>, int> mp; int main() { scanf("%d%d", &n, &m); for (int i = 1; i <= m; ++i) { int c; scanf("%d%d%d", &x, &y, &c); vx[c].push_back(x); vx[c].push_back(y); e[c].push_back(make_pair(x, y)); } scanf("%d", &q); for (int i = 1; i <= q; ++i) { scanf("%d%d", &qx[i], &qy[i]); } for (int c = 1; c <= m; ++c) { v.clear(); for (int i = 0; i < vx[c].size(); ++i) { v.push_back(vx[c][i]); fa[vx[c][i]] = vx[c][i]; } sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); for (int i = 0; i < e[c].size(); ++i) { if (find(e[c][i].first) != find(e[c][i].second)) fa[find(e[c][i].first)] = find(e[c][i].second); } if (v.size() > 450) { for (int i = 1; i <= q; ++i) { if (!binary_search(v.begin(), v.end(), qx[i])) continue; if (!binary_search(v.begin(), v.end(), qy[i])) continue; if (find(qx[i]) == find(qy[i])) ++ans[i]; } } else { for (int i = 0; i < v.size(); ++i) for (int j = 0; j < v.size(); ++j) { if (i == j) continue; if (find(v[i]) == find(v[j])) ++mp[make_pair(v[i], v[j])]; } } } for (int i = 1; i <= q; ++i) printf("%d\n", ans[i] + mp[make_pair(qx[i], qy[i])]); return 0; }
4
#include <bits/stdc++.h> using namespace std; const int N = 10005; pair<int, int> a[N], b[N * 15]; int n, cnt; void solve(int l, int r) { int m = (l + r) >> 1, mx = a[m].first; for (int i = l; i <= r; ++i) b[++cnt] = make_pair(mx, a[i].second); if (l < m) solve(l, m - 1); if (m < r) solve(m + 1, r); } int main() { scanf("%d", &n); for (int i = 1; i <= n; ++i) scanf("%d%d", &a[i].first, &a[i].second); sort(a + 1, a + n + 1); solve(1, n); sort(b + 1, b + cnt + 1); cnt = unique(b + 1, b + cnt + 1) - b - 1; printf("%d\n", cnt); for (int i = 1; i <= cnt; ++i) printf("%d %d\n", b[i].first, b[i].second); return 0; }
2
#include <iostream> #include <queue> #include <vector> #include <set> #include <algorithm> #define INF 1001001001001001001 using namespace std; typedef long long ll; typedef pair<int,int> pa; typedef pair<ll,pa > ppa; vector<pa> ve[2][100010]; int swi[2][200010]; ll res[2][200010],kar[2][200010]; int main(){ int M,N,K; cin>>M>>N>>K; int a,b,aa=0; bool fla=false,fla2=false; for(int i=1;i<=K;i++){ cin>>a>>b; ve[1][a].push_back(pa(i,b)); ve[0][b].push_back(pa(i,a)); swi[0][i]=a;swi[1][i]=b; if(a==1&&b==1){ fla=true; aa=i; } if(a==M&&b==N){ fla2=true; } } for(int i=0;i<2;i++){ fill(res[i],res[i]+2+K,INF); fill(kar[i],kar[i]+2+K,INF); } priority_queue<ppa,vector<ppa>,greater<ppa> > que; for(vector<pa>::iterator it=ve[1][1].begin();it!=ve[1][1].end();it++){ que.push(ppa(it->second-1,pa(1,it->first))); } if(fla){ res[0][aa]=0; } int cnt=0; while(!que.empty()){ ll dis=que.top().first; int yota=que.top().second.first; int ten=que.top().second.second; que.pop(); if(res[yota][ten]<INF) continue; res[yota][ten]=dis; res[1-yota][ten]=dis+1; for(vector<pa>::iterator it=ve[1-yota][swi[yota][ten]].begin();it!=ve[1-yota][swi[yota][ten]].end();it++){ int saki=it->first;int ba=it->second; ll hog=dis+1+abs(swi[1-yota][ten]-ba); if(kar[1-yota][saki]>hog&&res[1-yota][saki]==INF){ que.push(ppa(hog,pa(1-yota,saki))); kar[1-yota][saki]=hog; } } } ll ress=INF; for(vector<pa>::iterator it=ve[0][N].begin();it!=ve[0][N].end();it++){ ress=min(ress,res[1][it->first]+1+abs(M-it->second)); } for(vector<pa>::iterator it=ve[1][M].begin();it!=ve[1][M].end();it++){ ress=min(ress,res[0][it->first]+1+abs(N-it->second)); } if(ress<INF) cout<<ress<<endl; else cout<<-1<<endl; return 0; }
0
#include <bits/stdc++.h> template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, true : false; } template <typename T> inline bool chkmax(T &a, T b) { return b > a ? a = b, true : false; } template <class T> inline T read(T &x) { int sign = 1; char c = getchar_unlocked(); for (; !isdigit(c); c = getchar_unlocked()) if (c == '-') sign = -1; for (x = 0; isdigit(c); c = getchar_unlocked()) x = x * 10 + c - '0'; return x *= sign; } template <class T> inline void write(T x) { if (x == 0) { putchar_unlocked('0'); return; } if (x < 0) { putchar_unlocked('-'); x = -x; } static char s[20]; int top = 0; for (; x; x /= 10) s[++top] = x % 10 + '0'; while (top) putchar_unlocked(s[top--]); } const int maxN = 5000; const int LogN = 40; const int P = 7340033; const int G = 3; long long pwr(long long x, int k) { long long ret = 1; for (; k; k >>= 1, x = x * x % P) if (k & 1) ret = ret * x % P; return ret; } int a[LogN][maxN]; inline int gmod(int x) { if (x >= P) x -= P; return x; } namespace NTT { int n, STEP, g, inv; int rev[maxN]; int wn[maxN]; void init(int N) { for (n = 1, STEP = 0; n < N; n <<= 1, ++STEP) ; for (int i = 1; i < n; ++i) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (STEP - 1)); g = pwr(G, (P - 1) / n); wn[0] = 1; for (int i = 1; i <= n; ++i) wn[i] = (long long)wn[i - 1] * g % P; inv = pwr(n, P - 2); } void dft(int a[], int flag) { for (int i = 0; i < n; ++i) if (i < rev[i]) std::swap(a[i], a[rev[i]]); for (int k = 1; k < n; k <<= 1) { for (int i = 0; i < n; i += (k << 1)) for (int j = 0; j < k; ++j) { int w = flag == 1 ? wn[n / (k << 1) * j] : wn[n - n / (k << 1) * j]; int x = a[i + j]; int y = (long long)a[i + j + k] * w % P; a[i + j] = gmod(x + y); a[i + j + k] = gmod(x - y + P); } } if (flag == -1) { for (int i = 0; i < n; ++i) a[i] = (long long)a[i] * inv % P; } } } // namespace NTT void prework() { NTT::init(4000); a[0][0] = 1; for (int i = 1; i <= 32; ++i) { NTT::dft(a[i - 1], 1); for (int j = 0; j < NTT::n; ++j) { long long t = (long long)a[i - 1][j] * a[i - 1][j] % P; a[i][j] = t * t % P; } NTT::dft(a[i - 1], -1); NTT::dft(a[i], -1); for (int j = NTT::n - 1; j > 1000; --j) a[i][j] = 0; for (int j = 1000; j >= 1; --j) a[i][j] = a[i][j - 1]; } } int main() { if (fopen("tmp.in", "r")) { freopen("tmp.in", "r", stdin); freopen("tmp.out", "w", stdout); } int Q, n, k; prework(); read(Q); while (Q--) { read(n), read(k); int cnt = 0; for (; (n > 1) && (n & 1); n >>= 1) ++cnt; write(a[cnt][k]), putchar_unlocked('\n'); } return 0; }
4
#include <bits/stdc++.h> using namespace std; int const N = 1e6 + 100; long long int const LINF = numeric_limits<long long int>::max(); int const INF = numeric_limits<int>::max(); int const BN = 31; long long int gcd(long long int a, long long int b) { return b ? gcd(b, a % b) : a; } long long int pow_mod(long long int a, long long int e, long long int m) { long long int res = 1; while (e) { if (e & 1) res = res * a % m; a = a * a % m; e >>= 1; } return res; } long long int inv_mod(long long int b, long long int m) { b %= m; long long int x = 0, y = 1, r, q, a = m; while (b) { q = a / b; r = a % b; a = b; b = r; r = x; x = y; y = r - q * y; } x += m; return x % m; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector<long long int> arr(n); for (int i = 0; i < n; i++) { cin >> arr[i]; } sort(arr.begin(), arr.end()); long long int ans = 0; for (int i = 0; i < n / 2; i++) { ans += pow(arr[i] + arr[n - i - 1], 2); } cout << ans << endl; }
3
#include <bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC optimize("Ofast") using namespace std; long long int n, a[505], dp[505][505], b[505], p[505]; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; cin >> n; for (long long int i = 1; i <= n; i++) { cin >> a[i]; p[i] = b[a[i]]; b[a[i]] = i; } for (long long int j = 1; j <= n; j++) { for (long long int i = 1; i <= j; i++) { dp[i][j] = dp[i][j - 1] + 1; if (i == j) { continue; } if (i == j - 1) { if (a[i] == a[j]) { dp[i][j] = 1; } continue; } long long int x = p[j]; while (x >= i) { if (x == j - 1) { dp[i][j] = min(dp[i][j], dp[i][x - 1] + 1); } else if (x == i) { dp[i][j] = min(dp[i][j], dp[i + 1][j - 1]); } else dp[i][j] = min(dp[i][j], dp[x + 1][j - 1] + dp[i][x - 1]); x = p[x]; } } } cout << dp[1][n] << '\n'; }
4
#include <bits/stdc++.h> inline long long max(long long a, long long b) { return b < a ? a : b; } long long n, v[100010], x, y, root; long long e, bg[100010], nx[100010 << 1], to[100010 << 1]; inline void link(long long u, long long v) { to[++e] = v, nx[e] = bg[u], bg[u] = e; } long long f[4][100010]; void SummerPockets(long long now, long long fa) { long long Shiroha[4]; long long qinMaBaoZha(0); for (long long i = 0; i != 4; ++i) Shiroha[i] = 0; f[0][now] = v[now]; for (long long i = bg[now]; i; i = nx[i]) if (to[i] != fa) { SummerPockets(to[i], now); f[3][now] = max(f[3][now], f[1][now] + f[1][to[i]]); f[3][now] = max(f[3][now], f[1][now] + f[0][to[i]]); f[3][now] = max(f[3][now], f[2][now] + f[0][to[i]]); f[0][now] = max(f[0][now], f[0][to[i]] + v[now]); f[1][now] = max(f[1][now], f[1][to[i]]); f[2][now] = max(f[2][now], f[2][to[i]] + v[now]); f[3][now] = max(f[3][now], f[3][to[i]]); f[1][now] = max(f[1][now], Shiroha[0] + f[0][to[i]] + v[now]); f[2][now] = max(f[2][now], Shiroha[0] + f[1][to[i]] + v[now]); f[2][now] = max(f[2][now], Shiroha[0] + f[0][to[i]] + v[now]); f[2][now] = max(f[2][now], Shiroha[1] + f[0][to[i]] + v[now]); f[3][now] = max(f[3][now], Shiroha[0] + f[2][to[i]] + v[now]); f[3][now] = max(f[3][now], Shiroha[2] + f[0][to[i]] + v[now]); f[3][now] = max(f[3][now], Shiroha[1] + f[1][to[i]]); for (long long j = 0; j != 4; ++j) Shiroha[j] = max(Shiroha[j], f[j][to[i]]); } } signed main() { srand(time(NULL)); scanf("%lld", &n); for (long long i = 1; i <= n; ++i) scanf("%lld", v + i); for (long long i = 1; i != n; ++i) { scanf("%lld%lld", &x, &y); link(x, y); link(y, x); } root = rand() % n + 1; SummerPockets(1, 0); printf("%lld", f[3][1]); return 0; }
6
#include<bits/stdc++.h> using namespace std; int main(){ long long A=0, ans=0; string S; cin >> S; for(long long i=0;;i++){ if(S[i+1]=='\0') break; if(S[i]=='A') A++; else if(S[i]=='B' && S[i+1]=='C'){ ans += A; i++; }else{ A=0; } } cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n, i, x = 0; cin >> n; long long int a[n]; for (i = 0; i < n; ++i) { cin >> a[i]; } if (n == 1) { cout << "YES\n"; continue; } for (i = 1; i < n; ++i) { if ((a[i] - a[i - 1]) % 2 != 0) { x++; cout << "NO\n"; break; } } if (x == 0) { cout << "YES\n"; } } cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0; }
1
#include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <string> #include <queue> #include <stack> #include <set> #include <map> #include <iomanip> #include <utility> #include <tuple> #include <functional> #include <bitset> #include <cassert> #include <complex> #include <stdio.h> #include <time.h> #include <numeric> #include <random> #include <unordered_map> #include <unordered_set> #define all(a) a.begin(),a.end() #define rep(i, n) for (ll i = 0; i < (n); i++) #define pb push_back #define debug(x) cerr << #x << ':' << (x) << '\n' #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") using namespace std; typedef long long ll; typedef unsigned long long ull; typedef long double ld; typedef pair<ll, ll> P; typedef complex<ld> com; constexpr int inf = 1000000010; constexpr ll INF = 1000000000000000010; constexpr ld eps = 1e-12; constexpr ld pi = 3.141592653589793238; template<class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return true; } return false; } template<class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return true; } return false; } constexpr ll mod = 1000000007; int main() { cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); int h; cin >> h; int sz = 1 << (h - 1); vector<int> p(sz); rep(i, sz) { cin >> p[i]; p[i]--; } vector<vector<ll>> prod(sz, vector<ll>(h - 1)); rep(i, sz) { ll x = i + sz; ll tmp = x; rep(j, h - 1) { prod[i][j] = tmp; x >>= 1; tmp *= x; tmp %= mod; } } vector<vector<P>> vec(2 * sz, vector<P>()); vector<int> lst(2 * sz, -1); int bor = 1; int cap = 0; rep(i, 2 * sz) { if (i >= bor) { bor <<= 1; cap++; } int x = 1 << (h - cap); x *= (h - 1); if (i >= 2) vec[i].reserve(x); } rep(step, h - 1) { for (int i = sz - 1; i >= 0; i--) { int x = i + sz; int y = p[i] + sz; x >>= step; int idx = y; rep(j, h - 1) { if (lst[idx] == x) { vec[idx].back().first += prod[i][step] * prod[p[i]][j]; vec[idx].back().first %= mod; } else { vec[idx].pb({ prod[i][step] * prod[p[i]][j] % mod,x }); lst[idx] = x; } idx >>= 1; } } } rep(i, 2 * sz) { reverse(all(vec[i])); if (i & 1) { for (int j = 0; j < vec[i].size(); j++) { vec[i][j].second ^= 1; if (j > 0 && vec[i][j].second < vec[i][j - 1].second) swap(vec[i][j], vec[i][j - 1]); } } } ll ans = 0; for (int i = 0; i < 2 * sz; i += 2) { int x = 0, y = 0; while (x < vec[i].size() && y < vec[i + 1].size()) { if (vec[i][x].second < vec[i + 1][y].second) x++; else if (vec[i][x].second > vec[i + 1][y].second) y++; else { ans += vec[i][x].first * vec[i + 1][y].first % mod * (i >> 1) % mod * (vec[i][x].second >> 1); ans %= mod; x++; y++; } } } cout << ans << '\n'; }
0
#include <bits/stdc++.h> using namespace std; long long n, u, d, node, q, x; stack<int> st; string s; int main() { cin >> n >> q; while (q--) { scanf("%lld", &x); cin >> s; node = (n + 1) / 2; u = 0; d = node / 2; while (node != x) { if (node > x) { st.push(1); node -= d; u = d; d /= 2; } else { st.push(0); node += d; u = d; d /= 2; } } for (int i = 0; i < s.size(); ++i) { if (s[i] == 'L') { if (node % 2 == 0) { st.push(1); node -= d; if (d != 0) { u = d; } d /= 2; } } else if (s[i] == 'R') { if (node % 2 == 0) { st.push(0); node += d; if (d != 0) { u = d; } d /= 2; } } else { if (node == (n + 1) / 2) { u = 0; } else { if (st.top() == 1) { node += u; } else { node -= u; } st.pop(); d = u; u *= 2; } } } printf("%lld\n", node); } return 0; }
4
#include<cstdio> #include<algorithm> using namespace std; #define N 105000 int n,a[N],b[N],v[N*2],id1[N],id2[N]; long long su1,su2,su[N*2],as=1e18; long long solve(int i) { if(id1[i]>n&&id2[i]>n)return su[n]; else if(id1[i]>n+1||id2[i]>n+1)return su[n+1]-(id1[i]<=n+1?v[id1[i]]:0)-(id2[i]<=n+1?v[id2[i]]:0); else return su[n+2]-v[id1[i]]-v[id2[i]]; } int main() { scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d%d",&a[i],&b[i]),v[i*2-1]=a[i],v[i*2]=b[i],su1+=a[i],su2+=b[i]; sort(v+1,v+n*2+1);for(int i=1;i<=n;i++)id1[i]=lower_bound(v+1,v+n*2+1,a[i])-v,id2[i]=lower_bound(v+1,v+n*2+1,b[i])-v; for(int i=1;i<=n*2;i++)su[i]=su[i-1]+v[i]; as=min(su1,su2);for(int i=1;i<=n;i++)as=min(as,solve(i)); printf("%lld\n",as); }//
0
#include <bits/stdc++.h> using namespace std; long long n, p[200000], tosolve[20000]; long long moves[20000]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> p[i]; tosolve[1] = 2; moves[1] = 2; for (int i = 2; i <= n; i++) { for (int j = p[i]; j < i; j++) tosolve[i] += tosolve[j]; tosolve[i] += 2; tosolve[i] %= 1000000007; moves[i] = moves[i - 1] + tosolve[i]; moves[i] %= 1000000007; } cout << moves[n] << endl; cin.get(); cin.get(); return 0; }
4
#define _USE_MATH_DEFINES #include <iostream> #include <sstream> #include <cmath> #include <algorithm> #include <queue> #include <stack> #include <limits> #include <map> #include <string> #include <cstring> #include <set> #include <deque> #include <bitset> using namespace std; typedef long long ll; typedef pair<int,int> P; static const double eps = 10e-8; struct IC{ int fare; int dist; }; int main(){ int dpt,arv; struct IC ic[] = { 300,6, 500,13, 600,18, 700,23, 1350,43, 1650,58, 350,7, 450,12, 600,17, 1150,37, 1500,52, 250,5, 400,10, 1000,30, 1350,45, 250,5, 850,25, 1300,40, 600,20, 1150,35, 500,15, }; while(~scanf("%d",&dpt)){ if(dpt==0) break; int hDpt,mDpt; scanf("%d %d",&hDpt,&mDpt); scanf("%d",&arv); int hArv,mArv; scanf("%d %d",&hArv,&mArv); int DptT = hDpt*60+mDpt; int ArvT = hArv*60+mArv; int idx=0; int fare=0,dist=0; for(int i=1;i<=7;i++){ for(int j=i+1;j<=7;j++){ if(i==dpt && j==arv){ fare = ic[idx].fare; dist = ic[idx].dist; goto found; } idx++; } } found:; if(((17*60+30 <= DptT && DptT <= 19*60+30) || (17*60+30 <= ArvT && ArvT <= 19*60+30)) && dist <= 40){ fare /=2; } int tmp = fare / 50; if(fare % 50) fare = 50 * (tmp+1); printf("%d\n",fare); } }
0
#include <bits/stdc++.h> using namespace std; double x[1048577]; int f[20][3]; int n, l, r; double angl(int v) { return v * M_PI / 180.0; } int main() { cin >> n >> l >> r; for (int i = 0; i < n; ++i) cin >> f[i][0] >> f[i][1] >> f[i][2]; for (int i = 0; i < 1048577; ++i) x[i] = l; double res = -1000000; for (int j = 0; j < (1 << n); ++j) { for (int k = 0; k < n; ++k) { int bit = j & (1 << k); if (bit != 0) continue; double fx = f[k][0]; double fy = f[k][1]; double cx = x[j]; double alpha = angl(f[k][2]); double alpha90 = angl(90); if (fx >= cx) { double b = sqrt((fx - cx) * (fx - cx) + fy * fy); double h = fy; double a1 = sqrt(b * b - h * h); double alpha1 = min(alpha90, acos(h / b)); double alpha2 = min(alpha90, alpha1 - alpha); double a2 = tan(alpha2) * h; double val = cx + a1 - a2; x[j + (1 << k)] = max(x[j + (1 << k)], val); } else { double b = sqrt((fx - cx) * (fx - cx) + fy * fy); double h = fy; double a1 = sqrt(b * b - h * h); double alpha1 = min(alpha90, acos(h / b)); double alpha2 = min(alpha90, alpha1 + alpha); double a = tan(alpha2) * h; double val = cx + a - a1; x[j + (1 << k)] = max(x[j + (1 << k)], val); } res = max(res, x[j + (1 << k)]); } } printf("%.7lf\n", min(res, (double)r) - l); return 0; }
4
#include <bits/stdc++.h> using namespace std; using lli = long long; using ld = long double; mt19937 rnd(time(nullptr)); int Func(vector<int>& data, int l, int r) { int result = 0; int n = (r - l + 1) - 1; for (int i = l, k = 0; i <= r; i++, k++) { if ((n | k) == n) result ^= data[i]; } return result; } inline void Solve() { int n = 0; cin >> n; vector<int> data(n); for (auto& it : data) cin >> it; vector<vector<int>> result(n, vector<int>(n)); for (int l = 0; l <= n - 1; l++) result[l][l] = data[l]; for (int k = 1; k <= n - 1; k++) { for (int l = 0, r = k; (l <= n - 1) && (r <= n - 1); l++, r++) { result[l][r] = result[l][r - 1] ^ result[l + 1][r]; } } vector<vector<int>> prefix_max(n, vector<int>(n)); for (int r = 0; r <= n - 1; r++) { prefix_max[r][r] = result[r][r]; for (int l = r - 1; l >= 0; l--) prefix_max[r][l] = max(prefix_max[r][l + 1], result[l][r]); } vector<vector<int>> dp(n, vector<int>(n)); for (int l = 0; l <= n - 1; l++) { dp[l][l] = prefix_max[l][l]; for (int r = l + 1; r <= n - 1; r++) dp[l][r] = max(dp[l][r - 1], prefix_max[r][l]); } int q = 0; cin >> q; for (int i = 0; i < q; i++) { int l = 0, r = 0; cin >> l >> r; l--, r--; cout << dp[l][r] << '\n'; } } signed main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); cout << fixed << setprecision(20); Solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for (int64_t i = 0; i < (n); i++) int main() { cin.tie(0); ios::sync_with_stdio(false); string s;cin>>s; const int n=s.size(); vector<vector<int64_t>>dp(n+1,vector<int64_t>(13,0));dp[0][0]=1; REP(i,n) { int x; if(s[i]=='?')x=-1; else x=s[i]-'0'; REP(j,10) if(x==-1||x==j) REP(k,13) dp[i+1][(k*10+j)%13]+=dp[i][k]; REP(j,13)dp[i+1][j]%=1000000007; } cout<<dp[n][5]<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL), cout.tie(NULL); int t; cin >> t; while (t--) { long long int n; cin >> n; long long int a[n], zero = 0, sum = 0; for (int i = 0; i < n; i++) { cin >> a[i]; sum += a[i]; if (a[i] == 0) zero++; } if (sum + zero == 0) cout << zero + 1 << '\n'; else cout << zero << '\n'; } return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 1005; const int MAXM = 3005; const double inf = 1e8; const double eps = 1e-8; template <typename T> inline void read(T &AKNOI) { T x = 0, flag = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') flag = -1; ch = getchar(); } while (isdigit(ch)) { x = x * 10 + ch - '0'; ch = getchar(); } AKNOI = flag * x; } int n, m, U[MAXM], V[MAXM], A[MAXM], B[MAXM], C[MAXM], D[MAXM]; struct Edge { int v, nxt; double w; } e[MAXM << 1]; int first[MAXN], used[MAXN], eCnt; inline void AddEdge(int u, int v, double w) { e[++eCnt].v = v; e[eCnt].w = w; e[eCnt].nxt = first[u]; first[u] = eCnt; } inline void Add(int u, int v, double w) { AddEdge(u, v, w); AddEdge(v, u, 0); } double in[MAXN]; int S, T, level[MAXN], q[MAXN], head, tail; bool BFS() { for (int i = 1; i <= T; ++i) { level[i] = -1; used[i] = first[i]; } level[S] = 0; q[head = tail = 1] = S; while (head <= tail) { int u = q[head++]; if (u == T) return 1; for (int i = first[u]; i; i = e[i].nxt) { int v = e[i].v; if (e[i].w > eps && level[v] == -1) { level[v] = level[u] + 1; q[++tail] = v; } } } return 0; } double DFS(int u, double flow) { if (u == T) return flow; double ret = 0; for (int i = used[u]; i; i = e[i].nxt, used[u] = i) { int v = e[i].v; if (e[i].w > eps && level[v] == level[u] + 1) { double tmp = DFS(v, min(flow, e[i].w)); e[i].w -= tmp; e[i ^ 1].w += tmp; flow -= tmp; ret += tmp; if (flow < eps) break; } } return ret; } double Check(double mid) { eCnt = 1; for (int i = 1; i <= T; ++i) { in[i] = first[i] = 0; } for (int i = 1; i <= m; ++i) { double l = mid * A[i] + B[i]; double r = mid * C[i] + D[i]; Add(U[i], V[i], r - l); in[V[i]] += l; in[U[i]] -= l; } double ret = 0; for (int i = 1; i <= n; ++i) { if (in[i] > eps) { ret += in[i]; Add(S, i, in[i]); } else if (in[i] < -eps) { Add(i, T, -in[i]); } } while (BFS()) { ret -= DFS(S, inf); } return ret; } void init() { read(n); read(m); for (int i = 1; i <= m; ++i) { read(U[i]); read(V[i]); read(A[i]); read(B[i]); read(C[i]); read(D[i]); } S = n + 1, T = S + 1; } void solve() { double L = 0, R = 1, ok = -1; while (R - L > eps) { double M1 = L + (R - L) / 3, M2 = R - (R - L) / 3; double D1 = Check(M1), D2 = Check(M2); if (D1 < eps) { ok = M1; break; } if (D2 < eps) { ok = M2; break; } (D1 > D2) ? L = M1 : R = M2; } if (ok < 0) { printf("0\n"); return; } double L1 = 0, R1 = ok; while (R1 - L1 > eps) { double M = (L1 + R1) / 2; (Check(M) > eps) ? L1 = M : R1 = M; } double L2 = ok, R2 = 1; while (R2 - L2 > eps) { double M = (L2 + R2) / 2; (Check(M) > eps) ? R2 = M : L2 = M; } printf("%.8lf\n", L2 - L1); } int main() { init(); solve(); return 0; }
6
#include <bits/stdc++.h> using namespace std; map<string, int> a; string s, s1; int res, len, i; int main() { while (getline(cin, s)) { s1 = s.substr(1, s.length()); if (s[0] == '+') a[s1]++; else if (s[0] == '-') a.erase(s1); else { len = s.length() - s.find(":") - 1; res += a.size() * len; } } cout << res << endl; }
1
#include<bits/stdc++.h> using namespace std; #define int long long const int p=1e9+7; unsigned long long power(unsigned long long x, int y) { unsigned long long res = 1; // Initialize result x = x % p; // Update x if it is more than or // equal to p while (y > 0) { // If y is odd, multiply x with result if (y & 1) res = (res * x) % p; // y must be even now y = y >> 1; // y = y/2 x = (x * x) % p; } return res; } // Returns n^(-1) mod p unsigned long long modInverse(unsigned long long n ) { return power(n, p - 2); } // Returns nCr % p using Fermat's little // theorem. unsigned long long nCrModPFermat(unsigned long long n, int r) { // If n<r, then nCr should return 0 if (n < r) return 0; // Base case if (r == 0) return 1; // Fill factorial array so that we // can find all factorial of r, n // and n-r unsigned long long fac[n + 1]; fac[0] = 1; for (int i = 1; i <= n; i++) fac[i] = (fac[i - 1] * i) % p; return (fac[n] * modInverse(fac[r]) % p * modInverse(fac[n - r]) % p) % p; } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin>>t; while(t--){ int n,k; cin>>n>>k; map<int,int> m; for(int i=0;i<n;i++) { int x; cin>>x; m[x]++; } int l=0,ans=0,select,count; for(auto it=m.rbegin();it!=m.rend();it++){ int j=(it->second); if((l+j)>=k){ count=j;select=k-l; break; } else l+=j; } ans=nCrModPFermat(count,select); cout<<ans<<"\n"; } }
5
#include <bits/stdc++.h> using namespace std; int main() { pair<int, int> a[100001]; int n, ans = 0, max; cin >> n; for (int i = 0; i < n; i++) cin >> a[i].first >> a[i].second; sort(a, a + n); max = a[0].second; for (int i = 0; i < n; i++) { if (a[i].second < max) ans++; else max = a[i].second; } cout << ans << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { long long int n, i, j, k, dp[100003][5], A[1000000], B[1000000], t, sum, ans; cin >> n; for (i = 0; i < n; i++) { cin >> A[i]; } sort(A, A + n); sum = A[0]; ans = 1; for (i = 1; i < n; i++) { if (sum <= A[i]) { ans++; sum = sum + A[i]; } } cout << ans; }
4
#include <bits/stdc++.h> #define ll long long using namespace std; string s[1005],t[1005]; ll pre[1005][1005]; ll pre2[1005][1005]; ll mp[1005]; ll mq[1005]; int n1,m1,n2,m2; ll p=131,q=233; ll Q=1e9+7; void solve(){ mp[0]=mq[0]=1; for(int i=1;i<=1000;i++){ mp[i]=mp[i-1]*p%Q; mq[i]=mq[i-1]*q%Q; } for(int i=1;i<=n1;i++){ for(int j=1;j<=m1;j++){ pre[i][j]=(-pre[i-1][j-1]*p*q+pre[i-1][j]*p+pre[i][j-1]*q+s[i-1][j-1])%Q; pre[i][j]=(pre[i][j]+Q)%Q; } } for(int i=1;i<=n2;i++){ for(int j=1;j<=m2;j++){ pre2[i][j]=(-pre2[i-1][j-1]*p*q+pre2[i-1][j]*p+pre2[i][j-1]*q+t[i-1][j-1])%Q; pre2[i][j]=(pre2[i][j]+Q)%Q; } } ll hs=pre2[n2][m2]; for(int i=1;i<=n1-n2+1;i++){ for(int j=1;j<=m1-m2+1;j++){ ll v=(pre[i+n2-1][j+m2-1]+pre[i-1][j-1]*mp[n2]%Q*mq[m2]%Q-pre[i+n2-1][j-1]*mq[m2]%Q-pre[i-1][j+m2-1]*mp[n2]%Q)%Q; v=(v+Q)%Q; if(v==hs){ cout<<i-1<<' '<<j-1<<"\n"; } } } } int main(){ // freopen("a.txt","r",stdin); ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); cin>>n1>>m1; for(int i=0;i<n1;i++)cin>>s[i]; cin>>n2>>m2; for(int i=0;i<n2;i++)cin>>t[i]; solve(); return 0; }
0
#include <bits/stdc++.h> using namespace std; const int N = 205; int n, mod, fc[N], iv[N], ans, a1, dp[N][N][N], v[N][N][N]; int qp(int a, int b) { int ans = 1; while (b) { if (b & 1) ans = 1ll * ans * a % mod; a = 1ll * a * a % mod; b >>= 1; } return ans; } int dfs(int i, int j, int k) { int &f = dp[i][j][k]; if (v[i][j][k] == a1) return f; if (i == n) return f = 1; if (!k) return f = iv[n - i]; v[i][j][k] = a1; f = 0; for (int c = 0; c <= (a1 - j) / k; c++) { if (k > 1 && i + c < n + 1 - a1 - k + 1) continue; f = (f + 1ll * dfs(i + c, j + c * k, k - 1) * iv[c]) % mod; } return f; } int main() { scanf("%d%d", &n, &mod); fc[0] = iv[0] = 1; for (int i = 1; i <= n; i++) fc[i] = 1ll * fc[i - 1] * i % mod, iv[i] = qp(fc[i], mod - 2); for (a1 = max(1, (int)(n - 2 * sqrt(n) - 1)); a1 <= n; a1++) ans = (ans + dfs(0, 0, n + 1 - a1)) % mod; ans = 1ll * ans * fc[n] % mod; printf("%d\n", ans); return 0; }
5
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10, N = 1e9; const long long inf = 1ll << 60; char s[maxn]; long long x[maxn], y[maxn], x2, y2; int n; int ok(long long m) { long long t1 = m / n, t2 = m % n; long long x1 = t1 * (x[n] - x[0]) + x[0] + (x[t2] - x[0]); long long y1 = t1 * (y[n] - y[0]) + y[0] + (y[t2] - y[0]); long long res = abs(x2 - x1) + abs(y2 - y1); if (res > m) return 0; return 1; } int main() { cin >> x[0] >> y[0] >> x2 >> y2; scanf("%d", &n); scanf("%s", s + 1); for (int i = 1; i <= n; i++) { x[i] = x[i - 1], y[i] = y[i - 1]; if (s[i] == 'U') y[i]++; if (s[i] == 'D') y[i]--; if (s[i] == 'L') x[i]--; if (s[i] == 'R') x[i]++; } long long l = 1, r = 1ll << 50, m; if (ok(r)) while (l < r) { m = (l + r) / 2; if (ok(m)) r = m; else l = m + 1; } else r = -1; cout << r; }
3
//include //------------------------------------------ #include <vector> #include <list> #include <map> #include <climits> #include <set> #include <deque> #include <stack> #include <bitset> #include <algorithm> #include <functional> #include <numeric> #include <utility> #include <sstream> #include <iostream> #include <iomanip> #include <cstdio> #include <cmath> #include <cstdlib> #include <cctype> #include <string> #include <cstring> #include <ctime> #include <queue> #include <random> #include <cctype> #include <complex> #include <regex> using namespace std; #define C_MAX(a, b) ((a)>(b)?(a):(b)) #define SHOW_VECTOR(v) {std::cerr << #v << "\t:";for(const auto& xxx : v){std::cerr << xxx << " ";}std::cerr << "\n";} #define SHOW_MAP(v){std::cerr << #v << endl; for(const auto& xxx: v){std::cerr << xxx.first << " " << xxx.second << "\n";}} struct IntL { long long pos; long long s; long long t; }; bool comp_intl(const IntL &a, const IntL &b) { return a.pos < b.pos; } int main() { int N; cin >> N; vector<IntL> vx; vector<IntL> vy; for (int i = 0; i < N; i++) { long long x1, y1, x2, y2; cin >> x1 >> y1 >> x2 >> y2; if (x1 == x2) { IntL intL = {x1, min(y1, y2), max(y1, y2)}; vy.push_back(intL); } if (y1 == y2) { IntL intL = {y1, min(x1, x2), max(x1, x2)}; vx.push_back(intL); } } sort(vy.begin(), vy.end(), comp_intl); long long ans = 0; for (auto li: vx) { long long left = li.s; long long right = li.t; long long posY = li.pos; auto s1 = lower_bound(vy.begin(), vy.end(), (IntL) {left, LONG_LONG_MIN, LONG_LONG_MIN}, comp_intl); auto s2 = upper_bound(vy.begin(), vy.end(), (IntL) {right, LONG_LONG_MIN, LONG_LONG_MIN}, comp_intl); for (auto i = s1; i != s2; i++) { if (i->s <= posY && posY <= i->t) ans++; } } cout << ans << endl; }
0
#include <bits/stdc++.h> using namespace std; int main() { long long x, y, n; cin >> n; for (int i = 1; i <= n; i++) { cin >> x >> y; if (x - y == 1) cout << "NO" << endl; else cout << "YES" << endl; } return 0; }
1
#include <bits/stdc++.h> using namespace std; typedef long long i64; const int MAXN = 2000000 + 10; const int MOD = 1e9 + 7; template<class T> T modularExp(T base, T exp) { T ret = 1; while (exp) { if (exp & 1) ret = ret * base % MOD; base = base * base % MOD; exp >>= 1; } return ret; } int fact[MAXN + 1], inv[MAXN + 1]; void prepare() { fact[0] = 1; for (int i = 1; i <= MAXN; i++) fact[i] = 1LL * fact[i - 1] * i % MOD; inv[MAXN] = modularExp<i64>(fact[MAXN], MOD - 2) % MOD; for (int i = MAXN - 1; i >= 0; i--) inv[i] = 1LL * inv[i + 1] * (i + 1) % MOD; assert(inv[1] == 1); } i64 getBino(int n, int m) { return 1LL * fact[n] * inv[m] % MOD * inv[n - m] % MOD; } i64 calcRect(int n, int m, int a, int b) { return ((getBino(n + m + a + b, n + a) - getBino(m + a + b, a) - getBino(n + a + b, n + a) + getBino(a + b, a)) % MOD + MOD) % MOD; } int main() { int x1, x2, x3, x4, x5, x6; int y1, y2, y3, y4, y5, y6; prepare(); cin >> x1 >> x2 >> x3 >> x4 >> x5 >> x6 ; cin >> y1 >> y2 >> y3 >> y4 >> y5 >> y6 ; int n1 = x2 - x1 + 1, n2 = x6 - x5 + 1; int m1 = y2 - y1 + 1, m2 = y6 - y5 + 1; int answer = 0; for(int x = x3; x <= x4; x++) answer = (answer + calcRect(n1, m1, x-x2, y3-y2-1) * calcRect(n2, m2, x5-x, y5-y3) % MOD * (MOD - x - y3)) % MOD , answer = (answer + calcRect(n1, m1, x-x2, y4-y2) * calcRect(n2, m2, x5-x, y5-y4-1) % MOD * (x + y4 + 1)) % MOD; for(int y = y3; y <= y4; y++) answer = (answer + calcRect(n1, m1, x3-x2-1, y-y2) * calcRect(n2, m2, x5-x3, y5-y) % MOD * (MOD - y - x3)) % MOD, answer = (answer + calcRect(n1, m1, x4-x2, y-y2) * calcRect(n2, m2, x5-x4-1, y5-y) % MOD * (y + x4 + 1)) % MOD; cout << answer << endl ; }
0
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll mod = 1e9+7; ll rui(ll a,ll b){ ll ans=1; while(b>0){ if(b&1) ans = ans*a%mod; a = a*a%mod; b/=2; } return ans; } ll comb(ll a,ll b){ ll ans=1; for(ll i=a;i>a-b;i--){ ans=ans*i%mod; } for(ll i=1;i<=b;i++){ ans=(ans*rui(i,mod-2))%mod; } return ans; } int main() { ll n,a,b,sum,nca,ncb; cin>>n>>a>>b; sum = rui(2,n)-1; nca = comb(n,a); ncb = comb(n,b); cout<<(mod+mod+sum-nca-ncb) % mod; }
0
#include<bits/stdc++.h> using namespace std; int main(){ int a; int b; cin>>a; b=a+a*a+a*a*a; cout<<b; }
0
#include <bits/stdc++.h> #pragma GCC optimize "03" const long long int MOD = 1e9 + 7; const long long int INF = 1011111111; const long long int LLINF = 1000111000111000111LL; const long double EPS = 1e-10; const long double PI = 3.14159265358979323; using namespace std; long long int power(long long int base, long long int exponent) { long long int ans = 1; base %= MOD; exponent %= (MOD - 1); while (exponent != 0) { if (exponent & 1) ans = (1LL * ans * base) % MOD; base = (1LL * base * base) % MOD; exponent >>= 1; } return ans; } const long long int N = 1e6 + 5; long long int ar[N]; long long int size[N]; long long int root(long long int i) { while (i != ar[i]) { ar[i] = ar[ar[i]]; i = ar[i]; } return i; } void merge(long long int a, long long int b) { long long int r_a = root(a), r_b = root(b); if (r_a == r_b) return; if (size[r_a] < size[r_b]) { ar[r_a] = ar[r_b]; size[r_b] += size[r_a]; } else { ar[r_b] = ar[r_a]; size[r_a] += size[r_b]; } } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, k, i, j, p; cin >> p >> k; if (k == 0) { return cout << power(p, p - 1), 0; } for (i = 0; i < p; i++) { ar[i] = i; size[i] = 1; } for (i = 0; i < p; i++) merge(i, (i * k) % p); set<long long int> s; for (i = 1; i < p; i++) { s.insert(root(i)); } if (k <= 1) s.insert(root(0)); cout << power(p, (long long int)s.size()) << '\n'; cerr << '\n' << "Time elapsed : " << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'; }
2
#include <stdio.h> typedef struct pair{ double x,y; }pair; double abs(double a){ if(a<0) return -a; return a; } double naiseki(pair A,pair B,pair C,pair D){ pair a,b; a.x=B.x-A.x; a.y=B.y-A.y; b.x=D.x-C.x; b.y=D.y-C.y; return a.x*b.x+a.y*b.y; } int main(){ pair A,B,C,D; while(~scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&A.x,&A.y,&B.x,&B.y,&C.x,&C.y,&D.x,&D.y)){ if(naiseki(A,B,C,D)==0) printf("YES\n"); else printf("NO\n"); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; if (((n / 3 + 1) * 1 + (n / 3) * 2) == n) { cout << n / 3 + 1 << " " << n / 3 << "\n"; } else if (((n / 3) * 1 + (n / 3 + 1) * 2) == n) { cout << n / 3 << " " << n / 3 + 1 << "\n"; } else { cout << n / 3 << " " << n / 3 << "\n"; } } return 0; }
1
#include <bits/stdc++.h> using namespace std; void display(string s, int rock, int paper, int scissors) { int len = s.size(); char c[len + 1]; memset(c, 0, sizeof(char) * (len + 1)); for (int i = 0; i < len; ++i) { if (s[i] == 'R' && paper > 0) --paper, c[i] = 'P'; else if (s[i] == 'P' && scissors > 0) --scissors, c[i] = 'S'; else if (s[i] == 'S' && rock > 0) --rock, c[i] = 'R'; } for (int i = 0; i < len; ++i) { if (c[i] == 0) { if (paper > 0) c[i] = 'P', --paper; else if (rock > 0) c[i] = 'R', --rock; else if (scissors > 0) c[i] = 'S', --scissors; } } cout << c << endl; } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int q; cin >> q; while (q--) { int n, rock, paper, scissors; cin >> n >> rock >> paper >> scissors; string s; cin >> s; int r = 0, p = 0, ss = 0; for (int i = 0; i < s.size(); i++) { if (s[i] == 'R') r++; else if (s[i] == 'P') p++; else ss++; } int ans = std::min(r, paper) + std::min(p, scissors) + std::min(ss, rock); if (ans >= (n + 1) / 2) cout << "YES\n", display(s, rock, paper, scissors); else cout << "NO\n"; } }
2
#pragma GCC optimize("Ofast") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma") #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> #include <complex> #include <queue> #include <set> #include <unordered_set> #include <list> #include <chrono> #include <random> #include <iostream> #include <algorithm> #include <cmath> #include <string> #include <vector> #include <map> #include <unordered_map> #include <stack> #include <iomanip> #include <fstream> using namespace std; typedef long long ll; typedef long double ld; typedef pair<int,int> p32; typedef pair<ll,ll> p64; typedef pair<double,double> pdd; typedef vector<ll> v64; typedef vector<int> v32; typedef vector<vector<int> > vv32; typedef vector<vector<ll> > vv64; typedef vector<vector<p64> > vvp64; typedef vector<p64> vp64; typedef vector<p32> vp32; ll MOD = 998244353; double eps = 1e-12; #define forn(i,e) for(ll i = 0; i < e; i++) #define forsn(i,s,e) for(ll i = s; i < e; i++) #define rforn(i,s) for(ll i = s; i >= 0; i--) #define rforsn(i,s,e) for(ll i = s; i >= e; i--) #define ln "\n" #define dbg(x) cout<<#x<<" = "<<x<<ln #define mp make_pair #define pb push_back #define fi first #define se second #define INF 2e18 #define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL) #define all(x) (x).begin(), (x).end() #define sz(x) ((ll)(x).size()) void solve(){ ll n,sum1=0,sum2=0; cin>>n; vector<ll>v1(n),v2(n); forn(i,n) { cin>>v1[i]; // v1[i].se=i+1; sum1+=v1[i]; } forn(i,n) { cin>>v2[i]; // v2[i].se=i+1; sum2+=v2[i]; } if(sum1!=sum2) { cout<<-1<<endl; return; } v64 b,e; forn(i,n) { if(v1[i]>v2[i]) { forn(j,v1[i]-v2[i]) { b.pb(i+1); } continue; } forn(j,v2[i]-v1[i]) { e.pb(i+1); } } if(sz(b)-sz(e)!=0) { cout<<-1<<endl; return; } cout<<sz(b)<<endl; forn(i,sz(b)) { cout<<b[i]<<" "<<e[i]<<endl; } //cout<<endl; return; } int main() { fast_cin(); ll t; cin >> t; for(int it=1;it<=t;it++) { solve(); } return 0; }
1
#include <bits/stdc++.h> using namespace std; int n, m; long long a, b, t; int main(int argc, char* argv[]) { cin >> n >> m; a = 0; b = 0; for (int i = 0; i < n; i++) { cin >> t; a *= m; a += t; } cin >> n >> m; for (int i = 0; i < n; i++) { cin >> t; b *= m; b += t; } if (a == b) cout << "=" << endl; else if (a > b) cout << ">" << endl; else cout << "<" << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll maxN=1e6+10; ll a[maxN]; ll node[maxN*4]; ll lazy [ maxN * 4 ] ; ll num[maxN]; void Push_down(int l,int r,int rt) { if(lazy[rt]!=0) { int mid=(l+r)/2; node[rt*2]+=(mid-l+1)*lazy[rt]; node[rt*2+1]+=(r-mid)*lazy[rt]; lazy[rt*2]+=lazy[rt]; lazy[rt*2+1]+=lazy[rt]; lazy[rt]=0; } return ; } void Push_up(int rt) { node[ rt ] =node [ rt * 2 ] + node [ rt * 2 + 1 ] ; //node[ rt ] = max ( node[rt*2],node [rt *2+1]) ; } void Build(int l,int r,int rt) { lazy[rt]=0; if(l==r) { node[rt]=num[l] ; return ; } int mid=(l+r)/2; Build(l,mid,rt*2); Build(mid+1,r,rt*2+1); Push_up(rt); } int Query(int L, int R, int rt,ll k) { if(L==R) return L; Push_down(L,R,rt); int mid=(L+R)/2; if(k<=node[rt*2]) return Query(L,mid,rt*2,k); else return Query(mid+1,R,rt*2+1,k-node[rt*2]); } void Update(int l,int r ,int rt,int L,int R,ll K) { if (l>=L&&r<=R) { lazy[rt]+=K; node[rt]+=(ll)(r-l+1)*K; return ; } Push_down(l,r,rt); int mid=(l+r)/2; if(L<=mid) Update(l,mid,rt*2,L,R,K); if(R>mid) Update(mid+1,r,rt*2+1,L,R,K); Push_up(rt); } int fir(int l,int r,int rt) { if(l==r) return l; Push_down(l,r,rt); int mid=(l+r)/2; if(node[rt*2]) return fir(l,mid,rt*2); else return fir(mid+1,r,rt*2+1); } int main() { int UP=1e6+5; int n; ll k; scanf("%d%lld",&n,&k); ll sum=1; for(int i=1;i<=n;i++) { scanf("%lld",&a[i]); sum+=a[i]; sum-=2; } if(sum<k) { printf("-1\n"); return 0; } if(k==1) { printf("0\n"); return 0; } sort(a+1,a+1+n); for(int i=0;i<=UP;i++) num[i]=0; num[1]=1; Build(1,UP,1); ll pt=1; int ans=1e9; //printf("111\n"); for(int i=n;i>0;i--) { pt+=a[i]; pt-=2; int ros=fir(1,UP,1); Update(1,UP,1,ros,ros,-1); int sum=a[i]-1; Update(1,UP,1,ros+2,ros+2+sum/2-1,2ll); if(sum&1) Update(1,UP,1,ros+2+sum/2,ros+2+sum/2,1ll); if(pt>=k) { ans=min(ans,Query(1,UP,1,k)); } } printf("%d\n",ans-1); } /* #include <iostream> #include <cstdio> #include <cmath> #include <string> #include <cstring> #include <algorithm> #include <limits> #include <vector> #include <stack> #include <queue> #include <set> #include <map> #include <bitset> #include <unordered_map> #include <unordered_set> #define lowbit(x) ( x&(-x) ) #define pi 3.141592653589793 #define e 2.718281828459045 #define INF 0x3f3f3f3f #define HalF (l + r)>>1 #define lsn rt<<1 #define rsn rt<<1|1 #define Lson lsn, l, mid #define Rson rsn, mid+1, r #define QL Lson, ql, qr #define QR Rson, ql, qr #define myself rt, l, r #define pii pair<int, int> #define MP(a, b) make_pair(a, b) using namespace std; typedef unsigned long long ull; typedef unsigned int uit; typedef long long ll; const int maxN = 8e5 + 7; int N, K; ll len[maxN]; ll tree[maxN << 3], lazy[maxN << 3]; void pushdown(int rt, int l, int r) { if(lazy[rt]) { int mid = HalF; tree[lsn] += lazy[rt] * (mid - l + 1); tree[rsn] += lazy[rt] * (r - mid); lazy[lsn] += lazy[rt]; lazy[rsn] += lazy[rt]; lazy[rt] = 0; } } void pushup(int rt) { tree[rt] = tree[lsn] + tree[rsn]; } void update(int rt, int l, int r, int ql, int qr, ll val) { if(ql <= l && qr >= r) { lazy[rt] += val; tree[rt] += val * (r - l + 1); return; } pushdown(myself); int mid = HalF; if(qr <= mid) update(QL, val); else if(ql > mid) update(QR, val); else { update(QL, val); update(QR, val); } pushup(rt); } int _Fir(int rt, int l, int r) { if(l == r) return l; pushdown(myself); int mid = HalF; if(tree[lsn]) return _Fir(Lson); else return _Fir(Rson); } int _Ans(int rt, int l, int r, ll kth) { if(l == r) return l; pushdown(myself); int mid = HalF; if(kth <= tree[lsn]) return _Ans(Lson, kth); else return _Ans(Rson, kth - tree[lsn]); } int main() { scanf("%d%d", &N, &K); for(int i = 1; i <= N; i ++) scanf("%lld", &len[i]); sort(len + 1, len + N + 1, greater<ll>() ); int ans = INF; int _UP = 8e5 + 5; update(1, 0, _UP, 0, 0, 1); for(int i = 1, pos, nex_L, nex_R, tmp; i <= N; i ++) { pos = _Fir(1, 0, _UP); update(1, 0, _UP, pos, pos, -1); nex_L = pos + 2; tmp = (int)len[i] - 1; nex_R = nex_L + tmp / 2 - 1; update(1, 0, _UP, nex_L, nex_R, 2); if(tmp & 1) update(1, 0, _UP, nex_R + 1, nex_R + 1, 1); if(tree[1] >= K) { ans = min(ans, _Ans(1, 0, _UP, K)); } } if(ans < INF) printf("%d\n", ans); else printf("-1\n"); return 0; } */
6
#include <bits/stdc++.h> using namespace std; const long long MOD = 1e9 + 7; const int INF = 1e9; const long long IINF = 1e18; const int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; const char dir[4] = {'D', 'R', 'U', 'L'}; template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; } template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { for (int i = 0; i < v.size(); ++i) { os << v[i] << (i + 1 == v.size() ? "" : " "); } return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const pair<T, U> &p) { os << '(' << p.first << ',' << p.second << ')'; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, const map<T, U> &m) { os << '{'; for (auto itr = m.begin(); itr != m.end(); ++itr) { os << '(' << itr->first << ',' << itr->second << ')'; if (++itr != m.end()) os << ','; --itr; } os << '}'; return os; } template <typename T> ostream &operator<<(ostream &os, const set<T> &s) { os << '{'; for (auto itr = s.begin(); itr != s.end(); ++itr) { os << *itr; if (++itr != s.end()) os << ','; --itr; } os << '}'; return os; } void debug_out() { cerr << '\n'; } template <class Head, class... Tail> void debug_out(Head &&head, Tail &&...tail) { cerr << head; if (sizeof...(Tail) > 0) cerr << ", "; debug_out(move(tail)...); } template <typename T> T gcd(T x, T y) { return y != 0 ? gcd(y, x % y) : x; } template <typename T> T lcm(T x, T y) { return x / gcd(x, y) * y; } template <class T1, class T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true; } return false; } template <class T1, class T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true; } return false; } void solve() { int n, m; cin >> n >> m; vector<vector<pair<int, int>>> G(3 * n); for (int i = 0; i < m; ++i) { int u, v; cin >> u >> v; --u, --v; G[u].emplace_back(v, i); G[v].emplace_back(u, i); } vector<int> used(3 * n, 0), es; for (int v = 0; v < 3 * n; ++v) { if (used[v]) continue; for (auto e : G[v]) { int u = e.first; if (used[u]) continue; es.emplace_back(e.second); used[v] = used[u] = 1; break; } } if (es.size() >= n) { cout << "Matching" << '\n'; for (int i = 0; i < n; ++i) cout << es[i] + 1 << (i + 1 == n ? '\n' : ' '); } else { cout << "IndSet" << '\n'; for (int i = 0, cnt = 0; i < 3 * n && cnt < n; ++i) { if (used[i]) continue; cout << i + 1 << (++cnt == n ? '\n' : ' '); } } } int main() { cin.tie(0); ios::sync_with_stdio(false); int T; cin >> T; for (; T--;) { solve(); } }
3
#include <bits/stdc++.h> using namespace std; const double PI = 3.14159265358979323846; const double eps = (1e-5); int isGreater(long double x, long double y) { if (abs(x - y) < eps) return 0; if (x > y) return 1; return -1; } const int MAX_N = 105, H = 30, MID = 15; int n; map<vector<int>, int> cntToMask; map<int, vector<int> > maskToCnt; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n; vector<int> a(n); for (int i = 0; i < n; ++i) cin >> a[i]; for (int msk = 0; msk < (1 << MID); ++msk) { vector<int> cntOnes(n); for (int i = 0; i < n; ++i) { int x = a[i] ^ msk; for (int j = 0; j < MID; ++j) { if ((1 << j) & x) cntOnes[i]++; } } cntToMask[cntOnes] = msk; } for (int c = 0; c < (1 << MID); ++c) { int msk = (c << MID); vector<int> cntOnes(n); for (int i = 0; i < n; ++i) { int x = a[i] ^ msk; for (int j = MID; j < H; ++j) { if ((1 << j) & x) cntOnes[i]++; } } for (int b = 0; b <= H; ++b) { vector<int> need(n); for (int i = 0; i < n; ++i) need[i] = b - cntOnes[i]; auto it = cntToMask.find(need); if (it != cntToMask.end()) { cout << (it->second | msk) << '\n'; return 0; } } } cout << -1 << '\n'; return 0; }
6
#include <bits/stdc++.h> using namespace std; int n; int main() { cin >> n; if (n == 1) { cout << -1; return 0; } cout << n << " " << n + 1 << " " << n * (n + 1); return 0; }
3
#include <bits/stdc++.h> int num[30001], dp[30001][501]; int max(int a, int b) { return a > b ? a : b; } int main() { int n, d, x, i, j, ans; scanf("%d %d", &n, &d); for (i = 0; i < n; i++) { scanf("%d", &x); num[x]++; } memset(dp, -1, sizeof(dp)); dp[d][250] = num[d]; ans = 0; for (i = d; i <= 30000; i++) { for (j = 0; j <= 500; j++) { if (dp[i][j] == -1) continue; ans = max(ans, dp[i][j]); int step = d + j - 250; if (i + step <= 30000) dp[i + step][j] = max(dp[i + step][j], dp[i][j] + num[i + step]); if (step - 1 > 0 && i + step - 1 <= 30000) dp[i + step - 1][j - 1] = max(dp[i + step - 1][j - 1], dp[i][j] + num[i + step - 1]); if (i + step + 1 <= 30000) dp[i + step + 1][j + 1] = max(dp[i + step + 1][j + 1], dp[i][j] + num[i + step + 1]); } } printf("%d\n", ans); return 0; }
1
#include <bits/stdc++.h> using namespace std; int a, b, c; int main(){ cin >> a >> b >> c; if (c<min(a,b) || c>max(a,b)) cout << "No"; else cout << "Yes"; return 0; }
0
#include <bits/stdc++.h> using namespace std; int n, t, pi[1003], pref[1003], suf[1003], si[1003], temp, res, l; string s, z; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin >> s; cin >> n; for (int i = 1; i <= n; i++) { cin >> z; memset(suf, 0, sizeof(suf)); memset(pi, 0, sizeof(pi)); memset(si, 0, sizeof(si)); l = 0; temp = 0; pref[0] = -1; for (int j = 1; j < z.size(); j++) { pref[j] = s.size(); while (l != 0 && z[l] != z[j]) { l = pi[l - 1]; } if (z[l] == z[j]) { l++; pi[j] = l; } else { pi[j] = 0; } } pref[z.size()] = s.size(); l = 0; for (int j = 0; j < s.size(); j++) { if (l == z.size()) break; while (l != 0 && z[l] != s[j]) { l = pi[l - 1]; } if (z[l] == s[j]) { l++; } if (pref[l] == s.size() && l != 0) pref[l] = j - l + 1; } reverse(s.begin(), s.end()); reverse(z.begin(), z.end()); l = 0; for (int j = 1; j < z.size(); j++) { while (l != 0 && z[l] != z[j]) { l = si[l - 1]; } if (z[l] == z[j]) { l++; si[j] = l; } else { si[j] = 0; } } l = 0; suf[0] = s.size() + 1; for (int j = 0; j < s.size(); j++) { if (l == z.size()) break; while (l != 0 && z[l] != s[j]) { l = si[l - 1]; } if (z[l] == s[j]) { l++; } if (suf[l] == 0 && l != 0) suf[l] = s.size() - j - 2 + l; } for (int j = 0; j <= z.size(); j++) { if (suf[z.size() - j] - pref[j] >= (int)z.size() - 1) temp = 1; } if (temp == 1 && z.size() > 1) { res++; } reverse(s.begin(), s.end()); } cout << res << endl; }
5
#include <bits/stdc++.h> using namespace std; int main() { int n, x[1000], counter = 0; bool y = 0; cin >> n; for (int i = 0; i < n; i++) { cin >> x[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (x[i] < x[j] && i != j) { y = 0; break; } else y = 1; } if (y == 1) { x[i] = 0; i++; cout << i << endl; break; } } y = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (x[i] < x[j] && i != j) { y = 0; break; } else y = 1; } if (y == 1) { cout << x[i] << endl; break; } } return 0; }
1
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<cmath> #include<queue> #include<vector> #include<ctime> #include<map> #include<bitset> #include<set> #include<assert.h> #define LL long long #define mp(x,y) make_pair(x,y) #define pll pair<long long,long long> #define pii pair<int,int> #define SZ(x) ((int)x.size()) using namespace std; inline LL read() { LL f=1,x=0;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } int stack[20]; template<typename T>inline void write(T x) { if(x<0){putchar('-');x=-x;} if(!x){putchar('0');return;} int top=0; while(x)stack[++top]=x%10,x/=10; while(top)putchar(stack[top--]+'0'); } template<typename T>inline void pr1(T x){write(x);putchar(' ');} template<typename T>inline void pr2(T x){write(x);putchar('\n');} template<typename T>inline void chkmin(T &x,T y){x=x<y?x:y;} template<typename T>inline void chkmax(T &x,T y){x=x>y?x:y;} const int MAXN=200005; const int mod=1e9+7; const int inv2=(mod+1)>>1; int pow_mod(int a,int b) { int ret=1; for(;b;b>>=1,a=1LL*a*a%mod)if(b&1)ret=1LL*ret*a%mod; return ret; } int n,a[MAXN]; void ad(int &x,int y){x+=y;if(x>=mod)x-=mod;} struct bittree { int bit[MAXN]; int lowbit(int x){return x&-x;} void modify(int x,int c){for(;x<=n;x+=lowbit(x))ad(bit[x],c);} int qry(int x){int ret=0;for(;x>=1;x-=lowbit(x))ad(ret,bit[x]);return ret;} }bi,bi1,bi2; int id[MAXN],d[MAXN],nxt[MAXN],pre[MAXN],rnk[MAXN]; bool cmp(int n1,int n2){return a[n1]<a[n2];} int main() { // freopen("e.in","r",stdin); // freopen("e.out","w",stdout); n=read();for(int i=1;i<=n;i++)a[i]=read(),id[i]=i; sort(id+1,id+1+n,cmp);int sum=1; for(int i=1;i<=n;i++)rnk[id[i]]=i; for(int i=1;i<=n;i++)sum=1LL*sum*(a[id[i]]-i+1)%mod;if(!sum)return pr2(0),0; int ans=0; for(int i=1;i<=n;i++) { int u=id[i]; d[i]=1LL*pow_mod(a[u]-i+1,mod-2)*(a[u]-i)%mod; d[i]=d[i-1]?1LL*d[i-1]*d[i]%mod:d[i]; }nxt[n+1]=n+1; for(int i=1;i<=n;i++)pre[i]=(!d[i-1]?i-1:pre[i-1]); for(int i=n;i>=1;i--)nxt[i]=(!d[i+1]?i+1:nxt[i+1]); for(int i=n;i>=1;i--) { int u=rnk[i]; int nx=(bi.qry(nxt[u])-bi.qry(u-1)+mod)%mod; nx=1LL*nx*pow_mod(d[u],mod-2)%mod*(a[i]-u)%mod; ad(ans,1LL*nx*inv2%mod*sum%mod); bi.modify(u,1LL*pow_mod(a[i]-u+1,mod-2)*d[u-1]%mod); nx=(bi2.qry(u)-bi2.qry(pre[u]-1)+mod)%mod;int tot=bi1.qry(u); nx=1LL*nx*d[u-1]%mod*pow_mod(a[i]-u+1,mod-2)%mod; nx=1LL*nx*inv2%mod*sum%mod; nx=(1LL*sum*tot%mod-nx+mod)%mod; ad(ans,nx); bi1.modify(u,1); int val=(a[i]-u); if(d[u])bi2.modify(u,1LL*val*pow_mod(d[u],mod-2)%mod); else bi2.modify(u,val); }pr2(ans); return 0; }
0
#include <bits/stdc++.h> using namespace std; int main () { int a, b, c, x, ans=0; cin>>a>>b>>c>>x; for (int i=0;i<=a;i++) for(int j=0;j<=b;j++) if((x-i*500-j*100>=0)&&(x-i*500-j*100<=c*50)) ans++; cout<<ans<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int check(vector<char> a, int i, char b) { int dem = 0; int dem2 = 0; char C = a[i]; char D = a[i]; while (D != b) { dem++; D++; if (D == '[') D = 'A'; } while (C != b) { dem2++; C--; if (C == '@') C = 'Z'; } if (dem > dem2) return dem2; else return dem; } int main() { int n; cin >> n; int S1 = 0; int S2 = 999; vector<char> a; vector<char> b; vector<char> c; for (int i = 0; i < n; i++) { char p; cin >> p; a.push_back(p); } if (a.size() == 4) { S1 = check(a, 0, 'A') + check(a, 1, 'C') + check(a, 2, 'T') + check(a, 3, 'G'); cout << S1; } else { for (int i = 0; i < a.size() - 3; i++) { S1 = check(a, i, 'A') + check(a, i + 1, 'C') + check(a, i + 2, 'T') + check(a, i + 3, 'G'); S2 = min(S1, S2); } cout << S2; } }
1
#include <bits/stdc++.h> using namespace std; long long mod = 1e9 + 7; long long INF = 1e7; double EPS = 1e-12; double tick() { static clock_t oldt; clock_t newt = clock(); double diff = 1.0 * (newt - oldt) / CLOCKS_PER_SEC; oldt = newt; return diff; } long long int powP(long long int a, long long int b) { if (b == 0) return 1 % mod; long long int k; k = powP(a, b / 2); k = k * k % mod; if (b % 2 == 0) return k; else return a * k % mod; } bool bitSet(long long int n, long long int i) { if ((n & (1LL << i)) != 0) return true; else return false; } long long int findGcd(long long int a, long long int b) { if (a == 0) return b; else return findGcd(b % a, a); } long long int A[300010], B[300010], C[300010], Taken[300010]; long long int X[300010], Y[300010]; int main() { long long int T, i, p, j, l, n, e, r, b, c, k, m, q, a, d, w, x, y, u, v, z, t, curr, prev, sum, ans, pos, val, countA, indicator; scanf("%lld", &n); for (long long int i = (1); i <= (n - 1); ++i) { scanf("%lld%lld", &a, &b); C[a]++; if (b != n) { printf("NO\n"); return 0; } } long long int edges = 1; for (long long int i = (n - 1); i >= (1); --i) { long long int ind = i - 1; curr = n; while (ind > 0 and C[i] > 1) { if (C[ind] == 0 and Taken[ind] == 0) { C[i]--; Taken[ind] = 1; X[edges] = curr; Y[edges] = ind; edges++; curr = ind; } ind--; } if (C[i] > 1) { printf("NO\n"); return 0; } if (C[i] == 1) { X[edges] = curr; Y[edges] = i; edges++; } } assert(edges == n); printf("YES\n"); for (long long int i = (1); i <= (n - 1); ++i) { printf("%lld %lld\n", X[i], Y[i]); } return 0; }
5
#include <bits/stdc++.h> using namespace std; int n; string A[1010], B[1010]; string C[1010]; unordered_map<string, vector<int> > VA, VB; int cal() { while (1) { int f = 0; for (auto it = VB.begin(); it != VB.end(); ++it) { if (VA[it->first].size() != 1) continue; int idx = VA[it->first][0]; VA[it->first].clear(); string s = A[idx].substr(0, 2) + B[idx][0]; VB[s].push_back(idx); C[idx] = s; f = 1; } if (f == 0) break; } for (auto it = VB.begin(); it != VB.end(); ++it) { if (it->second.size() > 1) return 0; } return 1; } int main() { scanf("%d", &n); char ch[100]; for (int i = 1; i <= n; i++) { scanf("%s", &ch); string s1(ch); scanf("%s", &ch); string s2(ch); string s = s1.substr(0, 3); C[i] = s; VA[s].push_back(i); A[i] = s1; B[i] = s2; } for (auto it = VA.begin(); it != VA.end(); ++it) { if (it->second.size() == 1) continue; for (int i : it->second) { string s = A[i].substr(0, 2) + B[i][0]; VB[s].push_back(i); C[i] = s; } it->second.clear(); } if (cal()) { printf("YES\n"); for (int i = 1; i <= n; i++) { cout << C[i] << endl; } } else printf("NO\n"); return 0; }
4
#include <bits/stdc++.h> using namespace std; int p[111][5]; double dx[555], dy[555]; int cnt; int n; int main() { scanf("%d", &n); cnt = 0; for (int j = 0; j < 5; ++j) p[1][j] = ++cnt; dx[p[1][0]] = 3.830127018922193; dy[p[1][0]] = 3.366025403784439; dx[p[1][1]] = -3.601321235851749; dy[p[1][1]] = 10.057331467373021; dx[p[1][2]] = 0.466045194906253; dy[p[1][2]] = 19.192786043799030; dx[p[1][3]] = 10.411264148588986; dy[p[1][3]] = 18.147501411122495; dx[p[1][4]] = 12.490381056766580; dy[p[1][4]] = 8.366025403784439; for (int i = 2; i <= n; ++i) { p[i][0] = p[i - 1][2]; for (int j = 1; j < 5; ++j) p[i][j] = ++cnt; double x0 = dx[p[i - 1][2]] - dx[p[i - 1][0]]; double y0 = dy[p[i - 1][2]] - dy[p[i - 1][0]]; for (int j = 0; j < 5; ++j) { dx[p[i][j]] = dx[p[i - 1][j]] + x0; dy[p[i][j]] = dy[p[i - 1][j]] + y0; } } printf("%d\n", cnt); for (int i = 1; i <= cnt; ++i) printf("%.15lf %.15lf\n", dx[i], dy[i]); for (int i = 1; i <= n; ++i) for (int j = 0; j < 5; ++j) printf("%d%c", p[i][j], j == 4 ? '\n' : ' '); printf("%d", p[1][0]); for (int i = 1; i <= n; ++i) printf(" %d", p[i][2]); for (int i = n; i >= 1; --i) printf(" %d %d %d %d", p[i][4], p[i][1], p[i][3], p[i][0]); printf("\n"); return 0; }
4
#include <stdio.h> int main(){ int N=0; scanf("%d",&N); if(N<1200) { printf("ABC"); } else { printf("ARC"); } return 0; }
0
#include <bits/stdc++.h> using namespace std; int a[200000]; int b[200000]; int n, d; long long int x; int getNextX() { x = (x * 37 + 10007) % 1000000007; return x; } void initAB() { int i; for (i = 0; i < n; i = i + 1) { a[i] = i + 1; } for (i = 0; i < n; i = i + 1) { swap(a[i], a[getNextX() % (i + 1)]); } for (i = 0; i < n; i = i + 1) { if (i < d) b[i] = 1; else b[i] = 0; } for (i = 0; i < n; i = i + 1) { swap(b[i], b[getNextX() % (i + 1)]); } } vector<int> filter; vector<pair<int, int> > v; int main() { int i; cin >> n >> d >> x; initAB(); for (i = 0; i < n; i++) { if (b[i]) filter.push_back(i); v.push_back(make_pair(a[i], i)); } sort(v.rbegin(), v.rend()); if (filter.size() <= 1000) { int j; for (i = 0; i < n; i++) { int cx = 0; for (j = 0; j < (int)filter.size() && filter[j] <= i; j++) { cx = max(cx, a[i - filter[j]]); } printf("%d\n", cx); } } else { int j; int cx = 0; for (i = 0; i < n; i++) { cx = 0; for (j = 0; j < (int)v.size(); j++) { if (v[j].second <= i && b[i - v[j].second]) { cx = v[j].first; break; } } printf("%d\n", cx); } } return 0; }
2
#include <bits/stdc++.h> using namespace std; int main() { long long int n, k, i, j, l, f = 0, m, g = 0, size; cin >> n >> k; long long int arr[n]; long long int temp[k]; for (i = 0; i < n; i++) cin >> arr[i]; for (i = 0; i < n; i++) { if (f == k) break; for (j = 0; j < f; j++) { if (temp[j] == arr[i]) { g++; break; } } if (g == 0) temp[f++] = arr[i]; else g = 0; } for (j = f; j < n; j++) { g = 0; for (l = 0; l < f; l++) { if (temp[l] == arr[j]) { g++; break; } } if (g == 0) { if (f < k) temp[f++] = arr[j]; else { for (m = 1; m < f; m++) temp[m - 1] = temp[m]; temp[m - 1] = arr[j]; continue; } } } cout << f << endl; for (i = f - 1; i >= 0; i--) cout << temp[i] << " "; cout << endl; return 0; }
2
#include <bits/stdc++.h> using namespace std; const long long mod = (long long)(1000000007); double power(double x, int y) { double ans = 1; while (y) { if (y & 1) { ans = ans * x; } x = x * x; y = y >> 1; } return ans; } int main() { double m, n; cin >> m >> n; double ans = 0; for (int i = 1; i <= m; i++) { double x = power(i / m, n); double y = power((i - 1) / m, n); ans += (x - y) * i; } cout << ans; return 0; }
1
#include <bits/stdc++.h> using namespace std; const int MAXN = 100010; map<int, vector<pair<int, int> > > e; int n, m, root, cnt, deg[MAXN]; int c[MAXN], id[MAXN], to[MAXN], belong[MAXN]; inline int read() { int X = 0, flag = 0; char ch = 0; while (!isdigit(ch)) flag |= ch == '-', ch = getchar(); while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar(); return flag ? -X : X; } template <typename F> inline void write(F x, char ed = '\n') { 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(ed); } inline void dfs(int x, int eid, int father) { id[x] = ++cnt; belong[id[x]] = id[father]; to[eid] = id[x]; for (int i = 0; i < (int)e[x].size(); ++i) if (e[x][i].second != eid) dfs(e[x][i].first, e[x][i].second, father); } inline void Insert(int x, int val) { for (; x <= n; x += x & (-x)) c[x] += val; } inline int Sum(int x) { int ret = 0; for (; x > 0; x -= x & (-x)) ret += c[x]; return ret; } signed main() { n = read(); for (int i = 1, x, y; i <= n - 1; ++i) { x = read(), y = read(); e[x].push_back(make_pair(y, i)); e[y].push_back(make_pair(x, i)); deg[x]++, deg[y]++; } root = 1; for (int i = 1; i <= n; ++i) if (deg[i] > 2) root = i, i = n + 1; cnt = 0; id[root] = ++cnt; for (int i = 0; i < (int)e[root].size(); ++i) dfs(e[root][i].first, e[root][i].second, e[root][i].first); m = read(); memset(c, 0, sizeof(c)); for (int opt, x, y; m; m--) { opt = read(); if (opt == 1) { x = read(); Insert(to[x], -1); } if (opt == 2) { x = read(); Insert(to[x], 1); } if (opt == 3) { x = read(), y = read(); x = id[x], y = id[y]; if (x == y) { puts("0"); continue; } if (x > y) swap(x, y); if (x == 1) { int tem = Sum(y) - Sum(belong[y] - 1); if (tem) puts("-1"); else printf("%d\n", y - belong[y] + 1); } else { if (belong[x] != belong[y]) { int tem = Sum(x) - Sum(belong[x] - 1) + Sum(y) - Sum(belong[y] - 1); if (tem) puts("-1"); else printf("%d\n", x - belong[x] + 1 + y - belong[y] + 1); } else { int tem = Sum(y) - Sum(x); if (tem) puts("-1"); else printf("%d\n", y - x); } } } } return 0; }
4
#include <algorithm> #include <array> #include <cassert> #include <iostream> #include <vector> using namespace std; template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; } template<typename T, size_t size> ostream& operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; } template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } void dbg_out() { cerr << endl; } template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } #ifdef NEAL_DEBUG #define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__) #else #define dbg(...) #endif template<const int &MOD> struct _m_int { int val; _m_int(int64_t v = 0) { if (v < 0) v = v % MOD + MOD; if (v >= MOD) v %= MOD; val = int(v); } static int inv_mod(int a, int m = MOD) { // https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Example int g = m, r = a, x = 0, y = 1; while (r != 0) { int q = g / r; g %= r; swap(g, r); x -= q * y; swap(x, y); } return x < 0 ? x + m : x; } explicit operator int() const { return val; } explicit operator int64_t() const { return val; } _m_int& operator+=(const _m_int &other) { val -= MOD - other.val; if (val < 0) val += MOD; return *this; } _m_int& operator-=(const _m_int &other) { val -= other.val; if (val < 0) val += MOD; return *this; } static unsigned fast_mod(uint64_t x, unsigned m = MOD) { #if !defined(_WIN32) || defined(_WIN64) return unsigned(x % m); #endif // Optimized mod for Codeforces 32-bit machines. // x must be less than 2^32 * m for this to work, so that x / m fits in a 32-bit unsigned int. unsigned x_high = unsigned(x >> 32), x_low = unsigned(x); unsigned quot, rem; asm("divl %4\n" : "=a" (quot), "=d" (rem) : "d" (x_high), "a" (x_low), "r" (m)); return rem; } _m_int& operator*=(const _m_int &other) { val = fast_mod(uint64_t(val) * other.val); return *this; } _m_int& operator/=(const _m_int &other) { return *this *= other.inv(); } friend _m_int operator+(const _m_int &a, const _m_int &b) { return _m_int(a) += b; } friend _m_int operator-(const _m_int &a, const _m_int &b) { return _m_int(a) -= b; } friend _m_int operator*(const _m_int &a, const _m_int &b) { return _m_int(a) *= b; } friend _m_int operator/(const _m_int &a, const _m_int &b) { return _m_int(a) /= b; } _m_int& operator++() { val = val == MOD - 1 ? 0 : val + 1; return *this; } _m_int& operator--() { val = val == 0 ? MOD - 1 : val - 1; return *this; } _m_int operator++(int) { _m_int before = *this; ++*this; return before; } _m_int operator--(int) { _m_int before = *this; --*this; return before; } _m_int operator-() const { return val == 0 ? 0 : MOD - val; } bool operator==(const _m_int &other) const { return val == other.val; } bool operator!=(const _m_int &other) const { return val != other.val; } _m_int inv() const { return inv_mod(val); } _m_int pow(int64_t p) const { if (p < 0) return inv().pow(-p); _m_int a = *this, result = 1; while (p > 0) { if (p & 1) result *= a; p >>= 1; if (p > 0) a *= a; } return result; } friend ostream& operator<<(ostream &os, const _m_int &m) { return os << m.val; } }; extern const int MOD = 1e9 + 7; using mod_int = _m_int<MOD>; int main() { ios::sync_with_stdio(false); #ifndef NEAL_DEBUG cin.tie(nullptr); #endif int N, M; cin >> N >> M; vector<mod_int> dp(M + 1, 1); for (int i = 0; i < N; i++) { mod_int sum = 0; for (int m = 0; m <= M; m++) { mod_int product = m * dp[m]; dp[m] += sum + product; sum = sum + sum + product; } } cout << dp[M] << '\n'; }
0
#include <bits/stdc++.h> using namespace std; string onezero(int n) { string s = "1"; for (int i = 1; i < n; ++i) { s += "0"; } return s; } string findSum(string str1, string str2) { if (str1.length() > str2.length()) swap(str1, str2); string str = ""; int n1 = str1.length(), n2 = str2.length(); reverse(str1.begin(), str1.end()); reverse(str2.begin(), str2.end()); int carry = 0; for (int i = 0; i < n1; i++) { int sum = ((str1[i] - '0') + (str2[i] - '0') + carry); str.push_back(sum % 10 + '0'); carry = sum / 10; } for (int i = n1; i < n2; i++) { int sum = ((str2[i] - '0') + carry); str.push_back(sum % 10 + '0'); carry = sum / 10; } if (carry) str.push_back(carry + '0'); reverse(str.begin(), str.end()); return str; } int main() { ios_base::sync_with_stdio(NULL); cin.tie(NULL); cout.tie(NULL); int l; string s, ans; cin >> l >> s; if (s.size() % l != 0) { string zero = onezero(l); for (int i = 0; i < s.size() / l + 1; ++i) { ans += zero; } } else { string temp = s.substr(0, l); for (int i = 0; i < s.size() / l; ++i) { ans += temp; } if (ans <= s) { ans = ""; temp = findSum(temp, "1"); if (temp.size() != l) { temp = onezero(l); ans = onezero(l); } for (int i = 0; i < s.size() / l; ++i) { ans += temp; } } } cout << ans << endl; }
3
#include <stdio.h> int main (){ int H,A; scanf("%d %d", &H,&A); printf("%d", ((H-1)/A)+1); return 0; }
0
#include <bits/stdc++.h> using namespace std; set<pair<long long, long long> > s; int a, b, w, h, n; int A[100100]; bool good(long long w, long long h) { return (w >= a && h >= b) || (w >= b && h >= a); } int main() { ios::sync_with_stdio(false); cin >> a >> b >> w >> h >> n; for (int i = 0; i < (int)(n); ++i) { cin >> A[i]; } sort(A, A + n); s.insert(make_pair(min(w, h), max(w, h))); for (int i = 0; i < (int)(n + 1); ++i) { for (auto x : s) { if (good(x.first, x.second)) { cout << i << endl; return 0; } } if (i == n) { break; } long long m = A[n - i - 1]; vector<pair<long long, long long> > nval; for (auto x : s) { if (x.first <= 100000) { nval.push_back( make_pair(min(x.first * m, x.second), max(x.first * m, x.second))); } if (x.second <= 100000) { nval.push_back( make_pair(min(x.second * m, x.first), max(x.second * m, x.first))); } } s.clear(); for (auto x : nval) { s.insert(x); } } cout << -1 << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e6 + 7; int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int a; cin >> a; cout << a % 2 << endl; }
1
#include <bits/stdc++.h> using namespace std; const int MOD = 998244353; const int BASE = 27; const int oo = 1e9; const double EPS = 1e-9; const int N = 300000 + 1; const int BIT = 19; int n, k, second[N], first[N], dp[N][2]; int cl(int a, int b) { return a / b + (a % b != 0); } long long fn(long long pa, long long push_back, long long a, long long b) { long long ans = oo; if (pa <= k) { int cnt = cl(pa + a, k) - 1; if (b >= cnt && b <= a * k) { if (b == cnt) { ans = min(ans, ((pa + a) % k == 0 ? k : (pa + a) % k)); } else { ans = 1; } } } if (push_back <= k) { int cnt = cl(a, k) - 1; if (b >= cnt && b <= (k - push_back) + (a - 1) * k) { if (b == cnt) { ans = min(ans, (a % k == 0 ? k : a % k)); } else { ans = 1; } } } return ans; } int main() { cin >> n >> k; for (int i = 1; i <= n; ++i) scanf("%d", &first[i]); for (int i = 1; i <= n; ++i) scanf("%d", &second[i]); dp[0][0] = dp[0][1] = 0; for (int i = 1; i <= n; ++i) { dp[i][0] = fn(dp[i - 1][0], dp[i - 1][1], first[i], second[i]); dp[i][1] = fn(dp[i - 1][1], dp[i - 1][0], second[i], first[i]); } if (min(dp[n][1], dp[n][0]) <= k) { puts("YES"); } else { puts("NO"); } return 0; }
6
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; long long int x, y; for (int i = 0; i < n; ++i) { cin >> x >> y; cout << x << " " << x * 2 << endl; } }
1
#include <bits/stdc++.h> using namespace std; int solve(int n, int m, int a, int b) { a--; b--; int i1 = a / m, j1 = a % m; int i2 = b / m, j2 = b % m; if ((j1 == 0 && (j2 == m - 1 || b == n - 1)) || i1 == i2) return 1; if (j1 == 0 || j2 == m - 1 || b == n - 1 || i2 - i1 == 1 || j1 - j2 == 1) return 2; return 3; } int main() { for (int n, m, a, b; cin >> n >> m >> a >> b && n | m | a | b;) { cout << solve(n, m, a, b) << endl; } return 0; }
1
#include<bits/stdc++.h> using namespace std; #define int long long const int MAXN=9e5+1e1,Mod=1e9+7,Tp=9e5; int fac[MAXN],inv[MAXN],p[MAXN],f[MAXN]; int n,m,k,ans1=0,ans2=0; int Qpow(int A,int B) { int res=1; while(B>0) { if(B&1) (res*=A)%=Mod; (A*=A)%=Mod,B>>=1; } return res; } int C(int A,int B) { if(A<0 || B<0 || A<B) return 0; else return fac[A]*inv[B]%Mod*inv[A-B]%Mod; } signed main(void) { scanf("%lld%lld%lld",&n,&m,&k); fac[0]=inv[0]=p[0]=f[0]=1; for(int i=1;i<=Tp;i++) fac[i]=fac[i-1]*i%Mod; inv[Tp]=Qpow(fac[Tp],Mod-2); for(int i=Tp;i;i--) inv[i-1]=inv[i]*i%Mod; for(int i=1;i<=Tp;i++) p[i]=p[i-1]*3%Mod; for(int i=1;i<=Tp;i++) f[i]=(f[i-1]*2%Mod-C(i-1,m)-C(i-1,i-n)+Mod*2)%Mod; for(int i=0;i<n+m;i++) (ans1+=C(i+k,i)*p[n+m-i-1]%Mod*f[i]%Mod)%=Mod; for(int i=1;i<=Tp;i++) f[i]=(f[i-1]*2%Mod-C(i-1,k)-C(i-1,i-n)+Mod*2)%Mod; for(int i=0;i<n+k;i++) (ans2+=C(i+m,i)*p[n+k-i-1]%Mod*f[i]%Mod)%=Mod; printf("%lld\n",(p[n+m+k]-ans1-ans2+Mod*2)%Mod); return 0; } //300000 300000 300000 //415274335
0
#include<bits/stdc++.h> using namespace std; int main() { set<int> st; for(int i=0;i<=101;i++) st.insert(i); int n,x; cin>>x>>n; for(int i=1;i<=n;i++) { int p; cin>>p; st.erase(p); } int ans=0; for(auto it:st) if(abs(it-x)<abs(ans-x)) ans=it; cout<<ans; }
0
#include <bits/stdc++.h> using namespace std; int main(void) { unsigned n, k; cin >> n >> k; unsigned *temp = new unsigned[n]; for (unsigned i = 0; i < n; ++i) { cin >> temp[i]; } double max = 0.; for (int i = 0; i <= n - k; ++i) { unsigned sum = 0U; int j = i; for (; j < i + k; ++j) { sum += temp[j]; } double tmp = double(sum) / (j - i); if (tmp > max) max = tmp; for (; j < n; ++j) { sum += temp[j]; tmp = double(sum) / (j - i + 1); if (tmp > max) max = tmp; } } printf("%lf\n", max); delete[] temp; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long int n, m; long double k; cin >> n >> m >> k; vector<pair<string, long long int> > v(n); for (int i = 0; i < n; i++) { cin >> v[i].first >> v[i].second; } vector<pair<string, long long int> > x(m); for (int i = 0; i < m; i++) { cin >> x[i].first; x[i].second = 0; } vector<pair<string, long long int> > ans; map<string, long long int> mx; for (int i = 0; i < n; i++) { long double xx = v[i].second; long long int z = xx * k + 1e-7; if (z >= 100) { ans.push_back(make_pair(v[i].first, z)); mx[v[i].first] = 1; } } for (int i = 0; i < m; i++) { if (mx.find(x[i].first) == mx.end()) { mx[x[i].first] = 1; ans.push_back(x[i]); } } sort(ans.begin(), ans.end()); cout << ans.size() << endl; for (int i = 0; i < ans.size(); i++) cout << ans[i].first << " " << ans[i].second << endl; return 0; }
1
#include <iostream> using namespace std; int main() { while(1){ int a, b; char op; cin >> a >> op >> b; if(op == '+') cout << a+b <<endl; else if(op == '-') cout << a-b <<endl; else if(op == '*') cout << a*b <<endl; else if(op == '/') cout << a/b <<endl; else break; } return 0; }
0
#include <bits/stdc++.h> using namespace std; signed main(){ char a,b; cin >> a >> b; cout << ((a ^ b) ? "D" : "H") << endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; struct node { int l, r, mid; int val; node *lch, *rch; node(int a, int b) { l = a; r = b; mid = (a + b) / 2; lch = rch = 0; val = b - a; } void check() { if (lch == 0) lch = new node(l, mid); if (rch == 0) rch = new node(mid, r); } void init() { val = r - l; if (l == r - 1) return; check(); lch->init(); rch->init(); } void minus(int n) { --val; if (l == r - 1) return; check(); if (n < mid) { lch->minus(n); } else { rch->minus(n); } } int getord(int n) { if (l == r - 1) return 1; check(); if (n < mid) return lch->getord(n); else return lch->val + rch->getord(n); } int getnum(int ord) { if (l == r - 1) return l; if (ord <= lch->val) { return lch->getnum(ord); } else { return rch->getnum(ord - lch->val); } } }; int main() { int n; cin >> n; node *root = new node(0, n); int nfac = 1; for (int i = 2; i <= n; ++i) nfac *= i; root->init(); int p[200001]; for (int i = n - 1; i >= 0; --i) { int x, add; cin >> x; add = root->getord(x); p[i] = add - 1; root->minus(x); } root->init(); int q[200001]; for (int i = n - 1; i >= 0; --i) { int x, add; cin >> x; add = root->getord(x); q[i] = add - 1; root->minus(x); } root->init(); int perm[200001]; int carry = 0; for (int i = 0; i < n; ++i) { int k = carry + p[i] + q[i]; perm[i] = k % (i + 1); carry = k / (i + 1); } for (int i = n - 1; i >= 0; --i) { int out; out = root->getnum(perm[i] + 1); if (i != n - 1) cout << ' '; cout << out; root->minus(out); } cout << '\n'; return 0; }
2
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5; long long N, Q, A[MAXN]; long long solve(long long AA, long long BB) { long long X, Y; if ((AA + BB) & 1) { BB++; } Y = (AA + BB) / 2; X = AA - Y; return max(X, Y); } void solve() { vector<long long> diff(N - 1); long long inc = 0; for (int i = 0; i < (int)N - 1; ++i) { diff[i] = A[i + 1] - A[i]; if (diff[i] > 0) { inc += diff[i]; } } cout << solve(A[0], inc) << endl; for (int i = 0; i < (int)Q; ++i) { int l, r, x; cin >> l >> r >> x; l--; r--; if (l == 0) A[0] += x; if (l != 0) { if (diff[l - 1] > 0) inc -= diff[l - 1]; diff[l - 1] += x; if (diff[l - 1] > 0) inc += diff[l - 1]; } if (r != N - 1) { if (diff[r] > 0) inc -= diff[r]; diff[r] -= x; if (diff[r] > 0) inc += diff[r]; } cout << solve(A[0], inc) << endl; } } int main() { cin >> N; for (int i = 0; i < (int)N; ++i) cin >> A[i]; cin >> Q; solve(); return 0; }
4
#include <bits/stdc++.h> using namespace std; void solve() { long long x, y, a, b, c = 0; cin >> x >> y >> a >> b; vector<pair<long long, long long> > A; for (long long i = (a); i < (x + 1); i++) for (long long j = (b); j < (y + 1); j++) if (i > j) A.push_back(make_pair(i, j)); cout << A.size() << '\n'; for (long long i = (0); i < (A.size()); i++) cout << A[i].first << " " << A[i].second << '\n'; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t = 1; while (t--) { solve(); } return 0; }
1
#include <bits/stdc++.h> using namespace std; inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v; } template <class T> inline string toString(T x) { ostringstream sout; sout << x; return sout.str(); } inline int readInt() { int x; scanf("%d", &x); return x; } const double EPS = 1E-8; class UnionFind { public: vector<long long> par; vector<long long> siz; vector<long long> maxv; UnionFind(long long sz_) : par(sz_), siz(sz_, 1LL) { for (long long i = 0; i < sz_; ++i) par[i] = i; } void init(long long sz_) { par.resize(sz_); siz.assign(sz_, 1LL); for (long long i = 0; i < sz_; ++i) par[i] = i; } long long root(long long x) { while (par[x] != x) { x = par[x] = par[par[x]]; } return x; } bool merge(long long x, long long y) { x = root(x); y = root(y); if (x == y) return false; if (siz[x] < siz[y]) swap(x, y); siz[x] += siz[y]; par[y] = x; return true; } bool issame(long long x, long long y) { return root(x) == root(y); } long long size(long long x) { return siz[root(x)]; } }; long long mod_pow(long long x, long long n, long long mod) { long long res = 1; while (n) { if (n & 1) res = res * x; res %= mod; x = x * x % mod; n >>= 1; } return res; } bool sieve[5000000 + 10]; void make_sieve() { for (int i = 0; i < 5000000 + 10; ++i) sieve[i] = true; sieve[0] = sieve[1] = false; for (int i = 2; i * i < 5000000 + 10; ++i) if (sieve[i]) for (int j = 2; i * j < 5000000 + 10; ++j) sieve[i * j] = false; } bool isprime(long long n) { if (n == 0 || n == 1) return false; for (long long i = 2; i * i <= n; ++i) if (n % i == 0) return false; return true; } const int MAX = 510000; long long fac[MAX], finv[MAX], inv[MAX]; void COMinit() { fac[0] = fac[1] = 1; finv[0] = finv[1] = 1; inv[1] = 1; for (int i = 2; i < MAX; i++) { fac[i] = fac[i - 1] * i % 1000000007; inv[i] = 1000000007 - inv[1000000007 % i] * (1000000007 / i) % 1000000007; finv[i] = finv[i - 1] * inv[i] % 1000000007; } } long long COM(int n, int k) { if (n < k) return 0; if (n < 0 || k < 0) return 0; return fac[n] * (finv[k] * finv[n - k] % 1000000007) % 1000000007; } long long extGCD(long long a, long long b, long long &x, long long &y) { if (b == 0) { x = 1; y = 0; return a; } long long d = extGCD(b, a % b, y, x); y -= a / b * x; return d; } inline long long mod(long long a, long long m) { return (a % m + m) % m; } long long modinv(long long a, long long m) { long long x, y; extGCD(a, m, x, y); return mod(x, m); } long long GCD(long long a, long long b) { if (b == 0) return a; return GCD(b, a % b); } struct LazySegmentTree { private: int n; vector<long long> node, lazy; public: LazySegmentTree(vector<long long> v) { int sz = (int)v.size(); n = 1; while (n < sz) n *= 2; node.resize(2 * n - 1); lazy.resize(2 * n - 1, 0); for (int i = 0; i < sz; i++) node[i + n - 1] = v[i]; for (int i = n - 2; i >= 0; i--) node[i] = node[i * 2 + 1] + node[i * 2 + 2]; } void eval(int k, int l, int r) { if (lazy[k] != 0) { node[k] += lazy[k]; if (r - l > 1) { lazy[2 * k + 1] += lazy[k] / 2; lazy[2 * k + 2] += lazy[k] / 2; } lazy[k] = 0; } } void add(int a, int b, long long x, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; eval(k, l, r); if (b <= l || r <= a) return; if (a <= l && r <= b) { lazy[k] += (r - l) * x; eval(k, l, r); } else { add(a, b, x, 2 * k + 1, l, (l + r) / 2); add(a, b, x, 2 * k + 2, (l + r) / 2, r); node[k] = node[2 * k + 1] + node[2 * k + 2]; } } long long getsum(int a, int b, int k = 0, int l = 0, int r = -1) { if (r < 0) r = n; eval(k, l, r); if (b <= l || r <= a) return 0; if (a <= l && r <= b) return node[k]; long long vl = getsum(a, b, 2 * k + 1, l, (l + r) / 2); long long vr = getsum(a, b, 2 * k + 2, (l + r) / 2, r); return vl + vr; } }; using Weight = int; using Flow = int; struct Edge { int src, dst; Weight weight; Flow cap; Edge() : src(0), dst(0), weight(0) {} Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {} }; using Edges = std::vector<Edge>; using Graph = std::vector<Edges>; using Array = std::vector<Weight>; using Matrix = std::vector<Array>; void add_edge(Graph &g, int a, int b, Weight w = 1) { g[a].emplace_back(a, b, w); g[b].emplace_back(b, a, w); } void add_arc(Graph &g, int a, int b, Weight w = 1) { g[a].emplace_back(a, b, w); } int n; int a[10]; long long dp[101][11]; long long coef[101][101]; long long rec(int n, int c) { if (!n) { bool ok = false; for (int i = c; i < 10; i++) ok |= !!a[i]; return !ok; } if (c == 10) return 0; if (~dp[n][c]) return dp[n][c]; dp[n][c] = 0; for (int i = a[c]; i <= n; i++) { dp[n][c] = (dp[n][c] + coef[n][i] * rec(n - i, c + 1)) % 1000000007; } return dp[n][c]; } int main() { cin.tie(0); ios::sync_with_stdio(false); cin >> n; int sum = 0; for (int i = 0; i < (int)10; ++i) { cin >> a[i]; sum += a[i]; } coef[0][0] = 1; for (int i = 1; i <= 100; i++) { coef[i][0] = coef[i][i] = 1; for (int j = 1; j < i; j++) coef[i][j] = (coef[i - 1][j] + coef[i - 1][j - 1]) % 1000000007; } long long ans = 0; for (int dig = 1; dig <= n; dig++) { for (int i = 1; i < 10; i++) { memset(dp, -1, sizeof(dp)); bool u = !!a[i]; a[i] -= u; ans = (ans + rec(dig - 1, 0)) % 1000000007; a[i] += u; } } cout << ans << endl; return 0; }
4
#include <bits/stdc++.h> using namespace std; const int maxn = 2311111; int n, m, a[maxn], fa[maxn]; int get(int x) { if (fa[x] == x) return x; return fa[x] = get(fa[x]); } struct node { int i, j; bool operator<(const node &b) const { return a[(i - 1) * m + j] < a[(b.i - 1) * m + b.j]; } } p[maxn]; int lmin[maxn], cmin[maxn], ans[maxn]; int main() { cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> a[(i - 1) * m + j]; p[(i - 1) * m + j] = (node){i, j}; fa[(i - 1) * m + j] = (i - 1) * m + j; } } sort(p + 1, p + 1 + n * m); for (int i = 1; i <= n * m; i++) { int x = p[i].i, y = p[i].j; int id = (x - 1) * m + y; int fx = get(lmin[x]), fy = get(cmin[y]); ans[id] = max(ans[fx] + (a[fx] < a[id]), ans[fy] + (a[fy] < a[id])); if (a[fx] == a[id]) fa[fx] = id; if (a[fy] == a[id]) fa[fy] = id; lmin[x] = cmin[y] = id; } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cout << ans[get((i - 1) * m + j)] << ' '; } cout << endl; } return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; for (int i = 1; i <= n; i++) { n = n - i; } if (n == 0) { cout << "YES" << endl; } else { cout << "NO" << endl; } return 0; }
1
#include<bits/stdc++.h> using namespace std; signed main(){ int a,b; cin>>a>>b; cout<<(a-1)*(b-1)<<"\n"; }
0
#include <bits/stdc++.h> using namespace std; int main() { int n, cnt(0); cin >> n; int x[n], y[n]; for (int i = 0; i < n; i++) { cin >> x[i] >> y[i]; } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (x[i] == y[j]) cnt++; } } cout << cnt << endl; return 0; }
1
#include <bits/stdc++.h> using namespace std; long long p = 31; long long hash1(string s) { long long h = 0; long long pow = p; for (int i = 0; i < s.size(); i++) { if (s[i] == '.') h += 0; else h += (s[i] + 1) * pow; pow *= p; } return h; } int main() { int n, m; long long h = -1; cin >> n >> m; char cells[500][500]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { string s; cin >> s; if (s.empty()) break; long long newH = hash1(s); if (newH != 0 && h == -1) h = newH; if (newH != 0 && newH != h && h != -1) { cout << "NO"; return 0; } } } cout << "YES"; return 0; }
2
#include <bits/stdc++.h> using namespace std; int main(){ int a,b,c; cin>>a>>b>>c; cout<<min(min((a+b),(b+c)),(c+a))<<endl; return 0; }
0
#include <bits/stdc++.h> int a[10005]; int main() { int n, w; scanf("%d%d", &n, &w); for (int i = 0; i < n; i++) scanf("%d", &a[i]); int sum = 0, max1 = 0, min1 = 0; for (int i = 0; i < n; i++) { sum += a[i]; if (sum < min1) min1 = sum; if (sum > max1) max1 = sum; } if (max1 > 0) printf("%d\n", (w + min1) - max1 + 1 > 0 ? (w + min1) - max1 + 1 : 0); else printf("%d\n", w + min1 + 1 > 0 ? w + min1 + 1 : 0); }
5
#include<iostream> #include<cmath> #include<cstdio> using namespace std; int main(){ int n; int x[100]; int y; double sum1=0,sum2=0,sum3=0,sumi=0; cin >> n; for(int i=0; i<n; i++) cin >> x[i]; for(int i=0; i<n; i++){ cin >> y; sum1 += abs(x[i]-y); sum2 += (x[i]-y)*(x[i]-y); sum3 += abs( (x[i]-y)*(x[i]-y)*(x[i]-y) ); if(sumi < abs(x[i]-y)) sumi = abs(x[i]-y); } printf("%.8lf\n%.8lf\n",sum1, sqrt(sum2) ); printf("%.8lf\n%.8lf\n", pow(sum3,1.0/3.0), sumi); return 0; }
0
#include <bits/stdc++.h> #pragma GCC optimize("-O3") using namespace std; const double PI = acos(-1); long long gcd() { return 0ll; } template <typename T, typename... Args> T gcd(T a, Args... args) { return __gcd(a, (__typeof(a))gcd(args...)); } long long a[200005], bit[200005], bit1[200005]; long long b[200005], c[200005], st[200005], en[200005], tim; vector<long long> G[200005]; pair<long long, long long> dp[200005]; void dfs(long long u = 0, long long p = -1) { if (b[u] != c[u]) { dp[u].first = (b[u] == 0); dp[u].second = (b[u] == 1); } st[u] = tim++; for (auto it : G[u]) if (it != p) { dfs(it, u); dp[u].first += dp[it].first; dp[u].second += dp[it].second; } en[u] = tim - 1; } void upd(long long i, long long val) { for (i++; i < 200005; i += i & -i) bit[i] += val; } void upd1(long long i, long long val) { for (i++; i < 200005; i += i & -i) bit1[i] += val; } long long que(long long i) { long long res = 0; for (i++; i > 0; i -= i & -i) res += bit[i]; return res; } long long que1(long long i) { long long res = 0; for (i++; i > 0; i -= i & -i) res += bit1[i]; return res; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); { long long n; long long x, y, t; cin >> n; for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); i += 1 - 2 * ((0) > (n))) cin >> a[i] >> b[i] >> c[i]; for (__typeof(n) i = (1) - ((1) > (n)); i != (n) - ((1) > (n)); i += 1 - 2 * ((1) > (n))) { cin >> x >> y; G[x - 1].emplace_back(y - 1); G[y - 1].emplace_back(x - 1); } dfs(); if (dp[0].first != dp[0].second) { cout << "-1\n"; return 0; } vector<pair<long long, long long> > v(n); for (__typeof(n) i = (0) - ((0) > (n)); i != (n) - ((0) > (n)); i += 1 - 2 * ((0) > (n))) v[i] = {a[i], i}; sort((v).begin(), (v).end()); long long ans = 0; for (auto it : v) { if (que1(st[it.second])) continue; tie(x, y) = dp[it.second]; t = que(en[it.second]) - que(st[it.second]); x -= t; y -= t; if (x > y) swap(x, y); ans += 2 * x * it.first; upd(st[it.second], x); upd1(st[it.second], 1); upd1(en[it.second] + 1, -1); } cout << ans << "\n"; } return 0; }
5
#include <bits/stdc++.h> using namespace std; const int mod = 1e9 + 7; const int N = 1e6 + 10; const long long INF = 1e18; const long double EPS = 1e-12; int n, m, a[100005], b[100005]; int freq[100005]; void init() { cin >> n; for (int i = 0; i < (int)n; i++) { cin >> a[i]; } int l = 0, r = n - 1; while (l <= r && a[l] == a[r]) m++, l++, r--; if (l > r) { cout << (long long)(n) * (n + 1) / 2LL << endl; exit(0); } n = 0; for (int i = (int)l; i < (int)r + 1; i++) { b[n++] = a[i]; } } int main() { init(); long long ans = 0; auto go = [&](int l) { memset(freq, 0, sizeof(freq)); for (int i = 0; i < (int)l + 1; i++) freq[b[i]]++; for (int i = 0; i < n - 1 - i; i++) { if (i <= l && n - 1 - i > l) { if (not freq[b[n - 1 - i]]) return false; freq[b[n - 1 - i]]--; } else if (i > l) { if (b[i] != b[n - 1 - i]) return false; } } int tmp = 0; for (int i = (int)1; i < (int)n + 1; i++) { tmp += (freq[i] % 2); } return tmp <= 1; }; auto binSearch = [&]() { int lo = 0, hi = n; while (lo < hi) { int mid = (lo + hi) / 2; if (go(mid)) hi = mid; else lo = mid + 1; } ans += n - lo; }; binSearch(); reverse(b, b + n); binSearch(); if (ans == 0) cout << "0" << endl; else cout << (long long)(m + 1) * (ans + m - 1) << endl; return 0; }
3
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(0); cin.tie(0); long long int t; cin >> t; while (t--) { long long int n, k, x; cin >> n >> k; vector<long long int> a, b; for (int i = 0; i < n; i++) { cin >> x; a.push_back(x); } for (int i = 0; i < n; i++) { cin >> x; b.push_back(x); } sort(a.begin(), a.end()); sort(b.begin(), b.end()); long long int bi = n - 1; long long int ai = 0; while (k--) { if (b[bi] > a[ai]) { swap(b[bi], a[ai]); } else break; --bi; ++ai; } long long int tot = 0; for (int i = 0; i < n; i++) tot += a[i]; cout << tot << endl; } }
2
#include<bits/stdc++.h> #define mod 1000000007 using namespace std; int n; char s[3005]; int dp[3005][3005]; int si[3005][3005]; int main(){ cin>>n; cin>>s+1; dp[1][1]=1; for(int i=1;i<=n;i++) si[1][i]=1; for(int i=2;i<=n;i++){ for(int j=1;j<=i;j++){ if(s[i-1]=='>') dp[i][j]=(si[i-1][i-1]-si[i-1][j-1]+mod)%mod; else dp[i][j]=si[i-1][j-1]; } for(int j=1;j<=i;j++){ si[i][j]=si[i][j-1]+dp[i][j]; si[i][j]%=mod; } } cout<<si[n][n]<<endl; return 0; }
0
#include <bits/stdc++.h> using namespace std; int a1, a2, a3, n, s, i, j, k, x, ans, ans1, ans2; int main() { cin >> n >> s; for (i = 1; i <= n; i++) { cin >> x; if (x == 3) a1++; if (x == 4) a2++; if (x == 5) a3++; } ans = 1e9; for (i = s / n; i >= 0; i--) for (j = (s - i * a1) / (a2 + a3); j >= i; j--) { k = (s - i * a1 - j * a2) / a3; if (k * a3 - j * a2 > ans) break; if (i * a1 + j * a2 + k * a3 == s && ans > abs(a1 * i - a2 * j) + abs(a2 * j - a3 * k)) ans = abs(a1 * i - a2 * j) + abs(a2 * j - a3 * k), ans1 = i, ans2 = j; } if (ans == 1e9) cout << -1; else cout << ans1 << ' ' << ans2 << ' ' << ((s - ans1 * a1 - ans2 * a2) / a3); }
5
#include <bits/stdc++.h> using namespace std; const double INF = (int)1e9; const int T = (int)1e6; const int N = 111; int n; double p[N]; double q[N]; int r[N]; double ans = 1; int a[N]; int choose() { int v = -1; double val = INF; for (int i = 0; i < n; i++) { double w = (1 - q[i]) / (1 - q[i] * (1 - p[i])); if (w < val) { val = w; v = i; } } return v; } void addOne() { int v = choose(); a[v]++; q[v] *= (1 - p[v]); double Q = 1; for (int i = 0; i < n; i++) Q *= 1 - q[i]; ans += 1 - Q; return; } int main() { scanf("%d", &n); for (int i = 0; i < n; i++) { scanf("%d", &r[i]); p[i] = r[i] / 100.0; q[i] = 1; } for (int it = 0; it < T; it++) addOne(); printf("%.10lf\n", ans); return 0; }
4
#include <bits/stdc++.h> using namespace std; long long n; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; if ((n % 2) == 0) cout << n / 2; else cout << n / 2 + 1; return 0; }
1
#include <bits/stdc++.h> using std::max; const int maxn = 20000 + 11, maxk = 61, maxp = 111; int f1[maxn], f2[maxn], sum[maxn], a[maxn]; int maxf[maxp]; int main() { int n, k, p; scanf("%d%d%d", &n, &k, &p); for (int i = 1; i <= n; ++i) scanf("%d", &a[i]); sum[0] = 0; for (int i = 1; i <= n; ++i) sum[i] = (sum[i - 1] + a[i]) % p; int *pre = f1, *cur = f2; for (int i = 1; i <= n; ++i) pre[i] = sum[i]; for (int i = 2; i <= k; ++i) { for (int j = 0; j < p; ++j) maxf[j] = -1; maxf[sum[i - 1]] = pre[i - 1]; for (int j = i; j <= n; ++j) { cur[j] = 0; for (register int v = 0; v < p; ++v) if (maxf[v] >= 0) cur[j] = max(cur[j], maxf[v] + ((sum[j] - v) % p + p) % p); maxf[sum[j]] = max(maxf[sum[j]], pre[j]); } std::swap(pre, cur); } printf("%d", pre[n]); }
3
#include <bits/stdc++.h> using namespace std; int main() { long long int n, sum = 0; cin >> n; long long int i, a[n]; for (i = 0; i < n; i++) { cin >> a[i]; sum += a[i] + ((a[i] - 1) * i); } cout << sum; }
2
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++){ cin >> a[i]; } int q; cin >> q; for (int i = 0; i < q; i++){ int b, e, t; cin >> b >> e >> t; for (int j = b; j < e; j++){ swap(a[j], a[t + j - b]); } } for (int i = 0; i < n; i++){ cout << a[i]; if (i < n - 1){ cout << ' '; } } cout << endl; }
0
#include <bits/stdc++.h> using namespace std; int isPrime(long int k) { if (k == 1) return 0; if (k % 2 != 0) { for (int i = 3; i <= sqrt(k); i += 2) { if (k % i == 0) return 0; } return 1; } else return 0; } int pFact(long int k) { if (k % 2 == 0) return 2; else if (k % 3 == 0) return 3; else { for (int i = 5; i % 3 != 0 && i <= sqrt(k); i += 2) { if (k % i == 0) return i; } return k; } } int main() { long int n; cin >> n; long long int j = n; if (isPrime(n)) cout << n + 1; else { while (n > 1) { int k = pFact(n); j += n / k; n /= k; } cout << j; } return 0; }
2