CF#420 B. Okabe and Banana Trees 思维|暴力|几何

来源:互联网 发布:吃鸡网络延迟 编辑:程序博客网 时间:2024/06/04 17:49

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 thatx andy are integers and0 ≤ x, y. There is a tree in such a point, and it hasx + 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.

Input

The first line of input contains two space-separated integers m and b (1 ≤ m ≤ 1000,1 ≤ b ≤ 10000).

Output

Print the maximum number of bananas Okabe can get from the trees he cuts.

Example
Input
1 5
Output
30
Input
2 3
Output
25
Note

The graph above corresponds to sample test 1. The optimal rectangle is shown in red and has30 bananas.

题意:
在给出的一次函数曲线中找到下方向找到一个矩形 矩形的每一个点都大于0 权值为每个整点x+y 给定m和b 求最大的矩形

分析:
求最大的必然是整点 因为如果非整点的话不如在整点的矩形大 (可以根据图中看出)
本题我们可以考虑暴力
如果暴力的话 我们可以只暴力一维 另一维通过函数计算得到
那么最大的x = m*b 最大的y = b
所以不如枚举y
然后对于每一个(x,y) 可以推推公式得到权值计算的O(1)方法
复杂度O(n)

code:
#include<bits/stdc++.h>using namespace std;typedef long long ll; int main(){int m,b;scanf("%d%d",&m,&b);ll ans=0;for(int y=b;y>=0;y--){int x = (b-y)*m;ans = max(ans,1LL*(1+y)*y/2*(x+1)+1LL*(1+x)*x/2*(y+1));// 里面的式子也要转成longlong 否则WA 注意数据范围比较大}printf("%lld\n",ans);return 0;}