QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#420082 | #8349. 零和 | Bronya | 100 ✓ | 596ms | 12140kb | C++14 | 2.3kb | 2024-05-24 14:30:50 | 2024-05-24 14:30:50 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
mt19937 rnd(114514);
int dist[1000005],last[1000005];
const int tt=2000;
vector<pair<int,int> >zy[tt+5];
vector<int>sav[tt+5];
pair<int,int>val[tt+5];
int f[2][105];
bool check(vector<int>S){
int sum=0;
for(auto x:S)sum+=x;
return sum!=0&&sum!=-1;
}
pair<int,int> calc(vector<int>S){
memset(f,0,sizeof(f));f[0][50]=1;
for(auto x:S){
for(int j=0;j<=100;j++){
f[1][j]+=f[0][j];
if(j+x>=0&&j+x<=100)f[1][j+x]+=f[0][j];
}
for(int j=0;j<=100;j++)f[0][j]=f[1][j],f[1][j]=0;
}
return {f[0][50],f[0][49]};
}
vector<int>ans;
void Solve(int x){
if(!x)return;
int u=last[x],la=x-val[u].second;
// cerr<<val[u].first<<" "<<val[u].second <<" "<<sav[u].size()<<endl;
if(x==val[u].first){
ans=sav[u];
return;
}
if(val[u].first)la/=val[u].first;
Solve(la);
long long X=0,Y=0;
for(auto x:ans){
if(x>0)X+=x;
else Y+=x;
}
long long S=(X>-Y?X:Y);//cerr<<S<<endl;
for(auto x:sav[u])ans.push_back(x*S);
// for(auto x:ans)cerr<<x<<" ";cerr<<endl;
}
int main(){
int fr=0;const int N=1e6;
for(int i=1;i<=N;i++)dist[i]=31;
for(int t=1;t<=tt;t++){
int len=rnd()%12+3;
vector<int>S;
S.resize(len);
for(int i=0;i<len;i++)S[i]=((rnd()&1)?(rnd()%3+1):-(int)(rnd()%3+1));
while(!check(S))for(int i=0;i<len;i++)S[i]=((rnd()&1)?(rnd()%3+1):-(int)(rnd()%3+1));
auto [x,y]=calc(S);sav[t]=S;val[t]={x,y};
zy[x].push_back({y,t});fr=max(fr,x);
if(len<dist[x])dist[x]=len,last[x]=t;
}
for(int i=0;i<=fr;i++)sort(zy[i].begin(),zy[i].end());
for(int i=1;i<=N;i++){
if(dist[i]>30){cerr<<i<<endl;break;}
for(int u=0;u<=fr;u++){
if(i*u>N)break;
for(auto [x,y]:zy[u]){
if(dist[i]+x>N)break;
int v=i*u+x;
if(v>N)continue;
if(dist[v]>dist[i]+sav[y].size())dist[v]=dist[i]+sav[y].size(),last[v]=y;
}
}
}
int T;
scanf("%d",&T);
while(T--){
int x;
scanf("%d",&x);ans.clear();
Solve(x);
printf("%d\n",(int)ans.size());
for(auto x:ans)printf("%d ",x);putchar('\n');
}
return 0;
}
详细
Subtask #1:
score: 15
Accepted
Test #1:
score: 15
Accepted
time: 571ms
memory: 12136kb
input:
1000 4 10 9 7 5 9 4 4 10 8 2 10 8 8 4 2 10 4 9 9 8 9 6 9 5 6 9 1 2 5 8 4 4 8 2 2 7 6 8 2 3 1 1 3 1 10 6 3 4 1 6 7 1 5 7 7 3 6 3 3 9 6 9 5 7 5 9 8 3 5 2 4 9 3 10 2 10 10 3 1 9 4 8 9 6 6 6 7 7 7 10 1 2 3 2 6 6 8 1 3 10 8 5 7 8 2 7 9 3 1 9 9 1 10 6 7 9 9 7 9 1 2 7 3 1 5 4 9 2 6 4 9 9 10 4 4 5 9 10 2 2 ...
output:
4 -2 1 1 1 5 2 -2 -2 2 -2 6 1 -3 3 1 -1 2 6 2 1 -1 -3 -2 -2 5 2 1 -3 -1 -1 6 1 -3 3 1 -1 2 4 -2 1 1 1 4 -2 1 1 1 5 2 -2 -2 2 -2 6 -2 -3 2 3 2 2 3 2 -3 3 5 2 -2 -2 2 -2 6 -2 -3 2 3 2 2 6 -2 -3 2 3 2 2 4 -2 1 1 1 3 2 -3 3 5 2 -2 -2 2 -2 4 -2 1 1 1 6 1 -3 3 1 -1 2 6 1 -3 3 1 -1 2 6 ...
result:
ok Accepted
Subtask #2:
score: 15
Accepted
Dependency #1:
100%
Accepted
Test #2:
score: 15
Accepted
time: 596ms
memory: 11876kb
input:
1000 42 56 95 81 26 68 42 3 83 6 37 52 5 82 59 10 43 90 44 55 83 8 43 14 94 84 92 61 10 98 46 58 43 49 81 29 36 3 93 38 70 86 54 89 75 28 87 74 56 22 47 89 90 26 76 80 55 90 60 32 58 67 70 94 52 95 78 70 39 40 69 99 18 24 32 33 23 95 19 80 98 59 79 57 69 35 25 72 27 72 57 13 55 37 90 59 53 10 2 84 4...
output:
9 -1 -3 -2 1 3 -2 -2 1 -1 9 -1 -1 3 -2 2 3 -2 3 -3 10 -3 -3 1 -2 2 3 -1 3 1 3 10 -2 -2 3 -2 -3 -1 1 -3 2 1 7 1 2 -1 1 1 -1 -2 10 2 -3 3 -10 5 5 5 5 5 -5 9 -1 -3 -2 1 3 -2 -2 1 -1 3 3 3 -3 10 -1 -3 2 1 1 3 3 1 -2 1 5 1 3 -3 -1 3 9 2 3 2 -3 -1 -1 3 3 -1 9 -2 1 -2 2 2 -1 -3 -3 2 5 2 1 -3 -1...
result:
ok Accepted
Subtask #3:
score: 15
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Test #3:
score: 15
Accepted
time: 568ms
memory: 11884kb
input:
1000 1458 1427 1927 1845 764 1218 986 13 1479 494 1405 1711 88 1701 536 1286 1735 711 63 439 469 1726 739 1496 1055 1775 1946 860 134 742 1296 188 373 1156 230 1366 1123 1601 1015 481 1177 250 723 1265 1254 416 329 489 661 305 1895 1057 1676 676 1972 1031 1721 1483 1046 387 1522 1936 1778 1526 27 11...
output:
14 1 -2 -1 1 1 -1 1 3 -3 3 1 2 -2 1 16 -3 -2 2 -1 3 -2 2 -1 3 -3 2 -3 30 -15 -15 -15 17 1 3 -1 3 -1 1 2 -3 3 -2 -2 -13 -26 39 26 -26 26 17 1 3 -3 -1 3 14 14 21 -14 -21 -21 -14 7 7 -7 -21 -21 15 -2 1 1 1 3 9 9 -6 6 3 -9 -6 9 -3 3 15 1 -2 2 3 2 -2 -2 -1 1 2 -2 2 39 39 -39 16 2 -2 -2 2 -2 18 -18 ...
result:
ok Accepted
Subtask #4:
score: 15
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Test #4:
score: 15
Accepted
time: 587ms
memory: 11844kb
input:
1000 8882 3890 5276 2293 7017 8729 4107 8330 9804 3437 7125 8266 505 902 10 4246 46 4186 7869 6510 3510 681 8065 2419 1575 2776 2762 5567 3150 5914 4695 6152 2758 5157 1407 5964 8947 8203 7821 3411 3115 8356 8240 3506 1618 1108 4024 5791 2644 3315 3577 9512 5256 3454 7378 9383 4904 6094 8422 7100 71...
output:
19 -2 -3 2 3 2 2 -9 9 18 -9 -27 18 -9 18 -9 -9 18 -9 18 18 -1 2 2 2 1 -1 -2 -21 7 21 21 -7 -7 -21 14 -7 21 -14 19 3 3 -2 -2 -1 -2 -2 -18 -27 9 9 9 18 18 18 9 -18 27 -9 18 1 -3 -1 -1 1 1 10 10 10 10 -15 10 -10 5 5 15 -10 10 19 -1 2 2 -1 1 -2 1 -2 3 -18 27 -9 27 -18 9 27 -18 9 -27 19 1 -2 -1 2 -1...
result:
ok Accepted
Subtask #5:
score: 15
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Test #5:
score: 15
Accepted
time: 581ms
memory: 11880kb
input:
1000 96341 68443 37369 95002 5050 28444 863 40283 5948 58559 90074 93374 20434 66458 42427 81860 19058 44240 61491 29065 18196 59230 70644 48078 44160 52618 31944 3993 66326 63711 62648 80986 90080 12593 36140 908 97838 9909 62779 18661 92388 86436 83715 49365 10604 17467 65898 31682 64543 37267 202...
output:
23 1 1 -2 -1 -2 1 3 -2 2 -2 -1 -30 30 10 20 10 -20 -30 -30 10 10 20 -30 24 1 3 1 -3 1 -3 1 3 -1 -3 3 -1 3 -48 32 -16 48 -16 16 48 -48 32 48 48 22 3 3 -3 6 6 12 -6 -12 -6 -18 -18 12 -12 18 -18 12 12 186 279 -279 -186 -279 25 2 1 -3 -1 -1 -10 -5 5 15 10 -10 -10 -15 10 -5 5 60 -60 -180 60 -60 60 -18...
result:
ok Accepted
Subtask #6:
score: 25
Accepted
Dependency #1:
100%
Accepted
Dependency #2:
100%
Accepted
Dependency #3:
100%
Accepted
Dependency #4:
100%
Accepted
Dependency #5:
100%
Accepted
Test #6:
score: 25
Accepted
time: 573ms
memory: 12140kb
input:
1000 68691 709038 403562 87832 620545 194650 877186 754984 970483 972817 576860 95921 320214 328151 977559 803543 893740 413219 868313 153241 553644 186975 758312 166278 963660 596237 523965 976575 92819 743171 220709 536964 847991 151853 256090 912309 286952 609500 293131 143200 850426 155262 98688...
output:
24 2 -3 3 10 5 -5 -10 5 -10 -5 10 15 -15 15 130 -65 65 130 195 -65 195 -195 -130 -130 29 2 1 -3 -1 -1 5 10 5 5 -5 -5 5 15 5 5 5 -15 126 -189 -63 -189 -63 126 126 -189 189 126 -63 -126 25 1 -1 1 -2 -3 1 2 1 2 -1 -2 2 20 20 30 -30 -10 -30 20 -20 10 10 10 -10 20 23 1 1 -3 3 -3 -1 -1 -3 3 22 -33 33 2...
result:
ok Accepted