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

  1. Input:
  2. 10
  3. 100
  4. 200000
  5. 0
  6.  
  7. Output:
  8. 67
  9. 13015
  10. 143295493160
  11.  
  12. 题意:

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。

超时代码:

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<cstring>
  4. #include<cstdlib>
  5. using namespace std;
  6. typedef long long LL;
  7.  
  8. const int maxn = +;
  9. LL G[maxn];
  10. int opl[maxn];
  11. void init()
  12. {
  13. LL i,j;
  14. for(i=;i<maxn;i++) opl[i] = i;
  15. for(i=;i<maxn;i++)
  16. {
  17. if(opl[i]==i)
  18. {
  19. for(j=i;j<maxn;j=j+i)
  20. opl[j]=opl[j]/i*(i-);
  21. }
  22. for(j=;i*j<maxn;j++)
  23. G[j*i] = G[j*i] + opl[i]*j;
  24. }
  25. for(i=;i<maxn;i++)
  26. G[i] +=G[i-];
  27. }
  28. int main()
  29. {
  30. init();
  31. int T,n;
  32. while(scanf("%d",&n)>)
  33. {
  34. printf("%lld\n",G[n]);
  35. }
  36. return ;
  37. }

AC代码:

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<cstring>
  4. #include<cstdlib>
  5. using namespace std;
  6. typedef long long LL;
  7.  
  8. const int maxn = 1e6+;
  9. int phi[maxn];
  10. LL g[maxn];
  11. void init()
  12. {
  13. for(int i=;i<maxn;i++) phi[i] = i;
  14. for(int i=;i<maxn;i++)
  15. {
  16. if(phi[i]==i) phi[i] = i-;
  17. else continue;
  18. for(int j=i+i;j<maxn;j=j+i)
  19. phi[j] = phi[j]/i*(i-);
  20. }
  21. for(int i=;i<maxn;i++) g[i] = phi[i];
  22. for(int i=;i<=;i++)
  23. {
  24. for(LL j=i*i,k=i;j<maxn;j=j+i,k++)
  25. if(i!=k)
  26. g[j] = g[j] + phi[i]*k + phi[k]*i;
  27. else g[j] = g[j] + phi[i]*k;
  28. }
  29. g[] = ;
  30. for(int i=;i<maxn;i++) g[i] = g[i]+g[i-];
  31. }
  32. int main()
  33. {
  34. init();
  35. int T,n;
  36. scanf("%d",&T);
  37. while(T--)
  38. {
  39. scanf("%d",&n);
  40. printf("%lld\n",g[n]);
  41. }
  42. return ;
  43. }

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. cookie 使用笔记

    参考书<JSP Web 开发案例教程> index.jsp页面 dologin.jsp页面 welcome.jsp页面 页面显示 点击提交

  2. Ini文件操作函数

    /// <summary> /// Copies a string into the specified section of an initialization file. /// &l ...

  3. bzoj4238 电压

    首先先直接对图进行二染色,dfs染完色后,有的边为搜索树边,有的为非树边,当非树边连接的两头的点为异色的时候,那么很明显这条非树边和树边构成的环上的边必然不可能成为答案:如果非树边的两端的点同色,那么 ...

  4. c#读取INI文件类

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.IO;na ...

  5. JQuery书写Ajax的几种方式?

    1 $.ajax({ type: "Post", //请求方式 ("POST" 或 "GET"), 默认为 "GET" ...

  6. cometd使用-bayeux协议(读法:贝叶)

    bayeux.createChannelIfAbsent("/**", new ServerChannel.Initializer() { @Override public voi ...

  7. Java中的json数据类型操作

    package com.ss1.json; import java.util.ArrayList; import java.util.HashMap; import java.util.List; i ...

  8. python—类对象和实例对象的区别

    最近在对RF的通讯层的模块进行封装,需要将之前放在类似main里面的一个方法,如下所示:这段代码是开发提供,用于接口测试,模拟底层通讯,具体的通讯是在dll内,python这边只是做了个封装让RF进行 ...

  9. C语言初学者代码中的常见错误与瑕疵(4)

    问题 小学生数学 很多小学生在学习加法时,发现“进位”特别容易出错.你的任务是计算两个数在相加时需要多少次进位.你编制的程序应当可以连续处理多组数据,直到读到两个0(这是输入结束标记). 样例: 输入 ...

  10. 一个fork()系统调用的问题

    转载:http://coolshell.cn/articles/7965.html 题目:请问下面的程序一共输出多少个“-”? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...