Codeforces 1065E(计数)

题目链接

题意:限定字符串长度为n,字符集规模为A,以及m个数字b,对于任意数字bi满足长度为bi的前缀和后缀先反转再交换位置后形成的新串与原串视作相等,问存在多少不同串

思路:设,将字符串看成由长度串构成,那么只需考虑中对应串的方案数和中间单独的方案数,相乘即答案.

假设考虑位,形成回文的对应串有,不形成的有,相加后得,中间即.

#include <bits/stdc++.h>
#define DBG(x) cerr << #x << " = " << x << endl;
const long long mod = 998244353;
const int maxn = 2e5+5;
using namespace std;
typedef long long LL;

LL n,m,A;
LL b[maxn];

LL qpow(LL a,LL b,LL p){
    LL res=1;
    while(b){
        if(b&1)res=(res*a)%p;
        a=a*a%p,b/=2;
    }return res;
}

int main(){
    scanf("%I64d%I64d%I64d",&n,&m,&A);
    for(int i=1;i<=m;i++)scanf("%I64d",&b[i]);
    LL ans=1,inv=qpow(2,mod-2,mod);
    for(int i=1;i<=m;i++){
        LL tmp=qpow(A,b[i]-b[i-1],mod);
        ans=ans*tmp%mod*(1+tmp)%mod*inv%mod;
    }
    ans*=qpow(A,n-2*b[m],mod);
    printf("%I64d\n",ans%mod);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/DuskOB/p/10034474.html