题目网址:http://codeforces.com/contest/821/problem/B
题目:
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 + ybananas. 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.
思路:先求出在y=0的情况下,x的取值范围,再遍历x的取值范围,将0->x看成矩形的一条边。再将y(x)看作矩形的另一条边。遍历x的过程中,不断更新最大值。
计算:(以上图情况为例)
res=[(0+0)+(0+1)+(0+2)+(0+3)]+[(1+0)+(1+1)+(1+2)+(1+3)]+[(2+0)+(2+1)+(2+2)+(2+3)]
=[(3+3+3)+(2+2+2)+(1+1+1)+(0+0+0)]+[0*4+1*4+2*4+3*4]
有上述情况可推出:res=y*(y+1)/2*(i+1)+i*(i+1)/2*(y+1),其中i为x的当前取值,y为y(i)
代码:
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
int main(){
int m,b;
ll res=0;
scanf("%d%d",&m,&b);
ll x=m*b;
for(ll i=0;i<=x;i++){
ll cur=0;
ll y=(ll)(-i*1.0/m+b);
cur+=y*(y+1)/2*(i+1)+i*(i+1)/2*(y+1);
res=max(res, cur);
}
printf("%I64d\n",res);
return 0;
}