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 ...
随机推荐
- linux下 git 安装
1.使用yum安装 yum -y install git yum remove git 2.源代码安装 a.下载git源码 网址为 https://github.com/git/git/releas ...
- SpringMVC源码情操陶冶-View视图渲染
本节简单分析View视图对象的render方法 View接口 最重要的就是render()方法,具体源码如下 /** * Render the view given the specified mod ...
- Linux,activemq-cpp之消息过滤器
假设过滤器字符串如下: filt1=aaaa filt2=bbbb filt3=cccc activeMQ-cpp中消息过滤器,在发送消息的producer.cpp中,对message进行属性设置,m ...
- 替换Spring Boot 的EnableCaching注解
SpringBoot 中可使用@Cacheable注解来更方便的使用redis,这个注解是通过拦截器工作的,使用了@Cacheable的方法执行时,执行到CglibAopProxy.java中的 Dy ...
- 【Django】中间件
Middleware 这个地方把所有Request 拦截住,用我们自己的方式完成处理以后直接返回 Response.因此了解中间件的构成是非常必要的. Initializer: __init__(se ...
- Android 性能测试——Heap Viewer 工具
Android 性能测试--Heap Viewer 工具 Heap Viewer能做什么? 实时查看App分配的内存大小和空闲内存大小 发现Memory Leaks Heap Viewer使用条件 5 ...
- Java curator操作zookeeper获取kafka
Java curator操作zookeeper获取kafka Curator是Netflix公司开源的一个Zookeeper客户端,与Zookeeper提供的原生客户端相比,Curator的抽象层次更 ...
- 使用gitLab 或 github 关联本地仓库
要先在git里面注册自己的邮箱 然后: git commit -m 是为本次提交命名 刷新gitLab 发现更新了
- Git时光机穿梭之工作区和暂存区
Git和其他版本控制系统如SVN的一个不同之处就是有暂存区的概念. 先来看名词解释. 工作区(Working Directory) 就是你在电脑里能看到的目录,比如我的learngit文件夹就是一个工 ...
- OC和JS的交互
1.引入类拓展UIWebView+TS_JavaScriptContext, 这个类拓展是能在JSCotext出现的时候就可以拿到. 因为一般情况下JSCotext在webViewDidFinishL ...