[BZOJ4851][JSOI2016]位运算[矩阵快速幂]

题意

给定长度为 \(\rm |S|\)\(\rm 01\) 串并将其倍长 \(k\) 次得到一个 \(\rm|S|\times k\) 位的二进制数 \(R\) ,求有多少种在 \([0,R-1]\) 中选择

\(m\) 个互不相同的数字使得其异或和为 \(0\) 的方案。

\(\rm |S|\leq 50\ ,k \leq 10^5\ ,m\leq 7\) .

分析

假设选定的 \(m\) 个数字满足 \(A_0 < A_1 < \cdots < A_{m-1}\).

定义状态 \(f_S\) ,每一个二进制位表示 \(A_i\) 是否大于 \(A_{i+1}\) ,最后一位表示 \(A_{m-1}\) 是否小于上界(类似数位dp)。

对于原串每一位构造矩阵,对于循环转移相同,快速幂即可。

总时间复杂度为 \(O(2^{3n}*(log\ k+|S|))\).

代码

#include<bits/stdc++.h>
using namespace std;
#define go(u) for(int i=head[u],v=e[i].to;i;i=e[i].last,v=e[i].to)
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define pb push_back
typedef long long LL;
inline int gi(){
    int x=0,f=1;char ch=getchar();
    while(!isdigit(ch)) {if(ch=='-') f=-1;ch=getchar();}
    while(isdigit(ch)){x=(x<<3)+(x<<1)+ch-48;ch=getchar();}
    return x*f;
}
template<typename T>inline bool Max(T &a,T b){return a<b?a=b,1:0;}
template<typename T>inline bool Min(T &a,T b){return b<a?a=b,1:0;}
const int mod=1e9 + 7;
int m,K,maxn;
char str[130];
int cnt[130];
void add(LL &a,LL b){ a+=b;if(a>=mod) a-=mod; }
struct mt{
    LL v[130][130];
    mt(){memset(v,0,sizeof v);}
    void init(){memset(v,0,sizeof v);}
    mt operator *(const mt &rhs)const{
        mt res;
        rep(i,0,maxn)rep(j,0,maxn)rep(k,0,maxn)
        add(res.v[i][j],v[i][k]*rhs.v[k][j]%mod);
        return res;
    }
}B,tmp;
mt Pow(mt a,int b){
    mt res;
    rep(i,0,maxn) res.v[i][i]=1;
    for(;b;b>>=1,a=a*a) if(b&1) res=res*a;
    return res;
}
int main(){
    m=gi(),K=gi();maxn=(1<<m)-1;
    scanf("%s",str);
    int l=strlen(str);
    for(int i=0;i<130;++i) cnt[i]=cnt[i>>1]+(i&1);
    rep(i,0,maxn) B.v[i][i]=1;
    for(int i=0;i<l;++i){
        tmp.init();
        rep(a,0,maxn)
        rep(b,0,maxn)if(!(cnt[b]&1)){
            int S=0;
            for(int j=0;j<m;++j){
                if(a>>j&1) { S|=(1<<j); continue;}
                int x=(b>>j&1),y=j==m-1?str[i]-'0':(b>>j+1)&1;//
                if(x>y) goto A;
                S|=(x<y)<<j;
            }
            tmp.v[a][S]++;
            A:;
        }
        B=B*tmp;
    }
    B=Pow(B,K);
    printf("%lld\n",B.v[0][maxn]);
    return 0;
}

转载于:https://www.cnblogs.com/yqgAKIOI/p/9797277.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值