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. Android aidl Binder框架浅析

      转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38461079 ,本文出自[张鸿洋的博客] 1.概述 Binder能干什么?B ...

  2. 【spring-quartz】 定时调度,时间设置

    .   CronTrigger时间格式配置说明 CronTrigger配置格式: 格式: [秒] [分] [小时] [日] [月] [周] [年] 序号 说明 是否必填 允许填写的值 允许的通配符 1 ...

  3. Android控件之MultiAutoCompleteTextView(自动匹配输入的内容)

    一.功能 可支持选择多个值(在多次输入的情况下),分别用分隔符分开,并且在每个值选中的时候再次输入值时会自动去匹配,可用在发送短信,发邮件时选择联系人这种类型中 二.独特属性 android:comp ...

  4. ORA-12705: Cannot access NLS data files or invalid environment specified

    ASM实例无法启动 [grid@data ~]$ sqlplus / as sysasm SQL*Plus: Release 11.2.0.4.0 Production on Fri Sep 11 0 ...

  5. Command设计模式

    1 意图:将一个请求封装为一个对象,可以用不同的请求对客户进行参数化: 对请求排队或记录请求日志,以及支持可撤销的操作. 2 别名:Action.Transaction 3 动机:把请求变成一个对象. ...

  6. linux 相关学习记录

    (一)概念① 物理CPU实际Server中插槽上的CPU个数物理cpu数量,可以数不重复的 physical id 有几个② 逻辑CPU /proc/cpuinfo 用来存储cpu硬件信息的信息内容分 ...

  7. drupal_get_path_alias

    drupal_get_path_alias('path','language')这个函数是去读取url_alias表,获得某个path在特定language下的alias列的值--刚开始我 dpm(d ...

  8. Machine Learning – 第2周(Linear Regression with Multiple Variables、Octave/Matlab Tutorial)

    Machine Learning – Coursera Octave for Microsoft Windows GNU Octave官网 GNU Octave帮助文档 (有900页的pdf版本) O ...

  9. 基于websocket的jsp与java进行交互

    环境:jdk.18   tomcat-7.0.70 把tomcat中的 两个jir包复制到 注意:最后部署的时候要删除掉这两个jar包 tomcate中已经有了 ebSocket对象的创建和服务器连接 ...

  10. Trapping Rain Water [LeetCode]

    Problem Description: http://oj.leetcode.com/problems/trapping-rain-water/ Basic idea: Get the index ...