CodeTON Round 3 (Div. 1 + Div. 2, Rated, Prizes!)(第四题容斥原理)

Dashboard - CodeTON Round 3 (Div. 1 + Div. 2, Rated, Prizes!) - Codeforces

Problem - D - Codeforces

主要考点:容斥原理: 利用容斥原理求1-n以内与m互质的数的个数。

容斥原理:

A,B,C三个集合中的相容数量 :(AUBUC) .

#define n=倒U

A(中的数量)+B(中的数量)+C(中的数量)可能会有重复。所以需要减去n(A,B) ,n(A,C),n(B,C)。可是

此时会多减去n(a,b,c)所以加上即可。

得出规律:n个集合相容的数量可以奇数个数加上然后偶数个数减去。

利用容斥原理求1-n以内与m互质的数的个数。

先把m的质数因子求出放在vector里然后利用二进制枚举用或不用这个点。例如:101则用第一个质数和第三个质数.

vectorget(int x)
{
    vectorv;
    for(int i=2;i<=x/i;i++)
    {
        if(x%i==0)
        {
            v.emplace_back(i);
            while(x%i==0) x/=i;
        }
    }
    if(x>1) v.emplace_back(x);
    return v;
}
int dfs(int x,int y)
{
    vectorv= get(y);
    int ans=0;
    for(int i=1;i<(1<<(int)v.size());i++)
    {
        int f=0;
        int sum=1;
        for(int j=0;j>j)&1)
            {
                sum=sum*v[j];
                f++;
            }
        }
        if(f&1) ans+=x/sum;
        else ans-=x/sum;
    }
    return ans;
}

你可能感兴趣的:(c++)