A sweet little monster Om Nom loves candies very much. One day he found himself in a rather tricky situation that required him to think a bit in order to enjoy candies the most. Would you succeed with the same task if you were on his place?
One day, when he came to his friend Evan, Om Nom didn't find him at home but he found two bags with candies. The first was full of blue candies and the second bag was full of red candies. Om Nom knows that each red candy weighs Wr grams and each blue candy weighs Wb grams. Eating a single red candy gives Om Nom Hr joy units and eating a single blue candy gives Om Nom Hb joy units.
Candies are the most important thing in the world, but on the other hand overeating is not good. Om Nom knows if he eats more than C grams of candies, he will get sick. Om Nom thinks that it isn't proper to leave candy leftovers, so he can only eat a whole candy. Om Nom is a great mathematician and he quickly determined how many candies of what type he should eat in order to get the maximum number of joy units. Can you repeat his achievement? You can assume that each bag contains more candies that Om Nom can eat.
The single line contains five integers C, Hr, Hb, Wr, Wb (1 ≤ C, Hr, Hb, Wr, Wb ≤ 109).
Print a single integer — the maximum number of joy units that Om Nom can get.
10 3 5 2 3
16
In the sample test Om Nom can eat two candies of each type and thus get 16 joy units.
哎,做过一次,知道了最大公约数化简,最后枚举的时候忘了用大的枚举。。。
10596551 | 2015-04-05 05:09:39 | njczy2010 | C - Om Nom and Candies | GNU C++ | Accepted | 31 ms | 0 KB |
1 #include <cstdio> 2 #include <cstring> 3 #include <stack> 4 #include <vector> 5 #include <algorithm> 6 #include <queue> 7 #include <map> 8 9 #define ll long long 10 int const N = 2500; 11 int const M = 205; 12 int const INF = 0x3f3f3f3f; 13 ll const mod = 1000000007; 14 15 using namespace std; 16 17 ll c,hr,hb,wr,wb; 18 ll g; 19 ll ans; 20 ll ma; 21 ll lcm; 22 23 ll gcd(ll a,ll b) 24 { 25 if(b==0) 26 return a; 27 return gcd(b,a%b); 28 } 29 30 void ini() 31 { 32 g=gcd(wr,wb); 33 if(wr<wb){ 34 swap(wr,wb); 35 swap(hr,hb); 36 } 37 lcm=wr*wb/g; 38 ans=0; 39 ma=0; 40 } 41 42 void solve() 43 { 44 if(c%lcm==0){ 45 ans=max(c/wr*hr,c/wb*hb); 46 return; 47 } 48 ll shang=c/lcm; 49 if(shang>=2){ 50 c-=lcm*(shang-1); 51 ans=(shang-1)*(max(lcm/wr*hr,lcm/wb*hb)); 52 } 53 ll i; 54 for(i=0;i*wr<=c;i++){ 55 ma=max(ma,i*hr+(c-i*wr)/wb*hb); 56 } 57 ans+=ma; 58 } 59 60 void out() 61 { 62 printf("%I64d ",ans); 63 } 64 65 int main() 66 { 67 //freopen("data.in","r",stdin); 68 //scanf("%d",&T); 69 //for(int cnt=1;cnt<=T;cnt++) 70 //while(T--) 71 while(scanf("%I64d%I64d%I64d%I64d%I64d",&c,&hr,&hb,&wr,&wb)!=EOF) 72 { 73 ini(); 74 solve(); 75 out(); 76 } 77 }