UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#200683#2634. 经典题Anonyme1002308ms2380kbC++111.9kb2024-01-09 11:16:032024-01-09 12:14:01

answer

#include <bits/stdc++.h>
using namespace std;

#define QwQ330AwA return 0
#define ll long long

const int N = 1e5 + 10;
const int mod = 1e9 + 7;

int p[N], cnt;
int lim;
int phi[N];
bool vis[N];
int sum[N];
int n, a;

void init() {
	phi[1] = 1;
	for (int i = 2; i <= lim; i++) {
		if (!vis[i]) phi[i] = i - 1, p[++cnt] = i;
		for (int j = 1; j <= cnt && 1ll * i * p[j] <= lim; j++) {
			vis[i * p[j]] = 1;
			if (i % p[j] == 0) {
				phi[i * p[j]] = phi[i] * p[j];
				break;
			} else phi[i * p[j]] = phi[i] * phi[p[j]];
		}
	}
	for (int i = 1; i <= lim; i++) sum[i] = (sum[i - 1] + phi[i]) % mod;
}

ll ksm(ll a, int b) {
	ll ans = 1;
	for (; b; b >>= 1, a = a * a % mod) {
		if (b & 1) ans = ans * a % mod;
	}
	return ans;
}

unordered_map <int, int> f;

ll Sum(int n) {
	if (n <= lim) return sum[n];
	if (f.count(n)) return f[n];
	ll now = 1ll * n * (n + 1) / 2;
	now %= mod;
	for (int l = 2, r; l <= n; l = r + 1) {
		r = n / (n / l);
		now = (now - Sum(n / l) * (r - l + 1) % mod + mod) % mod;
	}
	return f[n] = now;
}

ll cal(int l, int r) {
	return (ksm(a, r + 1) - ksm(a, l) + mod) * ksm(a - 1, mod - 2) % mod;
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> n >> a;
	if (a == 1) {
		cout << 2ll * n % mod * n % mod;
		return 0;
	}
	lim = 1e5;
	init();
	ll ans = 0;
	ll cnt = 0;
	for (int l = 1, r; l <= n; l = r + 1) {
		r = n / (n / l);
		ll w = 1;
		int x = n / l;
		w = (w + Sum(x)) % mod;
		x /= 2;
		while (x) w = (w - Sum(x) + mod) % mod, x /= 2;
		w = w * ksm(2, mod - 2) % mod;
		ans = (ans + cal(l, r) * w % mod) % mod;
		cnt = (cnt + (r - l + 1) * w % mod) % mod;
	}
	ans = (ans * 2 % mod - cal(1, n) + mod) % mod;
	cnt = (cnt * 2 % mod - n + mod) % mod;
	ans = (ans + 1ll * n * n % mod) % mod;
	if (a & 1) ans = (ans + 1ll * n * n - cnt) % mod;
	ans = (ans % mod + mod) % mod;
	cout << ans;
	QwQ330AwA;
}

Details

小提示:点击横条可展开更详细的信息

Test #1:

score: 5
Accepted
time: 0ms
memory: 2176kb

input:

7 2

output:

335

result:

ok single line: '335'

Test #2:

score: 5
Accepted
time: 0ms
memory: 2176kb

input:

10 2

output:

2222

result:

ok single line: '2222'

Test #3:

score: 5
Accepted
time: 0ms
memory: 2172kb

input:

9 6

output:

12093927

result:

ok single line: '12093927'

Test #4:

score: 5
Accepted
time: 0ms
memory: 2176kb

input:

9 3

output:

29780

result:

ok single line: '29780'

Test #5:

score: 5
Accepted
time: 2ms
memory: 2172kb

input:

997 260072224

output:

705382927

result:

ok single line: '705382927'

Test #6:

score: 5
Accepted
time: 0ms
memory: 2176kb

input:

998 212845360

output:

566914370

result:

ok single line: '566914370'

Test #7:

score: 5
Accepted
time: 0ms
memory: 2172kb

input:

999 610209134

output:

317777717

result:

ok single line: '317777717'

Test #8:

score: 5
Accepted
time: 0ms
memory: 2172kb

input:

1000 27188366

output:

318581334

result:

ok single line: '318581334'

Test #9:

score: 5
Accepted
time: 3ms
memory: 2176kb

input:

999999 110895626

output:

77565423

result:

ok single line: '77565423'

Test #10:

score: 5
Accepted
time: 0ms
memory: 2176kb

input:

999998 649841067

output:

708559662

result:

ok single line: '708559662'

Test #11:

score: 5
Accepted
time: 3ms
memory: 2176kb

input:

999998 288899924

output:

152873350

result:

ok single line: '152873350'

Test #12:

score: 5
Accepted
time: 0ms
memory: 2172kb

input:

999997 31048277

output:

61751074

result:

ok single line: '61751074'

Test #13:

score: 5
Accepted
time: 285ms
memory: 2376kb

input:

999999996 178130636

output:

85287040

result:

ok single line: '85287040'

Test #14:

score: 5
Accepted
time: 284ms
memory: 2380kb

input:

999999996 111958784

output:

178608555

result:

ok single line: '178608555'

Test #15:

score: 5
Accepted
time: 292ms
memory: 2376kb

input:

999999996 377230388

output:

559966725

result:

ok single line: '559966725'

Test #16:

score: 5
Accepted
time: 286ms
memory: 2380kb

input:

999999998 257364834

output:

876948557

result:

ok single line: '876948557'

Test #17:

score: 5
Accepted
time: 290ms
memory: 2376kb

input:

999999996 67761122

output:

115966743

result:

ok single line: '115966743'

Test #18:

score: 5
Accepted
time: 286ms
memory: 2380kb

input:

999999998 510674992

output:

440716684

result:

ok single line: '440716684'

Test #19:

score: 5
Accepted
time: 288ms
memory: 2376kb

input:

999999999 45313438

output:

139611576

result:

ok single line: '139611576'

Test #20:

score: 5
Accepted
time: 289ms
memory: 2380kb

input:

999999997 165549638

output:

485984880

result:

ok single line: '485984880'