博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
UVA GCD - Extreme (II)
阅读量:4475 次
发布时间:2019-06-08

本文共 2000 字,大约阅读时间需要 6 分钟。

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
#include
#include
#include
#include
#define ll long long#define maxn 4000005using namespace std;int zs[maxn/4],t=0,low[maxn+5];ll f[maxn+5],n,T;bool v[maxn+5];inline void init(){ f[1]=1,low[1]=1; for(int i=2;i<=maxn;i++){ if(!v[i]) zs[++t]=i,f[i]=i*2-1,low[i]=i; for(int j=1,u;j<=t&&(u=zs[j]*i)<=maxn;j++){ v[u]=1; if(!(i%zs[j])){ low[u]=low[i]*zs[j]; if(low[i]==i) f[u]=f[i]*zs[j]+low[i]*(zs[j]-1); else f[u]=f[i/low[i]]*f[low[u]]; break; } low[u]=zs[j]; f[u]=f[i]*(2*zs[j]-1); } } for(int i=1;i<=maxn;i++) f[i]+=f[i-1];}int main(){ init(); while(scanf("%lld",&n)==1&&n) printf("%lld\n",f[n]-n*(n+1)/2); return 0;}

 

转载于:https://www.cnblogs.com/JYYHH/p/8319863.html

你可能感兴趣的文章
CURRICULUM VITAE
查看>>
Groovy 程序结构
查看>>
SpringAOP用到了什么代理,以及动态代理与静态代理的区别
查看>>
备忘pwnable.kr 之passcode
查看>>
好久没敲代码了,手有点生——一个小小的时钟
查看>>
运算符 AS和IS 的区别
查看>>
(转)详解C中volatile关键字
查看>>
easyui时的时间格式yyyy-MM-dd与yyyy-MM-ddd HH:mm:ss
查看>>
Codeforces Round #426 (Div. 2) (A B C)
查看>>
The Most Simple Introduction to Hypothesis Testing
查看>>
UVA10791
查看>>
P2664 树上游戏
查看>>
jQuery 停止动画
查看>>
Sharepoint Solution Gallery Active Solution时激活按钮灰色不可用的解决方法
查看>>
教你50招提升ASP.NET性能(二十二):利用.NET 4.5异步结构
查看>>
lua连续随机数
查看>>
checkstyle使用介绍
查看>>
会了这十种Python优雅的写法,让你工作效率翻十倍,一人顶十人用!
查看>>
在做操作系统实验的一些疑问
查看>>
Log4J日志配置详解
查看>>