QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#232543#7559. Bocchi the RockExplodingKonjacWA 1ms3468kbC++208.5kb2023-10-30 16:16:542023-10-30 16:16:54

Judging History

你现在查看的是最新测评结果

  • [2023-10-30 16:16:54]
  • 评测
  • 测评结果:WA
  • 用时:1ms
  • 内存:3468kb
  • [2023-10-30 16:16:54]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define OPENIOBUF

namespace FastIO
{

class FastIOBase
{
 protected:
#ifdef OPENIOBUF
	static const int BUFSIZE=1<<16;
	char buf[BUFSIZE+1];
	int buf_p=0;
#endif
	FILE *target;
	FastIOBase(FILE *f): target(f){}
	~FastIOBase()=default;
 public:
#ifdef OPENIOBUF
	virtual void flush()=0;
#endif
};

class FastOutput final: public FastIOBase
{
 public:
	FastOutput(FILE *f=stdout): FastIOBase(f) {}
#ifdef OPENIOBUF
	~FastOutput() { flush(); }
	void flush() { fwrite(buf,1,buf_p,target),buf_p=0; }
#endif
	void put(char x)
	{
#ifdef OPENIOBUF
		if(buf[buf_p++]=x,buf_p==BUFSIZE) flush();
#else
		putc(x,target);
#endif
	}
	FastOutput &operator <<(char x)
	{ return put(x),*this; }
	FastOutput &operator <<(const char *s)
	{ for(;*s;put(*(s++)));return *this; }
	FastOutput &operator <<(const std::string &s)
	{ return (*this)<<s.c_str(); }
	template<typename T>
	std::enable_if_t<std::is_integral<T>::value,FastOutput&> operator <<(T x)
	{
		if(x<0) return put('-'),(*this)<<(-x);
		char stk[numeric_limits<T>::digits10+1],*top=stk;
		do *(top++)=x%10+'0',x/=10; while(x);
		while(top!=stk) put(*(--top));
		return *this;
	}
	template<typename ...T>
	void writesp(T &&...x)
	{ std::initializer_list<int>{((*this)<<(x),put(' '),0)...}; }
	template<typename ...T>
	void writeln(T &&...x)
	{ std::initializer_list<int>{((*this)<<(x),put('\n'),0)...}; }
	template<typename Iter>
	void writesp(Iter begin,Iter end)
	{ while(begin!=end) (*this)<<*(begin++)<<' '; }
	template<typename Iter>
	void writeln(Iter begin,Iter end)
	{ while(begin!=end) (*this)<<*(begin++)<<'\n'; }
}qout;

class FastInput final: public FastIOBase
{
 private:
	bool __eof;
 public:
	FastInput(FILE *f=stdin): FastIOBase(f),__eof(false)
#ifdef OPENIOBUF
	{ buf_p=BUFSIZE; }
	void flush() { buf[fread(buf,1,BUFSIZE,target)]=EOF,buf_p=0; }
	bool eof()const { return buf[buf_p]==EOF; }
#else
	{}
	bool eof()const { return feof(target); }
#endif
	char get()
	{
		if(__eof) return EOF;
#ifdef OPENIOBUF
		if(buf_p==BUFSIZE) flush();
		char ch=buf[buf_p++];
#else
		char ch=getc(target);
#endif
		return ~ch?ch:(__eof=true,EOF);
	}
	void unget(char c)
	{
		__eof=false;
#ifdef OPENIOBUF
		buf[--buf_p]=c;
#else
		ungetc(c,target);
#endif
	}
	explicit operator bool()const { return !__eof; }
	FastInput &operator >>(char &x)
	{ while(isspace(x=get()));return *this; }
	template<typename T>
	std::enable_if_t<std::is_integral<T>::value,FastInput&> operator >>(T &x)
	{
		char ch,sym=0;x=0;
		while(isspace(ch=get()));
		if(__eof) return *this;
		if(ch=='-') sym=1,ch=get();
		for(;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=get());
		return unget(ch),sym?x=-x:x,*this;
	}
	FastInput &operator >>(char *s)
	{
		while(isspace(*s=get()));
		if(__eof) return *this;
		for(;!isspace(*s) && !__eof;*(++s)=get());
		return unget(*s),*s='\0',*this;
	}
	FastInput &operator >>(std::string &s)
	{
		char str_buf[(1<<8)+1]={0},*p=str_buf;
		char *const buf_end=str_buf+(1<<8);
		while(isspace(*p=get()));
		if(__eof) return *this;
		for(s.clear(),p++;;p=str_buf)
		{
			for(;p!=buf_end && !isspace(*p=get()) && !__eof;p++);
			if(p!=buf_end) break;
			s.append(str_buf);
		}
		unget(*p),*p='\0',s.append(str_buf);
		return *this;
	}
	template<typename ...T>
	void read(T &&...x)
	{ std::initializer_list<int>{((*this)>>(x),0)...}; }
	template<typename Iter>
	void read(Iter begin,Iter end)
	{ while(begin!=end) (*this)>>*(begin++); }
}qin;

} // namespace FastIO
using FastIO::qin,FastIO::qout;

