当前位置:网站首页>[Mobius inversion]
[Mobius inversion]
2022-06-25 08:03:00 【Mfy's little brother 1】
A. Luogu 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(){
// Linear sieve Mobius function
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. Luogu P3327 [SDOI2015] About a number and ( The number of factors for each number of preprocessing )
#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[]: Mobius ,f[]:mu[] The prefix and ,d[]: Number of factors for each number ,he[]:d[] And ,t[]: The prime factor with the smallest number
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);
}
}
边栏推荐
- MySQL简单权限管理
- Use the frame statistics function of the message and waveform recording analyzer royalscope to troubleshoot the accidental faults of the CAN bus
- 牛客:飞行路线(分层图+最短路)
- @The difference between resource and @autowired annotation, why recommend @resource?
- Force deduction 76 questions, minimum covering string
- [Video] ffplay uses MJPEG format to play USB camera
- c#ColorDialog更改文本颜色和FontDialog更改文本字体的使用示例
- 50. pow (x, n) - fast power
- Force deduction 76 questions, minimum covering string
- Determine whether the user is entering a page for the first time
猜你喜欢
c#磁盘驱动器及文件夹还有文件类的操作
力扣 272. 最接近的二叉搜索树值 II 递归
飞机引气系统的建模与故障仿真
50 pieces of professional knowledge of Product Manager (IV) - from problem to ability improvement: amdgf model tool
挖掘微生物暗物质——新思路
Import data into Matlab
What are the problems with traditional IO? Why is zero copy introduced?
FM信号、调制信号和载波
C#中如何调整图像大小
Pcb|about FPC reinforcement type
随机推荐
Knowledge sharing 𞓜 conventional laminated structure of six layer PCB
新版USBCAN卡CAN分析仪的CAN&CANFD综合测试分析软件LKMaster主要功能介绍
Authority design of SaaS system based on RBAC
Electronics: Lesson 010 - Experiment 9: time and capacitors
ffmpeg+SDL2实现音频播放
C control refresh
Set the textalign property of the label control in C to control the method of text centering
Tips on how to design soft and hard composite boards ~ 22021/11/22
洛谷P5994 [PA2014]Kuglarz(异或思维+MST)
Matlab code format one click beautification artifact
【补题】2021牛客暑期多校训练营6-n
WebSocket的理解以及应用场景
不怕百战失利,就怕灰心丧气
年后求职找B端产品经理?差点把自己坑惨了......
Looking for b-end product manager after years? I almost ruined myself
Startup, shutdown and restart of Oracle and MySQL on Linux
What are the problems with traditional IO? Why is zero copy introduced?
Six causes of PCB disconnection 2021-10-20
PH neutralization process modeling
飞机引气系统的建模与故障仿真