Problem I. Count
Problem Description
Multiple query, for each n, you need to get
n i-1
∑ ∑ [gcd(i + j, i - j) = 1]
i=1 j=1
Input
On the first line, there is a positive integer T, which describe the number of queries. Next there are T lines, each line give a positive integer n, as mentioned above.
T<=1e5, n<=2e7
Output
Your output should include T lines, for each line, output the answer for the corre- sponding n.
Sample Input
4
978
438
233
666
Sample Output
194041
38951
11065
89963
题意:计算:
n i-1
∑ ∑ [gcd(i + j, i - j) = 1]
i=1 j=1
即gcd(i+j,i-j)==1的个数,其中1<=i<=n,1<=j<i;
思路:由于 i,j 同奇或同偶时gcd(i+j,i-j)最小为2,所以 i为奇数时 ans += phi[i/2] ,i 为 偶数是 ans+=phi[i]。打表欧拉函数,前缀求和,效率O(n)。
代码:
#include<iostream>
#include<cstdio>
#include<map>
#include<vector>
#include<set>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#define maxn 20000000+2
typedef long long ll;
using namespace std;
ll m[maxn],phi[maxn],p[maxn],nump;
void init()
{
phi[1]=1;
for (int i=2;i<=maxn;i++)
{
if (!m[i])//i为素数
{
p[++nump]=i;//将i加入素数数组p中
phi[i]=i-1;//因为i是素数,由特性得知
}
for (int j=1;j<=nump&&p[j]*i<maxn;j++) //用当前已的到的素数数组p筛,筛去p[j]*i
{
m[p[j]*i]=1;//可以确定i*p[j]不是素数
if (i%p[j]==0) //看p[j]是否是i的约数,因为素数p[j],等于判断i和p[j]是否互质
{
phi[p[j]*i]=phi[i]*p[j]; //特性2
break;
}
else phi[p[j]*i]=phi[i]*(p[j]-1); //互质,特性3其,p[j]-1就是phi[p[j]]
}
}
phi[1]=0;
for(int i=2;i<maxn;i++)
{
if(i%2==1)
phi[i]=phi[i-1]+phi[i]/2;
else
phi[i]=phi[i]+phi[i-1];
}
return ;
}
int main()
{
init();
int T;
scanf("%d",&T);
while(T--)
{
int n;
scanf("%d",&n);
printf("%lld\n",phi[n]);
}
return 0;
}