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

10

100

200000

0

Output for Sample Input

67

13015

143295493160

/*
题目大意给出一个n,求sum(gcd(i,j),0<i<j<=n);
可以明显的看出来s[n]=s[n-1]+f[n];
f[n]=sum(gcd(i,n),0<i<n);
现在麻烦的是求f[n]
gcd(x,n)的值都是n的约数,则f[n]=
sum{i*g(n,i),i是n的约数},注意到gcd(x,n)=i的
充要条件是gcd(x/i,n/i)=1,因此满足条件的
x/i有phi(n/i)个,说明gcd(n,i)=phi(n/i).
f[n]=sum{i*phi(n/i),1=<i<n}
因此可以搞个for循环对i循环,只要i<n,f[n]+=i*phi(n/i);
*/
#include<iostream>
#include<cstdio>
using namespace std;
#define Max 4000000 __int64 s[Max+],f[Max+],phi[Max+];
int prime[Max/];
bool flag[Max+]; void Init()
{
int i,j,num=;
memset(flag,,sizeof(flag));
phi[]=;
for(i=;i<=Max;i++)//欧拉筛选
{
if(flag[i])
{
prime[num++]=i;
phi[i]=i-;
}
for(j=;j<num && prime[j]*i<=Max;j++)
{
flag[i*prime[j]]=false;
if(i%prime[j]==)
{
phi[i*prime[j]]=phi[i]*prime[j];
break;
}
else phi[i*prime[j]]=phi[i]*(prime[j]-);
}
}
memset(f,,sizeof(f));
for(i=;i<=Max;i++)//f[n]更新
{
//因为f[n]=gcd[1,n]+....+gcd[n-1,n]所以j=2*i开始,若从j=i开始那就等同于加上了一项gcd(n,n)
for(j=*i;j<=Max;j+=i)
f[j]+=i*phi[j/i];
}
memset(s,,sizeof(s));
for(i=;i<=Max;i++)
s[i]=s[i-]+f[i];
} int main()
{
Init();
int n;
while(cin>>n,n)
printf("%I64d\n",s[n]);
return ;
}
 
 

uva 11426 线性欧拉函数筛选+递推的更多相关文章

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

    G(i) = (gcd(1, i) + gcd(2, i) + gcd(3, i) + .....+ gcd(i-1, i)) ret = G(1) + G(2) + G(3) +.....+ G(n ...

  2. POJ_2478 Farey Sequence 【欧拉函数+简单递推】

    一.题目 The Farey Sequence Fn for any integer n with n >= 2 is the set of irreducible rational numbe ...

  3. UVA 12493 Stars (欧拉函数--求1~n与n互质的个数)

    pid=26358">https://uva.onlinejudge.org/index.phpoption=com_onlinejudge&Itemid=8&cate ...

  4. 紫书 例题 10-7 UVa 10820 (欧拉函数)

    这道题要找二元组(x, y) 满足1 <= x, y <= n 且x与y互素 那么我就可以假设x < y, 设这时答案为f(n) 那么答案就为2 * f(n) +1(x与y反过来就乘 ...

  5. 紫书 例题 10-27 UVa 10214(欧拉函数)

    只看一个象限简化问题,最后答案乘4+4 象限里面枚举x, 在当前这条固定的平行于y轴的直线中 分成长度为x的一段段.符合题目要求的点gcd(x,y) = 1 那么第一段1<= y <= x ...

  6. bzoj 2818 gcd 线性欧拉函数

    2818: Gcd Time Limit: 10 Sec  Memory Limit: 256 MB[Submit][Status][Discuss] Description 给定整数N,求1< ...

  7. bzoj 2190: [SDOI2008]仪仗队 线性欧拉函数

    2190: [SDOI2008]仪仗队 Time Limit: 10 Sec  Memory Limit: 259 MB[Submit][Status][Discuss] Description 作为 ...

  8. 紫书 例题 10-26 UVa 11440(欧拉函数+数论)

    这里用到了一些数论知识 首先素因子都大于M等价与M! 互质 然后又因为当k与M!互质且k>M!时当且仅当k mod M! 与M!互质(欧几里得算法的原理) 又因为N>=M, 所以N!为M! ...

  9. POJ2478(SummerTrainingDay04-E 欧拉函数)

    Farey Sequence Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16927   Accepted: 6764 D ...

随机推荐

  1. Python 学习日志9月20日

    9月20日 周三 多大年龄了,还活得像个小孩.——急什么,人生又不长. 你习惯了思考宇宙星辰,一百年真的不长,一生也就不那么长,许多人的价值观念你也就无法理解.同样,许多人也无法理解你的价值观念,感兴 ...

  2. map最基本操作

    #include<iostream> #include<map> using namespace std; int main() { /*map<int,char> ...

  3. python自动化基础问题解析

      (1)自动化代码中用到的设计模式: po模式(page object): 1.PO提供了一种业务流程与页面元素操作分离的模式,这使得测试代码变得更加清晰. 2.页面对象与用例分离,使得我们更好的复 ...

  4. 系统学习爬虫_2_urllib

    什么是urllib urlopen urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cad ...

  5. thinkphp 结合phpexcel实现excel导入

    控制器文件: class ExcelAction extends Action { public function __construct() { import('ORG.Util.ExcelToAr ...

  6. shell脚本调试打印日志问题

    shell脚本调试打印日志问题 1. 需求 我们在编写脚本的时候,有时候需要做调试,便于我们定位问题,有时候等脚本上线之后,我们需要保留脚本执行过程中的记录.便于我们在出问题的时候,定位问题. 2. ...

  7. 同时使用多个UITableView

    1.xib\storyboard中给2个tableView设置constraints(等宽) 方法 : ①设置mainTableView的上\下\左\三部分的约束为0:subTableView上\下\ ...

  8. luogu2312 解方程 (数论,hash)

    luogu2312 解方程 (数论,hash) 第一次外出学习讲过的题目,然后被讲课人的一番话惊呆了. 这个题,我想着当年全国只有十几个满分.....然后他又说了句我考场A这道题时,用了5个模数 确实 ...

  9. C++实现简易单向链表

    #include <iostream> #include <stdlib.h> #include <stdbool.h> using namespace std; ...

  10. python--动态传参,作用域,函数嵌套

    一 . 动态传参(重点)  * ,  ** * 与 ** * 在形参位置. * 表示不定参数, 接收的是位置参数 接收到的位置参数的动态传参: 都是元组 def eat(*food): # 在形参这里 ...