当前位置:网站首页>[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);
}
}
边栏推荐
- Looking for b-end product manager after years? I almost ruined myself
- 【红旗杯?】补题
- socket问题记录
- CAN总线工作状况和信号质量“体检”
- 洛谷P2486 [SDOI2011]染色(树链+线段树 + 树上区间合并 )
- c#ColorDialog更改文本颜色和FontDialog更改文本字体的使用示例
- Technology blog | how to communicate using SSE
- Set the textalign property of the label control in C to control the method of text centering
- FFT【模板】
- WebSocket的理解以及应用场景
猜你喜欢

50 pieces of professional knowledge of Product Manager (IV) - from problem to ability improvement: amdgf model tool

Requirements for Power PCB circuit board design 2021-11-09

Electronics: Lesson 010 - Experiment 8: relay oscillator

c#磁盘驱动器及文件夹还有文件类的操作

Pcb|about FPC reinforcement type

电子学:第011课——实验 10:晶体管开关

Solving some interesting problems with recurrence of function

C disk drives, folders and file operations

力扣 272. 最接近的二叉搜索树值 II 递归

电子学:第009课——实验 7:研究继电器
随机推荐
Technology blog | how to communicate using SSE
@The difference between resource and @autowired annotation, why recommend @resource?
[Video] ffplay uses MJPEG format to play USB camera
Drawing of clock dial
Electronics: Lesson 010 - Experiment 8: relay oscillator
navicat定时任务无效
Looking for b-end product manager after years? I almost ruined myself
1464. maximum product of two elements in an array
Machine learning notes linear regression of time series
SCM Project Training
新版USBCAN卡CAN分析仪的CAN&CANFD综合测试分析软件LKMaster主要功能介绍
牛客:飞行路线(分层图+最短路)
Basic use of ActiveMQ in Message Oriented Middleware
CAN总线工作状况和信号质量“体检”
基于STM32MP157调试MIPI-DSI屏幕
云计算考试版本1.0
[little knowledge] PCB proofing process
27. remove elements
电子学:第009课——实验 7:研究继电器
Application of can optical transceiver of ring network redundant can/ optical fiber converter in fire alarm system