#include<iostream>
using namespace std;
typedef struct Node{
int coe;
int index;
Node *next;
}node,*nodeptr;
nodeptr add(nodeptr head1,nodeptr head2);
void show(nodeptr head3);
void del(nodeptr k);
void addall(nodeptr a[],int end);
int main(void)
{
nodeptr head1,p;
head1=new node;
head1->next=NULL;
p=head1;
int M;
cin >> M;
for(int i=0;i<=M;i++){
nodeptr q=new node;
cout << "coe?" << endl;
cin >> q->coe;
cout << "index?" << endl;
cin >> q->index;
p->next=q;
p=q;
}
p->next=NULL;
nodeptr head2;
head2=new node;
head2->next=NULL;
p=head2;
int N;
cin >> N;
for(int i=0;i<=N;i++){
nodeptr q=new node;
cout << "coe?" << endl;
cin >> q->coe;
cout << "index?" << endl;
cin >> q->index;
p->next=q;
p=q;
}
p->next=NULL;
nodeptr a[M+1],h=head1->next;
int i=0;
while(h){
nodeptr g=head2->next,temp;
a[i]=new node;
a[i]->next=NULL;
temp=a[i];
while(g){
nodeptr k=new node;
k->coe=g->coe * h->coe;
k->index=g->index + h->index;
g=g->next;
temp->next=k;
temp=k;
}
temp->next=NULL;
h=h->next;
i++;
}
addall(a,M);
show(a[0]);
for(int i=0;i<M+1;i++){
del(a[i]);
}
del(head1);
del(head2);
return 0;
}
nodeptr add(nodeptr head1,nodeptr head2){
nodeptr head3,p,q;
head3=new node;
p=head1->next;
q=head2->next;
nodeptr m=new node;
head3->next=m;
while(p!=NULL || q!=NULL){
if(p==NULL){
m->next=q->next;
m->index=q->index;
m->coe=q->coe;
break;
}
if(q==NULL){
m->next=p->next;
m->index=p->index;
m->coe=p->coe;
break;
}
nodeptr temp=new node;
m->next=temp;
if(p->index==q->index){
m->index=q->index;
m->coe=q->coe+p->coe;
m=m->next;
p=p->next;
q=q->next;
}
else if(p->index>q->index){
m->index=q->index;
m->coe=q->coe;
m=m->next;
q=q->next;
}
else{
m->index=p->index;
m->coe=p->coe;
m=m->next;
p=p->next;
}
}
return head3;
}
void show(nodeptr head3){
nodeptr p=head3->next;
while(p){
cout << p->coe <<"x^" << p->index << "+";
p=p->next;
}
return;
}
void del(nodeptr head2){
nodeptr p=head2->next;
while(p){
nodeptr temp=p;
p=p->next;
delete temp;
}
delete head2;
}
void addall(nodeptr a[],int end){
if(end==0) return;
if(end%2==1){
for(int i=0;i<=end/2;i++){
a[i]=add(a[i],a[end-i]);
}
addall(a,end/2);
}
else{
for(int i=0;i<end/2;i++){
a[i]=add(a[i],a[end-i]);
}
addall(a,end/2);
}
return;
}
快200行
ohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh