http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2421

代码及其注释:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue> #define ll long long
#define lint long long
using namespace std; const int N=4000005;
int phi[N];//phi[k] 表示从1到k 和k互质的数有几个
ll f[N],sum[N];//f[k] 表示从1到k-1 依次求和k的最大公约数 所以最大公约数的总和
void phin(int n=N-1)
{
memset(phi,0,sizeof(phi));
phi[1]=1;
for(int i=2;i<=n;++i)
if(!phi[i])
{
for(int j=i;j<=n;j=j+i)
{
if(!phi[j]) phi[j]=j;
phi[j]=phi[j]/i*(i-1);
}
}
}
int main()
{
//freopen("data.in","r",stdin);
phin();
memset(f,0,sizeof(f));
memset(sum,0,sizeof(sum));
for(int i=1;i<N;++i)
for(int j=i+i,l=2;j<N;j=j+i,++l)
{
f[j]+=(ll)phi[l]*(ll)i;//这里的phi[l]表示小于j的数中与j的最大公约数是i的数的个数
}
for(int i=1;i<N;++i)
sum[i]=f[i]+sum[i-1];
int k;
while(cin>>k)
{
if(!k) break;
cout<<sum[k]<<endl;
}
return 0;
}

UVa 11426 - GCD - Extreme (II)的更多相关文章

  1. UVA 11426 - GCD - Extreme (II) (数论)

    UVA 11426 - GCD - Extreme (II) 题目链接 题意:给定N.求∑i<=ni=1∑j<nj=1gcd(i,j)的值. 思路:lrj白书上的例题,设f(n) = gc ...

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

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Problem JGCD Extreme (II)Input: Standard ...

  3. UVA 11426 GCD - Extreme (II) (欧拉函数+筛法)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/O 题意是给你n,求所有gcd(i , j)的和,其中 ...

  4. UVA 11426 GCD - Extreme (II) (欧拉函数)题解

    思路: 虽然看到题目就想到了用欧拉函数做,但就是不知道怎么做... 当a b互质时GCD(a,b)= 1,由此我们可以推出GCD(k*a,k*b)= k.设ans[i]是1~i-1与i的GCD之和,所 ...

  5. UVA 11426 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 ...

  6. uva 11426 GCD - Extreme (II) (欧拉函数打表)

    题意:给一个N,和公式 求G(N). 分析:设F(N)= gcd(1,N)+gcd(2,N)+...gcd(N-1,N).则 G(N ) = G(N-1) + F(N). 设满足gcd(x,N) 值为 ...

  7. UVa 11426 - GCD - Extreme (II) 转化+筛法生成欧拉函数表

    <训练指南>p.125 设f[n] = gcd(1, n) + gcd(2, n) + …… + gcd(n - 1, n); 则所求答案为S[n] = f[2]+f[3]+……+f[n] ...

  8. UVA 11426 - 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< ...

  9. UVA 11426 GCD - Extreme (II) (数论|欧拉函数)

    题意:求sum(gcd(i,j),1<=i<j<=n). 思路:首先能够看出能够递推求出ans[n],由于ans[n-1]+f(n),当中f(n)表示小于n的数与n的gcd之和 问题 ...

随机推荐

  1. [转载] 新浪微博MySQL优化的小结和反思

    原文: http://mp.weixin.qq.com/s?__biz=MzA4Nzg5Nzc5OA==&mid=206762682&idx=1&sn=1233ed1496d7 ...

  2. [转载] [Mark]分布式存储必读论文

    原文: http://50vip.com/423.html 分布式存储泛指存储存储和管理数据的系统, 与无状态的应用服务器不同, 如何处理各种故障以保证数据一致,数据不丢, 数据持续可用, 是分布式存 ...

  3. UIImageView 的contentMode属性

    UIViewContentModeScaleToFill UIViewContentModeScaleAspectFit UIViewContentModeScaleAspectFill UIView ...

  4. JavaScript中Trim(),TrimStart(),TrimEnd()的实现

    //去除字符串头尾空格或指定字符 String.prototype.Trim= function(c) { if(c==null||c=="") { var str= this.r ...

  5. ttttttttttt

    http://www.2cto.com/kf/201606/519504.html http://a67474506.iteye.com/blog/2079590 spring boot: http: ...

  6. Sqlserver_时间用法

    函数 描述 GETDATE() 返回当前的日期和时间 DATEPART() 返回日期/时间的单独部分 DATEADD() 在日期中添加或减去指定的时间间隔 DATEDIFF() 返回两个日期之间的时间 ...

  7. Hbase之批量删除数据

    import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseConfiguration; impo ...

  8. AD组策略添加本地账号、设置允许ping回显

    AD组策略添加本地账号 1. 管理工具--组策略管理--选择相应GPO(编辑)----首选项--控制面板设置--本地用户和组--右键添加账号 2.域成员计算机刷新组策略(gpupdate/force) ...

  9. VB6 GDI+ 入门教程[1] GDI+介绍

    http://vistaswx.com/blog/article/category/tutorial/page/2 VB6 GDI+ 入门教程[1] GDI+介绍 2009 年 6 月 18 日 17 ...

  10. 日期操作类--Date类

    Date-API ava.util包提供了Date类来封装当前的日期和时间.Date类提供两个构造函数来实例化Date对象.第一个构造函数使用当前日期和时间来初始化对象. Date( ) 第二个构造函 ...