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看起来风马牛不相及的运算,居然有一个比较"神奇"的结论 ...
随机推荐
- oracle触发器书写方法
CREATE SEQUENCE 序列名[INCREMENT BY n] --每次加几[START WITH n] --序列从几开始[{MAXVALUE/ MINVALUE n|NOMAXVALUE}] ...
- Fzu oj2194星系碰撞(排序+并查集+路径压缩)
Problem 2194 星系碰撞 Accept: 14 Submit: 48Time Limit: 30000 mSec Memory Limit : 327680 KB Proble ...
- Hashtable和HashMap区别
Hashtable和HashMap区别 相同点: 实现原理,功能相同,可以互用 主要区别: a.hashtable继承Directionary类,HashMap实现Map接口 b.Hashtable线 ...
- the serializable class XXX does not declare a static final seriaVersionUID...的问题
关于myeclips提示The serializable class XXX does not declare a static final serialVersionUID field of typ ...
- Spring之AOP面向切片
一.理论基础: AOP(Aspectoriented programming)面向切片/服务的编程,在Spring中使用最多的是对事物的处理.而AOP这种思想在程序中很多地方可以使用的,比如说, ...
- ubuntu使用记录
常用指令 ls 显示文件或目录 -l 列出文件详细信息l(list) -a 列出当前目录下所有文件及目录,包括隐藏的a(all) mkdir ...
- xib中设置控件的圆角
1.http://my.oschina.net/ioslighter/blog/387991?p=1 利用layer.cornerRadius实现一个圆形的view,将layer.cornerRadi ...
- Linux终端乱码的解决办法
用SSH连接Linux时经常会遇到乱码的情况,痛苦了好久,在网上找到一个解决办法,编辑~/.bash_profile文件,加入下面两行: LANG="zh_CN.GB18030" ...
- 【Pro ASP.NET MVC 3 Framework】.学习笔记.9.SportsStore:Securing the Administration Features
1 设置表单身份认证 因为ASP.NET MVC基于ASP.NET平台的核心,所以我们可以使用ASP.NET Form的身份认证,这是保持用户登录轨迹通用的方法.现在介绍最基本的配置. 在Web.co ...
- Hive报错之java.sql.SQLException: Field 'IS_STOREDASSUBDIRECTORIES' doesn't have a default value
在创建表的时候报出如下错误: hive> create table if not exists testfile_table( > site string, > url string ...