[Count] problem solution

Title effect: to a length \ (n (n <= { 18}) \ 10 ^) cycloalkyl, require any \ (m \) number, the \ (3 \) is not greater than the number \ (7 \ ) number (the entire sequence of \ (3,7 \) configuration), the total number of programs Q

\(Solution\)

Between \ (n \) is large, we had to convert to the idea of \ (m \) on.

Consider a state \ (j \) , if from the state \ (i \) transferred over, then \ (j \) the number of programs can add (i \) \ number of programs.

See \ (n-\) data, to consider fast power matrix.

Epicenter, \ ([I] [J] \) shows a state \ (J \) may be made of \ (I \) transfer over. Because it is a ring, we enumerate all states, it will be as a starting point, \ (dp \) again, then finished for the last state to \ (check \) is not legitimate at the beginning.

We handle only a beginning, put the initial matrix \ ([0] [i] \) assigned to \ (1 \) , the final matrix is a \ (1 * \) matrix number of states, it is noteworthy that the state can not just keep useful, useless state is likely to turn into a useful state.

#include<bits/stdc++.h>
using namespace std;
long long n;
int m,ans,cc;
const int mod=998244353;
const int P=mod;
int t=33;
struct Mat{
    int A[34][34];
    Mat(){
        memset(A,0,sizeof(A));
    }
    Mat operator *(const Mat&B)const {
        Mat tmp;
        for(int i=0; i<t; i++) {
            for(int j=0; j<t; j++) {
                for(int k=0; k<t; k++) {
                    tmp.A[i][j]=(tmp.A[i][j]+1ll*A[i][k]*B.A[k][j]%P)%P;
                }
            }
        }
        return tmp;
    }
}w,d;
Mat qpow(Mat a,long long b){
    Mat c=d;
    
    while(b){
        
        if(b&1)c=c*a;//cout<<"qwq\n";
        a=a*a;b>>=1;
        
    }
    return c;
}
inline int get(int x){
    int res=0;
    while(x){
        if(x&1)res++;
        x>>=1;
    }
    return res;
}
int cnt[500],tot=-1,mp[500];
int use[300];
inline bool check(int x,int y){
//  memset(use,0,sizeof(use));
    int L=0;
    for(int i=m-1;i>=0;--i)use[++L]=x>>i&1;
    for(int i=m-1;i>=0;--i)use[++L]=y>>i&1;
    for(int i=1;i<=L-m+1;++i){
        int r=0;
        for(int j=0;j<m;++j)
            r+=use[i+j];
        if(r>cc)return false;
    }
    return true;
}
int main(){
    scanf("%lld%d",&n,&m);
    //memset(cnt,-1,sizeof(cnt));
//  memset(mp,-1,sizeof(mp));
    cnt[0]=0;
    cc=m/2;
    t=(1<<m);
    for(int i=1;i<(1<<m);++i)cnt[i]=cnt[i-(i&-i)]+1;
    int H=(1<<m)-1;
    //cout<<cnt[8]<<endl;
    for(int i=0;i<(1<<m);++i){
    //  int nt=cnt[i]>>1;
        //if(cnt[nt]>cc)continue;
        if(cnt[i]>cc)continue;
        int nt=(i<<1)&H;
        if(cnt[nt]>cc)continue;
        if(cnt[nt]<=cc)w.A[i][nt]=1;
        nt|=1;
        if(cnt[nt]<=cc)w.A[i][nt]=1;
        //状态i所累加的值在mpnt列 
    }
    //for(int i=0;i<tot;++i)d.A[i][0]=1;
    /*for(int i=0;i<tot;++i){
        memset(d.A,0,sizeof(d.A));
        d.A[i][0]=1;
        Mat AA=qpow(w,n-m);
        //d.A[0][j]=1;
        //Mat e=AA*d;
        for(int j=0;j<tot;++j)
            if(check(cnt[j],cnt[i]))
                ans+=AA.A[j][0],ans%=mod;
    }*/
    for(int i=0;i<(1<<m);++i){
        if(cnt[i]>cc)continue;
        memset(d.A,0,sizeof(d.A));
        d.A[0][i]=1;
        //cout<<"qwq\n";
        Mat AA=qpow(w,n-m);
    //  cout<<"qwq\n";
        for(int j=0;j<(1<<m);++j){
            if(cnt[j]>cc)continue;
            if(check(j,i))ans+=AA.A[0][j],ans%=mod;
        }
        
    }
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/h-lka/p/12343883.html