QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#203352#6194. Knapsack Problemucup-team870WA 11ms3712kbC++144.0kb2023-10-06 16:53:172023-10-06 16:53:17

Judging History

你现在查看的是最新测评结果

  • [2023-10-06 16:53:17]
  • 评测
  • 测评结果:WA
  • 用时:11ms
  • 内存:3712kb
  • [2023-10-06 16:53:17]
  • 提交

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;
    //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) 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);
        //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(n-k,0);
        cout<<tot<<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
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3712kb

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: 11ms
memory: 3596kb

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:

16156493053373584
24565535429631234
29418802996321901
30685727712782520
21026118053726857
27272339796311010
28831028447377015
34577176478351368
9058907989829949
21525085254465501
6657226903539432
16750628911275484
11217846865301187
12674726744043780
33873426414376088
3890244742122278
319038768614465...

result:

wrong answer 1st numbers differ - expected: '16156444320083421', found: '16156493053373584'