uva 11424 GCD Extreme 求∑∑gcd(i,j) (1<i<n,i<j<n)

该博客详细介绍了如何解决UVA在线判题平台上的11424题——GCD Extreme。博主通过解释输入输出格式,展示了如何计算在1到N之间所有不同整数对(i, j)的最大公约数之和,其中1 < i < n,i < j < n。博客中包含欧拉函数的筛选法实现和求和算法的步骤,还提供了一个完整的C++代码示例来求解这个问题。" 4571469,309131,理解RFID EPC Gen2技术:区块、命令与标签状态,"['RFID技术', 'EPC协议', '存储管理', '标签状态', '命令操作']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem H
GCD Extreme
Input: Standard Input

Output: Standard Output

 

Given the value of N, you will have to find the value of G. The definition of G is given below:

 

Here GCD(i,j) means the greatest common divisor of integer i and integer j.

 

For those who have trouble understanding summation notation, the meaning of G is given in the following code:

G=0;

for(i=1;i<N;i++)

for(j=i+1;j<=N;j++)

{

    G+=gcd(i,j);

}

/*Here gcd() is a function that finds the greatest common divisor of the two input numbers*/

 

Input

The input file contains at most 20000 lines of inputs. Each line contains an integer N (1<N<200001). The meaning of N is given in the problem statement. Input is terminated by a line containing a single zero. 

 

Output

For each line of input produce one line of output. This line contains the value of G for the corresponding N. The value of G will fit in a 64-bit signed integer.

 

Sample Input                              Output for Sample Input

10

100             

20000

0

 

67

13015

1153104356

 


Problemsetter: Shahriar Manzoor and Syed Monowar Hossain

Special Thanks: Shahriar Manzoor and Syed Monowar Hossain

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int phi[1000100];
long long a[1000100];
void phi_table(int n )//筛选法求欧拉函数
{
 for(int i=2 ; i<= n; i++) phi[i] = 0;
 phi[1]= 1;
 for(int i =2; i<=n ;i ++)
  if(!phi[i])
  for(int j = i ; j<=n; j+=i)
  {
   if(!phi[j]) phi[j] = j;
   phi[j] = phi[j] /i*(i-1);
  }
}

void gcd_sum(int n)//求sigema gcd(i,n);(1<=i<=n-1);不包括n
{
    for(int i=2;i<n;i++) a[i]=phi[i];// /*最大公约数为1的*/
    double k=sqrt(0.5+n);
    for(int i=2;i<=k;i++)
    {
        for(int j=i,bit=1;j<n;j+=i,bit++)
        {
            if(i<bit) a[j]+=phi[bit]*i+phi[i]*bit;/*有几个数和j的最大公约数为j/i + 有几个数和j的最大公约数为i*/
            else if(i==bit) a[j]+=phi[i]*i;
        }
    }
}
void gcd_sum_again(int n)
{
    for(int i=1;i<n;i++) a[i]+=a[i-1];//二维的,要求和
}
int main()
{
    phi_table(1000100);
    gcd_sum(1000100);
    gcd_sum_again(1000100);
    int n;
    while(cin>>n&&n)
    {
        cout<<a[n]<<endl;
    }
    return 0;
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值