UVALive 2911 Maximum

Let x1, x2, … , xm be real numbers satisfying the following conditions:
a)- 1/√a ≤ xi ≤√a;
b) x1 + x2 + … + xm = b ∗√a for some integers a and b (a > 0).
Determine the maximum value of x
p
1 + x
p
2 + … + x
p
m for some even positive integer p.
Input
Each input line contains four integers: m, p, a, b (m ≤ 2000, p ≤ 12, p is even). Input is correct, i.e.
for each input numbers there exists x1, x2, … , xm satisfying the given conditions.
Output
For each input line print one number — the maximum value of expression, given above. The answer
must be rounded to the nearest integer.
Sample Input
1997 12 3 -318
10 2 4 -1
Sample Output
189548
6
题意大意:令满足a)- 1/√a ≤ xi ≤√a; b) x1 + x2 + … + xm = b ∗√a的xi^p的和最大(p为偶数)
思想:找最多满足 xi =√a 的个数,剩下的数为最小,最后一个数为b* √a减去前面的数,这样就会使xi^p的和最大;
(http://acm.hust.edu.cn/vjudge/contest/121559#problem/L)

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
int main()
{
    double t,res;
    int m,p,a,b,t1,t2;
    while (~scanf("%d%d%d%d",&m,&p,&a,&b))
    {
        int sum=a*b;   //这里的和乘了一个根号a;
        t1=0;t2=0;
        for(int i=0;i<m-1;i++)
        {
            if(sum>=a)    //因前面乘了一个根号a,所以这里也应该是sum>=a;
            {
                t1++;sum-=a;
            }
            else
            {
                t2++;
                sum++;
            }
        }
        t=sqrt(a);
        res=t1*pow(t,p)+t2*pow(1/t,p)+pow(sum/t,p);  //最后一项为剩下的那项
        printf("%.0f\n",res);  

    }
    return 0;
}

体会:算法无时无刻离不开数学,数学好学算法事半功倍,才是王道

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值