模拟一遍即可。

注意一些特殊情况,见代码。

 #include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#include<string.h>
using namespace std; const int maxn = ; struct Ploy{
int cnt;//项的数目
int coe[ maxn ];//各项系数
int exp[ maxn ];//各项指数
}a;
struct Ploy2{
int coe1,coe2;
int exp1,exp2;
}b;
struct Ploy3{
int coe,exp;
}tmp; bool Judge( int aim ){
//bool f = false;
for( int i=a.cnt-;i>=;i-- ){
if( a.coe[i]!=&&a.exp[i]>=aim ){
//f = true;
return true ;
}
}
return false;
} void solve( int n,int k ){
//ans.cnt = 0;
while( ){
//if( Judge(k)==false ) break;
int i;
for( i=a.cnt-;i>=;i-- ){
if( a.coe[i]!= ){
tmp.coe = a.coe[i];
tmp.exp = a.exp[i];
break;
}
}
if( tmp.exp<k ) break;
int delta_exp = tmp.exp-k;
int delta_coe = tmp.coe;//商
b.exp1 = tmp.exp,b.coe1 = tmp.coe;
b.exp2 = delta_exp,b.coe2 = delta_coe;
//bool f1 = false,f2 = false;
for( int i=;i<a.cnt;i++ ){
if( a.exp[i]==b.exp1 ){
a.coe[i] -= b.coe1;
//f1 = true;
}
if( a.exp[i]==b.exp2 ){
a.coe[i] -= b.coe2;
//f2 = true;
}
}
}
} int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n,k;
while( scanf("%d%d",&n,&k)== ){
if( n==k&&k==- ) break;
a.cnt = ;
for( int i=;i<=n;i++ ){
a.cnt ++;
scanf("%d",&a.coe[i]);
a.exp[i] = i;
}
if( k== ){
puts("");
continue;
}
//printf("input\n");
bool f = false;
for( int i=;i<a.cnt;i++ ){
if( a.coe[i]!= ){
f = true;
break;
}
}
if( f==false ) {
puts("");
continue;
}//多项式全为0
if( k>n ){
for( int i=;i<a.cnt;i++ ){
if( i== ) printf("%d",a.coe[i]);
else printf(" %d",a.coe[i]);
}
printf("\n");
continue;
}//商为0
//printf("solve\n");
solve( n,k );
f = false;
for( int i=;i<a.cnt;i++ ){
if( a.coe[i]!= ){
f = true;
break;
}
}
if( f==false ) {
puts("");
continue;
}//刚好整除
int pos = a.cnt-;
for( int i=a.cnt-;i>=;i-- ){
if( a.coe[i]!= ){
pos = i;
break;
}
}
for( int i=;i<=pos;i++ ){
if( i== ) printf("%d",a.coe[i]);
else {
printf(" %d",a.coe[i]);
}
}
printf("\n");
}
return ;
}
05-08 08:17