結果

問題 No.3363 Two Closest Numbers
コンテスト
ユーザー fken_prime_57
提出日時 2025-11-17 11:14:45
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 179 ms / 2,000 ms
コード長 5,598 bytes
コンパイル時間 6,002 ms
コンパイル使用メモリ 335,060 KB
実行使用メモリ 10,348 KB
最終ジャッジ日時 2025-11-17 20:49:02
合計ジャッジ時間 9,387 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 59
権限があれば一括ダウンロードができます

ソースコード

diff #

//#define //_GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <deque>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
using vll=vector<ll>;
using vvll=vector<vector<ll>>;
using Graph=vvll;
using Edgegraph=vector<vector<pair<ll,ll>>>;
using vch=vector<char>;
using vvch=vector<vector<char>>;
using P=pair<ll,ll>;
using vP=vector<P>;
using tup=tuple<ll,ll,ll>;
using vbl=vector<bool>;
using vvbl=vector<vbl>;
using vs=vector<string>;
using vvs=vector<vs>;
using vd=vector<double>;
using vvd=vector<vd>;
using mint = atcoder::modint998244353;
const int infint = 1073741823;
const ll inf = 1LL << 60;
template <class T> inline bool chmax(T& a,T b){if (a<b){a=b;return 1;}return 0;}
template <class T> inline bool chmin(T& a,T b){if (a>b){a=b;return 1;}return 0;}
#define rep(i,x,lim) for(ll i = (x);i < (ll)(lim);i++)
#define rep2(j,x,lim) for(int j = (x);j < (int)(lim);j++)
const ll big=(1e+9)+7;
const ll big2=998244353;

ll dx[8]={1,-1,0,0,1,1,-1,-1};
ll dy[8]={0,0,1,-1,1,-1,1,-1};

int modpow(ll x,ll n,ll m){
    if(n==0) return 1%m;
    x=((x%m)+m)%m;
    if(n%2==0){
        ll r=modpow(x,n/2,m);
        return r*r%m;
    }
    else{
        ll r=modpow(x,n/2,m);
        return r*r%m*x%m;
    }
}
//pは素数でなければならない。
int revmod(ll x,ll p){return modpow(x,p-2,p);}
//99 のRを高速に求める
int modp(ll p,ll q){
    ll gc=gcd(p,q);
    p/=gc;q/=gc;
    ll rev=revmod(p,big2);
    return (rev*q)%big2;
}
//nCrを求める modbig2
int nCr(ll n,ll r){
     ll ans=1;
    rep(i,1,n+1) ans=(ans*i)%big2;
    rep(i,1,r+1) ans=(ans*modpow(i,big2-2,big2))%big2;
    rep(i,1,n-r+1) ans=(ans*modpow(i,big2-2,big2))%big2;
    return ans;
}
ll op(ll a,ll b){return max(a,b);}
ll opmin(ll a,ll b){return min(a,b);}
ll e(){return 0;}

template<class T> size_t HashCombine(const size_t seed,const T &v){
    return seed^(std::hash<T>()(v)+0x9e3779b9+(seed<<6)+(seed>>2));
}
/* pair用 */
template<class T,class S> struct std::hash<std::pair<T,S>>{
    size_t operator()(const std::pair<T,S> &keyval) const noexcept {
        return HashCombine(std::hash<T>()(keyval.first), keyval.second);
    }
};

const int MAX = 510000;
mint fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
    const int MOD = mint::mod();
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i;
        inv[i] = MOD - inv[MOD%i] * (MOD / i);
        finv[i] = finv[i - 1] * inv[i];
    }
}

// 二項係数計算
mint COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * finv[k] * finv[n - k];
}

int main(){
    ll N;
    cin >> N;
    vch c(N);
    vector<mint> pow10(200001);
    pow10[0]=1;
    rep(i,1,200001) pow10[i]=pow10[i-1]*10;
    rep(i,0,N) cin >> c[i];
    if(N%2==1){
        mint ans=0;
        sort(c.begin(),c.end());
        rep(i,0,N/2+1){
            ans+=(c[i]-'0')*pow10[N/2-i];
        }
        rep(i,0,N/2){
            ans-=(c[N-1-i]-'0')*pow10[N/2-1-i];
        }  
        cout << ans.val() << '\n';
        return 0;
    }
    else{
        ll ans=inf;
        map<ll,ll> mp;
        rep(i,0,N){
            mp[c[i]-'0']++;
        }
        vll cnt;
        rep(i,1,10){
            if(mp[i]%2==1) cnt.push_back(i);
        }
        ll t1=inf;
        rep(i,0,(1<<cnt.size())){
            if(__builtin_popcount(i)==cnt.size()/2){
                vll s1,s2;
                rep(j,0,cnt.size()){
                    if(i >> j & 1) s1.push_back(cnt[j]);
                    else s2.push_back(cnt[j]);
                }
                do{
                    do{
                        ll temp1=0;
                        ll temp2=0;
                        rep(j,0,s1.size()){
                            ll po10=pow10[j].val();
                            temp1+=s1[j]*po10;
                            temp2+=s2[j]*po10;
                        }
                        chmin(t1,abs(temp1-temp2));
                    }while(next_permutation(s2.begin(),s2.end()));
                    sort(s2.begin(),s2.end());
                }while(next_permutation(s1.begin(),s1.end()));
            }
        }
        rep(k,1,10){
            if(mp[k]>=2){
                cnt.push_back(k);
                cnt.push_back(k);
                rep(i,0,(1<<cnt.size())){
                    if(__builtin_popcount(i)==cnt.size()/2){
                        vll s1,s2;
                        rep(j,0,cnt.size()){
                            if(i >> j & 1) s1.push_back(cnt[j]);
                            else s2.push_back(cnt[j]);
                        }
                        sort(s1.begin(),s1.end());
                        sort(s2.begin(),s2.end());
                        do{
                            do{
                                ll temp1=0;
                                ll temp2=0;
                                rep(j,0,s1.size()){
                                    ll po10=pow10[j].val();
                                    temp1+=s1[j]*po10;
                                    temp2+=s2[j]*po10;
                                }
                                chmin(t1,abs(temp1-temp2));
                            }while(next_permutation(s2.begin(),s2.end()));
                            sort(s2.begin(),s2.end());
                        }while(next_permutation(s1.begin(),s1.end()));
                    }
                }
                cnt.pop_back();
                cnt.pop_back();
            }
        }
        cout << t1 << '\n';
    }
}
0