結果

問題 No.2129 Perfect Binary Tree...?
ユーザー 👑 kmjpkmjp
提出日時 2022-11-22 23:44:46
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 99 ms / 3,000 ms
コード長 2,195 bytes
コンパイル時間 2,338 ms
コンパイル使用メモリ 209,424 KB
実行使用メモリ 23,312 KB
最終ジャッジ日時 2023-10-24 14:32:04
合計ジャッジ時間 6,332 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
8,396 KB
testcase_01 AC 81 ms
8,396 KB
testcase_02 AC 81 ms
8,400 KB
testcase_03 AC 80 ms
8,396 KB
testcase_04 AC 81 ms
8,516 KB
testcase_05 AC 82 ms
8,396 KB
testcase_06 AC 84 ms
10,068 KB
testcase_07 AC 84 ms
9,260 KB
testcase_08 AC 91 ms
14,476 KB
testcase_09 AC 91 ms
14,756 KB
testcase_10 AC 81 ms
8,400 KB
testcase_11 AC 84 ms
9,392 KB
testcase_12 AC 91 ms
14,232 KB
testcase_13 AC 99 ms
23,188 KB
testcase_14 AC 91 ms
14,952 KB
testcase_15 AC 90 ms
13,896 KB
testcase_16 AC 97 ms
23,188 KB
testcase_17 AC 82 ms
8,628 KB
testcase_18 AC 82 ms
8,396 KB
testcase_19 AC 82 ms
8,576 KB
testcase_20 AC 83 ms
8,528 KB
testcase_21 AC 82 ms
8,516 KB
testcase_22 AC 82 ms
8,528 KB
testcase_23 AC 97 ms
23,312 KB
testcase_24 AC 98 ms
22,792 KB
testcase_25 AC 87 ms
11,404 KB
testcase_26 AC 82 ms
8,396 KB
testcase_27 AC 86 ms
11,340 KB
testcase_28 AC 92 ms
14,552 KB
testcase_29 AC 93 ms
15,100 KB
testcase_30 AC 97 ms
23,188 KB
testcase_31 AC 91 ms
14,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------

int N;
string S,T;
int A,B,L;
ll F[202020],G[202020],num[202020];
const ll mo=998244353;

ll modpow(ll a, ll n = mo-2) {
	ll r=1;a%=mo;
	while(n) r=r*((n%2)?a:1)%mo,a=a*a%mo,n>>=1;
	return r;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>S>>T;
	A=S.size();
	B=T.size();
	FOR(i,min(A,B)) {
		if(S[i]!=T[i]) break;
		L=i+1;
	}
	if(A>B) swap(A,B);
	
	FOR(i,202020) {
		F[i]=((i+3)*modpow(2,i)+(i-3)*modpow(2,2*i))%mo;
		G[i]=((i-2)*modpow(2,i)+2)%mo;
		num[i]=modpow(2,i)-1;
	}
	ll tot=F[N];
	if(A==B&&A==L) {
		cout<<tot<<endl;
		return;
	}
	//cout<<A<<" "<<B<<" "<<L<<endl;
	vector<ll> C,CS,CW;
	if(A==L) {
		C.push_back(num[N]-num[N-A]);
		for(i=A+1;i<B;i++) {
			C.push_back(num[N+1-i]-num[N-i]);
		}
		C.push_back(num[N+1-B]);
	}
	else {
		C.push_back(num[N+1-A]);
		for(i=A-1;i>L;i--) {
			C.push_back(num[N+1-i]-num[N-i]);
		}
		C.push_back(num[N]-num[N-L]*2);
		for(i=L+1;i<B;i++) {
			C.push_back(num[N+1-i]-num[N-i]);
		}
		C.push_back(num[N+1-B]);
	}
	CS={C[0]};
	CW={0};
	for(i=1;i<C.size();i++) {
		CS.push_back((CS.back()+C[i]+mo)%mo);
		CW.push_back((CW.back()+i*C[i]%mo+mo)%mo);
	}
	assert(CS.back()==num[N]);
	FOR(i,C.size()) {
		j=i+(C.size()+1)/2;
		if(j>=C.size()) break;
		(tot-=((CW.back()-CW[j-1])-i*(CS.back()-CS[j-1]))%mo*C[i]%mo)%=mo;
		(tot+=(((int)C.size()+i)*(CS.back()-CS[j-1])-(CW.back()-CW[j-1]))%mo*C[i]%mo)%=mo;
	}
	
	cout<<(tot%mo+mo)%mo<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0