if (pA == 1 || pB == 1 || pC == 1) { if (pA == 1) { a = "1"; b = "1" + string(pB - 1, '0'); c = "0" + string(pC - 1, '1'); } elseif (pB == 1) { a = "0" + string(pA - 1, '1'); b = "1"; c = "1" + string(pC - 1, '0'); } else { a = "1" + string(pA - 1, '0'); b = "0" + string(pB - 1, '1'); c = "1"; } } else { int p = gcd(pA, pB); int q = gcd(pA, pC); int r = gcd(pB, pC); for (int i = 0; i < pA; i++) { int x = (i % p == 0) ^ (i % q == 0); a += x + '0'; } for (int i = 0; i < pB; i++) { int x = (i % p == 0) ^ (i % r == 0); b += x + '0'; } for (int i = 0; i < pC; i++) { int x = (i % q == 0) ^ (i % r == 0); c += x + '0'; } }
cout << "YES" << endl; for (auto& x : a) cout << x << string(d - 1, '0'); cout << endl; for (auto& x : b) cout << x << string(d - 1, '0'); cout << endl; for (auto& x : c) cout << x << string(d - 1, '0'); cout << endl; } } return0; }
while (!Q.empty()) { auto [x, y] = Q.front(); Q.pop(); bool op = y & 1; for (int i = 0; i < 3; i++) { int nx = x + dx[op][i], ny = y + dy[op][i]; if (mp[nx][ny] == 0) continue; if (dis[nx][ny] != -1) continue; if (mp[nx][ny] == to[op][mp[x][y]][i]) { dis[nx][ny] = dis[x][y] + 1; Q.push({ nx, ny }); } } }
int x, y; cin >> x >> y; cout << dis[x][y] << endl;
intmain(){ int A, B, X, Y; cin >> A >> B >> X >> Y; A = max(1, A); B = max(1, B); // 特判范围是一条线或一个点的情形 避免 d=0 除零错误 int d1 = gcd(A, X), d2 = gcd(B, Y); if (!check(A / d1) || !check(B / d2)) { cout << -1 << endl; return0; }
stack<array<int, 4>> res; while ((X != A && X != 0) || (Y != B && Y != 0)) { // 没有到达四个角 int cx = (X * 2 < A) ? 0 : A; int cy = (Y * 2 < B) ? 0 : B; X = 2 * X - cx; Y = 2 * Y - cy; res.push({ X, Y, cx, cy }); }
vector<ll> a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; }
ll now = 0; vector<ll> a1(n), a2(n); for (int i = 0; i < n; i++) { now = a1[i] = max(now, a[i]); now += b[i]; }
bool flag = 1; // all diff for (int i = 0; i < n; i++) { now = a2[i] = max(now, a[i] + k); now += b[i]; if (a2[i] == a1[i] + k) { flag = 0; } } ll delta = a2.back() - a1.back(); while (q--) { int x, i; cin >> x >> i; i--;
ll res; if (x == 1) { res = a1[i] + b[i] - 1; } else { res = a2[i] + b[i] - 1; if (flag) { // all diff res += (x - 2ll) * delta; } else { res += (x - 2ll) * k; } }
ll d = res / k, h = res % k; if (h == 0) { h = k; d--; } cout << (++d) << " " << h << endl; }
#include<bits/stdc++.h> usingnamespace std; using ll = longlong; #define double long double
doubleqpow(double a, ll b){ double res = 1; while (b) { if (b & 1) { res *= a; } b >>= 1; a = a * a; } return res; } intmain(){ ios::sync_with_stdio(false); cin.tie(0), cout.tie(0);
int n, k; cin >> n >> k; double p, q; cin >> p; q = 1 - p; cout << fixed << setprecision(12); cerr << fixed << setprecision(12);
double ans; if (k > 165) { ans = n / (1 - p); cout << ans << endl; return; }
vector<double> A(N); for (int i = 0; i < N; i++) { cin >> A[i]; }
auto calc = [&](double c) { vector<double> p(N); for (int i = 0; i < N; i++) { p[i] = A[i] / (A[i] + c); } return p; };
double l = 0, r = 1e15; while (r - l > eps) { double mid = (l + r) / 2; auto p = calc(mid); if (accumulate(p.begin(), p.end(), 0.0l) < K) { r = mid; } else { l = mid; } }
auto p = calc(l); for (int i = 0; i < N; i++) { cout << p[i] << endl; }
return0; }
有三种修改方案:
二分一个在 $[0,1]$ 之间的数,对于本题,可以二分 $p_{1}$。
判断 $l$ 和 $r$ 的相对差:即判断 $\dfrac{r-l}{l}$ 是否在误差范围内。
1 2 3 4 5 6 7 8 9 10
double l = 0, r = 1e15; while ((r - l) / l > eps) { // 修改这一行 double mid = (l + r) / 2; auto p = calc(mid); if (accumulate(p.begin(), p.end(), 0.0l) < K) { r = mid; } else { l = mid; } }
控制二分次数:$V \cdot \epsilon^{-1}=10^{15}\cdot 10^{18}=10^{23}$,$\log V + \log \epsilon^{-1}=76$,因此二分 $100$ 次足够。
1 2 3 4 5
double l = 0, r = 1e15; for (int _ = 0; _ < 100; _++) { double mid = (l + r) / 2;