转载请注明出处,谢谢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. 訪问远程WAMP 下phpmyadmin

    WAMP环境是一个非常优秀的webservice集成环境,它集成的phpmyadmin也是一款非常优秀的数据库訪问软件.wamp默认安装下,phpmyadmin工具仅仅能本地用,在站点开发中,数据库都 ...

  2. RelativeLayout经常使用属性介绍

    以下介绍一下RelativeLayout用到的一些重要的属性: 第一类:属性值为true或false     android:layout_centerHrizontal                ...

  3. HTTP协议报文格式

    HTTP协议报文格式 接下来我们看看HTTP协议(Hypertext Transfer Protocol――超文本传输协议)浏览器端(客户端)向WEB服务器端访问页面的过程和HTTP协议报文的格式. ...

  4. xtrabackup备份恢复测试

    http://blog.chinaunix.net/uid-20682026-id-3319204.html

  5. Andriod布局之LinearLayout

    LinearLayout是安卓中的常见布局,即线性布局.(提示:在Andriod中要常用alt+/快捷键来补全代码 其中有一个重要的属性android:orientation,它是表示线性布局的方向问 ...

  6. menu控件绑定sql数据库

    public int treeID; protected void Page_Load(object sender, EventArgs e) { SqlConnection CN = new Sql ...

  7. 将集合类转换成DataTable

    /// <summary> /// 将集合类转换成DataTale /// </summary> /// <param name="list"> ...

  8. UILabel自适应高度,自动换行

    CGRect rect; rect = self.labelInfo.frame; //UILabel高度自适应 rect.size.height = [self.labelInfo.text bou ...

  9. java基础学习笔记

    Q:    What if the main method is declared as private?  如果将主函数声明为私有类型的会怎样? A:     The program compile ...

  10. hdu3081 Marriage Match II(最大流)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud Marriage Match II Time Limit: 2000/1000 M ...