QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#290653#3295. 循环之美MoRanSky100 ✓282ms122996kbC++231.7kb2023-12-25 06:17:542023-12-25 06:17:54

Judging History

你现在查看的是最新测评结果

  • [2023-12-25 06:17:54]
  • 评测
  • 测评结果:100
  • 用时:282ms
  • 内存:122996kb
  • [2023-12-25 06:17:54]
  • 提交

answer

#include <iostream>
#include <cstdio>
#include <map>
#include <cstring>
using namespace std;

typedef long long LL;

const int S = 1e7 + 1, T = 2005, INF = 0x3f3f3f3f;

int primes[S], tot, fac[S], mu[S];

bool st[S];

int gcd(int a, int b) {
	return b ? gcd(b, a % b) : a;
}

map<int, int> F[T], G[T], mu2;

LL ans;

LL f(int n, int k) {
	if (n == 0) return 0;
	if (n == 1) return 1;
	if (F[k].count(n)) return F[k][n];
	else if (k > 1) {
		int p = fac[k];
		return F[k][n] = (f(n, k / p) + f(n / p, k));
	} else {
		if (n < S) return mu[n];
		int res = 1;
		for (int l = 2, r, t; l <= n; l = r + 1) {
			t = n / l, r = n / t;
			res -= f(t, k) * (r - l + 1);
		}
		return F[k][n] = res;
	}
}

LL g(int n, int k) {
	if (n == 0) return 0;
	if (G[k].count(n)) return G[k][n];
	else if (k > 1) {
		int p = fac[k];
		return G[k][n] = (g(n, k / p) - g(n / p, k / p));
	} else return n;
}

void init() {
	for (int i = 1; i < S; i++) mu[i] = 1;
	for (int i = 2; i < S; i++) {
		if (!st[i]) primes[++tot] = i, mu[i] = -1, fac[i] = i;
		for (int j = 1; primes[j] * i < S; j++) {
			st[primes[j] * i] = true;
			fac[primes[j] * i] = primes[j];
			if (i % primes[j] == 0) {
				mu[i * primes[j]] = 0;
				break;
			}
			mu[i * primes[j]] = -mu[i];
		}
	}
	for (int i = 2; i < S; i++) mu[i] += mu[i - 1];
}

int main() {
	init();
	int n, m, K; scanf("%d%d%d", &n, &m, &K);
	for (int i = 2; i <= K; i++) {
		while (K % i == 0 && (K / i) % i == 0) K /= i;
	}
	for (int l = 1, r, a, b; l <= min(n, m); l = r + 1) {
		a = n / l, b = m / l, r = min(min(n, m), min(n / a, m / b));
		ans += a * (f(r, K) - f(l - 1, K)) * g(b, K);
	}
	printf("%lld\n", ans);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 4
Accepted
time: 95ms
memory: 94500kb

input:

9 19 2

output:

78

result:

ok 1 number(s): "78"

Test #2:

score: 4
Accepted
time: 88ms
memory: 94400kb

input:

97 9998 2

output:

395230

result:

ok 1 number(s): "395230"

Test #3:

score: 4
Accepted
time: 93ms
memory: 94480kb

input:

925 776383828 2

output:

291158369963

result:

ok 1 number(s): "291158369963"

Test #4:

score: 4
Accepted
time: 93ms
memory: 95420kb

input:

8901 326326877 2

output:

1177246855340

result:

ok 1 number(s): "1177246855340"

Test #5:

score: 4
Accepted
time: 89ms
memory: 94388kb

input:

10 18 3

output:

85

result:

ok 1 number(s): "85"

Test #6:

score: 4
Accepted
time: 80ms
memory: 94404kb

input:

99 9124 3

output:

414445

result:

ok 1 number(s): "414445"

Test #7:

score: 4
Accepted
time: 87ms
memory: 94580kb

input:

876 435006787 3

output:

173773888677

result:

ok 1 number(s): "173773888677"

Test #8:

score: 4
Accepted
time: 89ms
memory: 95392kb

input:

9077 999901423 3

output:

4138517057280

result:

ok 1 number(s): "4138517057280"

Test #9:

score: 4
Accepted
time: 106ms
memory: 94708kb

input:

8 18 70

output:

44

result:

ok 1 number(s): "44"

Test #10:

score: 4
Accepted
time: 91ms
memory: 94732kb

input:

99 7689 100

output:

257777

result:

ok 1 number(s): "257777"

Test #11:

score: 4
Accepted
time: 90ms
memory: 97088kb

input:

904 791040170 780

output:

168263165839

result:

ok 1 number(s): "168263165839"

Test #12:

score: 4
Accepted
time: 109ms
memory: 99676kb

input:

9752 700341570 1900

output:

2191452433400

result:

ok 1 number(s): "2191452433400"

Test #13:

score: 4
Accepted
time: 91ms
memory: 99592kb

input:

67890 96085338 98

output:

2313308017745

result:

ok 1 number(s): "2313308017745"

Test #14:

score: 4
Accepted
time: 162ms
memory: 110096kb

input:

199752 831377991 582

output:

49964059187765

result:

ok 1 number(s): "49964059187765"

Test #15:

score: 4
Accepted
time: 154ms
memory: 106336kb

input:

402829 239198013 1974

output:

25093728301882

result:

ok 1 number(s): "25093728301882"

Test #16:

score: 4
Accepted
time: 124ms
memory: 101568kb

input:

920812 97518123 30

output:

22745569858620

result:

ok 1 number(s): "22745569858620"

Test #17:

score: 4
Accepted
time: 219ms
memory: 118220kb

input:

2000000 902323111 630

output:

399982029285532

result:

ok 1 number(s): "399982029285532"

Test #18:

score: 4
Accepted
time: 221ms
memory: 119516kb

input:

5000000 1000000000 1122

output:

1315768281328847

result:

ok 1 number(s): "1315768281328847"

Test #19:

score: 4
Accepted
time: 102ms
memory: 99408kb

input:

10000000 100000000 100

output:

337737297053529

result:

ok 1 number(s): "337737297053529"

Test #20:

score: 4
Accepted
time: 233ms
memory: 118428kb

input:

19283746 992837465 330

output:

4445506723817465

result:

ok 1 number(s): "4445506723817465"

Test #21:

score: 4
Accepted
time: 214ms
memory: 119784kb

input:

20000000 1000000000 1540

output:

5417869022476158

result:

ok 1 number(s): "5417869022476158"

Test #22:

score: 4
Accepted
time: 132ms
memory: 103608kb

input:

79331983 76782798 210

output:

1350083306292618

result:

ok 1 number(s): "1350083306292618"

Test #23:

score: 4
Accepted
time: 102ms
memory: 98596kb

input:

99997327 86263723 1999

output:

5241443368179591

result:

ok 1 number(s): "5241443368179591"

Test #24:

score: 4
Accepted
time: 276ms
memory: 122996kb

input:

967272686 923726686 1260

output:

198034442762888587

result:

ok 1 number(s): "198034442762888587"

Test #25:

score: 4
Accepted
time: 282ms
memory: 122480kb

input:

999193233 997313141 1848

output:

242952866318443894

result:

ok 1 number(s): "242952866318443894"

Extra Test:

score: 0
Extra Test Passed