BZOJ 4176: Lucas的数论 [杜教筛]
4176: Lucas的数论
题意:求\(\sum_{i=1}^n \sum_{j=1}^n \sigma_0(ij)\) \(n \le 10^9\)
代入\(\sigma_0(nm)=\sum_{i\mid n}\sum_{j\mid m}[(i,j)=1]\)
反演得到
g(n) = \sum_{i=1}^n \sigma_0(i)
\]
杜教筛\(\mu \ \sigma_0\)的前缀和
当然和前面的题一样,\(\sigma_0\)也可以用预处理+分块
复杂度\(O(n^{\frac{2}{3}})\)
把上题TLE的杜教筛代码抄上就行了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <ctime>
using namespace std;
typedef long long ll;
const int N=1664512, mo=1e9+7;
int U=1664510;
inline ll read(){
char c=getchar(); ll x=0,f=1;
while(c<'0' || c>'9') {if(c=='-')f=-1; c=getchar();}
while(c>='0' && c<='9') {x=x*10+c-'0'; c=getchar();}
return x*f;
}
inline void mod(int &x) {if(x>=mo) x-=mo; else if(x<0) x+=mo;}
inline void mod(ll &x) {if(x>=mo) x-=mo; else if(x<0) x+=mo;}
bool notp[N]; int p[N/10], mu[N], lp[N], si[N];
void sieve(int n) {
mu[1]=1; si[1]=1;
for(int i=2; i<=n; i++) {
if(!notp[i]) p[++p[0]] = i, mu[i] = -1, si[i] = lp[i] = 2;
for(int j=1; j <= p[0] && i*p[j] <= n; j++) {
int t = i*p[j];
notp[t] = 1;
if(i%p[j] == 0) {
mu[t] = 0;
lp[t] = lp[i] + 1;
si[t] = si[i] / lp[i] * lp[t];
break;
}
mu[t] = -mu[i];
lp[t] = 2;
si[t] = si[i] * 2;
}
mu[i] += mu[i-1];
si[i] += si[i-1];
}
}
namespace ha {
const int p = 1001001;
struct ha {
struct meow{int ne, val, r;} e[10000];
int cnt, h[p];
ha() {cnt=0; memset(h, 0, sizeof(h));}
inline void insert(int x, int val) {
int u = x%p;
for(int i=h[u];i;i=e[i].ne) if(e[i].r == x) return;
e[++cnt] = (meow){h[u], val, x}; h[u] = cnt;
}
inline int quer(int x) {
int u = x%p;
for(int i=h[u];i;i=e[i].ne) if(e[i].r == x) return e[i].val;
return -1;
}
} hs, hu;
} using ha::hs; using ha::hu;
int dj_u(int n) {
if(n <= U) return mu[n];
if(hu.quer(n) != -1) return hu.quer(n);
int ans = 1, r;
for(int i=2; i<=n; i=r+1) {
r = n/(n/i);
mod(ans -= (r-i+1) * dj_u(n/i) %mo);
}
hu.insert(n, ans);
return ans;
}
int dj_s(int n) {
if(n <= U) return si[n];
if(hs.quer(n) != -1) return hs.quer(n);
int ans = n, r, now, last = dj_u(1);
for(int i=2; i<=n; i=r+1, last=now) {
r = n/(n/i); now = dj_u(r);
mod(ans -= (ll) (now - last) * dj_s(n/i) %mo);
}
hs.insert(n, ans);
return ans;
}
int solve(int n) {
dj_u(n); dj_s(n);
int ans=0, r, last=0, now;
for(int i=1; i<=n; i=r+1, last=now) {
r = n/(n/i); now = dj_u(r); ll t = dj_s(n/i); //printf("hi [%d, %d] %d %d %lld\n", i, r, now, last, t);
mod(ans += (ll) (now - last) * t %mo * t %mo);
}
return ans;
}
int main() {
freopen("in", "r", stdin);
int n=read(); //U = pow(n, 2.0/3);
sieve(U);
printf("%d", solve(n));
}
BZOJ 4176: Lucas的数论 [杜教筛]的更多相关文章
- bzoj 4176: Lucas的数论 -- 杜教筛,莫比乌斯反演
4176: Lucas的数论 Time Limit: 30 Sec Memory Limit: 256 MB Description 去年的Lucas非常喜欢数论题,但是一年以后的Lucas却不那么 ...
- [bzoj 4176] Lucas的数论 (杜教筛 + 莫比乌斯反演)
题面 设d(x)d(x)d(x)为xxx的约数个数,给定NNN,求 ∑i=1N∑j=1Nd(ij)\sum^{N}_{i=1}\sum^{N}_{j=1} d(ij)i=1∑Nj=1∑Nd(ij) ...
- bzoj4176. Lucas的数论 杜教筛
题意:求\(\sum_{i=1}^n\sum_{j=1}^nd(ij),d是约数个数函数\) 题解:首先有一个结论\(d(ij)=\sum_{x|i}\sum_{y|j}[(i,j)==1]\) 那么 ...
- 【BZOJ4176】Lucas的数论-杜教筛
求$$\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{n}f(ij)$$,其中$f(x)$表示$x$的约数个数,$0\leq n\leq 10^9$,答案膜$10^9+ ...
- bzoj 4176 Lucas的数论
bzoj 4176 Lucas的数论 和约数个数和那题差不多.只不过那个题是多组询问,这题只询问一次,并且 \(n\) 开到了 \(10^9\). \[ \begin{align*} \sum_{i= ...
- Mobius反演与积性函数前缀和演学习笔记 BZOJ 4176 Lucas的数论 SDOI 2015 约数个数和
下文中所有讨论都在数论函数范围内开展. 数论函数指的是定义域为正整数域, 且值域为复数域的函数. 数论意义下的和式处理技巧 因子 \[ \sum_{d | n} a_d = \sum_{d | n} ...
- BZOJ 4176 Lucas的数论 莫比乌斯反演+杜教筛
题意概述:求,n<=10^9,其中d(n)表示n的约数个数. 分析: 首先想要快速计算上面的柿子就要先把d(ij)表示出来,有个神奇的结论: 证明:当且仅当a,b没有相同的质因数的时候我们统计其 ...
- bzoj 4176: Lucas的数论【莫比乌斯反演+杜教筛】
首先由这样一个结论: \[ d(ij)=\sum_{p|i}\sum_{q|j}[gcd(p,q)==1] \] 然后推反演公式: \[ \sum_{i=1}^{n}\sum_{j=1}^{n}\su ...
- ●BZOJ 4176 Lucas的数论
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4176 题解: 莫比乌斯反演,杜教筛 首先有这么一个结论: 令d(n)表示n的约数的个数(就是 ...
随机推荐
- c语言基础学习09_复合类型
=============================================================================涉及到的知识点有:一.结构体1.定义结构体st ...
- 安装JDK出现错误:-bash: /usr/java/jdk1.7.0_71/bin/java: /lib/ld-linux.so.2: bad ELF interpreter: No such file or directory解决办法
1.错误描述:安装好jdk之后,通过java -version,javac,java等命令测试是否安装成功时出现错误-bash: /usr/java/jdk1.7.0_71/bin/java: /li ...
- struts配置json需要的jar包
- maven 阿里云仓库配置
<!-- 设定主仓库,按设定顺序进行查找. --> <repositories> <repository> <id>nexus-aliyun</i ...
- excel生成sql语句
很多时候,我们想把excel中数据导入到数据库中. 方法有很多种,比如直接拷贝然后黏贴进编辑状态的框中,这种情况有个弊端,就是excel中每列的数据必须和数据库中一一对应,这个很难,基本上不太可能,数 ...
- python3 第八章 - 完善九九乘法表
前面我们在第四章的时候挖了个坑:怎么用优雅的方式来打印九九乘法表.这一章我们就来填上这个坑. 首先,我们再来看下九九乘法表是什么样子的 1 x 1 = 1 1 x 2 = 2 2 x 2 = 4 1 ...
- linu_nginx_location语法
location的作用是什么? 每个server中都需要配置location,通过location匹配域名后内容,再通过location响应同一个域名下不同请求 location语法 location ...
- Django类方式写view
问题: Django官方教程中都是通过def函数方式来写view,如何通过类方式写view以及为何要通过类方式写view? 那,如何解决这个问题? 用户访问浏览器,一般两种方式,get获取网页和pos ...
- MySQL两大存储引擎InnoDB与MyISAM
1.InnoDB存储引擎 MySQL5.5中InnoDB成为默认的存储引擎.InnoDB是事务型存储引擎,被设计用来处理大量的短期事务.它的性能和自动崩溃恢复特性,使得它在非事务场景中也很流行. 所以 ...
- 一个滑动选中RecyclerView中Item的布局SlidingCheckLayout,手指滑过Item时多项选中。
SlidingCheckLayout是一个滑动选中RecyclerView中Item的布局,手指滑过Item时多项选中. 作者:竹尘居士 github:https://github.com/homgw ...