1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| #include<bits/stdc++.h> #define inf 2147483647 using namespace std; int t,d[3],ans,cnt,u[3][6],td[3],a[6][4]; const int v[]={100,50,20,10,5,1}; bool check(int x,int y){ switch(y){ case 5:return true; case 4:return x%5==0; case 2:return x%10==0; case 1:return x%50==0; case 0:return x%100==0; } } void dfs(int x){ if(x<0){if(td[0]==td[1]&&td[1]==td[2])ans=min(ans,cnt);return;} if(cnt>=ans)return; if(!check(td[0]-td[1],x)||!check(td[1]-td[2],x))return; for(int i=0;i<3;i++){ int nex=(i+1)%3,pre=(i+2)%3; for(int j=0;j<=u[i][x];j++) for(int k=0;k<=u[i][x]-j;k++){ td[i]-=j*v[x],td[pre]+=k*v[x],cnt+=j+k; dfs(x-1); cnt-=j+k,td[i]+=j*v[x],td[pre]-=k*v[x]; } for(int j=0;j<=u[nex][x];j++) for(int k=0;k<=u[pre][x];k++){ td[i]+=j*v[x],td[pre]-=k*v[x],cnt+=j+k; dfs(x-1); cnt-=j+k,td[i]-=j*v[x],td[pre]+=k*v[x]; } } } int main(){ scanf ("%d",&t); while(t--){ ans=inf; for(int i=0;i<3;i++)scanf ("%d",&td[i]),d[i]=td[i]; for(int i=0;i<3;i++) for(int j=0;j<6;j++)scanf ("%d",&u[i][j]); dfs(5); if(ans==inf)puts("impossible"); else printf("%d\n",ans); } return 0; }
|