Bzoj 1021 [shoi2008]debt Cycle of debt

Source: Internet
Author: User
Tags greatest common divisor

1021: [Shoi2008]debt cycle of debt time limit:1 Sec Memory limit:162 MB
submit:694 solved:356
[Submit] [Status] [Discuss] Description

Alice, Bob and Cynthia always fret over their troubled debts, and one day they decided to sit down together to solve the problem. However, it is a very troublesome matter to identify the authenticity of banknotes, so they decide to exchange cash as little as possible when they are repaid. For example, Alice owes Bob 10 Yuan, while Cynthia and the two don't owe each other. Now suppose Alice has only one 50 yuan, Bob has 3 10 Yuan and 10 Zhang 1 yuan, Cynthia 3 Yuan 20. A more straightforward approach is that Alice will give $50 to Bob, and Bob will find his money to Alice, so there will be a total of 14 banknotes exchanged. But this is not the best approach, the best thing is: Alice put 50 to Cynthia,cynthia and then two 20 to Alice, another 20 to Bob, and Bob put a 10 to C, at this time only 5 banknotes were exchanged. It wasn't long before they found out that it was a tricky problem, so they found a math-savvy you to solve the problem.

Input

The first line of input includes three integers: x1, x2, X3 ( -1,000≤x1,x2,x3≤1,000), where X1 represents the Money Alice owes Bob (if X1 is negative, it means Bob owes Alice's money) X2 on behalf of Bob owes Cynthia Money (if X2 is a negative number, stating that Cynthia owes Bob Money) X3 represents Cynthia owes Alice's Money (if X3 is negative, stating that Alice owes Cynthia Money) Next there are three lines, each line includes 6 natural numbers: A100,A50,A20,A10,A5,A1 b100,b50,b20,b10,b5,b1 c100,c50,c20,c10,c5,c1 A100 says Alice has a number of $100 banknotes, and B50 represents Bob's 50-dollar bill Count, And so on In addition, we guarantee a a10+a5+a1≤30,b10+b5+b1≤30,c10+c5+c1≤30, and the total value of all three banknotes will not exceed 1,000.

Output

If the debt can be repaid, the output needs to exchange the minimum number of banknotes, and if not, output "impossible" (note that the words are all lowercase, not quoted when outputting to a file).

Sample InputEnter a
10 0 0
0 1 0 0 0 0
0 0 0 3 0 10
0 0 3 0 0 0
Input Two
-10-10-10
0 0 0 0 0 0
0 0 0 0 0 0
0 0 0 0 0 0Sample Outputoutput A
5
Output two
0HINT

For 100% of data, X1, x2, x3≤|1,000|.

Source

One eye DP. F[i][j][k] said to the first denomination, there is a J yuan, the second person and K yuan money of the minimum exchange number. Then a backpack ran ...

However, teacher Chen's optimization silently hope: Nest Meng can be found from small to large considering the words, if the number of the greatest common divisor mod value and the target state is different

Then there's nothing to say.

So you can ignore it decisively.

and then consider 100,50,20.

They.. Seems to be a multiples of 10.

So just divide it by 10 and then think about it. You can quickly n more to avoid the tle.

Unfortunately I am lazy person qaq ...

The code is so ugly ....

1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5#include <queue>6#include <cstring>7 #definePAU Putchar (")8 #defineENT Putchar (' \ n ')9 using namespacestd;Ten Const intmaxn= ++Ten; One intx1,x2,x3,dp[2][maxn][maxn],cnt[3][6],has[3],msum[6],money[Ten]={1,5,Ten, -, -, -}; AInlineintRead () { -     intx=0, sig=1;CharCh=GetChar (); -      while(!isdigit (CH)) {if(ch=='-') sig=-1; ch=GetChar ();} the      while(IsDigit (CH)) x=Ten*x+ch-'0', ch=GetChar (); -     returnx*=Sig; - } -InlinevoidWriteintx) { +     if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; -     intlen=0, buf[ the]; while(x) buf[len++]=x%Ten, x/=Ten; +      for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return; A } at voidinit () { -X1=read (); X2=read (); x3=read (); -      for(intI=0; i<=2; i++) -          for(intj=5; j>=0; j--){ -Cnt[i][j]=read (); has[i]+=cnt[i][j]*money[j];msum[j]+=Cnt[i][j]; -         } in     return; - } to voidWork () { +     intall=has[0]+has[1]+has[2],t=1; -memset (dp[0],-1,sizeof(dp[0])); thedp[0][has[0]][has[1]]=0; *      for(intI=0; i<=5; i++){ $t^=1; Memset (dp[t^1],-1,sizeof(dp[t^1]));Panax Notoginseng          for(intj=0; j<=all;j++){ -              for(intk=0; j+k<=all;k++){ the                 if(dp[t][j][k]>=0){ +                      for(intA=0; a<=msum[i];a++){ A                          for(intb=0; b+a<=msum[i];b++){ the                             intpa=j+money[i]* (a-cnt[0][i]); +                             intpb=k+money[i]* (b-cnt[1][i]); -                             if(pa>=0&&pb>=0&&pa+pb<=All ) { $                                 intDelta=abs (cnt[0][I]-A) +abs (cnt[1][i]-b) +abs (msum[i]-a-b-cnt[2][i]); $Delta/=2; -                                 if(dp[t^1][pa][pb]==-1) dp[t^1][pa][pb]=dp[t][j][k]+Delta; -                                 Else if(dp[t^1][pa][pb]>dp[t][j][k]+delta) dp[t^1][pa][pb]=dp[t][j][k]+Delta; the                             } -                         }Wuyi                     }  the                 } -             } Wu         } -     } About     return; $ } - voidprint () { -     intla=has[0]-(X1-X3), lb=has[1]-(X2-X1), lc=has[2]-(x3-x2); -     if(la<0|| lb<0|| lc<0|| dp[6&1][la][lb]<0) puts ("Impossible"); A     ElseWrite (dp[6&1][la][lb]); +     return; the } - intMain () {init (); work ();p rint ();return 0;}

Bzoj 1021 [shoi2008]debt Cycle of debt

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.