题解:P10877 「KDOI-07」n1gr tS0i

· · 题解

抢题解拉

题意

对于每个字符串,可以对于 1\le l<r\le n 的区间,翻转每个字符(0 变成 1),操作正好 n 次。求最后不同字符串的种数。

思路

题意读出后,我开始打暴力,但打挂了。可是,调题的时候我发现,正确的样例全是 2^p\bmod 998244353。

分类讨论

n=2

答案显然为 1。

n=3

答案为 4。

|||| |:-:|:-:|:-:| |0|1|0| |0|0|1| |1|1|1| |0|0|0|

n\ge 4

||||| |:-:|:-:|:-:|:-:| |0|1|0|0| |0|0|1|1|

每一步有两种选择,答案为 2^n。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
namespace IO {
    char ibuf[(1<<20)+1],*iS,*iT;
#define gh() getchar()
    inline long long read() {
        char ch=gh();
        long long x=0;
        bool t=0;
        while(ch<'0'||ch>'9')t|=ch=='-',ch=gh();
        while(ch>='0'&&ch<='9')x=x*10+(ch^48),ch=gh();
        return t?-x:x;
    }
    inline void print(long long x) {
        if(x<0)putchar('-'),x=-x;
        if(x>9)print(x/10);
        putchar(x%10+'0');
    }
    inline char getc() {
        char ch=gh();
        while(ch<'a'||ch>'z')ch=gh();
        return ch;
    }
}
using namespace IO;
int n,t,mod=998244353;
string s;
int qp(int a, int b) {
    int res = 1 % mod;
    while (b) {
        if (b & 1) {
            res = (res * a) % mod;
        }
        b >>= 1;
        a = (a * a) % mod;
    }
    return res;
}
main() {
    t=read();
    while(t--) {
        n=read();
        cin>>s;
        if(n==2)printf("1\n");
        else if(n==3)printf("4\n");
        else cout<<qp(2,n)<<"\n";
    }
    return 0;
}