1319 - Maximum
Time limit: 3.000 seconds
Let x1, x2,..., xm be real numbers satisfying the following conditions:
-
a)
- -xi ; b)
- x1 + x2 +...+ xm = b * for some integers a and b (a > 0).
Determine the maximum value of xp1 + xp2 +...+ xpm for some even positive integer p.
Input
Each input line contains four integers: m, p, a, b ( m2000, p12, 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,那么-1xi*a,x1* + x2* +...+ xm* = ab。
为了使其的p次方和最大,p又是偶数,我们可以让一部分x*的值为-1,一部分为a,最后一个x的范围只要在-1到a间就可以了。m最大2000,直接枚举,然后计算,最终结果除以^p。代码如下:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
void work(int m,int p,int a,int b)
{
double res=0;
for(int i=0;i<m;i++)
{
int last=a*b+i-(m-i-1)*a;
if(last>=-1 && last<=a)
{
res+=i;
res+=pow((double)last,p);
res+=(double)(m-i-1)*pow((double)a,p);
res/=pow((double)a, p/2.0);
break;
}
}
printf("%d\n", (int)(res+0.5));
}
int main()
{
int m,p,a,b;
while(~scanf("%d%d%d%d",&m,&p,&a,&b))
work(m,p,a,b);
}