QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#578237 | #6397. Master of Both III | shstyle | TL | 968ms | 59576kb | C++20 | 2.4kb | 2024-09-20 17:34:32 | 2024-09-20 17:34:34 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=(1<<22)+10,mod=998244353;
typedef long long ll;
typedef pair<ll,ll> PII;
ll n,a[N];
int p[N];
int l[N],r[N];
mt19937 rnd(time(0));
ll mi[N];
ll dp[30][30];
ll ddp[N];
vector<PII> e[N];
bool tf[N];
int cal(int x){
int sum=0;
for(int i=0;i<n;i++){
if((x>>i)&1) sum++;
}
return sum;
}
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
a[0]=a[n];
memset(dp,0x3f,sizeof dp);
dp[0][0]=0;
// for(int i=0;i<n;i++) dp[0][i]=a[i];
for(int i=1;i<n;i++){
for(int j=0;j<n;j++){
int nw=(j+i)%n;
dp[i][nw]=min(dp[i-1][nw],dp[i-1][j]+a[i]);
}
// for(int j=0;j<n;j++) cout<<dp[i][j]<<" ";
// cout<<endl;
}
for(int i=0;i<n;i++){
a[i]=dp[n-1][i];
// cout<<a[i]<<endl;
}
memset(ddp,0x3f,sizeof ddp);
ddp[1]=0;
for(int i=1;i<(1<<n);i++){
vector<int> v;
for(int j=0;j<n;j++){
if((i>>j)&1) v.push_back(j);
}
// int nw=0;
for(int j=0;j<n;j++){
int nw=i;
for(auto k:v){
int xx=((k-j)%n+n)%n;
nw|=(1<<xx);
}
// nw|=i;
// cout<<i<<" "<<nw<<" "<<a[j]<<endl;
ddp[nw]=min(ddp[nw],ddp[i]+a[j]);
}
}
// queue<int> q;
// q.push(1);
// ddp[1]=0;
// tf[1]=1;
// while(q.size()){
// int t=q.front();
// q.pop();
// for(auto [j,w]:e[t]){
// ddp[j]=min(ddp[j],ddp[t]+w);
// // cout<<j<<" "<<w<<endl;
// if(!tf[j]){
// tf[j]=1;
// q.push(j);
// }
// }
// }
vector<PII> v;
for(int i=1;i<(1<<n);i++) v.push_back({cal(i),i});
sort(v.begin(),v.end(),greater<PII>());
// for(int i=1;i<(1<<n);i++) cout<<ddp[i]<<endl;
ddp[1]=0;
for(auto [c,i]:v){
for(int j=0;j<n;j++){
if(!((i>>j)&1)){
int nw=i^(1<<j);
if(nw!=0){
ddp[i]=min(ddp[i],ddp[nw]);
}
}
}
}
ll ans=0;
for(int i=1;i<(1<<n);i++){
// cout<<ddp[i]<<endl;;
ll res=ddp[i]%mod*i%mod;
ans=(ans+res)%mod;
}
cout<<ans<<endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 42580kb
input:
3 2 1 2
output:
45
result:
ok 1 number(s): "45"
Test #2:
score: 0
Accepted
time: 0ms
memory: 42612kb
input:
4 1919810 999999998 999999997 114114514
output:
152175989
result:
ok 1 number(s): "152175989"
Test #3:
score: 0
Accepted
time: 3ms
memory: 42628kb
input:
3 842160586 705327547 868243944
output:
78597628
result:
ok 1 number(s): "78597628"
Test #4:
score: 0
Accepted
time: 7ms
memory: 42516kb
input:
5 198327434 147392532 760837755 771109105 676721155
output:
751568230
result:
ok 1 number(s): "751568230"
Test #5:
score: 0
Accepted
time: 7ms
memory: 42672kb
input:
10 831766351 33417723 223739726 80131988 348810263 415437931 119999060 799356097 512022962 23197703
output:
308170104
result:
ok 1 number(s): "308170104"
Test #6:
score: 0
Accepted
time: 8ms
memory: 42960kb
input:
12 892965903 35291219 261557729 131390943 457251874 944586973 724767219 190756777 658923976 587806068 793999675 378390067
output:
964920873
result:
ok 1 number(s): "964920873"
Test #7:
score: 0
Accepted
time: 16ms
memory: 42640kb
input:
14 249132751 477356204 594343028 32906794 270726189 883801423 329535378 877124753 100792287 152414432 142520554 196476850 924736849 383197276
output:
796031217
result:
ok 1 number(s): "796031217"
Test #8:
score: 0
Accepted
time: 968ms
memory: 59576kb
input:
20 627365465 726842612 947302944 649244156 293375951 318148820 237155023 981487641 688151803 844901013 430309799 733877736 520864483 720366437 28746495 143052089 306590290 18586578 662663479 375430013
output:
179404754
result:
ok 1 number(s): "179404754"
Test #9:
score: -100
Time Limit Exceeded
input:
21 805448889 595358753 391340394 525130530 272725205 157594893 261894302 29704333 909085958 127205196 104570238 495437480 458664573 599968678 690908307 220500006 735062204 172834136 241126905 814694254 294923292
output:
260115885