您的位置:首页 > 其它

poj 1996多项式计算

2016-07-25 19:33 169 查看
The Highest Profits

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 656 Accepted: 286
Description

This program will not be part of the KOKODáKH collection, but it may be more important than the games themselves. It will serve to the marketing staff to find suitable procedures for promoting and selling the collection. So try to do your best. 

Extensive marketing case study tried to prove that the total income from selling a product is polynomial function of number of satisfied customers. Experiments showed that the real results are not exactly the same which you can obtain using the function. However,
this method is widely used. The reason is probably nonexistence of better solution. Let's denote the number of satisfied customers y, than we can express profits (denoted x) as 
x = P(y) =a0 +a1.y +a2.y2 + ... +am.ym 

The number of satisfied customers depends on the price of a product. Again, there is hypothese that this dependence is polynomial. If we denote the price z, we can write 

y = Q(x) =b0 +b1.z +b2.z2 + ... +bn.zn 

Coeficients ai and bi strongly depend on the season of the year, the moon phase, the purchasing power of customers, inflation rate and hunderds of other parameters. Besides on the kind of product and its quality, of course. In the past there was lot of effort
put into the reserarch of these parameters. For various combinations of input parameters, the coeficients are stated in Pyshwejc's marketing tables. It is not thus difficult to find out their values. But the degree of polynoms is usualy very high. It is very
difficult to substitute one polynom into the other and to compute the dependency of the profit on the price. This dependency is usually crucial for us to set the right price. 

Your goal is to write the program which can substitute the polynom Q into the polynom P and determine the restulting polynom R indicating dependency of the profit on the price: 

x = R(z) =c0 +c1.z +c2.z2 + ... +cp.zp

Input

The input consists of Z assignments. The number of them is given by the single positive integer Z appearing on the first line of input. Each assingement constist of three lines. On the first line there are two integers m and n (0 <= n,m <= 100) separated by
space. These numbers give the degree of polynoms Q and P. On the second line there are m+1 integers a0 ... am. These numbers are coeficients of polynom P. On the third line there are n+1 integers b0 ... bn. Always am <> 0 and bn <> 0. Coeficients ai and bi
are separated by space and they are chosen in order to each resulting coeficient could fit into the standard type integer.
Output

The program prints exactly one line for each assignement. On this line, there will be p+1 numbers. These numbers are coeficients c0 ... cp of resulting polynom. The coeficients are separated by space and the line does not consist any redundant spaces. The coeficient
in the highest degree should not be zero.
Sample Input
3
0 0
7
-2
1 1
6 6
9 -6
3 3
-3 6 -5 1
0 3 -3 1

Sample Output
7
60 -36
-3 18 -63 123 -156 138 -86 36 -9 1


题意:就是用 y(z)这个函数代入  x(y)函数,求出相应的系数就可以了

这里有一个之前没有想到的做法就是:

x = P(y) =a0 +a1.y +a2.y2 + ... +am.ym 

先代入用arr数组存 一个多项式 y 

代入进行计算

然后 arr=arr*  y(就是 y的平方)后作为整体代入

然后重复上叙步骤

我这里使用了滚动数组

同时需要注意这个滚动数组需要注意初始化

滚动数组的运用大家可以百度一下

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;

int ans[10010];
int a[110],b[110];
int n,m;
int arr[3][10010];

int main()
{
//freopen("in.txt","r",stdin);
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
n++,m++;
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<m;i++)
scanf("%d",&b[i]);

memset(ans,0,sizeof(ans));
memset(arr,0,sizeof(arr));
for(int i=0;i<m;i++)
arr[0][i]=b[i];
ans[0]=a[0];
for(int i=1;i<n;i++){
for(int j=0;j<(m*i+1);j++){
ans[j]+=arr[(i-1)%2][j]*a[i];
arr[i%2][j]=0;
}

for(int j=0;j<m*i;j++)
for(int k=0;k<m;k++)
arr[i%2][j+k]+=arr[(i-1)%2][j]*b[k];
}

int len=(n-1)*(m-1)+1;
printf("%d",ans[0]);
for(int i=1;i<len;i++)
printf(" %d",ans[i]);
puts("");
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: