当前位置:网站首页>【莫比乌斯反演】
【莫比乌斯反演】
2022-06-25 06:43:00 【mfy的1号小迷弟】
A.洛谷P2522 [HAOI2011]Problem b
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=50000+5;
int mu[maxn],prime[maxn],cnt,vis[maxn],n,m,f[maxn],k,a,b,c,d;
void init(){
//线性筛莫比乌斯函数
f[1]=mu[1]=1;
for(int i=2;i<=maxn;i++){
if(vis[i]==0){
prime[++cnt]=i;
mu[i]=-1;
}
for(int j=1;j<=cnt;j++){
if(i*prime[j]>maxn)break;
vis[i*prime[j]]=1;
mu[i*prime[j]]=i%prime[j]?-mu[i]:0;
if(i%prime[j]==0)break;
}
f[i]=f[i-1]+mu[i];
}
}
int getsum(int x,int y){
int ans=0;
for(int l=1,r;l<=min(x,y);l=r+1){
r=min(x/(x/l),y/(y/l));
ans+=(f[r]-f[l-1])*(x/l)*(y/l);
}
return ans;
}
int main(){
init();
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
printf("%d\n",getsum(b/k,d/k)-getsum((a-1)/k,d/k)-getsum((c-1)/k,b/k)+getsum((a-1)/k,(c-1)/k));
}
}
B.洛谷P3327 [SDOI2015]约数个数和(预处理每个数的因子个数)
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=20101009;
const int maxn=1e7+5;
int mu[maxn],prime[maxn],cnt,vis[maxn],n,m,k,t[maxn],d[maxn];
ll he[maxn],f[maxn];
void init(int k) {
//mu[]:莫比乌斯,f[]:mu[]前缀和,d[]:每个数的因子个数,he[]:d[]的前缀和,t[]:每个数最小的质因子
vis[0]=vis[1]=mu[1]=d[1]=1;
for(int i=2;i<=k;i++) {
if (!vis[i])prime[++cnt]=i,mu[i]=-1,d[i]=2,t[i]=1;
for(int j=1;j<=cnt&&i*prime[j]<=k;j++) {
vis[i*prime[j]]=1;
if(i%prime[j]==0){
mu[i*prime[j]]=0;
d[i*prime[j]]=d[i]/(t[i]+1)*(t[i]+2);
t[i*prime[j]]=t[i]+1;
break;
}
else mu[i*prime[j]]=-mu[i],d[i*prime[j]]=d[i]<<1,t[i*prime[j]]=1;
}
}
for(int i=1;i<=k;i++)f[i]=f[i-1]+mu[i],he[i]=he[i-1]+d[i];
}
int main(){
init(50000);
int T;
scanf("%d",&T);
while(T--){
ll ans=0;
scanf("%d%d",&n,&m);
if(n>m)swap(n,m);
for(int l=1,r;l<=n;l=r+1){
r=min(n/(n/l),m/(m/l));
ans+=1ll*(f[r]-f[l-1])*he[n/l]*he[m/l];
}
printf("%lld\n",ans);
}
}
边栏推荐
- Mining microbial dark matter -- a new idea
- Machine learning notes linear regression of time series
- Misunderstanding of switching triode
- Lebel only wants an asterisk in front of it, but doesn't want to verify it
- 年后求职找B端产品经理?差点把自己坑惨了......
- Force deduction 76 questions, minimum covering string
- [Video] ffplay uses MJPEG format to play USB camera
- 使用Adobe Acrobat Pro调整PDF页面为统一大小
- CAN透传云网关CANIOT,CANDTU记录CAN报文远程收发CAN数据
- How to use ad wiring for PCB design?
猜你喜欢

Hisilicon 3559 sample parsing: Vio

Share the process requirements for single-layer flexible circuit board

ts环境搭建

搞清信息化是什么,让企业转型升级走上正确的道路

test

TCP与UDP

基于STM32MP157调试MIPI-DSI屏幕

OpenCV每日函数 结构分析和形状描述符(8) fitLine函数 拟合直线

微信小程序开通客服消息功能开发

Three Siemens fire-fighting hosts fc18 are equipped with can optical transceiver for optical fiber redundant ring network networking test
随机推荐
Technology blog | how to communicate using SSE
MySQL简单权限管理
Advantages and differences of three kinds of vias in PCB 2021-10-27
TCP与UDP
基于STM32MP157调试MIPI-DSI屏幕
Anaconda navigator启动慢的一个解决方法
【视频】ffplay 使用mjpeg格式播放usb摄像头
权限、认证系统相关名词概念
机器学习笔记 - 时间序列的线性回归
产品经理专业知识50篇(四)-从问题到能力提升:AMDGF模型工具
How to select lead-free and lead-free tin spraying for PCB? 2021-11-16
Use the frame statistics function of the message and waveform recording analyzer royalscope to troubleshoot the accidental faults of the CAN bus
How to use printf of 51 single chip microcomputer
Tips 𞓜 how to clean PCB boards 2021-10-22
ffmpeg+SDL2实现音频播放
27. 移除元素
MySQL simple permission management
力扣78:子集
Linux上oracle和mysql的启动,关闭,重启
用函数的递归来解决几道有趣的题