有 个 ,对于每一个 求 。
其中 。
结论 。
证明:对于 必有 所以是成对出现的。
我们知道成对出现的两个数的和 所以任何的对和都是 。
所以可以得到 为 然后就可以直接求了。
注:建议不要用筛子(像我一样),可以直接枚举,筛子莫名其妙变慢了。
#include<bits/stdc++.h>
#pragma GCC optimize(1,2,3,"Ofast")
#define rep(i,j,k) for(int i=(j);i<=(k);++i)
#define per(i,j,k) for(int i=(j);i>=(k);--i)
using namespace std;
const int MAXSIZE = 1 << 20;
char buf[MAXSIZE], *p1, *p2;
#define gc() \
(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, MAXSIZE, stdin), p1 == p2) \
? EOF \
: *p1++)
int rd() {
int x = 0, f = 1;
char c = gc();
while (!isdigit(c)) {
if (c == '-') f = -1;
c = gc();
}
while (isdigit(c)) x = x * 10 + (c ^ 48), c = gc();
return x * f;
}
namespace rsy{
const int N = 4e5 + 10;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
int qmi(int a,int b){
int res=1;
while(b){
if(b&1)res*=a;
a*=a,b>>=1;
}return res;
}
int t,n,primes[N],cnt,idx;
pii fj[100];
bool st[N];
void fjj(int x){
for(int i=1;i<=cnt;i++){
int s=0;
if(!st[i])break;
while(x%primes[i]==0)x/=primes[i],s++;
if(!s)break;
fj[++idx]={primes[i],s};
}
if(x>1)fj[++idx]={x,1};
}
void init(int x){
for(int i=2;i<=x;++i){
if(!st[i])primes[++cnt]=i;
for(int j=1;primes[j]<=x/i;j++){
st[primes[j]*i]=1;
if(i%primes[j]==0)break;
}
}
}
int phi(int x){
int res=x,t=ceil(sqrt(x));
for(int i=1;;i++){
if(primes[i]>t)break;
if(x%primes[i]!=0)continue;
while(x%primes[i]==0)x/=primes[i];
res=res/primes[i]*(primes[i]-1);
}
if(x>1)res=res/x*(x-1);
return res;
}
signed main()
{
init(35000);
t=rd();
while(t--){
ll res=0;idx=0;
n=rd();
if(n==1){printf("1\n");continue;}
for(int i=1;i<=n/i;i++)
if(n%i==0){
res+=(1ll*i*phi(i)+1ll)>>1ll;
if(i!=n/i)res+=(1ll*(n/i)*phi(n/i)+1ll)>>1ll;
}
printf("%lld\n",res);
}
return 0;
}
}
signed main()
{
return rsy::main();
}感谢 six_one 对于 LaTeX 错误的贡献!