Okabe needs bananas for one of his experiments for some strange reason. So he decides to go to the forest and cut banana trees.
Consider the point (x, y) in the 2D plane such that x and y are integers and 0 ≤ x, y. There is a tree in such a point, and it has x + y bananas. There are no trees nor bananas in other points. Now, Okabe draws a line with equation . Okabe can select a single rectangle with axis aligned sides with all points on or under the line and cut all the trees in all points that are inside or on the border of this rectangle and take their bananas. Okabe's rectangle can be degenerate; that is, it can be a line segment or even a point.
Help Okabe and find the maximum number of bananas he can get if he chooses the rectangle wisely.
Okabe is sure that the answer does not exceed 1018. You can trust him.
The first line of input contains two space-separated integers m and b (1 ≤ m ≤ 1000,1 ≤ b ≤ 10000).
Print the maximum number of bananas Okabe can get from the trees he cuts.
1 5
30
2 3
25
The graph above corresponds to sample test 1. The optimal rectangle is shown in red and has 30 bananas.
题意:给你一个函数关系是式的系数m和b,问以这条函数曲线上的一点作为矩形的一个顶点,与对应的(x,0), (0,y), (0,0)点构成的矩形中有多少个香蕉(每个点的香蕉数等于该点的x与y坐标之和)。
分析:首先我们可以得到x = (b-y)*m,由题目的数据范围可知1<=y<=10000, 1<=x<=1e7。我们可以枚举以y点构成的矩形中含有香蕉的面积,在枚举途中记录最大值即可。
课推得范围内香蕉计算公式:x*(1+x)/2*(y+1) + y*(1+y)/2*(x+1)
代码如下:
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int MX = 105;
int main(){
int m, b;
scanf("%d%d", &m, &b);
LL ans = 0;
for(int y = 0; y <= b; y++){
LL x = (LL)(b-y) * m;
LL cnt = (x*(x+1)/2*(y+1) + y*(y+1)/2*(x+1));
ans = max(ans, cnt);
}
cout << ans << endl;
return 0;
}