poly-stl

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,double> pid;
typedef vector<pid> vp;
typedef vp::reverse_iterator vri;

inline void readPoly(vp &v){
    v.clear();
    int N;
    cin>>N;
    double A;
    ll B;
    while(N--&&cin>>A>>B)
        v.push_back( pid(B,A) );
    sort(v.begin(),v.end());
}
inline void outputPoly(vp &v){
    vri p=v.rbegin();
    while((p+1)!=v.rend()&&fabs(p->second)<1e-6) ++p;
    if((p+1)==v.rend()&&p->first==0){
        if(fabs(p->second)<1e-6) cout<<0<<endl;
        else cout<<p->second<<endl;
        return ;
    }
    if(p->second<0) cout<<'-';
    if( fabs(fabs(p->second)-1)>=1e-6 )
        cout<<fabs(p->second);
    cout<<'x';
    if(p->first>1) cout<<'^'<<p->first;
    for(++p;p!=v.rend();++p){
        if(fabs(p->second)<1e-6) continue;
        if(p->second>0) cout<<'+';
        else cout<<'-';
        if(p->first==0){
            cout<<fabs(p->second);
            continue;
        }
        if( fabs(fabs(p->second)-1)>=1e-6 )
            cout<<fabs(p->second);
        cout<<'x';
        if(p->first>1) cout<<'^'<<p->first;
    }
    cout<<endl;
}

int K;
vp A[20],Ans[20];

inline void clear(vp &f){
    int Cnt=0;
    sort(f.begin(),f.end());
    for(int i=0,I=f.size();i<I;i++)
        if(fabs(f[i].second)<1e-6){
            f[i]=pid(1e9,1e9);
            Cnt++;
        }
        else if(f[i].first==f[i+1].first){
            f[i+1].second+=f[i].second;
            f[i]=pid(1e9,1e9);
            Cnt++;
        }
    sort(f.begin(),f.end());
    while(Cnt--) f.pop_back();
    if(f.empty()) f.push_back( pid(0,0) );
}
inline void add(vp &f,const vp &g){
    for(const auto &a : g) f.push_back(a);
    clear(f);
}
inline void dis(vp &f,const vp &g){
    for(const auto &a : g) f.push_back(pid(a.first,-a.second));
    clear(f);
}
inline void mul(vp &f,const vp &g,vp &ans){
    for(const auto &a : g)
        for(const auto &b : f)
            ans.push_back( pid(a.first+b.first,a.second*b.second) );
    clear(ans);
    f=ans;
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cout.precision(6);

    cin>>K;
    for(int i=0;i<K;i++) readPoly(A[i]);
    for(int i=1;i<K;i++){
        char c=0;
        while(c!='+'&&c!='-'&&c!='*') cin>>c;
        if(0);
        else if(c=='+') add(A[0],A[i]);
        else if(c=='-') dis(A[0],A[i]);
        else if(c=='*') mul(A[0],A[i],Ans[i]);
    }
    outputPoly(A[0]);
    return 0;
}
posted @ 2020-09-16 22:43  JustinRochester  阅读(190)  评论(0编辑  收藏  举报