QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#669528#9479. And DNAbulijiojiodibuliduo#AC ✓6ms25240kbC++175.4kb2024-10-23 18:54:392024-10-23 18:54:41

Judging History

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

  • [2024-10-23 18:54:41]
  • 评测
  • 测评结果:AC
  • 用时:6ms
  • 内存:25240kb
  • [2024-10-23 18:54:39]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}()); 
const ll mod=998244353;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head

template<int MOD, int RT> struct mint {
	static const int mod = MOD;
	static constexpr mint rt() { return RT; } // primitive root for FFT
	int v; explicit operator int() const { return v; } // explicit -> don't silently convert to int
	mint():v(0) {}
	mint(ll _v) { v = int((-MOD < _v && _v < MOD) ? _v : _v % MOD);
		if (v < 0) v += MOD; }
	bool operator==(const mint& o) const {
		return v == o.v; }
	friend bool operator!=(const mint& a, const mint& b) { 
		return !(a == b); }
	friend bool operator<(const mint& a, const mint& b) { 
		return a.v < b.v; }
   
	mint& operator+=(const mint& o) { 
		if ((v += o.v) >= MOD) v -= MOD; 
		return *this; }
	mint& operator-=(const mint& o) { 
		if ((v -= o.v) < 0) v += MOD; 
		return *this; }
	mint& operator*=(const mint& o) { 
		v = int((ll)v*o.v%MOD); return *this; }
	mint& operator/=(const mint& o) { return (*this) *= inv(o); }
	friend mint pow(mint a, ll p) {
		mint ans = 1; assert(p >= 0);
		for (; p; p /= 2, a *= a) if (p&1) ans *= a;
		return ans; }
	friend mint inv(const mint& a) { assert(a.v != 0); 
		return pow(a,MOD-2); }
		
	mint operator-() const { return mint(-v); }
	mint& operator++() { return *this += 1; }
	mint& operator--() { return *this -= 1; }
	friend mint operator+(mint a, const mint& b) { return a += b; }
	friend mint operator-(mint a, const mint& b) { return a -= b; }
	friend mint operator*(mint a, const mint& b) { return a *= b; }
	friend mint operator/(mint a, const mint& b) { return a /= b; }
};

const int MOD=998244353; 
using mi = mint<MOD,5>; // 5 is primitive root for both common mods

namespace simp {
	vector<mi> fac,ifac,invn;
	void check(int x) {
		if (fac.empty()) {
			fac={mi(1),mi(1)};
			ifac={mi(1),mi(1)};
			invn={mi(0),mi(1)};
		}
		while (SZ(fac)<=x) {
			int n=SZ(fac),m=SZ(fac)*2;
			fac.resize(m);
			ifac.resize(m);
			invn.resize(m);
			for (int i=n;i<m;i++) {
				fac[i]=fac[i-1]*mi(i);
				invn[i]=mi(MOD-MOD/i)*invn[MOD%i];
				ifac[i]=ifac[i-1]*invn[i];
			}
		}
	}
	mi gfac(int x) {
		assert(x>=0);
		check(x); return fac[x];
	}
	mi ginv(int x) {
		assert(x>0);
		check(x); return invn[x];
	}
	mi gifac(int x) {
		assert(x>=0);
		check(x); return ifac[x];
	}
	mi binom(int n,int m) {
		if (m < 0 || m > n) return mi(0);
		return gfac(n)*gifac(m)*gifac(n - m);
	}
	mi binombf(int n,int m) {
		if (m < 0 || m > n) return mi(0);
		if (m>n-m) m=n-m;
		mi p=1,q=1;
		for (int i=1;i<=m;i++) p=p*(n+1-i),q=q*i;
		return p/q;
	}
}

namespace linear_seq {
	const int N=10010;
	ll res[N],base[N],_c[N],_md[N];

	vector<int> Md;
	void mul(ll *a,ll *b,int k) {
		rep(i,0,k+k) _c[i]=0;
		rep(i,0,k) if (a[i]) rep(j,0,k) _c[i+j]=(_c[i+j]+a[i]*b[j])%mod;
		for (int i=k+k-1;i>=k;i--) if (_c[i])
			rep(j,0,SZ(Md)) _c[i-k+Md[j]]=(_c[i-k+Md[j]]-_c[i]*_md[Md[j]])%mod;
		rep(i,0,k) a[i]=_c[i];
	}
	int solve(ll n,VI a,VI b) { // a 系数 b 初值 b[n+1]=a[0]*b[n]+...
		ll ans=0,pnt=0;
		int k=SZ(a);
		assert(SZ(a)==SZ(b));
		rep(i,0,k) _md[k-1-i]=-a[i];_md[k]=1;
		Md.clear();
		rep(i,0,k) if (_md[i]!=0) Md.push_back(i);
		rep(i,0,k) res[i]=base[i]=0;
		res[0]=1;
		while ((1ll<<pnt)<=n) pnt++;
		for (int p=pnt;p>=0;p--) {
			mul(res,res,k);
			if ((n>>p)&1) {
				for (int i=k-1;i>=0;i--) res[i+1]=res[i];res[0]=0;
				rep(j,0,SZ(Md)) res[Md[j]]=(res[Md[j]]-res[k]*_md[Md[j]])%mod;
			}
		}
		rep(i,0,k) ans=(ans+res[i]*b[i])%mod;
		if (ans<0) ans+=mod;
		return ans;
	}
	VI BM(VI s) {
		VI C(1,1),B(1,1);
		int L=0,m=1,b=1;
		rep(n,0,SZ(s)) {
			ll d=0;
			rep(i,0,L+1) d=(d+(ll)C[i]*s[n-i])%mod;
			if (d==0) ++m;
			else if (2*L<=n) {
				VI T=C;
				ll c=mod-d*powmod(b,mod-2)%mod;
				while (SZ(C)<SZ(B)+m) C.pb(0);
				rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
				L=n+1-L; B=T; b=d; m=1;
			} else {
				ll c=mod-d*powmod(b,mod-2)%mod;
				while (SZ(C)<SZ(B)+m) C.pb(0);
				rep(i,0,SZ(B)) C[i+m]=(C[i+m]+c*B[i])%mod;
				++m;
			}
		}
		return C;
	}
	int gao(VI a,ll n) {
		VI c=BM(a);
		c.erase(c.begin());
		rep(i,0,SZ(c)) c[i]=(mod-c[i])%mod;
		return solve(n,c,VI(a.begin(),a.begin()+SZ(c)));
	}
};

