1. 程式人生 > >hdu6434 杭電多校賽 第十場 1009 I題 count

hdu6434 杭電多校賽 第十場 1009 I題 count

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;
}