[hdu6434]Problem I. Count

Posted memory-of-winter

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[hdu6434]Problem I. Count相关的知识,希望对你有一定的参考价值。

题目大意:$T(Tleqslant 10^5)$组数据,每组数据给你$n(nleqslant 2 imes 10^7)$,求$sumlimits_{i=1}^nsumlimits_{j=1}^{i-1}[(i+j,i-j)==1]$

题解:
$$
defdsum{displaystylesumlimits}
egin{align*}
&dsum_{i=1}^ndsum_{j=1}^{i-1}[(i+j,i-j)=1]\
&令k=i-j\
=&dsum_{i=1}^ndsum_{k=1}^{i-1}[(2i-k,k)=1]\
=&dsum_{i=1}^ndsum_{k=1}^{i-1}[(2i,k)=1]\
end{align*}
$$

$$
herefore
(2i,k)=1Rightarrow
egin{cases}
(i,k)=1\
(2,k)=1\
end{cases}\
当i为偶数时:\
(i,k)=1\
Rightarrow (2,k)=1\
herefore ans=varphi(i)\
当i为奇数时:\
(2,k)=1\
Rightarrow k为奇数\
herefore k必为与i互质的数的奇数\
ecause (i,k)=1Rightarrow(i,i-k)=1\
herefore 当i为奇数的时候,k奇偶各半\
herefore ans=dfrac{varphi(i)}2
$$

卡点:

 

C++ Code:

#include <cstdio>
#define maxn 20000010
int Tim, n;
long long pre[maxn];
int plist[maxn << 3], pt, phi[maxn];
bool isp[maxn];
void sieve(int n) {
	phi[1] = 1;
	pre[1] = 0;
	isp[1] = true;
	for (int i = 2; i < n; i++) {
		if (!isp[i]) {
			plist[pt++] = i;
			phi[i] = i - 1;
		}
		for (int j = 0; j < pt && i * plist[j] < n; j++) {
			int tmp = i * plist[j];
			isp[tmp] = true;
			if (i % plist[j] == 0) {
				phi[tmp] = phi[i] * plist[j];
				break;
			}
			phi[tmp] = phi[i] * phi[plist[j]];
		}
		pre[i] = pre[i - 1] + ((i & 1) ? phi[i] / 1 : phi[i]);
	}
}
int main() {
	sieve(maxn);
	scanf("%d", &Tim);
	while (Tim --> 0) {
		scanf("%d", &n);
		printf("%lld
", pre[n]);
	}
	return 0;
}

  

以上是关于[hdu6434]Problem I. Count的主要内容,如果未能解决你的问题,请参考以下文章

hdu 6434 Count (欧拉函数)

2018年东北地区赛S - Problem I. Spell Boost HDU - 6508

UVALive - 6434 (贪心)

2016-2017 ACM-ICPC, NEERC, Northern Subregional Contest Problem I. Integral Polygons

hdu 6319 Problem A. Ascending Rating (2018 Multi-University Training Contest 3 A)

[kuangbin带你飞]专题十六 KMP & 扩展KMP & ManacherK - Count the string HDU - 3336(前缀数量问题)