topic link
https://pintia.cn/problem-sets/994805046380707840/problems/994805060372905984
ideas
In fact, it is a polynomial division operation. We try to use BB as much as possible.The maximum item A
of B is of the same order as the corresponding position item of , and then a subtraction operation can be performed. For details, please refer to the code. Note that there are several pits here:
- The order of the remainder is less than the order of the quotient: test points 3, 4
- Remove zeros in quotient: test points 1, 4
- The maximum order of B is larger than that of A: test points 2, 3
code
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define endl "\n"
#define PII pair<int,int>
#define INF 0x3f3f3f3f
const int N = 2e4 + 10;
double a[N], b[N], q[N];
struct Node {
int e;
double c;
};
void print(vector<Node> &ans) {
int l = ans.size();
cout << l << " ";
for (int i = 0; i < l; ++i) {
cout << ans[i].e << " " << ans[i].c << " \n"[i == l - 1];
}
if (!l) cout << "0 0.0" << endl;
}
int n, m;
int main() {
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cout << fixed << setprecision(1);
cin >> n;
double ci;
int ei, maxe1 = 0, maxe2 = 0;
for (int i = 1; i <= n; ++i) {
cin >> ei >> ci;
a[ei] = ci;
maxe1 = max(maxe1, ei);
}
cin >> m;
for (int i = 1; i <= m; ++i) {
cin >> ei >> ci;
b[ei] = ci;
maxe2 = max(maxe2, ei);
}
//除法运算
for (int i = maxe1; i >= maxe2; --i) {
int qe = i - maxe2;
double qc = a[i] / b[maxe2];
q[qe] = qc;
for (int j = maxe2; j >= 0; --j) {
int pe = j + qe;
double pc = qc * b[j];
a[pe] -= pc;
}
a[i] = 0;
}
//得到商
vector<Node> ans;
for (int i = maxe1 - maxe2; i >= 0; --i)
if (fabs(q[i]) >= 0.05)
ans.push_back({
i, q[i]});
print(ans);
//得到余数
ans.clear();
for (int i = maxe1; i >= 0; --i)
if (fabs(a[i]) >= 0.05)
ans.push_back({
i, a[i]});
print(ans);
return 0;
}
/*
2 4 4 3 2
2 5 4 3 2
*/