Problem M - TFF
Let F(x) and G(x) be two functions of x with all positive integer co-efficient and let H(x) be their multiplication.
Formally,
F(x) = a0 + a1*x + a2*x^ 2 + ..... + an-1*x^( n-1)
G(x) = b0 + b1*x + b2*x^ 2 + ..... + bm -1*x^( m-1)
H(x) = F(x) * G(x)
= c0 + c1*x + c2*x^ 2 + ..... + ck-1*x^( k-1)
Where an-1 != 0 and bm-1 != 0 and ck-1 != 0
Given G(x) and H(x), find F(x).
Input Specification
First line will contain an integer t denoting the number of test cases. Then the description of the t test cases will follow. The first line of every test case will contain the integer m. The next line will contain m integers which denotes b0 , b1 , b2 , .... , bm-1 . Description of H(x) will be given in the following two lines in exactly the same format.
Output Specification
For every test case you need to print the F(x) function in the same format as G(x) and H(x),which were given in the input.
Constraints
1 <= t <= 10
1 <= a i , b i <= 1000
1 <= n, m <= 1000
Sample
input
1
2
7 3
3
14 41 15
output
2
2 5
Explanation of sample 1
Here,
G(x) = 7 + 3*x
H(x) = 14 + 41*x + 15*x ^2
= (7+3*x) *(2+5*x)
So, F(x) = 2 + 5 * x
题意:给你 G(x) 从 x 的 0 次方 到 x 的 n 次方的系数,H(x) 同理。 其中 H(x) = G(x) * F(x), 求 F(x);
思路:很纯的模拟题, 模拟 多项式除法,注意读题,不会出现 会有余数的情况。
AC代码:
#include<bits/stdc++.h>
using namespace std;
int Hx[5000];
int Gx[5000];
int tmp[5000];
int main()
{
int t; scanf("%d",&t);
while(t --){
memset(tmp,0,sizeof(tmp));
int n; scanf("%d",&n);
for(int i = 0;i < n;i ++) scanf("%d",&Gx[i]);
int f; scanf("%d",&f);
for(int i = 0;i < f;i ++) scanf("%d",&Hx[i]);
for(int i = 0;i < n / 2;i ++) swap(Gx[i],Gx[n-i-1]);
for(int i = 0;i < f / 2;i ++) swap(Hx[i],Hx[f-i-1]);
int ans[5000] = {0}; int pa = 0;
for(int i = 0;i < f - n + 1;i ++){
int t = Hx[i] / Gx[0];
ans[pa++] = t;
for(int k = 1;k < n;k ++)
tmp[k] = t * Gx[k];
for(int k = 1;k < n;k ++)
Hx[i+k] -= tmp[k];
}
printf("%d\n",pa);
for(int i = pa - 1;i >= 0;i --)
if(i == pa - 1) printf("%d",ans[i]);
else printf(" %d",ans[i]);
printf("\n");
}
return 0;
}