UVALive - 2911 Maximum

来源:互联网 发布:vv51软件下载 编辑:程序博客网 时间:2024/06/16 11:50

题目大意:给定 m, p, a, b. 根据题目中的两个条件.求出 xp1 + xp2 +...+ xpm 最大值.


解题思路: 贪心, 由于题目明确了 p 是偶数, 所以 x 绝对值越大的时候 x^p 值越大. 然后我们根据条件,发现 x 尽可能取 sqrt(a) 是最好的, 但是不一定能全部取得 sqrt(a), 那么多出来的还要拿一部分去抵消。这时候我们就用  -1/sqrt(a) 去抵消是最好的, 这样就能满足最大了, 不过要注意, 抵消到最后剩下那部分也要考虑进去

#include <cstdio>#include <cmath>int main() {int m, p, a, b;while (scanf("%d%d%d%d", &m, &p, &a, &b) != EOF) {int temp = a * b, cnt = 0;double num = pow(sqrt(a), p);for (int i = 1; i < m; i++)temp < a ? temp++, cnt++ : temp -= a;printf("%d\n",int((m-1-cnt)*num + cnt/num + pow(temp/sqrt(a), p) + 0.5));}return 0;}


0 0