当前位置: 首页 > news >正文

GCD - Extreme (II) UVA - 11426 数学

Given the value of
N
, you will have to nd the value of
G
. The de nition of
G
is given below:
G
=
i<N
i
=1
j
N
j
=
i
+1
GCD
(
i; j
)
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 le contains at most 100 lines of inputs. Each line contains an integer
N
(1
< N <
4000001).
The meaning of
N
is given in the problem statement. Input is terminated by a line containing a single
zero.
Output
For each line of input produce one line of output. This line contains the value of
G
for the corresponding
N
. The value of
G
will t in a 64-bit signed integer.
Sample Input
10
100
200000
0
Sample Output
67
13015
143295493160
 
设 f(n)=gcd(1,n)+gcd(2,n)+...+gcd(n-1,n),则
s(n)=f(2)+f(3)+...+f(n);
对于 f(n)=gcd(1,n)+...+gcd(n-1,n);
设 g(n,i)= { gcd(x,n)=i 的个数 },则 f(n)=Sum{ i*g(n,i) };
gcd(x,n)=i -->gcd(x/i,n/i)=1;
那么满足条件的x/i有 phi(n/i)个;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 4000005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-4
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}


ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; }



/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/

int phi[maxn];
void init() {
	for (int i = 2; i <= maxn; i++)phi[i] = 0;
	phi[1] = 1;
	for (int i = 2; i <= maxn; i++) {
		if (!phi[i]) {
			for (int j = i; j <= maxn; j += i) {
				if (!phi[j])phi[j] = j;
				phi[j] = phi[j] / i * (i - 1);
			}
		}
	}
}

ll sum[maxn], f[maxn];

int main() {
//	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	init();
	for (int i = 1; i <= maxn; i++) {
		for (int j = i * 2; j <= maxn; j += i)f[j] += i * phi[j / i];
	}
	sum[2] = f[2];
	for (int j = 3; j <= maxn; j++)sum[j] = sum[j - 1] + f[j];
	int n;
	while (rdint(n) == 1&&n) {
		printf("%lld\n", sum[n]);
	}
	return 0;
}

 

 

转载于:https://www.cnblogs.com/zxyqzy/p/10324199.html

相关文章:

  • 博客搭建祭
  • python3 --- locale命名空间让程序更加安全了
  • 学习第一周
  • C# 8将为断言和自动化测试带来Caller Expression Attribute
  • 原生js商品排序
  • 河南减税红利:顶格50%减征 小微企业受益面达98%
  • 迷你音乐播放器v1.0正式上线!
  • CNCF 技术监督委员会首添中国面孔,阿里巴巴李响入选全球9人名单
  • 更换VC后DDC提示证书不可用
  • 用户超5亿,三年投10亿,开发者如何抢滩支付宝小程序蓝海?
  • 自由软件基金会主席 RMS 谈全面监控
  • 聊聊flink的CsvTableSource
  • Java面向对象及其三大特征
  • canvas 五子棋游戏
  • 12-dm-permissions.rules
  • bearychat的java client
  • CentOS7 安装JDK
  • Dubbo 整合 Pinpoint 做分布式服务请求跟踪
  • electron原来这么简单----打包你的react、VUE桌面应用程序
  • golang中接口赋值与方法集
  • Javascript 原型链
  • js面向对象
  • Koa2 之文件上传下载
  • PHP的Ev教程三(Periodic watcher)
  • React Transition Group -- Transition 组件
  • Webpack入门之遇到的那些坑,系列示例Demo
  • 基于Android乐音识别(2)
  • 前端技术周刊 2018-12-10:前端自动化测试
  • 前嗅ForeSpider教程:创建模板
  • 如何实现 font-size 的响应式
  • 使用agvtool更改app version/build
  • 思考 CSS 架构
  • 写给高年级小学生看的《Bash 指南》
  • ​ArcGIS Pro 如何批量删除字段
  • ​iOS实时查看App运行日志
  • #vue3 实现前端下载excel文件模板功能
  • #WEB前端(HTML属性)
  • $ git push -u origin master 推送到远程库出错
  • (附源码)ssm高校社团管理系统 毕业设计 234162
  • (十三)Flask之特殊装饰器详解
  • (一)appium-desktop定位元素原理
  • (一)pytest自动化测试框架之生成测试报告(mac系统)
  • (转) 深度模型优化性能 调参
  • (转)socket Aio demo
  • (转)菜鸟学数据库(三)——存储过程
  • (转)微软牛津计划介绍——屌爆了的自然数据处理解决方案(人脸/语音识别,计算机视觉与语言理解)...
  • . ./ bash dash source 这五种执行shell脚本方式 区别
  • .L0CK3D来袭:如何保护您的数据免受致命攻击
  • .NET 8.0 发布到 IIS
  • .net core 实现redis分片_基于 Redis 的分布式任务调度框架 earth-frost
  • .Net Core和.Net Standard直观理解
  • .Net 垃圾回收机制原理(二)
  • .net 设置默认首页
  • .Net 知识杂记
  • // an array of int