Codeforces803 F.Coprime Subsequences(容斥)

题意:

给定长度为n的序列,
问有多少个gcd为1的子序列。
答案对1e9+7取模。

数据范围:n<=1e5,a(i)<=1e5

解法:

设 f ( i ) 为 g c d 为 i 的 子 序 列 数 量 设f(i)为gcd为i的子序列数量 f(i)gcdi

考 虑 容 斥 , 先 计 算 出 g c d = i 的 倍 数 的 数 量 , 然 后 容 斥 掉 g c d 为 i ∗ 2 , i ∗ 3.. 的 数 量 考虑容斥,先计算出gcd=i的倍数的数量,然后容斥掉gcd为i*2,i*3..的数量 ,gcd=i,gcdi2,i3..

设 c n t [ i ] 为 序 列 中 i 的 数 量 , 那 么 f [ i ] = ( 2 c n t [ i ] − 1 ) − f [ i ∗ 2 ] − f [ i ∗ 3 ] . . . 设cnt[i]为序列中i的数量,那么f[i]=(2^{cnt[i]}-1)-f[i*2]-f[i*3]... cnt[i]i,f[i]=(2cnt[i]1)f[i2]f[i3]...

其 中 2 c n t [ i ] − 1 是 从 所 有 i 的 倍 数 中 选 出 若 干 个 数 的 方 案 数 , 其中2^{cnt[i]-1}是从所有i的倍数中选出若干个数的方案数, 2cnt[i]1i,

即 C c n t [ i ] 1 + C c n t [ i ] 2 . . C c n t [ i ] c n t [ i ] , 因 为 不 能 取 空 集 , 因 此 减 掉 C c n t [ i ] 0 , 即C_{cnt[i]}^1+C_{cnt[i]}^2..C_{cnt[i]}^{cnt[i]},因为不能取空集,因此减掉C_{cnt[i]}^0, Ccnt[i]1+Ccnt[i]2..Ccnt[i]cnt[i]Ccnt[i]0,

从 后 往 前 逆 推 即 可 从后往前逆推即可

code:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxm=1e5+5;
const int mod=1e9+7;
int f[maxm];
int a[maxm];
int cnt[maxm];
int p2[maxm];
int n;
signed main(){
    p2[0]=1;
    for(int i=1;i<maxm;i++)p2[i]=p2[i-1]*2%mod;
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i],cnt[a[i]]++;
    for(int i=1;i<maxm;i++){
        for(int j=i+i;j<maxm;j+=i){
            cnt[i]+=cnt[j];
        }
    }
    for(int i=maxm-1;i>=1;i--){
        f[i]=p2[cnt[i]]-1;
        for(int j=i+i;j<maxm;j+=i){
            f[i]=(f[i]-f[j])%mod;
        }
    }
    cout<<(f[1]%mod+mod)%mod<<endl;
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,Codeforces Round 511 (Div. 1)是一个比赛的名称。然而,引用内容中没有提供与这个比赛相关的具体信息或问题。因此,我无法回答关于Codeforces Round 511 (Div. 1)的问题。如果您有关于这个比赛的具体问题,请提供更多的信息,我将尽力回答。 #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces Round 867 (Div. 3)(A题到E题)](https://blog.csdn.net/wdgkd/article/details/130370975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值