1138 - 震惊,99%+的中国人都会算错的问题

题意

给你k个数问你在1,n里面恰好是奇数个数的倍数的数有多少个

题解

理解了一会容斥原理啊
然后这个就是一个容斥原理的裸题了
但是我觉得一般这种容斥系数都不好看出来。。
但是你可以列一个方程,高斯消元找一波规律就可以了
这题的容斥系数是
(1)i2(i1) ( − 1 ) i ∗ 2 ( i − 1 )
CODE:

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
typedef long long LL;
const LL K=20;
LL a[K];
LL ans;
LL n,k;
LL gcd (LL x,LL y)
{
    if (x==0) return y;
    return gcd(y%x,x);
}
void dfs (LL x,LL y,LL z)//当前到第几个数了   乘积是多少了   选了多少个了 
{
    if (y>n) return ;
    if (x>k)
    {
        if (z==0) return ;
        LL t;
        if (z&1) t=1;
        else t=-1;
        t=t*(1<<z-1);
        //printf("%lld %lld %lld\n",y,z,t);
        ans=ans+(n/y)*t;
        return ;
    }
    dfs(x+1,y,z);
    dfs(x+1,y*a[x]/gcd(y,a[x]),z+1);
}
int main()
{
    LL T;
    scanf("%lld",&T);
    while (T--)
    {
        ans=0;
        scanf("%lld%lld",&n,&k);
        for (LL u=1;u<=k;u++)   scanf("%lld",&a[u]);
        dfs(1,1,0);
        printf("%lld\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值