分析一下~只有一种情况背包搜索会超时,那就是当他们的性价比相同的时候,这种情况用数论的知识判断一下就可以了~ac~!最后发现害我wa了那么多次的原因居然是一个a写成了v。。。- -!坑爹。。
#include<iostream>
#include<vector>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#include<string>
#include<map>
#include<set>
#include<cmath>
#include<cassert>
#include<cstring>
#include<iomanip>
using namespace std;
#ifdef _WIN32
#define i64 __int64
#define out64 "%I64d\n"
#define in64 "%I64d"
#else
#define i64 long long
#define out64 "%lld\n"
#define in64 "%lld"
#endif
#define FOR(i,a,b) for( int i = (a) ; i <= (b) ; i ++)
#define FF(i,a) for( int i = 0 ; i < (a) ; i ++)
#define FFD(i,a) for( int i = (a)-1 ; i >= 0 ; i --)
#define S64(a) scanf(in64,&a)
#define SS(a) scanf("%d",&a)
#define LL(a) ((a)<<1)
#define RR(a) (((a)<<1)+1)
#define SZ(a) ((int)a.size())
#define PP(n,m,a) puts("---");FF(i,n){FF(j,m)cout << a[i][j] << ' ';puts("");}
#define pb push_back
#define CL(Q) while(!Q.empty())Q.pop()
#define MM(name,what) memset(name,what,sizeof(name))
#define read freopen("in.txt","r",stdin)
#define write freopen("out.txt","w",stdout)
const int inf = 0x3f3f3f3f;
const i64 inf64 = 0x3f3f3f3f3f3f3f3fLL;
const double oo = 1e9;
const double eps = 1e-14;
const double pi = acos(-1.0);
i64 gcd(i64 _a, i64 _b)
{
if (!_a || !_b)
{
return max(_a, _b);
}
i64 _t;
while (_t = _a % _b)
{
_a = _b;
_b = _t;
}
return _b;
};
i64 ext_gcd (i64 _a, i64 _b, i64 &_x, i64 &_y)
{
if (!_b)
{
_x = 1;
_y = 0;
return _a;
}
i64 _d = ext_gcd (_b, _a % _b, _x, _y);
i64 _t = _x;
_x = _y;
_y = _t - _a / _b * _y;
return _d;
}
i64 invmod (i64 _a, i64 _p)
{
i64 _ans, _y;
ext_gcd (_a, _p, _ans, _y);
_ans < 0 ? _ans += _p : 0;
return _ans;
}
i64 n,s1,s2,a1,a2;
double w1,w2;
bool yes()
{
i64 g1 = gcd(s1,a1);
i64 g2 = gcd(s2,a2);
s1 /= g1;
a1 /= g1;
s2 /= g2;
a2 /= g2;
if(a1==a2 && s1==s2)
{
s1*=g1;
a1*=g1;
a2*=g2;
s2*=g2;
return true;
}
else
{
s1*=g1;
a1*=g1;
a2*=g2;
s2*=g2;
return false;
}
}
i64 find()
{
i64 rest;
i64 t1 = n/s1;
rest = n - t1*s1;
i64 t2 = rest/s2;
i64 now = t1*a1 + t2*a2;
double ex;
i64 temp;
if(yes())
{
i64 gab = gcd(s1,s2);
s1/=gab;
s2/=gab;
n/=gab;
i64 x1 = invmod(s1,s2);
i64 x2 = (1-x1*s1)/s2;
i64 pa = x1*a1 + x2*a2;
x1*=n;
x2*=n;
i64 zx = x1/s2;
i64 re = n*pa;
if(x2 + zx*s1<0)
{
x1 = n/s1;
rest = n - x1*s1;
x2 = rest/s2;
temp = x1*s1 + x2*s2;
re = temp;
while(true)
{
x1--;
if(x1<0) break;
rest = n - x1*s1;
x2 = rest/s2;
temp = x1*s1 + x2*s2;
if(temp>re)
{
re = temp;
}
}
return re*pa;
}
else
{
return re;
}
}
else if(s1>s2)
{
while(true)
{
t1--;
if(t1<0) break;
rest = n - t1*s1;
ex = w1*t1*s1 + rest*w2;
if(ex < double(now) + eps)
{
break;
}
t2 = rest/s2;
temp = t1*a1 + t2*a2;
if(temp > now)
{
now = temp;
}
}
}
else if(s1<s2)
{
while(true)
{
t2++;
rest = n - t2*s2;
if(rest<0) break;
ex = w1*rest + t2*s2*w2;
if(ex < double(now) + eps)
{
break;
}
t1 = rest/s1;
temp = t1*a1 + t2*a2;
if(temp > now)
{
now = temp;
}
}
}
return now;
}
int main()
{
int T;
cin>>T;
for(int tt=1;tt<=T;tt++)
{
cin>>n>>s1>>a1>>s2>>a2;
w1 = double(a1)/double(s1);
w2 = double(a2)/double(s2);
if(w2>w1)
{
swap(w1,w2);
swap(a1,a2);
swap(s1,s2);
}
cout<<"Case #"<<tt<<": "<<find()<<endl;
}
return 0;
}