\[\begin{eqnarray*}
ans&=&\sum_{i=1}^nf(i)\\
&=&\sum_{i=1}^n\sum_{d|i}\gcd(d,\frac{i}{d})\\
&=&\sum_{i=1}^n\sum_{d|i}\sum_{k|d,k|\frac{i}{d}}\varphi(k)\\
&=&\sum_{k=1}^n\varphi(k)\sum_{k^2|i}\sigma_0(\frac{i}{k^2})\\
&=&\sum_{k=1}^n\varphi(k)\sum_{i=1}^{\lfloor\frac{n}{k^2}\rfloor}\lfloor\frac{n}{k^2i}\rfloor\\
&=&\sum_{k=1}^{\sqrt{n}}\varphi(k)S(\lfloor\frac{n}{k^2}\rfloor)
\end{eqnarray*}\]

其中

\[S(n)=\sum_{i=1}^n\lfloor\frac{n}{i}\rfloor\]

枚举所有$k$,然后分段计算$S$即可。

时间复杂度

\[\begin{eqnarray*}
T(n)&=&O(\sqrt{n}+\sum_{i=1}^{\sqrt{n}}\sqrt{\frac{n}{i^2}})\\
&=&O(\sqrt{n}\sum_{i=1}^{\sqrt{n}}\frac{1}{i})\\
&=&O(\sqrt{n}\log n)
\end{eqnarray*}\]

#include<cstdio>
typedef long long ll;
const int N=31622800;
const ll n=1000000000000000LL;
int i,j,k,tot,p[N/10],phi[N];bool v[N];ll ans;
inline ll F(ll n){
ll ret=0;
for(ll i=1,j;i<=n;i=j+1){
j=n/(n/i);
ret+=n/i*(j-i+1);
}
return ret;
}
int main(){
for(phi[1]=1,i=2;i<=n/i;i++){
if(!v[i])phi[i]=i-1,p[tot++]=i;
for(j=0;j<tot;j++){
k=i*p[j];
if(k>n/k)break;
v[k]=1;
if(i%p[j])phi[k]=phi[i]*(p[j]-1);else{
phi[k]=phi[i]*p[j];
break;
}
}
}
for(i=1;i<=n/i;i++)ans+=F(n/i/i)*phi[i];
return printf("%lld",ans),0;
}

  

PE530 : GCD of Divisors的更多相关文章

  1. 【Project Euler】530 GCD of Divisors 莫比乌斯反演

    [题目]GCD of Divisors [题意]给定f(n)=Σd|n gcd(d,n/d)的前缀和F(n),n=10^15. [算法]莫比乌斯反演 [题解]参考:任之洲数论函数.pdf 这个范围显然 ...

  2. 数论2&莫&杜

    积性函数: 积性函数定义ok 积性函数指对于所有互质的整数\(a\)和\(b\)有性质\(f(ab)=f(a)f(b)\)的数论函数 除数函数? 莫比乌斯函数\(\mu\)ok \[ \phi(i) ...

  3. project euler做题记录

    ProjectEuler_做题记录 简单记录一下. problem 441 The inverse summation of coprime couples 神仙题.考虑答案为: \[\begin{a ...

  4. [gcd]Codeforces Common Divisors

    Common Divisors time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  5. [Lyft Level 5 Challenge 2018 - Elimination Round][Codeforces 1033D. Divisors]

    题目链接:1033D - Divisors 题目大意:给定\(n\)个数\(a_i\),每个数的约数个数为3到5个,求\(\prod_{i=1}^{n}a_i\)的约数个数.其中\(1 \leq n ...

  6. codeforces#410C Mike and gcd problem

    题目:Mike and gcd problem 题意:给一个序列a1到an ,如果gcd(a1,a2,...an)≠1,给一种操作,可以使ai和ai+1分别变为(ai+ai+1)和(ai-ai+1); ...

  7. Divisors of Two Integers CodeForces - 1108B (数学+思维)

    Recently you have received two positive integer numbers xx and yy. You forgot them, but you remember ...

  8. CF 1033 D. Divisors

    D. Divisors http://codeforces.com/contest/1033/problem/D 题意: 给n个(n<=500)个数,($a_i <= 2 \times 1 ...

  9. codeforces 703E Mishka and Divisors

    codeforces 703E Mishka and Divisors 题面 给出大小为\(1000\)的数组和一个数\(k\),求长度最短的一个子序列使得子序列的元素之积是\(k\)的倍数,如果有多 ...

随机推荐

  1. Eclipse DDT

    http://www.eclipse.org/downloads/ https://github.com/DDT-IDE/DDT/blob/latest/documentation/UserGuide ...

  2. Atom编辑器的插件

    先说下atom的插件安装方法吧,因为没用过苹果电脑,所以这里就只说下windows的操作吧. " ctrl+, "调出设置面板 点击install按钮,进去搜索插件面板 1.exp ...

  3. 只用@property定义一个属性speed,子类不能直接用_speed,需要在interface的成员变量列表里写上_speed

    //写法一: @interface Person : NSObject { } @property (nonatomic, strong) NSString *name; @end @implemen ...

  4. storyboard连线容易出现的问题

    - 连接的方法代码被删掉,但是连线没有去掉 - 可能会出现方法找不到错误 - unrecognized selector sent to instance- 连接的属性代码被删掉,但是连线没有去掉 - ...

  5. undefined method `environment' for nil:NilClass when importing Bootstrap into rails

    今天做项目时往Gemfile里加了各gem, 然后bundle update了一下, 然后悲剧了,出现了undefined method `environment' for nil:NilClass ...

  6. 利用 autoconf 和 automake 生成 Makefile 文件

    一.相关概念的介绍 什么是 Makefile?怎么书写 Makefile?竟然有工具可以自动生成 Makefile?怎么生成啊?开始的时候,我有这么多疑问,所以,必须得先把基本的概念搞个清楚. 1.M ...

  7. android 项目中如何引入第三方jar包

    http://www.360doc.com/content/13/0828/08/11482448_310390794.shtml

  8. UI设计

    微软压平的瓷砖设计符合最直接的信息接受方式,但不符合人类的审美观念. 换句话说,除了平,还有艺术,人类的脑力活动从不会平的像个白板,它是九曲十八弯的脉冲波动. 人类几千年的的审美史上,从没有出现过这种 ...

  9. main(int argc, char **argv)参数解读

    main(int argc, char **argv)参数解读 编译生成了test.exe ,然后在控制台下相应的目录下输入:test  1  2  3 4 argc就是一个输入了多少个参数,包括te ...

  10. Wireshark抓包工具

    首先下载并安装Wireshark软件,最好选择中文版,因为会使你用的更顺手. 安装完毕之后,双击打开Wireshark软件,主界面还是比较清晰明了的,可是怎么用还是稀里糊涂的吧. 点击菜单栏红圈中的选 ...