团体程序设计天梯赛 L2-018 多项式A除以B (25分)
题目链接:
L2-018 多项式A除以B (25分)
思路:
仿照竖式除法,维护余数和结果即可;
代码:
#include<bits/stdc++.h>
using namespace std;
inline void read(vector<double> & v) {
int n;
cin >> n;
for(int i = 0; i < n; i++) {
int e, c;
cin >> e >> c;
if(i == 0) v.resize(e + 1);
v[e] = c;
}
}
inline void out(vector<double> & v) {
int ans = 0;
for(double & x : v) if(abs(x) >= 0.1) ++ans;
cout << ans;
for(int i = v.size() - 1; i >= 0; i--) {
if(abs(v[i]) >= 0.1) printf(" %d %.1f", i, v[i]);
else if(!ans && !i) cout << " 0 0.0";
}
cout << '\n';
}
int main() {
#ifdef MyTest
freopen("Sakura.txt", "r", stdin);
#endif
vector<double> a, b;
read(a), read(b);
int n = a.size(), m = b.size();
vector<double> ans(n);
while(n >= m) {
int e = n - m;
double c = a[n - 1] * 1.0 / b[m - 1];
ans[e] += c;
vector<double> res(n);
for(int i = 0; i < m; i++) res[i + e] = b[i] * c;
for(int i = 0; i < n; i++) a[i] -= res[i];
while(a[n - 1] == 0) --n;
}
out(ans);
out(a);
return 0;
}