Codeforces 803F Coprime Subsequences DP+GCD

来源:互联网 发布:淘宝网络营销 编辑:程序博客网 时间:2024/06/06 03:29

点击打开链接

题意:n个数a[i],问有多少个子序列的gcd为1? (子序列下标不同就算不同) n,a[i]<=1e5 

令dp[i]:gcd为i的子序列个数 
i的倍数的子序列有2^c[i]-1个,这些子序列的gcd可能为(i,2i,3i...) 
则gcd为i的子序列个数:dp[i]=2^(c[i])-dp[2i]-dp[3i]...

#include <bits/stdc++.h>using namespace std;typedef long long ll;const ll mod=1e9+7;const int N=2e5+20;const int M=1e5;ll cnt[N],c[N];//c[i] i的倍数的个数 ll dp[N];//dp[i]:gcd为i的子序列个数 int n;ll pw2[N]; int main(){ while(cin>>n){memset(dp,0,sizeof(dp));memset(cnt,0,sizeof(cnt));int x;pw2[0]=1;for(int i=1;i<=n;i++){scanf("%I64d",&x);cnt[x]++;pw2[i]=(2ll*pw2[i-1])%mod;}for(int i=M;i>=1;i--){ll tot=0;for(int k=i;k<=M;k+=i)tot+=cnt[k];//tot:i的倍数个数 dp[i]=pw2[tot]-1;for(int k=i+i;k<=M;k+=i)dp[i]=(dp[i]-dp[k]+mod)%mod;}cout<<dp[1]<<endl;}return 0;} 


原创粉丝点击