QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#122485#5530. No Zero-Sum SubsegmentExplodingKonjacWA 34ms34736kbC++148.3kb2023-07-10 16:25:262023-07-10 16:25:29

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-10 16:25:29]
  • 评测
  • 测评结果:WA
  • 用时:34ms
  • 内存:34736kb
  • [2023-07-10 16:25:26]
  • 提交

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
{
#ifdef OPENIOBUF
 public:
	void flush()
	{ fwrite(buf,1,buf_p,target),buf_p=0; }
#endif
 private:
	void __putc(char x)
	{
#ifdef OPENIOBUF
		if(buf[buf_p++]=x,buf_p==BUFSIZE) flush();
#else
		putc(x,target);
#endif
	}
	template<typename T>
	void __write(T x)
	{
		char stk[64],*top=stk;
		if(x<0) return __putc('-'),__write(-x);
		do *(top++)=x%10,x/=10; while(x);
		for(;top!=stk;__putc(*(--top)+'0'));
	}
 public:
	FastOutput(FILE *f=stdout): FastIOBase(f){}
#ifdef OPENIOBUF
	~FastOutput(){ flush(); }
#endif
	FastOutput &operator <<(char x)
	{ return __putc(x),*this; }
	FastOutput &operator <<(const char *s)
	{ for(;*s;__putc(*(s++)));return *this; }
	FastOutput &operator <<(const string &s)
	{ return (*this)<<s.c_str(); }
	template<typename T>
	enable_if_t<is_integral<T>::value,FastOutput&> operator <<(const T &x)
	{ return __write(x),*this; }
	template<typename ...T>
	void writesp(const T &...x)
	{ initializer_list<int>{(this->operator<<(x),__putc(' '),0)...}; }
	template<typename ...T>
	void writeln(const T &...x)
	{ initializer_list<int>{(this->operator<<(x),__putc('\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
{
#ifdef OPENIOBUF
 public:
	void flush()
	{ buf[fread(buf,1,BUFSIZE,target)]=EOF,buf_p=0; }
#endif
 private:
	bool __eof;
	char __getc()
	{
		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 __ungetc(char c)
	{
		__eof=false;
#ifdef OPENIOBUF
		buf_p--;
#else
		ungetc(c,target);
#endif
	}
 public:
	FastInput(FILE *f=stdin): FastIOBase(f),__eof(false)
#ifdef OPENIOBUF
	{ buf_p=BUFSIZE; }
	bool eof()const { return buf[buf_p]==EOF; }
#else
	{}
	bool eof()const { return feof(target); }
#endif
	char peek() { return __getc(); }
	explicit operator bool()const { return !__eof; }
	FastInput &operator >>(char &x)
	{ while(isspace(x=__getc()));return *this; }
	template<typename T>
	enable_if_t<is_integral<T>::value,FastInput&> operator >>(T &x)
	{
		char ch,sym=0;x=0;
		while(isspace(ch=__getc()));
		if(__eof) return *this;
		if(ch=='-') sym=1,ch=__getc();
		for(x=0;isdigit(ch);x=(x<<1)+(x<<3)+(ch^48),ch=__getc());
		return __ungetc(ch),sym?x=-x:x,*this;
	}
	FastInput &operator >>(char *s)
	{
		while(isspace(*s=__getc()));
		if(__eof) return *this;
		for(;!isspace(*s) && !__eof;*(++s)=__getc());
		return __ungetc(*s),*s='\0',*this;
	}
	FastInput &operator >>(string &s)
	{
		char str_buf[(1<<8)+1]={0},*p=str_buf;
		char *const buf_end=str_buf+(1<<8);
		while(isspace(*p=__getc()));
		if(__eof) return *this;
		for(s.clear(),p++;;p=str_buf)
		{
			for(;p!=buf_end && !isspace(*p=__getc()) && !__eof;p++);
			if(p!=buf_end) break;
			s.append(str_buf);
		}
		__ungetc(*p),*p='\0',s.append(str_buf);
		return *this;
	}
	template<typename ...T>
	void read(T &...x)
	{ initializer_list<int>{(this->operator>>(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)
#else
#define FILEIO(file)
#endif

namespace Math
{

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

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)
	{ 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:
	template<typename ...T>
	constexpr DModint(T ...x): BaseT(x...){}
	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:
	template<typename ...T>
	constexpr SModint(T ...x): BaseT(x...){}
	static constexpr int getMod() { return MOD; }
};

} // namespace Math

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

constexpr int MAXN=4e6;
int T;
Mint fac[MAXN+5],inv[MAXN+5];
inline Mint calc2(int b,int c,int d)
{
	d-=2*b;
	if(b<0 || c<0 || d<0) return 0;
	return fac[b+c+d]*inv[b]*inv[c]*inv[d];
}
inline Mint calc1(int a,int b,int c,int d)
{
	if(a<0 || b<0 || c<0 || d<0) return 0;
	if(!a) return calc2(b-1,c,d-1)+calc2(b,c,d);
	else return calc2(b,c-1,d-a)+calc2(b-1,c,d-a-1);
}

int main()
{
	freopen("../input.in","r",stdin);
	qin>>T;
	fac[0]=1;
	for(int i=1;i<=MAXN;i++) fac[i]=fac[i-1]*i;
	inv[MAXN]=fac[MAXN].inv();
	for(int i=MAXN;i>=1;i--) inv[i-1]=inv[i]*i;
	while(T--)
	{
		int a,b,c,d; // -2,-1,+1,+2
		qin>>a>>b>>c>>d;
		int sum=-2*a-1*b+2*c+1*d;
		if(sum==0) { qout<<"0\n";continue; }
		else if(sum<0) swap(a,d),swap(b,c);
		Mint ans;
		auto solve=[&](int i)
		{
			if(!i) return calc1(a,b-1,c,d-1)+calc1(a,b,c,d);
			else return calc1(a-i,b,c-1,d-i)+calc1(a-i,b-1,c,d-i-1);
		};
		int lim=min(a,d);
		ans+=solve(0);
		if(lim) ans+=solve(lim)+(lim-1)*solve(1);
		qout<<ans()<<'\n';
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 34ms
memory: 34736kb

input:

5
69 0 0 0
1 1 1 1
0 0 3 3
6 1 0 6
10000 10000 1000000 1000000

output:


result:

wrong answer Answer contains longer sequence [length = 5], but output contains 0 elements