您的位置:首页 > 其它

codeforces 821 B Okabe and Banana Trees

2017-06-26 22:22 330 查看
题目:http://codeforces.com/contest/821/problem/B

大致题意:

给两个正整数m,b,画出一条直线y = -x/m + b,求直线下的一个矩形使得获取的香蕉数最多,每个点(x,y)的香蕉数目为 x+y。

解法:

可推得公式,矩形右上角为(x,y)的香蕉总数为

(x+1) * x / 2 * (y+1) + (y+1) * y /2 * (x+1)

然后,依次遍历直线上的格点,求出矩形右上角为格点坐标的香蕉总数,取最大值。

代码:

#include <iostream>
#include <algorithm>
using namespace std;
int main(){
long long m,b;
cin >> m >> b;
long long ans = -1;
for(long long i = 0 ; i <= b; i++){
long long j = m * (b-i);
ans = max(ans,
(1+i) * i / 2 * (j+1) + (1+j) * j /2 * (i+1));
}
cout << ans << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: