uva 11417 - GCD
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的更多相关文章
- UVA 11426 - GCD - Extreme (II) (数论)
UVA 11426 - GCD - Extreme (II) 题目链接 题意:给定N.求∑i<=ni=1∑j<nj=1gcd(i,j)的值. 思路:lrj白书上的例题,设f(n) = gc ...
- UVA.12716 GCD XOR (暴力枚举 数论GCD)
UVA.12716 GCD XOR (暴力枚举 数论GCD) 题意分析 题意比较简单,求[1,n]范围内的整数队a,b(a<=b)的个数,使得 gcd(a,b) = a XOR b. 前置技能 ...
- 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) 此 ...
- UVa 12716 - GCD XOR(筛法 + 找规律)
链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- UVA 12716 GCD XOR【异或】
参考:http://www.cnblogs.com/naturepengchen/articles/3952145.html #include<stdio.h> #include<s ...
- UVa 11426 - GCD - Extreme (II)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 10892 (GCD) LCM Cardinality
我一直相信这道题有十分巧妙的解法的,去搜了好多题解发现有的太过玄妙不能领会. 最简单的就是枚举n的所有约数,然后二重循环找lcm(a, b) = n的个数 #include <cstdio> ...
- UVa 11889 (GCD) Benefit
好吧,被大白书上的入门题给卡了.=_=|| 已知LCM(A, B) = C,已知A和C,求最小的B 一开始我想当然地以为B = C / A,后来发现这时候的B不一定满足gcd(A, B) = 1 A要 ...
- UVa 12716 (GCD == XOR) GCD XOR
题意: 问整数n以内,有多少对整数a.b满足(1≤b≤a)且gcd(a, b) = xor(a, b) 分析: gcd和xor看起来风马牛不相及的运算,居然有一个比较"神奇"的结论 ...
随机推荐
- 转:python字符串/元组/列表/字典互转
#-*-coding:utf-8-*- #1.字典 dict = {'name': 'Zara', 'age': 7, 'class': 'First'} #字典转为字符串,返回:<type ...
- 王家林 大数据Spark超经典视频链接全集[转]
压缩过的大数据Spark蘑菇云行动前置课程视频百度云分享链接 链接:http://pan.baidu.com/s/1cFqjQu SCALA专辑 Scala深入浅出经典视频 链接:http://pan ...
- 搜集好的java技术帖子,持续更新,java程序员的要求
1.Java NIO 系列教程 2.Java实现 二叉搜索树算法(BST) 3. Java 并发工具包 java.util.concurrent 用户指南 4.架构师之路系列:http://blog. ...
- struts一些实用常量配置_2015.01.04
- zw版【转发·台湾nvp系列Delphi例程】HALCON FillUpShape2
zw版[转发·台湾nvp系列Delphi例程]HALCON FillUpShape2 procedure TForm1.Button1Click(Sender: TObject);var op : H ...
- yii2的windows下安装及前期步骤
Yii2的安装(以生成basic目录为例) 第一步:服务器安装好后生成www目录,在该目录下新建yii2目录,把下载的compser.phar包放在该目录下 第二步:dos命令下进入项目目录 第三步: ...
- 解决PHP在IE浏览器下载文件,中文文件名乱码问题
前提:我们网站所有文件全部使用的是UTF-8 NO BOM的编码方式 1.找测试重现.360浏览器下载的呵呵,果然文件名是乱码.再请测试在ie浏览器下测试.IE9,8,7也全部是乱码.查看编码就是UT ...
- 对DotNet分布式应用搭建的考虑
设计前的考虑和准备工作 1 对业务需求的理解重要性远远胜于对技术架构的理解 2 架构包含技术架构和业务架构 3 没有万能和通用的架构,只有符合自身业务需求的架构 4 架构本身的复杂性要截至在架构设计阶 ...
- mybatis 相关总结
1. Mybatis的<where><foreach><set>等标签详解:http://blog.csdn.net/zenson_g/article/detail ...
- docker-gerrit
Docker Hub https://yeasy.gitbooks.io/docker_practice/content/repository/dockerhub.html 但如何直接下载成压缩文件呢 ...