当前位置:网站首页>(codeforce547)C-Mike and Foam(质因子+容斥原理)
(codeforce547)C-Mike and Foam(质因子+容斥原理)
2022-07-29 03:28:00 【AC__dream】
题目链接:Problem - 547C - Codeforces
题目:
样例输入:
5 6
1 2 3 4 6
1
2
3
4
5
1
样例输出:
0
1
3
5
6
2
题意:一个酒吧,有n种啤酒,从1到n。第i瓶啤酒上面有ai毫升的泡沫。
有q个查询。每次查询给出一个编号,当编号为X的啤酒已经在货架上,那么就将其从货架上取下来,否则就应该把它放到货架上。每次询问后,应当输出架子的分数。货架的分数是满足i<j且gcd(ai,aj)=1的数对(i,j)的个数。
分析:这道题如果暴力求解的话复杂度就是O(n*q),肯定是不能过的。比如现在已经有m种啤酒放在了货架上,此时货架的分数是x,那么我们再放入货架上一瓶啤酒,那么答案会比x多一个增量delt,我们就是要维护这种增量,这样放完啤酒后的分数就是x+delt了。同理,如果从货架上取下一瓶啤酒那么原理是一样的。
两瓶啤酒什么情况下才算是互质的呢?肯定是两者不含有公共的质因子,看一下数据范围发现a数组的范围是5e5,那么由于2*3*5*7*11*13*17=510510>5e5,所以每个数最多含有6个质因子,那么我们就可以对其质因子进行容斥定理,我们用一个num[s]记录质因子状态为s的数的个数,那么我们设A(p1,……,pk)表示含有质因子p1,……,pk,那么由于都是质数,我们可以用乘积来维护,也就是用num[s]记录质因子状态为s的数的个数。由于加入一个数和删除一个数基本上是类似的,下面我以加入一个数为例进行分析增量计算方法:
假如新加入的数为x,我们对x进行质因子分解得到p1,p2,……,pk,那么我们的增量就是
,当计算完状态s的贡献后不要忘记将状态s的数量num[s]加一。
删除一个数与添加一个数唯一的不同就是,删除一个数是先将状态s的数量减1再计算该状态的贡献,而添加一个数是先算贡献再将状态s的数量加1。
由于a[]大小是小于5e5的,所以每个数的质因子最多有7个,那么对于每次询问容斥原理复杂度最多就是C(7,0)+C(7,1)+C(7,2)+C(7,3)+C(7,4)+C(7,5)+C(7,6)+C(7,7)=2^7,所以复杂度是肯定没问题的。
最后分享一个我在做题时遇到的一个低级bug,但是却很难发现错误所在,由于对于每个询问是给出的酒的编号t,所以可以直接用vis[t]标记酒是否在货架上,而我是直接用vis[a[t]]表示泡沫为a[t]的酒是否在货架上,由于可能会有两瓶编号不一样的酒的泡沫是一样的,所以就会出现问题。
下面是代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
#include<queue>
#include<vector>
#include<cmath>
using namespace std;
const int N=1e6+10;
int num[N];//num[st]记录当前架子上的酒包含质因子状态为st的数的个数
bool vis[N];
long long a[N],p[N];
long long ans;
long long add(long long x)
{
//预处理x的质因子
int tt=0;
long long ans=0;
for(int i=2;i*i<=x;i++)
{
if(x%i==0)
{
p[tt++]=i;
while(x%i==0) x/=i;
}
}
if(x!=1) p[tt++]=x;
tt--;
for(int s=0;s<1<<(tt+1);s++)
{
long long f=1,t=1;
for(int i=0;i<=tt;i++)
{
if(s>>i&1)
{
f*=-1;
t*=p[i];
}
}
if(t==1) t=0;//t==1说明没有更新,也就是状态为0
ans+=f*num[t];
num[t]++;
}
return ans;
}
long long sub(long long x)
{
//预处理x的质因子
int tt=0;
long long ans=0;
for(int i=2;i*i<=x;i++)
{
if(x%i==0)
{
p[tt++]=i;
while(x%i==0) x/=i;
}
}
if(x!=1) p[tt++]=x;
tt--;
for(int s=0;s<1<<(tt+1);s++)
{
long long f=1,t=1;
for(int i=0;i<=tt;i++)
{
if(s>>i&1)
{
f*=-1;
t*=p[i];
}
}
if(t==1) t=0;//t==1说明没有更新,也就是状态为0
num[t]--;
ans+=f*num[t];
}
return ans;
}
int main()
{
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++)
scanf("%lld",&a[i]);
while(q--)
{
long long t;
scanf("%lld",&t);
if(vis[t])
{
ans-=sub(a[t]);
vis[t]=false;
}
else
{
ans+=add(a[t]);
vis[t]=true;
}
printf("%lld\n",ans);
}
return 0;
}边栏推荐
- C language programming | exchange binary odd and even bits (macro Implementation)
- July 28, 2022 Gu Yujia's study notes
- 军品研制过程-转阶段
- Use of leak scanning (vulnerability scanning) tool burpsuite or burp Suite (with installation and installation package download of burpsuite+1.7.26)
- Military product development process - transition phase
- Leetcode 1331 array sequence number conversion [map] the leetcode path of heroding
- The difference between int and integer. Is int or integer used in practical applications?
- three.js 第五十四用如何给shader传递结构体数组
- MYCAT read / write separation configuration
- Pp-yoloe details
猜你喜欢

Watermelon book learning Chapter 6 -- SVM
![MOS管 —— 快速复苏应用笔记(贰)[参数与应用]](/img/54/eb040a51304192def8cfb360c7c213.png)
MOS管 —— 快速复苏应用笔记(贰)[参数与应用]

今晚7:30 | 连界、将门、百度、碧桂园创投四位大佬眼中的AI世界,是继续高深还是回归商业本质?...

Configure vscade to realize ROS writing

MySQL installation and configuration super detailed tutorial and simple database and table building method

July 28, 2022 Gu Yujia's study notes
![LeetCode 1331 数组序号转换[Map] HERODING的LeetCode之路](/img/be/d429d0c437dc5ed7cb4448e223a83a.png)
LeetCode 1331 数组序号转换[Map] HERODING的LeetCode之路

接口自动化测试实践指导(上):接口自动化需要做哪些准备工作

Self study notes on Apache file management -- mapping folders and configuring Apache virtual machines based on single IP and multi domain names

Bingbing learning notes: operator overloading -- implementation of date class
随机推荐
Does domestic ERP have a chance to beat sap?
three.js 第五十四用如何给shader传递结构体数组
How to solve the time zone problem in MySQL timestamp
深入C语言(1)——操作符与表达式
Pp-yoloe details
Design of smoke temperature, humidity and formaldehyde monitoring based on single chip microcomputer
AI platform, AI midrange architecture
Summary of SAP localized content in China
Alibaba Sentinel - workflow and principle analysis
How dare you write a resume that is proficient in concurrent programming? Why do you use a two-way linked list in AQS?
力扣刷题之分数加减运算(每日一题7/27)
Example analysis of while, repeat and loop loops in MySQL process control
2 neural network toolbox NN
[freeswitch development practice] unimrcp compilation and installation
Let's talk about the summary of single merchant function modules
i. MX 8m plus integrated dedicated neural processing engine (NPU)
Principle knowledge is useful
Leetcode 1331 array sequence number conversion [map] the leetcode path of heroding
Sanzi chess (player + computer)
Code speed optimization