fzu1969 GCD Extreme 类似于uva10561
Description
Given the value of N, you will have to find the value of G. 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 20000 lines of inputs. Each line contains an integer N (1<N <1000001). 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 fit in a 64-bit signed integer.
Sample Input
Sample Output
Source
题意:给出数字n,求对于所有满足1<= i < j <= n 的所有数对,(i,j)所对应的gcd(i,j)之和。
思路:设f(n) = gcd(1,n)+gcd(2,n)+gcd(3,n)+。。。+gcd(n-1,n)。则所求答案s(n)=f(2)+f(3)+f(4)+。。。+f(n)。
注意到所有的gcd(x,n)的值都是n的约数,所以可以按照这个约数来进行分类。用g(n,i)来表示满足gcd(x,n)=i且x<n的正整数x的个数。则f(n)={i×g(n,i)|i为n的约数}。注意,g(n,i)=phi(n,i)。
如果对于每个n都枚举i,按照数据范围来看肯定会超时,我们不如反着来,先枚举i,再找i的倍数n。这样时间复杂度会进一步减少。
/*
* Author: Joshua
* Created Time: 2014年09月07日 星期日 19时26分30秒
* File Name: fzu1969.cpp
*/
#include<cstdio>
#include<cstring>
typedef long long LL;
#define maxn 1000005
int phi[maxn],a[maxn],f[maxn];
LL s[maxn];
bool p[maxn];
int tot=; void pri()
{
memset(p,,sizeof(p));
for (int i=;i<maxn;++i)
if (p[i])
{
for (int j=i<<;j<maxn;j+=i)
p[j]=false;
}
for (int i=;i<maxn;++i)
if (p[i]) a[++tot]=i;
} void phi_table()
{
for (int i=;i<maxn;++i)
{
phi[i]=i;
int temp=i;
for (int j=;j<=tot;++j)
{
if (temp==) break;
if (p[temp])
{
phi[i]/=temp;
phi[i]*=temp-;
break;
}
if (temp % a[j] == )
{
phi[i]/=a[j];
phi[i]*=a[j]-;
while (temp%a[j]==) temp/=a[j];
}
}
}
} void solve()
{
pri();
phi_table();
for (int i=;i<maxn;++i)
for (int j=i;j<maxn;j+=i)
f[j]+=i*phi[j/i];
for (int i=;i<maxn;++i)
s[i]+=s[i-]+f[i];
} int main()
{
int n;
solve();
while (scanf("%d",&n) && n)
printf("%lld\n",s[n]);
return ;
}
fzu1969 GCD Extreme 类似于uva10561的更多相关文章
- spoj 3871. GCD Extreme 欧拉+积性函数
3871. GCD Extreme Problem code: GCDEX Given the value of N, you will have to find the value of G. Th ...
- UVA 11426 GCD - Extreme (II) (欧拉函数)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Problem JGCD Extreme (II)Input: Standard ...
- UVA 11426 - GCD - Extreme (II) (数论)
UVA 11426 - GCD - Extreme (II) 题目链接 题意:给定N.求∑i<=ni=1∑j<nj=1gcd(i,j)的值. 思路:lrj白书上的例题,设f(n) = gc ...
- 【UVa11426】GCD - Extreme (II)(莫比乌斯反演)
[UVa11426]GCD - Extreme (II)(莫比乌斯反演) 题面 Vjudge 题解 这.. 直接套路的莫比乌斯反演 我连式子都不想写了 默认推到这里把.. 然后把\(ans\)写一下 ...
- UVA11426 GCD - Extreme (II) (欧拉函数/莫比乌斯反演)
UVA11426 GCD - Extreme (II) 题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 10 100 200000 0 输出样例#1: 67 13 ...
- GCD - Extreme (II) for(i=1;i<N;i++) for(j=i+1;j<=N;j++) { G+=gcd(i,j); } 推导分析+欧拉函数
/** 题目:GCD - Extreme (II) 链接:https://vjudge.net/contest/154246#problem/O 题意: for(i=1;i<N;i++) for ...
- 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) 此 ...
- USACO GCD Extreme(II)
题目大意:求gcd(1,2)+gcd(1,3)+gcd(2,3)+...+gcd(n-1,n) ---------------------------------------------------- ...
- UVa 11426 (欧拉函数 GCD之和) GCD - Extreme (II)
题意: 求sum{gcd(i, j) | 1 ≤ i < j ≤ n} 分析: 有这样一个很有用的结论:gcd(x, n) = i的充要条件是gcd(x/i, n/i) = 1,因此满足条件的x ...
随机推荐
- 51nod_1490: 多重游戏(树上博弈)
题目链接 该题实质上是一个树上博弈的问题.要定义四种状态--2先手必胜 1先手必败 3可输可赢 0不能控制 叶子结点为先手必胜态: 若某结点的所有儿子都是先手必败态,则该结点为先手必胜态: 若某结点的 ...
- Docker for windows10 配置镜像加速
windows玩docker的时候,经常碰到这种问题: Error response from daemon: Get https://registry-1.docker.io/v2/: net/ht ...
- DL4NLP——词表示模型(二)基于神经网络的模型:NPLM;word2vec(CBOW/Skip-gram)
本文简述了以下内容: 神经概率语言模型NPLM,训练语言模型并同时得到词表示 word2vec:CBOW / Skip-gram,直接以得到词表示为目标的模型 (一)原始CBOW(Continuous ...
- (转)Java线程:新特征-条件变量
Java线程:新特征-条件变量 条件变量是Java5线程中很重要的一个概念,顾名思义,条件变量就是表示条件的一种变量.但是必须说明,这里的条件是没有实际含义的,仅仅是个标记而已,并且条件的含义往往 ...
- .babelrc 文件
文件干啥用的 babel是降es6转义成浏览器能理解的es5语法. 如果项目中用了babel 转移,需要定义babel需要的插件和预设转码. babel 一般可以配合 webpack . browse ...
- Vue项目搭建完整剖析全过程
Vue项目搭建完整剖析全过程 项目源码地址:https://github.com/ballyalex 有帮助的话就加个星星呗~! 项目技术栈:vue+webpack+bower+sass+axios ...
- git分支管理之创建与合并分支
在版本回退里,你已经知道,每次提交,Git都把它们串成一条时间线,这条时间线就是一个分支.截止到目前,只有一条时间线,在Git里,这个分支叫主分支,即master分支.HEAD严格来说不是指向提交,而 ...
- C++中加const与不加const的区别
“常量”与“只读变量”的区别. 常量肯定是只读的,例如5, "abc",等,肯定是只读的,因为常量是被编译器放在内存中的只读区域,当然也就不能够去修改它. “只读变量”则是在内存中 ...
- Python获取区域面积
import cv2 import numpy as np def get_leave_areas(image): img = cv2.imread(image) GrayImage = cv2.cv ...
- owncloud存储云服务搭建
owncloud是一款开源的存储云,用php写的,其作用可以像百度网盘那样存放文件到这个云当中, 只要能连上网,手机,平板,电脑都可以从中下载或者上传文件,用起来非常还不错,部署的过程也很简单,废话不 ...