当前位置:网站首页>(codeforce547) c-mike and foam
(codeforce547) c-mike and foam
2022-07-29 03:32:00 【AC__ dream】
Topic link :Problem - 547C - Codeforces
subject :
The sample input :
5 6
1 2 3 4 6
1
2
3
4
5
1
Sample output :
0
1
3
5
6
2
The question : A bar , Yes n Grow beer , from 1 To n. The first i A bottle of beer has ai Ml of foam .
Yes q A query . Each query is given a number , When the number is X Our beer is already on the shelf , Then take it off the shelf , Otherwise, you should put it on the shelf . After each inquiry , The score of the shelf should be output . The score of the shelf is to meet i<j And gcd(ai,aj)=1 The number of (i,j) The number of .
analysis : If this problem is solved violently, the complexity is O(n*q), It must be impossible . For example, there are already m Grow beer and put it on the shelf , At this time, the score of the shelf is x, Then we put a bottle of beer on the shelf , Then the answer will be better than x One more increment delt, We just want to maintain this increment , So the score after putting in the beer is x+delt 了 . Empathy , If you take a bottle of beer from the shelf, the principle is the same .
Under what circumstances are two bottles of beer mutually qualitative ? It must be that the two do not contain common qualitative factors , Look at the data range and find a The range of the array is 5e5, So because of 2*3*5*7*11*13*17=510510>5e5, So each number contains at most 6 A quality factor , Then we can Carry out the inclusion exclusion theorem for its prime factor , We use one num[s] Record the quality factor status as s The number of , Then we set A(p1,……,pk) Indicates that there is a qualitative factor p1,……,pk, Then because they are prime numbers , We can use product to maintain , Also is to use num[s] Record the quality factor status as s The number of . Because adding a number and deleting a number are basically similar , Next, I will add a number as an example to analyze the incremental calculation method :
If the number of new additions is x, We are right. x We can get p1,p2,……,pk, Then our increment is , When the status is calculated s Don't forget to put the state after your contribution s The number of num[s] Add one .
The only difference between deleting a number and adding a number is , To delete a number, first change the status s The quantity of 1 Then calculate the contribution of this state , Adding a number is to calculate the contribution first and then the status s The number of add 1.
because a[] The size is less than 5e5 Of , So the prime factor of each number has at most 7 individual , So for each query, the complexity of the inclusion exclusion principle is at most 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, So the complexity is definitely no problem .
Finally, I want to share a low-level problem I encountered when I was doing questions bug, But it's hard to find the mistake , Because for each inquiry is given the wine number t, So you can use it directly vis[t] Mark whether the wine is on the shelf , And I use it directly vis[a[t]] Indicates that the foam is a[t] Is your wine on the shelf , Since there may be two bottles of wine with different numbers, the foam is the same , So there's a problem .
Here is the code :
#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] Record the quality factor status of the wine on the current shelf as st The number of
bool vis[N];
long long a[N],p[N];
long long ans;
long long add(long long x)
{
// Preprocessing x The quality factor of
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 Description not updated , That is, the status is 0
ans+=f*num[t];
num[t]++;
}
return ans;
}
long long sub(long long x)
{
// Preprocessing x The quality factor of
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 Description not updated , That is, the status is 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;
}
边栏推荐
- Ten thousand words detailed Google play online application standard package format AAB
- Simple understanding of Poe and UPS Technology
- AI_ Drug: VAE of molecular generation model (I)
- Matlab learning -- structured programs and user-defined functions
- What is eplato cast by Plato farm on elephant swap? Why is there a high premium?
- Learn exkmp again (exkmp template)
- Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
- 腾讯云使用pem登录
- How to deploy sentinel cluster of redis
- Pp-yoloe details
猜你喜欢
A case of gradually analyzing the splitting of classes -- colorful ball collisions
for_each用法示例
Various minor problems of jupyter notebook, configuration environment, code completion, remote connection, etc
Flask creation process day05-06 creation project
Implement Lmax disruptor queue from scratch (VI) analysis of the principle of disruptor solving pseudo sharing and consumers' elegant stopping
Understanding of p-type problems, NP problems, NPC problems, and NP hard problems in natural computing
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
Practical application cases of digital Twins - smart energy
Learn more than 4000 words, understand the problem of this pointing in JS, and handwrite to realize call, apply and bind
Exness: dove resolution helped gold rebound, and the focus turned to U.S. GDP
随机推荐
【C】 Array
How close can QA be to business code QA conducts testability transformation on business code
Target detection, industrial defects, image segmentation -- deep learning data set induction
Division of data link layer, protocols used in data link layer and detailed introduction
Introduction to static routing and dynamic routing protocols OSPF and rip and static routing configuration commands
Summary of basic knowledge points of C language
深入C语言(2)——结构的定义与使用
Machine learning based on deepchem
Simple code implementation of K-means clustering
How to realize shortcut keys for interface scaling by vscade
A simple and general method to obtain the size of function stack space
Learn more than 4000 words, understand the problem of this pointing in JS, and handwrite to realize call, apply and bind
后缀自动机(sam)板子 from jly
mysql的timestamp存在的时区问题怎么解决
KNN method predicts pregnancy, KNN principle simple code
Makefile details
Functions and comparison of repeaters, hubs, bridges, switches and routers
深入C语言(3)—— C的输入输出流
Learn exkmp again (exkmp template)
Tencent cloud logs in with PEM