hdu6434: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.

 

题目链接:  http://acm.hdu.edu.cn/showproblem.php?pid=6434

 

小知识:如果 a 与 b 互质,a、b和(a + b)互质。

所以题目求的是a、b互质并且a+b == i * 2的数有几对。a + b == i * 2, 所以就是求所有与i*2互质的数的个数 / 2。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int maxn = 2e7+5;
int phi[maxn * 2], prim[maxn * 2];

void get_ouler() {
    memset(phi, 0, sizeof phi);
    phi[1] = 1;
    int id = 0;
    for(int i = 2; i < maxn * 2; i++) {
        if(!phi[i]) {
            phi[i] = i - 1;
            prim[id++] = i;
        }
        for(int j = 0; j < id && prim[j] * i < maxn * 2; j++) {
            if(i % prim[j]) {
                phi[i * prim[j]] = phi[i] * (prim[j] - 1);
            } else {
                phi[i * prim[j]] = phi[i] * prim[j];
                break;
            }
        }
    }
}



ll sum[maxn] = {0};
void init() {
    for(int i = 2; i < maxn; i++)
        sum[i] = phi[i * 2] / 2 + sum[i - 1];
}

int main()
{
    get_ouler();
    init();
    int T, n;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        printf("%lld\n", sum[n]);
    }
    return 0;
}

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值