LA 2911Maximum poj 3066

来源:互联网 发布:nodejs工程师 知乎 编辑:程序博客网 时间:2024/05/27 00:45

原题:(注意uva la上面的题目描述有错误!!)
Let x1, x2, …, xm be real numbers satisfying the following conditions:
这里写图片描述
for some integers a and b (a > 0).

Determine the maximum value of x1^p + x2^p + … + xm^p 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
中文:
给你m,p,a,b,而且满足图中的两个式子,问你x1^p + x2^p + … + xm^p 最小是多少?

//#include <bits/stdc++.h>#include<iostream>#include<cmath>using namespace std;int main(){    ios::sync_with_stdio(false);    double m,p,a,b,ans;    while(cin>>m>>p>>a>>b)    {        double x=sqrt(a);        double y=-1/x;        double ab=a*b;        int xx=0,yy=0;        for(int i=0;i<m-1;i++)        {            if(ab>=x)            {                ab-=a;                xx++;            }            else            {                ab+=1;                yy++;            }        }        ans=xx*pow(x,p)+yy*pow(y,p)+pow(ab/x,p);        cout<<(int)(ans+0.5)<<endl;    }    return 0;}

解答:
这题坑了我一下午的时间,首先uva上面的题目描述有错误!!!
这里写图片描述
研究了半天,是在不知道是咋回事,看别人的解题报告。了解题的大概意思,结果还是卡住了,呵呵。
思路很简单,既然是让你找最后式子的最大值,而且p是一个偶数,那么就让xi尽量取根号a即可。不过程序不能简单粗暴的写,否则会过不去。计算取多少个根号a要用循环的减去,这样才能满足精度要求。

0 0
原创粉丝点击