UVA11426

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2421

解题思路:

  思路来源于:http://www.cnblogs.com/staginner/archive/2012/10/29/2745135.html

  今天第一次接触到需要用到欧拉函数解决的题目,看了几篇介绍欧拉函数的文章,在这里跟大家推荐两篇:

  http://blog.csdn.net/sentimental_dog/article/details/52002608

  http://m.blog.csdn.net/HelloWorld10086/article/details/43764639

  接下来进入正题。我们用add(x,y)表示当i循环到x,j循环到y时能为上限为n的G(我们用G(n)表示)增加的数值。当x,y互质的时候易知add(x,y) = 1,由此我们顺藤摸瓜地知道add(2x,2y) = 2, ..., add(kx,ky) = k。根据这个结论,我们可以利用欧拉函数,求出小于z的正整数中与z互质的数的数目,把它加入ans[z](我们用ans[z]来保存n = z时的答案。),然后将其翻倍,一一将值加入ans[2z], ans[3z], ..., ans[kz]。最后还要记得:对于每一个ans[x],其实还要将前面所有的ans[]累加起来。

  end.

  P.S. 其实Staginner大神讲的比我好一万倍,大家如果看不懂我的这篇文章可以看看最上面的那个思路来源。

AC代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 typedef long long ll;
 5 const int maxn=4000000+3;
 6 ll ans[maxn],phi[maxn];
 7 void init(){
 8     for(int i=2;i<=maxn;i++)    phi[i]=0;
 9     phi[1]=1;
10     for(int i=2;i<=maxn;i++){
11         if(!phi[i]){
12             for(int j=i;j<=maxn;j+=i){
13                 if(!phi[j]) phi[j]=j;
14                 phi[j]=phi[j]/i*(i-1);
15             }
16         }
17         for(int j=1;j*i<=maxn;j++){
18             ans[i*j]+=(j*phi[i]);
19         }
20     }
21     for(int i=1;i<=maxn;i++)    ans[i]+=ans[i-1];
22 }
23 int main()
24 {
25     init();
26     int n;
27     while(scanf("%d",&n)==1&&n) printf("%lld\n",ans[n]);
28     return 0;
29 }
时间: 2024-08-06 21:22:19

UVA11426的相关文章

【UVa11426】GCD - Extreme (II)(莫比乌斯反演)

[UVa11426]GCD - Extreme (II)(莫比乌斯反演) 题面 Vjudge 题解 这.. 直接套路的莫比乌斯反演 我连式子都不想写了 默认推到这里把.. 然后把\(ans\)写一下 \[ans=\sum_{d=1}^nd\sum_{i=1}^{n/d}\mu(i)[\frac{n}{id}]^2\] 令\(T=id\) 然后把\(T\)提出来 \[ans=\sum_{T=1}^n[\frac{n}{T}]^2\sum_{d|T}d\mu(\frac{T}{d})\] 后面那一堆

UVa11426 GCD - Extreme (II)

见http://www.cnblogs.com/SilverNebula/p/6280370.html II的数据范围是I的20倍. 但是做I时用的O(n)+O(nlogn)+O(n)的算法足够了. 没错我就是在水博 /*by SilverN*/ #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<cmath> using names

GCD - Extreme (II)(UVA11426)

思路:欧拉函数: 欧拉函数,然后用下等差数列公式就行了. 1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<queue> 5 #include<math.h> 6 #include<vector> 7 #include<bitset> 8 using namespace std; 9 typedef long long LL;

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

题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=473&problem=2421&mosmsg=Submission+received+with+ID+13800900 Given the value of N, you will have to ?nd the value of G. The de?nition

uva11426(莫比乌斯反演)

传送门:GCD Extreme (II) 题意:给定n(n<=4000000),求G G=0 for(int i=1;i<n;i++) for(int j=i+1;j<=n;j++) G+=gcd(i,j).   分析:这题本来应该欧拉函数预处理来解决,不过尝试一下莫比乌斯反演,没想到也AC了,复杂度O(nlog(n)),应该是题目100case中大数据不多,不然会超时吧. 设F(n)表示gcd(x,y)==n的倍数所有gcd之和,f(n)表示gcd(x,y)==n的所有gcd之和,那么

UVA11426 欧拉函数

大白书P125 1 #include <iostream> 2 #include <cstring> 3 using namespace std; 4 #define MMX 4000010 5 #define LL long long 6 int phi[MMX],f[MMX]; 7 LL S[MMX]; 8 9 void calc_phi(int n) //求1--n的欧拉函数,phi[i]=φ(i) 10 { 11 for (int i=2;i<=n;i++) 12 p

uva11426 gcd、欧拉函数

题意:给出N,求所有满足i<j<=N的gcd(i,j)之和 这题去年做过一次... 设f(n)=gcd(1,n)+gcd(2,n)+......+gcd(n-1,n),那么answer=S[N]=f(1)+f(2)+...+f(N). 先求出每一个f(n). 令g(n,i)=[满足gcd(x,n)=i且x<N的x的数量],i是n的约数 那么f(n)=sigma[i*g(n,i)] (i即gcd的值,g(n,i)为数量) 又注意到gcd(x,n)=i -> gcd(x/i,n/i)=

最大公约数之和——极限版II

P1490 - [UVa11426 ]最大公约数之和--极限版II Description Input 输入包含至多100组数据.每组数据占一行,包含正整数N(2<=N<=1<N<4000000).输入以N=0结束. Output 对于每组数据,输出一行,即所对应的G值.答案保证在64位带符号整数范围内. Sample Input 10 100 200000 0 Sample Output 67 13015 143295493160 Hint 数据范围: 对于30%的数据,2<

《算法竞赛入门经典——训练指南》第二章题库

UVa特别题库 UVa网站专门为本书设立的分类题库配合,方便读者提交: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=442 注意,下面注有"extra"的习题并没有在书中出现,但在上面的特别题库中有,属于附加习题. 基础练习 (Basic Problems) UVa11388 GCD LCM UVa11889 Benefit UVa10943 How do y