cf 547C 容斥原理

#include <iostream>
#include<stdio.h>
#include<algorithm>
#include<vector>
#define ll long long
using namespace std;
const int N =500000+10;
vector<int> fac[N];
int vis[N],cnt[N],v[N];
int n,q,a[N];
ll ans;
int x;
void dfs(int u,int mul,int tp,int bo)
{
    if(u==fac[x].size())
    {
        if(bo) cnt[mul]--;
        ans+=tp*cnt[mul];//当前mul的倍数有多少个
        if(!bo) cnt[mul]++;
        return ;
    }
    dfs(u + 1, mul * 1, tp, bo);
    dfs(u + 1, mul * fac[x][u], -tp, bo);
}
int main()
{
    //分解质因子
    for(int i=2;i<N;i++)
    {
        if(v[i]) continue;
        for(int j=i;j<N;j+=i)
        {
          v[j]=1;
          fac[j].push_back(i);
        }
    }
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    while(q--)
    {
        scanf("%d",&x);
        if(vis[x])
        {
            vis[x]=0;
            x=a[x];
            dfs(0,1,-1,1);
        }
        else
        {
            vis[x] = 1;
            x = a[x];
            dfs(0, 1, 1, 0);
        }
         printf("%I64d\n", ans);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值