uva11426 GCD Extreme(II)
题意:求sum(gcd(i,j),1<=i<j<=n)1<n<4000001
思路:
1.建立递推关系,s(n)=s(n-1)+gcd(1,n)+gcd(2,n)+……+gcd(n-1,n);
2.设f(n)=gcd(1,n)+gcd(2,n)+……+gcd(n-1,n)。
gcd(x,n)=i是n的约数(x<n),按照这个约数进行分类。设满足gcd(x,n)=i的约束有g(n,i)个,则有f(n)=sum(i*g(n,i))。
而gcd(x,n)=i等价于gcd(x/i,n/i)=1,因此g(n,i)等价于phi(n/i).phi(x)为欧拉函数。
3.降低时间复杂度。用筛法预处理phi[x]表
用筛法预处理f(x)->枚举因数,更新其所有倍数求解。
#include <iostream>
#include <cstdio>
#include<cstdlib>
#include<cctype>
#include<cstring>
#include<vector>
#include<cassert> using namespace std;
const int maxn = ;
#define LL long long
#define clc(a,b) memset(a,b,sizeof(a))
LL S[maxn],f[maxn];
LL phi[maxn];
void phi_table(int n)
{
for(int i=;i<=n;i++)
phi[i]=;
phi[]=;
for(int i=;i<=n;i++)
{
if(phi[i]==)
{
for(int j=i;j<=n;j+=i)
{
if(!phi[j])
phi[j]=j;
phi[j]=phi[j]/i*(i-);
}
}
}
} int main()
{
phi_table(maxn);
clc(f,);
for(int i=;i<=maxn;i++)
{
for(int n=i*;n<=maxn;n+=i)
f[n]+=i*phi[n/i];
}
S[]=f[];
for(int n=;n<=maxn;n++)
S[n]=S[n-]+f[n];
int n;
while(scanf("%d",&n),n)
{
printf("%lld\n",S[n]);
}
return ;
}
uva11426 GCD Extreme(II)的更多相关文章
- UVA11426 GCD - Extreme (II) (欧拉函数/莫比乌斯反演)
UVA11426 GCD - Extreme (II) 题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 10 100 200000 0 输出样例#1: 67 13 ...
- UVA11426 GCD - Extreme (II) —— 欧拉函数
题目链接:https://vjudge.net/problem/UVA-11426 题意: 求 ∑ gcd(i,j),其中 1<=i<j<=n . 题解:1. 欧拉函数的定义:满足 ...
- UVA11426 GCD - Extreme (II)---欧拉函数的运用
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- [题解] UVA11426 GCD - Extreme (II)
题面 莫反是不可能莫反的,这辈子都不可能莫反了 题目要求的是 \[ \sum\limits_{i=1}^n \sum\limits_{j=i+1}^n \gcd(i,j) \] 稍微变个亚子 \[ \ ...
- 【UVa11426】GCD - Extreme (II)(莫比乌斯反演)
[UVa11426]GCD - Extreme (II)(莫比乌斯反演) 题面 Vjudge 题解 这.. 直接套路的莫比乌斯反演 我连式子都不想写了 默认推到这里把.. 然后把\(ans\)写一下 ...
- 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 ...
- 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 ...
- GCD - Extreme (II)(UVA11426)
思路:欧拉函数: 欧拉函数,然后用下等差数列公式就行了. 1 #include<stdio.h> 2 #include<algorithm> 3 #include<ios ...
随机推荐
- ExtJS4.2学习(20)动态数据表格之前几章总结篇1(转)
鸣谢:http://www.shuyangyang.com.cn/jishuliangongfang/qianduanjishu/2014-02-18/196.html --------------- ...
- jquery判断对象是否获得焦点
var isFocus=$("#tRow").is(":focus"); if(true==isFocus){ alert("focus") ...
- PAT-乙级-1011. A+B和C (15)
1011. A+B和C (15) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 HOU, Qiming 给定区间[-231, 231 ...
- uva 701
参考了一下http://hi.baidu.com/renxl51/item/e80b688f9f54aadd5e0ec1de 给一个数字x,求最小的正整数e,使得pow(2,e) == x*pow(1 ...
- spoj 178
输出相邻的点 比较简单吧....... #include <cstdio> #include <cstring> using namespace std; int main ...
- hdu 4664 Triangulation 博弈论
看到这题时,当时还不会做,也没搞懂sg函数,于是狠狠的钻研了下博弈论,渐渐的知道了sg函数…… 现在在来做这题就很容易了,1A 打表容易发现在80左右的时候就出现循环节了 代码如下: #include ...
- eclipse安装Log4E插件以及简单使用
一. Log4E插件下载 下载地址:http://log4e.jayefem.de/content/view/3/2/ 二.安装Log4E插件 将下载下来的压缩包解压缩,如下图所示: 解压缩生成的[d ...
- UIcollectionView的使用(首页的搭建2)
2.2 直接购买的UIcollectionCell 2.2.1创建CFPromptBuyCell,继承自UICollectionViewCell,定义了标题和图片两个属性 2.2.2 在.m文件中定义 ...
- Spring下载地址
spring官方网站改版后,不提供直接下载,而是通过maven下载,所以将直接下载的地址给出: http://maven.springframework.org/release/org/springf ...
- Qt: 多线程, 就是这么简单(确实非常简洁明了)
#include <iostream>#include <QApplication>#include <QThread>#include <QString&g ...