CF 803 F Coprime Subsequences (容斥原理)

Description

给定一个序列,求有多少个子串(可以不连续)满足所有数的 gcd 为1

Solution

ansi gcd i 的子串个数,fi为数列中是 i 的倍数的数,则gcd i 的倍数的子序列个数显然是2f(i)11,则:

ansi=d=1niμ(d)×(2f(id)1)

发现 ansi 还可以表示为:
ansi=2f(i)1d=2niansid

答案为 ans1

Code

#include<bits/stdc++.h>

using namespace std;

#define For(i , j , k) for (register int i = (j) , i##_end_ = (k) ; i <= i##_end_ ; ++ i)
#define Fordown(i , j , k) for (register int i = (j) , i##_end_ = (k) ; i >= i##_end_ ; -- i)
#define Set(a , b) memset(a , b , sizeof(a))
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
typedef long long LL;

template <typename T> inline bool chkmax(T &a , T b) { return a < b ? (a = b , 1) : 0; }
template <typename T> inline bool chkmin(T &a , T b) { return b < a ? (a = b , 1) : 0; }

int _ , __;
char c_;
inline int read()
{
    for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-') __ = -1;
    for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

inline void File()
{
#ifndef ONLINE_JUDGE
    freopen("divisor.in" , "r" , stdin);
    freopen("divisor.out" , "w" , stdout);
#endif
}

const int maxn = 1000003, maxm = 1000003;

int n, m, a[maxn], cnt[maxm], ans[maxm], pow2[maxn];

int num[13], cntt;
inline void write(int x)
{
    if (!x){
        putchar('0'); return ;
    }
    cntt = 0;
    do
    {
        num[++ cntt] = x % 10;
        x /= 10;
    }while (x);
    Fordown(i, cntt, 1) putchar(num[i] + 48);
}

int main()
{
    File();
    n = read();
    pow2[0] = 1;
    For(i, 1, n) {
        a[i] = read(), ++ cnt[a[i]], pow2[i] = pow2[i - 1] << 1;
        chkmax(m, a[i]);
        if (pow2[i] >= Mod) pow2[i] -= Mod;
    }
    For(i, 1, m >> 1) for (register int j = 2 * i; j <= m; j += i) cnt[i] += cnt[j];
    Fordown(i, m, 1) {
        ans[i] = pow2[cnt[i]] - 1;
        for (register int j = 2 * i; j <= m; j += i)
        {
            ans[i] -= ans[j];
            if (ans[i] < 0) ans[i] += Mod;
        }
    }
    write(ans[1]); putchar('\n');
    return 0;
}
//雪花遣霰作前锋,势破张皇欲暗空。
//筛瓦巧寻疏处漏,跳阶误到暖边融。
//寒声带雨山难白,冷气侵人火失红。
//方讶一冬暄较甚,今宵敢叹卧如弓。
//--杨万里《霰》
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值