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 ;
}
最新文章
- folly::AtomicHashmap源码分析(一)
- jdk研究——java.lang
- One to One 的数据库模型设计与NHibernate配置
- struts2 Result Type四个常用转跳类型
- 20145304 《Java程序设计》课程总结
- 【BZOJ1208】[HNOI2004]宠物收养所 Splay
- 【WEB前端经验之谈】时间一年半,或沉淀、或从零开始。
- PHP开发规范
- 例说C#深拷贝与浅拷贝
- Windows Phone 8本地化多语言支持
- BZOJ 3240([Noi2013]矩阵游戏-费马小定理【矩阵推论】-%*s-快速读入)
- [TWRP 2.8.4] for 小米2S/2SC 支持中英文切换
- Docker 三剑客之 Docker Swarm
- 利用 Docker 备份、迁移数据库
- 大数据分析中Redis怎么做到220万ops
- 整理C++面试题for非CS程序猿——更新至【48】
- 一个开源的强类型客户端(.NET 中的 Open Fegin)&mdash; Rabbit Go
- javaScript设计模式之----工厂模式
- UE、UI、UCD、UED?职责划分?
- 报文分析4、TCP协议的头结构
热门文章
- “CNKI 中国知网 PDF 全文下载”油猴脚本在线安装地址
- Codeforces Round #526 (Div. 2) E. The Fair Nut and Strings
- Ubuntu 12.04更新源(转)
- c++(类) this指针
- bzoj3223 文艺平衡树 codevs3303 翻转区间
- python 写 excel 模块 : xlwt
- React事件处理程序
- RabbitMQ消息队列(四): 消息路由
- 最新Python异步编程详解
- OC的UUID生成