3871. GCD Extreme

Problem code: GCDEX

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(k=i;k< N;k++)

for(j=i+1;j<=N;j++)

{

G+=gcd(k,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="" problem="" statement.="" input="" terminated="" by="" a="" line="" containing="" single="" zero.="" <h3="">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.

Example

Input:
10
100
200000
0 Output:
67
13015
143295493160 题意:

G=0;

for(k=i;k< N;k++)

for(j=i+1;j<=N;j++)

{

G+=gcd(k,j);

}

思路: G[n] = sigma( d|n  phi[d]*(n/d) ); 这个能求出S[n]的值,累加求和就行。

   关键在于G[n]函数能用筛选来做,因为是积性函数。

两种筛选方法,一种TLE,一种ac。

超时代码:

 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long LL; const int maxn = +;
LL G[maxn];
int opl[maxn];
void init()
{
LL i,j;
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++)
G[j*i] = G[j*i] + opl[i]*j;
}
for(i=;i<maxn;i++)
G[i] +=G[i-];
}
int main()
{
init();
int T,n;
while(scanf("%d",&n)>)
{
printf("%lld\n",G[n]);
}
return ;
}

AC代码:

 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long LL; const int maxn = 1e6+;
int phi[maxn];
LL g[maxn];
void init()
{
for(int i=;i<maxn;i++) phi[i] = i;
for(int i=;i<maxn;i++)
{
if(phi[i]==i) phi[i] = i-;
else continue;
for(int j=i+i;j<maxn;j=j+i)
phi[j] = phi[j]/i*(i-);
}
for(int i=;i<maxn;i++) g[i] = phi[i];
for(int i=;i<=;i++)
{
for(LL j=i*i,k=i;j<maxn;j=j+i,k++)
if(i!=k)
g[j] = g[j] + phi[i]*k + phi[k]*i;
else g[j] = g[j] + phi[i]*k;
}
g[] = ;
for(int i=;i<maxn;i++) g[i] = g[i]+g[i-];
}
int main()
{
init();
int T,n;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
printf("%lld\n",g[n]);
}
return ;
}

spoj 3871. GCD Extreme 欧拉+积性函数的更多相关文章

  1. spoj 3871 gcd extreme

    题目大意给出一个n,求sum(gcd(i,j),<i<j<=n); 可以明显的看出来s[n]=s[n-]+f[n]; f[n]=sum(gcd(i,n),<i<n); 现 ...

  2. POJ 2480 Longge's problem (积性函数,欧拉函数)

    题意:求∑gcd(i,n),1<=i<=n思路:f(n)=∑gcd(i,n),1<=i<=n可以知道,其实f(n)=sum(p*φ(n/p)),其中p是n的因子.为什么呢?原因 ...

  3. 51nod1040 最大公约数之和,欧拉函数或积性函数

    1040 最大公约数之和 给出一个n,求1-n这n个数,同n的最大公约数的和.比如:n = 6时,1,2,3,4,5,6 同6的最大公约数分别为1,2,3,2,1,6,加在一起 = 15 看起来很简单 ...

  4. poj 2480 Longge's problem 积性函数

    思路:首先给出几个结论: 1.gcd(a,b)是积性函数: 2.积性函数的和仍然是积性函数: 3.phi(a^b)=a^b-a^(b-1); 记 f(n)=∑gcd(i,n),n=p1^e1*p2^e ...

  5. 【模板】埃拉托色尼筛法 && 欧拉筛法 && 积性函数

    埃拉托色尼筛法 朴素算法 1 vis[1]=1; 2 for (int i=2;i<=n;i++) 3 if (!vis[i]) 4 { 5 pri[++tot]=i; 6 for (int j ...

  6. POJ_2480 Longge's problem【积性函数+欧拉函数的理解与应用】

    题目: Longge is good at mathematics and he likes to think about hard mathematical problems which will ...

  7. 积性函数初步(欧拉$\varphi$函数)

    updata on 2020.4.3 添加了欧拉\(\varphi\)函数为积性函数的证明和它的计算方式 1.积性函数 设\(f(n)\)为定义在正整数上的函数,若\(f(1)=1\),且对于任意正整 ...

  8. hdu2421-Deciphering Password-(欧拉筛+唯一分解定理+积性函数+立方求和公式)

    Deciphering Password Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  9. Master of Phi (欧拉函数 + 积性函数的性质 + 狄利克雷卷积)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6265 题目大意:首先T是测试组数,n代表当前这个数的因子的种类,然后接下来的p和q,代表当前这个数的因 ...

随机推荐

  1. Fzu oj2194星系碰撞(排序+并查集+路径压缩)

    Problem 2194 星系碰撞 Accept: 14    Submit: 48Time Limit: 30000 mSec    Memory Limit : 327680 KB  Proble ...

  2. C++之路进阶——bzoj1468(tree)

    F.A.Qs Home Discuss ProblemSet Status Ranklist Contest ModifyUser  gryz2016 Logout 捐赠本站 Notice:由于本OJ ...

  3. mac office

    弄了个office2016正式版的教程,大多数朋友表示搞不懂,SO重新写了个超级详细的,在不懂我也是醉了.Mac office 2016免费安装教程微软近日正式向 Office 365 订阅用户发布了 ...

  4. 夺命雷公狗ThinkPHP项目之----企业网站12之文章添加的实现

    我们现在就开始写文章添加了,居然是添加当然布列外,我们还是要先讲模版搞定再说被: <!doctype html> <html> <head> <meta ch ...

  5. 9. 星际争霸之php设计模式--代理模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  6. java.lang.UnsatisfiedLinkError: no sapjco3 in java.library.path

    1.system32添加sapjco3.dll 2.tomcat bin下添加sapjco3.dll 3.项目中添加sapjco3.jar 包

  7. CSS 文字阴影(text-shadow)怎么用

    textShadow="1px 1px 1px #ff0000" textShadow="水平位移 垂直位移 模糊半径 阴影颜色"

  8. nodejs和mongodb实践

    首先,当然是都安装了nodejs 和mongodb了.这必须是前提条件. 现在我们要用nodejs连接mongodb数据库了.我这里只是一个非常非常简单是实践,初学嘛.更深入的学习之后,我会仔细写笔记 ...

  9. Mongodb 笔记09 备份、部署MongoDB

    备份 1. 只有在有信心能在紧急情况下完成迅速部署的情况下,备份才是有用的.所以,无论选择了哪种备份技术,一定要对备份及恢复备份的操作进行练习,知道了然于心. 2. 通常情况下,应对副本集的非主节点( ...

  10. 使用ActionFilterAttribute 记录 WebApi Action 请求和返回结果记录

    使用ActionFilterAttribute 记录 WebApi Action 请求和返回结果记录 C#进阶系列——WebApi 异常处理解决方案 [ASP.NET Web API教程]4.3 AS ...