Codeforces 526 C Om Nom and Candies
Test instructions
Give a capacity of C backpack, the value of two items HR,HB and capacity CR,CB, to fill the maximum value of backpack can be installed.
Limit:
1 <= c,hr,hb,wr,wb <= 1e9
Ideas:
Remaining system
Set the first item has x, the second item has y, then there are:
X*HR + Y*HB <= C
Then there are:
(C-Y*HB)% hr = t
It is not difficult to see that for the same remaining system T, the optimal value at both ends.
Therefore, all remaining systems are calculated, and the optimal values are obtained.
Complexity O (sqrt (n))
/*codeforces 526 C Om Nom and candies test instructions: Give a capacity of C backpack, two items of value HR,HB and capacity CR,CB, to fill the maximum value of backpack can be installed. Restrictions: 1 <= c,hr,hb,wr,wb <= 1e9 ideas: The remainder of the first item has x, the second item has y, then there are: X*hr + Y*HB <= C is: (C-Y*HB)% hr = T not difficult to see To the same remaining system T, the optimal value at both ends. Therefore, all remaining systems are calculated, and the optimal values are obtained. Complexity O (sqrt (n)) */#include <iostream> #include <cstdio> #include <algorithm> #include <vector># include<map>using namespace Std;const int n=1000000; #define LL __int64map<ll,int> Mp;int a[n],r[n],cnt;int Main () {LL c,hr,hb,wr,wb;cin>>c>>hr>>hb>>wr>>wb;if (WR<WB) {swap (Hr,Hb), swap (Wr, WB);} cnt=0; ll Lim=min (wb+1,c/wr+1);//cout<<lim<<endl;for (int i=0;i<=lim;++i) {ll tmp= ((c-wr*i)%Wb+Wb)%Wb;// Cout<<tmp<<endl;if (Mp.find (TMP) ==mp.end ()) {r[cnt]=tmp;a[cnt++]=i;mp[tmp]=1;}} ll Ans=0;if (HR*WB<=HB*WR) {for (int i=0;i<cnt;++i) {LL tmp=wr*a[i];if (tmp>c) Continue;ans=max (ans,Hr*a[i]+ ( C-TMP)/WB*HB);}} else{for (int i=0;i<cnt;++i) {LL d=__gcd (WR,WB); LL nn= (C/wr-a[i])/(wb/d); nn=nn*wb/d+i; LL tmp=nn*wr;if (tmp>c) Continue;ans=max (ans,hr*nn+ (c-tmp)/WB*HB);}} Cout<<ans<<endl;return 0;}
Codeforces 526 C Om Nom and candies remainder system