欧拉 uva 11426 - GCD - Extreme (II)

UVA11426 GCD - Extreme (II) (欧拉函数/莫比乌斯反演) UVA11426 GCD - Extreme (II) 题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 10 100 200000 0 输出样例#1: 67 13015 143295493160 Solution 这道题我用莫比乌斯反演和欧拉函数都写了一遍,发现欧拉函数比莫比乌斯反演优秀? 求所有\(gcd=k\)的数对的个数,记作\(f[k... 阅读详情

题意:(详见蓝皮书P125)

给定正整数n,求下列表达式的值:

G =
i<N     j<=N
GCD(i, j)
i=1     j=i+1

Input

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

Output

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

Sample Input

10

100

2000000

Sample Output

67

13015

143295493160

分析:

记f[n]= ∑gcd(i,n)     (1<=i<=n) ,那么所求就是f[1]+f[2]+......+f[n]=sum[n];

那么怎么求f[n]呢?

设d=gcd(x,n),显然d为n的因数,

那么 gcd(x/d,n/d)==1;   (*)

根据phi的定义,(*)式有 phi(n/d)个解,那么这一种情况的解的和 就是 d*phi(n/d);

这样用逐个分解因数的方法求一个f[n]的时间复杂度为n^(1/2),显然会TLE。

可以模拟筛法求phi的过程求出f[n]。

注意使用 long long

代码如下:

// by spark 2016/5/21 Friday 23:50
#include<cstdio>
#include<iostream>
#define LL long long
using namespace std;
const int maxn=4000000+5;
int phi[maxn];
LL sum[maxn],f[maxn];
void phi_table(int n){
	int i,j;
	for(i=1;i<=n;i++)phi[i]=i;
	for(i=2;i<=n;i++){
		if(phi[i]==i)
			for(j=i;j<=n;j+=i)
				phi[j]=phi[j]/i*(i-1);
	}
}
void f_table(int n){
	int i,j;
	for(i=1;i<=n;i++)
		for(j=i+i;j<=n;j+=i)
			f[j]+=i*phi[j/i];
}
int main(){
	int n,i;
	phi_table(maxn);   //打出phi表 
	f_table(maxn);    //计算f 
	for(i=1;i<=maxn;i++)
		sum[i]=sum[i-1]+f[i];    //前缀和 
	while(true){
		cin>>n;
		if(!n) return 0;
		cout<<sum[n]<<endl;
	}
	return 0;
}






UVA11426 GCD - Extreme (II)欧拉函数】 Given the value of N, you will have to find the value of G. The definition of G is given below: G=∑i=1i<N∑j=i+1i≤NGCD(i,j)G =\sum_{i=1}^{i<N}\sum_{j=i+1}^{i≤N}GCD(i, j)G=i=1∑i<N​j=i+1∑i≤N​GCD(i,j)     Here GCD(i, j) means the g 阅读详情

相关推荐

欧拉定理+思维】UVA - 11426 GCD - Extreme (II)

欧拉定理+思维】UVA - 11426 GCD - Extreme (II) https://vjudge.net/problem/UVA-11426 题意: 计算 ∑i=1i&amp;lt;N∑j=i+1j&amp;lt;=NGCD(i,j)\sum{_{i=1}^{i&amp;lt;N}} \sum{_{j=i+1}^{j&amp;lt;=N}}GCD(i,j)∑i=1i&lt;N​∑j=i...

Bryce1010's Blog 643

欧拉UVA 11426 GCD - Extreme (II)

这次不挂题目地址。。。因为UVa的感觉。。。好吧我还是贴题目吧。 Problem J GCD Extreme (II) Input: Standard Input Output: Standard Output   Given the value of N, you will have to find the value of G. The definition of

曜雨丶的博客 566

UVa 11426 - GCD - Extreme (II) (数学 欧拉函数)

UVA - 11426 GCD - Extreme (II) Time Limit: 10000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu [Submit]   [Go Back]   [Status]   Description Problem J GCD Extreme

zhaosdfa 868

欧拉函数 - GCD - Extreme (II) - UVA 11426

欧拉函数 - GCD - Extreme (II) - UVA 11426 题意: 给定正整数N,计算:给定正整数N,计算:给定正整数N,计算: G=∑i=1i<N∑j=i+1j≤NGCD(i,j)G=\sum_{i=1}^{i<N}\sum_{j=i+1}^{j≤N}GCD(i,j)G=i=1∑i<N​j=i+1∑j≤N​GCD(i,j) 其中,GCD(i,j)表示i和j的最大公约数。其中,GCD(i,j)表示i和j的最大公约数。其中,GCD(i,j)表示i和j的最大公约数。 输入: 多

njuptACMcxk的博客 279

UVa 11426 GCD - Extreme (II)

UVa 11426 GCD - Extreme (II) 欧拉函数的应用,AC代码如下: #include<bits/stdc++.h> using namespace std; typedef long long ll; const int N=4e6+5; ll a[N],phi[N]; void prephi(){ memset(a,0,sizeof(a)); for(int i=1;i<=N;i++) phi[i]=i; for(int i=2;i<=N;

旺崽的博客 2608

uva 11426 GCD - Extreme (II) 欧拉函数

