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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值