const int N=601000;
int n,m;
mi dp[N][3][3],b1;
VI ret;
map<int,mi> f;

mi gao(int n) {
	if (f.count(n)) return f[n];
	if (n==0) return f[0]=1;
	else if (n==1) return f[1]=b1;
	else if (n%2==0) {
		return f[n]=(gao(n/2)+gao(n/2-1));
	} else {
		return f[n]=b1*gao(n/2);
	}
}
int main() {
	scanf("%d%d",&n,&m);
	dp[0][0][0]=1;
	dp[0][1][1]=1;
	dp[0][2][2]=1;
	ret.pb(0);
	rep(i,1,1001) {
		rep(j,0,3) rep(k,0,3) rep(l,0,3) {
			if (l==(k+1)%3||(l==0&&k==1)) {
				dp[i][j][l]+=dp[i-1][j][k];
			}
		}
		ret.pb((int)(dp[i][0][0]+dp[i][1][1]+dp[i][2][2]));
	}
	b1=linear_seq::gao(ret,n);
	//printf("!! %d\n",(int)b1);
	printf("%d\n",(int)gao(m));
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 25232kb

input:

3 2

output:

4

result:

ok "4"

Test #2:

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

input:

3 0

output:

1

result:

ok "1"

Test #3:

score: 0
Accepted
time: 5ms
memory: 25124kb

input:

100 100

output:

343406454

result:

ok "343406454"

Test #4:

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

input:

686579306 119540831

output:

260602195

result:

ok "260602195"

Test #5:

score: 0
Accepted
time: 6ms
memory: 25036kb

input:

26855095 796233790

output:

492736984

result:

ok "492736984"

Test #6:

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

input:

295310488 262950628

output:

503008241

result:

ok "503008241"

Test #7:

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

input:

239670714 149827706

output:

994702969

result:

ok "994702969"

Test #8:

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

input:

790779949 110053353

output:

898252188

result:

ok "898252188"

Test #9:

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

input:

726600542 795285932

output:

183726777

result:

ok "183726777"

Test #10:

score: 0
Accepted
time: 5ms
memory: 24964kb

input:

957970519 585582861

output:

298814018

result:

ok "298814018"

Test #11:

score: 0
Accepted
time: 3ms
memory: 25232kb

input:

93349859 634036506

output:

110693443

result:

ok "110693443"

Test #12:

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

input:

453035113 34126396

output:

306244220

result:

ok "306244220"

Test #13:

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

input:

31994526 100604502

output:

930620701

result:

ok "930620701"

Test #14:

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

input:

234760741 249817734

output:

440195858

result:

ok "440195858"

Test #15:

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

input:

542621111 646412689

output:

207377992

result:

ok "207377992"

Test #16:

score: 0
Accepted
time: 5ms
memory: 25232kb

input:

28492783 602632297

output:

65234506

result:

ok "65234506"

Test #17:

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

input:

213500301 768820204

output:

540205113

result:

ok "540205113"

Test #18:

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

input:

697808101 753041955

output:

93561295

result:

ok "93561295"

Test #19:

score: 0
Accepted
time: 5ms
memory: 25200kb

input:

585126464 450455977

output:

91109717

result:

ok "91109717"

Test #20:

score: 0
Accepted
time: 5ms
memory: 25240kb

input:

236696315 482334538

output:

925575199

result:

ok "925575199"

Test #21:

score: 0
Accepted
time: 6ms
memory: 25200kb

input:

632719214 298704996

output:

358926097

result:

ok "358926097"

Test #22:

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

input:

869119333 933404114

output:

318501108

result:

ok "318501108"

Test #23:

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

input:

6977994 814763202

output:

585332987

result:

ok "585332987"

Test #24:

score: 0
Accepted
time: 6ms
memory: 24972kb

input:

3 1

output:

3

result:

ok "3"

Test #25:

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

input:

3 1000000000

output:

279679226

result:

ok "279679226"

Test #26:

score: 0
Accepted
time: 6ms
memory: 25020kb

input:

4 0

output:

1

result:

ok "1"

Test #27:

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

input:

4 1

output:

2

result:

ok "2"

Test #28:

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

input:

4 1000000000

output:

1755648

result:

ok "1755648"

Test #29:

score: 0
Accepted
time: 2ms
memory: 24960kb

input:

1000000000 0

output:

1

result:

ok "1"

Test #30:

score: 0
Accepted
time: 2ms
memory: 25040kb

input:

1000000000 1

output:

696798313

result:

ok "696798313"

Test #31:

score: 0
Accepted
time: 3ms
memory: 25196kb

input:

1000000000 1000000000

output:

703786301

result:

ok "703786301"

Extra Test:

score: 0
Extra Test Passed