当前位置:网站首页>【莫比乌斯反演】
【莫比乌斯反演】
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);
}
}
边栏推荐
- Application of can optical transceiver of ring network redundant can/ optical fiber converter in fire alarm system
- Invalid Navicat scheduled task
- Analysis and utilization of Microsoft Office Word remote command execution vulnerability (cve-2022-30190)
- Ubuntu18下登录mysql 5.7设置root密码
- 判断用户是否是第一次进入某个页面
- Introduction to the main functions of the can & canfd comprehensive test and analysis software lkmaster of the new usbcan card can analyzer
- 2160. minimum sum of the last four digits after splitting
- 取消word文档中某些页面的页眉
- 新版USBCAN卡CAN分析仪的CAN&CANFD综合测试分析软件LKMaster主要功能介绍
- [daily training] 207 Class Schedule Card
猜你喜欢

Mining microbial dark matter -- a new idea

Importer des données dans MATLAB

navicat定时任务无效

Technology blog | how to communicate using SSE

The fourth floor is originally the fourth floor. Let's have a look

STL tutorial 4- input / output stream and object serialization
![[little knowledge] PCB proofing process](/img/bf/f66677294a14baf08cc35d1e8c1e31.jpg)
[little knowledge] PCB proofing process

Machine learning notes linear regression of time series

Invalid Navicat scheduled task

Five causes of PCB board deformation and six solutions 2021-10-08
随机推荐
WinForm实现窗口始终在顶层
一文了解 | 革兰氏阳性和阴性菌区别,致病差异,针对用药
PCB board design - automatic layout 2021-10-15
JDBC-DAO层实现
Application of can optical transceiver of ring network redundant can/ optical fiber converter in fire alarm system
bat启动.NET Core
50. pow (x, n) - fast power
Atlas conference vulnerability analysis collection
2265. 统计值等于子树平均值的节点数
Bicubic difference
Kinsing双平台挖矿家族病毒分析
LeetCode_哈希表_中等_454.四数相加 II
NSIS silent installation vs2013 runtime
差点被这波Handler 面试连环炮带走~
Mysql面试-执行sql响应比较慢,排查思路。
@The difference between resource and @autowired annotation, why recommend @resource?
Analysis of kinsing dual platform mining family virus
产品经理专业知识50篇(四)-从问题到能力提升:AMDGF模型工具
navicat定时任务无效
How to use printf of 51 single chip microcomputer