using LL=long long;
using LD=long double;
using UI=unsigned int;
using ULL=unsigned long long;

#ifndef DADALZY
#define FILEIO(file) freopen(file".in","r",stdin),freopen(file".out","w",stdout)
#define LOG(...) [](auto...){}(__VA_ARGS__)
#else
#define FILEIO(file)
#define LOG(...) fprintf(stderr,__VA_ARGS__)
#endif

namespace Math
{

template<typename Derived>
class ModintBase
{
#define DEF_OP1(op,expr) \
	friend constexpr Derived operator op(const Derived &lhs,const Derived &rhs) \
	{ return Derived(lhs)op##=rhs; } \
	constexpr Derived &operator op##=(const Derived &rhs) \
	{ return (expr),*static_cast<Derived*>(this); }
#define DEF_OP2(op,expr) \
	constexpr Derived operator op(int) \
	{ Derived res(*static_cast<Derived*>(this)); return op(*this),res; } \
	constexpr Derived &operator op() \
	{ return (expr),*static_cast<Derived*>(this); }
#define DEF_OP3(op,expr) \
	constexpr Derived operator op()const \
	{ return (expr); }
#define DEF_OP4(op) \
	friend constexpr bool operator op(const Derived &lhs,const Derived &rhs) \
	{ return lhs.val op rhs.val; }
#define MOD Derived::getMod()
 protected:
	int val;
 private:
	template<typename T>
	static constexpr std::enable_if_t<std::is_integral<T>::value,T> __inv(T a,T b)
	{ T x=0,y=1,t=0;for(;a;t=b/a,std::swap(a,b-=t*a),std::swap(y,x-=t*y));return x; }
	template<typename T>
	static constexpr std::enable_if_t<std::is_integral<T>::value,T> __fix(T x)
	{ return Derived::fix(x); }
	static constexpr void __qmo(int &x)
	{ x+=(x>>31&MOD); }
 public:
	constexpr ModintBase(): val(0) {}
	constexpr ModintBase(const Derived &x): val(x.val) {}
	template<typename T>
	constexpr ModintBase(T x): val(__fix(x)) {}
	constexpr static Derived unfixed(int x)
	{ return reinterpret_cast<Derived&>(x); }
	constexpr Derived inv()const
	{ return Derived(__inv(val,MOD)); }
	constexpr int operator ()()const
	{ return val; }
	DEF_OP1(+,__qmo(val+=rhs.val-MOD))
	DEF_OP1(-,__qmo(val-=rhs.val))
	DEF_OP1(*,(val=__fix(1ll*val*rhs.val)))
	DEF_OP1(/,(val=__fix(1ll*val*__inv(rhs.val,MOD))))
	DEF_OP2(++,__qmo(val+=1-MOD))
	DEF_OP2(--,__qmo(--val))
	DEF_OP3(+,*this)
	DEF_OP3(-,unfixed(val?MOD-val:0))
	DEF_OP4(==) DEF_OP4(!=) DEF_OP4(<) DEF_OP4(>) DEF_OP4(<=) DEF_OP4(>=)
#undef DEF_OP1
#undef DEF_OP2
#undef DEF_OP3
#undef DEF_OP4
#undef MOD
};
template<typename T,typename U>
constexpr std::enable_if_t<std::is_integral<T>::value,U> qpow(U x,T y)
{ U res(1);for(;y;x*=x,y>>=1)if(y&1)res*=x;return res; }

class FastMod
{
 private:
	using ULL=uint64_t;
	using U128=__uint128_t;
	ULL p,m;
 public:
	constexpr FastMod(): p(0),m(0) {}
	constexpr FastMod(ULL p): p(p),m((U128(1)<<64)/p) {}
	constexpr ULL getp()const { return p; }
	constexpr ULL operator()(ULL a)const
	{ ULL q=(U128(m)*a)>>64,r=a-q*p;return r>=p?r-p:r; }
};

// Modint for dynamic MOD
class DModint: public ModintBase<DModint>
{
 private:
	using BaseT=ModintBase<DModint>;
	static FastMod R;
	friend BaseT;
	template<typename T> static constexpr T fix(T x)
	{ return x<0?R.getp()-R(-x):R(x); }
 public:
	using BaseT::BaseT;
	static constexpr void setMod(int mod) { R=FastMod(mod); }
	static constexpr int getMod() { return R.getp(); }
};
FastMod DModint::R{};

// Modint for static MOD
template<int MOD>
class SModint: public ModintBase<SModint<MOD>>
{
 private:
	using BaseT=ModintBase<SModint<MOD>>;
	friend BaseT;
	template<typename T> static constexpr T fix(T x)
	{ return (x%=MOD)<0?x+MOD:x; }
 public:
	using BaseT::BaseT;
	static constexpr int getMod() { return MOD; }
};

} // namespace Math

constexpr int MOD=998244353;
using Mint=Math::SModint<MOD>;

int n;
char s[100005];

// Y: 0, P: 1, "YP"[k]
// R: 0, P: 1, "RB"[k]
inline bool chkp(char c,int k) { return c!="RB"[k^1]; }
inline bool chke(char c,int k) { return c!="YP"[k^1]; }

Mint _dp[2][2][2][50005];
Mint solve()
{
	auto f=_dp[0],g=_dp[1];
	for(int e: {0,1}) for(int p: {0,1})
		fill(f[e][p],f[e][p]+n+1,0);
	for(int e: {0,1})
		if(chke(1,e)) f[e][0][0]=1;
	for(int i=1;i<n;i++)
	{
		for(int e: {0,1}) for(int p: {0,1})
			fill(g[e][p],g[e][p]+i+1,0);
#pragma GCC unroll(2)
		for(int e=0;e<2;e++) 
#pragma GCC unroll(2)
		for(int p=0;p<2;p++)
		for(int j=0;j<i;j++)
		{
			if(!f[e][p][j]()) continue;
			Mint from=f[e][p][j];
			bool fl[2]={chkp(s[i*2],0),chkp(s[i*2],1)};
			if(chke(s[i*2+1],e))
			{
				if(fl[0]) g[e][p][j]+=from;
				if(fl[1]) g[e][p][j]+=from;
			}
			if(chke(s[i*2+1],!e))
			{
				if(!j)
				{
					if(fl[0]) g[!e][0][1]+=from;
					if(fl[1]) g[!e][1][1]+=from;
				}
				else
				{
					if(fl[p]) g[!e][!p][j-1]+=from;
					if(fl[!p]) g[!e][!p][j+1]+=from;
				}
			}
		}
		swap(f,g);
	}
	Mint res;
	for(int e=0;e<2;e++) for(int p=0;p<2;p++)
	{
		if(chkp(s[2*n],0)) res+=f[e][p][0];
		if(chkp(s[2*n],1)) res+=f[e][p][0];
		if(chkp(s[2*n],p)) res+=f[e][p][1];
	}
	return res;
}

int main()
{
	qin>>n>>(s+1);
	qout<<solve()()<<'\n';
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3416kb

input:

2
????

output:

12

result:

ok 1 number(s): "12"

Test #2:

score: 0
Accepted
time: 0ms
memory: 3468kb

input:

3
??YR?B

output:

4

result:

ok 1 number(s): "4"

Test #3:

score: 0
Accepted
time: 0ms
memory: 3296kb

input:

5
YBYRPBYRYB

output:

0

result:

ok 1 number(s): "0"

Test #4:

score: -100
Wrong Answer
time: 0ms
memory: 3420kb

input:

10
PRPBPRPRPRPBYB?R?BY?

output:

5

result:

wrong answer 1st numbers differ - expected: '3', found: '5'