Description
Bessie has broken into Farmer John's house again! She has discovered a pile of lemons and a pile of oranges in the kitchen (effectively an unlimited number of each), and she is determined to eat as much as possible.
Bessie has a maximum fullness of T (1≤T≤5,000,000). Eating an orange increases her fullness by A, and eating a lemon increases her fullness by B (1≤A,B≤T). Additionally, if she wants, Bessie can drink water at most one time, which will instantly decrease her fullness by half (and will round down).
Help Bessie determine the maximum fullness she can achieve!
奶牛Bessie潜入了农夫约翰的家,她发现这里有无穷无尽的柠檬派和橘子派。
Bessie的饱胀值一开始是0,且上限是T,每个柠檬派可以提供A点饱胀值,每个橘子派可以提供B点饱胀值。
Bessie可以不断地吃东西,如果她的饱胀值没有超出T的话。同时,Bessie有一次喝水的机会,喝完后,她的饱胀值将减少一半(往下取整)。
请计算出Bessie的饱胀值最多可以达到多少。
Input
The first (and only) line has three integers T, A, and B.
Output
A single integer, representing the maximum fullness Bessie can achieve.
Sample Input
8 5 6
Sample Output
8
这道题实际上只需要设表示不喝水是否能达到
和
表示喝水是否能达到
,然后扫两遍就可以了,下面是程序:
#include<stdio.h>
#include<iostream>
using namespace std;
const int N=5000005;
bool f[2][N];
int main(){
int i,n,a,b;
scanf("%d%d%d",&n,&a,&b);
f[0][a]=f[0][b]=1;
for(i=1;i<=n;i++){
if(f[0][i]){
if(i+a<=n){
f[0][i+a]=1;
}
if(i+b<=n){
f[0][i+b]=1;
}
f[1][i/2]=1;
}
}
for(i=1;i<=n;i++){
if(f[1][i]){
if(i+a<=n){
f[1][i+a]=1;
}
if(i+b<=n){
f[1][i+b]=1;
}
}
}
while(!f[0][n]&&!f[1][n]){
--n;
}
printf("%d\n",n);
return 0;
}