GCD
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<501). The meaning of N is given in the problem statement. Input is terminated by a line containing a single zero.  This zero should not be processed.

Output

For each line of input produce one line of output. This line contains the value of G for corresponding N.

Sample Input                              Output for Sample Input

10

100

500

0

 

67

13015

442011


Problemsetter: Shahriar Manzoor

Special Thanks: Syed Monowar Hossain

 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
const int maxn = ; int G[maxn];
int opl[maxn];
void init()
{
int i,j;
memset(G,,sizeof(G));
for(i=;i<maxn;i++) opl[i] = i;
for(i=;i<maxn;i++)
{
if(opl[i]==i)
{
for(j=i;j<maxn;j=j+i)
opl[j] = opl[j]/i*(i-);
}
for(j=;i*j<maxn;j++)/**opl [ i ] 此时已经算出**/
G[j*i] = G[j*i] + opl[i]*j;
}
for(i=;i<maxn;i++)
G[i] +=G[i-];
}
int main()
{
int n;
init();
while(scanf("%d",&n)>)
{
if(n==)break;
printf("%d\n",G[n]);
}
return ;
}

uva 11417 - GCD的更多相关文章

  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.12716 GCD XOR (暴力枚举 数论GCD)

    UVA.12716 GCD XOR (暴力枚举 数论GCD) 题意分析 题意比较简单,求[1,n]范围内的整数队a,b(a<=b)的个数,使得 gcd(a,b) = a XOR b. 前置技能 ...

  3. UVA 11424 GCD - Extreme (I) (欧拉函数+筛法)

    题目:给出n,求gcd(1,2)+gcd(1,3)+gcd(2,3)+gcd(1,4)+gcd(2,4)+gcd(3,4)+...+gcd(1,n)+gcd(2,n)+...+gcd(n-1,n) 此 ...

  4. UVa 12716 - GCD XOR(筛法 + 找规律)

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

  5. UVA 12716 GCD XOR【异或】

    参考:http://www.cnblogs.com/naturepengchen/articles/3952145.html #include<stdio.h> #include<s ...

  6. UVa 11426 - GCD - Extreme (II)

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

  7. UVa 10892 (GCD) LCM Cardinality

    我一直相信这道题有十分巧妙的解法的,去搜了好多题解发现有的太过玄妙不能领会. 最简单的就是枚举n的所有约数,然后二重循环找lcm(a, b) = n的个数 #include <cstdio> ...

  8. UVa 11889 (GCD) Benefit

    好吧,被大白书上的入门题给卡了.=_=|| 已知LCM(A, B) = C,已知A和C,求最小的B 一开始我想当然地以为B = C / A,后来发现这时候的B不一定满足gcd(A, B) = 1 A要 ...

  9. UVa 12716 (GCD == XOR) GCD XOR

    题意: 问整数n以内,有多少对整数a.b满足(1≤b≤a)且gcd(a, b) = xor(a, b) 分析: gcd和xor看起来风马牛不相及的运算,居然有一个比较"神奇"的结论 ...

随机推荐

  1. Ruby操作VBA的注意事项和技巧(1):乱码、获取VBA活动和非活动窗口的名称与路径、文件路径的智能拼接与截取(写入日期)

    1.VBA编辑器复制粘贴出来的代码乱码     解决方法:切换到中文输入模式再复制出来就行了 2.获取VBA活动和非活动窗口的名称与路径 Dim wbpath, filename As String ...

  2. Java基础(32):String与StringBuilder、StringBuffer的区别(String类)

    在Java中,除了可以使用 String 类来存储字符串,还可以使用 StringBuilder 类或 StringBuffer 类存储字符串,那么它们之间有什么区别呢? String 类具有是不可变 ...

  3. java中的断言

     断言:也就是所谓的assertion,是jdk1.4后加入的新功能. 它主要使用在代码开发和测试时期,用于对某些关键数据的判断,如果这个关键数据不是你程序所预期的数据,程序就提出警告或退出. 当软件 ...

  4. uboot.lds (一)

    lds文件与scatter文件相似都是决定一个可执行程序的各个段的存储位置,以及入口地址,这也是链接定位的作用.U-boot的lds文件说明如下:       SECTIONS{        ... ...

  5. Kafka集群模式部署

    环境:kafka 0.8.1.1 基本概念 Kafka维护按类区分的消息,称为主题(topic) 生产者(producer)向kafka的主题发布消息 消费者(consumer)向主题注册,并且接收发 ...

  6. 09---Net基础加强

    复习 Person类: using System; using System.Collections.Generic; using System.Linq; using System.Text; us ...

  7. 包括post,get请求(http,https)的HttpClientUtils

    package cn.knet.data.untils; import java.io.IOException; import java.net.SocketTimeoutException; imp ...

  8. fnd_profile.value('AFLOG_ENABLED')的取值 和配置文件相关SQL

    SELECT * FROM FND_PROFILE_OPTIONS_VL TT WHERE TT.PROFILE_OPTION_NAME LIKE '%AFLOG%' FND:启用调试日志 详细的参考 ...

  9. 【py】安装ipython-notebook

    os:ubunutu(debian)-based linux 分两步: 安装ipython及其依赖包 sudo apt-get install ipython-notebook   安装可选的附加工具 ...

  10. Openstack的用户登录流程

    openstack的用户登录,需要获得集中权限. token 只需要提供用户名和密码即可获得,接口 http://public_url/tokens method:POST body:{"a ...