solution
stringlengths
11
983k
difficulty
int64
0
21
language
stringclasses
2 values
#include <bits/stdc++.h> using namespace std; int n, k, mod, cnt[100005], s[100005], inv[100005], L = 100005, R, ans; void divide(int l, int r, int h) { if (h <= 1 || l == r) { cnt[r - l + 1]++, L = min(L, r - l + 1), R = max(R, r - l + 1); return; } int mid = (l + r) >> 1; divide(l, mid, h - 1), divide(mid + 1, r, h - 1); } int calc(int x, int y) { int ret = 1ll * x * y % mod * inv[2] % mod; for (int i = 1; i <= x; i++) ret = (ret - 1ll * (s[i + y] - s[i])) % mod; return (ret + mod) % mod; } int main() { scanf("%d%d%d", &n, &k, &mod); divide(1, n, k); inv[0] = inv[1] = s[1] = 1; for (int i = 2; i <= max(4, n); i++) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod, s[i] = (s[i - 1] + inv[i]) % mod; for (int i = L; i <= R; i++) { ans = (ans + 1ll * i * (i - 1) % mod * inv[4] % mod * cnt[i]) % mod; ans = (ans + 1ll * cnt[i] * (cnt[i] - 1) % mod * inv[2] % mod * calc(i, i)) % mod; for (int j = i + 1; j <= R; j++) ans = (ans + 1ll * cnt[i] * cnt[j] % mod * calc(i, j)) % mod; } printf("%d\n", ans); }
13
CPP
#include <bits/stdc++.h> using namespace std; int MOD; int inv(long long int n) { long long int r = 1; int e = MOD - 2; while (e > 0) { if (e & 1) r *= n, r %= MOD; e >>= 1; n *= n, n %= MOD; } return r; } map<int, int> occ; vector<pair<int, int> > v; int findSizes(int s, int h) { if ((h == 1) || (s == 1)) occ[s]++; else findSizes(s / 2, h - 1), findSizes((s + 1) / 2, h - 1); return 0; } int main() { int n, k; cin >> n >> k >> MOD; findSizes(n, k); for (auto it = occ.begin(); it != occ.end(); it++) v.push_back(*it); int i, j; long long int ans = 0; for (i = 0; i < v.size(); i++) { long long int x = ((long long int)v[i].first * (v[i].first - 1)) % MOD; x *= inv(4), x %= MOD; ans += v[i].second * x, ans %= MOD; } for (i = 0; i < v.size(); i++) { for (j = i; j < v.size(); j++) { long long int o = ((long long int)v[i].second * v[j].second) % MOD; if (i == j) o = ((long long int)v[i].second * (v[i].second - 1) / 2) % MOD; for (k = 2; k <= v[i].first + v[j].first; k++) { long long int c = (inv(2) - inv(k) + MOD) % MOD; c *= o, c %= MOD; ans += c * (min(k - 1, v[i].first) - max(k - v[j].first, 1) + 1), ans %= MOD; } } } cout << ans << endl; return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; const int mxN = 1e5; int n, k, q, l1, l2, c1, c2; long long iv[mxN + 1], pi[mxN + 1], ans; void ms(int l = 0, int r = n - 1, int h = k) { if (h < 2 || l == r) { if (!l1 || r - l + 1 == l1) { l1 = r - l + 1; ++c1; } else { l2 = r - l + 1; ++c2; } return; } int m = (l + r) / 2; ms(l, m, h - 1); ms(m + 1, r, h - 1); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> k >> q; iv[1] = 1; for (int i = 2; i <= n; ++i) iv[i] = (q - q / i) * iv[q % i] % q; for (int i = 1; i <= n; ++i) pi[i] = (pi[i - 1] + iv[i]) % q; ms(); if (c1 > 1) for (int i = 1; i <= l1; ++i) ans += (pi[l1 + i] - pi[i] + q) * c1 % q * (c1 - 1) % q; if (c2 > 1) for (int i = 1; i <= l2; ++i) ans += (pi[l2 + i] - pi[i] + q) * c2 % q * (c2 - 1) % q; for (int i = 1; i <= l1; ++i) ans += (pi[l2 + i] - pi[i] + q) * c1 % q * c2 % q * 2; cout << (iv[2] * n % q * (n - 1) % q - ans % q + q) * iv[2] % q; }
13
CPP
#include <bits/stdc++.h> using namespace std; template <class T> inline void in(T &x) { x = 0; char c = getchar(); bool f = 0; while (!isdigit(c)) f |= (c == '-'), c = getchar(); while (isdigit(c)) x = x * 10 + (c ^ '0'), c = getchar(); f ? x = -x : 0; } template <class T> inline void out(T x, const char c = '\n') { static short st[30]; short m = 0; if (x < 0) putchar('-'), x = -x; do st[++m] = x % 10, x /= 10; while (x); while (m) putchar(st[m--] | '0'); putchar(c); } template <class T> inline void err(const T &x, const char c = '\n') { cerr << x << c; } template <class T, class... Args> inline void in(T &x, Args &...args) { in(x); in(args...); } template <class T, class... Args> inline void out(const T &x, const Args &...args) { out(x, ' '); out(args...); } template <class T, class... Args> inline void err(const T &x, const Args &...args) { err(x, ' '); err(args...); } template <class T> inline void prt(T a[], int n) { for (register int i = 0; i < n; ++i) out(a[i], i == n - 1 ? '\n' : ' '); } template <class T> inline void clr(T a[], int n) { memset(a, 0, sizeof(T) * n); } template <class T> inline void clr(T *a, T *b) { memset(a, 0, sizeof(T) * (b - a)); } template <class T> inline bool ckmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <class T> inline bool ckmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } namespace MOD_CALC { int md; inline int add(const int a, const int b) { return a + b >= md ? a + b - md : a + b; } inline int sub(const int a, const int b) { return a - b < 0 ? a - b + md : a - b; } inline int mul(const int a, const int b) { return (long long)a * b % md; } inline void inc(int &a, const int b) { (a += b) >= md ? a -= md : 0; } inline void dec(int &a, const int b) { (a -= b) < 0 ? a += md : 0; } inline int qpow(int a, int b) { int r = 1; for (; b; b >>= 1, a = mul(a, a)) if (b & 1) r = mul(r, a); return r; } inline int qpow(int a, int b, const int p) { int r = 1; for (; b; b >>= 1, a = (long long)a * a % p) if (b & 1) r = (long long)r * a % p; return r; } inline int mdinv(const int a) { return qpow(a, md - 2); } template <class... Args> inline int add(const int a, const int b, const Args &...args) { return add(add(a, b), args...); } template <class... Args> inline int mul(const int a, const int b, const Args &...args) { return mul(mul(a, b), args...); } } // namespace MOD_CALC using namespace MOD_CALC; namespace i207M { int iv[200005]; int inv2; void prework() { iv[1] = 1; for (register int i = 2; i < 200005; ++i) iv[i] = mul(md - md / i, iv[md % i]); for (register int i = 2; i < 200005; ++i) inc(iv[i], iv[i - 1]); } map<int, int> cnt; void divide(int l, int r, int h) { if (h <= 1 || l == r) { ++cnt[r - l + 1]; return; } int mid((l + r) / 2); divide(l, mid, h - 1); divide(mid + 1, r, h - 1); } int calc(int a, int b) { int res = 0; for (register int i = 1; i <= a; ++i) dec(res, sub(iv[i + b], iv[i])); inc(res, mul(inv2, mul(a, b))); return res; } void solve() { int ans = 0; for (const auto &it : cnt) { inc(ans, mul(it.first, it.first - 1, inv2, inv2, it.second)); inc(ans, mul((long long)it.second * (it.second - 1) / 2 % md, calc(it.first, it.first))); } for (const auto &it : cnt) for (const auto &is : cnt) { int x = it.first, y = is.first; if (x >= y) continue; inc(ans, mul(it.second, is.second, calc(x, y))); } out(ans); } int n, k; signed main() { in(n, k, md); inv2 = mdinv(2); prework(); divide(1, n, k); solve(); return 0; } } // namespace i207M signed main() { i207M::main(); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vs = vector<string>; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int MOD; struct modnum { int v; modnum() : v(0) {} modnum(ll _v) : v(_v % MOD) { if (v < 0) v += MOD; } explicit operator int() const { return v; } friend ostream& operator<<(ostream& o, const modnum& n) { return o << n.v; } friend istream& operator<<(istream& i, modnum& n) { ll v; i >> v; n = modnum(v); return i; } friend bool operator==(const modnum& a, const modnum& b) { return a.v == b.v; } friend bool operator!=(const modnum& a, const modnum& b) { return a.v != b.v; } modnum& operator+=(const modnum& o) { v += o.v; if (v >= MOD) v -= MOD; return *this; } modnum& operator-=(const modnum& o) { v -= o.v; if (v < 0) v += MOD; return *this; } modnum& operator*=(const modnum& o) { v = int(ll(v) * ll(o.v) % MOD); return *this; } modnum operator-() { modnum res; if (v) res.v = MOD - v; return res; } friend modnum operator+(const modnum& a, const modnum& b) { return modnum(a) += b; } friend modnum operator-(const modnum& a, const modnum& b) { return modnum(a) -= b; } friend modnum operator*(const modnum& a, const modnum& b) { return modnum(a) *= b; } modnum pow(int e) const { if (e == 0) return 1; if (e & 1) return *this * this->pow(e - 1); return (*this * *this).pow(e / 2); } modnum& operator/=(const modnum& o) { return (*this) *= o.pow(MOD - 2); } friend modnum operator/(const modnum& a, const modnum& b) { return modnum(a) /= modnum(b); } }; using mn = modnum; int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); int N, K; cin >> N >> K >> MOD; vector<mn> recip(N + 1), pref(N + 1); for (int i = 1; i <= N; i++) { recip[i] = mn(1) / i; pref[i] = pref[i - 1] + recip[i]; } auto comb = [&](int a, int b) { mn res = 0; for (int i = 1; i <= a; i++) { res += mn(b) / 2 - (pref[i + b] - pref[i]); } return res; }; int szA = N, szB = N, ctA = 1, ctB = 0; for (int i = 0; i < K - 1; i++) { int nszA = szA / 2, nszB = (szB + 1) / 2; int nctA = 0, nctB = 0; if (szA & 1) { nctA += ctA; nctB += ctA; } else nctA += 2 * ctA; if (szB & 1) { nctA += ctB; nctB += ctB; } else nctB += 2 * ctB; szA = nszA, szB = nszB; ctA = nctA, ctB = nctB; } mn ans = 0; ans += mn(ctA) * szA * (szA - 1) / 4; if (ctA > 1) ans += mn(ctA) * (ctA - 1) / 2 * comb(szA, szA); ans += mn(ctB) * szB * (szB - 1) / 4; if (ctB > 1) ans += mn(ctB) * (ctB - 1) / 2 * comb(szB, szB); if (ctA > 0 && ctB > 0) ans += mn(ctA) * ctB * comb(szA, szB); cout << ans << endl; return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; int n, k, mo; int n1, n2, len, ans = 0; int Sum(int x, int y) { x += y; return (x >= mo) ? x - mo : x; } int Sub(int x, int y) { x -= y; return (x < 0) ? x + mo : x; } int Mul(int x, int y) { return (long long)x * y % mo; } int Pow(int x, int y = mo - 2) { int z = 1; while (y) { if (y & 1) z = Mul(z, x); y >>= 1; x = Mul(x, x); } return z; } int C(int x) { return Mul(x, Mul(x - 1, (mo + 1) / 2)); } namespace task1 { int Calc(int x) { return Mul((mo + 1) / 2, C(x)); } } // namespace task1 namespace task2 { int Calc(int x, int y) { int ans = Mul(x, y); for (int i = 2; i <= x + y; i++) { int l = max(1, i - y); int r = min(x, i - 1); if (l <= r) ans = Sub(ans, Mul(Pow(i), (r - l + 1) * 2)); } return Mul(ans, (mo + 1) / 2); } } // namespace task2 int main() { scanf("%d%d%d", &n, &k, &mo); if (k > 30) { len = 0; n2 = n; n1 = 0; } else { len = n / (1 << (k - 1)); n2 = n - len * (1 << (k - 1)); n1 = (1 << (k - 1)) - n2; } ans = Sum(ans, Mul(n1, task1::Calc(len))); ans = Sum(ans, Mul(n2, task1::Calc(len + 1))); ans = Sum(ans, Mul(C(n1), task2::Calc(len, len))); ans = Sum(ans, Mul(C(n2), task2::Calc(len + 1, len + 1))); ans = Sum(ans, Mul(Mul(n1, n2), task2::Calc(len, len + 1))); printf("%d\n", ans); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int n, k, q, inv[N], sum[N]; map<int, int> cnt; void divide(int l, int r, int k) { if (l == r || k <= 1) { ++cnt[r - l + 1]; return; } int mid = (l + r) >> 1; divide(l, mid, k - 1); divide(mid + 1, r, k - 1); } int ans; int calc(int x, int y) { int res = 1ll * x * y % q * inv[2] % q; for (int i = 1; i <= x; ++i) res = (res - sum[i + y] + sum[i]) % q; return res; } int main() { scanf("%d %d %d", &n, &k, &q); inv[1] = sum[1] = 1; for (int i = 2; i < N; ++i) inv[i] = 1ll * inv[q % i] * (q - q / i) % q, sum[i] = (sum[i - 1] + inv[i]) % q; divide(1, n, k); for (auto i : cnt) { int l = i.first, c = i.second; ans += 1ll * l * (l - 1) % q * inv[4] % q * c % q; ans %= q; ans += 1ll * c * (c - 1) % q * inv[2] % q * calc(l, l) % q; ans %= q; } for (auto i : cnt) for (auto j : cnt) { int x = i.first, y = j.first, nx = i.second, ny = j.second; if (x >= y) continue; ans += 1ll * nx * ny % q * calc(x, y) % q; ans %= q; } printf("%d\n", (ans + q) % q); return 0; }
13
CPP
#include <bits/stdc++.h> #pragma GCC optimize("O2,Ofast,inline,unroll-all-loops,-ffast-math") #pragma GCC target("popcnt") using namespace std; int n, k; long long inv[100010], sum[100010], p, ans = 0; map<int, int> M; template <class T> void read(T &x) { char ch = x = 0; bool fl = false; while (!isdigit(ch)) fl |= ch == '-', ch = getchar(); while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar(); x = fl ? -x : x; } long long calc(int l, int r) { return (sum[r] - sum[l - 1] + p) % p; } long long Calc(int A, int B) { long long ret = 1LL * A * B % p * inv[2] % p; for (int i = 1; i <= A; i++) { ret = (ret - calc(i + 1, i + B) + p) % p; } return ret; } void solve(int l, int r, int h) { if (h == 1 || l == r) return ans = (ans + 1LL * (r - l + 1) * (r - l) % p * inv[4]) % p, M[r - l + 1]++, void(); int mid = l + r >> 1; solve(l, mid, h - 1), solve(mid + 1, r, h - 1); } void init() { inv[1] = 1; for (int i = 2; i < 100010; i++) { inv[i] = (p - p / i) * inv[p % i] % p; } for (int i = 1; i < 100010; i++) { sum[i] = (sum[i - 1] + inv[i]) % p; } } int main() { read(n), read(k), read(p), init(); solve(1, n, k); for (auto &x : M) { ans = (ans + Calc(x.first, x.first) * x.second % p * (x.second - 1) % p * inv[2]) % p; for (auto &y : M) { if (x.first < y.first) ans = (ans + Calc(x.first, y.first) * x.second % p * y.second) % p; } } printf("%lld\n", ans); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vvi = vector<vi>; using vll = vector<ll>; using vvll = vector<vll>; using vb = vector<bool>; using vs = vector<string>; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int MOD; struct modnum { int v; modnum() : v(0) {} modnum(ll _v) : v(_v % MOD) { if (v < 0) v += MOD; } explicit operator int() const { return v; } friend ostream& operator<<(ostream& o, const modnum& n) { return o << n.v; } friend istream& operator<<(istream& i, modnum& n) { ll v; i >> v; n = modnum(v); return i; } friend bool operator==(const modnum& a, const modnum& b) { return a.v == b.v; } friend bool operator!=(const modnum& a, const modnum& b) { return a.v != b.v; } modnum& operator+=(const modnum& o) { v += o.v; if (v >= MOD) v -= MOD; return *this; } modnum& operator-=(const modnum& o) { v -= o.v; if (v < 0) v += MOD; return *this; } modnum& operator*=(const modnum& o) { v = int(ll(v) * ll(o.v) % MOD); return *this; } modnum operator-() { modnum res; if (v) res.v = MOD - v; return res; } friend modnum operator+(const modnum& a, const modnum& b) { return modnum(a) += b; } friend modnum operator-(const modnum& a, const modnum& b) { return modnum(a) -= b; } friend modnum operator*(const modnum& a, const modnum& b) { return modnum(a) *= b; } modnum pow(int e) const { if (e == 0) return 1; if (e & 1) return *this * this->pow(e - 1); return (*this * *this).pow(e / 2); } modnum& operator/=(const modnum& o) { return (*this) *= o.pow(MOD - 2); } friend modnum operator/(const modnum& a, const modnum& b) { return modnum(a) /= modnum(b); } }; using mn = modnum; int main() { ios_base::sync_with_stdio(false); cin.tie(0), cout.tie(0); int N, K; cin >> N >> K >> MOD; if (K >= 20 || (1 << (K - 1)) >= N) { cout << 0 << endl; return 0; } int ct = 1 << (K - 1); int ctB = N % ct; int ctA = ct - ctB; int szA = N / ct; int szB = szA + 1; vector<mn> recip(N + 1), pref(N + 1); for (int i = 1; i <= N; i++) { recip[i] = mn(1) / i; pref[i] = pref[i - 1] + recip[i]; } auto comb = [&](int a, int b) { mn res = 0; for (int i = 1; i <= a; i++) { res += mn(b) / 2 - (pref[i + b] - pref[i]); } return res; }; mn ans = 0; ans += mn(ctA) * szA * (szA - 1) / 4; if (ctA > 1) ans += mn(ctA) * (ctA - 1) / 2 * comb(szA, szA); ans += mn(ctB) * szB * (szB - 1) / 4; if (ctB > 1) ans += mn(ctB) * (ctB - 1) / 2 * comb(szB, szB); if (ctA > 0 && ctB > 0) ans += mn(ctA) * ctB * comb(szA, szB); cout << ans << endl; return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; inline void proc_status() { ifstream t("/proc/self/status"); cerr << string(istreambuf_iterator<char>(t), istreambuf_iterator<char>()) << endl; } template <typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } template <typename T> inline T read() { register T sum(0), fg(1); register char ch(getchar()); for (; !isdigit(ch); ch = getchar()) if (ch == '-') fg = -1; for (; isdigit(ch); ch = getchar()) sum = sum * 10 - '0' + ch; return sum * fg; } const int MAXN = (int)1e5; int MOD; inline int fpm(int x, int y) { int res = 1; for (; y; y >>= 1, x = (long long)x * x % MOD) if (y & 1) res = (long long)res * x % MOD; return res; } int n, k; inline void input() { n = read<int>(), k = read<int>(), MOD = read<int>(); } map<int, int> cnt; inline void merge_sort(int l, int r, int dep) { if (l == r || dep == 1) { ++cnt[r - l + 1]; return; } int mid = (l + r) / 2; merge_sort(l, mid, dep - 1); merge_sort(mid + 1, r, dep - 1); } int pre[MAXN * 2 + 5]; inline int calc(int p, int q) { int sum = (long long)p * q % MOD * ((MOD + 1) / 2) % MOD; for (int i = 1; i <= p; ++i) (sum -= (pre[i + q] - pre[i]) % MOD) %= MOD; return sum; } inline void solve() { merge_sort(1, n, k); pre[0] = 0; for (int i = 1; i <= 2 * n; ++i) pre[i] = (pre[i - 1] + fpm(i, MOD - 2)) % MOD; int ans = 0; for (auto i : cnt) { (ans += (long long)i.first * (i.first - 1) / 2 % MOD * ((MOD + 1) / 2) % MOD * i.second % MOD) %= MOD; (ans += (long long)calc(i.first, i.first) * (i.second * (i.second - 1) / 2 % MOD) % MOD) %= MOD; } for (auto i : cnt) for (auto j : cnt) if (i.first < j.first) (ans += (long long)calc(i.first, j.first) * i.second % MOD * j.second % MOD) %= MOD; printf("%d\n", (ans + MOD) % MOD); } int main() { input(); solve(); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; map<int, int> st; int n, h, mod, inv[maxn]; int qpow(int a, int b) { int ans = 1; for (; b; b >>= 1, a = 1ll * a * a % mod) if (b & 1) ans = 1ll * ans * a % mod; return ans; } void sol(int l, int r, int h) { if (r - l + 1 <= 1 || h <= 1) { st[r - l + 1]++; } else { int mid = l + r >> 1; sol(l, mid, h - 1); sol(mid + 1, r, h - 1); } } int cal(int x, int y) { int ret = 1ll * x * y % mod * (mod + 1) / 2 % mod; for (int i = 1; i <= x; ++i) ret -= inv[i + y] - inv[i], ret %= mod; return (ret % mod + mod) % mod; } int main() { scanf("%d%d%d", &n, &h, &mod); sol(1, n, h); inv[0] = inv[1] = 1; for (int i = 2; i <= n; ++i) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod; for (int i = 1; i <= n; ++i) inv[i] = (inv[i - 1] + inv[i]) % mod; int ans = 0; for (auto c : st) ans = (ans + 1ll * (c.first) * (c.first - 1) % mod * qpow(4, mod - 2) % mod * c.second + 1ll * (c.second) * (c.second - 1) / 2 % mod * cal(c.first, c.first)) % mod; for (auto c : st) for (auto d : st) if (c.first < d.first) ans = (ans + 1ll * (c.second) * d.second % mod * cal(c.first, d.first)) % mod; printf("%d", ans); }
13
CPP
#include <bits/stdc++.h> int q, I[200010]; long long inv(int a, int p) { return a == 1 ? 1 : (1 + p * (a - inv(p % a, a))) / a % p; } int f(int a, int b) { int s = 0; for (int i = 0; i < a + b - 1; i++) s = (s + ((i + 1 < a ? i + 1 : a) - (i - b + 1 > 0 ? i - b + 1 : 0)) * 1ll * i % q * I[i + 2]) % q; return (q + 1ll) / 2 * s % q; } int main() { int n, k; scanf("%d%d%d", &n, &k, &q); --k; if (k > 18) k = 18; int a = n >> k, b = a + 1, x, y; if (a) x = (b << k) - n, y = (1 << k) - x; else x = 0, y = n; for (int i = *I = 1; i <= b * 2; i++) I[i] = I[i - 1] * 1ll * i % q; long long P = inv(I[b * 2], q); for (int i = b * 2; i; i--) I[i] = I[i - 1] * P % q, P = P * i % q; int s = (x * (x - 1ll) / 2 % q * f(a, a) + 1ll * x * y % q * f(a, b) + y * (y - 1ll) / 2 % q * f(b, b) + a * (a - 1ll) / 2 % q * (q + 1 >> 1) % q * x + b * (b - 1ll) / 2 % q * (q + 1 >> 1) % q * y) % q; printf("%d\n", s); }
13
CPP
#include <bits/stdc++.h> const int N = 2e5 + 5; int n, k, mo, ans, inv[N], i, cnt[N], mn = N; void dfs(int l, int r, int h) { if (l == r || h <= 1) { ++cnt[r - l + 1]; mn = std::min(mn, r - l + 1); return; } int m = l + r >> 1; dfs(l, m, h - 1); dfs(m + 1, r, h - 1); } inline int calc(int x, int y) { int ans = 0; for (i = 2; i <= x + y; ++i) ans = (ans + 1ll * inv[i] * (std::min(i - 1, x) - std::max(i - y, 1) + 1)) % mo; return (1ll * x * y % mo * inv[2] + mo - ans) % mo; } int main() { scanf("%d%d%d", &n, &k, &mo); for (i = 2, inv[1] = 1; i < N; ++i) inv[i] = 1ll * (mo - mo / i) * inv[mo % i] % mo; dfs(1, n, k); ans = (1ll * mn * (mn - 1) % mo * cnt[mn] + 1ll * (mn + 1) * mn % mo * cnt[mn + 1]) % mo * inv[4] % mo; ans = (ans + 1ll * cnt[mn] * (cnt[mn] - 1) / 2 % mo * calc(mn, mn)) % mo; ans = (ans + 1ll * cnt[mn + 1] * (cnt[mn + 1] - 1) / 2 % mo * calc(mn + 1, mn + 1)) % mo; ans = (ans + 1ll * cnt[mn] * cnt[mn + 1] % mo * calc(mn, mn + 1)) % mo; printf("%d\n", ans); return 0; }
13
CPP
#include <bits/stdc++.h> char ch; bool fs; void re(int& x) { while (ch = getchar(), ch < 33) ; if (ch == '-') fs = 1, x = 0; else fs = 0, x = ch - 48; while (ch = getchar(), ch > 33) x = x * 10 + ch - 48; if (fs) x = -x; } using namespace std; int n, k, mod, ans, inv[200001], sum[200001]; map<int, int> s; void divide(int l, int r, int h) { if (h <= 1 || l == r) ++s[r - l + 1]; else divide(l, l + r >> 1, h - 1), divide((l + r >> 1) + 1, r, h - 1); } int calc(int x, int y) { int res = 1ll * inv[2] * x % mod * y % mod; for (int i = 1; i <= x; ++i) res = (res - (sum[i + y] - sum[i])) % mod; return (res + mod) % mod; } int main() { re(n), re(k), re(mod); divide(1, n, k); sum[1] = inv[1] = 1; for (int i = 2; i < 200001; ++i) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod, sum[i] = (sum[i - 1] + inv[i]) % mod; for (auto i : s) { ans = (ans + 1ll * inv[4] * i.first % mod * (i.first - 1) % mod * i.second) % mod; ans = (ans + 1ll * inv[2] * i.second % mod * (i.second - 1) % mod * calc(i.first, i.first)) % mod; } for (auto i : s) for (auto j : s) if (i.first < j.first) ans = (ans + 1ll * i.second * j.second % mod * calc(i.first, j.first)) % mod; printf("%d\n", ans); }
13
CPP
#include <bits/stdc++.h> using namespace std; inline int read() { char c = getchar(); int x = 0; bool f = 0; for (; !isdigit(c); c = getchar()) f ^= !(c ^ 45); for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48); if (f) x = -x; return x; } int mod; struct modint { int x; modint(int o = 0) { x = o; } modint &operator=(int o) { return x = o, *this; } modint &operator+=(modint o) { return x = x + o.x >= mod ? x + o.x - mod : x + o.x, *this; } modint &operator-=(modint o) { return x = x - o.x < 0 ? x - o.x + mod : x - o.x, *this; } modint &operator*=(modint o) { return x = 1ll * x * o.x % mod, *this; } modint &operator^=(int b) { modint a = *this, c = 1; for (; b; b >>= 1, a *= a) if (b & 1) c *= a; return x = c.x, *this; } modint &operator/=(modint o) { return *this *= o ^= mod - 2; } modint &operator+=(int o) { return x = x + o >= mod ? x + o - mod : x + o, *this; } modint &operator-=(int o) { return x = x - o < 0 ? x - o + mod : x - o, *this; } modint &operator*=(int o) { return x = 1ll * x * o % mod, *this; } modint &operator/=(int o) { return *this *= ((modint(o)) ^= mod - 2); } template <class I> friend modint operator+(modint a, I b) { return a += b; } template <class I> friend modint operator-(modint a, I b) { return a -= b; } template <class I> friend modint operator*(modint a, I b) { return a *= b; } template <class I> friend modint operator/(modint a, I b) { return a /= b; } friend modint operator^(modint a, int b) { return a ^= b; } friend bool operator==(modint a, int b) { return a.x == b; } friend bool operator!=(modint a, int b) { return a.x != b; } bool operator!() { return !x; } modint operator-() { return x ? mod - x : 0; } }; inline modint qpow(modint a, int b) { return a ^ b; } int n, k; map<int, int> mp; modint iv[100005], sum[100005]; void div(int l, int r, int k) { if (k <= 1 || l == r) return mp[r - l + 1]++, void(); int mid = l + r >> 1; div(l, mid, k - 1), div(mid + 1, r, k - 1); } modint getprob(int x, int y) { modint res = modint(x) * y; for (register int i = (1); i <= (x); ++i) res -= 2 * (sum[i + y] - sum[i]); return res; } signed main() { n = read(), k = read(), mod = read(); iv[1] = 1; for (register int i = (2); i <= (max(4, n)); ++i) iv[i] = iv[mod % i] * (mod - mod / i); for (register int i = (1); i <= (n); ++i) sum[i] = sum[i - 1] + iv[i]; div(1, n, k); modint res = 0; for (auto it1 : mp) for (auto it2 : mp) { if (it1.first == it2.first) { int cnt = it1.second, len = it1.first; res += iv[2] * len * (len - 1) * cnt; res += modint(cnt) * (cnt - 1) * iv[2] * getprob(len, len); } else if (it1.first < it2.first) { int l1 = it1.first, l2 = it2.first, c1 = it1.second, c2 = it2.second; res += getprob(l1, l2) * c1 * c2; } } res *= iv[2]; cout << res.x; return 0; }
13
CPP
#include <bits/stdc++.h> const int N = 200010; int moder; int inv[N]; int cnt[N]; int pre[N]; int ans; void init() { inv[1] = 1; for (int i = 2; i < N; ++i) { inv[i] = moder - 1ll * (moder / i) * inv[moder % i] % moder; } for (int i = 1; i < N; ++i) { pre[i] = (pre[i - 1] + inv[2] - inv[i]) % moder; pre[i] += pre[i] < 0 ? moder : 0; } } void mergesort(int l, int r, int h) { if (h <= 1) { if (r >= l) { ++cnt[r - l + 1]; ans = (ans + 1ll * (r - l + 1) * (r - l) % moder * inv[4]) % moder; } return; } int mid = (l + r) >> 1; mergesort(l, mid, h - 1); mergesort(mid + 1, r, h - 1); } int main() { int n, k; scanf("%d%d%d", &n, &k, &moder); init(); if (k >= 20) { puts("0"); return 0; } mergesort(1, n, k); for (int i = 0; i <= n; ++i) { if (!cnt[i]) continue; for (int j = i; j <= n; ++j) { if (!cnt[j]) continue; int coe = i == j ? 1ll * cnt[i] * (cnt[i] - 1) / 2 % moder : 1ll * cnt[i] * cnt[j] % moder; for (int k = 1; k <= i; ++k) { ans = (ans + 1ll * coe * (pre[k + j] - pre[k])) % moder; } } } ans += ans < 0 ? moder : 0; printf("%d\n", ans); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; inline long long read() { long long x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = (x << 1) + (x << 3) + c - '0'; c = getchar(); } return x * f; } const int n = read(), m = read(), mod = read(); const int inv2 = (mod + 1) / 2; int ans, inv[200020], s[200020]; vector<pair<int, int> > vec; void Solve(int l, int r, int h) { if (h == 1 || l == r) { int len = (r - l + 1); ans = (ans + 1LL * len * (len - 1) / 2 % mod * inv2 % mod) % mod; for (int i = 0; i < (int)vec.size(); ++i) { if (vec[i].first == len) { ++vec[i].second; return; } } vec.push_back(make_pair(len, 1)); return; } int mid = (l + r) >> 1; Solve(l, mid, h - 1); Solve(mid + 1, r, h - 1); } int main() { inv[1] = s[1] = 1; for (int i = 2; i <= n << 1; ++i) { inv[i] = 1LL * (mod - mod / i) * inv[mod % i] % mod; s[i] = (s[i - 1] + inv[i]) % mod; } Solve(1, n, m); for (int x = 0; x < (int)vec.size(); ++x) { for (int y = x; y < (int)vec.size(); ++y) { int A = vec[x].first, B = vec[y].first, cA = vec[x].second, cB = vec[y].second; int sum = 0; for (int i = 1; i <= A; ++i) { sum = (sum + (s[i + B] - s[i] + mod) % mod) % mod; } int tot = (x == y ? 1LL * cA * (cA - 1) / 2 % mod : 1LL * cA * cB % mod); ans = (ans + 1LL * tot * (1LL * A * B % mod * inv2 % mod - sum)) % mod; } } printf("%d\n", (ans + mod) % mod); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; template <class T> inline void rd(T &x) { char ch; x = 0; bool fl = false; while (!isdigit(ch = getchar())) (ch == '-') && (fl = true); for (x = (ch ^ '0'); isdigit(ch = getchar()); x = x * 10 + (ch ^ '0')) ; (fl == true) && (x = -x); } template <class T> inline void output(T x) { if (x / 10) output(x / 10); putchar(x % 10 + '0'); } template <class T> inline void ot(T x) { if (x < 0) putchar('-'), x = -x; output(x); putchar(' '); } template <class T> inline void prt(T a[], int st, int nd) { for (register int i = st; i <= nd; ++i) ot(a[i]); putchar('\n'); } namespace Modulo { int mod; inline int ad(int x, int y) { return x + y >= mod ? x + y - mod : x + y; } inline int sub(int x, int y) { return ad(x, mod - y); } inline int mul(int x, int y) { return (long long)x * y % mod; } inline void inc(int &x, int y) { x = ad(x, y); } inline void inc2(int &x, int y) { x = mul(x, y); } inline int qm(int x, int y = mod - 2) { int ret = 1; while (y) { if (y & 1) ret = mul(x, ret); x = mul(x, x); y >>= 1; } return ret; } template <class... Args> inline int ad(const int a, const int b, const Args &...args) { return ad(ad(a, b), args...); } template <class... Args> inline int mul(const int a, const int b, const Args &...args) { return mul(mul(a, b), args...); } } // namespace Modulo using namespace Modulo; namespace Miracle { const int N = 1e5 + 5; int n, k; int iv[N], s[N]; int l1, l2, c1, c2; void divi(int l, int r, int d) { if (d == k || l == r) { if (!l1) { l1 = r - l + 1; ++c1; } else if (r - l + 1 == l1) ++c1; else if (!l2) l2 = r - l + 1, ++c2; else ++c2; return; } int mid = (l + r) >> 1; divi(l, mid, d + 1); divi(mid + 1, r, d + 1); } int calc(int l1, int l2) { if (!l1 || !l2) return 0; int ret = mul(l1, l2, qm(2)); for (register int i = 1; i <= l1; ++i) { ret = sub(ret, sub(s[i + l2], s[i])); } return ret; } int main() { rd(n); rd(k); rd(mod); iv[1] = 1; for (register int i = 2; i <= n; ++i) { iv[i] = mul(mod - mod / i, iv[mod % i]); } for (register int i = 1; i <= n; ++i) s[i] = ad(s[i - 1], iv[i]); divi(1, n, 1); int ans = ad(mul(c1, l1, (l1 - 1), qm(4)), mul(c2, l2, (l2 - 1), qm(4))); inc(ans, mul(c1, c1 - 1, qm(2), calc(l1, l1))); inc(ans, mul(c2, c2 - 1, qm(2), calc(l2, l2))); inc(ans, mul(c1, c2, calc(l1, l2))); ot(ans); return 0; } } // namespace Miracle signed main() { Miracle::main(); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; int read() { int X = 0, w = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') w = -1; c = getchar(); } while (c >= '0' && c <= '9') X = X * 10 + c - '0', c = getchar(); return X * w; } const int N = 100000 + 10; int mod, inv2, inv4; int qpow(int a, int b) { int c = 1; for (; b; b >>= 1, a = 1ll * a * a % mod) if (b & 1) c = 1ll * c * a % mod; return c; } int n, k; int inv[N], sum[N], o[N]; vector<int> l; void divide(int l, int r, int d) { if (d == k || l == r) { ++o[r - l + 1]; return; } int mid = (l + r) >> 1; divide(l, mid, d + 1), divide(mid + 1, r, d + 1); } int calc(int x, int y) { int res = 1ll * x * y % mod * inv2 % mod; for (int i = 1; i <= x; ++i) res = (res - (sum[i + y] - sum[i] + mod) % mod + mod) % mod; return res; } int main() { n = read(), k = read(), mod = read(), inv2 = qpow(2, mod - 2), inv4 = qpow(4, mod - 2); inv[1] = 1; for (int i = 2; i <= n; ++i) inv[i] = mod - 1ll * inv[mod % i] * (mod / i) % mod; for (int i = 1; i <= n; ++i) sum[i] = (sum[i - 1] + inv[i]) % mod; divide(1, n, 1); for (int i = 1; i <= n; ++i) if (o[i]) l.emplace_back(i); int ans = 0; for (int i : l) ans = (ans + 1ll * i * (i - 1) % mod * inv4 % mod * o[i]) % mod; for (int i : l) ans = (ans + 1ll * o[i] * (o[i] - 1) / 2 % mod * calc(i, i)) % mod; if (l.size() > 1) ans = (ans + 1ll * o[l[0]] * o[l[1]] % mod * calc(l[0], l[1])) % mod; printf("%d\n", ans); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, k, p, ans, inv4, cnt[N], inv[N]; void divide(int x, int k) { if (k == 1 || x == 1) return (void)(++cnt[x], ans = (ans + (long long)x * (x - 1) % p * inv4) % p); divide(x / 2, k - 1), divide(x - x / 2, k - 1); } inline int calc(int x, int y) { int ans = (long long)x * y % p * (p + 1) / 2 % p; for (int i = 2; i <= x + y; ++i) ans = (ans + (min(x, i - 1) - max(1, i - y) + 1ll) * (p - inv[i])) % p; return ans; } int main() { scanf("%d%d%d", &n, &k, &p), inv4 = (p + 1ll) * (p + 1) / 4 % p; inv[1] = 1; for (int i = 2; i <= n; ++i) inv[i] = (long long)(p - p / i) * inv[p % i] % p; divide(n, k); int x = 0; for (int i = 1; i <= n; ++i) if (cnt[i]) { x = i; break; } ans = (ans + (long long)cnt[x] * (cnt[x] - 1) / 2 % p * calc(x, x)) % p; if (cnt[x + 1]) ans = (ans + (long long)cnt[x] * cnt[x + 1] % p * calc(x, x + 1) + (long long)cnt[x + 1] * (cnt[x + 1] - 1) / 2 % p * calc(x + 1, x + 1)) % p; return printf("%d", ans), 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 2E5 + 10; int P; template <typename T1, typename T2> void Inc(T1 &a, T2 b) { a += b; if (a >= P) a -= P; } template <typename T1, typename T2> void Dec(T1 &a, T2 b) { a -= b; if (a < 0) a += P; } template <typename T1, typename T2> T1 Add(T1 a, T2 b) { a += b; return a >= P ? a - P : a; } template <typename T1, typename T2> T1 Sub(T1 a, T2 b) { a -= b; return a < 0 ? a + P : a; } long long ksm(long long a, long long b) { long long ret = 1; for (; b; b >>= 1, (a *= a) %= P) if (b & 1) (ret *= a) %= P; return ret; } int n, k, inv2, inv4; long long Ans, sum[MAXN << 1]; map<int, int> M; long long calc(int len1, int len2) { long long ret = 1ll * len1 * len2 % P * inv2 % P; for (int i = 1; i <= len1; i++) Dec(ret, 1ll * Sub(sum[i + len2], sum[i]) % P); return ret; } void solve(int l, int r, int d) { if (l == r || d == k) { int len = r - l + 1; M[len]++; Inc(Ans, 1ll * len * (len - 1) % P * inv4 % P); return; } int mid = (l + r) >> 1; solve(l, mid, d + 1); solve(mid + 1, r, d + 1); } int main() { scanf("%d%d%d", &n, &k, &P); inv2 = ksm(2, P - 2); inv4 = ksm(4, P - 2); for (int i = 1; i <= 2 * n; i++) sum[i] = (sum[i - 1] + ksm(i, P - 2)) % P; solve(1, n, 1); for (auto p1 : M) { Inc(Ans, 1ll * p1.second * (p1.second - 1) % P * inv2 % P * calc(p1.first, p1.first) % P); for (auto p2 : M) if (p1.first < p2.first) Inc(Ans, 1ll * p1.second * p2.second % P * calc(p1.first, p2.first) % P); } printf("%lld\n", Ans); }
13
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 2e5 + 5; long long read() { long long s = 0; char c = getchar(), lc = '+'; while (c < '0' || '9' < c) lc = c, c = getchar(); while ('0' <= c && c <= '9') s = s * 10 + c - '0', c = getchar(); return lc == '-' ? -s : s; } void write(long long x) { if (x < 0) { putchar('-'); x = -x; } if (x < 10) putchar(x + '0'); else { write(x / 10); putchar(x % 10 + '0'); } } void print(long long x, char c = '\n') { write(x); putchar(c); } long long sum[N], ans, p; long long power(long long a, long long b) { long long ret = 1; while (b) { if (b & 1) ret = ret * a % p; a = a * a % p; b /= 2; } return ret; } long long calc(long long x, long long y) { long long ret = 0; for (long long i = 1; i <= x; i++) ret = (ret + sum[i + y] - sum[i] + p) % p; return ret; } long long tot[N][2]; void Count(long long l, long long r, long long h, long long Min) { if (r - l + 1 == Min) tot[h][0]++; else tot[h][1]++; if (h == 1) return void(ans = (ans + (r - l + 1) * (r - l) % p * power(4, p - 2) % p) % p); if (l == r) return; long long mid = (l + r) / 2; Count(l, mid, h - 1, Min / 2); Count(mid + 1, r, h - 1, Min / 2); } void merge_sort(long long h, long long Min) { if (h <= 0) return; if (h == 1) ans = (ans + tot[h][0] * (tot[h][0] - 1) / 2 % p * calc(Min, Min) % p + tot[h][1] * (tot[h][1] - 1) / 2 % p * calc(Min + 1, Min + 1) % p + tot[h][0] * tot[h][1] % p * calc(Min, Min + 1) % p) % p; merge_sort(h - 1, Min / 2); } signed main() { memset(tot, 0, sizeof(tot)); long long n = read(), k = read(); p = read(); for (long long i = 1; i <= n * 2; i++) sum[i] = (sum[i - 1] + power(2, p - 2) - power(i, p - 2) + p) % p; Count(1, n, k, n); merge_sort(k, n); print(ans); return 0; }
13
CPP
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) #pragma GCC optimize("Ofast") using namespace std; namespace ywy { unordered_map<int, int> mp; long long inv[200001], ans = 0; int p; inline void solve(int a, int b) { long long cjr = a; cjr *= b; cjr %= p; cjr *= mp[a]; cjr %= p; cjr *= mp[b]; cjr %= p; ans = (ans + cjr * inv[2]) % p; for (register int i = 2; i <= a + b; i++) { long long l = max(1, i - b), r = min(i - 1, a); if (l > r) continue; cjr = p - inv[i]; cjr *= (r - l + 1); cjr %= p; cjr *= mp[a]; cjr %= p; cjr *= mp[b]; ans = (ans + cjr) % p; } } void digui(int l, int r, int h) { if (h <= 1) { mp[r - l + 1]++; return; } if (l == r) return; int mid = (l + r) >> 1; digui(l, mid, h - 1); digui(mid + 1, r, h - 1); } void ywymain() { int n, k; cin >> n >> k >> p; inv[0] = inv[1] = 1; for (register int i = 2; i <= 100000; i++) inv[i] = (inv[p % i] * (p - p / i)) % p; digui(1, n, k); for (unordered_map<int, int>::iterator i = mp.begin(); i != mp.end(); i++) { long long cjr = i->first - 1; cjr *= i->first; cjr %= p; cjr *= i->second; cjr %= p; ans = (ans + cjr * inv[4]) % p; cjr = inv[2]; cjr *= (i->second - 1); cjr %= p; cjr *= i->second; cjr %= p; for (register int j = 2; j <= i->first * 2; j++) { long long l = max(1, j - i->first), r = min(j - 1, i->first); if (l > r) continue; long long h = p - inv[j]; h *= cjr; h %= p; h *= (r - l + 1); h %= p; ans = (ans + h) % p; } cjr *= inv[2]; cjr %= p; cjr *= i->first; cjr %= p; cjr *= i->first; ans = (ans + cjr) % p; } for (unordered_map<int, int>::iterator i = mp.begin(); i != mp.end(); i++) { unordered_map<int, int>::iterator j = i; j++; for (; j != mp.end(); j++) solve(i->first, j->first); } cout << ans << endl; } } // namespace ywy int main() { ywy::ywymain(); return (0); }
13
CPP
#include <bits/stdc++.h> using namespace std; template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; } template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; } long long P; const int maxn = 110000; long long rec[maxn]; long long deg(long long x, long long d) { if (d < 0) d += P - 1; long long y = 1; while (d) { if (d & 1) (y *= x) %= P; (x *= x) %= P; d /= 2; } return y; } map<int, long long> cnt; void merge(int l, int k) { if (k <= 1 || l == 1) { ++cnt[l]; return; } merge(l / 2, k - 1); merge(l - l / 2, k - 1); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.precision(10); cout << fixed; int n, k; cin >> n >> k >> P; merge(n, k); long long ans = 0; for (auto w1 : cnt) { int n = w1.first; long long q = w1.second; (ans += q * n % P * (n - 1) % P * deg(4, -1)) %= P; } for (auto w1 : cnt) for (auto w2 : cnt) { int n = w1.first, m = w2.first; if (m < n) continue; long long coef; if (n == m) coef = w1.second * (w1.second - 1) / 2 % P; else coef = w1.second * w2.second % P; long long R = 1LL * n * m % P * (P + 1) / 2 % P; for (int s = 1; s <= (int)(n + m); ++s) { int l = max(1, s - m), r = min(n, s - 1); (R -= 1LL * (r - l + 1) * deg(s, -1)) %= P; } (ans += coef * R) %= P; } if (ans < 0) ans += P; cout << ans << '\n'; return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; using pii = pair<int, int>; using ll = long long; const int N = 100005; int n, kk, P; ll ans, inv[N], sum[N]; map<int, int> cnt; ll qpow(ll a, ll b) { ll ret = 1; while (b) { if (b & 1) ret = ret * a % P; a = a * a % P; b >>= 1; } return ret; } void proc(int l, int r, int dep) { if (dep <= 1 || l == r) { cnt[r - l + 1]++; return; } int mid = (l + r) >> 1; proc(l, mid, dep - 1); proc(mid + 1, r, dep - 1); } int calc(int x, int y) { ll ret = ll(x) * y % P; for (int i = 1; i <= x; i++) ret = (ret + P - (sum[i + y] - sum[i]) * 2 % P) % P; return ret; } int main() { ios::sync_with_stdio(false); cin >> n >> kk >> P; for (int i = 1; i < N; i++) inv[i] = qpow(i, P - 2), sum[i] = (sum[i - 1] + inv[i]) % P; proc(1, n, kk); for (auto &i : cnt) { ll t = i.first, s = i.second; ans = (ans + t * (t - 1) % P * inv[2] % P * s % P + s * (s - 1) % P * inv[2] % P * calc(t, t) % P) % P; } for (auto &i : cnt) for (auto &j : cnt) if (i.first < j.first) ans = (ans + calc(i.first, j.first) * 1ll * i.second % P * j.second % P) % P; cout << ans * inv[2] % P << endl; return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; inline int read() { int x = 0; char ch = getchar(); while (ch < '0' || ch > '9') ch = getchar(); while ('0' <= ch && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x; } int n, k, mod; long long inv[N], sum[N], ans; map<long long, long long> m; void solve(int l, int r, int dep) { if (l == r || !dep) { m[r - l + 1]++; return; } int mid = (l + r) >> 1; solve(l, mid, dep - 1), solve(mid + 1, r, dep - 1); } void init() { inv[0] = inv[1] = 1; for (register int i = 2; i <= n; i++) inv[i] = (mod - mod / i) * inv[mod % i] % mod, sum[i] = (sum[i - 1] + inv[i]) % mod; } long long cal(long long x, long long y) { long long res = x * y; for (register int i = 1; i <= x; i++) (res += mod - (sum[i + y] - sum[i] + mod) % mod * 2 % mod) %= mod; return res; } void work() { for (map<long long, long long>::iterator it = m.begin(); it != m.end(); it++) { long long len = it->first, cnt = it->second; (ans += len * (len - 1) % mod * inv[2] % mod * cnt % mod) %= mod; (ans += cnt * (cnt - 1) % mod * inv[2] % mod * cal(len, len)) %= mod; } for (map<long long, long long>::iterator it1 = m.begin(); it1 != m.end(); it1++) { for (map<long long, long long>::iterator it2 = m.begin(); it2 != m.end(); it2++) { long long x = it1->first, y = it2->first; if (x < y) (ans += cal(x, y) * it1->second % mod * it2->second % mod) %= mod; } } } int main() { n = read(), k = read() - 1, mod = read(); init(); solve(1, n, k); work(); printf("%lld", ans * inv[2] % mod); }
13
CPP
#include <bits/stdc++.h> using namespace std; long long mod, inv2; vector<pair<long long, long long> > vec; long long inv[20000005], ans, sum; long long calc(long long len) { return len * (len - 1) % mod * inv2 % mod; } void merge(long long l, long long r, long long h) { if (h == 1 || l == r) { long long len = r - l + 1; ans += calc(len) * inv2 % mod; ans %= mod; for (long long i = 0; i < (long long)vec.size(); ++i) if (vec[i].first == len) { ++vec[i].second; return; } return vec.push_back(make_pair(len, 1)), void(0); } long long mid = (l + r) >> 1; merge(l, mid, h - 1); merge(mid + 1, r, h - 1); } void init(long long n) { inv[1] = 1; for (long long i = 2; i <= n; ++i) inv[i] = 1ll * (mod - (mod / i)) * inv[mod % i] % mod; for (long long i = 1; i <= n; ++i) inv[i] = (inv[i] + inv[i - 1]) % mod; } signed main() { long long n, K; cin >> n >> K >> mod; inv2 = (mod + 1) / 2; init(n + n); merge(1, n, K); for (long long i = 0; i < (long long)vec.size(); ++i) for (long long j = i; j < (long long)vec.size(); ++j) { long long lenA = vec[i].first, lenB = vec[j].first; long long cntA = vec[i].second, cntB = vec[j].second; sum = 0; for (long long k = 1; k <= lenA; ++k) sum = (sum + inv[k + lenB] - inv[k] + mod) % mod; long long tot = (i == j ? calc(cntA) : cntA * cntB) % mod; ans += tot * ((lenA * lenB % mod * inv2 % mod - sum + mod) % mod) % mod; ans %= mod; } cout << ans; return 0; }
13
CPP
#include <bits/stdc++.h> std::map<long long, long long> mp; long long p, inv[200005], sum[200005]; inline long long read() { register long long x = 0, f = 1; register char s = getchar(); while (s > '9' || s < '0') { if (s == '-') f = -1; s = getchar(); } while (s >= '0' && s <= '9') { x = x * 10 + s - '0'; s = getchar(); } return x * f; } inline long long max(const long long &x, const long long &y) { return x > y ? x : y; } inline void simulate(long long l, long long r, long long k) { if (k == 1 || l == r) { std::map<long long, long long>::iterator it = mp.find(r - l + 1); if (it != mp.end()) ++it->second; else mp.insert(std::make_pair(r - l + 1, 1)); return; } long long mid = l + r >> 1; simulate(l, mid, k - 1); simulate(mid + 1, r, k - 1); } inline long long calc(long long len1, long long len2) { long long res = 0; for (register long long i = 1; i <= len1; ++i) { (res += (inv[2] * len2 % p - (sum[i + len2] - sum[i]) % p) % p) %= p; } return res; } signed main() { long long ans = 0; long long n = read(), k = read(); p = read(); inv[1] = 1; for (register long long i = 2; i <= max(n, 4); ++i) inv[i] = (p - (p / i)) * 1ll * inv[p % i] % p; for (register long long i = 1; i <= n; ++i) sum[i] = (sum[i - 1] + inv[i]) % p; simulate(1, n, k); for (std::map<long long, long long>::iterator it = mp.begin(); it != mp.end(); ++it) { long long len = it->first, cnt = it->second; (ans += cnt * ((len - 1) * 1ll * len % p) % p * inv[4] % p) %= p; } for (std::map<long long, long long>::iterator it1 = mp.begin(); it1 != mp.end(); ++it1) { for (std::map<long long, long long>::iterator it2 = mp.begin(); it2 != mp.end(); ++it2) { if (it1->first == it2->first) { long long len = it1->first, cnt = (it1->second - 1) * 1ll * (it1->second) / 2 % p; (ans += cnt * 1ll * calc(len, len) % p) %= p; } else if (it1->first < it2->first) { long long cnt = (it2->second) * (it1->second) % p; (ans += cnt * 1ll * calc(it1->first, it2->first) % p) %= p; } } } printf("%lld\n", (ans + p) % p); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int n, k, mod, ans; int inv[N], sum[N]; map<int, int> cnt; map<int, int>::iterator it1, it2; int read() { int ret = 0, f = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = 0; c = getchar(); } while (isdigit(c)) ret = ret * 10 + (c ^ 48), c = getchar(); return f ? ret : -ret; } void up(int &x, int y) { x += y; if (x >= mod) x -= mod; if (x < 0) x += mod; } int qpow(int x, int y) { int res = 1; x %= mod; for (; y; y >>= 1, x = (long long)x * x % mod) if (y & 1) res = (long long)res * x % mod; return res; } void init() { n = read(); k = read(); mod = read(); for (int i = 1; i < N; ++i) sum[i] = inv[i] = qpow(i, mod - 2), up(sum[i], sum[i - 1]); } void divide(int l, int r, int dp) { if (dp <= 1 || l == r) { cnt[r - l + 1]++; return; } int mid = (l + r) >> 1; divide(l, mid, dp - 1); divide(mid + 1, r, dp - 1); } int calc(int x, int y) { int res = (long long)x * y % mod; for (int i = 1; i <= x; ++i) up(res, -(sum[i + y] - sum[i]) * 2 % mod); return res; } void solve() { for (it1 = cnt.begin(); it1 != cnt.end(); ++it1) { int t = it1->first, s = it1->second; up(ans, (long long)t * (t - 1) % mod * inv[2] % mod * s % mod); up(ans, (long long)s * (s - 1) % mod * inv[2] % mod * calc(t, t) % mod); } for (it1 = cnt.begin(); it1 != cnt.end(); ++it1) for (it2 = cnt.begin(); it2 != cnt.end(); ++it2) { int x = it1->first, y = it2->first; if (x >= y) continue; up(ans, (long long)calc(x, y) * it1->second % mod * it2->second % mod); } printf("%d\n", (long long)ans * inv[2] % mod); } int main() { init(); divide(1, n, k); solve(); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e6 + 10; int mod = 1e9 + 7; long long power(long long a, long long b) { long long res = 1; while (b) { if (b & 1) res = res * a % mod; a = a * a % mod; b >>= 1; } return res; } long long INV(long long a) { return power(a, mod - 2); } mt19937 Rand(123456); int Range(int l, int r) { return l + Rand() % (r - l + 1); } map<long long, long long> mp; void work(int l, int r, int k) { if (k == 1 || l == r) { ++mp[r - l + 1]; return; } int mid = (l + r) / 2; work(l, mid, k - 1); work(mid + 1, r, k - 1); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int n, k; cin >> n >> k >> mod; vector<long long> inv(2 * n), inv_sum; for (int i = 0; i <= 2 * n - 1; i++) inv[i] = INV(i); inv_sum = inv; for (int i = 1; i <= 2 * n - 1; i++) inv_sum[i] += inv_sum[i - 1]; for (auto& I : inv_sum) I %= mod; auto merge = [&](int l, int r, const vector<long long>& inv_sum) { long long res = (long long)l * r % mod * INV(2) % mod; for (int i = 1; i <= l; i++) res -= inv_sum[i + r] - inv_sum[i]; res %= mod; res += mod; return res % mod; }; work(1, n, k); long long res = 0; for (auto I : mp) res += (I.first * (I.first - 1) % mod * INV(4) % mod) * I.second % mod, res += I.second * (I.second - 1) % mod * INV(2) % mod * merge(I.first, I.first, inv_sum) % mod; for (auto I : mp) for (auto J : mp) if (I.first < J.first) res += merge(I.first, J.first, inv_sum) * I.second % mod * J.second % mod; res %= mod; res += mod; res %= mod; cout << res; }
13
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 10; int n, k, mod, ans; int inv[N], sum[N]; map<int, int> cnt; map<int, int>::iterator it1, it2; int read() { int ret = 0, f = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = 0; c = getchar(); } while (isdigit(c)) ret = ret * 10 + (c ^ 48), c = getchar(); return f ? ret : -ret; } void up(int &x, int y) { x += y; if (x >= mod) x -= mod; if (x < 0) x += mod; } int Pow(int b, int k) { int r = 1; for (; k; k >>= 1, b = 1ll * b * b % mod) if (k & 1) r = 1ll * r * b % mod; return r; } void divide(int l, int r, int dp) { if (dp <= 1 || l == r) { cnt[r - l + 1]++; return; } int mid = (l + r) >> 1; divide(l, mid, dp - 1); divide(mid + 1, r, dp - 1); } int calc(int x, int y) { int res = (long long)x * y % mod; for (int i = 1; i <= x; ++i) up(res, -(sum[i + y] - sum[i]) * 2 % mod); return res; } int main() { n = read(); k = read(); mod = read(); for (int i = 1; i < N; ++i) sum[i] = inv[i] = Pow(i, mod - 2), up(sum[i], sum[i - 1]); divide(1, n, k); for (it1 = cnt.begin(); it1 != cnt.end(); ++it1) { int t = it1->first, s = it1->second; up(ans, (long long)t * (t - 1) % mod * inv[2] % mod * s % mod); up(ans, (long long)s * (s - 1) % mod * inv[2] % mod * calc(t, t) % mod); } for (it1 = cnt.begin(); it1 != cnt.end(); ++it1) for (it2 = cnt.begin(); it2 != cnt.end(); ++it2) { int x = it1->first, y = it2->first; if (x >= y) continue; up(ans, (long long)calc(x, y) * it1->second % mod * it2->second % mod); } printf("%d\n", (long long)ans * inv[2] % mod); }
13
CPP
#include <bits/stdc++.h> using namespace std; int MOD; int inv[200005]; void gen_inv(int maxa) { inv[1] = 1; for (int i = 2; i <= maxa; i++) inv[i] = MOD - 1LL * inv[MOD % i] * (MOD / i) % MOD; } int getans(int u, int v) { int ans = 0; for (int i = 2; i <= u + v; i++) ans = (ans + 1LL * (min(u, i - 1) - max(1, i - v) + 1) * inv[i]) % MOD; return ans; } int main() { int n, k; scanf("%d%d%d", &n, &k, &MOD); k--; gen_inv(2 * n); int ans = 1LL * n * (n - 1) / 2 % MOD * inv[2] % MOD; if (k <= 20 && (n >> k)) { int c0 = n >> k, c1 = c0 + 1; int tot1 = n & (1 << k) - 1, tot0 = (1 << k) - tot1; ans = (ans - 1LL * tot0 * (tot0 - 1) / 2 % MOD * getans(c0, c0) % MOD + MOD) % MOD; ans = (ans - 1LL * tot1 * (tot1 - 1) / 2 % MOD * getans(c1, c1) % MOD + MOD) % MOD; ans = (ans - 1LL * tot0 * tot1 % MOD * getans(c0, c1) % MOD + MOD) % MOD; } else ans = 0; printf("%d\n", ans); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; template <typename T> void read(T &x) { x = 0; bool f = 0; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = 1; for (; isdigit(c); c = getchar()) x = x * 10 + (c ^ 48); if (f) x = -x; } template <typename F> inline void write(F x, 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); } template <typename T> inline void Mx(T &x, T y) { x < y && (x = y); } template <typename T> inline void Mn(T &x, T y) { x > y && (x = y); } const int N = 500005; long long inv[N]; int n, k, P; long long L1, L2, C1, C2; long long calc(int L1, int L2) { long long ans = 0; for (int i = 1; i <= L1; i++) ans = ans + inv[i + L2] - inv[i]; return (ans % P + P) % P; } void solve(int l, int r, int k) { if (k <= 1 || l == r) { int len = r - l + 1; (!L1 || len == L1) ? L1 = len : !L2 && (L2 = len); len == L1 ? C1++ : C2++; return; } int mid = (l + r) >> 1; solve(l, mid, k - 1), solve(mid + 1, r, k - 1); } int main() { read(n), read(k), read(P); inv[0] = inv[1] = 1; long long inv2 = (P + 1) >> 1; for (int i = 2; i <= n; i++) inv[i] = (P - P / i) * inv[P % i] % P; for (int i = 2; i <= n; i++) inv[i] = inv[i - 1] + inv[i] % P; solve(1, n, k); long long ans = (long long)n * (n - 1) % P * inv2 % P * inv2 % P; ans = (ans - C1 * (C1 - 1) / 2 % P * calc(L1, L1) - C2 * (C2 - 1) / 2 % P * calc(L2, L2) % P - C1 * C2 % P * calc(L1, L2)) % P; write((ans % P + P) % P); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100005; int n, k, mod, ans, inv[N], s[N]; map<int, int> c; int ksm(int a, int b) { int r = 1; while (b) { if (b & 1) r = 1ll * r * a % mod; a = 1ll * a * a % mod; b >>= 1; } return r; } void pre(int l, int r, int k) { if (k <= 1 || l == r) { c[r - l + 1]++; return; } int mid = (l + r) >> 1; pre(l, mid, k - 1); pre(mid + 1, r, k - 1); } int clc(int x, int y) { int r = 1ll * x * y % mod; for (int i = 1; i <= x; i++) r = (r - 2ll * (s[i + y] - s[i]) % mod) % mod; return r; } int main() { scanf("%d%d%d", &n, &k, &mod); for (int i = 1; i <= 1e5; i++) inv[i] = ksm(i, mod - 2), s[i] = (s[i - 1] + inv[i]) % mod; pre(1, n, k); for (map<int, int>::iterator i = c.begin(); i != c.end(); i++) { ans = (ans + 1ll * i->first * (i->first - 1) % mod * inv[2] % mod * i->second % mod) % mod; ans = (ans + 1ll * i->second * (i->second - 1) % mod * inv[2] % mod * clc(i->first, i->first) % mod) % mod; } for (map<int, int>::iterator i = c.begin(); i != c.end(); i++) for (map<int, int>::iterator j = c.begin(); j != c.end(); j++) if (i->first < j->first) ans = (ans + 1ll * clc(i->first, j->first) * i->second % mod * j->second % mod) % mod; printf("%lld\n", (1ll * ans * inv[2] % mod + mod) % mod); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; char buf[1 << 12], *pp1 = buf, *pp2 = buf, nc; int ny; inline char gc() { return pp1 == pp2 && (pp2 = (pp1 = buf) + fread(buf, 1, 1 << 12, stdin), pp1 == pp2) ? EOF : *pp1++; } inline int read() { int x = 0; for (ny = 1; nc = gc(), (nc < 48 || nc > 57) && nc != EOF;) if (nc == 45) ny = -1; if (nc < 0) return nc; for (x = nc - 48; nc = gc(), 47 < nc && nc < 58 && nc != EOF; x = (x << 3) + (x << 1) + (nc ^ 48)) ; return x * ny; } int n, k, Mod, Fac[MAXN], iFac[MAXN], ans, t[MAXN], bel[MAXN], cnt, len[MAXN], inv[MAXN], sum[MAXN]; inline int C(int n, int m) { return n < 0 || m < 0 || n < m ? 0 : 1ll * Fac[n] * iFac[m] % Mod * iFac[n - m] % Mod; } inline int Fp(int x, int k) { int ans = 1; for (; k; k >>= 1, x = 1ll * x * x % Mod) if (k & 1) ans = 1ll * ans * x % Mod; return ans; } int t1, t2; inline int calc(int len1, int len2) { int ans = 0; for (int i = (0); i <= (len1 - 1); i++) ans = (ans + 1ll * i * (sum[i + len2 + 1] - sum[i + 1] + Mod) % Mod) % Mod; return 1ll * ans * (Mod + 1 >> 1) % Mod; } inline void Div(int l, int r, int h) { if (h == k || l == r) { len[++cnt] = r - l + 1; return; } int mid = l + r >> 1; Div(l, mid, h + 1), Div(mid + 1, r, h + 1); } int main() { n = read(), k = read() - 1, Mod = read(), Div(1, n, 0); if (k > 20) return puts("0"), 0; Fac[0] = 1; for (int i = (1); i <= (n); i++) Fac[i] = 1ll * Fac[i - 1] * i % Mod; iFac[n] = Fp(Fac[n], Mod - 2); for (int i = (n); i >= (1); i--) iFac[i - 1] = 1ll * iFac[i] * i % Mod; for (int i = (1); i <= (n); i++) inv[i] = 1ll * iFac[i] * Fac[i - 1] % Mod, sum[i] = (sum[i - 1] + inv[i]) % Mod; if (k) { for (int i = (1); i <= (cnt); i++) if (len[i] == ((n >> k))) t1++; else t2++; ans = (calc(n >> k, n >> k) * (1ll * t1 * (t1 - 1) % Mod) + (calc(n >> k, (n >> k) + 1) + calc((n >> k) + 1, n >> k)) * (1ll * t1 * t2 % Mod) + calc((n >> k) + 1, (n >> k) + 1) * (1ll * t2 * (t2 - 1) % Mod)) % Mod; } for (int i = (1); i <= (cnt); i++) ans = (ans + (1ll * len[i] * (len[i] - 1) / 2 % Mod) * (Mod + 1 >> 1) % Mod) % Mod; cout << ans << "\n"; return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 7; long long MOD, f[N]; long long qpow(long long x, long long y) { long long z = 1; while (y > 0) { if (y % 2) z = z * x % MOD; x = x * x % MOD; y /= 2; } return z; } map<int, int> ma; pair<int, int> a[10]; void dfs(int x, int k) { if (k == 1 || x == 1) { ma[x]++; return; } dfs((x + 1) / 2, k - 1); dfs(x / 2, k - 1); } int main() { int n, k; scanf("%d%d%lld", &n, &k, &MOD); dfs(n, k); for (int i = 1; i <= n * 2; i++) f[i] = (f[i - 1] - qpow(i, MOD - 2) + qpow(2, MOD - 2) + MOD) % MOD; int cnt = 0; for (auto p : ma) { a[++cnt] = p; } long long ans = 0; for (int i = 1; i <= cnt; i++) { ans += 1LL * a[i].second * a[i].first % MOD * (a[i].first - 1) % MOD * qpow(4, MOD - 2) % MOD; long long sum = 0; for (int j = 1; j <= a[i].first; j++) { sum = (sum + f[j + a[i].first] - f[j] + MOD) % MOD; } ans += sum * a[i].second % MOD * (a[i].second - 1) % MOD * qpow(2, MOD - 2) % MOD; } if (cnt == 2) { long long sum = 0; for (int j = 1; j <= a[1].first; j++) { sum = (sum + f[j + a[2].first] - f[j] + MOD) % MOD; } ans += sum * a[1].second % MOD * a[2].second % MOD; } printf("%lld", ans % MOD); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; const int N = 220000; map<int, int> H; int inv[N]; void build(int st, int en, int k) { if (k == 1) { int len = en - st + 1; if (H.count(len)) H[len]++; else H[len] = 1; return; } if (st == en) return; int mid = st + en >> 1; build(st, mid, k - 1); build(mid + 1, en, k - 1); } int main() { ios_base::sync_with_stdio(0); int n, k, mod; cin >> n >> k >> mod; build(1, n, k); if (!H.size() || (H.size() == 1 && H.begin()->first == 1)) { cout << 0 << endl; return 0; } inv[1] = 1; for (int i = 2; i < N; i++) inv[i] = (long long)inv[mod % i] * (mod - mod / i) % mod; int ans = 0; if (H.size() == 1) { int len = H.begin()->first, cnt = H.begin()->second; ans = (long long)len * (len - 1) % mod * inv[4] % mod * cnt % mod; if (cnt > 1) { int now = 0, nn = 0; for (int sum = 2; sum <= len + len; sum++) { int st = max(1, sum - len), en = min(len, sum - 1); nn = en - st + 1; now += (long long)((inv[2] - inv[sum] + mod) % mod) * nn % mod; if (now >= mod) now -= mod; } now = (long long)now * cnt % mod * (cnt - 1) % mod * inv[2] % mod; (ans += now) >= mod && (ans -= mod); } } else { int len = H.begin()->first, cnt = H.begin()->second; ans = (long long)len * (len - 1) % mod * inv[4] % mod * cnt % mod; if (cnt > 1) { int now = 0, nn = 0; for (int sum = 2; sum <= len + len; sum++) { int st = max(1, sum - len), en = min(len, sum - 1); nn = en - st + 1; now += (long long)((inv[2] - inv[sum] + mod) % mod) * nn % mod; if (now >= mod) now -= mod; } now = (long long)now * cnt % mod * (cnt - 1) % mod * inv[2] % mod; (ans += now) >= mod && (ans -= mod); } map<int, int>::iterator it = H.begin(); it++; len = it->first, cnt = it->second; ans += (long long)len * (len - 1) % mod * inv[4] % mod * cnt % mod; if (ans >= mod) ans -= mod; if (cnt > 1) { int now = 0, nn = 0; for (int sum = 2; sum <= len + len; sum++) { int st = max(1, sum - len), en = min(len, sum - 1); nn = en - st + 1; now += (long long)((inv[2] - inv[sum] + mod) % mod) * nn % mod; if (now >= mod) now -= mod; } now = (long long)now * cnt % mod * (cnt - 1) % mod * inv[2] % mod; (ans += now) >= mod && (ans -= mod); } int len1 = H.begin()->first, len2 = it->first; int cnt1 = H.begin()->second, cnt2 = it->second; int now = 0, nn = 0; for (int sum = 2; sum <= len1 + len2; sum++) { int st = max(1, sum - len2), en = min(len1, sum - 1); nn = en - st + 1; now += (long long)((inv[2] - inv[sum] + mod) % mod) * nn % mod; if (now >= mod) now -= mod; } now = (long long)now * cnt1 % mod * cnt2 % mod; (ans += now) >= mod && (ans -= mod); } cout << ans << endl; return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; long long n, m, mod, sum[200005], inv[200005], ans, s1, s2, num1, num2; void split(long long l, long long r, long long dep) { if (l > r) return; if (dep == 1 || l == r) { if (!s1) s1 = r - l + 1; if (s1 == r - l + 1) num1++; else { if (!s2) s2 = r - l + 1; if (s2 == r - l + 1) num2++; } ans = (ans + (r - l + 1) * (r - l) % mod * inv[4]) % mod; return; } long long mid = l + r >> 1; split(l, mid, dep - 1); split(mid + 1, r, dep - 1); } int main() { scanf("%lld%lld%lld", &n, &m, &mod); inv[0] = inv[1] = 1; for (long long i = 2; i <= n + 10; i++) inv[i] = (mod - mod / i) * inv[mod % i] % mod; for (long long i = 1; i <= n + 10; i++) sum[i] = (sum[i - 1] + inv[i]) % mod; split(1, n, m); if (s1 && s2) { long long nw = 0; for (long long i = 1; i <= s1; i++) { nw = (nw + inv[2] * s1 - sum[i + s1] + sum[i] + mod) % mod; } ans = (ans + num1 * (num1 - 1) % mod * inv[2] % mod * nw) % mod; nw = 0; for (long long i = 1; i <= s1; i++) { nw = (nw + inv[2] * s2 - sum[i + s2] + sum[i] + mod) % mod; } ans = (ans + num1 * num2 % mod * nw) % mod; nw = 0; for (long long i = 1; i <= s2; i++) { nw = (nw + inv[2] * s2 - sum[i + s2] + sum[i] + mod) % mod; } ans = (ans + num2 * (num2 - 1) % mod * inv[2] % mod * nw) % mod; } else { long long nw = 0; for (long long i = 1; i <= s1; i++) { nw = (nw + inv[2] * s1 - sum[i + s1] + sum[i] + mod) % mod; } ans = (ans + num1 * (num1 - 1) % mod * inv[2] % mod * nw) % mod; } printf("%lld", ans); }
13
CPP
#include <bits/stdc++.h> using namespace std; const int maxn = 200100; int mod, n, h; inline void Add(int &a, int b) { a = a + b >= mod ? a + b - mod : a + b; } int inv[maxn], sum[maxn]; inline int Qsum(int l, int r) { return (sum[r] - sum[l - 1] + mod) % mod; } int cnt[maxn], S, L; int ans; inline void getblock(int l, int r, int dep) { if (dep >= h || l == r) { int size = r - l + 1; cnt[size]++; if (L == 0) L = size; else if (L != size) S = size; if (S > L) swap(S, L); Add(ans, 1ll * size * (size - 1) % mod * inv[4] % mod); return; } int mid = (l + r) >> 1; getblock(l, mid, dep + 1); getblock(mid + 1, r, dep + 1); } inline int getans(int size1, int size2) { int ans = 1ll * size1 * size2 * inv[2] % mod; for (int i = 1; i <= size1; i++) Add(ans, mod - Qsum(i + 1, i + size2)); return ans; } int main() { scanf("%d%d%d", &n, &h, &mod); inv[1] = 1; for (int i = 2; i < maxn; i++) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod; for (int i = 1; i < maxn; i++) sum[i] = (sum[i - 1] + inv[i]) % mod; getblock(1, n, 1); if (S == 0) S = L, L = 0; Add(ans, 1ll * getans(S, S) * cnt[S] % mod * (cnt[S] - 1) % mod * inv[2] % mod); if (L) Add(ans, 1ll * getans(L, L) * cnt[L] % mod * (cnt[L] - 1) % mod * inv[2] % mod); if (L) Add(ans, 1ll * getans(S, L) * cnt[S] % mod * cnt[L] % mod); printf("%d", ans); }
13
CPP
#include <bits/stdc++.h> using namespace std; const int N = 100005; int inv[N], n, k, mo, ans; map<int, int> mp; void get(int n, int k) { if (k == 1 || n == 1) mp[n]++; else get(n / 2, k - 1), get(n - n / 2, k - 1); } int calc(int x, int y) { int ans = 1ll * x * y % mo * (mo + 1) / 2 % mo; for (int i = (int)(1); i <= (int)(y); i++) ans = (ans + mo + 1ll * inv[i] - inv[i + x]) % mo; return ans; } int main() { scanf("%d%d%d", &n, &k, &mo); inv[0] = inv[1] = 1; for (int i = (int)(2); i <= (int)(n); i++) inv[i] = 1ll * inv[mo % i] * (mo - mo / i) % mo; for (int i = (int)(1); i <= (int)(n); i++) inv[i] = (inv[i] + inv[i - 1]) % mo; get(n, k); for (auto a : mp) { ans = (ans + 1ll * a.first * (a.first - 1) / 2 % mo * (mo + 1) / 2 % mo * a.second) % mo; if (a.second >= 2) ans = (ans + 1ll * a.second * (a.second - 1) / 2 % mo * calc(a.first, a.first)) % mo; for (auto b : mp) if (a.first < b.first) ans = (ans + 1ll * calc(a.first, b.first) * a.second % mo * b.second) % mo; } printf("%d", ans); }
13
CPP
#include <bits/stdc++.h> using namespace std; const int MOD = (int)1e9 + 7; const int MAXN = (int)1e5 + 3; const int infint = (int)1e9 + 3; const long long inf = (long long)1e12; int n, k, q; vector<int> v; int add(int a, int b) { int c = a + b; if (c >= q) c -= q; if (c < 0) c += q; return c; } int mul(int a, int b) { int c = (1LL * a * b) % q; return c; } int pwr(int a, int b) { if (b == 0) return 1; int c = pwr(a, b >> 1); c = mul(c, c); if (b & 1) c = mul(c, a); return c; } int inv(int a) { return pwr(a, q - 2); } void solve(int n, int k) { if (k <= 1 || n == 1) { v.push_back(n); return; } solve(n / 2, k - 1); solve((n + 1) / 2, k - 1); } int div2; int inversion(int n) { return mul(n, mul(n - 1, inv(4))); } int sum(int a, int b) { if (a > b) return 0; a--; int s1 = mul(a, mul(a + 1, div2)), s2 = mul(b, mul(b + 1, div2)); s1 = add(s2, -s1); s1 = add(s1, a - b); return s1; } int ted[MAXN]; int f(int n1, int n2) { int cur = 0; for (int dem = 2; dem <= n1 + n2; dem++) { int num = sum(max(1, dem - n2), min(n1, dem - 1)); cur = add(cur, mul(num, inv(dem))); } return mul(cur, div2); } int debug(int n1, int n2) { int cur = 0; for (int i = 1; i <= n1; i++) for (int j = 1; j <= n2; j++) cur = add(cur, mul(i - 1, inv(i + j))); return mul(cur, inv(2)); } int main() { ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); cin >> n >> k >> q; solve(n, k); sort(v.begin(), v.end()); div2 = inv(2); int ans = 0; for (auto u : v) ans = add(ans, inversion(u)), ted[u]++; vector<int> diff = v; diff.resize(unique(diff.begin(), diff.end()) - diff.begin()); for (int i = 0; i < diff.size(); i++) for (int j = 0; j < diff.size(); j++) { int zarib; if (i != j) zarib = mul(ted[diff[i]], ted[diff[j]]); else zarib = mul(ted[diff[i]], ted[diff[j]] - 1); ans = add(ans, mul(zarib, f(diff[i], diff[j]))); } cout << ans; }
13
CPP
#include <bits/stdc++.h> using namespace std; long long MOD; long long powmod(long long a, long long n) { if (n == 0) return 1; if (n % 2) return (a * powmod(a, n - 1)) % MOD; long long c = powmod(a, n / 2); return (c * c) % MOD; } long long inv(long long a) { return powmod(a, MOD - 2); } long long fact[110000]; long long invfact[110000]; long long ncr(long long n, long long r) { if (r < 0 || n < 0) return 0; if (n < r) return 0; long long a = fact[n]; a = (a * invfact[r]) % MOD; a = (a * invfact[n - r]) % MOD; return a; } long long get(long long a, long long b) { long long ans = 0; for (long long q = 2; q <= a + b; q++) { long long f = min(a, q - 1) - max(1LL, q - b) + 1; ans += f * inv(q); ans %= MOD; } return ans; } int main() { long long n, k, q; cin >> n >> k >> q; MOD = q; map<long long, long long> r; r[n] = 1; for (int j = 1; j < k; j++) { map<long long, long long> rr; for (auto x : r) { if (x.first == 1) { rr[x.first] += x.second; } else { rr[x.first / 2] += x.second; rr[(x.first + 1) / 2] += x.second; } } r = rr; if (rr.count(1) && rr[1] == n) break; } long long avg = n * (n - 1) / 2; avg %= MOD; avg = (avg * inv(2)) % MOD; if (r.size() == 1) { auto x = *r.begin(); long long freq = x.second; long long g = x.first; avg -= (freq * (freq - 1) / 2) % MOD * get(g, g); avg %= MOD; } else if (r.size() == 2) { vector<pair<int, int> > x; for (auto b : r) x.push_back(b); long long freq0 = x[0].second; long long freq1 = x[1].second; avg -= (freq0 * (freq0 - 1) / 2) % MOD * get(x[0].first, x[0].first); avg %= MOD; avg -= (freq1 * (freq1 - 1) / 2) % MOD * get(x[1].first, x[1].first); avg %= MOD; avg -= (freq0 * freq1) % MOD * get(x[0].first, x[1].first); avg %= MOD; } avg %= MOD; if (avg < 0) avg += MOD; cout << avg << '\n'; }
13
CPP
#include <bits/stdc++.h> template <typename T> inline void read(T &x) { x = 0; char c = getchar(); while (!isdigit(c)) c = getchar(); while (isdigit(c)) x = x * 10 + (c ^ 48), c = getchar(); } using namespace std; int n, K, P; long long inv[201000], isum[201000]; inline void init() { inv[1] = 1; for (int i = 2; i <= max(n, 20); ++i) inv[i] = ((P - inv[P % i] * (P / i)) % P + P) % P; for (int i = 1; i <= max(n, 20); ++i) isum[i] = (isum[i - 1] + inv[i]) % P; } int tmp, cnt[201000]; void dfs_find(int L, int R, int nwk) { if (nwk == 1) { ++cnt[R - L + 1]; tmp = R - L + 1; return; } if (L == R) { ++cnt[1]; tmp = 1; return; } int mid = (L + R) >> 1; dfs_find(L, mid, nwk - 1); dfs_find(mid + 1, R, nwk - 1); } inline long long sol(int t, int tt) { long long res = 0; for (int i = 1; i <= t; ++i) { res += isum[i + tt] - isum[i]; } return (res % P + P) % P; } int main() { read(n), read(K), read(P); init(); if (K >= 30) { puts("0"); return 0; } dfs_find(1, n, K); int jzp, zzz; if (cnt[tmp - 1]) { jzp = tmp, zzz = tmp - 1; } else { jzp = tmp + 1, zzz = tmp; } int inv2 = inv[2]; long long ans = sol(jzp, jzp) * cnt[jzp] % P * (cnt[jzp] - 1) % P * inv2 % P; ans = (ans + sol(zzz, zzz) * cnt[zzz] % P * (cnt[zzz] - 1) % P * inv2 % P) % P; ans = (ans + sol(jzp, zzz) * cnt[jzp] % P * cnt[zzz] % P) % P; ans = 1ll * n * (n - 1) % P * inv[4] % P - ans; printf("%lld\n", (ans % P + P) % P); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, k, mod, c[N], inv[N], s[N]; void dfs(int d, int l, int r) { if (d <= 1 || l == r) { ++c[r - l + 1]; return; } int m = (l + r) >> 1; dfs(d - 1, l, m); dfs(d - 1, m + 1, r); } int solve(int n, int m) { int res = (long long)inv[2] * n % mod * m % mod; for (int i = 1; i <= n; ++i) (res += s[i] - s[i + m]) %= mod; return (res + mod) % mod; } int main() { scanf("%d%d%d", &n, &k, &mod); inv[1] = 1; for (int i = 2; i <= max(4, n); ++i) inv[i] = (mod - mod / i) * (long long)inv[mod % i] % mod; for (int i = 1; i <= n; ++i) s[i] = (s[i - 1] + inv[i]) % mod; dfs(k, 1, n); int l = 0; for (int i = 1; i <= n; ++i) if (c[i]) { l = i; break; } int res = ((c[l] * (l - 1ll) + c[l + 1] * (l + 1ll)) % mod * l % mod * inv[4] + c[l] * (c[l] - 1ll) % mod * inv[2] % mod * solve(l, l) + (long long)c[l] * c[l + 1] % mod * solve(l, l + 1) + c[l + 1] * (c[l + 1] - 1ll) % mod * inv[2] % mod * solve(l + 1, l + 1)) % mod; printf("%d\n", res); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; inline char gc() { static char buf[100000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? EOF : *p1++; } inline long long read() { long long x = 0; char ch = getchar(); bool positive = 1; for (; !isdigit(ch); ch = getchar()) if (ch == '-') positive = 0; for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0'; return positive ? x : -x; } inline void write(long long a) { if (a < 0) { a = -a; putchar('-'); } if (a >= 10) write(a / 10); putchar('0' + a % 10); } inline void writeln(long long a) { write(a); puts(""); } inline void wri(long long a) { write(a); putchar(' '); } const int N = 100005; int ycl[N], tong[N], mod, inv; void solve(int l, int r, int dep) { if (l == r || dep == 1) { tong[r - l + 1]++; return; } int mid = (l + r) >> 1; solve(l, mid, dep - 1); solve(mid + 1, r, dep - 1); } long long get(long long len) { return len * (len - 1) / 2 % mod * inv % mod; } inline long long ksm(long long a, int b) { int ans = 1; for (; b; b >>= 1) { if (b & 1) ans = ans * a % mod; a = a * a % mod; } return ans; } int n, k; long long get(long long a, long long b) { if (a + b > n) return 0; long long ans = 0; for (int i = 1; i <= a; i++) { ans = (ans + inv * b - ycl[i + b] + ycl[i]) % mod; } return (ans + mod) % mod; } int main() { cin >> n >> k >> mod; inv = (mod + 1) / 2; for (int i = 1; i <= n; i++) ycl[i] = (ycl[i - 1] + ksm(i, mod - 2)) % mod; solve(1, n, k); for (int i = 1; i <= n; i++) if (tong[i]) { cout << (get(i) * tong[i] + get(i + 1) * tong[i + 1] + get(i, i + 1) * tong[i] % mod * tong[i + 1] + tong[i] * (tong[i] - 1) / 2 % mod * get(i, i) + tong[i + 1] * (tong[i + 1] - 1) / 2 % mod * get(i + 1, i + 1)) % mod << endl; return 0; } }
13
CPP
#include <bits/stdc++.h> const int MAXN = 100010; int mod = 998244353; void reduce(int& x) { x += x >> 31 & mod; } int mul(int a, int b) { return (long long)a * b % mod; } int pow(int a, int b, int res = 1) { for (; b; b >>= 1, a = mul(a, a)) if (b & 1) res = mul(res, a); return res; } void mma(int& x, int y, int z) { x = (x + (long long)y * z) % mod; } int remod(long long x) { x %= mod; return x + (x >> 63 & mod); } std::map<int, int> hav; void solve(int l, int r, int dep) { if (l == r || dep <= 1) return (void)(++hav[r - l + 1]); int mid = l + r >> 1; solve(l, mid, dep - 1); solve(mid + 1, r, dep - 1); } inline int calc1(int x) { return (long long)pow(4, mod - 2, x * (x - 1ll) % mod); } inline int coef(std::pair<int, int> x, std::pair<int, int> y) { int t = x.second; if (x.first == y.first) return (long long)t * (t - 1) / 2 % mod; return mul(t, y.second); } int n, K; const int MAXS = MAXN << 1; int inv[MAXS]; inline int calc2(int x, int y) { int res = pow(2, mod - 2, mul(x, y)); for (int i = 1; i <= x; ++i) reduce(res -= inv[i + y]), reduce(res += inv[i] - mod); return res; } int main() { std::ios_base::sync_with_stdio(false), std::cin.tie(0); std::cin >> n >> K >> mod; inv[0] = inv[1] = 1; for (int i = 2; i != MAXS; ++i) inv[i] = mul(inv[mod % i], mod - mod / i); for (int i = 1; i != MAXS; ++i) reduce(inv[i] += inv[i - 1] - mod); solve(1, n, K); int ans = 0; for (auto t : hav) mma(ans, t.second, calc1(t.first)); for (auto x : hav) for (auto y : hav) if (x.first <= y.first) mma(ans, coef(x, y), calc2(x.first, y.first)); std::cout << ans << std::endl; return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 10; int n, k, P, inv[N], ans, f[2], c[2]; inline void upd(int &x, int y) { ((x += y) > P) && (x -= P); } void sol(int l, int r, int k) { if (!k || l == r) { int len = r - l + 1; c[len & 1] = len; f[len & 1]++; return; } int mid = (l + r) >> 1; sol(l, mid, k - 1); sol(mid + 1, r, k - 1); } inline int C0(int n) { return 1ll * n * (n - 1) / 2 % P * inv[2] % P; } inline int C1(int n) { int ans = 1ll * n * (1ll * n * inv[2] % P - inv[n + 1] + P) % P; for (int i = (2); i <= (n); ++i) upd(ans, P - 1ll * (i - 1) * (inv[i] + inv[2 * n + 2 - i]) % P); return ans; } inline int C2(int x, int y) { if (x < y) swap(x, y); int ans = 1ll * x * y % P * inv[2] % P; for (int i = (2); i <= (x); ++i) upd(ans, P - 1ll * (i - 1) * (inv[i] + inv[x + y + 2 - i]) % P); return ans; } int main() { scanf("%d%d%d", &n, &k, &P); inv[1] = 1; for (int i = (2); i <= (2 * n); ++i) inv[i] = 1ll * (P - P / i) * inv[P % i] % P; sol(1, n, k - 1); for (int i = (0); i <= (1); ++i) upd(ans, 1ll * f[i] * C0(c[i]) % P), upd(ans, 1ll * f[i] * (f[i] - 1) / 2 % P * C1(c[i]) % P); upd(ans, 1ll * f[0] * f[1] % P * C2(c[0], c[1]) % P); printf("%d\n", ans); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; int n, m, M, I2; map<int, int> cnt; long long inv[200050]; void divide(int m, int l, int r) { if (m == 1 || l == r) ++cnt[r - l + 1]; else { int mid = (l + r) >> 1; divide(m - 1, l, mid); divide(m - 1, mid + 1, r); } } long long pw(long long a, long long k) { long long ans = 1; for (; k; k >>= 1) { if (k & 1) ans = ans * a % M; a = a * a % M; } return ans; } void init() { scanf("%d%d%d", &n, &m, &M); divide(m, 1, n); I2 = (M + 1) >> 1; for (int i = 1; i < 200050; ++i) inv[i] = (inv[i - 1] + pw(i, M - 2)) % M; } long long C2(int x) { return 1LL * x * (x - 1) / 2 % M; } long long calc(int a, int b) { long long ans = 1LL * a * b % M * I2 % M; for (int i = 1; i <= a; ++i) ans += M - inv[i + b] + inv[i]; return ans % M; } void solve() { int ans = 0; for (auto p : cnt) ans += 1LL * C2(p.first) * I2 % M * p.second % M, ans %= M; for (auto p : cnt) { ans += calc(p.first, p.first) * C2(p.second) % M, ans %= M; for (auto q : cnt) if (p.first < q.first) ans += calc(p.first, q.first) * (p.second * q.second % M) % M, ans %= M; } printf("%d\n", ans); } int main() { init(); solve(); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; const int N = 2e5 + 5; int n, k, q, A[N], num[2], Min, mod, ans, I4; namespace { int add(int x) { return (x >= mod) ? x - mod : x; } int sub(int x) { return (x < 0) ? x + mod : x; } void Add(int &x, int y) { x = add(x + y); } void Sub(int &x, int y) { x = sub(x - y); } int Pow(int x, int y = mod - 2) { int res = 1; for (; y; y >>= 1, x = 1LL * x * x % mod) { if (y & 1) res = 1LL * res * x % mod; } return res; } } // namespace vector<int> L; void solve(int l, int r, int k) { if (l > r) return; if (k <= 1 || l == r) { int Len = r - l + 1; Min = min(Min, Len); L.push_back(Len); Add(ans, 1LL * Len * (Len - 1) % mod * I4 % mod); return; } int mid = (l + r) >> 1; solve(l, mid, k - 1); solve(mid + 1, r, k - 1); } int inv[N]; int calc(int Sum, int x, int y) { int L = max(Sum - y, 1), R = min(Sum - 1, x); return max(R - L + 1, 0); } int main() { cin >> n >> k >> mod; Min = 1e9; I4 = Pow(4); inv[0] = 0; for (int i = 1; i <= n * 2; ++i) inv[i] = Pow(i); solve(1, n, k); for (int i = 0; i < (int)L.size(); ++i) ++num[L[i] - Min]; for (int i = 1; i <= 2 * (Min + 1); ++i) { int tot = 0; Add(tot, 1LL * num[0] * (num[0] - 1) % mod * inv[2] % mod * calc(i, Min, Min) % mod); Add(tot, 1LL * num[1] * (num[1] - 1) % mod * inv[2] % mod * calc(i, Min + 1, Min + 1) % mod); Add(tot, 1LL * num[0] * num[1] % mod * calc(i, Min + 1, Min) % mod); Add(ans, 1LL * tot * inv[2] % mod * sub(1 - 2LL * inv[i] % mod) % mod); } printf("%d\n", ans); }
13
CPP
#include <bits/stdc++.h> std::map<long long, long long> mp; long long p, inv[200005], sum[200005]; inline long long read() { register long long x = 0, f = 1; register char s = getchar(); while (s > '9' || s < '0') { if (s == '-') f = -1; s = getchar(); } while (s >= '0' && s <= '9') { x = x * 10 + s - '0'; s = getchar(); } return x * f; } inline long long max(const long long &x, const long long &y) { return x > y ? x : y; } inline void simulate(long long l, long long r, long long k) { if (k == 1 || l == r) { std::map<long long, long long>::iterator it = mp.find(r - l + 1); if (it != mp.end()) ++it->second; else mp.insert(std::make_pair(r - l + 1, 1)); return; } long long mid = l + r >> 1; simulate(l, mid, k - 1); simulate(mid + 1, r, k - 1); } inline long long calc(long long len1, long long len2) { long long res = 0; for (register long long i = 1; i <= len1; ++i) { (res += (inv[2] * 1ll * len2 % p - (sum[i + len2] - sum[i]) % p) % p) %= p; } return res; } signed main() { long long ans = 0; long long n = read(), k = read(); p = read(); inv[1] = 1; for (register long long i = 2; i <= max(n, 4); ++i) inv[i] = (p - (p / i)) * 1ll * inv[p % i] % p; for (register long long i = 1; i <= n; ++i) sum[i] = (sum[i - 1] + inv[i]) % p; simulate(1, n, k); for (std::map<long long, long long>::iterator it = mp.begin(); it != mp.end(); ++it) { long long len = it->first, cnt = it->second; (ans += cnt * ((len - 1) * 1ll * len % p) % p * inv[4] % p) %= p; } for (std::map<long long, long long>::iterator it1 = mp.begin(); it1 != mp.end(); ++it1) { for (std::map<long long, long long>::iterator it2 = mp.begin(); it2 != mp.end(); ++it2) { if (it1->first == it2->first) { long long len = it1->first, cnt = (it1->second - 1) * 1ll * (it1->second) / 2 % p; (ans += cnt * 1ll * calc(len, len) % p) %= p; } else if (it1->first < it2->first) { long long cnt = (it2->second) * 1ll * (it1->second) % p; (ans += cnt * 1ll * calc(it1->first, it2->first) % p) %= p; } } } printf("%lld\n", (ans + p) % p); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; using uint = unsigned int; using ll = long long; using ull = unsigned long long; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } uint MD; struct ModInt { using M = ModInt; uint v; ModInt() : v{0} {} ModInt(ll _v) { set_v(_v % MD + MD); } M& set_v(uint _v) { v = (_v < MD) ? _v : _v - MD; return *this; } explicit operator bool() const { return v != 0; } M operator-() const { return M(0) - *this; } M operator+(const M& r) const { return M().set_v(v + r.v); } M operator-(const M& r) const { return M().set_v(v + MD - r.v); } M operator*(const M& r) const { return M().set_v(ull(v) * r.v % MD); } M operator/(const M& r) const { return *this * r.inv(); } M& operator+=(const M& r) { return *this = *this + r; } M& operator-=(const M& r) { return *this = *this - r; } M& operator*=(const M& r) { return *this = *this * r; } M& operator/=(const M& r) { return *this = *this / r; } M pow(ll n) const { M x = *this, r = 1; while (n) { if (n & 1) r *= x; x *= x; n >>= 1; } return r; } M inv() const { return (*this).pow(MD - 2); } friend ostream& operator<<(ostream& os, const M& r) { return os << r.v; } }; using Mint = ModInt; const int B = 200200; V<Mint> fact(B), iFac(B), inv(B), inv_sm(B); void first() { fact[0] = Mint(1); for (int i = 1; i < B; i++) fact[i] = fact[i - 1] * Mint(i); for (int i = 0; i < B; i++) iFac[i] = fact[i].inv(); for (int i = 1; i < B; i++) inv[i] = Mint(i).inv(); for (int i = 1; i < B; i++) inv_sm[i] = inv_sm[i - 1] + inv[i]; } Mint C(int n, int k) { if (n < k || k < 0) return 0; return fact[n] * iFac[k] * iFac[n - k]; } map<int, int> mp; void dfs(int l, int r, int h) { if (l == r) { mp[1]++; return; } if (h <= 1) { mp[r - l + 1]++; return; } int md = (l + r) / 2; dfs(l, md, h - 1); dfs(md + 1, r, h - 1); } int main() { int n, h; cin >> n >> h >> MD; first(); dfs(0, n - 1, h); auto two = [&](int l1, int l2) { Mint ans = 0; for (int i = 1; i <= l1; i++) { ans += Mint(i - 1) * (inv_sm[i + l2] - inv_sm[i]); } return ans / Mint(2); }; using P = pair<int, int>; V<P> ps; for (auto p : mp) { ps.push_back(p); } int k = int(ps.size()); Mint ans = 0; for (int i = 0; i < k; i++) { int c1, f1; tie(c1, f1) = ps[i]; ans += Mint(f1) * Mint(c1) * Mint(c1 - 1) / Mint(4); ans += Mint(f1) * Mint(f1 - 1) * two(c1, c1); for (int j = i + 1; j < k; j++) { int c2, f2; tie(c2, f2) = ps[j]; ans += Mint(f1) * Mint(f2) * (two(c1, c2) + two(c2, c1)); } } cout << ans << endl; return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; const int inf = 1e9; const long long Inf = 1e18; const int N = 1e5 + 10; int n, k, mod, inv[N], sinv[N], ans = 0; int gi() { int x = 0, o = 1; char ch = getchar(); while ((ch < '0' || ch > '9') && ch != '-') ch = getchar(); if (ch == '-') o = -1, ch = getchar(); while (ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar(); return x * o; } template <typename T> bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }; template <typename T> bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }; int add(int a, int b) { return a + b >= mod ? a + b - mod : a + b; } int sub(int a, int b) { return a - b < 0 ? a - b + mod : a - b; } void inc(int &a, int b) { a = (a + b >= mod ? a + b - mod : a + b); } void dec(int &a, int b) { a = (a - b < 0 ? a - b + mod : a - b); } vector<int> seg; map<int, int> cnt; void solve(int l, int r, int d) { if (d <= 1 || l == r) { seg.push_back(r - l + 1); return; } int mid = (l + r) >> 1; solve(l, mid, d - 1), solve(mid + 1, r, d - 1); } int calc(int a, int b) { int ret = 1ll * a * b % mod; for (int i = 1; i <= a; i++) dec(ret, 2ll * sub(sinv[i + b], sinv[i]) % mod); return ret; } int main() { n = gi(), k = gi(), mod = gi(); inv[1] = 1; for (int i = 2; i <= max(n, 2); i++) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod; sinv[1] = 1; for (int i = 2; i <= n; i++) sinv[i] = add(sinv[i - 1], inv[i]); solve(1, n, k); for (auto i : seg) inc(ans, (1ll * i * (i - 1) / 2) % mod), ++cnt[i]; for (auto i : cnt) if (i.second >= 2) inc(ans, 1ll * calc(i.first, i.first) * ((1ll * i.second * (i.second - 1) / 2) % mod) % mod); for (auto i : cnt) for (auto j : cnt) if (i.first < j.first) inc(ans, 1ll * calc(i.first, j.first) * i.second % mod * j.second % mod); ans = 1ll * ans * inv[2] % mod, cout << ans; return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; int inv[100010], s[100010], num[100010], n, k, MOD, ans; void work(int l, int r, int h) { if (h <= 1 || l == r) { num[r - l + 1]++; return; } int mid = (l + r) >> 1; work(l, mid, h - 1); work(mid + 1, r, h - 1); } int basic(int x) { return 1ll * x * (x - 1) % MOD * inv[4] % MOD; } int cal(int x, int y) { int tans = 0; for (int i = 1; i <= x; i++) tans = (0ll + tans + s[i + y] - s[i] + MOD) % MOD; return (1ll * x * y % MOD * inv[2] - tans + MOD) % MOD; } int mypow(int x, int n) { int tans = 1; for (; n; n >>= 1, x = 1ll * x * x % MOD) if (n & 1) tans = 1ll * tans * x % MOD; return tans; } int main() { scanf("%d%d%d", &n, &k, &MOD); inv[4] = mypow(4, MOD - 2); for (int i = 1; i <= n; i++) inv[i] = mypow(i, MOD - 2); for (int i = 1; i <= n; i++) s[i] = (s[i - 1] + inv[i]) % MOD; work(1, n, k); for (int i = 1; i <= n; i++) if (num[i]) { ans = (ans + 1ll * num[i] * basic(i)) % MOD; ans = (ans + 1ll * num[i] * (num[i] - 1) % MOD * inv[2] % MOD * cal(i, i)) % MOD; if (num[i + 1]) { ans = (ans + 1ll * num[i + 1] * basic(i + 1)) % MOD; ans = (ans + 1ll * num[i + 1] * (num[i + 1] - 1) % MOD * inv[2] % MOD * cal(i + 1, i + 1)) % MOD; ans = (ans + 1ll * num[i] * num[i + 1] % MOD * cal(i, i + 1)) % MOD; } break; } printf("%d\n", ans); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; int get() { char ch; while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-') ; if (ch == '-') { int s = 0; while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0'; return -s; } int s = ch - '0'; while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0'; return s; } const int N = 1e5 + 5; int n, k, q; map<int, int> cnt; long long ans; long long js[N], inv[N]; int mo; void build(int l, int r, int h) { if (l == r || h <= 1) { cnt[r - l + 1]++; return; } int mid = (l + r) / 2; build(l, mid, h - 1); build(mid + 1, r, h - 1); } long long calc(int la, int lb) { long long ret = inv[2] * la % mo * lb % mo; for (int i = 2; i <= la + lb; i++) { int l = 1, r = la; l = max(l, i - lb); r = min(r, i - 1); ret = (ret + mo - 1ll * (r - l + 1) * inv[i] % mo) % mo; } return ret; } int main() { n = get(); k = get(); mo = q = get(); build(1, n, k); inv[0] = inv[1] = 1; for (int i = 2; i <= 1e5; i++) inv[i] = 1ll * (q - q / i) * inv[q % i] % q; for (map<int, int>::iterator h = cnt.begin(); h != cnt.end(); h++) { int s = (*h).first, c = (*h).second; ans = (ans + 1ll * s * (s - 1) * inv[4] % q * c % q) % q; map<int, int>::iterator p = h; ans = (ans + inv[2] * c * (c - 1) % mo * calc(s, s) % mo) % mo; p++; for (; p != cnt.end(); p++) ans = (ans + 1ll * c * (*p).second % mo * calc(s, (*p).first)) % mo; } cout << ans << endl; return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; map<int, int> mp; int n, k, q; void dfs(int l, int r, int dep) { if (dep <= 1 || l == r) { mp[r - l + 1]++; return; } int mid = (l + r) >> 1; dfs(l, mid, dep - 1); dfs(mid + 1, r, dep - 1); } int ADD(int x, int y) { return (x += y) >= q ? x - q : x; } int SUB(int x, int y) { return (x -= y) < 0 ? x + q : x; } int MUL(int x, int y) { return (long long)x * y % q; } int HALF(int x) { return x & 1 ? (x + q >> 1) : (x >> 1); } const int N = 100010; int inv[N], invs[N]; int calc(int x, int y) { int ret = MUL(x, y); for (int i = 1; i <= x; ++i) { ret = SUB(ret, MUL(SUB(invs[i + y], invs[i]), 2)); } return ret; } int main() { scanf("%d%d%d", &n, &k, &q); dfs(1, n, k); inv[0] = inv[1] = 1; invs[0] = invs[1] = 1; for (int i = 2; i <= n; ++i) { inv[i] = MUL(inv[q % i], q - q / i); invs[i] = ADD(invs[i - 1], inv[i]); } int ans = 0; for (auto i : mp) for (auto j : mp) if (i.first <= j.first) { if (i.first == j.first) { int ret = calc(i.first, i.first); ans = ADD(ans, MUL(ret, HALF(MUL(i.second, i.second - 1)))); ans = ADD(ans, MUL(i.second, HALF(MUL(i.first, i.first - 1)))); } else { int ret = calc(i.first, j.first); ans = ADD(ans, MUL(ret, MUL(i.second, j.second))); } } cout << HALF(ans); }
13
CPP
#include <bits/stdc++.h> const int MAXN = 1e5 + 20; int n, k, M; int inv[MAXN], pre_inv[MAXN]; void math_pre() { inv[1] = 1; for (int i = 2; i <= ((n < 4) ? 4 : n); ++i) inv[i] = 1ll * (M - M / i) * inv[M % i] % M; for (int i = 1; i <= n; ++i) pre_inv[i] = (pre_inv[i - 1] + inv[i]) % M; } struct map { static const int MAXMap = 2; int tot; struct pad { int key, val; pad() {} pad(const int &KEY, const int &VAL) : key(KEY), val(VAL) {} } node[MAXMap + 1]; map() { tot = 0; } pad *find(const int &key) { pad *ret = node; while (ret - node < tot && ret->key != key) ++ret; return ret; } void insert(const pad &new_element) { node[tot++] = new_element; } pad *begin() { return &node[0]; } pad *end() { return &node[tot]; } } Map; void solve(const int &l, const int &r, const int &h) { if (l >= r || h <= 1) { int len = r - l + 1; map::pad *it = Map.find(len); if (it == Map.end()) Map.insert(map::pad(len, 1)); else ++it->val; return; } int mid = (l + r) >> 1; solve(l, mid, h - 1), solve(mid + 1, r, h - 1); } int calc(const int &len1, const int &len2) { int ret = 0; for (int i = 1; i <= len1; ++i) ret = ((ret + 1ll * inv[2] * len2 % M - (pre_inv[i + len2] - pre_inv[i + 1 - 1])) % M + M) % M; return ret; } int main() { scanf("%d%d%d", &n, &k, &M); math_pre(); solve(1, n, k); int ans = 0; for (map::pad *it = Map.begin(); it != Map.end(); ++it) { int len = it->key, cnt = it->val; ans = (ans + 1ll * cnt * len % M * (len - 1) % M * inv[4] % M) % M; } for (map::pad *it1 = Map.begin(); it1 != Map.end(); ++it1) for (map::pad *it2 = Map.begin(); it2 != Map.end(); ++it2) { if (it1 == it2) { int len = it1->key, cnt = 1ll * (0 + (it1->val - 1)) * it1->val / 2 % M; ans = (ans + 1ll * cnt * calc(len, len) % M) % M; } else if (it1->key < it2->key) { int len1 = it1->key, len2 = it2->key, cnt = 1ll * it1->val * it2->val % M; ans = (ans + 1ll * cnt * calc(len1, len2) % M) % M; } } printf("%d", ans); }
13
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int n, k, p, inv[N], res[2][2]; vector<int> segs, tmp; void dfs(int l, int r, int k) { if (l == r || k == 1) { segs.emplace_back(r - l + 1); tmp.emplace_back(r - l + 1); return; } int mid = (l + r) >> 1; dfs(l, mid, k - 1); dfs(mid + 1, r, k - 1); } int calc(int a, int b) { int ans = 0; for (int i = 3; i <= a + b; ++i) { int inf = max(i - b, 1), suf = min(a, i - 1); assert(inf <= suf); ans = (ans + 1ll * (suf - inf + 1) * (i - 2) % p * inv[i]) % p; } return 1ll * ans * inv[2] % p; } int main() { scanf("%d%d%d", &n, &k, &p); inv[0] = inv[1] = 1; for (int i = 2; i < N; ++i) inv[i] = 1ll * (p - p / i) * inv[p % i] % p; dfs(1, n, k); sort(tmp.begin(), tmp.end()); tmp.erase(unique(tmp.begin(), tmp.end()), tmp.end()); int ans = 0, m = tmp.size(); assert(m <= 2); for (int i = 0; i < m; ++i) for (int j = 0; j < m; ++j) res[i][j] = calc(tmp[i], tmp[j]); int cnt[2] = {0, 0}; for (int i = int(segs.size()) - 1; ~i; --i) { int cur = segs[i], ind = (cur != tmp[0]) ? 1 : 0; ans = (ans + 1ll * cur * (cur - 1) % p * inv[4]) % p; for (int j = 0; j < m; ++j) ans = (ans + 1ll * res[ind][j] * cnt[j]) % p; ++cnt[ind]; } printf("%d\n", ans); return 0; }
13
CPP
#include <bits/stdc++.h> const int Maxn = 100000; int n, k, Mod; int sum[Maxn + 5]; int inv[Maxn + 5]; void init() { inv[1] = 1; for (int i = 2; i <= Maxn; i++) { inv[i] = 1ll * (Mod - Mod / i) * inv[Mod % i] % Mod; } for (int i = 1; i <= Maxn; i++) { sum[i] = (sum[i - 1] + inv[i]) % Mod; } } int cnt[Maxn + 5]; void merge_sort(int l, int r, int k) { if (k == 1 || l == r) { cnt[r - l + 1]++; return; } int mid = (l + r) >> 1; merge_sort(l, mid, k - 1); merge_sort(mid + 1, r, k - 1); } int calc(int len_1, int len_2) { int ans = 0; for (int i = 1; i <= len_1; i++) { ans = (ans + 1ll * len_2 * inv[2]) % Mod; int now = (sum[i + len_2] - sum[i] + Mod) % Mod; ans = (ans - now + Mod) % Mod; } return ans; } int lis[Maxn + 5], lis_len; int solve() { int ans = 0; for (int i = 1; i <= n; i++) { if (cnt[i]) { lis[++lis_len] = i; ans = (ans + 1ll * cnt[i] * i * (i - 1) % Mod * inv[4]) % Mod; if (cnt[i] > 1) { ans = (ans + 1ll * cnt[i] * (cnt[i] - 1) % Mod * inv[2] % Mod * calc(i, i)) % Mod; } } } for (int i = 1; i <= lis_len; i++) { for (int j = i + 1; j <= lis_len; j++) { ans = (ans + 1ll * cnt[lis[i]] * cnt[lis[j]] % Mod * calc(lis[i], lis[j])) % Mod; } } return ans; } int main() { scanf("%d%d%d", &n, &k, &Mod); init(); merge_sort(1, n, k); printf("%d\n", solve()); return 0; }
13
CPP
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; template <typename T> void dprintln(const T &t) { cout << t << endl; } template <typename T, typename... Args> void dprintln(const T &t, const Args &...rest) { cout << t << ' '; dprintln(rest...); } template <typename T> void println(const T &t) { cout << t << '\n'; } template <typename T, typename... Args> void println(const T &t, const Args &...rest) { cout << t << ' '; println(rest...); } template <typename T> void print(const T &t) { cout << t << ' '; } template <typename T, typename... Args> void print(const T &t, const Args &...rest) { cout << t << ' '; print(rest...); } template <class T> void scan(T &t) { cin >> t; } template <class T, class... Args> void scan(T &a, Args &...rest) { cin >> a; scan(rest...); } using ll = long long; using vl = vector<ll>; using vi = vector<int>; using pii = pair<int, int>; using vb = vector<bool>; using vpii = vector<pii>; auto bet = [](const ll x, const ll y, const ll i) { return x <= i && i <= y; }; template <typename T1, typename T2> T1 ceil(T1 x, T2 y) { return (x + y - 1) / y; } inline int h_bit(unsigned int x) { return 31 - __builtin_clz(x); } inline int h_bitll(unsigned long long x) { return 63 - __builtin_clzll(x); } template <typename T> struct bit { vector<T> a; explicit bit(int n, int v = 0) { a.resize(n + 1); if (v != 0) { for (int i = 1; i <= n; ++i) a[i] = v; } } T sum(T x) { T res = 0; while (x) { res += a[x]; x -= x & -x; } return res; } T sum(int l, int r) { if (l > r) return 0; return sum(r) - sum(l - 1); } void add(int x, T v) { while (x < a.size()) { a[x] += v; x += x & -x; } } void clear() { fill(a.begin(), a.end(), 0); } }; vi get_prime(int n) { vi minp(n + 1), p; for (int i = 2; i <= n; i++) { if (!minp[i]) { minp[i] = i; p.push_back(i); } for (auto &x : p) { if (x <= minp[i] && x * i <= n) minp[x * i] = x; else break; } } return p; } const int mod = 998244353; inline void add_mod(ll &x, const ll &y) { x += y; if (x >= mod) x -= mod; } inline ll submod(ll x, ll y) { return x >= y ? x - y : x - y + mod; } void sub_mod(ll &x, const ll y) { x -= y; if (x < 0) x += mod; } template <typename T> using vv = vector<vector<T>>; template <typename T1, typename T2 = T1> using vp = vector<pair<T1, T2>>; template <typename T, int n> using va = vector<array<T, n>>; struct UF { vi par; explicit UF(int n) { par.assign(n + 1, 0); for (int i = (1); i < (n + 1); ++i) par[i] = i; } int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); } void unite(int x, int y) { par[find(x)] = find(y); } }; vi get_popcnt(int n) { vi res(n + 1); for (int i = (0); i < (n); ++i) { if (i * 2 <= n) res[i * 2] = res[i]; if ((i & 1) == 0) res[i + 1] = res[i] + 1; } return res; } const int N = 30, M = 1005; int l1 = 0, l2 = 0; int c1, c2; void dfs(int a, int h) { if (a == 0) return; if (h == 1 || a == 1) { if (l1 == 0) { l1 = a; ++c1; } else if (a == l1) ++c1; else { if (l2 == 0) l2 = a; ++c2; } return; } dfs(a / 2, h - 1), dfs(a - a / 2, h - 1); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int n, k, q; scan(n, k, q); vl inv(max(4, n) + 1); inv[1] = 1; for (int i = (2); i < (max(4, n) + 1); ++i) inv[i] = q - ll(q / i) * inv[q % i] % q; dfs(n, k); ll ans = 0; if (c1 > 0) ans += 1LL * c1 * (l1 - 1) % q * l1 % q * inv[4] % q; if (c2 > 0) ans += 1LL * c2 * (l2 - 1) % q * l2 % q * inv[4] % q; if (c1 > 1) { ll sum = 0; for (int i = (2); i < (2 * l1 + 1); ++i) { int mi = max(1, i - l1); int ma = min(l1, i - 1); sum += 1LL * (ma - mi + 1) * (inv[2] - inv[i] + q) % q; } ans += 1LL * c1 * (c1 - 1) / 2 % q * sum % q; } if (c2 > 1) { ll sum = 0; for (int i = (2); i < (2 * l2 + 1); ++i) { int mi = max(1, i - l2); int ma = min(l2, i - 1); sum += 1LL * (ma - mi + 1) * (inv[2] - inv[i] + q) % q; } ans += 1LL * c2 * (c2 - 1) / 2 % q * sum % q; } if (c1 > 0 && c2 > 0) { ll sum = 0; for (int i = (2); i < (l1 + l2 + 1); ++i) { int mi = max(1, i - l2); int ma = min(l1, i - 1); sum += 1LL * (ma - mi + 1) * (inv[2] - inv[i] + q) % q; } ans += 1LL * c1 * c2 % q * sum % q; } println(ans % q); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; int i, j, n, k, mo, rev[N], a[N], num, d[N], s[N], l, r; long long ans; void work(int l, int r, int k) { if (k <= 1 || l == r) { a[++num] = r - l + 1; return; } int m = (l + r) >> 1; work(l, m, k - 1), work(m + 1, r, k - 1); } int calc(int a, int b) { int i; long long S = (long long)a * b % mo; for (i = 1; i <= a; i++) S -= s[i + b] - s[i]; return S % mo; } int main() { scanf("%d%d%d", &n, &k, &mo); rev[0] = rev[1] = 1; for (i = 2; i <= n; i++) rev[i] = (long long)(mo - mo / i) * rev[mo % i] % mo; for (i = 1; i <= n; i++) s[i] = (s[i - 1] + rev[i]) % mo; for (i = 1; i <= n; i++) s[i] <<= 1; work(1, n, k); for (i = 1; i <= num; i++) d[a[i]]++; for (i = 1; i <= n; i++) if (d[i]) l = !l ? i : l, r = i; for (i = l; i <= r; i++) ans += ((long long)i * (i - 1) / 2 % mo * d[i] + (long long)d[i] * (d[i] - 1) / 2 % mo * calc(i, i)) % mo; if (l != r) ans += (long long)d[l] * d[r] % mo * calc(l, r) % mo; ans = ans % mo * (mo + 1) / 2 % mo; printf("%lld\n", (ans + mo) % mo); }
13
CPP
#include <bits/stdc++.h> using namespace std; const int N = 200005; int n, k, mod, q1, q2, ans; int inv[N], cnt[N]; void solve(int l, int r, int d) { if (d == 1 || l == r) { int len = r - l + 1; q1&& q1 != len ? q2 = len : q1 = len; ans = (ans + 1ll * len * (len - 1) / 2 % mod * (mod + 1) / 2) % mod; cnt[len]++; return; } int mid = (l + r) / 2; solve(l, mid, d - 1); solve(mid + 1, r, d - 1); } int main() { scanf("%d%d%d", &n, &k, &mod); inv[0] = inv[1] = 1; for (int i = (int)(2); i <= (int)(N - 1); i++) inv[i] = 1ll * inv[mod % i] * (mod - mod / i) % mod; for (int i = (int)(1); i <= (int)(N - 1); i++) inv[i] = (inv[i] + inv[i - 1]) % mod; solve(1, n, k); for (int i = (int)(1); i <= (int)(q1); i++) { int val = 1ll * (i - 1) * cnt[q1] % mod * (mod + 1) / 2 % mod; ans = (ans + 1ll * (cnt[q1] - 1) * (inv[i + q1] + mod - inv[i]) % mod * val) % mod; ans = (ans + 1ll * (cnt[q2] - 0) * (inv[i + q2] + mod - inv[i]) % mod * val) % mod; } for (int i = (int)(1); i <= (int)(q2); i++) { int val = 1ll * (i - 1) * cnt[q2] % mod * (mod + 1) / 2 % mod; ans = (ans + 1ll * (cnt[q1] - 0) * (inv[i + q1] + mod - inv[i]) % mod * val) % mod; ans = (ans + 1ll * (cnt[q2] - 1) * (inv[i + q2] + mod - inv[i]) % mod * val) % mod; } printf("%d\n", ans); }
13
CPP
#include <bits/stdc++.h> using namespace std; const long long N = 1e6 + 5; long long n, k, gs, ans, mod, len[N], jc[N], inv[N], sum[N], ycl[2005][2005]; inline long long read() { long long res = 0, f = 1; char c = getchar(); while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); } while (isdigit(c)) { res = res * 10 + c - 48; c = getchar(); } return res * f; } void build(long long l, long long r, long long h) { if (h <= 1 || l == r) { len[++gs] = r - l + 1; return; } long long mid = (l + r) / 2; build(l, mid, h - 1); build(mid + 1, r, h - 1); } long long C(long long n, long long m) { if (n < 0 || m < 0 || n < m) return 0; return jc[n] * inv[m] % mod * inv[n - m] % mod; } signed main() { n = read(); k = read(); mod = read(); jc[0] = jc[1] = inv[0] = inv[1] = 1; for (long long i = 2; i <= 100000; i++) jc[i] = jc[i - 1] * i % mod; for (long long i = 2; i <= 100000; i++) inv[i] = (mod - mod / i) * inv[mod % i] % mod; build(1, n, k); for (long long i = 1; i <= gs; i++) { ans = (ans + len[i] * (len[i] - 1) % mod * inv[4] % mod) % mod; } sort(len + 1, len + gs + 1); long long x = 0; for (long long i = 1; i <= gs; i++) if (len[i] == len[i - 1]) sum[x]++; else { len[++x] = len[i]; sum[x] = 1; } gs = x; for (long long i = 1; i <= gs; i++) for (long long j = 1; j <= i; j++) { long long res = 0; res = len[i] * len[j] % mod * inv[2] % mod; for (long long a = 1; a <= len[i] + len[j]; a++) { long long l = max(1ll, a - len[j]), r = min(a - 1, len[i]); res = (res - inv[a] * (r - l + 1) % mod + mod) % mod; } if (i == j) ans = (ans + res * sum[i] % mod * (sum[i] - 1 + mod) % mod * inv[2] % mod) % mod; else ans = (ans + res * sum[i] % mod * sum[j] % mod) % mod; } cout << ans % mod; return 0; }
13
CPP
#include <bits/stdc++.h> long long n, k, q, t1, t2, cnt1, cnt2, inv4, inv2, inv[200001]; long long ans; inline long long mul(const long long &a, const long long &b) { return 1ll * a * b % q; } inline long long qsm(long long a, long long b) { long long ans = 1; while (b) { if (b & 1) ans = mul(ans, a); a = mul(a, a); b >>= 1; } return ans; } void merge(long long l, long long r, long long k) { if (l < r) { if (k <= 1) { if (t1 == r - l + 1) ++cnt1; else if (t2 == r - l + 1) ++cnt2; else if (!t1) t1 = r - l + 1, cnt1 = 1; else t2 = r - l + 1, cnt2 = 1; (ans += mul(mul(r - l + 1, r - l), inv4)) %= q; return; } merge(l, (l + r) >> 1, k - 1); merge(((l + r) >> 1) + 1, r, k - 1); } else { { if (t1 == r - l + 1) ++cnt1; else if (t2 == r - l + 1) ++cnt2; else if (!t1) t1 = r - l + 1, cnt1 = 1; else t2 = r - l + 1, cnt2 = 1; (ans += mul(mul(r - l + 1, r - l), inv4)) %= q; return; } } } long long getans(long long n, long long m) { long long ans = 0; (ans += mul(inv2, mul(n, m))) %= q; for (long long i = 1; i <= n; i++) (ans += (inv[i] - inv[i + m] + q)) %= q; return ans; } signed main() { scanf("%I64d%I64d%I64d", &n, &k, &q); inv4 = qsm(4, q - 2); inv2 = (q + 1) >> 1; inv[0] = inv[1] = 1; for (long long i = 2; i <= n; i++) inv[i] = mul(q - q / i, inv[q % i]); for (long long i = 1; i <= n; i++) (inv[i] += inv[i - 1]) %= q; merge(1, n, k); (ans += mul(getans(t1, t1), mul(mul(cnt1, cnt1 - 1), inv2))) %= q; (ans += mul(getans(t2, t2), mul(mul(cnt2, cnt2 - 1), inv2))) %= q; (ans += mul(getans(t1, t2), mul(cnt1, cnt2))) %= q; printf("%I64d", ans); }
13
CPP
#include <bits/stdc++.h> using namespace std; void Freopen() { freopen( "title" ".in", "r", stdin); freopen( "title" ".out", "w", stdout); } int read() { int g = 0, f = 1; char ch = getchar(); while (ch < '0' || '9' < ch) { if (ch == '-') f = -1; ch = getchar(); } while ('0' <= ch && ch <= '9') { g = g * 10 + ch - '0'; ch = getchar(); } return g * f; } const int N = 1e5 + 5; int n, k, mod, l1, l2, c1, c2, inv[N], s[N]; int ksm(int x, int y) { int re = 1; for (; y; y >>= 1, x = 1ll * x * x % mod) if (y & 1) re = 1ll * re * x % mod; return re; } void solve(int l, int r, int h) { if (h == k || l == r) { if (!l1) l1 = r - l + 1, c1++; else if (r - l + 1 == l1) c1++; else if (!l2) l2 = r - l + 1, c2++; else c2++; return void(); } int mid = l + r >> 1; solve(l, mid, h + 1), solve(mid + 1, r, h + 1); } int calc(int l1, int l2) { if (!l1 || !l2) return 0; int re = 1ll * l1 * l2 % mod * inv[2] % mod; for (int i = (1); i <= (l1); i++) re = (1ll * re + mod - (s[i + l2] + mod - s[i]) % mod) % mod; return re; } signed main() { n = read(), k = read(), mod = read(); inv[1] = 1; for (int i = (2); i <= ((int)1e5); i++) inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod; for (int i = (1); i <= (n); i++) s[i] = (s[i - 1] + inv[i]) % mod; solve(1, n, 1); int ans = (1ll * c1 * l1 % mod * (l1 - 1) % mod * inv[4] % mod + 1ll * c2 * l2 % mod * (l2 - 1) % mod * inv[4] % mod) % mod; ans = (ans + 1ll * c1 * (c1 - 1) % mod * inv[2] % mod * calc(l1, l1) % mod) % mod; ans = (ans + 1ll * c2 * (c2 - 1) % mod * inv[2] % mod * calc(l2, l2) % mod) % mod; ans = (ans + 1ll * c1 * c2 % mod * calc(l1, l2) % mod) % mod; return cout << ans, signed(); }
13
CPP
#include <bits/stdc++.h> using namespace std; int n, K, P, inv[200003], cnt[200003], mn = 1e9, ans; void solve(int l, int r, int dep) { if (dep == 1 || l == r) { ++cnt[r - l + 1], mn = min(mn, r - l + 1); return; } int mid = (l + r) >> 1; solve(l, mid, dep - 1), solve(mid + 1, r, dep - 1); } int calc(int A, int B) { int res = 0; for (int i = 2; i <= A + B; ++i) res = (res + 1ll * (min(i - 1, A) - max(1, i - B) + 1) * (i - 2) % P * inv[i] % P) % P; return 1ll * res * inv[2] % P; } int main() { cin >> n >> K >> P; solve(1, n, K); inv[1] = 1; for (int i = 2; i <= 2 * n; ++i) inv[i] = P - 1ll * P / i * inv[P % i] % P; ans = 1ll * inv[2] * (1ll * (1ll * (mn) * ((mn)-1) / 2 % P) * cnt[mn] % P + 1ll * (1ll * (mn + 1) * ((mn + 1) - 1) / 2 % P) * cnt[mn + 1] % P) % P; ans = (ans + 1ll * (1ll * (cnt[mn]) * ((cnt[mn]) - 1) / 2 % P) * calc(mn, mn) % P) % P; ans = (ans + 1ll * (1ll * (cnt[mn + 1]) * ((cnt[mn + 1]) - 1) / 2 % P) * calc(mn + 1, mn + 1) % P) % P; ans = (ans + 1ll * cnt[mn] * cnt[mn + 1] % P * calc(mn, mn + 1) % P) % P; cout << ans; return 0; }
13
CPP
#include <bits/stdc++.h> #pragma GCC optimize "-O3" using namespace std; long long MOD; long long pw(long long a, long long b) { long long ans = 1; while (b) { while (!(b & 1)) b >>= 1, a = (a * a) % MOD; ans = (ans * a) % MOD, --b; } return ans; } map<int, int> mm; void run(int n, int k) { if (n <= 0) return; if (n == 1) { ++mm[1]; return; } if (k <= 1) { ++mm[n]; return; } int n2 = n / 2; run(n2, k - 1); run(n - n2, k - 1); } long long n, k; long long get(int a, int b) { if (a > b) swap(a, b); long long ans = 0; for (int i = 0; i <= (a - 1) + (b - 1); ++i) { long long cnt = 0; if (i < a) cnt = i + 1; else if (i < b) cnt = a; else cnt = i + 1 - (i + 1 - a) - (i + 1 - b); ans = (ans + cnt * pw(i + 2, MOD - 2)) % MOD; } return ans; } int main() { ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n >> k >> MOD; run(n, k); vector<pair<int, long long>> vv; for (auto x : mm) vv.push_back(x); long long ans = n * (n - 1) * pw(4, MOD - 2); long long sum = 0; for (int i = 0; i < vv.size(); ++i) { long long go = get(vv[i].first, vv[i].first); sum = (sum + go * vv[i].second % MOD * (vv[i].second - 1) % MOD * pw(2, MOD - 2)) % MOD; for (int j = i + 1; j < vv.size(); ++j) sum = (sum + get(vv[i].first, vv[j].first) * vv[i].second % MOD * vv[j].second) % MOD; } ans = (ans - sum + MOD) % MOD; cout << ans << "\n"; return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; void read(int &x) { char ch; int fu = 1; while ((ch = getchar()) <= 32) ; x = 0; if (ch == '-') fu = -1; else x = ch - 48; while ((ch = getchar()) > 32) x = x * 10 + ch - 48; x *= fu; } void read(long long &x) { char ch; int fu = 1; while ((ch = getchar()) <= 32) ; x = 0; if (ch == '-') fu = -1; else x = ch - 48; while ((ch = getchar()) > 32) x = x * 10 + ch - 48; x *= fu; } const double pi = acos(-1); void upmax(int &a, int b) { if (a < b) a = b; } void upmin(int &a, int b) { if (a > b) a = b; } const int N = 200220, inf = 1000000000; int n, k, p, ans; int a[N], inv[N], s[N]; void get(int n, int k) { if (k == 1 || n == 1) { a[n]++; return; } get(n / 2, k - 1); get((n + 1) / 2, k - 1); } int calc(int n, int m) { int res = 1LL * n * m % p * inv[2] % p; for (int i = 1; i <= m; i++) res = (0LL + res - s[n + i] + s[i]) % p; return res; } int main() { scanf("%d%d%d", &n, &k, &p); inv[1] = 1; for (int i = 2; i <= max(n * 2, 4); i++) inv[i] = -1LL * inv[p % i] * (p / i) % p; for (int i = 1; i <= n * 2; i++) s[i] = (s[i - 1] + inv[i]) % p; get(n, k); for (int i = 1; i <= n; i++) if (a[i]) { ans = (ans + 1LL * i * (i - 1) % p * inv[4] % p * a[i]) % p; } ans = (ans + p) % p; for (int i = 1; i <= n; i++) if (a[i]) ans = (ans + 1LL * a[i] * (a[i] - 1) % p * inv[2] % p * calc(i, i)) % p; for (int i = 1; i <= n; i++) if (a[i]) for (int j = i + 1; j <= n; j++) if (a[j]) ans = (ans + 1LL * a[i] * a[j] % p * calc(i, j)) % p; ans = (ans + p) % p; printf("%d\n", ans); return 0; }
13
CPP
#include <bits/stdc++.h> using namespace std; int n, k, p, to[210000], pre[210000], ans, inv[2100000], inv2; vector<int> V; int Pow(int x, int y) { int ans = 1; for (int i = 1; i <= y; i *= 2, x = 1LL * x * x % p) if (i & y) ans = 1LL * ans * x % p; return ans; } void dfs(int l, int r, int h) { if (l < r) { if (h <= 1) { ans = (ans + 1LL * (r - l + 1) * (r - l) / 2 % p * Pow(2, p - 2)) % p; V.push_back(r - l + 1); } else { int m = (l + r) / 2; dfs(l, m, h - 1); dfs(m + 1, r, h - 1); } } else { V.push_back(1); } } int main() { scanf("%d%d%d", &n, &k, &p); dfs(1, n, k); map<int, int> M; for (auto y : V) { M[y] += 1; } inv2 = Pow(2, p - 2); for (int i = 1; i <= n; i++) inv[i] = Pow(i, p - 2); for (auto it : M) for (auto it2 : M) { int fac; if (it.first == it2.first) fac = 1LL * it.second * (it.second - 1) % p; else fac = 1LL * it.second * it2.second % p; int tmp = 0; for (int l = 2; l <= it.first + it2.first; l++) { int low = max(l - it2.first, 1) - 1; int hi = min(l - 1, it.first) - 1; tmp = (tmp + 1LL * (low + hi) * (hi - low + 1) / 2 % p * inv[l] % p * inv2) % p; } ans = (ans + 1LL * tmp * fac) % p; } printf("%d\n", ans); }
13
CPP
n = int(input()) A = [] for i in range(n): A = A+[input().split()] for a in A: if int(a[2]) < int(a[0]) or int(a[2]) > int(a[1]): print(a[2]) else: print(int(a[2])*(int(a[1])//int(a[2])+1))
7
PYTHON3
import math for _ in range(int(input())): l,r,d = map(int,input().split()) if l>d: print(d) elif r<d: print(d) elif r==d: print(d*2) elif r%d !=0: print(math.ceil(r/d)*d) else: print(r+d)
7
PYTHON3
for k in range(int(input())): l,r,d = map(int,input().split()) if d==1: if l==1: print(r+1) else: print(1) else: if l>=d: if l%d==0: if l//d>1: print(d) else: print(((r//d)+1)*d) else: print(d) else: print(((r//d)+1)*d)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; long long f[1000001]; long long pow(long long a, long long b, long long MOD) { long long x = 1, y = a; while (b > 0) { if (b % 2 == 1) { x = (x * y); if (x > MOD) x %= MOD; } y = (y * y); if (y > MOD) y %= MOD; b /= 2; } return x; } long long InverseEuler(long long n, long long MOD) { return pow(n, MOD - 2, MOD); } long long C(long long n, long long r, long long MOD) { return (f[n] * ((InverseEuler(f[r], MOD) * InverseEuler(f[n - r], MOD)) % MOD)) % MOD; } int main() { int q; cin >> q; while (q--) { long long l, r, d; cin >> l >> r >> d; long long ans1 = d; long long y = r / d; long long ans2 = (y + 1) * d; if (d >= l) cout << ans2 << endl; else cout << ans1 << endl; } }
7
CPP
#include <bits/stdc++.h> using namespace std; int l, r, d, n; int main() { ios::sync_with_stdio(false); cin >> n; while (n--) { cin >> l >> r >> d; if (d > r || d < l) { cout << d << endl; continue; } cout << d * (r / d + 1) << endl; } return 0; }
7
CPP
t = int(input()) for i in range(t): l, r, d = map(int, input().split()) if l > d: print(d) else: print(((r+d)//d) * d)
7
PYTHON3
def func(l,r,d): if l<=d: print((r//d+1)*d) else: print(d) def main(): count=int(input()) for _ in range(count): arr=input().split() func(int(arr[0]),int(arr[1]),int(arr[2])) main()
7
PYTHON3
q = int(input()) for i in range(q): inputs = input().split() l = int(inputs[0]) r = int(inputs[1]) d = int(inputs[2]) if l>d: print(d) else: x = r//d print(d*(x+1))
7
PYTHON3
n= int(input()) for i in range(n): a,b,c = map(int,input().split()) if c>b: print(c) elif c<a: print(c) else: print(((b//c)+1)*c)
7
PYTHON3
n = int(input()) for i in range(n): l,r,d = map(int,input().split()) '''a = l // d b = r // d ans1 = a*d ans2 = b*d if ans1 < l and ans1 != 0: print(ans1) else: if ans2 > r: print(ans2) else: print(ans2+d) ''' if d < l: print(d) else: print((r//d+1)*d)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; const long long N = 1e5, N2 = 2e5, N1 = 1e6, M = 1e2; long long binpow(long long a, long long n) { long long ans = 1; while (n) { if (n & 1) ans *= a; a *= a; n >>= 1; } return ans; } string s, s1, s2; long long n, m, t, x, y, z; long long k, k1, k2, g, g1, g2, ans, kol, kol1; char c; long long mxx = 0, mxy = 0; int main() { cin.tie(0); cin >> t; while (t--) { long long l, r, d; cin >> l >> r >> d; long long d1 = d; if (d < l) cout << d << endl; else if (r % d == 0) cout << r + d << endl; else { cout << d * (r / d + 1) << endl; } } return 0; }
7
CPP
import sys q = int(input()) for i in range(q): l, r, d = map(int, sys.stdin.readline().split()) ans = 0 if l > d: ans = d else: ans = r + d - r%d print(ans)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; bool com(long long a, long long b) { return a < b; } int main() { long long t; cin >> t; while (t--) { long long l, r, d; cin >> l >> r >> d; if (d < l) cout << d << "\n"; else { if (r % d == 0) cout << r + d << "\n"; else cout << r + d - (r % d) << "\n"; } } }
7
CPP
#include <bits/stdc++.h> using namespace std; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } int main() { int q; cin >> q; while (q--) { long long a, b, c; cin >> a >> b >> c; long long ans; if (b % c == 0) ans = b + c; else { ans = ((b / c) + 1) * c; } if (c < a && c > 0) ans = min(ans, c); cout << ans << "\n"; } }
7
CPP
for u in range(int(input())): l,r,d=map(int,input().split()) if(d<l): print(d) elif(d>=l and d<=r): print((r//d+1)*d) elif(d>r): print(d)
7
PYTHON3
n = int(input()) for i in range(n): l,r,d = map(int, input().strip().split()) if d < l: print(d) else: print((r // d + 1) * d)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; while (q--) { long long int l, r, d; cin >> l >> r >> d; if (l / d == 0) { long long int x = r / d; cout << (x + 1) * d << endl; } else if (l / d == 1 && l % d == 0) { cout << (r / d + 1) * d << endl; } else cout << d << endl; } }
7
CPP
q = int(input()) for i in range(q): entrada = list(map(int,input().split())) if (entrada[2]) < (entrada[0]): print(entrada[2]) else: saida = ((entrada[1]) // (entrada[2]) + 1) print(saida*(entrada[2]))
7
PYTHON3
from sys import stdin q = int(stdin.readline()) for i in range(q): R = lambda: map(int, stdin.readline().split()) l, r, d = R() small = (l-1) // d large = r // d if small * d < l and small * d > 0: print(d) else: print((large + 1) * d)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { int q; scanf("%d", &q); while (q--) { int l, r, d; scanf("%d %d %d", &l, &r, &d); if (d < l) printf("%d\n", d); else if (d > r) printf("%d\n", d); else { int res = r - d; res = res % d; printf("%d\n", r + d - res); } } }
7
CPP
#include <bits/stdc++.h> using namespace std; int main() { int q; cin >> q; while (q--) { long long int l, r, d; cin >> l >> r >> d; long long int ans, i, flag = 1; if (d < l) ans = d; else if (r % d == 0) { ans = (ceil((long double)r / d) + 1) * d; } else { ans = ceil((long double)r / d) * d; } cout << ans << endl; } }
7
CPP
q=int(input()) for i in range(q): l,r,d=map(int,input().split()) k=1 x=0 if (d<l)or(d>r): print(d) else: kol=r//d+1 print(d*kol)
7
PYTHON3
t=int(input()) while t>0: l,r,d=[int(x) for x in input().split()] if(d<l or d>r): print(d) else: print(((r//d)*d)+d) t-=1
7
PYTHON3
n=int(input()) while n!=0: n=n-1 str_len=input() l=int(str_len.split(' ')[0]) r = int(str_len.split(' ')[1]) d = int(str_len.split(' ')[2]) # l=int(input()) # r=int(input()) # d=int(input()) if d not in range(l,r+1): print(d) else: print(int(int((r/d+1))*d))
7
PYTHON3
from math import * q = int(input()) print() for i in range(q): s = input() left, r, d = s.split() # split string input left = float(left) r = float(r) d = float(d) if (left / d) > 1 or (r / d) < 1: print(int(d)) else: print(int(d * (floor(r / d) + 1)))
7
PYTHON3
q = int(input()) for i in range(q): l, r, d = map(int, input().split()) if d < l: print(d) else: print(r + d - r % d)
7
PYTHON3
for _ in range(int(input())): a,b,c = map(int, input().split()) print(c if c < a else (b//c+1)*c)
7
PYTHON3
for _ in range(int(input())): l,r,d = map(int,input().split()) if l>d or r<d: print(d) else: t = r//d print((t+1)*d)
7
PYTHON3
#include <bits/stdc++.h> using namespace std; int main() { long long n; cin >> n; while (n--) { long long l, r, k; cin >> l >> r >> k; if (r - k <= r - l && k <= r) { cout << r + k - (r % k) << endl; } else { cout << k << endl; } } return 0; }
7
CPP
rd = lambda: list(map(int, input().split())) for _ in range(rd()[0]): l, r, d = rd() print(d if not (l <= d <= r) else r // d * d + d)
7
PYTHON3
# int(input()) # [int(s) for s in input().split()] # input() def solve(): q = int(input()) for i in range(q): l, r, d = [int(s) for s in input().split()] if d < l: print(d) else: print(r+(d-r%d)) if __name__ == "__main__": solve()
7
PYTHON3
q = int(input()) for i in range(q): l, r, d = map(int, input().split()) if d < l or d > r: print(d) else: print((r//d)*d+d)
7
PYTHON3