https://vjudge.net/problem/UVA%20-%2011426 题目大意:如上图所示,就是让你求那个式子的结果。 思路:不妨引入一个新函数fff,且有f(n)=∑i=1n−1gcd(n,i)f(n)=\sum_{i=1}^{n-1}gcd(n,i)f(n)=∑i=1n−1​gcd(n,i),那么对于任意给定的数nnn,答案就是∑i=2nf(i)\sum_{i=2}^{n}f...

xiji333的博客 249

UVALive5964 LCM Extreme && UVA11426 GCD - Extreme (II) 欧拉函数筛法

UVALive5964 LCM Extreme 点击打开链接 UVA11426 GCD - Extreme (II)  点击打开链接 #include #include #include #include #include #include #include #include #include using namespace std; #define LL

Megumin的博客 618

UVA - 11426-GCD - Extreme (II)欧拉函数应用)

GCD - Extreme (II) Given the value of N, you will have to find the value of G. The definition of G is given below: -------------------------------∑i=1i<N\sum_{i=1}^{i<N}∑i=1i<N​∑j=i+1j<=N\sum_{j=i+1}^{j<=N}∑j=i+1j<=N​ GCD(i,j)------------

qq_43461168的博客 258

UVA11426 - GCD - Extreme (II)(数论,欧拉函数)

UVA11426 - GCD - Extreme (II)(数论,欧拉函数)

weixin_62802134的博客 654

GCD - Extreme (II)UVA - 11426欧拉函数)

一.题目链接: UVA-11426 二.题目大意: 三.分析: 贴一个聚聚的题解,自然不是我这般蒟蒻能想到的... 还有一点困扰了我好久,就是该如何快速的计算 F[n]. 如聚聚博客中写到 本蒟蒻只想到了的复杂度计算 F[],被自己蠢哭_(:3」∠)_ 正解是分别计算当 gcd(x, j) = i 时对 F[] 的贡献,易得 i |j,复杂度为 四.代码实现: ...

The___Flash的博客 320

UVA11426 GCD - Extreme (II)---欧拉函数的运用

GCD - Extreme (II) 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 tho...

我想要西瓜 283

UVA 11426-GCD - Extreme (II)欧拉函数】

思路:这题比较不好想,我也是看了几位博主的题解才大概理解,参考链接。 #include&lt;cstdio&gt; #include&lt;cmath&gt; #include&lt;queue&gt; #include&lt;vector&gt; #include&lt;cstring&gt; #include&lt;iostream&gt; #include&lt;algorithm&

独钓门前月 411

GCD - Extreme (II) UVA - 11426 欧拉

问题: 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 com...

HXX904的博客 217

GCD - Extreme (II) UVA - 11426欧拉函数与gcd的关系)

思路:若gcd(n,x)=i,则gcd(n/i,x/i)=1; 假设b[n]表示1到n-1与n的gcd的和,那么G[n]=G[n-1]+b[n]; a[i]表示与gcd(n, x)= i 的x的个数;b[n]=sum( a[i] * i ) ,所以我们只需求a[i]即可;根据gcd(n, x)=i ----->gcd(n/i, x/i)= 1, 因此仅仅要求出欧拉函数phi(n...

Endeavor_G的博客 753

GCD - Extreme (II) UVA - 11426 (欧拉函数)

题意: G=0; for(i=1;i<N;i++) for(j=i+1;j<=N;j++) { G+=gcd(i,j); } 给出n,求出想应的G 思路: 进行打表。G[i]表示要求的值,a[i]表示从1–(i-1)与i的GCD之和。可以推出G[i]=G[i-1]+a[i]。 所以我们的重点在于求出a[i]。这里就需要对欧拉函数进行利用了,很巧妙。 ...

呼吸 218

UVA - 11426 GCD - Extreme (II) gcd思维+巧用欧拉函数

UVA - 11426 题意:求∑N−1i=1∑Nj=i+1gcd(i,j) \sum_{i=1}^{N-1}\sum_{j = i+1}^{N} gcd(i,j) (N<=4e6, 1 <= i < j <= N) 思路:因为有两个变的数i,j所以我们考虑固定一个,设为n.那么我们只要分别求出gcd(i,n) == d 的有多少对,那么贡献就是cnt*d. 上面这个和设为f(n)

Marcus-Bao的个人主页 663

UVA - 11426 GCD - Extreme (II)

UVA - 11426 GCD - Extreme (II) 传送门UVA - 11426 题意 给你nnn,让你求 ∑i=1n−1∑j=i+1ngcd(i,j)∑i=1n−1∑j=i+1ngcd(i,j)\sum_{i = 1}^{n -1} \sum_{j = i + 1}^n gcd(i,j) 题解 这道题就是个筛法的经典练习。 首先,设一个函数f(x)f(x)f(x) f(...

fnoi11awyfeng的博客 343

Uva 11426

Uva 11426 GCD - Extreme (II) 题目链接:GCD - Extreme (II) 题目大意:emm,其实没有题目大意,就是求 我们考虑设f(n)(gcd(n,i)),(i=1…n−1)f(n)=\Sigma(gcd(n,i)),(i=1…n-1)f(n)(gcd(n,i)),(i=1…n−1) 答案就是从f(2)到f(n)的和,接下来我们考虑如何求f(n)。 设g(...

lycccccccc的博客 200
上一篇: 高精度模板
下一篇: 【欧拉】nkoj3685--8数
INCINCIBLE
博客等级 码龄11年 31粉丝 195原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值