Codeforces Round #305 (Div. 1)C. Mike and Foam(素数+容斥)

题目链接:点这里!!!


题意:

输入n(n<=2e5),q(q<=2e5)。紧接着输入n个数a1~an(ai<=5e5)。给定一个空集合。

针对每个q,输入x,如果a[x]在不在集合里面,将a[x]插入进去,并且求当前该集合中有多少对互质的数;

如果a[x]在集合里面,将a[x]删除,并且求当前该集合有多少对互质的数。


题解:

1、 将所有数的素因子求出来。(最多6个)


2、如果将a[x]插入进去,我们检查由a[x]的素因子能组成的数有哪些,全部+1。

      比如a[x]=12,我们将2、3、6全部+1。


3、我们再利用容斥原理可以求出有多少数与x不互质。


4、删除是逆过程,我们还要注意a[x]=1的情况单独处理就可以了。


代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define PI 2*asin(1.0)
#define LL long long
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]C:\Program Files\Git\bin
const LL  MOD = 1E9+7;
const LL N = 5e5+15;
const int maxn = 5e5+15;
const int letter = 130;
const LL INF = 1e18;
const double pi=acos(-1.0);
const double eps=1e-10;
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int n,q,a[maxn],ps[maxn];
int dis[N],vis[N],prime[50000],cnt=0;
vector<int>G[maxn];
inline void init(){
    vis[1]=1;
    for(int i=2;i<N;i++){
        if(!vis[i])prime[++cnt]=i;
        for(int j=1;j<=cnt&&prime[j]*i<N;j++){
            vis[i*prime[j]]=1;
            if(i%prime[j]==0)break;
        }
    }
}
int main(){
    int x;
    init();
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++){
        scanf("%d",&x);
        ps[i]=x;
        if(x==1){
            G[i].pb(1);
            continue;
        }
        for(int j=1;prime[j]*prime[j]<=x;j++){
            if(x%prime[j]==0){
                G[i].pb(prime[j]);
                while(x%prime[j]==0) x/=prime[j];
            }
        }
        if(x!=1) G[i].pb(x);
    }
    LL sum=0,ans=1,pp;
    int cc,cnt=0;
    while(q--){
        scanf("%d",&x);
        if(ps[x]==1){
            if(!a[x]) sum+=1ll*cnt,cnt++;
            if(a[x]) sum-=1ll*(cnt-1),cnt--;
        }
        else if(!a[x]){
            int p=G[x].size();
            pp=0;
            for(int i=1;i<(1<<p);i++){
                ans=1,cc=0;
                for(int j=0;j<p;j++){
                    if(i&(1<<j)){
                        ans*=G[x][j];
                        cc++;
                    }
                }
                if(cc&1) pp+=dis[ans];
                else pp-=dis[ans];
                dis[ans]++;
            }
            sum+=1ll*(cnt-pp);
            cnt++;
        }
        else {
            int p=G[x].size();
            pp=0;
            for(int i=1;i<(1<<p);i++){
                ans=1,cc=0;
                for(int j=0;j<p;j++){
                    if(i&(1<<j)){
                        ans*=G[x][j];
                        cc++;
                    }
                }
                if(cc&1) pp+=1ll*dis[ans];
                else pp-=1ll*dis[ans];
                dis[ans]--;
            }
            sum-=1ll*(cnt-pp);
            cnt--;
        }
        a[x]=!a[x];
        printf("%I64d\n",sum);
    }
    return 0;
}
/*
*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值