转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents    by---cxlove

题意:求sigma (gcd (i , j))  1 <= i < j <= n

和POJ 2480类似,如果枚举j,求的话,还是会TLE的。。。

考虑sigma(gcd (i , n)) = sigma (d * phi[n / d]) d | n。

做法同样是先预处理出phi,然后枚举gcd = d。

O(n)预处理,O(1)查询

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#define lowbit(x) (x & (-x))
#define Key_value ch[ch[root][1]][0]
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef long long LL;
const int N = 1000005;
int phi[N];
LL ans[N];
void init () {
for (int i = 2 ; i < N ; i ++) {
if (phi[i]) continue;
for (int j = i ; j < N ; j += i) {
if (phi[j] == 0) phi[j] = j;
phi[j] = phi[j] / i * (i - 1);
}
}
for (int i = 2 ; i < N ; i ++)
ans[i] = phi[i];
for (int i = 2 ; i * i < N ; i ++) {
ans[i * i] += 1LL * phi[i] * i;
for (int j = i + 1 ; j * i < N ; j ++) {
ans[j * i] += 1LL * phi[i] * j + 1LL * phi[j] * i;
}
}
for (int i = 1 ; i < N ; i ++)
ans[i] += ans[i - 1];
}
int main () {
#ifndef ONLINE_JUDGE
freopen ("input.txt" , "r" , stdin);
// freopen ("output.txt" , "w" , stdout);
#endif
int n ;
init ();
while (cin >> n && n) {
cout << ans[n] << endl;
}
return 0;
}

SPOJ GCDEX (数论)的更多相关文章

  1. SPOJ ARCTAN (数论) Use of Function Arctan

    详细的题解见这里. 图片转自上面的博客 假设我们已经推导出来x在处取得最小值,并且注意到这个点是位于两个整点之间的,所以从这两个整数往左右两边枚举b就能找到b+c的最小值. 其实只用往一边枚举就够了, ...

  2. spoj gcdex

    题解: 首先我们设gcd(i,j)=k 所以我们就要求对于所有k的方案总数 可以线性帅选欧拉函数 然后算法一:枚举k,O(NT) 算法二:考虑到我们只要n/k的整数部分 容易证明是sqrt(n)级别的 ...

  3. SPOJ - LOCKER 数论 贪心

    题意:求出\(n\)拆分成若干个数使其连乘最大的值 本题是之江学院网络赛的原题,计算规模大一点,看到EMAXX推荐就做了 忘了大一那会是怎么用均值不等式推出结果的(还给老师系列) 结论倒还记得:贪心分 ...

  4. (转载)有关反演和gcd

    tips : 积性函数 F (n) = Π F (piai ) 若F (n), G (n)是积性函数则 F (n) * G (n) Σd | n F (n) 是积性函数 n = Σd | n  φ ( ...

  5. bzoj 2226: [Spoj 5971] LCMSum 数论

    2226: [Spoj 5971] LCMSum Time Limit: 20 Sec  Memory Limit: 259 MBSubmit: 578  Solved: 259[Submit][St ...

  6. SPOJ DIVCNT2 [我也不知道是什么分类了反正是数论]

    SPOJ DIVCNT2 - Counting Divisors (square) 题意:求 \[ \sum_{i=1}^n\sigma_0(i^2) \] 好棒啊! 带着平方没法做,考虑用其他函数表 ...

  7. BZOJ 2226 [Spoj 5971] LCMSum 最大公约数之和 | 数论

    BZOJ 2226 [Spoj 5971] LCMSum 这道题和上一道题十分类似. \[\begin{align*} \sum_{i = 1}^{n}\operatorname{LCM}(i, n) ...

  8. 数论 - Funny scales(SPOJ - SCALE)

    Funny scales Problem's Link ------------------------------------------------------------------------ ...

  9. SPOJ - POLYNOM Polynomial(数论乱搞)题解

    题意 :给你n个数,问你是否存在一个多项式(最多三次方)满足f(i)= xi. 思路:讲一个神奇的思路: x3 - (x - 1)3 = 3x2 - 3x + 1 x2 - (x - 1)2 = 2x ...

随机推荐

  1. LDA-线性判别分析(三)

    本来是要调研 Latent Dirichlet Allocation 的那个 LDA 的, 没想到查到很多关于 Linear Discriminant Analysis 这个 LDA 的资料.初步看了 ...

  2. [Hapi.js] Serving static files

    hapi does not support serving static files out of the box. Instead it relies on a module called Iner ...

  3. iOS之Swift语言的学习

    好久都没有来这个熟悉而又陌生的地方啦, 想想已经有两三个月了吧,不过我相信以后还是会经常来的啦,因为忙碌的学习已经过去啦,剩下的就是要好好的总结好好的复习了,好好的熟悉下我们之前学习的知识点,将他们有 ...

  4. C# Datatable导出Excel方法

    C# 导出Excel方法  先引用下System.IO;System.data; 具体函数如下: public static bool ExportCSV(DataTable dt, string f ...

  5. ORA-01152错误解决方法(转)

    具体步骤如下: startup force; alter system set "_allow_resetlogs_corruption"=true scope=spfile; r ...

  6. The run destination XXX的 iPhone is not valid for Running the scheme 'Day7通讯录Demo'.

    错误:

  7. UIView的常用方法

    bringSubviewToFront: 把指定的子视图移动到顶层 - (void)bringSubviewToFront:(UIView *)view 参数 view 需要移到顶层的视图 conve ...

  8. Shell glob

    在Linux中,glob是用来匹配路径名的通配符,glob主要包含以下4种: Wildcard Matching * 匹配0个或者多个字符,比如d*,可以匹配d, d1, dd1 ?匹配单个字符,比如 ...

  9. php5.5新特性之yield理解

    今天,在阅读别人代码时,其中出现了一个陌生的关键字yield,想一探究竟,于是找到:http://php.net/manual/zh/language.generators.overview.php ...

  10. 工作中小知识点汇总(c#)

    1.OOP 实体与数据库字段转换(注意 此时实体字段必须和数据库中查询的字段列名相同) list = ModelExtend.GetByDataTablePart<EZRate>(ds.T ...