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 ...
随机推荐
- struts——文件上传
上传文件在一个系统当中是一个很常用的功能,也是一个比较重要的功能.今天我们就一起来学习一下Struts2如何上传文件. 今天讲的上传文件的方式有三种: 1,以字节为单位传输文件: 2,Struts2封 ...
- poj1410
简单题,注意是实心矩形 #include <iostream> #include <math.h> #include <iomanip> #define eps 1 ...
- Html中value和name属性的作用
1.按钮中用的value 指的是按钮上要显示的文本 比如“确定”“删除”等 2.复选框用的value 指的是这个复选框的值 3.单选框用的value 和复选框一样 4.下拉菜单用的value 是列表 ...
- keil将程序装入外部FLASH具体解释
在实际项目中,常常出现芯片的内部FLASH空间不够的情况,这就须要将程序分一部分装到外部FLASH中. 为了让大家能少走些弯路,在这里把我在这当中遇到的一些问题和经验教训给大家分享一下. 仅供參考,假 ...
- [Regular Expressions] Match the Same String Twice
Regular Expression Backreferences provide us a method to match a previously captured pattern a secon ...
- 函数nvl,nvl2,nullif,coalesce
NVL: Converts a null value to an actual valueNVL2:If expr1 is not null, NVL2 returns expr2. If expr1 ...
- cocoapods在OS X Yosemite系统中报错
之前使用cocoapods一直是正常使用的,刚换了电脑,使用pod install的时候报错: /System/Library/Frameworks/Ruby.framework/Versions/2 ...
- html中把li前面的的小圆点换成小图片的方法
li { list-style: none; background: url(../img/li_dis.png) no-repeat left; padding-left: 20px; }
- access数据库的连接字符串以及数据库操作类
<!--access数据库连接方式--> <add name="QYTangConnectionString" connectionString="Pr ...
- react-native使用react-art制作SVG动画
想要使用SVG做一个动画,郁闷了一上午终于有了一点思路.. 其实我是看了一篇国外的文章.网址:http://browniefed.com/blog/2015/05/03/getting-react-a ...