solution
stringlengths 11
983k
| difficulty
int64 0
21
| language
stringclasses 2
values |
---|---|---|
#include <bits/stdc++.h>
inline int min(int x, int y) { return x < y ? x : y; }
inline int getch() {
int ch = getchar();
while (!isdigit(ch)) ch = getchar();
return ch;
}
int n;
int main() {
scanf("%d", &n);
int NOE = 0;
for (register int i = 1; i <= n; ++i) {
int ch = getch();
if (ch == '8') ++NOE;
}
printf("%d\n", min(NOE, n / 11));
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int32_t main() {
long long m;
long long n, k = 0, k1 = 0, k0 = 0, sm = 0, mn = 1e5, sm_original = 0, f = 0,
k2 = 0, t, mx = -1;
string s;
cin >> n;
cin >> s;
for (long long i = 0; i < s.size(); i++)
if (s[i] == '8') k++;
n = s.size() / 11;
cout << min(n, k);
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int an = 0;
char a[100];
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == '8') {
an++;
}
}
int m = n / 11;
cout << min(m, an);
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count() *
((uint64_t) new char | 1));
long long get_random_int(long long a, long long b) {
return uniform_int_distribution<long long>(a, b)(rng);
}
int main() {
ios_base::sync_with_stdio(false);
cout << fixed;
cout.precision(10);
int n;
cin >> n;
string s;
cin >> s;
int cnt8 = 0;
int cntr = 0;
for (int i = 0; i < n; ++i) {
if (s[i] == '8') {
cnt8++;
} else {
cntr++;
}
}
int res = 0;
for (int i = cnt8; i >= 0; --i) {
res = max(res, min(i, cntr / 10));
cntr++;
}
cout << res;
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long n;
string s;
long long cnt[10];
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
cin >> s;
for (long long(i) = (0); (i) <= (n - 1); (i)++) cnt[s[i] - '0']++;
cout << min(n / 11, cnt[8]) << endl;
return 0;
}
| 7 | CPP |
n = int(input())
s = input()
c, nph = s.count('8'), n//11
print(min(c,nph)) | 7 | PYTHON3 |
n=int(input())
s=input()
num_eight=0
for i in range(n):
if s[i]=='8':
num_eight+=1
phone_num=n//11
if num_eight<=phone_num:
print(num_eight)
else:
print(phone_num) | 7 | PYTHON3 |
long = int(input())
ch = input()
nb = 0
for i in range(0, long):
if ch[i] == '8':
nb += 1
div=long//11
if long>=11:
print(min(div,nb))
else:
print(0) | 7 | PYTHON3 |
n = int(input())
s = input()
m = s.count("8")
n
if m == 0 or n < 11:
print(0)
else:
lo,hi = 1,m
while lo <= hi:
mid = (lo+hi) // 2
if mid * 10 <= n - mid:
lo = mid + 1
else:
hi = mid - 1
print(hi)
| 7 | PYTHON3 |
n=int(input())
s=input()
eight=s.count('8')
print(min(eight,int(n//11))) | 7 | PYTHON3 |
x=int(input())
s=input()
d=x//11
e=s.count('8')
print(min(e,d))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, ans;
char str[1000];
int main() {
while (cin >> n) {
cin >> str;
ans = 0;
for (int i = 0; i < n; ++i)
if (str[i] == '8') ans++;
ans = min(ans, n / 11);
cout << ans << endl;
}
return 0;
}
| 7 | CPP |
n=int(input())
s=input()
k=s.count('8')
if(n//11<=k):
print(n//11)
else:
print(k)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, num, ans, cnt = 0;
char x;
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> x;
if (x == '8') num++;
}
for (int i = 1; i <= num; i++) {
for (int j = 1; j <= 11; j++) cnt++;
if (cnt > n) {
ans = i - 1;
break;
}
if (cnt == n) {
ans = i;
break;
}
}
if (cnt < n) ans = num;
cout << ans;
return 0;
}
| 7 | CPP |
n = int(input())
s = input()
if n >= 11:
print(min(s.count('8'), n // 11))
else:
print(0) | 7 | PYTHON3 |
n = int(input())
m = input()
t = 0
for i in range (len(m)):
if (m[i]=='8'):
t=t+1
if (n//11<=t):
print (n//11)
print ()
else:
print (t)
print ()
| 7 | PYTHON3 |
def counter(cards, n):
if(n<11):
return 0
else:
max_by_len = n // 11
count_of_eight = 0
for c in cards:
if c == '8':
count_of_eight += 1
if count_of_eight >= max_by_len:
break
return count_of_eight
def stand_input():
n = int(input())
cards = input()
res = counter(cards, n)
print(res)
stand_input() | 7 | PYTHON3 |
n=int(input())
c=0
x=input()
for i in range(len(x)):
if x[i]=='8':
c+=1
if c==0:
#print (0)
print(0)
else:
a=[n//11,c]
print(min(a))
| 7 | PYTHON3 |
import math
n=int(input())
s=input()
c=0
d=s.count('8')
print(min(n//11,d)) | 7 | PYTHON3 |
a=int(input())
s=str(input())
l=list(s)
k=l.count('8')
m=min(int(a/11),k)
print(m) | 7 | PYTHON3 |
a=input()
b=input()
cont=0
i=0
while(i<int(a) and cont<=int(a)//11-1):
if b[i]=="8":
cont=cont+1
i=i+1
print(cont) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, count = 0;
string s;
cin >> n >> s;
for (int i = 0; i < n; i++) {
if (s[i] == '8') count++;
}
if (n < 11)
cout << "0" << endl;
else {
cout << min(count, n / 11) << endl;
}
return 0;
}
| 7 | CPP |
import collections
def solve():
n = int(input())
cards = collections.Counter(input())
eights = cards['8']
ans: int = min(eights, n // 11)
print(ans)
if __name__ == '__main__':
solve()
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int n, con;
char a[100000];
int main() {
scanf("%d", &n);
scanf("%s", a);
for (int i = 0; i < n; ++i)
if (a[i] == '8') con++;
printf("%d\n", min(con, n / 11));
return 0;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
signed main() {
int n, cnt = 0;
char s[105];
cin >> n;
scanf("%s", s);
for (int i = 0; i < n; i++) cnt += s[i] == '8';
cout << min(cnt, n / 11);
}
| 7 | CPP |
n=int(input())
number=str(input())
ndiv=int(n/11)
if(n<11):
print('0')
else:
nume=number.count("8")
if nume>=ndiv:
print(ndiv)
else :
print(nume) | 7 | PYTHON3 |
#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int inf = 0x3f3f3f3f;
string s;
int len;
int fac[101];
inline char nc() {
static char buf[100000], *p1 = buf, *p2 = buf;
if (p1 == p2) {
p2 = (p1 = buf) + fread(buf, 1, 100000, stdin);
if (p1 == p2) return EOF;
}
return *p1++;
}
inline void read(int &x) {
char c = getchar();
int b = 1;
for (; c < '0' || c > '9'; c = getchar())
if (c == '-') b = -1;
for (x = 0; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = getchar())
;
x = x * b;
}
inline void read(long long &x) {
char c = getchar();
int b = 1;
for (; c < '0' || c > '9'; c = getchar())
if (c == '-') b = -1;
for (x = 0; c >= '0' && c <= '9'; x = x * 10 + c - '0', c = getchar())
;
x = x * b;
}
int main(int argc, char *argv[]) {
int flag = 0, sum = 0;
read(len);
cin >> s;
for (int i = (0); i <= (len - 1); i++) {
if (s[i] == '8') flag = 1, sum++;
}
if (!flag || len <= 10) {
printf("0\n");
return 0;
}
for (int i = (sum); i >= (1); i--)
if (i + i * 10 <= len) {
printf("%d\n", i);
return 0;
}
return 0;
}
| 7 | CPP |
n = int(input())
s = input()
n8 = 0
for x in s:
if x == '8':
n8 += 1
print(min(n8, n // 11)) | 7 | PYTHON3 |
n = int(input())
lst = list(input())
print(min(lst.count('8'), n // 11))
| 7 | PYTHON3 |
n = int(input())
num = int(input())
p = 0
count = 0
c = 0
a=[0]
while n != 0:
if (num % 10) == 8:
p += 1
c-=1
num //= 10
c += 1
n-=1
#print(c,p)
while c >= 10 and p > 0:
c -= 10
p -= 1
count += 1
#print(c,p)
if p>0:
count=count+(c+p)//11
print(count)
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
int coun = 0;
string s;
cin >> s;
for (int i = 0; i < n; i += 1) {
if (s[i] == '8') coun++;
}
cout << min(coun, n / 11);
return 0;
}
| 7 | CPP |
n=int(input())
s=list(str(input()))
e,m=0,0
if '8' not in s or n<11:
print(0)
else:
e=s.count('8')
m=n//11
print(min(e,m)) | 7 | PYTHON3 |
n = int(input())
s = input()
eights = s.count('8')
print(min(eights, len(s) // 11))
| 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
string s;
cin >> s;
int count = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '8') {
count++;
}
}
if (n == 11 && count >= 1) {
cout << 1 << "\n";
} else {
int a = n / 11;
if (count >= a) {
cout << a << "\n";
} else {
cout << count << "\n";
}
}
return 0;
}
| 7 | CPP |
n=int(input())
s=input()
c=0
for i in range(len(s)):
if(s[i]=='8'):
c=c+1
a=n//11
print(min(c,a))
| 7 | PYTHON3 |
# A. Phone Numbers
n=int(input())
s=input()
m=s.count("8")
ans=min(m,n//11)
print(ans) | 7 | PYTHON3 |
from collections import Counter
n = int(input())
c = Counter(input())
if '8' in list(c):
print(min(n//11,c['8']))
else:
print(0) | 7 | PYTHON3 |
#include <bits/stdc++.h>
using namespace std;
int main() {
int n, count = 0;
char st[100];
cin >> n;
cin >> st;
if (n >= 11) {
for (int i = 0; i <= n; i++) {
if (st[i] == '8')
count++;
else
continue;
}
if (count == 0) {
count = 0;
} else if (count != 0 && n / count >= 11) {
count = count;
} else {
count = n / 11;
}
}
cout << count;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T& x) {
x = 0;
T f = 1;
char ch = getchar();
while (!isdigit(ch)) f = ch == '-' ? -f : f, ch = getchar();
while (isdigit(ch)) x = x * 10 + ch - '0', ch = getchar();
x *= f;
}
template <typename T>
void viet(T x) {
if (x > 9) viet(x / 10);
putchar(x % 10 + '0');
}
int n, cnt, ret;
string s;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> s;
for (int i = 0; i < s.size(); i++) {
if (s[i] == '8') cnt++;
}
n -= cnt;
int foo = min(cnt, n / 10);
ret += foo;
cnt -= foo;
n -= foo * 10;
if (cnt > 0) ret += (cnt + n) / 11;
cout << ret << endl;
}
| 7 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1123456;
template <typename T>
T sqr(T x) {
return x * x;
}
template <typename T>
void vout(T s) {
cout << s << "\n";
exit(0);
}
long long bp(long long a, long long n) {
long long res = 1;
while (n) {
if (n % 2) res *= a;
a *= a;
n >>= 1;
}
return res;
}
long long f(long long x) {
long long res = 0;
while (x) {
res += x % 10;
x /= 10;
}
return res;
}
long long a[MAXN];
int main() {
ios_base ::sync_with_stdio(0);
cin.tie(0);
long long n, kol = 0;
cin >> n;
for (int i = 0; i < n; i++) {
char cc;
cin >> cc;
if (cc == '8') kol++;
}
cout << min(kol, n / 11) << "\n";
cerr << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec"
<< "\n";
return 0;
}
| 7 | CPP |
n = int(input())
s = input()
n8 = 0
for i in range(n):
if s[i] == '8':
n8 += 1
ans = 0
for i in range(n8, 0, -1):
if (n - i) // 10 >= i:
ans = i
break
print(ans)
| 7 | PYTHON3 |
#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;
long long mod = 998244353;
const long long N = 1e5 + 5;
inline long long read() {
long long x = 0, f = 1;
char ch = getchar();
while ((ch > '9' || ch < '0')) {
if (ch == '-') f = -1;
ch = getchar();
}
while ('0' <= ch && ch <= '9') x = x * 10 + (ch ^ 48), ch = getchar();
return x * f;
}
inline long long ksm(long long x, long long y = mod - 2, long long z = mod) {
long long ret = 1;
while (y) {
if (y & 1LL) ret = ret * x % mod;
x = x * x % mod;
y >>= 1LL;
}
return ret;
}
long long inv[N], sum[N];
void init(long long n) {
inv[1] = 1;
for (register signed i = 2; i <= n; i++)
inv[i] = inv[mod % i] * (mod - mod / i) % mod;
for (register signed i = 1; i <= n; i++) sum[i] = (sum[i - 1] + inv[i]) % mod;
}
long long k, n, ans;
map<long long, long long> S;
void MS(long long l, long long r, long long h) {
if (h == k || l == r) {
S[r - l + 1]++;
return;
}
long long mid = (l + r) >> 1;
MS(l, mid, h + 1);
MS(mid + 1, r, h + 1);
}
long long calc(long long x, long long y) {
long long res = x * y % mod;
for (register signed i = 1; i <= x; ++i)
res -= (sum[i + y] - sum[i]) * 2, res %= mod;
return (res % mod + mod) % mod;
}
signed main() {
n = read();
k = read();
mod = read();
init(n);
MS(1, n, 1);
for (map<long long, long long>::iterator X = S.begin(); X != S.end(); X++) {
long long x = X->first, y = X->second;
ans += x * (x - 1) % mod * inv[2] % mod * y % mod;
ans %= mod;
ans += y * (y - 1) % mod * inv[2] % mod * calc(x, x) % mod;
ans %= mod;
}
for (map<long long, long long>::iterator X = S.begin(); X != S.end(); X++)
for (map<long long, long long>::iterator Y = S.begin(); Y != S.end(); Y++) {
long long x = X->first, y = Y->first, a = X->second, b = Y->second;
if (x >= y) continue;
ans += calc(x, y) * a % mod * b % mod;
ans %= mod;
}
ans = ans * inv[2] % mod;
ans += mod;
ans %= mod;
cout << ans << '\n';
}
| 13 | CPP |
#include <bits/stdc++.h>
const int N = 200005;
int n, k, q, mod, cnt[2];
int sH[N], ans;
void up(int &x, int y) { x += y - mod, x += x >> 31 & mod; }
void up(int &x, int y, int z) { x = (x + (long long)y * z) % mod; }
int c(int n) { return (long long)n * (n - 1) / 2 % mod; }
void solve(int n, int m) {
if (m == 1 || n == 1) return void(++cnt[n - q]);
solve(n >> 1, m - 1), solve(n + 1 >> 1, m - 1);
}
int f(int a, int b) {
return ((long long)a * b % mod * (mod + 1 >> 1) + mod + sH[a] + sH[b] -
sH[a + b]) %
mod;
}
int main() {
std::ios::sync_with_stdio(0), std::cin.tie(0);
std::cin >> n >> k >> mod;
q = n >> std::min(k - 1, 20), solve(n, k);
sH[1] = 1;
for (int i = 2; i <= n; ++i)
sH[i] = (long long)(mod - mod / i) * sH[mod % i] % mod;
for (int i = 2; i <= n; ++i) up(sH[i], sH[i - 1]);
for (int i = 2; i <= n; ++i) up(sH[i], sH[i - 1]);
up(ans, (long long)c(q) * (mod + 1 >> 1) % mod, cnt[0]);
up(ans, (long long)c(q + 1) * (mod + 1 >> 1) % mod, cnt[1]);
up(ans, f(q, q), c(cnt[0]));
up(ans, f(q + 1, q + 1), c(cnt[1]));
up(ans, f(q, q + 1), (long long)cnt[0] * cnt[1] % mod);
std::cout << ans << '\n';
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
int md;
inline void add(int &a, int b) {
a += b;
if (a >= md) a -= md;
}
inline void sub(int &a, int b) {
a -= b;
if (a < 0) a += md;
}
inline int mul(int a, int b) { return (int)((long long)a * b % md); }
inline int power(int a, long long b) {
int res = 1;
while (b > 0) {
if (b & 1) {
res = mul(res, a);
}
a = mul(a, a);
b >>= 1;
}
return res;
}
inline int inv(int a) {
a %= md;
if (a < 0) a += md;
int b = md, u = 0, v = 1;
while (a) {
int t = b / a;
b -= t * a;
swap(a, b);
u -= t * v;
swap(u, v);
}
assert(b == 1);
if (u < 0) u += md;
return u;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k >> md;
if (k >= 20 || n <= (1 << (k - 1))) {
cout << 0 << '\n';
return 0;
}
int bc = (1 << (k - 1));
int small_size = n / bc;
int big_size = small_size + 1;
int big_cnt = n % bc;
int small_cnt = bc - big_cnt;
vector<int> blocks(bc);
for (int i = 0; i < n; i++) {
blocks[i % (int)blocks.size()]++;
}
map<int, int> mp;
for (int x : blocks) {
mp[x]++;
}
vector<int> fact(n + 1), inv_fact(n + 1);
fact[0] = inv_fact[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = mul(fact[i - 1], i);
inv_fact[i] = inv(fact[i]);
}
int ans = 0;
for (int b1id = 0; b1id < bc; b1id++) {
int b = blocks[b1id];
add(ans, mul(mul(b, b - 1), inv(4)));
}
vector<int> sum_inv(n + 1);
for (int i = 0; i < n; i++) {
sum_inv[i + 1] = sum_inv[i];
add(sum_inv[i + 1], inv(i + 1));
}
for (int b1id = 0; b1id < bc; b1id++) {
int b1 = blocks[b1id];
if (b1 == small_size)
small_cnt--;
else
big_cnt--;
for (int x = 2; x <= b1; x++) {
if (small_cnt > 0) {
int aux = sum_inv[x + small_size];
sub(aux, sum_inv[x]);
int prob = mul(x - 1, aux);
add(ans, mul(small_cnt, mul(prob, inv(2))));
}
if (big_cnt > 0) {
int aux = sum_inv[x + big_size];
sub(aux, sum_inv[x]);
int prob = mul(x - 1, aux);
add(ans, mul(big_cnt, mul(prob, inv(2))));
}
}
if (b1 == small_size)
small_cnt++;
else
big_cnt++;
}
cout << ans << '\n';
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using std::cerr;
using std::endl;
const int N = 1e5 + 10;
int n, K, P, inv[N], sum[N], ans;
std::map<int, int> map;
void divide(int l, int r, int dep) {
if (l == r || dep == K) {
return ++map[r - l + 1], void();
}
int mid = (l + r) >> 1;
divide(l, mid, dep + 1);
divide(mid + 1, r, dep + 1);
}
inline long long calc(int x, int y) {
int ret = 1ll * x * y % P * inv[2] % P;
for (int i = 1; i <= x; ++i) ret = (ret - sum[i + y] + sum[i]) % P;
ret = (ret % P + P) % P;
return ret;
}
int main() {
scanf("%d %d %d", &n, &K, &P);
divide(1, n, 1);
inv[1] = sum[1] = 1;
for (int i = 2, lim = std::max(4, n); i <= lim; ++i) {
inv[i] = P - 1ll * P / i * inv[P % i] % P;
sum[i] = (sum[i - 1] + inv[i]) % P;
}
for (auto m : map) {
ans = (ans + 1ll * m.first * (m.first - 1) % P * inv[4] % P * m.second) % P;
ans = (ans + calc(m.first, m.first) * m.second % P * (m.second - 1) % P *
inv[2]) %
P;
}
for (auto m1 : map)
for (auto m2 : map)
if (m1.first < m2.first)
ans = (ans + calc(m1.first, m2.first) * m1.second % P * m2.second) % P;
std::cout << ans << '\n';
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
using ull = uint64_t;
using ll = int64_t;
using ld = long double;
int mod;
ll pw(ll a, int b) {
if (!b) {
return 1;
}
ll v = pw(a, b / 2);
v = (v * v) % mod;
if (b & 1) {
v = (v * a) % mod;
}
return v;
}
ll ans = 0;
ll norm(ll x) {
x %= mod;
if (x < 0) {
x += mod;
}
return x;
}
const int N = 300228;
ll invs[N];
void go(ll mul, int a, int b) {
mul %= mod;
ll cans = 0;
for (int i = 2; i <= a + b; ++i) {
int al = max(1, i - b);
int ar = min(a, i - 1);
if (al <= ar) {
ll c = ar - al + 1;
cans += c * invs[i];
cans %= mod;
}
}
ans = (ans + cans * mul) % mod;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cout.setf(ios::fixed);
cout.precision(20);
int n, k;
cin >> n >> k >> mod;
for (int i = 1; i <= n; ++i) {
invs[i] = pw(i, mod - 2);
}
int c = 1;
for (int i = 1; i < k; ++i) {
c *= 2;
if (c >= n) {
cout << 0 << "\n";
return 0;
}
}
int sz = n / c;
ll bc = n % c;
go(bc * (bc - 1), sz + 1, sz + 1);
go((c - bc) * (c - bc - 1), sz, sz);
go(2ll * bc * (c - bc), sz, sz + 1);
ll all = ll(n) * ll(n - 1) / 2;
cout << norm((all - ans) * invs[2]) << "\n";
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k;
int Mod;
int inv4;
int inv[200010];
int sum[200010];
int fpow(int a, int b) {
int ans = 1, t = a;
while (b) {
if (b & 1) ans = 1ll * ans * t % Mod;
t = 1ll * t * t % Mod;
b >>= 1;
}
return ans;
}
void init() {
int N = 200000;
for (int i = 1; i <= N; i++) {
inv[i] = fpow(i, Mod - 2);
sum[i] = (sum[i - 1] + inv[i]) % Mod;
}
return;
}
int mn = 0;
int cnt[100010];
int calc(int x, int y) {
int ans = 1ll * x * y % Mod * inv[2] % Mod;
for (int i = 1; i <= x; i++)
ans = ((ans - sum[i + y] + sum[i]) % Mod + Mod) % Mod;
return ans;
}
void divide(int l, int r, int d) {
if (l == r || d <= 1) {
cnt[r - l + 1]++;
mn = min(mn, r - l + 1);
return;
}
int mid = (l + r) >> 1;
divide(l, mid, d - 1);
divide(mid + 1, r, d - 1);
return;
}
int main() {
scanf("%d %d %d", &n, &k, &Mod);
mn = n;
init();
divide(1, n, k);
int s = mn, t = mn + 1;
int x = cnt[mn], y = cnt[mn + 1];
int ans = 0;
ans = (ans + 1ll * s * (s - 1) % Mod * inv[4] % Mod * x) % Mod;
ans = (ans + 1ll * t * (t - 1) % Mod * inv[4] % Mod * y) % Mod;
ans = (ans + 1ll * x * (x - 1) % Mod * inv[2] % Mod * calc(s, s)) % Mod;
ans = (ans + 1ll * y * (y - 1) % Mod * inv[2] % Mod * calc(t, t)) % Mod;
ans = (ans + 1ll * x * y % Mod * calc(s, t)) % Mod;
printf("%d\n", ans);
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
int mod;
const int maxn = 200111;
map<int, int> mp;
int n, k;
long long inv[maxn];
int id[maxn], g[maxn], gn;
long long sum;
long long calc(long long a, long long b) {
long long ret = 0;
for (int i = 2; i <= a + b; i++) {
long long l = max(1ll, i - b), r = min(i - 1ll, a);
long long cnt = max(0ll, r - l + 1);
ret = (ret + cnt * inv[i]) % mod;
}
return ret;
}
void solve(int l, int r, int k) {
if (l == r || k == 1) {
gn++;
for (int i = l; i <= r; i++) id[i] = i - l + 1, g[i] = gn;
mp[r - l + 1]++;
return;
}
int m = l + r >> 1;
solve(l, m, k - 1);
solve(m + 1, r, k - 1);
}
int main() {
cin >> n >> k >> mod;
inv[1] = 1;
for (int i = 2; i < maxn; i++)
inv[i] = mod - 1ll * (mod / i) * inv[mod % i] % mod;
solve(1, n, k);
long long ans = (1ll * n * (n - 1) / 2) % mod;
for (auto x : mp) {
for (auto y : mp) {
if (x.first > y.first) continue;
if (x.first == y.first) {
ans = (ans - 2ll * (1ll * x.second * (x.second - 1) / 2) % mod *
calc(x.first, x.first)) %
mod;
} else
ans = (ans - 2ll * x.second * y.second % mod * calc(x.first, y.first)) %
mod;
}
}
cout << ((ans * inv[2] % mod) + mod) % mod << endl;
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename T1, typename T2>
inline T1 max(T1 a, T2 b) {
return a < b ? b : a;
}
template <typename T1, typename T2>
inline T1 min(T1 a, T2 b) {
return a < b ? a : b;
}
const char lf = '\n';
namespace ae86 {
const int bufl = 1 << 15;
char buf[bufl], *s = buf, *t = buf;
inline int fetch() {
if (s == t) {
t = (s = buf) + fread(buf, 1, bufl, stdin);
if (s == t) return EOF;
}
return *s++;
}
inline int ty() {
int a = 0;
int b = 1, c = fetch();
while (!isdigit(c)) b ^= c == '-', c = fetch();
while (isdigit(c)) a = a * 10 + c - 48, c = fetch();
return b ? a : -a;
}
} // namespace ae86
using ae86::ty;
const int _ = 100007;
int mo;
template <typename T1, typename T2>
inline T1 ad(T1 &a, T2 b) {
return a = a + b >= mo ? a + b - mo : a + b;
}
template <typename T1, typename T2>
inline T1 dl(T1 &a, T2 b) {
return a = a >= b ? a - b : a - b + mo;
}
template <typename T1, typename T2>
inline T1 add(T1 a, T2 b) {
return a + b >= mo ? a + b - mo : a + b;
}
template <typename T1, typename T2>
inline T1 del(T1 a, T2 b) {
return a >= b ? a - b : a - b + mo;
}
long long powa(long long a, long long t) {
long long b = 1;
a = (a + mo) % mo;
while (t) {
if (t & 1) b = b * a % mo;
a = a * a % mo, t >>= 1;
}
return b;
}
inline long long inva(long long a) { return powa(a, mo - 2); }
long long ri[_] = {0}, sri[_] = {0};
void fuck(int n = _ - 1) {
ri[0] = 0, sri[0] = 0;
ri[1] = 1, sri[1] = ri[1];
for (int i = 2; i <= n; i++)
ri[i] = ri[mo % i] * (mo - mo / i) % mo, sri[i] = add(sri[i - 1], ri[i]);
}
map<int, int> cnt;
void dfs(int x, int l, int r) {
if (x <= 1 || l == r) {
cnt[r - l + 1]++;
return;
}
int mid = (l + r) >> 1;
dfs(x - 1, l, mid), dfs(x - 1, mid + 1, r);
}
long long sumri(long long a, long long b) {
long long ans = a * b % mo;
for (int i = 1; i <= a; i++) dl(ans, del(sri[i + b], sri[i]) * 2 % mo);
return ans;
}
int n, tim;
int main() {
ios::sync_with_stdio(0), cout.tie(nullptr);
n = ty(), tim = ty(), mo = ty();
fuck();
dfs(tim, 1, n);
long long ans = 0;
for (auto i : cnt) {
long long a = i.first, b = i.second;
ad(ans, a * (a - 1) % mo * ri[2] % mo * b % mo);
ad(ans, b * (b - 1) % mo * ri[2] % mo * sumri(a, a) % mo);
for (auto j : cnt) {
long long c = j.first, d = j.second;
if (a >= c) continue;
ad(ans, sumri(a, c) * b % mo * d % mo);
}
}
cout << ans * ri[2] % mo << lf;
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
int p;
int Pow(int a, int b) {
int ans = 1;
for (; b; b >>= 1, a = 1ll * a * a % p) {
if (b & 1) ans = 1ll * ans * a % p;
}
return ans;
}
int solve(int a, int b) {
if (a <= 0 || b <= 0) return 0;
int ans = 0;
for (int sm = 2; sm <= a + b; sm++)
ans = (1ll * (min(a, sm - 1) - max(1, sm - b) + 1) * (sm - 2) % p *
Pow(2 * sm, p - 2) % p +
1ll * ans) %
p;
return ans;
}
int C(int n) { return (1ll * n * (n - 1) / 2) % p; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k >> p;
int iv = (p + 1) / 2;
pair<int, int> A = {n, 1}, B = {0, 0};
while (--k) {
if (A.first & 1) {
B = {(A.first / 2) + 1, A.second};
A.first /= 2;
break;
}
A.first /= 2, A.second *= 2;
}
if (k != 0) {
while (--k) {
if (A.first & 1) {
B.first /= 2, B.second = 2 * B.second + A.second;
A.first /= 2;
} else {
A.first /= 2, A.second = 2 * A.second + B.second;
B.first = (B.first + 1) / 2;
}
}
}
int ans = 0;
ans = (1ll * C(A.first) * iv % p * A.second + 1ll * ans) % p;
ans = (1ll * C(B.first) * iv % p * B.second + 1ll * ans) % p;
ans = (1ll * C(A.second) * solve(A.first, A.first) + 1ll * ans) % p;
ans = (1ll * C(B.second) * solve(B.first, B.first) + 1ll * ans) % p;
ans =
(1ll * A.second * B.second % p * solve(A.first, B.first) + 1ll * ans) % p;
return cout << ans << endl, 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
vector<int> vec;
int n, k, q, cnt[100005];
inline int power(int a, int b) {
int ans = 1;
for (; b; a = 1LL * a * a % q, b >>= 1) ans = b & 1 ? 1LL * ans * a % q : ans;
return ans;
}
inline int inv(int a) { return power(a, q - 2); }
void partition(int a, int b) {
if (min(a, b) == 1) {
if (!cnt[a]++) vec.push_back(a);
return;
}
partition(a >> 1, --b);
partition(a + 1 >> 1, b);
}
int main() {
cin >> n >> k >> q;
partition(n, k);
int ans = 0;
for (auto x : vec) {
for (auto y : vec)
if (x <= y) {
int res = 1LL * x * y % q * inv(2) % q;
for (int i = 2; i <= x + y; i++)
(res += q - (i - max(i - x - 1, 0) - max(i - y - 1, 0) - 1LL) *
inv(i) % q) %= q;
(ans += 1LL * res * cnt[x] % q *
(x ^ y ? cnt[y] : (cnt[y] - 1LL) * inv(2) % q) % q) %= q;
}
(ans += (x - 1LL) * x % q * inv(4) % q * cnt[x] % q) %= q;
}
cout << ans;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long inv[100005], n, k, p, cnt[3], len[3], sum[100005], ans;
void gb(long long l, long long r, long long h) {
if (h <= 1 || l == r) {
if (!len[1] || len[1] == r - l + 1)
len[1] = r - l + 1, cnt[1]++;
else
len[2] = r - l + 1, cnt[2]++;
return;
}
long long mid = l + r >> 1;
gb(l, mid, h - 1), gb(mid + 1, r, h - 1);
}
inline long long work(long long x, long long y) {
long long s = inv[2] * x % p * y % p;
for (long long i = 1; i <= x; i++) s = (s - sum[i + y] + sum[i]) % p;
return (s + p) % p;
}
signed main() {
cin >> n >> k >> p;
inv[1] = 1;
for (long long i = 2; i <= max(n, 4ll); i++)
inv[i] = inv[p % i] * (p - p / i) % p;
for (long long i = 1; i <= n; i++) sum[i] = (sum[i - 1] + inv[i]) % p;
gb(1, n, k);
ans = (len[1] * (len[1] - 1) % p * cnt[1] % p * inv[4] +
len[2] * (len[2] - 1) % p * cnt[2] % p * inv[4]) %
p;
ans =
(ans + cnt[1] * (cnt[1] - 1) % p * inv[2] % p * work(len[1], len[1])) % p;
ans =
(ans + cnt[2] * (cnt[2] - 1) % p * inv[2] % p * work(len[2], len[2])) % p;
ans = (ans + cnt[1] * cnt[2] % p * work(len[1], len[2])) % p;
printf("%lld", ans);
}
| 13 | CPP |
#include <bits/stdc++.h>
const int MAXN = 100007;
long long MOD;
inline long long FST(long long base, int times) {
long long ret = 1;
while (times) {
if (times & 1) ret = ret * base % MOD;
times >>= 1;
base = base * base % MOD;
}
return ret;
}
long long seg[MAXN], tot_seg;
long long inv[MAXN], invS[MAXN];
void getSeg(const int &l, const int &r, const int &h) {
if (h <= 1 || l == r) {
seg[++tot_seg] = r - l + 1;
return;
}
const int &mid = (l + r) >> 1;
getSeg(l, mid, h - 1);
getSeg(mid + 1, r, h - 1);
return;
}
inline long long calc(long long max_i, long long max_j) {
long long ret = max_i * max_j % MOD;
for (int i = 1; i <= max_i; ++i)
ret = (ret - (invS[i + max_j] - invS[i]) * 2) % MOD;
return ret;
}
long long buc[2][2];
int main() {
int n, k;
scanf("%d%d%I64d", &n, &k, &MOD);
inv[1] = 1;
for (int i = 2; i <= n; ++i) inv[i] = inv[i - 1] * i % MOD;
inv[n] = FST(inv[n], MOD - 2);
for (int i = n; i > 1; --i) {
long long tmp_inv = inv[i];
inv[i] = inv[i - 1] * inv[i] % MOD;
inv[i - 1] = tmp_inv * i % MOD;
}
for (int i = 1; i <= n; ++i) invS[i] = (invS[i - 1] + inv[i]) % MOD;
inv[2] = FST(2, MOD - 2);
long long ans = 0;
getSeg(1, n, k);
for (int i = 1; i <= tot_seg; ++i) {
if (!buc[0][0]) buc[0][0] = seg[i];
if (seg[i] == buc[0][0])
++buc[0][1];
else {
if (!buc[1][0]) buc[1][0] = seg[i];
++buc[1][1];
}
ans = (ans + seg[i] * (seg[i] - 1) / 2 % MOD) % MOD;
}
for (int i = 0; i < 2; ++i)
if (buc[i][1] >= 2)
ans = (ans + calc(buc[i][0], buc[i][0]) *
(buc[i][1] * (buc[i][1] - 1) / 2 % MOD) % MOD) %
MOD;
if (buc[0][0] && buc[0][1])
ans = (ans +
calc(buc[0][0], buc[1][0]) * (buc[1][1] * buc[0][1] % MOD) % MOD) %
MOD;
ans = ans * inv[2] % MOD;
printf("%I64d\n", (ans + MOD) % MOD);
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0, f = 1;
char c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-') f = -1;
for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
return x * f;
}
const int MAXN = 100010;
const int INF = 2147483600;
long long Mod;
int N, K, Q;
int mn = 100000;
long long inv[MAXN << 1], sm[MAXN << 1];
long long cnt[MAXN << 1];
long long ans;
inline void div(int l, int r, int x) {
if (x == 1 || l == r) {
mn = min(mn, r - l + 1);
++cnt[r - l + 1];
return;
}
int mid = (l + r) >> 1;
div(l, mid, x - 1);
div(mid + 1, r, x - 1);
}
inline void calc(int x) {
(ans += cnt[x] * x % Mod * (x - 1) % Mod * inv[4] % Mod) %= Mod;
}
int main() {
N = read(), K = read(), Mod = read();
div(1, N, K);
inv[1] = 1;
for (int i = 2; i <= 2 * N + 2; i++)
inv[i] = (Mod - (Mod / i) * inv[Mod % i] % Mod) % Mod;
for (int i = 1; i <= 2 * N + 2; i++) sm[i] = (sm[i - 1] + inv[i]) % Mod;
calc(mn);
calc(mn + 1);
for (int i = 1; i <= mn; i++) {
(ans += inv[2] * cnt[mn] % Mod * (cnt[mn] - 1) % Mod * inv[2] % Mod * (mn) %
Mod) %= Mod;
ans = (ans -
cnt[mn] % Mod * (cnt[mn] - 1) % Mod * inv[2] % Mod *
((sm[mn + i] - sm[i] + Mod) % Mod) % Mod +
Mod) %
Mod;
(ans += inv[2] * cnt[mn] % Mod * cnt[mn + 1] % Mod * (mn + 1) % Mod) %= Mod;
ans = (ans -
cnt[mn] * cnt[mn + 1] % Mod *
((sm[mn + i + 1] - sm[i] + Mod) % Mod) % Mod +
Mod) %
Mod;
(ans += inv[2] * cnt[mn + 1] % Mod * (cnt[mn + 1] - 1) % Mod * inv[2] %
Mod * (mn + 1) % Mod) %= Mod;
ans = (ans -
cnt[mn + 1] % Mod * (cnt[mn + 1] - 1) % Mod * inv[2] % Mod *
((sm[mn + i + 1] - sm[i] + Mod) % Mod) % Mod +
Mod) %
Mod;
}
int i = mn + 1;
(ans += inv[2] * cnt[mn + 1] % Mod * (cnt[mn + 1] - 1) % Mod * inv[2] % Mod *
(mn + 1) % Mod) %= Mod;
ans = (ans -
cnt[mn + 1] % Mod * (cnt[mn + 1] - 1) % Mod * inv[2] % Mod *
((sm[mn + i + 1] - sm[i] + Mod) % Mod) % Mod +
Mod) %
Mod;
cout << ans << endl;
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
const int N = 100005;
int n, d, inv[N << 1], P, Inv2;
int c[N];
void getp(int l, int r, int dep) {
if (dep <= 1 || l == r) {
++c[r - l + 1];
return;
}
int mid = (l + r) >> 1;
getp(l, mid, dep - 1);
getp(mid + 1, r, dep - 1);
}
int ans;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(0);
std::cin >> n >> d >> P;
inv[1] = 1;
for (int i = 2; i <= n + n; ++i) {
inv[i] = 1ll * (P - P / i) * inv[P % i] % P;
}
Inv2 = inv[2];
getp(1, n, d);
for (int i = 1; i <= n; ++i) {
if (c[i]) {
ans = (ans + 1ll * i * (i - 1) / 2 % P * Inv2 % P * c[i]) % P;
for (int j = i; j <= n; ++j) {
if (c[j]) {
int sum = 0;
for (int k = 2; k <= i + j; ++k) {
int t = std::min(i, k - 1) - std::max(1, k - j) + 1;
sum = (sum + 1ll * t * (k - 2) % P * inv[k]) % P;
}
sum = 1ll * sum * Inv2 % P;
int t =
i == j ? 1ll * c[i] * (c[i] - 1) / 2 % P : 1ll * c[i] * c[j] % P;
ans = (ans + 1ll * t * sum) % P;
}
}
}
}
std::cout << ans << std::endl;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class t>
inline t read(t &x) {
char c = getchar();
bool f = 0;
x = 0;
while (!isdigit(c)) f |= c == '-', c = getchar();
while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
if (f) x = -x;
return x;
}
template <class t, class... A>
inline void read(t &x, A &...a) {
read(x);
read(a...);
}
template <class t>
inline void write(t x) {
if (x < 0)
putchar('-'), write(-x);
else {
if (x > 9) write(x / 10);
putchar('0' + x % 10);
}
}
const long long N = 1e5 + 5;
long long ans, a1, a2, b1, b2, n, k, inv[N << 1], mod;
long long fpow(long long x, long long y) {
long long res = 1;
for (; y; y >>= 1, x = x * x % mod)
if (y & 1) res = res * x % mod;
return res;
}
void dico(long long l, long long r, long long h) {
if (h == 1 || l == r) {
l = r - l + 1;
if (!a1)
a1 = l, b1 = 1;
else if (a1 == l)
b1++;
else
a2 = l, b2++;
return;
}
long long mid = l + r >> 1;
dico(l, mid, h - 1);
dico(mid + 1, r, h - 1);
}
long long calc(long long n, long long m) {
long long res = 0;
for (long long i = 2; i <= n + m; i++)
res = (res + inv[i] * min(i - 1, n + m - i + 1) % mod) % mod;
return mod - res;
}
void init(long long n) {
inv[1] = 1;
for (long long i = 2; i <= n; i++)
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
}
signed main() {
read(n, k, mod);
ans = n * (n - 1) / 2 * (mod + 1 >> 1) % mod;
init(n << 1);
dico(1, n, k);
ans = (ans + b1 * (b1 - 1) / 2 * calc(a1, a1) % mod) % mod;
if (a2) {
ans = (ans + b2 * (b2 - 1) / 2 * calc(a2, a2) % mod) % mod;
ans = (ans + b1 * b2 % mod * calc(a1, a2) % mod) % mod;
}
write(ans);
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline T gi() {
T f = 1, x = 0;
char c = getchar();
while (c < '0' || c > '9') {
if (c == '-') f = -1;
c = getchar();
}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f * x;
}
const int INF = 0x3f3f3f3f, N = 100003, M = N << 1;
int n, k, mod;
pair<int, int> p[2];
long long inv[N], sum[N];
inline long long qpow(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;
}
inline void pre() {
for (int i = 1; i <= max(n, 4); i += 1)
inv[i] = qpow(i, mod - 2), sum[i] = (sum[i - 1] + inv[i]) % mod;
return;
}
inline long long solve(long long len1, long long len2) {
long long res = 0;
for (int i = 1; i <= len1; i += 1)
res = ((res + len2 * inv[2] % mod - (sum[i + len2] - sum[i]) % mod) % mod +
mod) %
mod;
return res;
}
int main() {
n = gi<int>(), k = gi<int>(), mod = gi<int>();
--k;
if (k > 20) k = 20;
int u = min(n, 1 << k);
int a = n / u, b = a + 1, cnta = b * u - n, cntb = u - cnta;
p[0] = {a, cnta}, p[1] = {b, cntb};
pre();
long long ans = 0;
for (int i = 0; i <= 1; i += 1) {
int len = p[i].first, cnt = p[i].second;
ans = (ans + 1ll * cnt * len % mod * (len - 1) % mod * inv[4] % mod) % mod;
}
for (int i = 0; i <= 1; i += 1)
for (int j = 0; j <= 1; j += 1) {
if (i == j) {
long long len = p[i].first,
cnt = 1ll * p[i].second * (p[i].second - 1) / 2 % mod;
ans = (ans + 1ll * cnt * solve(len, len) % mod) % mod;
} else if (p[i].first < p[j].first) {
long long len1 = p[i].first, len2 = p[j].first,
cnt = 1ll * p[i].second * p[j].second % mod;
ans = (ans + 1ll * cnt * solve(len1, len2) % mod) % mod;
}
}
printf("%lld\n", ans);
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
long long x = 0;
char ch = getchar();
bool d = 1;
for (; !isdigit(ch); ch = getchar())
if (ch == '-') d = 0;
for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - '0';
return d ? x : -x;
}
inline unsigned long long rnd() {
return ((unsigned long long)rand() << 30 ^ rand()) << 4 | rand() % 4;
}
const int N = 1e5 + 5;
int a[N], mo, inv2;
int C(int n) { return (long long)(n - 1) * n % mo * inv2 % mo; }
int ksm(int x, int p) {
int res = 1;
for (; p; p >>= 1, x = (long long)x * x % mo) {
if (p & 1) res = (long long)res * x % mo;
}
return res;
}
vector<long long> v;
int sum[N], tong[N];
void solve(int l, int r, int k) {
if (k <= 1 || l == r) {
if (!tong[r - l + 1]) v.push_back(r - l + 1);
tong[r - l + 1]++;
return;
}
int mid = l + r >> 1;
solve(l, mid, k - 1);
solve(mid + 1, r, k - 1);
}
int calc(int x, int y) {
int res = (long long)x * y % mo * inv2 % mo;
for (int i = (int)(1); i <= (int)(x); i++) {
int ssw = (sum[i + y] - sum[i] + mo) % mo;
res = (res - ssw + mo) % mo;
}
return res;
}
int main() {
int n = read(), k = read();
mo = read();
inv2 = (mo + 1) / 2;
for (int i = (int)(1); i <= (int)(n); i++) {
int inv = ksm(i, mo - 2);
sum[i] = (sum[i - 1] + inv) % mo;
}
solve(1, n, k);
int ans = 0;
for (auto x : v) {
ans = (ans + (long long)C(x) * inv2 % mo * tong[x]) % mo;
ans = (ans + (long long)C(tong[x]) * calc(x, x)) % mo;
}
for (auto x : v)
for (auto y : v)
if (x > y) {
ans = (ans + (long long)tong[x] * tong[y] % mo * calc(x, y)) % mo;
}
cout << ans;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
const long long N = 200005;
long long n, k, mo, gs[3], len[3], top, inv[N], s[N], ans;
map<long long, long long> ma;
inline long long read() {
long long ret = 0, ff = 1;
char ch = getchar();
while (!isdigit(ch)) {
if (ch == '-') ff = -1;
ch = getchar();
}
while (isdigit(ch)) {
ret = ret * 10 + (ch ^ 48);
ch = getchar();
}
return ret * ff;
}
void write(long long x) {
if (x < 0) {
x = -x, putchar('-');
}
if (x > 9) write(x / 10);
putchar(x % 10 + 48);
}
void writeln(long long x) { write(x), puts(""); }
void writesp(long long x) { write(x), putchar(' '); }
void mergesort(long long l, long long r, long long h) {
if (l == r || h <= 1) {
ma[r - l + 1]++;
return;
}
long long mid = (l + r) >> 1;
mergesort(l, mid, h - 1), mergesort(mid + 1, r, h - 1);
}
long long calc(long long x, long long y) {
long long res = x * y % mo * inv[2] % mo;
for (long long i = 1; i <= x; i++)
res = (res - (s[i + y] - s[i]) % mo + mo) % mo;
return res;
}
signed main() {
n = read(), k = read(), mo = read();
mergesort(1, n, k);
for (map<long long, long long>::iterator it = ma.begin(); it != ma.end();
it++) {
len[++top] = it->first;
gs[top] = it->second;
}
inv[0] = inv[1] = 1;
s[0] = 1, s[1] = 2;
for (long long i = 2; i <= 200000; i++)
inv[i] = (mo - mo / i) * inv[mo % i] % mo, s[i] = (s[i - 1] + inv[i]) % mo;
ans = (ans + gs[1] * len[1] % mo * (len[1] - 1) % mo * inv[4] % mo) % mo;
ans = (ans + gs[2] * len[2] % mo * (len[2] - 1) % mo * inv[4] % mo) % mo;
ans = (ans +
gs[1] * (gs[1] - 1) % mo * inv[2] % mo * calc(len[1], len[1]) % mo) %
mo;
ans = (ans +
gs[2] * (gs[2] - 1) % mo * inv[2] % mo * calc(len[2], len[2]) % mo) %
mo;
ans = (ans + gs[1] * gs[2] % mo * calc(len[1], len[2]) % mo) % mo;
write(ans);
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
inline long long Getint() {
char ch = getchar();
long long x = 0, fh = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') fh = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
(x *= 10) += ch ^ 48;
ch = getchar();
}
return fh * x;
}
const int N = 200005;
int n, h;
long long mod, fc[N], fiv[N], inv[N], sm[N];
int su[N];
struct nod {
int a, l;
nod(int x = 0, int y = 0) {
a = x;
l = y;
}
};
vector<nod> a;
inline long long Solve(int l1, int l2) {
long long Ans = 1ll * l1 * l2 % mod * inv[2] % mod;
for (int i = 1; i <= l1; i++) (Ans += sm[i] - sm[i + l2] + mod) %= mod;
return (Ans % mod + mod) % mod;
}
void Build(int l, int r, int h) {
if (h == 1) {
su[r - l + 1]++;
return;
}
if (l == r) {
su[1]++;
return;
}
int mid = l + r >> 1;
Build(l, mid, h - 1);
Build(mid + 1, r, h - 1);
}
int main() {
n = Getint();
h = Getint();
mod = Getint();
fc[0] = fc[1] = fiv[0] = fiv[1] = inv[0] = inv[1] = 1;
for (int i = 2; i <= N - 1; i++) {
fc[i] = fc[i - 1] * i % mod;
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
fiv[i] = fiv[i - 1] * inv[i] % mod;
}
for (int i = 1; i <= N - 1; i++) {
sm[i] = (sm[i - 1] + inv[i]) % mod;
}
Build(1, n, h);
long long Ans = 0;
for (int i = 1; i <= n; i++) {
if (!su[i]) continue;
a.push_back(nod(su[i], i));
(Ans += 1ll * su[i] * i % mod * (i - 1) % mod * inv[4]) %= mod;
}
for (int i = 0; i <= int(a.size()) - 1; i++) {
(Ans += 1ll * a[i].a * (a[i].a - 1) % mod * inv[2] % mod *
Solve(a[i].l, a[i].l)) %= mod;
for (int j = i + 1; j <= int(a.size()) - 1; j++) {
(Ans += 1ll * a[i].a * a[j].a % mod * Solve(a[i].l, a[j].l)) %= mod;
}
}
cout << Ans % mod << '\n';
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, sgn = 1;
char ch = getchar();
for (; !isdigit(ch); ch = getchar())
if (ch == '-') sgn = -1;
for (; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48);
return x * sgn;
}
const int N = 2e5 + 10;
int n, k, mod;
long long sum[N], inv[N];
map<int, int> m;
map<int, int>::iterator it, it1;
long long ans;
long long qp(long long x, int t) {
long long res = 1;
for (; t; t >>= 1, x = x * x % mod)
if (t & 1) res = res * x % mod;
return res;
}
void divide(int l, int r, int k) {
if (l == r || k <= 1) {
m[r - l + 1]++;
return;
}
int mid = l + r >> 1;
divide(l, mid, k - 1);
divide(mid + 1, r, k - 1);
}
long long calc(int x, int y) {
long long res = 1ll * x * y % mod;
for (int i = 1; i <= x; i++)
res = (res - 2 * sum[i + y] + 2 * sum[i] + mod) % mod;
return (res + mod) % mod;
}
int main() {
n = read(), k = read(), mod = read();
for (int i = 1; i < N; i++) inv[i] = qp(i, mod - 2);
for (int i = 1; i < N; i++) sum[i] = (sum[i - 1] + inv[i]) % mod;
divide(1, n, k);
for (it = m.begin(); it != m.end(); it++) {
long long x = it->first, s = it->second;
ans =
(ans + x * (x - 1) % mod * inv[2] % mod * inv[2] % mod * s % mod) % mod;
ans = (ans +
s * (s - 1) % mod * inv[2] % mod * inv[2] % mod * calc(x, x) % mod) %
mod;
}
for (it = m.begin(); it != m.end(); it++)
for (it1 = m.begin(); it1 != m.end(); it1++) {
long long x1 = it->first, s1 = it->second, x2 = it1->first,
s2 = it1->second;
if (x1 >= x2) continue;
ans = (ans + s1 * s2 % mod * inv[2] % mod * calc(x1, x2) % mod) % mod;
}
printf("%lld\n", ans);
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k, mod;
long long inv[100009], sum[100009], ans;
map<int, int> tag;
map<int, int>::iterator it1, it2;
long long Read() {
long long x = 0;
char c = getchar();
bool f = 0;
while (!isdigit(c)) {
if (c == '-') f = 1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
}
long long Pow(long long x, long long y) {
long long ans = 1;
while (y) {
if (y & 1) ans = ans * x % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
void Fix(long long &x) { x = x >= mod ? x - mod : x; }
long long C(long long n) { return n * (n - 1) / 2 % mod; }
void Solve(int l, int r, int k) {
if (k == 1 || l == r) {
tag[r - l + 1]++;
return;
}
int mid = (l + r) >> 1;
Solve(l, mid, k - 1);
Solve(mid + 1, r, k - 1);
}
long long Calc(int a, int b) {
long long ans = 1ll * a * b % mod * inv[2] % mod;
for (int i = 1; i <= a; ++i) Fix(ans = ans - (sum[i + b] - sum[i]) + mod);
return ans;
}
int main() {
n = Read(), k = Read(), mod = Read();
for (int i = 1; i <= n; ++i)
inv[i] = Pow(i, mod - 2), Fix(sum[i] = sum[i - 1] + inv[i]);
Solve(1, n, k);
for (it1 = tag.begin(); it1 != tag.end(); ++it1) {
Fix(ans += C(it1->first) * inv[2] % mod * it1->second % mod);
Fix(ans += C(it1->second) * Calc(it1->first, it1->first) % mod);
}
for (it1 = tag.begin(); it1 != tag.end(); ++it1)
for (it2 = tag.begin(); it2 != tag.end(); ++it2) {
if (it1->first <= it2->first) break;
Fix(ans += 1ll * it1->second * it2->second % mod *
Calc(it1->first, it2->first) % mod);
}
printf("%lld\n", ans);
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
map<int, int> mp;
int n, k, mod, inv2;
int ans = 0;
inline void add(int& a, int b) {
a += b;
if (a >= mod) a -= mod;
if (a < 0) a += mod;
}
inline int ksm(int a, int b) {
int ans = 1;
for (; b; b >>= 1, a = (long long)a * a % mod)
if (b & 1) ans = (long long)ans * a % mod;
return ans;
}
inline void build(int l, int r, int h) {
if (l < r) {
if (h <= 1) {
int len = r - l + 1;
mp[len]++;
add(ans, (long long)len * (len - 1) / 2ll % mod * inv2 % mod);
} else {
int mid = (l + r) >> 1;
build(l, mid, h - 1);
build(mid + 1, r, h - 1);
}
} else
mp[1]++;
}
signed main() {
cin >> n >> k >> mod;
inv2 = ksm(2, mod - 2);
build(1, n, k);
for (auto i : mp) {
for (auto j : mp) {
int gs = (long long)i.second * (j.second - (i.first == j.first)) % mod;
for (int l = 2; l <= i.first + j.first; ++l) {
int minn = max(1, l - j.first);
int maxx = min(i.first, l - 1);
int tmp = (long long)gs * (maxx - minn + 1) % mod *
(inv2 - ksm(l, mod - 2)) % mod * inv2 % mod;
add(ans, tmp);
}
}
}
cout << ans;
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
template <typename T>
T inverse(T a, T m) {
T u = 0, v = 1;
while (a != 0) {
T t = m / a;
m -= t * a;
swap(a, m);
u -= t * v;
swap(u, v);
}
assert(m == 1);
return u;
}
template <typename T>
class Modular {
public:
using Type = typename decay<decltype(T::value)>::type;
constexpr Modular() : value() {}
template <typename U>
Modular(const U& x) {
value = normalize(x);
}
template <typename U>
static Type normalize(const U& x) {
Type v;
if (-mod() <= x && x < mod())
v = static_cast<Type>(x);
else
v = static_cast<Type>(x % mod());
if (v < 0) v += mod();
return v;
}
const Type& operator()() const { return value; }
template <typename U>
explicit operator U() const {
return static_cast<U>(value);
}
constexpr static Type mod() { return T::value; }
Modular& operator+=(const Modular& other) {
if ((value += other.value) >= mod()) value -= mod();
return *this;
}
Modular& operator-=(const Modular& other) {
if ((value -= other.value) < 0) value += mod();
return *this;
}
template <typename U>
Modular& operator+=(const U& other) {
return *this += Modular(other);
}
template <typename U>
Modular& operator-=(const U& other) {
return *this -= Modular(other);
}
Modular& operator++() { return *this += 1; }
Modular& operator--() { return *this -= 1; }
Modular operator++(int) {
Modular result(*this);
*this += 1;
return result;
}
Modular operator--(int) {
Modular result(*this);
*this -= 1;
return result;
}
Modular operator-() const { return Modular(-value); }
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, int>::value,
Modular>::type&
operator*=(const Modular& rhs) {
value = normalize(static_cast<int64_t>(value) *
static_cast<int64_t>(rhs.value));
return *this;
}
template <typename U = T>
typename enable_if<is_same<typename Modular<U>::Type, long long>::value,
Modular>::type&
operator*=(const Modular& rhs) {
long long q = static_cast<long long>(static_cast<long double>(value) *
rhs.value / mod());
value = normalize(value * rhs.value - q * mod());
return *this;
}
template <typename U = T>
typename enable_if<!is_integral<typename Modular<U>::Type>::value,
Modular>::type&
operator*=(const Modular& rhs) {
value = normalize(value * rhs.value);
return *this;
}
Modular& operator/=(const Modular& other) {
return *this *= Modular(inverse(other.value, mod()));
}
friend const Type& abs(const Modular& x) { return x.value; }
template <typename U>
friend bool operator==(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename U>
friend bool operator<(const Modular<U>& lhs, const Modular<U>& rhs);
template <typename V, typename U>
friend V& operator>>(V& stream, Modular<U>& number);
private:
Type value;
};
template <typename T>
bool operator==(const Modular<T>& lhs, const Modular<T>& rhs) {
return lhs.value == rhs.value;
}
template <typename T, typename U>
bool operator==(const Modular<T>& lhs, U rhs) {
return lhs == Modular<T>(rhs);
}
template <typename T, typename U>
bool operator==(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) == rhs;
}
template <typename T>
bool operator!=(const Modular<T>& lhs, const Modular<T>& rhs) {
return !(lhs == rhs);
}
template <typename T, typename U>
bool operator!=(const Modular<T>& lhs, U rhs) {
return !(lhs == rhs);
}
template <typename T, typename U>
bool operator!=(U lhs, const Modular<T>& rhs) {
return !(lhs == rhs);
}
template <typename T>
bool operator<(const Modular<T>& lhs, const Modular<T>& rhs) {
return lhs.value < rhs.value;
}
template <typename T>
Modular<T> operator+(const Modular<T>& lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) += rhs;
}
template <typename T, typename U>
Modular<T> operator+(const Modular<T>& lhs, U rhs) {
return Modular<T>(lhs) += rhs;
}
template <typename T, typename U>
Modular<T> operator+(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) += rhs;
}
template <typename T>
Modular<T> operator-(const Modular<T>& lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) -= rhs;
}
template <typename T, typename U>
Modular<T> operator-(const Modular<T>& lhs, U rhs) {
return Modular<T>(lhs) -= rhs;
}
template <typename T, typename U>
Modular<T> operator-(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) -= rhs;
}
template <typename T>
Modular<T> operator*(const Modular<T>& lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) *= rhs;
}
template <typename T, typename U>
Modular<T> operator*(const Modular<T>& lhs, U rhs) {
return Modular<T>(lhs) *= rhs;
}
template <typename T, typename U>
Modular<T> operator*(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) *= rhs;
}
template <typename T>
Modular<T> operator/(const Modular<T>& lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> operator/(const Modular<T>& lhs, U rhs) {
return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> operator/(U lhs, const Modular<T>& rhs) {
return Modular<T>(lhs) /= rhs;
}
template <typename T, typename U>
Modular<T> power(const Modular<T>& a, const U& b) {
assert(b >= 0);
Modular<T> x = a, res = 1;
U p = b;
while (p > 0) {
if (p & 1) res *= x;
x *= x;
p >>= 1;
}
return res;
}
template <typename T>
bool IsZero(const Modular<T>& number) {
return number() == 0;
}
template <typename T>
string to_string(const Modular<T>& number) {
return to_string(number());
}
template <typename U, typename T>
U& operator<<(U& stream, const Modular<T>& number) {
return stream << number();
}
template <typename U, typename T>
U& operator>>(U& stream, Modular<T>& number) {
typename common_type<typename Modular<T>::Type, long long>::type x;
stream >> x;
number.value = Modular<T>::normalize(x);
return stream;
}
using ModType = int;
struct VarMod {
static ModType value;
};
ModType VarMod::value;
ModType& md = VarMod::value;
using Mint = Modular<VarMod>;
Mint Solve(int a, int b) {
Mint ans = 0, p5 = Mint(1) / 2;
for (int i = 2; i <= a + b; ++i) {
int mina = max(1, i - b);
int maxa = min(a, i - 1);
ans += (p5 - Mint(1) / i) * (maxa - mina + 1);
}
return ans;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k >> md;
if (n == 1) {
cout << 0 << endl;
return 0;
}
map<int, int> s;
s[n] = 1;
while (--k) {
map<int, int> t;
for (auto [a, b] : s) {
if (a == 1) {
t[a] += b;
} else {
t[a / 2] += b;
t[a - a / 2] += b;
}
}
s = std::move(t);
if (s.size() == 1 && s.begin()->first == 1) break;
}
if (s.size() == 1) {
int a1 = s.begin()->first, b1 = s.begin()->second;
Mint c1 = Solve(a1, a1);
cout << Mint(a1) * (a1 - 1) / 4 * b1 + c1 * b1 * (b1 - 1) / 2 << endl;
} else {
int a1 = s.begin()->first, b1 = s.begin()->second;
int a2 = s.rbegin()->first, b2 = s.rbegin()->second;
Mint c1 = Solve(a1, a1), c2 = Solve(a2, a2), c3 = Solve(a1, a2);
cout << Mint(a1) * (a1 - 1) / 4 * b1 + Mint(a2) * (a2 - 1) / 4 * b2 +
c1 * b1 * (b1 - 1) / 2 + c2 * b2 * (b2 - 1) / 2 + c3 * b1 * b2
<< endl;
}
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
int n, K, P, ans, cnt[maxn], inv[maxn], pre[maxn];
int qp(int x, int y) {
int z = 1;
for (; y; y >>= 1, x = 1LL * x * x % P) {
if (y & 1) z = 1LL * z * x % P;
}
return z;
}
int main() {
scanf("%d %d %d", &n, &K, &P);
for (int i = 1; i <= n; i++) {
inv[i] = qp(i, P - 2);
pre[i] = (pre[i - 1] + inv[i]) % P;
}
int inv2 = (P + 1) >> 1;
function<void(int, int, int)> solve = [&](int l, int r, int h) {
if (l > r) return;
if (h == 1 || l == r) {
cnt[r - l + 1]++;
ans = (ans + 1LL * (r - l) * (r - l + 1) / 2 % P * inv2) % P;
} else {
int mid = (l + r) >> 1;
solve(l, mid, h - 1), solve(mid + 1, r, h - 1);
}
};
solve(1, n, K);
auto calc = [&](int n, int m) {
int ans = 0;
auto solve = [&]() {
for (int i = 1; i <= n; i++) {
ans = (ans + 1LL * inv2 * i % P * (pre[i + m] - pre[i] + P)) % P;
ans = (ans - 1LL * inv2 * (pre[i + m] - pre[i] + P) % P + P) % P;
}
};
solve(), swap(n, m), solve();
return ans;
};
vector<int> V;
for (int i = 1; i <= n; i++) {
if (cnt[i]) V.push_back(i);
}
assert(V.size() <= 2);
for (int x : V) {
ans = (ans + 1LL * cnt[x] * (cnt[x] - 1) / 2 % P * calc(x, x)) % P;
}
if (V.size() == 2) {
ans = (ans + 1LL * cnt[V[0]] * cnt[V[1]] % P * calc(V[0], V[1])) % P;
}
printf("%d\n", ans);
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
const int MN = 200000 + 5;
using namespace std;
template <typename T>
inline T& IN(T& in) {
in = 0;
char c = getchar();
int f = 1;
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) in = in * 10 + c - '0', c = getchar();
return in *= f;
}
int n, k, P;
long long ans;
map<int, int> len;
long long inv[MN], s[MN];
long long qp(long long a, long long b) {
long long c = 1;
for (; b; b >>= 1, a = a * a % P)
if (b & 1) c = c * a % P;
return c;
}
void build(int l, int r, int k) {
if (k == 1 || l == r) return len[r - l + 1]++, void();
int mid = l + r >> 1;
build(l, mid, k - 1), build(mid + 1, r, k - 1);
}
long long calc(long long x, long long y) {
long long res = x * y % P * inv[2] % P;
for (int i = 1; i <= x; ++i) res = (res - (s[i + y] - s[i]) % P + P) % P;
return res;
}
void input() {
IN(n), IN(k), IN(P);
int N = 200000;
inv[1] = 1, s[1] = 1;
for (int i = 2; i <= N; ++i)
inv[i] = (P - P / i) * inv[P % i] % P, s[i] = (s[i - 1] + inv[i]) % P;
build(1, n, k);
for (auto it : len) {
long long x = it.first, y = it.second;
ans = (ans + x * (x - 1) % P * inv[4] % P * y % P +
y * (y - 1) % P * inv[2] % P * calc(x, x) % P) %
P;
}
for (auto x : len)
for (auto y : len)
if (x.first < y.first)
ans = (ans + calc(x.first, y.first) * x.second % P * y.second % P) % P;
printf("%lld\n", ans);
}
int main() {
input();
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
const int MAXN = 100000;
int n, mxdepth, MOD;
int INV2;
int inv[2 * MAXN + 1];
vector<int> parts;
void rec(int l, int r, int h) {
if (l > r) return;
if (h == 1 || l == r) {
parts.push_back(r - l + 1);
return;
}
int m = (l + r) / 2;
rec(l, m, h - 1);
rec(m + 1, r, h - 1);
}
int cntpairs(int sz) { return (long long)sz * (sz - 1) / 2 % MOD; }
int calc(int sz) { return (long long)cntpairs(sz) * INV2 % MOD; }
int calc(int sza, int szb) {
int ret = 0;
for (int den = (2); den <= (sza + szb); ++den) {
int lo = max(1, den - szb), hi = min(sza, den - 1), cnt = hi - lo + 1;
if (lo > hi) continue;
int cur = (long long)(den - 2) % MOD * inv[den] % MOD * INV2 % MOD;
ret = (ret + (long long)cnt * cur) % MOD;
}
return ret;
}
int solve() {
INV2 = (MOD + 1) / 2;
inv[1] = 1;
for (int i = (2); i <= (2 * n); ++i)
inv[i] = (long long)(MOD - MOD / i) * inv[MOD % i] % MOD;
parts.clear();
rec(1, n, mxdepth);
int sz1 = -1, cnt1 = 0, sz2 = -1, cnt2 = 0;
for (int i = (0); i < (((int)(parts).size())); ++i) {
int x = parts[i];
if (x == sz1)
++cnt1;
else if (x == sz2)
++cnt2;
else if (sz1 == -1)
sz1 = x, ++cnt1;
else if (sz2 == -1)
sz2 = x, ++cnt2;
else
assert(false);
}
int ret = 0;
if (cnt1 != 0) ret = (ret + (long long)cnt1 * calc(sz1)) % MOD;
if (cnt2 != 0) ret = (ret + (long long)cnt2 * calc(sz2)) % MOD;
if (cnt1 != 0) ret = (ret + (long long)cntpairs(cnt1) * calc(sz1, sz1)) % MOD;
if (cnt1 != 0 && cnt2 != 0)
ret = (ret + (long long)cnt1 * cnt2 % MOD * calc(sz1, sz2)) % MOD;
if (cnt2 != 0) ret = (ret + (long long)cntpairs(cnt2) * calc(sz2, sz2)) % MOD;
return ret;
}
void run() {
scanf("%d%d%d", &n, &mxdepth, &MOD);
printf("%d\n", solve());
}
int main() {
run();
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long P;
long long inv[500005], s[500005];
int n, m;
int len[2], cnt[2];
void add(long long &x, long long y) {
x += y;
if (x >= P) x -= P;
if (x < 0) x += P;
}
void init() {
inv[0] = inv[1] = 1;
for (int i = 2; i <= n + m; i++) inv[i] = (P - P / i) * inv[P % i] % P;
for (int i = 1; i <= n + m; i++) s[i] = (s[i - 1] + inv[i]) % P;
}
long long calc(long long l1, long long l2) {
long long ans = l1 * l2 % P * inv[2] % P;
for (int i = 1; i <= l1; i++) add(ans, P - (s[i + l2] - s[i]) % P);
return ans;
}
long long sum(long long x) { return x * (x - 1) / 2 % P; }
void dfs(int l, int r, int h) {
if (h <= 1 || l == r) {
if (!len[0] || r - l + 1 == len[0])
len[0] = r - l + 1, cnt[0]++;
else if (!len[1] || r - l + 1 == len[1])
len[1] = r - l + 1, cnt[1]++;
return;
}
int mid = (l + r) >> 1;
dfs(l, mid, h - 1), dfs(mid + 1, r, h - 1);
}
int main() {
cin >> n >> m >> P;
init();
dfs(1, n, m);
long long ans = 0;
for (int i = 0; i <= 1; i++) {
add(ans, sum(len[i]) * inv[2] % P * cnt[i] % P);
add(ans, sum(cnt[i]) * calc(len[i], len[i]) % P);
}
add(ans, cnt[0] * cnt[1] % P * calc(len[0], len[1]) % P);
cout << ans << endl;
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, K, P, I, ans, inv[N], H[N], len[2], cnt[2];
int ad(int k1, int k2) { return k1 += k2 - P, k1 += k1 >> 31 & P; }
int su(int k1, int k2) { return k1 -= k2, k1 += k1 >> 31 & P; }
int mu(int k1, int k2) { return 1LL * k1 * k2 % P; }
int po(int k1, int k2) {
int k3 = 1;
for (; k2; k2 >>= 1, k1 = mu(k1, k1))
if (k2 & 1) k3 = mu(k3, k1);
return k3;
}
void sol(int k1, int k2, int k3) {
if (k1 >= K || k2 == k3) {
int x = 0;
if (len[x] && len[x] != k3 - k2 + 1) ++x;
len[x] = k3 - k2 + 1;
++cnt[x];
ans = ad(ans, mu(mu(k3 - k2 + 1, k3 - k2 + 1 - 1), mu(I, I)));
return;
}
int mid = (k2 + k3) >> 1;
sol(k1 + 1, k2, mid);
sol(k1 + 1, mid + 1, k3);
}
int calc(int x, int y) {
if (!x || !y) return 0;
int res = mu(mu(x, y), I);
for (int i = (1); i <= (x); ++i) {
res = su(res, su(H[i + y], H[i]));
}
return res;
}
int main() {
scanf("%d%d%d", &n, &K, &P);
I = po(2, P - 2);
inv[0] = inv[1] = 1;
for (int i = (2); i <= (N - 1); ++i) inv[i] = mu(P - P / i, inv[P % i]);
for (int i = (1); i <= (N - 1); ++i) H[i] = ad(H[i - 1], inv[i]);
sol(1, 1, n);
auto C2 = [&](int x) { return mu(mu(x, x - 1), I); };
ans = ad(ans, mu(C2(cnt[0]), calc(len[0], len[0])));
ans = ad(ans, mu(C2(cnt[1]), calc(len[1], len[1])));
ans = ad(ans, mu(mu(cnt[0], cnt[1]), calc(len[0], len[1])));
printf("%d\n", ans);
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
int n, m, t, x, y, mod, ans, v[200010];
std::map<int, int> map;
void solve(int l, int r, int k) {
if (l == r || k <= 1) {
map[r - l + 1]++;
return;
}
solve(l, l + r >> 1, k - 1), solve((l + r >> 1) + 1, r, k - 1);
}
int fpow(long long a, int b) {
long long s = 1;
for (; b; b >>= 1, a = a * a % mod)
if (b & 1) s = s * a % mod;
return s;
}
long long f(long long x) {
return (long long)x * (x - 1) % mod * fpow(4, mod - 2) % mod;
}
long long f(long long x, long long y) {
long long res = (long long)x * y % mod * fpow(2, mod - 2) % mod;
for (int i = 1; i <= x; i++) res = (res - v[i + y] + v[i]);
return (res % mod + mod) % mod;
}
int main() {
scanf("%d%d%d", &n, &m, &mod), solve(1, n, m);
for (int i = 0; i <= n + m; i++) v[i] = (v[i - 1] + fpow(i, mod - 2)) % mod;
t = map.begin()->first, x = map[t], y = map[t + 1];
printf("%d\n",
(x * f(t) + y * f(t + 1) + x * (x - 1) / 2 % mod * f(t, t) +
x * y % mod * f(t, t + 1) + y * (y - 1) / 2 % mod * f(t + 1, t + 1)) %
mod);
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int cnt[maxn], mod;
int Pow(int x, int p) {
int r = 1;
while (p) {
if (p & 1) r = (long long)x * r % mod;
p >>= 1;
x = (long long)x * x % mod;
}
return r;
}
void dfs(int n, int k) {
if (n == 1 || k == 1) {
cnt[n]++;
return;
}
dfs(n / 2, k - 1);
dfs((n + 1) / 2, k - 1);
}
int calc(int x, int y) {
int res = 0;
for (int i = 2; i <= x + y; ++i)
res = (res + (long long)min(x + y - i + 1, i - 1) * (i - 2) % mod *
Pow(2 * i, mod - 2) % mod) %
mod;
return res;
}
int main() {
int n, k;
scanf("%d %d", &n, &k);
scanf("%d", &mod);
dfs(n, k);
int ans = 0;
for (int i = 1; i <= n; ++i)
if (cnt[i]) {
ans = (ans + (long long)cnt[i] * i % mod * (i - 1) % mod *
Pow(4, mod - 2) % mod) %
mod;
ans =
(ans + (long long)cnt[i] * (cnt[i] - 1) / 2 % mod * calc(i, i)) % mod;
}
for (int i = 1; i <= n; ++i)
if (cnt[i])
for (int j = i + 1; j <= n; ++j)
if (cnt[j])
ans = (ans + (long long)cnt[i] * cnt[j] % mod * calc(i, j)) % mod;
cout << ans << endl;
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
int n, k, mod;
long long inv[100009], sum[100009], ans;
map<int, int> tong;
map<int, int>::iterator it1, it2;
inline long long rd() {
long long x = 0;
char c = getchar();
bool f = 0;
while (!isdigit(c)) {
if (c == '-') f = 1;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
}
inline long long power(long long x, long long y) {
long long ans = 1;
while (y) {
if (y & 1) ans = ans * x % mod;
x = x * x % mod;
y >>= 1;
}
return ans;
}
inline void MOD(long long &x) { x = x >= mod ? x - mod : x; }
inline long long C(long long n) { return n * (n - 1) / 2 % mod; }
inline void solve(int l, int r, int k) {
if (k == 1 || l == r) {
tong[r - l + 1]++;
return;
}
int mid = (l + r) >> 1;
solve(l, mid, k - 1);
solve(mid + 1, r, k - 1);
}
inline long long calc(int a, int b) {
long long ans = 1ll * a * b % mod * inv[2] % mod;
for (int i = 1; i <= a; ++i) MOD(ans = ans - (sum[i + b] - sum[i]) + mod);
return ans;
}
int main() {
n = rd();
k = rd();
mod = rd();
for (int i = 1; i <= n; ++i)
inv[i] = power(i, mod - 2), MOD(sum[i] = sum[i - 1] + inv[i]);
solve(1, n, k);
for (it1 = tong.begin(); it1 != tong.end(); ++it1) {
MOD(ans += C(it1->first) * inv[2] % mod * it1->second % mod);
MOD(ans += C(it1->second) * calc(it1->first, it1->first) % mod);
}
for (it1 = tong.begin(); it1 != tong.end(); ++it1)
for (it2 = tong.begin(); it2 != tong.end(); ++it2) {
if (it1->first <= it2->first) break;
MOD(ans += 1ll * it1->second * it2->second % mod *
calc(it1->first, it2->first) % mod);
}
cout << ans;
return 0;
}
| 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 std::cerr;
using std::cout;
int mod;
inline int add(int a, int b) {
a += b - mod;
return a + (a >> 31 & mod);
}
inline int dec(int a, int b) {
a -= b;
return a + (a >> 31 & mod);
}
inline int mul(int a, int b) {
long long r = (long long)a * b;
return r >= mod ? r % mod : r;
}
inline void Inc(int &a, int b) {
a += b - mod;
a += a >> 31 & mod;
}
const int N = 1e5 + 7;
int n, k;
int inv[N], H[N];
std::map<int, int> cnt;
inline void solve(int l, int r, int d) {
if (d == 1 || l == r) {
cnt[r - l + 1]++;
return;
}
int mid = l + r >> 1;
solve(l, mid, d - 1);
solve(mid + 1, r, d - 1);
}
inline int calc(int x, int y) {
int ans = mul(x, y);
ans = mul(ans, mod + 1 >> 1);
for (int register i = 1; i <= x; ++i) Inc(ans, dec(H[i], H[i + y]));
return ans;
}
signed main() {
scanf("%d%d%d", &n, &k, &mod);
int iv2 = mod + 1 >> 1, iv4 = mul(iv2, iv2);
inv[0] = inv[1] = H[0] = H[1] = 1;
for (int register i = 2; i <= n; ++i)
H[i] = add(H[i - 1], inv[i] = mul(mod - mod / i, inv[mod % i]));
solve(1, n, k);
int ans = 0;
for (auto t : cnt) {
Inc(ans, mul(mul(t.first, t.first - 1), mul(iv4, t.second)));
Inc(ans,
mul(mul(t.second, t.second - 1), mul(iv2, calc(t.first, t.first))));
}
for (auto i1 : cnt)
for (auto i2 : cnt)
if (i1.first < i2.first) {
Inc(ans, mul(calc(i1.first, i2.first), mul(i1.second, i2.second)));
}
cout << ans << "\n";
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000;
int q;
inline int add(int x, int y) {
x += y;
return x >= q ? x - q : x;
}
inline int sub(int x, int y) {
x -= y;
return x < 0 ? x + q : x;
}
inline int mul(int x, int y) { return (int)(1LL * x * y % q); }
int mpow(int b, int p) {
int ret;
for (ret = 1; p; p >>= 1, b = mul(b, b))
if (p & 1) ret = mul(ret, b);
return ret;
}
int a[MAXN + 5];
void get(int l, int r, int h) {
if (l == r || h == 1)
a[r - l + 1]++;
else {
int m = (l + r) >> 1;
get(l, m, h - 1), get(m + 1, r, h - 1);
}
}
int inv[MAXN + 5], fct[MAXN + 5], ifct[MAXN + 5];
int comb(int n, int m) {
if (n < m || m < 0)
return 0;
else
return mul(fct[n], mul(ifct[m], ifct[n - m]));
}
int si[MAXN + 5];
void init() {
inv[1] = 1;
for (int i = 2; i <= MAXN; i++) inv[i] = sub(0, mul(q / i, inv[q % i]));
fct[0] = 1;
for (int i = 1; i <= MAXN; i++) fct[i] = mul(fct[i - 1], i);
ifct[MAXN] = mpow(fct[MAXN], q - 2);
for (int i = MAXN - 1; i >= 0; i--) ifct[i] = mul(ifct[i + 1], i + 1);
for (int i = 1; i <= MAXN; i++) si[i] = add(si[i - 1], inv[i]);
}
int b[MAXN + 5], cnt;
int main() {
int n, k;
scanf("%d%d%d", &n, &k, &q), get(1, n, k), init();
for (int i = 1; i <= n; i++)
if (a[i]) b[++cnt] = i;
int ans = 0;
for (int i = 1; i <= cnt; i++)
ans = add(ans, mul(mul(mul(mul(b[i], b[i] - 1), inv[2]), inv[2]), a[b[i]]));
for (int o1 = 1; o1 <= cnt; o1++)
for (int o2 = 1; o2 <= cnt; o2++) {
int coef = mul(a[b[o1]], o1 == o2 ? a[b[o2]] - 1 : a[b[o2]]), del = 0;
if (coef == 0) continue;
for (int i = 1; i <= b[o1]; i++) {
int k = add(mul(inv[2], i - 1), 1), c = mul(inv[i + 1], b[o2]),
d = sub(si[i + b[o2]], si[i]);
del = add(del, mul(k, sub(c, d)));
}
ans = add(ans, mul(del, coef));
}
printf("%d\n", ans);
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class T>
void minn(T &a, T b) {
a = min(a, b);
}
template <class T>
void maxx(T &a, T b) {
a = max(a, b);
}
void io() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
}
const long long MOD = 1000000007LL;
const long long PRIME = 105943LL;
const long long INF = 1e18;
long long mod;
inline long long add(long long a, long long b) { return (a + b) % mod; }
inline long long mul(long long a, long long b) { return (1LL * a * b) % mod; }
inline long long pow(long long a, long long p) {
long long ret = 1LL;
while (p) {
if (p & 1LL) ret = mul(ret, a);
a = mul(a, a), p >>= 1LL;
}
return ret;
}
inline long long inv(long long x) { return pow(x, mod - 2); }
void go(int l, int r, int h, map<int, int> &cnt) {
if (l <= r)
if (h <= 1 || l == r)
cnt[r - l + 1]++;
else {
int m = (l + r) / 2;
go(l, m, h - 1, cnt);
go(m + 1, r, h - 1, cnt);
}
}
long long solve(int x) { return mul(x, mul(x - 1, inv(4))); }
long long solve(int x, int y) {
long long ret = 0;
for (int sz = 2; sz <= (int)x + y; sz++)
ret = add(ret, mul(mul(sz - 2, min(x, sz - 1) - max(1, sz - y) + 1),
mul(inv(2), inv(sz))));
return ret;
}
int main() {
io();
int n, k;
cin >> n >> k >> mod;
map<int, int> cnt;
go(1, n, k, cnt);
assert(cnt.size() < 3);
int s = cnt.size();
vector<long long> len, num;
for (auto en : cnt) len.push_back(en.first), num.push_back(en.second);
long long ans = 0;
for (int i = 0; i < (int)(s); i++) {
long long temp = mul(num[i], solve(len[i]));
ans = add(ans, temp);
}
for (int i = 0; i < (int)(s); i++) {
long long temp = mul(num[i] * (num[i] - 1) / 2, solve(len[i], len[i]));
ans = add(ans, temp);
}
for (int i = 0; i < (int)(s); i++)
for (int j = i + 1; j < (int)(s); j++) {
long long temp = mul(mul(num[i], num[j]), solve(len[i], len[j]));
ans = add(ans, temp);
}
cout << ans << "\n";
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long n, m, mod, inv[300000], ans, cnt[300000], pre[300000];
void upd(long long &x, long long y) { x = (x + y) % mod; }
void solve(long long x, long long m) {
if (!m || x == 1) {
++cnt[x];
return;
}
solve((x + 1) / 2, m - 1);
solve(x / 2, m - 1);
}
int main() {
scanf("%lld%lld%lld", &n, &m, &mod);
m = min(m - 1, 20LL);
solve(n, m);
long long p = max(n, 4LL);
inv[1] = 1;
for (long long i = 2; i <= p; ++i)
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
for (long long i = 1; i <= p; ++i) pre[i] = (pre[i - 1] + inv[i]) % mod;
for (long long i : {n >> m, (n >> m) + 1}) {
upd(ans, cnt[i] * i % mod * (i - 1) % mod * inv[4]);
for (long long j : {n >> m, (n >> m) + 1}) {
long long sum = 0;
for (long long k = 1; k <= i; ++k) {
upd(sum, (k - 1) * (pre[k + j] - pre[k]));
}
sum = sum * inv[2] % mod;
upd(ans, sum * cnt[i] % mod * (i == j ? cnt[i] - 1 : cnt[j]));
}
}
upd(ans, mod);
printf("%lld\n", ans);
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
char ch = getchar();
long long s = 0, w = 1;
while (ch < '0' || ch > '9') {
if (ch == '-') w = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9') {
s = s * 10 + ch - '0';
ch = getchar();
}
return s * w;
}
inline int lowbit(int x) { return x & (-x); }
int mod, n, h;
int k[2], m[2];
int ans, inv2;
inline int Z(int x) { return (x >= mod ? x - mod : x); }
inline int C2(int n) { return 1LL * n * (n - 1) % mod * inv2 % mod; }
inline int ksm(int a, int b) {
int ans = 1;
while (b) {
if (b & 1) ans = 1LL * ans * a % mod;
b >>= 1;
a = 1LL * a * a % mod;
}
return ans;
}
void Solve(int l, int r, int h) {
if (h == 1 || l == r) {
if (!k[0]) {
k[0] = r - l + 1;
m[0]++;
} else if (k[0] == r - l + 1)
m[0]++;
else
k[1] = r - l + 1, m[1]++;
ans = Z(ans + 1LL * inv2 * C2(r - l + 1) % mod);
return;
}
Solve(l, ((l + r) >> 1), h - 1);
Solve(((l + r) >> 1) + 1, r, h - 1);
}
inline int calc(int n, int m) {
int s = 1LL * inv2 * n % mod * m % mod;
int l = 1, r = 0;
for (register int S = 2; S <= n + m; S++) {
while (l + m < S) l++;
while (r < S - 1 && r < n) r++;
if (l > r) break;
s = Z(s + mod - 1LL * ksm(S, mod - 2) * (r - l + 1) % mod);
}
return s;
}
int main() {
n = read(), h = read(), mod = read();
inv2 = ksm(2, mod - 2);
Solve(1, n, h);
if (!k[1]) {
ans = Z(ans + 1LL * C2(m[0]) * calc(k[0], k[0]) % mod);
} else {
ans = Z(ans + 1LL * C2(m[0]) * calc(k[0], k[0]) % mod);
ans = Z(ans + 1LL * C2(m[1]) * calc(k[1], k[1]) % mod);
ans = Z(ans + 1LL * m[0] * m[1] % mod * calc(k[0], k[1]) % mod);
}
cout << ans << '\n';
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline void read(T &x) {
x = 0;
char c = getchar();
int f = 1;
while (!isdigit(c)) {
if (c == '-') f = -1;
c = getchar();
}
while (isdigit(c)) {
x = x * 10 + c - '0';
c = getchar();
}
x *= f;
}
template <class T>
inline void umin(T &x, T y) {
x = x < y ? x : y;
}
template <class T>
inline void umax(T &x, T y) {
x = x > y ? x : y;
}
inline unsigned int R() {
static unsigned int seed = 416;
return seed ^= seed >> 5, seed ^= seed << 17, seed ^= seed >> 13;
}
const int N = 233333;
int n, k, mo, s[N], len;
long long res;
void solve(int l, int r, int h) {
if (l > r) return;
if (h <= 1 || l == r) {
s[++len] = r - l + 1;
return;
}
int mid = (l + r) >> 1;
solve(l, mid, h - 1);
solve(mid + 1, r, h - 1);
}
inline int power(int a, int n) {
int res = 1;
while (n) {
if (n & 1) res = 1LL * res * a % mo;
a = 1LL * a * a % mo;
n >>= 1;
}
return res;
}
long long solve(int n, int m) {
long long res = 1LL * n * m % mo * power(2, mo - 2) % mo;
for (register int c = (1); c <= (n + m); c++) {
int l = max(1, c - m), r = min(n, c - 1);
if (r - l + 1 >= 1) res = (res - 1LL * (r - l + 1) * power(c, mo - 2)) % mo;
}
return res;
}
int main() {
read(n);
read(k);
read(mo);
solve(1, n, k);
sort(s + 1, s + len + 1);
for (register int i = (1); i <= (len); i++)
res += 1LL * s[i] * (s[i] - 1) % mo * power(4, mo - 2) % mo;
static pair<int, int> a[N];
int tot = 0;
for (register int i = (1); i <= (len); i++)
if (a[tot].first == s[i])
a[tot].second++;
else
a[++tot] = pair<int, int>(s[i], 1);
assert(tot <= 2);
for (register int i = (1); i <= (tot); i++)
res += 1LL * a[i].second * (a[i].second - 1) / 2 % mo *
solve(a[i].first, a[i].first) % mo;
if (tot == 2)
res += 1LL * a[1].second * a[2].second % mo *
solve(a[1].first, a[2].first) % mo;
printf("%lld", (res % mo + mo) % mo);
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();
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 7;
int n, k, p, cnt[N];
long long ifac[N];
inline void solve(int l, int r, int t) {
if (t == 1 || l == r) {
cnt[r - l + 1]++;
return;
}
int d = (l + r) >> 1;
solve(l, d, t - 1), solve(d + 1, r, t - 1);
}
inline long long getans(int a, int b) {
long long sum = (p + 1) / 2, ans = sum * a % p * b % p;
for (int i = 1; i <= a; i++) ans = (ans - ifac[i + b] + ifac[i]) % p;
return (ans + p) % p;
}
int main() {
cin >> n >> k >> p, ifac[0] = ifac[1] = 1;
for (int i = 2; i <= n; i++) ifac[i] = ifac[p % i] * (p - p / i) % p;
for (int i = 1; i <= n; i++) ifac[i] = (ifac[i] + ifac[i - 1]) % p;
solve(1, n, k);
int a = 0, b = 0;
for (int i = 1; i <= n; i++)
if (cnt[i] && !a)
a = i;
else if (cnt[i])
b = i;
long long s = getans(a, a) * ((1ll * cnt[a] * (cnt[a] - 1) / 2) % p) % p;
s = (s + getans(b, b) * ((1ll * cnt[b] * (cnt[b] - 1) / 2) % p)) % p;
s = (s + getans(a, b) * cnt[a] % p * cnt[b] % p) % p;
s = (s + (1ll * a * (a - 1) / 2 * cnt[a] % p +
1ll * b * (b - 1) / 2 * cnt[b] % p) %
p * ((p + 1) / 2)) %
p;
cout << s << endl;
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
int md;
inline void add(int &a, int b) {
a += b;
if (a >= md) a -= md;
}
inline void sub(int &a, int b) {
a -= b;
if (a < 0) a += md;
}
inline int mul(int a, int b) { return (int)((long long)a * b % md); }
inline int power(int a, long long b) {
int res = 1;
while (b > 0) {
if (b & 1) {
res = mul(res, a);
}
a = mul(a, a);
b >>= 1;
}
return res;
}
inline int inv(int a) {
a %= md;
if (a < 0) a += md;
int b = md, u = 0, v = 1;
while (a) {
int t = b / a;
b -= t * a;
swap(a, b);
u -= t * v;
swap(u, v);
}
assert(b == 1);
if (u < 0) u += md;
return u;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k >> md;
if (k >= 20 || n <= (1 << (k - 1))) {
cout << 0 << '\n';
return 0;
}
int bc = (1 << (k - 1));
int small_size = n / bc;
int big_size = small_size + 1;
int big_cnt = n % bc;
int small_cnt = bc - big_cnt;
vector<int> blocks(bc);
for (int i = 0; i < n; i++) {
blocks[i % (int)blocks.size()]++;
}
map<int, int> mp;
for (int x : blocks) {
mp[x]++;
}
vector<int> fact(n + 1), inv_fact(n + 1);
fact[0] = inv_fact[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = mul(fact[i - 1], i);
inv_fact[i] = inv(fact[i]);
}
int ans = 0;
for (int b1id = 0; b1id < bc; b1id++) {
int b = blocks[b1id];
add(ans, mul(mul(b, b - 1), inv(4)));
}
vector<int> sum_inv(n + 1);
for (int i = 0; i < n; i++) {
sum_inv[i + 1] = sum_inv[i];
add(sum_inv[i + 1], inv(i + 1));
}
for (int b1id = 0; b1id < bc; b1id++) {
int b1 = blocks[b1id];
if (b1 == small_size)
small_cnt--;
else
big_cnt--;
int salt;
for (int x = 2; x <= b1; x++) {
if (small_cnt > 0) {
int aux = sum_inv[x + small_size];
sub(aux, sum_inv[x]);
int prob = mul(x - 1, aux);
add(ans, mul(small_cnt, mul(prob, inv(2))));
}
if (big_cnt > 0) {
int aux = sum_inv[x + big_size];
sub(aux, sum_inv[x]);
int prob = mul(x - 1, aux);
add(ans, mul(big_cnt, mul(prob, inv(2))));
}
}
if (b1 == small_size)
small_cnt++;
else
big_cnt++;
}
cout << ans << '\n';
return 0;
}
| 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;
int n, k, mod, num[200005];
void Add(int &a, int b) { ((a += b) >= mod) && (a -= mod); }
int ksm(int a, int b) {
int ans = 1;
while (b) {
if (b & 1) ans = 1ll * ans * a % mod;
a = 1ll * a * a % mod;
b >>= 1;
}
return ans;
}
void dfs(int dep, int l, int r) {
if (dep == 1) {
++num[r - l + 1];
return;
}
if (l == r) {
++num[1];
return;
}
int mid = l + r >> 1;
dfs(dep - 1, l, mid);
dfs(dep - 1, mid + 1, r);
}
int ans, sm[200005], inv[200005], ny[200005];
int main() {
scanf("%d%d%d", &n, &k, &mod);
n <<= 1;
sm[0] = ny[0] = 1;
for (int i = 1; i <= n; ++i) sm[i] = 1ll * sm[i - 1] * i % mod;
inv[n] = ksm(sm[n], mod - 2);
for (int i = n - 1; i >= 0; --i) inv[i] = 1ll * inv[i + 1] * (i + 1) % mod;
for (int i = 1; i <= n; ++i) ny[i] = 1ll * inv[i] * sm[i - 1] % mod;
n >>= 1;
dfs(k, 1, n);
for (int i = 1; i <= n; ++i) {
if (num[i]) {
Add(ans, 1ll * i * (i - 1) % mod * ksm(4, mod - 2) % mod * num[i] % mod);
}
}
for (int i = 1; i <= n; ++i) {
if (num[i])
for (int j = i; j <= n; ++j) {
if (num[j]) {
for (int k = 2; k <= i + j; ++k) {
if (i == j)
Add(ans, 1ll * (k - 2) * ny[2 * k] % mod *
min(k - 1, min(k, i) - max(k - j, 1) + 1) % mod *
(1ll * num[i] * (num[i] - 1) % mod * ny[2] % mod) %
mod);
else
Add(ans, 1ll * (k - 2) * ny[2 * k] % mod *
min(k - 1, min(k, i) - max(k - j, 1) + 1) % mod *
num[i] % mod * num[j] % mod);
}
}
}
}
printf("%d\n", ans);
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &x, T y) {
return (y < x) ? (x = y, 1) : 0;
}
template <typename T>
inline bool chkmax(T &x, T y) {
return (y > x) ? (x = y, 1) : 0;
}
inline int read() {
int x;
char c;
int f = 1;
while ((c = getchar()) != '-' && (c > '9' || c < '0'))
;
if (c == '-') f = -1, c = getchar();
x = c ^ '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 1) + (x << 3) + (c ^ '0');
return x * f;
}
inline long long readll() {
long long x;
char c;
int f = 1;
while ((c = getchar()) != '-' && (c > '9' || c < '0'))
;
if (c == '-') f = -1, c = getchar();
x = c ^ '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 1ll) + (x << 3ll) + (c ^ '0');
return x * f;
}
int n, m, mod, ans;
int ksm(int x, int y) {
int res = 1;
while (y) {
if (y & 1) res = (long long)res * x % mod;
x = (long long)x * x % mod;
y >>= 1;
}
return res;
}
int main() {
n = read(), m = read() - 1, mod = read();
for (register int i = 1, iend = m; i <= iend; ++i)
if (n / (1 << i) == 0) return printf("0\n"), 0;
int u = n / (1 << m), v = u + 1;
int t2 = n - ((n / (1 << m)) << m), t1 = (1 << m) - t2;
ans =
(ans + (long long)u * (u - 1) / 2 % mod * (mod + 1) / 2 % mod * t1) % mod;
ans =
(ans + (long long)v * (v - 1) / 2 % mod * (mod + 1) / 2 % mod * t2) % mod;
for (register int i = 2, iend = u + v; i <= iend; ++i)
ans =
(ans + (long long)t1 * t2 % mod * (min(i - 1, u) - max(1, i - v) + 1) %
mod * (i - 2) % mod * ksm(i * 2, mod - 2)) %
mod;
for (register int i = 2, iend = u * 2; i <= iend; ++i)
ans = (ans + (long long)t1 * (t1 - 1) / 2 % mod *
(min(i - 1, u) - max(1, i - u) + 1) % mod * (i - 2) % mod *
ksm(i * 2, mod - 2)) %
mod;
t1 = t2, u = v;
for (register int i = 2, iend = u * 2; i <= iend; ++i)
ans = (ans + (long long)t1 * (t1 - 1) / 2 % mod *
(min(i - 1, u) - max(1, i - u) + 1) % mod * (i - 2) % mod *
ksm(i * 2, mod - 2)) %
mod;
printf("%d\n", ans);
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class S, class T>
ostream& operator<<(ostream& o, const pair<S, T>& p) {
return o << "(" << p.first << "," << p.second << ")";
}
template <class T>
ostream& operator<<(ostream& o, const vector<T>& vc) {
o << "{";
for (const T& v : vc) o << v << ",";
o << "}";
return o;
}
using ll = long long;
template <class T>
using V = vector<T>;
template <class T>
using VV = vector<vector<T>>;
constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); }
unsigned int mod = 1;
struct ModInt {
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
uint v;
ModInt() : v(0) {}
ModInt(ll _v) : v(normS(_v % mod + mod)) {}
explicit operator bool() const { return v != 0; }
static uint normS(const uint& x) { return (x < mod) ? x : x - mod; }
static ModInt make(const uint& x) {
ModInt m;
m.v = x;
return m;
}
ModInt operator+(const ModInt& b) const { return make(normS(v + b.v)); }
ModInt operator-(const ModInt& b) const { return make(normS(v + mod - b.v)); }
ModInt operator-() const { return make(normS(mod - v)); }
ModInt operator*(const ModInt& b) const { return make((ull)v * b.v % mod); }
ModInt operator/(const ModInt& b) const { return *this * b.inv(); }
ModInt& operator+=(const ModInt& b) { return *this = *this + b; }
ModInt& operator-=(const ModInt& b) { return *this = *this - b; }
ModInt& operator*=(const ModInt& b) { return *this = *this * b; }
ModInt& operator/=(const ModInt& b) { return *this = *this / b; }
ModInt& operator++(int) { return *this = *this + 1; }
ModInt& operator--(int) { return *this = *this - 1; }
ll extgcd(ll a, ll b, ll& x, ll& y) const {
ll p[] = {a, 1, 0}, q[] = {b, 0, 1};
while (*q) {
ll t = *p / *q;
for (int i = 0; i < (int)(3); i++) swap(p[i] -= t * q[i], q[i]);
}
if (p[0] < 0)
for (int i = 0; i < (int)(3); i++) p[i] = -p[i];
x = p[1], y = p[2];
return p[0];
}
ModInt inv() const {
ll x, y;
extgcd(v, mod, x, y);
return make(normS(x + mod));
}
ModInt pow(ll p) const {
if (p < 0) return inv().pow(-p);
ModInt a = 1;
ModInt x = *this;
while (p) {
if (p & 1) a *= x;
x *= x;
p >>= 1;
}
return a;
}
bool operator==(const ModInt& b) const { return v == b.v; }
bool operator!=(const ModInt& b) const { return v != b.v; }
friend istream& operator>>(istream& o, ModInt& x) {
ll tmp;
o >> tmp;
x = ModInt(tmp);
return o;
}
friend ostream& operator<<(ostream& o, const ModInt& x) { return o << x.v; }
};
using mint = ModInt;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
int N, K;
cin >> N >> K >> mod;
K--;
V<int> s = {N};
for (int i = 0; i < (int)(K); i++) {
if ((int)s.size() == N) break;
V<int> ns;
for (int v : s) {
ns.push_back((v + 1) / 2);
if (v / 2 != 0) ns.push_back(v / 2);
}
s = ns;
}
true;
sort(s.begin(), s.end());
V<int> v, n;
{
int c = 0;
for (int x : s)
if (s[0] == x) c++;
v.push_back(s[0]);
n.push_back(c);
if (c != (int)s.size()) {
v.push_back(s.back());
n.push_back((int)s.size() - c);
}
}
mint res = 0;
for (int v : s) res += mint(v) * (v - 1) / 4;
for (int i = 0; i < (int)(v.size()); i++)
for (int j = 0; j < (int)(i + 1); j++) {
mint tmp = mint(v[i]) * v[j] / 2;
for (int x = 2; x <= v[i] + v[j]; x++) {
mint num = x - 1 - max(x - 1 - v[i], 0) - max(x - 1 - v[j], 0);
tmp -= num / x;
}
res += tmp * (i == j ? n[i] * (n[i] - 1) / 2 : n[i] * n[j]);
}
cout << res << endl;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
long long mod = 0;
inline long long pls(long long a, long long b) {
return a + b < mod ? a + b : a + b - mod;
}
inline long long dec(long long a, long long b) {
return a >= b ? a - b : a - b + mod;
}
int len1 = 0, len2 = 0, c1 = 0, c2 = 0;
void dfs(int L, int R, int h) {
if (L > R) return;
if (h <= 1 || L == R) {
int len = R - L + 1;
if (len1 == 0) {
len1 = len;
c1 = 1;
} else if (len1 == len)
++c1;
else if (len2 == 0) {
len2 = len;
c2 = 1;
} else
++c2;
return;
}
int mid = (L + R) >> 1;
dfs(L, mid, h - 1);
dfs(mid + 1, R, h - 1);
}
long long inv[100003], sum[100003];
void pre() {
inv[1] = inv[0] = 1;
for (int i = 2; i <= 100000; ++i)
inv[i] = (mod - mod / i) * inv[mod % i] % mod;
for (int i = 1; i <= 100000; ++i) sum[i] = pls(sum[i - 1], inv[i]);
}
long long calc(int A, int B) {
if (A == 0 || B == 0) return 0;
long long ret = (long long)A * B % mod * inv[2] % mod;
for (int i = 1; i <= A; ++i) ret = dec(ret, dec(sum[i + B], sum[i]));
return ret;
}
int main() {
int n = 0, k = 0;
scanf("%d %d %lld", &n, &k, &mod);
dfs(1, n, k);
pre();
long long ans =
pls((long long)len1 * (len1 - 1ll) % mod * inv[4] % mod * c1 % mod,
(long long)len2 * (len2 - 1ll) % mod * inv[4] % mod * c2 % mod);
ans = pls(ans, calc(len1, len1) * c1 % mod * (c1 - 1ll) % mod * inv[2] % mod);
ans = pls(ans, calc(len2, len2) * c2 % mod * (c2 - 1ll) % mod * inv[2] % mod);
ans = pls(ans, calc(len1, len2) * c1 % mod * c2 % mod);
printf("%lld", ans);
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class T>
int chkmax(T& a, T b) {
if (b > a) {
a = b;
return 1;
}
return 0;
}
template <class T>
int chkmin(T& a, T b) {
if (b < a) {
a = b;
return 1;
}
return 0;
}
template <class iterator>
void output(iterator begin, iterator end, ostream& out = cerr) {
while (begin != end) {
out << (*begin) << " ";
begin++;
}
out << '\n';
}
template <class T>
void output(T x, ostream& out = cerr) {
output(x.begin(), x.end(), out);
}
void fast_io() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
}
int n, k;
long long MOD;
long long power(long long a, long long deg) {
long long res = 1;
while (deg) {
if ((deg & 1LL) == 0) {
a = (a * a) % MOD;
deg >>= 1;
} else {
res = (res * a) % MOD;
deg -= 1;
}
}
return res;
}
long long inv(long long a) { return power(a, MOD - 2); }
long long calc(int len1, int len2) {
long long res = 0;
for (int i = 2; i <= len1 + len2; ++i) {
long long cnt = (long long)(min(len1, i - 1) - max(1, i - len2) + 1);
res = (res + cnt * inv(i)) % MOD;
}
long long p_len = ((long long)len1 * (long long)len2) % MOD;
res = ((p_len * inv(2) - res) % MOD + MOD) % MOD;
return res;
}
long long calc(int _len) {
long long len = (long long)(_len);
long long x = (len * (len - 1)) % MOD;
x = (x * inv(4)) % MOD;
return x;
}
const int LG = 20;
signed main() {
cin >> n >> k >> MOD;
map<int, int> mp;
mp[n] = 1;
for (int i = 0; i < min(k - 1, LG); ++i) {
map<int, int> new_mp;
for (auto pp : mp) {
int key = pp.first, val = pp.second;
if (key == 1) {
new_mp[key] += val;
} else {
new_mp[key / 2] += val;
new_mp[key - key / 2] += val;
}
}
mp = new_mp;
}
long long ans = 0;
vector<pair<int, int> > v;
for (auto pp : mp) {
v.push_back(pp);
}
for (int i = 0; i < v.size(); ++i) {
ans = (ans + calc(v[i].first) * v[i].second) % MOD;
}
for (int i = 0; i < v.size(); ++i) {
long long cnt = ((v[i].second) * (v[i].second - 1)) % MOD;
cnt = (cnt * inv(2)) % MOD;
ans = (ans + calc(v[i].first, v[i].first) * cnt) % MOD;
}
if (v.size() >= 2) {
long long cnt = (v[0].second * v[1].second) % MOD;
ans = (ans + calc(v[0].first, v[1].first) * cnt) % MOD;
}
cout << ans << '\n';
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &x, T y) {
return y < x ? x = y, 1 : 0;
}
template <typename T>
inline bool chkmax(T &x, T y) {
return x < y ? x = y, 1 : 0;
}
const int INF = 0x3f3f3f3f;
const int N = 1e5 + 10;
int cnt[N];
int mod;
inline int read() {
int x = 0, flag = 1;
char ch = getchar();
while (!isdigit(ch) && ch != '-') ch = getchar();
if (ch == '-') flag = -1, ch = getchar();
while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch - '0'), ch = getchar();
return x * flag;
}
inline int fpm(int a, int b) {
int res = 1;
while (b) {
if (b & 1) res = 1ll * res * a % mod;
a = 1ll * a * a % mod, b /= 2;
}
return res;
}
inline void Dfs(int n, int k) {
if (n == 1 || k == 1) {
cnt[n]++;
return;
}
Dfs(n / 2, k - 1), Dfs((n + 1) / 2, k - 1);
}
inline int Calc(int x, int y) {
int res = 0;
for (int i = (2), iend = (x + y); i <= iend; i++)
res = (res + 1ll * min(x + y - i + 1, i - 1) * (i - 2) % mod *
fpm(2 * i, mod - 2)) %
mod;
return res;
}
int main() {
int n = read(), k = read(), ans = 0;
mod = read();
Dfs(n, k);
for (int i = (1), iend = (n); i <= iend; i++)
if (cnt[i]) {
ans = (ans + 1ll * cnt[i] * i % mod * (i - 1) % mod * fpm(4, mod - 2)) %
mod;
ans = (ans + 1ll * cnt[i] * (cnt[i] - 1) / 2 % mod * Calc(i, i)) % mod;
}
for (int i = (1), iend = (n); i <= iend; i++)
if (cnt[i])
for (int j = (i + 1), jend = (n); j <= jend; j++)
if (cnt[j])
ans = (ans + 1ll * cnt[i] * cnt[j] % mod * Calc(i, j)) % mod;
printf("%d\n", ans);
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 51;
map<int, int> mp;
int n, kk, MOD, res, u, v, w, x;
int invf[MAXN], pr[MAXN];
inline int read() {
register int num = 0, neg = 1;
register char ch = getchar();
while (!isdigit(ch) && ch != '-') {
ch = getchar();
}
if (ch == '-') {
neg = -1;
ch = getchar();
}
while (isdigit(ch)) {
num = (num << 3) + (num << 1) + (ch - '0');
ch = getchar();
}
return num * neg;
}
inline void dfs(int l, int r, int depth) {
if (depth >= kk || l == r) {
return (void)mp[r - l + 1]++;
}
int mid = (l + r) >> 1;
dfs(l, mid, depth + 1), dfs(mid + 1, r, depth + 1);
}
inline int calc(int x, int y) {
int res = 0;
for (register int i = 1; i <= x; i++) {
res = (res + (pr[i + y] - pr[i] + MOD) % MOD * 2 % MOD) % MOD;
}
return (((long long int)x * y - res) % MOD + MOD) % MOD;
}
int main() {
n = read(), kk = read(), MOD = read(), invf[1] = pr[1] = 1, dfs(1, n, 1);
for (register int i = 2; i <= max(n, 2); i++) {
invf[i] = MOD - (long long int)(MOD / i) * invf[MOD % i] % MOD,
pr[i] = (pr[i - 1] + invf[i]) % MOD;
}
for (auto i : mp) {
tie(u, v) = i, res = (res + (long long int)u * (u - 1) / 2 % MOD * v) % MOD;
v >= 2
? res = (res + (long long int)v * (v - 1) / 2 % MOD * calc(u, u)) % MOD
: 1;
}
for (auto i : mp) {
for (auto j : mp) {
if (i.first < j.first) {
tie(u, v) = i, tie(w, x) = j;
res = (res + (long long int)calc(u, w) * v % MOD * x % MOD) % MOD;
}
}
}
printf("%d\n", (long long int)res * invf[2] % MOD);
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, k, P;
vector<int> pos;
int cnt[N];
void solve(int l, int r, int k) {
if (k <= 1 || l == r) {
if (cnt[r - l + 1] == 0) pos.emplace_back(r - l + 1);
cnt[r - l + 1]++;
return;
}
int mid = (l + r) / 2;
solve(l, mid, k - 1);
solve(mid + 1, r, k - 1);
return;
}
long long inv[N], sinv[N];
void init(int n = 100000) {
inv[1] = 1;
for (int i = 2; i <= n; i++) inv[i] = (P - P / i) * inv[P % i] % P;
sinv[0] = 0;
for (int i = 1; i <= n; i++) sinv[i] = (sinv[i - 1] + inv[i]) % P;
return;
}
long long calc(int x, int y) {
long long ans = 0;
for (int i = 1; i <= x; i++) {
long long res = inv[2] * y % P;
res = (res - (sinv[i + y] - sinv[i] + P) % P + P) % P;
ans = (ans + res) % P;
}
return ans;
}
int main() {
scanf("%d%d%d", &n, &k, &P);
init();
solve(1, n, k);
int len = pos.size();
long long ans = 0;
for (int i = 0; i < len; i++)
ans =
(ans + 1LL * pos[i] * (pos[i] - 1) % P * inv[4] % P * cnt[pos[i]] % P) %
P;
for (int i = 0; i < len; i++)
for (int j = i + 1; j < len; j++)
ans =
(ans + calc(pos[i], pos[j]) * cnt[pos[i]] % P * cnt[pos[j]] % P) % P;
for (int i = 0; i < len; i++)
ans = (ans + calc(pos[i], pos[i]) * cnt[pos[i]] % P * (cnt[pos[i]] - 1) %
P * inv[2] % P) %
P;
printf("%lld", ans);
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 2;
int mod, num1 = 0, num2 = 0, cnt1 = 0, cnt2 = 0, rev[N], sum[N];
void add(int &x, int y) {
x += y;
if (x >= mod) {
x -= mod;
}
}
void sub(int &x, int y) {
x -= y;
if (x < 0) {
x += mod;
}
}
int mul(int x, int y) { return (1ll * x * y) % mod; }
int binpow(int x, int y) {
int tich = 1;
while (y) {
if (y & 1) {
tich = mul(tich, x);
}
x = mul(x, x);
y >>= 1;
}
return tich;
}
void recur(int l, int r, int dep) {
if (dep < 2 || l == r) {
if (!num1 || r - l + 1 == num1) {
num1 = r - l + 1;
cnt1++;
} else {
num2 = r - l + 1;
cnt2++;
}
return;
}
recur(l, (l + r) / 2, dep - 1);
recur((l + r) / 2 + 1, r, dep - 1);
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n, i, j, k, l, dep, ans = 0;
cin >> n >> dep >> mod;
for (i = 1; i < N; i++) {
rev[i] = binpow(i, mod - 2);
sum[i] = sum[i - 1];
add(sum[i], rev[i]);
}
recur(1, n, dep);
if (num1) {
add(ans, mul(cnt1, mul(num1, mul(num1 - 1, rev[4]))));
j = mul(mul(cnt1, cnt1 - 1), rev[2]);
for (i = 1; i <= num1; i++) {
add(ans, mul(mul(rev[2], num1), j));
sub(ans, mul((sum[i + num1] - sum[i] + mod) % mod, j));
}
}
if (num2) {
add(ans, mul(cnt2, mul(num2, mul(num2 - 1, rev[4]))));
j = mul(mul(cnt2, cnt2 - 1), rev[2]);
for (i = 1; i <= num2; i++) {
add(ans, mul(mul(rev[2], num2), j));
sub(ans, mul((sum[i + num2] - sum[i] + mod) % mod, j));
}
}
if (num1 && num2) {
j = mul(cnt1, cnt2);
for (i = 1; i <= num1; i++) {
add(ans, mul(mul(rev[2], num2), j));
sub(ans, mul((sum[i + num2] - sum[i] + mod) % mod, j));
}
}
cout << ans;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 5;
int n, k, mod, cnt[MAXN], inv[MAXN] = {0, 1}, sum[MAXN];
int a, b, ans;
inline int read() {
int x = 0;
char ch = getchar();
while (!isdigit(ch)) ch = getchar();
while (isdigit(ch)) {
x = x * 10 + ch - '0';
ch = getchar();
}
return x;
}
inline void Add(int &x, int y) { x += y, x >= mod && (x -= mod); }
inline void prepare() {
for (int i = 2; i <= n * 2; i++)
inv[i] = mod - 1ll * mod / i * inv[mod % i] % mod;
for (int i = 1; i <= n * 2; i++) sum[i] = (sum[i - 1] + inv[i]) % mod;
}
void dfs_pre(int l, int r, int dep, int &len) {
if (dep == k || l == r) return cnt[len = r - l + 1]++, void();
int mid = (l + r) >> 1;
dfs_pre(l, mid, dep + 1, len);
dfs_pre(mid + 1, r, dep + 1, len);
}
inline int calc(int x, int y) {
int ans = 0;
for (int i = 1; i <= x; i++) Add(ans, (mod + sum[i + y] - sum[i]) % mod);
return ans;
}
int main() {
n = read(), k = read(), mod = read();
if (k >= 19) {
puts("0");
return 0;
}
dfs_pre(1, n, 1, a), prepare();
if (cnt[a - 1])
b = a - 1;
else if (cnt[a + 1])
b = a + 1;
assert((cnt[a - 1] & cnt[a + 1]) == 0);
Add(ans, 1ll * calc(a, a) * cnt[a] % mod * (cnt[a] - 1) % mod * inv[2] % mod);
Add(ans, 1ll * calc(b, b) * cnt[b] % mod * (cnt[b] - 1) % mod * inv[2] % mod);
Add(ans, 1ll * calc(a, b) * cnt[a] % mod * cnt[b] % mod);
printf("%lld\n", (1ll * n * (n - 1) % mod * inv[4] % mod - ans + mod) % mod);
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 7;
int n, k, p, sum, a[2], b[2], inv[N], res[N];
inline void adds(int u) {
if (a[0] == 0)
a[0] = u;
else if (a[0] != u)
a[1] = u;
if (a[0] == u) b[0]++;
if (a[1] == u) b[1]++;
}
inline void solve(int l, int r, int t) {
if (t == 1 || l == r) {
adds(r - l + 1);
return;
}
int d = (l + r) >> 1;
solve(l, d, t - 1), solve(d + 1, r, t - 1);
}
inline int getsum(int x, int y) {
int ans = 0;
memset(res, 0, sizeof(res));
for (int i = 1; i <= x; i++) res[i + 1]++, res[i + y + 1]--;
for (int i = 1; i <= x + y; i++)
res[i] += res[i - 1],
ans = (ans + 1ll * res[i] % p * (-inv[i] + inv[2] + p)) % p;
return ans;
}
int main() {
cin >> n >> k >> p, solve(1, n, k), inv[0] = inv[1] = 1;
for (int i = 2; i <= 123456; i++) inv[i] = 1ll * (p - p / i) * inv[p % i] % p;
sum = (1ll * (a[0] - 1) * a[0] % p * inv[4] % p * b[0] % p +
1ll * (a[1] - 1) * a[1] % p * inv[4] % p * b[1] % p) %
p;
sum = (sum + 1ll * b[0] * b[1] % p * getsum(a[0], a[1])) % p;
sum = (sum + 1ll * b[0] * (b[0] - 1) / 2 % p * getsum(a[0], a[0])) % p;
sum = (sum + 1ll * b[1] * (b[1] - 1) / 2 % p * getsum(a[1], a[1])) % p;
cout << sum << endl;
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
template <class t, class u>
void chmax(t& first, u second) {
if (first < second) first = second;
}
template <class t, class u>
void chmin(t& first, u second) {
if (second < first) first = second;
}
template <class t>
using vc = vector<t>;
template <class t>
using vvc = vc<vc<t>>;
using pi = pair<ll, ll>;
using vi = vc<ll>;
template <class t, class u>
ostream& operator<<(ostream& os, const pair<t, u>& p) {
return os << "{" << p.first << "," << p.second << "}";
}
template <class t>
ostream& operator<<(ostream& os, const vc<t>& v) {
os << "{";
for (auto e : v) os << e << ",";
return os << "}";
}
using uint = unsigned;
using ull = unsigned long long;
uint mod = 1;
struct mint {
uint v;
mint(ll vv = 0) { s(vv % mod + mod); }
mint& s(uint vv) {
v = vv < mod ? vv : vv - mod;
return *this;
}
mint operator-() const { return mint() - *this; }
mint& operator+=(const mint& rhs) { return s(v + rhs.v); }
mint& operator-=(const mint& rhs) { return s(v + mod - rhs.v); }
mint& operator*=(const mint& rhs) {
v = ull(v) * rhs.v % mod;
return *this;
}
mint& operator/=(const mint& rhs) { return *this *= rhs.inv(); }
mint operator+(const mint& rhs) const { return mint(*this) += rhs; }
mint operator-(const mint& rhs) const { return mint(*this) -= rhs; }
mint operator*(const mint& rhs) const { return mint(*this) *= rhs; }
mint operator/(const mint& rhs) const { return mint(*this) /= rhs; }
mint pow(ll n) const {
mint res(1), x(*this);
while (n) {
if (n & 1) res *= x;
x *= x;
n >>= 1;
}
return res;
}
mint inv() const { return pow(mod - 2); }
friend ostream& operator<<(ostream& os, const mint& m) { return os << m.v; }
};
const ll Vmax = (1 << 21) + 10;
mint fact[Vmax], finv[Vmax], inv[Vmax];
void initfact() {
fact[0] = 1;
for (ll i = ll(1); i < ll(Vmax); i++) {
fact[i] = fact[i - 1] * i;
}
finv[Vmax - 1] = fact[Vmax - 1].inv();
for (ll i = Vmax - 2; i >= 0; i--) {
finv[i] = finv[i + 1] * (i + 1);
}
for (ll i = Vmax - 1; i >= 1; i--) {
inv[i] = finv[i] * fact[i - 1];
}
}
mint choose(ll n, ll k) { return fact[n] * finv[n - k] * finv[k]; }
mint binom(ll first, ll second) {
return fact[first + second] * finv[first] * finv[second];
}
mint catalan(ll n) {
return binom(n, n) - (n - 1 >= 0 ? binom(n - 1, n + 1) : 0);
}
signed main() {
cin.tie(0);
ios::sync_with_stdio(0);
cout << fixed << setprecision(20);
ll n, h;
cin >> n >> h;
cin >> mod;
initfact();
chmin(h, 20);
ll s = min(ll(1) << (h - 1), n);
ll sz[2], cnt[2];
sz[0] = n / s;
sz[1] = sz[0] + 1;
cnt[1] = n % s;
cnt[0] = s - cnt[1];
mint ans = 0;
for (ll i = ll(1); i < ll(n); i++) {
ll k = n - i + 1;
mint den = choose(n, k) * k * (k - 1);
mint num = 0;
for (ll first = ll(0); first < ll(2); first++)
for (ll second = ll(0); second < ll(2); second++) {
mint wab = cnt[first] * (cnt[second] - (first == second));
mint sum = 0;
for (ll c = ll(0); c < ll(2); c++)
for (ll d = ll(0); d < ll(2); d++) {
mint x = 0;
ll rem = n;
if (c) rem -= sz[first];
if (d) rem -= sz[second];
if (rem >= k) x = choose(rem, k);
if (c ^ d)
sum -= x;
else
sum += x;
}
num += wab * sum;
}
ans += (mint(1) - num / den) / 2 * i;
}
cout << ans << endl;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
int md;
inline void add(int &a, int b) {
a += b;
if (a >= md) a -= md;
}
inline void sub(int &a, int b) {
a -= b;
if (a < 0) a += md;
}
inline int mul(int a, int b) { return (int)((long long)a * b % md); }
inline int power(int a, long long b) {
int res = 1;
while (b > 0) {
if (b & 1) {
res = mul(res, a);
}
a = mul(a, a);
b >>= 1;
}
return res;
}
inline int inv(int a) {
a %= md;
if (a < 0) a += md;
int b = md, u = 0, v = 1;
while (a) {
int t = b / a;
b -= t * a;
swap(a, b);
u -= t * v;
swap(u, v);
}
assert(b == 1);
if (u < 0) u += md;
return u;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, k;
cin >> n >> k >> md;
if (k >= 20 || n <= (1 << (k - 1))) {
cout << 0 << '\n';
return 0;
}
int bc = (1 << (k - 1));
int small_size = n / bc;
int big_size = small_size + 1;
int big_cnt = n % bc;
int small_cnt = bc - big_cnt;
vector<int> blocks(bc);
for (int i = 0; i < n; i++) {
blocks[i % (int)blocks.size()]++;
}
map<int, int> mp;
for (int x : blocks) {
mp[x]++;
}
vector<int> fact(n + 1), inv_fact(n + 1);
fact[0] = inv_fact[0] = 1;
for (int i = 1; i <= n; i++) {
fact[i] = mul(fact[i - 1], i);
inv_fact[i] = inv(fact[i]);
}
int ans = 0;
for (int b1id = 0; b1id < bc; b1id++) {
int b = blocks[b1id];
add(ans, mul(mul(b, b - 1), inv(4)));
}
vector<int> sum_inv(n + 1);
for (int i = 0; i < n; i++) {
sum_inv[i + 1] = sum_inv[i];
add(sum_inv[i + 1], inv(i + 1));
}
for (int b1id = 0; b1id < bc; b1id++) {
int b1 = blocks[b1id];
if (b1 == small_size)
small_cnt--;
else
big_cnt--;
for (int x = 2; x <= b1; x++) {
if (small_cnt > 0) {
int aux = sum_inv[x + small_size];
sub(aux, sum_inv[x]);
int prob = mul(x - 1, aux);
add(ans, mul(small_cnt, mul(prob, inv(2))));
}
if (big_cnt > 0) {
int aux = sum_inv[x + big_size];
sub(aux, sum_inv[x]);
int prob = mul(x - 1, aux);
add(ans, mul(big_cnt, mul(prob, inv(2))));
}
}
if (b1 == small_size)
small_cnt++;
else
big_cnt++;
}
cout << ans << '\n';
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class T>
T gi() {
T x = 0;
bool f = 0;
char c = getchar();
while (c != '-' && (c < '0' || c > '9')) c = getchar();
if (c == '-') f = 1, c = getchar();
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f ? -x : x;
}
const int N = 1e5 + 10;
int Mod, inv[N];
unordered_map<int, int> t;
void solve(int l, int r, int k) {
if (k == 1 || l == r) return (void)t[r - l + 1]++;
int mid = (l + r) >> 1;
solve(l, mid, k - 1), solve(mid + 1, r, k - 1);
}
int C(int n) { return 1ll * n * (n - 1) / 2 % Mod; }
int calc(int x, int y) {
int res = 1ll * (Mod + 1) / 2 * x % Mod * y % Mod;
for (int i = 1; i <= x; i++)
res = (1ll * res + Mod - inv[i + y] + inv[i]) % Mod;
return res;
}
int main() {
int n = gi<int>(), k = gi<int>(), ans = 0;
Mod = gi<int>();
inv[1] = 1;
for (int i = 2; i <= n; i++)
inv[i] = 1ll * (Mod - Mod / i) * inv[Mod % i] % Mod;
for (int i = 2; i <= n; i++) (inv[i] += inv[i - 1]) %= Mod;
solve(1, n, k);
for (auto i : t)
ans = (ans + 1ll * C(i.first) * i.second % Mod * ((Mod + 1) / 2)) % Mod,
ans = (ans + 1ll * C(i.second) * calc(i.first, i.first)) % Mod;
for (auto i : t)
for (auto j : t) {
if (j.first >= i.first) break;
ans = (ans + 1ll * calc(j.first, i.first) * j.second % Mod * i.second) %
Mod;
}
cout << ans << endl;
return 0;
}
| 13 | CPP |
#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
inline void chkmin(T1 &x, T2 y) {
if (y < x) x = y;
}
template <class T1, class T2>
inline void chkmax(T1 &x, T2 y) {
if (y > x) x = y;
}
const int BUF_SIZE = 1 << 20;
char buf[BUF_SIZE], *P1 = buf, *P2 = buf, obuf[BUF_SIZE], *PO = obuf;
inline char getc() {
if (P1 == P2) P2 = (P1 = buf) + fread(buf, 1, BUF_SIZE, stdin);
return P1 == P2 ? EOF : *P1++;
}
inline void read(int &x) {
register char ch = getc();
x = 0;
while (!isdigit(ch)) ch = getc();
while (isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getc();
}
inline void flushO() {
fwrite(obuf, PO - obuf, 1, stdout);
PO = obuf;
}
inline void putc(char ch) {
if (PO == obuf + (BUF_SIZE)) flushO();
*PO++ = ch;
}
inline void prints(char s[]) {
for (char *ss = s; *ss != '\0'; ss++) putc(*ss);
}
inline void write(long long x) {
if (x > 9) write(x / 10);
putc(x % 10 ^ 48);
}
const int N = 100005;
int MOD;
inline int mo(int x) { return x >= MOD ? x - MOD : x; }
struct mint {
int x;
mint() {}
mint(int a) { x = a; }
};
inline mint operator+(mint a, mint b) { return mo(a.x + b.x); }
inline mint operator+=(mint &a, mint b) { return a = a + b; }
inline mint operator-(mint a, mint b) { return mo(a.x + MOD - b.x); }
inline mint operator-(mint a) { return mo(MOD - a.x); }
inline mint operator-=(mint &a, mint b) { return a = a - b; }
inline mint operator*(mint a, mint b) { return 1ll * a.x * b.x % MOD; }
inline mint operator*=(mint &a, mint b) { return a = a * b; }
inline mint operator^(mint a, int b) {
mint res = mint{1};
for (; b; b >>= 1, a *= a)
if (b & 1) res *= a;
return res;
}
inline mint Inv(mint a) { return a ^ MOD - 2; }
inline mint operator/(mint a, mint b) { return a * Inv(b); }
inline mint operator/=(mint &a, mint b) { return a = a / b; }
int n, k, la, lb, ca, cb;
mint inv[N], sinv[N], ans;
inline void math_init(int n) {
inv[1] = 1;
for (int i = 2; i <= (n); i++) inv[i] = -inv[MOD % i] * (MOD / i);
for (int i = 1; i <= (n); i++) sinv[i] = sinv[i - 1] + inv[i];
}
void solve(int l, int r, int h) {
if (h <= 1 || l == r) {
int len = r - l + 1;
ans += inv[4] * len * (len - 1) + (ca * la + cb * lb) * inv[2] * len;
for (int i = 1; i <= (len); i++)
ans -= (sinv[la + i] - sinv[i]) * ca + (sinv[lb + i] - sinv[i]) * cb;
if (!la) la = len;
if (len != la && !lb) lb = len;
if (len == la)
ca++;
else
cb++;
return;
}
int mid = l + r >> 1;
solve(l, mid, h - 1);
solve(mid + 1, r, h - 1);
}
int main() {
scanf("%d%d%d", &n, &k, &MOD);
math_init(max(n, 4));
solve(1, n, k);
printf("%d", ans);
}
| 13 | CPP |