QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#67552#3295. 循环之美alpha1022100 ✓818ms111328kbC++231.7kb2022-12-10 17:13:302022-12-10 17:13:31

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-12-10 17:13:31]
  • 评测
  • 测评结果:100
  • 用时:818ms
  • 内存:111328kb
  • [2022-12-10 17:13:30]
  • 提交

answer

#include <cstdio>
#include <utility>
#include <algorithm>
#include <map>
#include <unordered_map>
using namespace std;
const int N = 1e9;
const int K = 2e3;
const int CNT = 1e7;
int n,m,k;
int vis[CNT + 5],prime[CNT + 5],cnt,mu[CNT + 5];
int f[K + 5];
unordered_map<int,int> w;
map<pair<int,int>,long long> mem;
long long ans;
int calc(int n)
{
	if(n <= CNT)
		return mu[n];
	if(w.count(n))
		return w[n];
	long long ret = 1;
	for(register int l = 2,r;l <= n;l = r + 1)
	{
		r = n / (n / l);
		ret -= (long long)(r - l + 1) * calc(n / l);
	}
	return w[n] = ret;
}
int F(int n)
{
	return f[k] * (n / k) + f[n % k];
}
long long S(int n,int k)
{
	if(n == 0 || n == 1)
		return n;
	if(k == 1)
		return calc(n);
	if(mem.count(make_pair(n,k)))
		return mem[make_pair(n,k)];
	long long ret = 0;
	for(int i = 1;i * i <= k;++i)
		if(!(k % i))
		{
			if(mu[i] - mu[i - 1])
				ret += (mu[i] - mu[i - 1]) * (mu[i] - mu[i - 1]) * S(n / i,i);
			if(i ^ (k / i) && mu[k / i] - mu[k / i - 1])
				ret += (mu[k / i] - mu[k / i - 1]) * (mu[k / i] - mu[k / i - 1]) * S(n / (k / i),k / i);
		}
	return mem[make_pair(n,k)] = ret;
}
int main()
{
	mu[1] = 1;
	for(register int i = 2;i <= CNT;++i)
	{
		if(!vis[i])
			mu[prime[++cnt] = i] = -1;
		for(register int j = 1;j <= cnt && i * prime[j] <= CNT;++j)
		{
			vis[i * prime[j]] = 1;
			if(!(i % prime[j]))
				break;
			else
				mu[i * prime[j]] = -mu[i];
		}
		mu[i] += mu[i - 1];
	}
	scanf("%d%d%d",&n,&m,&k);
	for(register int i = 1;i <= k;++i)
		f[i] = f[i - 1] + (__gcd(i,k) == 1);
	for(register int l = 1,r;l <= min(n,m);l = r + 1)
	{
		r = min(n / (n / l),m / (m / l));
		ans += (S(r,k) - S(l - 1,k)) * (n / l) * F(m / l);
	}
	printf("%lld\n",ans);
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 4
Accepted
time: 115ms
memory: 83784kb

input:

9 19 2

output:

78

result:

ok 1 number(s): "78"

Test #2:

score: 4
Accepted
time: 121ms
memory: 83792kb

input:

97 9998 2

output:

395230

result:

ok 1 number(s): "395230"

Test #3:

score: 4
Accepted
time: 133ms
memory: 84004kb

input:

925 776383828 2

output:

291158369963

result:

ok 1 number(s): "291158369963"

Test #4:

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

input:

8901 326326877 2

output:

1177246855340

result:

ok 1 number(s): "1177246855340"

Test #5:

score: 4
Accepted
time: 120ms
memory: 83736kb

input:

10 18 3

output:

85

result:

ok 1 number(s): "85"

Test #6:

score: 4
Accepted
time: 126ms
memory: 83976kb

input:

99 9124 3

output:

414445

result:

ok 1 number(s): "414445"

Test #7:

score: 4
Accepted
time: 127ms
memory: 83904kb

input:

876 435006787 3

output:

173773888677

result:

ok 1 number(s): "173773888677"

Test #8:

score: 4
Accepted
time: 114ms
memory: 84532kb

input:

9077 999901423 3

output:

4138517057280

result:

ok 1 number(s): "4138517057280"

Test #9:

score: 4
Accepted
time: 119ms
memory: 83748kb

input:

8 18 70

output:

44

result:

ok 1 number(s): "44"

Test #10:

score: 4
Accepted
time: 114ms
memory: 83796kb

input:

99 7689 100

output:

257777

result:

ok 1 number(s): "257777"

Test #11:

score: 4
Accepted
time: 119ms
memory: 83936kb

input:

904 791040170 780

output:

168263165839

result:

ok 1 number(s): "168263165839"

Test #12:

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

input:

9752 700341570 1900

output:

2191452433400

result:

ok 1 number(s): "2191452433400"

Test #13:

score: 4
Accepted
time: 136ms
memory: 86272kb

input:

67890 96085338 98

output:

2313308017745

result:

ok 1 number(s): "2313308017745"

Test #14:

score: 4
Accepted
time: 212ms
memory: 92684kb

input:

199752 831377991 582

output:

49964059187765

result:

ok 1 number(s): "49964059187765"

Test #15:

score: 4
Accepted
time: 231ms
memory: 91412kb

input:

402829 239198013 1974

output:

25093728301882

result:

ok 1 number(s): "25093728301882"

Test #16:

score: 4
Accepted
time: 180ms
memory: 88404kb

input:

920812 97518123 30

output:

22745569858620

result:

ok 1 number(s): "22745569858620"

Test #17:

score: 4
Accepted
time: 520ms
memory: 103516kb

input:

2000000 902323111 630

output:

399982029285532

result:

ok 1 number(s): "399982029285532"

Test #18:

score: 4
Accepted
time: 416ms
memory: 100744kb

input:

5000000 1000000000 1122

output:

1315768281328847

result:

ok 1 number(s): "1315768281328847"

Test #19:

score: 4
Accepted
time: 145ms
memory: 86828kb

input:

10000000 100000000 100

output:

337737297053529

result:

ok 1 number(s): "337737297053529"

Test #20:

score: 4
Accepted
time: 494ms
memory: 103704kb

input:

19283746 992837465 330

output:

4445506723817465

result:

ok 1 number(s): "4445506723817465"

Test #21:

score: 4
Accepted
time: 432ms
memory: 101316kb

input:

20000000 1000000000 1540

output:

5417869022476158

result:

ok 1 number(s): "5417869022476158"

Test #22:

score: 4
Accepted
time: 261ms
memory: 91780kb

input:

79331983 76782798 210

output:

1350083306292618

result:

ok 1 number(s): "1350083306292618"

Test #23:

score: 4
Accepted
time: 136ms
memory: 85408kb

input:

99997327 86263723 1999

output:

5241443368179591

result:

ok 1 number(s): "5241443368179591"

Test #24:

score: 4
Accepted
time: 818ms
memory: 111328kb

input:

967272686 923726686 1260

output:

198034442762888587

result:

ok 1 number(s): "198034442762888587"

Test #25:

score: 4
Accepted
time: 739ms
memory: 109224kb

input:

999193233 997313141 1848

output:

242952866318443894

result:

ok 1 number(s): "242952866318443894"

Extra Test:

score: 0
Extra Test Passed