Codeforces Round #420 (Div. 2) - B

 

题目链接:http://codeforces.com/contest/821/problem/B

题意:二维每个整点坐标(x,y)拥有的香蕉数量为x+y,现在给你一个直线方程的m和b参数,让你找一个位于这个直线下方的点,该点与原点构成的长方形区域里面的香蕉数量最多。

思路:由方程可以得知y的取值在[0,b]之间,所以枚举y,然后通过方程解出一个最接近解的整数x,然后就是普通的计算了。 对于计算通过预处理一个等差数列的前缀和就可以O(1)计算结果了。

 

#define _CRT_SECURE_NO_DEPRECATE
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<time.h>
#include<cmath>
using namespace std;
typedef long long int LL;
const LL INF = 9223372036854775807;
const int MAXN = 1e7 + 24;
LL sum[MAXN];
void Init(){
    sum[0] = 0;
    for (int i = 1; i < MAXN; i++){
        sum[i] = sum[i - 1] + i;
    }
}
int main(){
//#ifdef kirito
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
//#endif
//    int start = clock();
    int m, b; Init();
    while (~scanf("%d%d",&m,&b)){
        LL ans = -1;
        for (int y = 0; y <= b; y++){
            LL x = (b - y)* m;
            if (x >= 0){
                ans = max(ans, 1LL * (sum[y] * (x + 1) + sum[x] * (y + 1)));
            }
        }
        printf("%I64d\n", ans);
    }
//#ifdef LOCAL_TIME
//    cout << "[Finished in " << clock() - start << " ms]" << endl;
//#endif
    return 0;
}

 

posted @ 2017-06-29 15:25  キリト  阅读(151)  评论(0编辑  收藏  举报