結果

問題 No.2129 Perfect Binary Tree...?
ユーザー 👑 potato167potato167
提出日時 2022-10-31 18:31:19
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 3,000 ms
コード長 3,007 bytes
コンパイル時間 2,262 ms
コンパイル使用メモリ 208,592 KB
実行使用メモリ 12,056 KB
最終ジャッジ日時 2023-09-22 15:25:58
合計ジャッジ時間 5,074 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 4 ms
4,716 KB
testcase_04 AC 4 ms
4,868 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 8 ms
5,568 KB
testcase_07 AC 7 ms
5,432 KB
testcase_08 AC 15 ms
6,928 KB
testcase_09 AC 15 ms
6,872 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 5 ms
4,496 KB
testcase_12 AC 17 ms
9,148 KB
testcase_13 AC 28 ms
12,056 KB
testcase_14 AC 17 ms
7,932 KB
testcase_15 AC 14 ms
7,716 KB
testcase_16 AC 22 ms
11,184 KB
testcase_17 AC 4 ms
4,924 KB
testcase_18 AC 4 ms
4,744 KB
testcase_19 AC 4 ms
4,916 KB
testcase_20 AC 5 ms
5,080 KB
testcase_21 AC 4 ms
4,868 KB
testcase_22 AC 5 ms
4,968 KB
testcase_23 AC 23 ms
10,596 KB
testcase_24 AC 25 ms
11,712 KB
testcase_25 AC 11 ms
6,348 KB
testcase_26 AC 4 ms
4,624 KB
testcase_27 AC 8 ms
5,752 KB
testcase_28 AC 18 ms
8,596 KB
testcase_29 AC 21 ms
8,660 KB
testcase_30 AC 29 ms
11,316 KB
testcase_31 AC 18 ms
9,660 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
using std::cout;
using std::cin;
using std::endl;
using ll=long long;
using ld=long double;
ll ILL=2167167167167167167;
const int INF=2100000000;
const ll mod=998244353;
#define rep(i,a) for (ll i=0;i<a;i++)
#define all(p) p.begin(),p.end()
template<class T> using _pq = priority_queue<T, vector<T>, greater<T>>;
template<class T> ll LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> ll UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,const T &b){if(a>b){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
void yneos(bool a){if(a) cout<<"Yes\n"; else cout<<"No\n";}
template<class T> void vec_out(vector<T> &p){for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}
template<class T> T min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T sum(vector<T> &a){assert(!a.empty());T ans=a[0]-a[0];for(auto &x:a) ans+=x;return ans;}



void solve();
// oddloop
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int t=1;
	//cin>>t;
	rep(i,t) solve();
}

void solve(){
	ll N;
	cin>>N;
	vector<ll> fact(N+1,1);
	rep(i,N) fact[i+1]=(fact[i]*2ll)%mod;
	string S,T;
	cin>>S>>T;
	if(S.size()>T.size()) swap(S,T);
	int L=0;
	while(L<(int)S.size()&&S[L]==T[L]) L++;
	ll ans=0;
	auto f=[&](int dan)->ll{
		return ((fact[N]-fact[N-dan])*(fact[N-dan]-1))%mod;
	};
	for(int i=1;i<N;i++){
		ans+=(fact[i]*f(i))%mod;
		ans%=mod;
	}
	if(S==T){
		cout<<(ans+mod)%mod<<"\n";
		return;
	}
	for(int i=L;i<(int)(T.size());i++) ans-=f(i);
	for(int i=L;i<(int)(S.size());i++) ans-=f(i);
	ans=(ans+mod)%mod;
	vector<ll> p,q;
	if(L==(int)S.size()){
		p.push_back(fact[N]-fact[N-L]);
		for(int i=L+1;i<(int)(T.size());i++){
			p.push_back(fact[N-i]);
		}
		p.push_back(fact[N+1-(int)(T.size())]-1);
	}else{
		p.push_back(fact[N]-fact[N+1-L]+1);
		for(int i=L+1;i<(int)(S.size());i++){
			p.push_back(fact[N-i]);
		}
		p.push_back(fact[N+1-(int)(S.size())]-1);
		for(int i=L+1;i<(int)(T.size());i++){
			q.push_back(fact[N-i]);
		}
		p.push_back(fact[N+1-(int)(T.size())]-1);
		reverse(all(q));
		for(auto x:q) p.push_back(x);
	}
	ans=(ans*2ll)%mod;
	ll len=p.size();
	ll tmp=0,diff=0;
	rep(i,len){
		tmp=(tmp+min(i,len-i)*p[i])%mod;
		if(i!=0&&i<=len/2) diff-=p[i];
		if(i==0||(len+1)/2<i) diff+=p[i];
	}
	rep(i,len){
		ans=(ans+(tmp*p[i])%mod)%mod;
		tmp=(tmp+diff)%mod;
		diff+=(p[(i+1)%len]*2ll);
		diff-=p[(len/2+1+i)%len];
		diff-=p[((len+1)/2+1+i)%len];
		diff%=mod;
	}
	ans=(ans*((mod+2)/2))%mod;
	cout<<(ans%mod+mod)%mod<<"\n";
}
0