UVA GCD - Extreme (II)
discription
Given the value of N, you will have to find the value of G. The definition of G is given below:
Here GCD(i, j) means the greatest common divisor of integer i and integer j.
For those who have trouble understanding summation notation, the meaning of G is given in the
following code:
G=0;
for(i=1;i<N;i++)
for(j=i+1;j<=N;j++)
{
G+=gcd(i,j);
}
/*Here gcd() is a function that finds
the greatest common divisor of the two
input numbers*/
Input
The input file contains at most 100 lines of inputs. Each line contains an integer N (1 < N < 4000001).
The meaning of N is given in the problem statement. Input is terminated by a line containing a single
zero.
Output
For each line of input produce one line of output. This line contains the value of G for the corresponding
N. The value of G will fit in a 64-bit signed integer.
Sample Input
10
100
200000
0
Sample Output
67
13015
143295493160
貌似是蓝书上有的一道题,当时刘汝佳是用 N log N 的筛法筛的,但是我们如果把积性函数推出来的话,可以
把那个log也去掉,做到O(N)预处理,O(1)查询。
大概最后就是推这么个积性函数: f(T)=Σφ(d)*(T/d) ,其中d|T
优化了一个log之后艹爆了时限hhh


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define ll long long
#define maxn 4000005
using namespace std;
int zs[maxn/],t=,low[maxn+];
ll f[maxn+],n,T;
bool v[maxn+]; inline void init(){
f[]=,low[]=;
for(int i=;i<=maxn;i++){
if(!v[i]) zs[++t]=i,f[i]=i*-,low[i]=i;
for(int j=,u;j<=t&&(u=zs[j]*i)<=maxn;j++){
v[u]=;
if(!(i%zs[j])){
low[u]=low[i]*zs[j];
if(low[i]==i) f[u]=f[i]*zs[j]+low[i]*(zs[j]-);
else f[u]=f[i/low[i]]*f[low[u]];
break;
} low[u]=zs[j];
f[u]=f[i]*(*zs[j]-);
}
} for(int i=;i<=maxn;i++) f[i]+=f[i-];
} int main(){
init();
while(scanf("%lld",&n)==&&n) printf("%lld\n",f[n]-n*(n+)/);
return ;
}
UVA GCD - Extreme (II)的更多相关文章
- UVA 11426 - GCD - Extreme (II) (数论)
UVA 11426 - GCD - Extreme (II) 题目链接 题意:给定N.求∑i<=ni=1∑j<nj=1gcd(i,j)的值. 思路:lrj白书上的例题,设f(n) = gc ...
- UVA 11426 GCD - Extreme (II) (欧拉函数)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud Problem JGCD Extreme (II)Input: Standard ...
- 【UVa11426】GCD - Extreme (II)(莫比乌斯反演)
[UVa11426]GCD - Extreme (II)(莫比乌斯反演) 题面 Vjudge 题解 这.. 直接套路的莫比乌斯反演 我连式子都不想写了 默认推到这里把.. 然后把\(ans\)写一下 ...
- UVA11426 GCD - Extreme (II) (欧拉函数/莫比乌斯反演)
UVA11426 GCD - Extreme (II) 题目描述 PDF 输入输出格式 输入格式: 输出格式: 输入输出样例 输入样例#1: 10 100 200000 0 输出样例#1: 67 13 ...
- GCD - Extreme (II) for(i=1;i<N;i++) for(j=i+1;j<=N;j++) { G+=gcd(i,j); } 推导分析+欧拉函数
/** 题目:GCD - Extreme (II) 链接:https://vjudge.net/contest/154246#problem/O 题意: for(i=1;i<N;i++) for ...
- UVa 11426 - GCD - Extreme (II)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 11426 (欧拉函数 GCD之和) GCD - Extreme (II)
题意: 求sum{gcd(i, j) | 1 ≤ i < j ≤ n} 分析: 有这样一个很有用的结论:gcd(x, n) = i的充要条件是gcd(x/i, n/i) = 1,因此满足条件的x ...
- UVA 11426 GCD - Extreme (II) (欧拉函数+筛法)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/O 题意是给你n,求所有gcd(i , j)的和,其中 ...
- GCD - Extreme (II) UVA - 11426(欧拉函数!!)
G(i) = (gcd(1, i) + gcd(2, i) + gcd(3, i) + .....+ gcd(i-1, i)) ret = G(1) + G(2) + G(3) +.....+ G(n ...
随机推荐
- 【BZOJ 3172】[Tjoi2013]单词 AC自动机
关于AC自动机:一个在kmp与Trie的基础上建立的数据结构,关键在于Trie树结构与fail指针,他们各有各的应用.在AC自动机里最典型的就是多串匹配,原本效率为O(n*l+n*l+m*l),(n是 ...
- POJ1062:昂贵的聘礼(dfs)
昂贵的聘礼 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 58108 Accepted: 17536 题目链接:http ...
- Binding and styling text to a RichTextBox in WPF
http://www.codeproject.com/Articles/137209/Binding-and-styling-text-to-a-RichTextBox-in-WPF The Rich ...
- javascript中的递增递减操作符
javascript中递增递减属于一元操作符,所谓一元操作符,即只能操作一个值的操作符. 递增和递减操作符各有两个版本:前置型和后置型.顾名思义,前置型应该位于要操作的变量之前,而后置型应该位于要操作 ...
- input输入浮动提示
html代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...
- 【HDU3853】LOOPS [期望DP]
LOOPS Time Limit: 5 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description Akemi Homura is a ...
- 【BZOJ】1827: [Usaco2010 Mar]gather 奶牛大集会
[算法]树型DP||树的重心(贪心) [题解] 两遍DFS,第一次得到所有节点子树的路径和,第二次给出除了该子树外其它部分的路径和,时时计算答案. long long!!! #include<c ...
- algorithm ch7 QuickSort
快速排序是基于分治模式的排序,它将数组a[p,...,r]分成两个子数组a[p,...q-1],a[q+1,...,r],使得a[p,...,q-1]中每个元素都小于a[q],而且小于等于a[q+1, ...
- PL/SQL 07 触发器 trigger
--触发器 触发器有三类: 数据操作触发器 用before触发器进行数据校验 用after触发器进行级联操作 语句触发器限制数据的操作和记录操作日志 instead of 触发 ...
- CSS变形
css3 变形/变换 相关属性 transform transform-origin transform-style:flat/preserve-3d perspective: 长度单位 perspe ...