結果

問題 No.3363 Two Closest Numbers
コンテスト
ユーザー こめだわら
提出日時 2025-11-17 21:58:05
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,141 bytes
コンパイル時間 3,174 ms
コンパイル使用メモリ 287,652 KB
実行使用メモリ 7,852 KB
最終ジャッジ日時 2025-11-17 21:58:12
合計ジャッジ時間 6,310 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 53 WA * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;

#define rep(i,n) for(ll i=0;i<n;++i)
#define all(a) (a).begin(),(a).end()
ll intpow(ll a, ll b){ ll ans = 1; while(b){ if(b & 1) ans *= a; a *= a; b /= 2; } return ans; }
ll modpow(ll a, ll b, ll p){ ll ans = 1; while(b){ if(b & 1) (ans *= a) %= p; (a *= a) %= p; b /= 2; } return ans; }
template<class T> T div_floor(T a, T b) { return a / b - ((a ^ b) < 0 && a % b); }
template<class T> T div_ceil(T a, T b) { return a / b + ((a ^ b) > 0 && a % b); }
template <typename T, typename U> inline bool chmin(T &x, U y) { return (y < x) ? (x = y, true) : false; }
template <typename T, typename U> inline bool chmax(T &x, U y) { return (x < y) ? (x = y, true) : false; }

template<typename T>
ostream &operator<<(ostream &os, const vector<T> &a){
	if (a.empty()) return os;
	os << a.front();
	for (auto e : a | views::drop(1)){
		os << ' ' << e;
	}
	return os;
}

void dump(auto ...vs){
	((cout << vs << ' '), ...) << endl;
}



void solve() {
	const ll mod=998244353;
	ll N;
	cin>>N;
	vector<ll> C(N);
	rep(i,N)cin>>C[i];
	sort(all(C));
	if (N%2==1){
		ll s0=0;
		ll s1=0;
		rep(i,N/2+1){
			s0*=10;
			s0+=C[i];
			s0%=mod;
		}
		rep(i,N/2){
			s1*=10;
			s1+=C[N-i-1];
			s1%=mod;
		}
		s0-=s1;
		s0%=mod;
		s0+=mod;
		s0%=mod;
		cout<<s0<<'\n';
		return;
	}
	else{
		vector<ll> cnt(9,0);
		rep(i,N){
			cnt[C[i]-1]++;
		}
		rep(i,9){
			cnt[i]%=2;
		}
		if (count(all(cnt),1)==0){
			cout<<0<<'\n';
			return;
		}
		ll ans=1e18;
		rep(bi,1<<9){
			vector<ll> l0,l1;
			rep(i,9){
				if (cnt[i]>0){
					if (bi>>i&1)l0.push_back(i);
					else l1.push_back(i);
				}
			}
			if (l0.size()!=l1.size()){
				continue;
			}
			do{
				do{
					ll a0=0;
					ll a1=0;
					for (ll a:l0){
						a0*=10;
						a0+=a;
					}
					for (ll a:l1){
						a1*=10;
						a1+=a;
					}
					chmin(ans,abs(a0-a1));
				}while (next_permutation(all(l1)));
			} while (next_permutation(all(l0)));
		}
		cout<<ans%mod<<'\n';
	}
	return;
}


int main() {
	cin.tie(0)->sync_with_stdio(0);
	ll T=1;
	while (T--){
		solve();
	}
	return 0;
}
0