http://www.51nod.com/onlineJudge/questionCode.html#!problemId=2026

参考及推导:https://www.cnblogs.com/ivorysi/p/9157781.html

(其公式有一处小问题,请注意。)

然后就没了……我觉得讲得挺详细了。

另外map跑得可能比哈希表还快可还行。

#include<map>
#include<cmath>
#include<stack>
#include<queue>
#include<cstdio>
#include<cctype>
#include<vector>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int p=1e9+;
const int N=;
const int M=2e6+;
const int MOD=;
const int INV=;
inline int read(){
int X=,w=;char ch=;
while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
while(isdigit(ch))X=(X<<)+(X<<)+(ch^),ch=getchar();
return w?-X:X;
}
struct node{
int to,nxt,w;
}e[M];
bool he[N+];
int su[N+],tot,cnt,head[MOD+],mu[N+],sum[N+];
inline void add(int v,int w){
int u=v%MOD;
e[++cnt].to=v;e[cnt].w=w;e[cnt].nxt=head[u];head[u]=cnt;
}
inline int query(int v){
int u=v%MOD;
for(int i=head[u];i;i=e[i].nxt)
if(v==e[i].to)return e[i].w;
return -;
}
inline int sub(int a,int b){
a-=b;if(a<)a+=p;if(a>=p)a-=p;return a;
}
inline int inc(int a,int b){
a+=b;if(a<)a+=p;if(a>=p)a-=p;return a;
}
inline int AP(int l,int r){
return (ll)inc(l,r)*sub(r+,l)%p*INV%p;
}
void Euler(int n){
mu[]=;
for(int i=;i<=n;i++){
if(!he[i]){
su[++tot]=i;mu[i]=-;
}
for(int j=;j<=tot&&i*su[j]<=n;j++){
int pri=su[j];he[i*pri]=;
if(i%pri==){
mu[i*pri]=;break;
}else mu[i*pri]=mu[i]*mu[pri];
}
}
for(int i=;i<=n;i++)sum[i]=inc(sum[i-],mu[i]*i);
}
int S(int n){
if(n<=N)return sum[n];
int tmp=query(n);
if(tmp!=-)return tmp;
int ans=;
for(int i=,j;i<=n;i=j+){
j=n/(n/i);
ans=inc(ans,(ll)AP(i,j)*S(n/i)%p);
}
ans=sub(,ans);
add(n,ans);
return ans;
}
int main(){
Euler(N);
int n=read(),ans=;
for(int i=,j;i<=n;i=j+){
j=n/(n/i);
ans=inc(ans,(ll)sub(S(j),S(i-))*(n/i)%p);
}
printf("%lld\n",(ll)ans*ans%p);
return ;
}

+++++++++++++++++++++++++++++++++++++++++++

+本文作者:luyouqi233。               +

+欢迎访问我的博客:http://www.cnblogs.com/luyouqi233/+

+++++++++++++++++++++++++++++++++++++++++++

51NOD 2026:Gcd and Lcm——题解的更多相关文章

  1. 51Nod 2026 Gcd and Lcm

    题目传送门 分析: 开始玩一个小小的trick 我们发现\(f(n)=\sum_{d|n}\mu(d)\cdot d\)是一个积性函数 所以: \(~~~~f(n)=\prod f(p_i^{a_i} ...

  2. 【51nod】2026 Gcd and Lcm

    题解 话说LOJ说我今天宜学数论= =看到小迪学了杜教筛去蹭了一波小迪做的题 标解的杜教筛的函数不懂啊,怎么推的毫无思路= = 所以写了个复杂度稍微高一点的?? 首先,我们发现f是个积性函数,那么我们 ...

  3. 51nod 1575 Gcd and Lcm

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1575 万年巨坑终于填掉了…… 首先是煞笔西瓜的做题历程O_O. ...

  4. hdu 4497 GCD and LCM 数学

    GCD and LCM Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4 ...

  5. HDU 4497 GCD and LCM(数论+容斥原理)

    GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  6. G - GCD and LCM 杭电

    Given two positive integers G and L, could you tell me how many solutions of (x, y, z) there are, sa ...

  7. hdu 4497 GCD and LCM (非原创)

    GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total ...

  8. HDOJ 4497 GCD and LCM

    组合数学 GCD and LCM Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) ...

  9. GCD 与 LCM UVA - 11388

    题目链接: https://cn.vjudge.net/problem/23709/origin 本题其实有坑 数据大小太大, 2的32次方,故而一定是取巧的算法,暴力不可能过的 思路是最大公因数的倍 ...

随机推荐

  1. WSL跑linux服务程序

    前段时间折腾了一次WSL下的Apache,无奈遇到各种奇葩问题,总是解决不了,最终放弃,甚至得出了一个现在看来比较可笑的结论:WSL是不可能跑Linux服务程序的! 当时的思路想歪了,由于Apache ...

  2. 【廖雪峰老师python教程】——IO编程

    同步IO 异步IO 最常见的IO——读写文件 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求操作系统打开一 ...

  3. JSP学习(JavaBean)

    Java Web学习 一.搭建java web开发环境: (1)安装jdk (2)安装Tomcat服务器(Apache的开源项目),安装Tomcat并设置环境变量 (3)安装EclipseEE(或者M ...

  4. Unity初探—SpaceShoot

    Unity初探—SpaceShoot DestroyByBoundary脚本(C#) 在游戏中我们添加了一个Cube正方体,让他来作为游戏的边界.它是可以触发触发事件的(勾选Is Trigger),当 ...

  5. SQL学习(时间,存储过程,触发器)

    SQL学习 几个操作时间的函数 --datapart 获取时间中的年月日时分秒等部分 select DATEPART(year,current_timestamp); select DATEPART( ...

  6. Siki_Unity_2-10_数据结构与算法

    Unity 2-10 数据结构与算法 任务1-1:数据结构简介 数据结构:数据存储的结构,数据之间的关系 数据结构分类: 集合:同属于一个集合 线性结构:数据元素存在一对一的关系 树形结构:数据元素存 ...

  7. C#使用EF连接PGSql数据库

    前言 由于项目需要,使用到了PGSql数据库,说实话这是第一次接触并且听说PGSql(PostgreSQL)关系型数据库,之前一直使用的都是SqlServer,一头雾水的各种找资源,终于将PGSql与 ...

  8. Java学习个人备忘录之面向对象概念

    对象,其实就是该类事物实实在在存在的个体. 类与对象之间的关系?类:一类事物的描述.对象:该类事物的实例.在java中通过new来创建的.举例来说,类就是汽车说明书,类只能在理论上造一辆汽车,并且这个 ...

  9. TensorFlow源码框架 杂记

    一.为什么我们需要使用线程池技术(ThreadPool) 线程:采用“即时创建,即时销毁”策略,即接受请求后,创建一个新的线程,执行任务,完毕后,线程退出: 线程池:应用软件启动后,立即创建一定数量的 ...

  10. 爬取CVPR 2018过程中遇到的坑

    爬取 CVPR 2018 过程中遇到的坑 使用语言及模块 语言: Python 3.6.6 模块: re requests lxml bs4 过程 一开始都挺顺利的,先获取到所有文章的链接再逐个爬取获 ...