
输入样例:
4 4 1 2 -3 1 -1 0 -1
3 2 3 1 -2 0 1
输出样例:
3 2 0.3 1 0.2 0 -1.0
1 1 -3.1
大佬思路:
- 数组模拟,数组下标是i的元素值代表的是指数为i的项的系数,并需要对缺幂项进行补零。
- 如: 2x^4 + x^2 - 5x + 8 --> [8,-5,1,0,2]
- 若A多项式的最高次幂为t1, B多项式的最高次幂为t2, 则第一次除法得到的商的最高次幂为t1 – t2, 最高次 幂的系数为A[t1] / B[t2],
- 然后⽤用A[i] -= B[i – (t1 – t2)] * A[t1] / B[t2], 其中i从A的最高次幂t1到大于等于 t1 – t2, 这样就算完成了一个除法。
- 例如A = [-1, -1, -3, 0, 1], B = [1, -2, 3], 则t1 = 4, t2= 2, 所以第一次除法商的最高次幂为2, 系数为A[4] / A[2] = 0.3, 循环A[i] -= B[i – (t1 – t2)] * A[t1] / B[t2], i从4到2, 新的A=[-1, -1, -10/3, 2/3, 0], 然后重复上⾯面的步骤, 直到A的最高项幂次⼩于B的最高项幂次, 此时A多项式就是余项。
- 这种写法真的太巧妙了,鄙人不善奔跑…
答案:
#include <iostream>
#include<bits/stdc++.h>
#define ll long long
#define PII pair<int,int>
#define PSS pair<string,string>
#define PSI pair<string,int>
#define pb push_back
#define mem(a,b) memset(a,b,sizeof(a))
#define x first
#define y second
#define eps 1e-6
const int N = 1e4 + 10;
using namespace std;
double c1[N];
double c2[N];
double c3[N];
int get_num(double *c,int s){ /// 看四舍五入且保留一位小数之后还有多少个非0项
int tot=0;
for(int i=s;i>=0;i--){
if(fabs(c[i])+0.05>=0.1) tot++;
}
return tot;
}
void dis(double *c,int s){
cout<<get_num(c,s);
if(!get_num(c,s)) cout<<" 0 0.0";
for(int i=s;i>=0;i--){
if(fabs(c[i])+0.05>=0.1)
printf(" %d %.1f",i,c[i]);
}
}
int main(){
int m;
cin>>m;
int maxn1=-1;
for(int i=0;i<m;i++){
int x;
cin>>x;
maxn1=max(maxn1,x);
cin>>c1[x]; /// 指数为x的项的系数放在c1[]的下标为x的位置上
}
int n;
cin>>n;
int maxn2=-1;
for(int i=0;i<n;i++){
int x;
cin>>x;
maxn2=max(maxn2,x);
cin>>c2[x]; /// 指数为x的项的系数放在c2[]的下标为x的位置上
}
int l=maxn1;
int r=maxn2;
while(l>=r){
double c=c1[l]/c2[r];
c3[l-r]=c;
for(int i=l,j=r;j>=0;i--,j--) c1[i]-=c2[j]*c;
while(fabs(c1[l])<eps) l--;
}
dis(c3,maxn1-maxn2); /// 商的最高次幂是max1 - max2
cout<<endl;
dis(c1,l);
cout<<endl;
return 0;
}

4888

被折叠的 条评论
为什么被折叠?



