QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#136518 | #6397. Master of Both III | ammardab3an# | WA | 7ms | 38232kb | C++20 | 3.1kb | 2023-08-08 23:24:08 | 2023-08-08 23:24:11 |
Judging History
answer
// By AmmarDab3an
#include "bits/stdc++.h"
using namespace std;
#define int int64_t
#define ll int64_t
// typedef unsigned int uint;
// typedef long long int ll;
// typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> iii;
typedef pair<ll, pll> lll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pll> vpll;
#define endl '\n'
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define freopenI freopen("input.txt", "r", stdin);
#define freopenO freopen("output.txt", "w", stdout);
const int INF = 0x3f3f3f3f;
const ll INFLL = 0x3f3f3f3f3f3f3f3f;
const int MOD = 998244353; // 1e9 + 7;
const double EPS = 1e-9;
const double PI = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rand(int x, int y) {
return uniform_int_distribution<int>(x, y)(rng);
}
int mul(int a, int b){
int ret = (1ll * (a%MOD) * (b%MOD)) % MOD;
return (ret+MOD)%MOD;
}
int add(int a, int b){
int ret = (1ll * (a%MOD) + (b%MOD)) % MOD;
return (ret+MOD)%MOD;
}
int pow_exp(int n, int p){
if(!p) return 1;
if(p&1) return mul(n, pow_exp(n, p-1));
int tmp = pow_exp(n, p/2);
return mul(tmp, tmp);
}
int inv(int x){
return pow_exp(x, MOD-2);
}
const int MAX = 2e5 + 10;
const int NMAX = 2e5 + 10;
const int MMAX = 2e5 + 10;
const int LOG_MAX = ceil(log2(double(NMAX)));
const int BLOCK = ceil(sqrt(double(NMAX)));
int fac[NMAX], ifac[NMAX];
void init(){
fac[0] = 1;
for(int i = 1; i < NMAX; i++){
fac[i] = mul(fac[i-1], i);
}
ifac[NMAX-1] = inv(fac[NMAX-1]);
for(int i = NMAX-2; i >= 0; i--){
ifac[i] = mul(ifac[i+1], i+1);
}
}
int choose(int n, int c){
assert(n >= c);
return mul(fac[n], mul(ifac[c], ifac[n-c]));
}
int n;
int arr[55];
int pow2[55];
int dist[1<<22];
int32_t main(){
fastIO;
cin >> n;
for(int i = 0; i < n; i++){
cin >> arr[i];
}
pow2[0] = 1;
for(int i = 1; i < n; i++){
pow2[i] = (pow2[i-1] * 2) % MOD;
}
memset(dist, INF, sizeof dist);
priority_queue<pii, vpii, greater<pii>> que;
dist[1] = 0;
que.push({0, 1});
while(!que.empty()){
auto [cur_d, msk] = que.top();
que.pop();
if(cur_d > dist[msk]) continue;
// cout << bitset<4>(msk) << ' ' << dist[msk] << endl;
for(int i = 0; i < n; i++) if((msk>>i)&1)
for(int j = 0; j < n; j++) {
int nmsk = msk ^ (1<<j);
int d = i > j ? i-j : n-(j-i);
assert(d > 0);
assert((j+d)%n == i);
d = arr[d];
if(dist[nmsk] > cur_d+d){
dist[nmsk] = cur_d+d;
que.push({cur_d+d, nmsk});
}
}
}
int ans = 0;
for(int msk = 0; msk < (1<<n); msk++){
int cur_msk = 1;
int sm_pow2 = 0;
for(int i = 0; i < n; i++) if((msk>>i)&1){
cur_msk |= 1 << i;
sm_pow2 = add(sm_pow2, pow2[i]);
}
// cout << bitset<4>(cur_msk) << ' ' << dist[cur_msk] << ' ' << sm_pow2 << endl;
int cans = mul(dist[cur_msk], sm_pow2);
ans = add(ans, cans);
}
cout << ans << endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 7ms
memory: 37220kb
input:
3 2 1 2
output:
45
result:
ok 1 number(s): "45"
Test #2:
score: 0
Accepted
time: 2ms
memory: 36704kb
input:
4 1919810 999999998 999999997 114114514
output:
152175989
result:
ok 1 number(s): "152175989"
Test #3:
score: 0
Accepted
time: 3ms
memory: 37804kb
input:
3 842160586 705327547 868243944
output:
78597628
result:
ok 1 number(s): "78597628"
Test #4:
score: 0
Accepted
time: 3ms
memory: 37376kb
input:
5 198327434 147392532 760837755 771109105 676721155
output:
751568230
result:
ok 1 number(s): "751568230"
Test #5:
score: -100
Wrong Answer
time: 1ms
memory: 38232kb
input:
10 831766351 33417723 223739726 80131988 348810263 415437931 119999060 799356097 512022962 23197703
output:
458771697
result:
wrong answer 1st numbers differ - expected: '308170104', found: '458771697'