题意

求Σ{1<=i<N} Σ{i<j<=N} GCD(i, j)     (N<=4000000)

分析

原始思路

暴力求明显是不行的,我们把式子简化形式一下发现它可以写成Σ{2<=j<=N} GCD(1~j-1, j)

这个形式就给我们一种思路:可以只枚举j,然后快速算出GCD(1~j-1, j)

我们当然不能枚举1~j-1那么算,那么再换种思路,枚举可能的答案k,即j的所有约数。分别计算GCD(1~j-1, j) = k的方案数(HDU 1695),然后加起来就能求出和了。

【求GCD(x, j) = k,x ∈ (1, j-1)的个数】我们知道j必须是k的倍数,所以可以在等式两边同时除以k变成:GCD(x, j/k) = 1,x ∈ (1, j/k-1)。那么显然答案等于phi(j/k)。

进一步优化

上面的方法还是超时,我们还需要优化。在求j的约数时会有很多无用状态,它的过程很像是暴力判断素数一样,联想到筛法求素数,我们也可以想到类似的思路:枚举k,那么k的倍数就是j,然后再算GCD(1~j-1, j) = k。

#include
#include
#include
#include
#include
#include
#define MID(x,y) ((x+y)/2)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int MAX = 4000002;
int phi[MAX];
bool noprime[MAX];
vector prime;
void Euler(int n){
phi[1] = 0;
for (int i = 2; i

UVA 11426 GCD-Extreme(II) ★ (欧拉函数)的更多相关文章

  1. UVA 11426 GCD - Extreme (II) (欧拉函数+筛法)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/O 题意是给你n,求所有gcd(i , j)的和,其中 ...

  2. UVA 11426 GCD - Extreme (II)(欧拉函数打表 + 规律)

    Given the value of N, you will have to find the value of G. The definition of G is given below:Here ...

  3. uva 11426 GCD - Extreme (II) (欧拉函数打表)

    题意:给一个N,和公式 求G(N). 分析:设F(N)= gcd(1,N)+gcd(2,N)+...gcd(N-1,N).则 G(N ) = G(N-1) + F(N). 设满足gcd(x,N) 值为 ...

  4. UVA 11426 - GCD - Extreme (II) 欧拉函数-数学

    Given the value of N, you will have to find the value of G. The definition of G is given below:G =i< ...

  5. UVA 11426 GCD - Extreme (II) 欧拉函数

    分析:枚举每个数的贡献,欧拉函数筛法 #include <cstdio> #include <iostream> #include <ctime> #include ...

  6. 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) 此 ...

  7. UVA11426 GCD - Extreme (II) (欧拉函数/莫比乌斯反演)

    UVA11426 GCD - Extreme (II) 题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 10 100 200000 0 输出样例#1: 67 13 ...

  8. UVA11426 GCD - Extreme (II)---欧拉函数的运用

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  9. UVA11426 GCD - Extreme (II) —— 欧拉函数

    题目链接:https://vjudge.net/problem/UVA-11426 题意: 求 ∑ gcd(i,j),其中 1<=i<j<=n . 题解:1. 欧拉函数的定义:满足 ...

  10. UVA 11426 - GCD - Extreme (II) (数论)

    UVA 11426 - GCD - Extreme (II) 题目链接 题意:给定N.求∑i<=ni=1∑j<nj=1gcd(i,j)的值. 思路:lrj白书上的例题,设f(n) = gc ...

随机推荐

  1. Can't update table 'test_trigger' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

    [Err] 1442 - Can't update table 'test_trigger' in stored function/trigger because it is already used ...

  2. VIM配置(转载)

    注: 转载于http://www.cnblogs.com/ma6174/ 花了很长时间整理的,感觉用起来很方便,共享一下. 我的vim配置主要有以下优点: 1.按F5可以直接编译并执行C.C++.ja ...

  3. JS利用正则配合replace替换指定字符

    替换指定字符的方法有很多,在本文为大家详细介绍下,JS利用正则配合replace是如何做到的,喜欢的朋友可以参考下 定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一 ...

  4. Java7 新特性 switch 可以使用String

    今天和大家分享下 在java7中可以使用String 作为switch 中的参数. 原来在java7之前,switch只能去接收一个 byte.char.short.int 类型 现在在java7中 ...

  5. Maintainable HashCode and Equals Using Apache Commons

    Java hashCode and equals methods can be tricky to implement correctly. Fortunately, all majors IDEs ...

  6. 【leetcode】Add Two Numbers(middle) ☆

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  7. POJ 1663

    #include<iostream>//cheng da cai zi using namespace std; int main() { int time; cin>>tim ...

  8. Ubuntu 装JDK

    我是按照这篇文章安装jdk的: http://www.cnblogs.com/bluestorm/archive/2012/05/10/2493592.html   先去 Oracle下载Linux下 ...

  9. Project Euler 99:Largest exponential 最大的幂

    Largest exponential Comparing two numbers written in index form like 211 and 37 is not difficult, as ...

  10. python学习[二]

    继续学习 http://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html 词典的每个元素是键值对.元素没有顺序.dic = {'tom':11 ...