C. Square Subsets(状压dp+组合数学/线性基)

https://codeforces.com/problemset/problem/895/C


线性基待补。

思路:将质因子状态集合状压,0表示此时次数为偶数,1表示次数次数为奇数。

对于当前s,奇数时候其分解的质因子要和s异或得之前的状态。偶数直接沿用。

对于某个数字出现的次数k,我们考虑取C(1,K)+C(3,K)+----=2^(K-1).偶数同理。

最后注意,卡时间,预处理幂次。卡空间。改成滚动。

 

 

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=100;
typedef long long LL;
const LL mod=1e9+7;
inline LL read(){LL x=0,f=1;char ch=getchar();	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
return x*f;}
LL a[maxn],cnt[maxn];
LL dp[2][1<<(20)];
LL primes[maxn]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67};
LL p[200000];
LL solve(LL num,LL re){
    for(LL i=0;i<19;i++){
        while(num>1&&(num%primes[i]==0)){
            re^=(1<<i);
            num/=primes[i];
        }
    }
    return re;
}
int main(void){
    p[0]=1;
    for(LL i=1;i<200000-10;i++) p[i]=p[i-1]*2%mod;
    LL n;n=read();
    for(LL i=1;i<=n;i++){
        LL x;x=read();cnt[x]++;
    }
    dp[0][0]=1;
    LL bol=0;LL ans=0;
    for(LL i=1;i<=70;i++){
        if(!cnt[i]){
            for(LL s=0;s<p[19];s++) dp[i&1][s]=dp[(i&1)^1][s];
            continue;///压维后直接继承过去
        }
      ///  bol^=1;
        LL chan=solve(i,0);
        for(LL s=0;s<p[19];s++){
            LL pre=chan^s;
            ///当前取奇数个
            dp[i&1][s]=(dp[i&1][s]%mod+p[cnt[i]-1]%mod*dp[(i&1)^1][pre]%mod)%mod;
            ///当前取偶数个
            dp[i&1][s]=(dp[i&1][s]%mod+p[cnt[i]-1]%mod*dp[(i&1)^1][s]%mod)%mod;
            ///dp[bol][s]=(((dp[bol^1][pre]%mod+dp[bol^1][s]%mod)%mod)*p[cnt[i]-1]%mod)%mod;
        }
        for(LL s=0;s<p[19];s++){
            dp[(i&1)^1][s]=0;
        }
        ans=dp[i&1][0]%mod;
    }
    printf("%lld\n",(ans-1)%mod);
   	return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值