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;
}
体会:算法无时无刻离不开数学,数学好学算法事半功倍,才是王道