SPOJ GCDEX (数论)
转载请注明出处,谢谢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 (数论)的更多相关文章
- SPOJ ARCTAN (数论) Use of Function Arctan
详细的题解见这里. 图片转自上面的博客 假设我们已经推导出来x在处取得最小值,并且注意到这个点是位于两个整点之间的,所以从这两个整数往左右两边枚举b就能找到b+c的最小值. 其实只用往一边枚举就够了, ...
- spoj gcdex
题解: 首先我们设gcd(i,j)=k 所以我们就要求对于所有k的方案总数 可以线性帅选欧拉函数 然后算法一:枚举k,O(NT) 算法二:考虑到我们只要n/k的整数部分 容易证明是sqrt(n)级别的 ...
- SPOJ - LOCKER 数论 贪心
题意:求出\(n\)拆分成若干个数使其连乘最大的值 本题是之江学院网络赛的原题,计算规模大一点,看到EMAXX推荐就做了 忘了大一那会是怎么用均值不等式推出结果的(还给老师系列) 结论倒还记得:贪心分 ...
- (转载)有关反演和gcd
tips : 积性函数 F (n) = Π F (piai ) 若F (n), G (n)是积性函数则 F (n) * G (n) Σd | n F (n) 是积性函数 n = Σd | n φ ( ...
- bzoj 2226: [Spoj 5971] LCMSum 数论
2226: [Spoj 5971] LCMSum Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 578 Solved: 259[Submit][St ...
- SPOJ DIVCNT2 [我也不知道是什么分类了反正是数论]
SPOJ DIVCNT2 - Counting Divisors (square) 题意:求 \[ \sum_{i=1}^n\sigma_0(i^2) \] 好棒啊! 带着平方没法做,考虑用其他函数表 ...
- BZOJ 2226 [Spoj 5971] LCMSum 最大公约数之和 | 数论
BZOJ 2226 [Spoj 5971] LCMSum 这道题和上一道题十分类似. \[\begin{align*} \sum_{i = 1}^{n}\operatorname{LCM}(i, n) ...
- 数论 - Funny scales(SPOJ - SCALE)
Funny scales Problem's Link ------------------------------------------------------------------------ ...
- SPOJ - POLYNOM Polynomial(数论乱搞)题解
题意 :给你n个数,问你是否存在一个多项式(最多三次方)满足f(i)= xi. 思路:讲一个神奇的思路: x3 - (x - 1)3 = 3x2 - 3x + 1 x2 - (x - 1)2 = 2x ...
随机推荐
- LeetCode227:Basic Calculator II
Implement a basic calculator to evaluate a simple expression string. The expression string contains ...
- java public protect default private
(1)对于public修饰符,它具有最大的访问权限,可以访问任何一个在CLASSPATH下的类.接口.异常等.它往往用于对外的情况,也就是对象或类对外的一种接口的形式. (2)对于protected修 ...
- CLR via C# - Char_String
.NET中Char表示为16为的Unicode值,Char提供两个public const字段MinValue('\0',写成'\u0000'也是一样的)和MaxValue('\uffff'). Ch ...
- EasyUI 1.3.1以下的组合验证
适用于EasyUI 1.3.1以下的, 1.3.2已经自带组合验证(如validType:['validator1','validator2']) $.extend($.fn.validatebox. ...
- 表单元素-select
<form> <select size="2"> <option value="JMS HADEN">JMS HADEN&l ...
- C# WinFrom 导入Excel文件,显示进度条
因为WINForm程序是在64位上运行如果使用另外一种快速的读取Excel的方法会报“未在本地计算机上注册“Microsoft.Jet.OLEDB.12.0”提供程序” 所以我就换了现在这种读取有点慢 ...
- iOS 开发~UIWindow
比如下面例子: 写出第一个iOS的程序,在界面上显示“Hello World” 1.如何新建一个工程 iOS—>Single View Application—>工程名.保存位置 2.运行 ...
- 0115——cocoapod的使用
iOS 最新版 CocoaPods 的安装流程 1.移除现有Ruby默认源 $gem sources --remove https://rubygems.org/ 2.使用新的源 $gem sourc ...
- ARM编译空间属性(转)
原文地址:http://www.cnblogs.com/hongzg1982/articles/2205093.html 1. 程序的空间属性 一般情况下,一个程序本质上都是由 bss段.data段. ...
- git commit的--amend选项
git commit --amend常常用来修改某个branch上最顶端的commit,大多数情况下,这个命令给人的感觉是用新的commit替换了原来的commit.git commit --amen ...