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 ;
}

最新文章

  1. folly::AtomicHashmap源码分析(一)
  2. jdk研究——java.lang
  3. One to One 的数据库模型设计与NHibernate配置
  4. struts2 Result Type四个常用转跳类型
  5. 20145304 《Java程序设计》课程总结
  6. 【BZOJ1208】[HNOI2004]宠物收养所 Splay
  7. 【WEB前端经验之谈】时间一年半,或沉淀、或从零开始。
  8. PHP开发规范
  9. 例说C#深拷贝与浅拷贝
  10. Windows Phone 8本地化多语言支持
  11. BZOJ 3240([Noi2013]矩阵游戏-费马小定理【矩阵推论】-%*s-快速读入)
  12. [TWRP 2.8.4] for 小米2S/2SC 支持中英文切换
  13. Docker 三剑客之 Docker Swarm
  14. 利用 Docker 备份、迁移数据库
  15. 大数据分析中Redis怎么做到220万ops
  16. 整理C++面试题for非CS程序猿——更新至【48】
  17. 一个开源的强类型客户端(.NET 中的 Open Fegin)&mdash; Rabbit Go
  18. javaScript设计模式之----工厂模式
  19. UE、UI、UCD、UED?职责划分?
  20. 报文分析4、TCP协议的头结构

热门文章

  1. “CNKI 中国知网 PDF 全文下载”油猴脚本在线安装地址
  2. Codeforces Round #526 (Div. 2) E. The Fair Nut and Strings
  3. Ubuntu 12.04更新源(转)
  4. c++(类) this指针
  5. bzoj3223 文艺平衡树 codevs3303 翻转区间
  6. python 写 excel 模块 : xlwt
  7. React事件处理程序
  8. RabbitMQ消息队列(四): 消息路由
  9. 最新Python异步编程详解
  10. OC的UUID生成