QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#283283 | #6194. Knapsack Problem | ucup-team022 | WA | 107ms | 3440kb | C++17 | 2.1kb | 2023-12-14 12:03:32 | 2023-12-14 12:03:32 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double db;
typedef pair<int,int> pr;
const int mod=998244353;
const db eps=1e-9;
ll b[20],ans;
int n,c[20],N,a[20],to[20],tc[20];
bool bad;
db f[9][9],tf[9][9];
void Getinv(){
for(int i=0;i<n;i++)f[i][i+n]=1;
for(int i=0;i<n;i++){
int p=i;
while(p<n&&fabs(f[p][i])<eps)p++;
if(p>=n)return bad=1,void();
for(int j=i;j<n+n;j++)swap(f[p][j],f[i][j]);
for(int j=i+1;j<n;j++){
db v=f[j][i]/f[i][i];
for(int k=i;k<n+n;k++)f[j][k]-=v*f[i][k];
}
}
for(int i=n-1;i>=0;i--){
db v=1/f[i][i];
for(int j=i;j<n+n;j++)f[i][j]*=v;
for(int j=i-1;j>=0;j--){
db x=f[j][i];
for(int k=i;k<n+n;k++){
f[j][k]-=x*f[i][k];
}
}
}
}
void dfs(int x){
if(x==N){
ll sum=0;
for(int i=0;i<N;i++)sum+=1ll*a[i]*b[i];
ans=max(ans,sum);
return ;
}
for(int i=0;;i++){
a[x]+=i;
bool ok=1;
for(int j=0;j<n;j++){
if((x+1)&(1<<j))tc[j]-=i;
if(tc[j]<0)ok=0;
}
if(!ok){
for(int j=0;j<n;j++)if((x+1)&(1<<j))tc[j]+=i;
a[x]-=i;
return ;
}
dfs(x+1);
for(int j=0;j<n;j++)if((x+1)&(1<<j))tc[j]+=i;
a[x]-=i;
}
}
void Solve(){
cin>>n,N=(1<<n)-1;
for(int i=0;i<N;i++)cin>>b[i];
for(int i=0;i<n;i++)cin>>c[i];
ans=0;
for(int i=0;i<(1<<N);i++){
if(__builtin_popcount(i)!=N-n)continue;
memset(f,0,sizeof(f)),bad=0;
int id[20]={0},tot=0;
for(int j=0;j<N;j++){
if(i&(1<<j))continue;
to[tot]=j,id[j]=(tot++);
for(int k=0;k<n;k++){
f[k][id[j]]=((j+1)>>k)%2;
tf[k][id[j]]=f[k][id[j]];
}
}
Getinv();
if(bad)continue;
memset(a,0,sizeof(a));
bool nd=0;
for(int i=0;i<n;i++){
db v=0;
for(int j=0;j<n;j++)v+=c[j]*f[i][j+n];
if(fabsl(v-roundl(v))<eps)a[to[i]]=roundl(v);
else a[to[i]]=floorl(v),nd=1;
if(a[to[i]]<0)bad=1;
}
if(bad)continue;
//if(nd)for(int i=0;i<N;i++)a[i]=max(a[i]-1,0);
for(int i=0;i<n;i++){
tc[i]=c[i];
for(int j=0;j<N;j++)if((j+1)&(1<<i))tc[i]-=a[j];
}
dfs(0);
}
cout<<ans<<'\n';
}
int main(){
int t;
cin>>t;
while(t--)Solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3440kb
input:
3 2 1 2 4 4 5 3 3226252 19270256 2430652 1187613 12496062 10286165 17494834 24 85 34 4 901133 6693218 13245489 14740234 16186210 11382783 19277321 3855635 16523184 10168517 16195031 971041 10303441 8395899 11618555 529321239 218214127 92942310 207467810
output:
18 1949753378 7832404777567179
result:
ok 3 number(s): "18 1949753378 7832404777567179"
Test #2:
score: 0
Accepted
time: 107ms
memory: 3332kb
input:
100 4 4488409 9842893 14331290 11289097 15777479 21131954 25620334 6146092 10634508 15988956 20477387 17435190 21923584 27278027 31766493 200477197 258791439 590664017 982949628 4 5484152 19851747 25335961 6555937 12039831 26407861 31891996 10897184 16381166 30749333 36233145 17453055 22937114 37304...
output:
16156444320083421 24565535429631234 29418802996321901 30685727712782520 21026118053726857 27272339796311010 28831028447377015 34577176834491128 9058894962996375 21525085254465501 6657186892229297 16750628911275484 11217846865301187 12674726744043780 33873234230125448 3890237279573366 319038768614465...
result:
ok 100 numbers
Test #3:
score: 0
Accepted
time: 106ms
memory: 3440kb
input:
100 4 11618334 18295966 29914477 5116382 16734869 23412289 35030567 15002473 26620994 33298395 44916782 20118655 31737127 38414456 50032872 52573649 733715025 105771527 204824011 4 2142433 13679527 15822005 7304783 9447231 20984363 23126840 6271380 8413827 19950941 22093291 13576168 15718643 2725577...
output:
17648887427676796 21625331519839488 5123483163674640 20332898488476516 10882410276737710 25799406290369942 9021794681785918 14079661025657823 19651479225495798 19108340996997031 7502197529195960 14859286025000990 26534486448012504 21196964793845212 17222108748663918 19112873745810973 150116234459639...
result:
ok 100 numbers
Test #4:
score: -100
Wrong Answer
time: 104ms
memory: 3440kb
input:
100 4 19912840 5584811 25497404 10173442 30086113 15758284 35670730 10055784 29968204 15640365 35553036 20228929 40141706 25813818 45726645 199637398 648830099 620879036 721665690 4 7571016 7507237 15078004 19283701 26854580 26790914 34361725 2809630 10380467 10316661 17887650 22093187 29664080 2960...
output:
21172351446279597 12216316207781859 31685774482329793 36066732654517313 19340822375673782 17094361082328640 19075803268324489 23946411427126244 15750520513128720 10839993372177143 28038252774564427 12098359408203383 16661717061725678 16141878677697603 10422881545500853 23195259150880456 161145733965...
result:
wrong answer 98th numbers differ - expected: '24656983842107825', found: '24656983842107818'