QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#203454 | #6194. Knapsack Problem | ucup-team870 | WA | 3ms | 3632kb | C++14 | 4.5kb | 2023-10-06 17:33:43 | 2023-10-06 17:33:44 |
Judging History
answer
#include <bits/stdc++.h>
#define pb push_back
#define ob pop_back
#define For(i,l,r) for(int i=l; i<=r; i++)
#define per(i,r,l) for(int i=r; i>=l; i--)
#define IOS {cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);}
using namespace std;
typedef long long ll;
int n,k;
ll c[17],ans[17],an[17][17],a[4][17],tot,su[5];
vector<int> q;
ll flo(ll y,ll x){
if (y<0) y=-y,x=-x;
if (x>=0) return x/y;
else return x/y-(x%y!=0);
}
ll cei(ll y,ll x){
if (y<0) y=-y,x=-x;
if (x<=0) return x/y;
return (x-1)/y+1;
}
void eq(int now,int sz){
memcpy(an[now],a[sz],sizeof(an[now]));
//change
ll k2=a[sz][now];
For(i,0,k-1) if (i!=sz){
For(j,0,n) if (j!=now){
ll k1=a[i][now],y=a[i][j],z=a[sz][j];
a[i][j]=k2*y-z*k1;
}
a[i][now]=0;
}
//change self
if (k2<0)
For(j,0,n) if (j!=now) a[sz][j]*=-1;
a[sz][now]=0;
}
void check(){
ans[0]=1; //++cctt;
//for(auto i:q) cout<<' '<<i<<' '; cout<<endl;
for(int i=q.size()-1;i>=0;--i){
int now=q[i]; ll tmp=0;
For(j,0,n) if (j!=now) tmp+=ans[j]*an[now][j];
//check illegal
if (tmp%an[now][now]!=0) return;
tmp=tmp/(-an[now][now]);
if (tmp<0) {/*printf("error\n");*/ return;}
ans[now]=tmp;
}
ll sum=0;
For(i,1,n) if (ans[i]<0) return;
//For(i,1,n) printf("%lld ",ans[i]); printf("\n");
//check sum
For(i,0,k-1){
ll tmp=0;
For(j,1,n) if (j&(1<<i)) tmp+=ans[j];
if (tmp!=su[i]) {/*printf("error2\n");*/return;}
}
For(i,1,n) sum+=ans[i]*c[i];
tot=max(tot,sum);
}
void dfs(int cnt,int las){
//check 0
bool flag=1;
For(i,0,k-1) if (a[i][0]>0) flag=0;
if (flag) memset(ans,0,sizeof(ans)),check();
if (cnt==1){
ll mi=0,ma=1e9; int now=0;
For(i,0,k-1)
For(j,1,n) if (a[i][j]!=0) {now=j; break;}
//cal
For(i,0,k-1){
ll tmp,k1=a[i][now],k2=a[i][0];
//k=0
if (k1==0){
if (k2>0) return; // illegal
}
//!=0
else if (k1>0){
tmp=flo(k1,-k2),ma=min(ma,tmp);
}
else tmp=cei(k1,-k2),mi=max(ma,tmp);
}
//illegal
if (mi>ma) return;
memset(ans,0,sizeof(ans));
ans[now]=mi; check(); ans[now]=ma; check();
return;
}
ll tmp[4][17]; memcpy(tmp,a,sizeof(a));
For(i,las,k-1){
int now=0;
For(j,1,n) if (a[i][j]!=0) {now=j; break;}
if (now){
eq(now,i); q.pb(now); dfs(cnt-1,i);
q.ob(); memcpy(a,tmp,sizeof(a));
}
//illegal
else if (a[i][0]>0) return;
}
return;
}
int main(){
int _; cin>>_;
while(_--){
cin>>k; n=(1<<k)-1;
For(i,1,n) cin>>c[i];
For(i,0,k-1){
int x; cin>>x; su[i]=x;
a[i][0]=-x;
For(j,1,n) if (j&(1<<i)) a[i][j]=1;
}
For(i,0,k-1) eq(1<<i,i),q.pb(1<<i);
//clear 0
int ct=n-k;
For(j,1,n) if (__builtin_popcount(j)>=2){
ll tt=c[j];
For(i,0,k-1) if (j&(1<<i)) tt-=c[1<<i];
if (tt<=0){
For(i,0,k-1) a[i][j]=0;
--ct; //cout<<' '<<j<<endl;
}
}
//For(i,0,k-1) {For(j,0,n) printf("%lld ",a[i][j]); printf("\n");}
//clear
//opt
/*
ll tmp[4][17]; memcpy(tmp,a,sizeof(a));
For(o,0,k-1){
For(i,0,k-1) if (i!=o){
//find now;
int now=0;
For(j,1,n) if (a[i][j]!=0) {now=j; break;}
eq(now,i); q.pb(now);
}
dfs(n-k-k+1); cout<<"##";
//clear
For(i,1,k-1) q.ob(); memcpy(a,tmp,sizeof(a));
}*/
dfs(ct,0);
cout<<tot<<endl;
//cout<<cctt<<endl;
memset(a,0,sizeof(a)); memset(an,0,sizeof(an));
tot=0; q.clear();
}
}
/*
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
4
4488409 9842893 14331290 11289097 15777479 21131954 25620334 6146092 10634508 15988956 20477387 17435190 21923584 27278027 31766493
200477197 258791439 590664017 982949628
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3600kb
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: -100
Wrong Answer
time: 3ms
memory: 3632kb
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 30685726224479370 21026118053726857 27272339796311010 28831028447377015 34577176478351368 9058894962996375 21525085254465501 6657186892229297 16750628911275484 11217846865301187 12674726744043780 33873234230125448 3890237279573366 319038768614465...
result:
wrong answer 4th numbers differ - expected: '30685727712782520', found: '30685726224479370'