One of the tasks students routinely carry out in their mathematics classes is to solve a polynomial equation. It is, given a polynomial, sayX2 - 4X + 1<tex2html_verbatim_mark> , to find its roots (2±)<tex2html_verbatim_mark> .

If the students' task is to find the roots of a given polynomial, the teacher's task is then to find a polynomial that has a given root. Ms. Galsone is an enthusiastic mathematics teacher who is bored with finding solutions of quadratic equations that are as simple as a + b<tex2html_verbatim_mark> . She wanted to make higher-degree equations whose solutions are a little more complicated. As usual in problems in mathematics classes, she wants to maintain all coefficients to be integers and keep the degree of the polynomial as small as possible (provided it has the specified root). Please help her by writing a program that carries out the task of the teacher's side.

You are given a number t<tex2html_verbatim_mark> of the form:

t =  + 

<tex2html_verbatim_mark>

where a<tex2html_verbatim_mark> and b<tex2html_verbatim_mark> are distinct prime numbers, and m<tex2html_verbatim_mark> and n<tex2html_verbatim_mark> are integers greater than 1.

In this problem, you are asked to find t<tex2html_verbatim_mark> 's minimal polynomial on integers, which is the polynomial F(X) = adXd + ad-1Xd-1 + ... a1X +a0<tex2html_verbatim_mark> satisfying the following conditions.

  1. Coefficients a0,..., ad<tex2html_verbatim_mark> are integers and ad > 0<tex2html_verbatim_mark> .
  2. F(t) = 0<tex2html_verbatim_mark> .
  3. The degree d<tex2html_verbatim_mark> is minimum among polynomials satisfying the above two conditions.
  4. F(X)<tex2html_verbatim_mark> is primitive. That is, coefficients a0,..., ad<tex2html_verbatim_mark> have no common divisors greater than one.

For example, the minimal polynomial of  + <tex2html_verbatim_mark> on integers is F(X) = X4 -10X2 + 1<tex2html_verbatim_mark> . Verifying F(t) = 0<tex2html_verbatim_mark> is as simple as the following ( = , = <tex2html_verbatim_mark> ).

F(t) = ( + )4 -10( + )2 + 1
  = ( +4 +6 +4 + ) - 10( +2 + ) + 1
  = 9 + 12 +36 + 8 +4 - 10(3 + 2 + 2) + 1
  = (9 + 36 + 4 - 50 + 1) + (12 + 8 - 20)
  = 0

<tex2html_verbatim_mark>

Verifying that the degree of F(t)<tex2html_verbatim_mark> is in fact minimum is a bit more difficult. Fortunately, under the condition given in this problem, which is that a<tex2html_verbatim_mark> and b<tex2html_verbatim_mark> are distinct prime numbers and m<tex2html_verbatim_mark> and n<tex2html_verbatim_mark> greater than one, the degree of the minimal polynomial is always mn<tex2html_verbatim_mark> . Moreover, it is always monic. That is, the coefficient of its highest-order term ( ad<tex2html_verbatim_mark> ) is one.

Input

The input consists of multiple datasets, each in the following format.

a  m  b  n

<tex2html_verbatim_mark>

This line represents  + <tex2html_verbatim_mark> . The last dataset is followed by a single line consisting of four zeros. Numbers in a single line are separated by a single space.

Every dataset satisfies the following conditions.

  1.  + 4<tex2html_verbatim_mark>
  2. mn20<tex2html_verbatim_mark>
  3. The coefficients of the answer a0,..., ad<tex2html_verbatim_mark> are between (- 231 + 1)<tex2html_verbatim_mark> and (231 - 1)<tex2html_verbatim_mark> , inclusive.

Output

For each dataset, output the coefficients of its minimal polynomial on integers F(X) = adXd + ad-1Xd-1 + ... a1X + a0<tex2html_verbatim_mark> , in the following format.

ad  ad-1 ..  a1  a0

<tex2html_verbatim_mark>

Non-negative integers must be printed without a sign (+ or -). Numbers in a single line must be separated by a single space and no other characters or extra spaces may appear in the output.

3 2 2 2
3 2 2 3
2 2 3 4
31 4 2 3
3 2 2 7
0 0 0 0

Sample Output

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <stack>
#include <queue>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <climits>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define PI 3.1415926535897932626
using namespace std;
int gcd(int a, int b) {return a % b == 0 ? b : gcd(b, a % b);}
#define MAXN 25
const double eps = 1e-8;
LL a,m,b,n;
LL C[MAXN][MAXN];
int Hash[MAXN][MAXN],tot;
double A[MAXN][MAXN];
void init()
{
for (int i = 0 ; i <= 20 ; i++)
{
C[i][0] = C[i][i] = 1;
for (int j = 1; j < i ; j++)
C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
}
tot = 0;
for (int i = 0 ; i < m ; i++)
for (int j = 0 ; j < n ; j++)
Hash[i][j] = tot++;
}
void build()
{
memset(A,0,sizeof(A));
A[0][0] = 1;
for (int i = 1; i <= tot ; i++)
{
for (int j = 0 ; j <= i ; j++)
{
int l = j , r = i - j;
double tmp = C[i][l] * pow(a * 1.0,l / m) * pow(b * 1.0,r / n);
l %= m ; r %= n;
A[Hash[l][r]][i] += tmp;
}
}
A[tot][tot] = 1;
A[tot][tot + 1] = 1;
tot++;
}
void print(double x)
{
char s[100];
sprintf(s,"%.0lf",x);
if (strcmp(s,"-0") == 0) printf(" %s",s + 1);
else printf(" %s",s);
}
void gauss()
{
for (int i = 0 ; i < tot ; i++)
{
int r = i;
for (int j = i + 1; j < tot ; j++)
{
if (fabs(A[j][i]) > fabs(A[r][i])) r = j;
}
if (fabs(A[r][i]) < eps) continue;
for (int j = i ; j <= tot ; j++) swap(A[r][j],A[i][j]);
for (int j = 0 ; j < tot ; j++)
{
if (i == j) continue;
if (fabs(A[j][i]) < eps) continue;
double tmp = A[j][i] / A[i][i];
for (int k = i ; k <= tot ; k++)
{
A[j][k] -= tmp * A[i][k];
}
}
}
printf("1");
for (int i = tot - 2; i >= 0; i--)
print(A[i][tot] / A[i][i]);
printf("\n");
}
int main()
{
while(scanf("%lld%lld%lld%lld",&a,&m,&b,&n) != EOF)
{
if (n == 0 && m == 0 && b == 0 && n == 0) break;
init();
build();
gauss();
}
return 0;
}

  

1 0 -10 0 1
1 0 -9 -4 27 -36 -23
1 0 -8 0 18 0 -104 0 1
1 0 0 -8 -93 0 24 -2976 2883 -32 -3720 -23064 -29775
1 0 -21 0 189 0 -945 -4 2835 -252 -5103 -1260 5103 -756 -2183 这里思路比较简单。注意有个负0处理参照了别人了的代码。照着抄的。。
思路就是简单记录a,b的各种次幂组合根据组合数确定系数。最后为0.注意最高项为1;

UVALIVE 3891 The Teacher's Side of Math的更多相关文章

  1. UVALive 6073 Math Magic

                                                  6073 Math MagicYesterday, my teacher taught us about m ...

  2. UVA 1397 - The Teacher&#39;s Side of Math(高斯消元)

    UVA 1397 - The Teacher's Side of Math 题目链接 题意:给定一个x=a1/m+b1/n.求原方程组 思路:因为m*n最多20,全部最高项仅仅有20.然后能够把每一个 ...

  3. Math Magic(完全背包)

    Math Magic Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Sta ...

  4. ZOJ3662:Math Magic(全然背包)

    Yesterday, my teacher taught us about math: +, -, *, /, GCD, LCM... As you know, LCM (Least common m ...

  5. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  6. sqlalchemy 学习-- 多表操作

    一对多:一对一 # one -- many class Students(Base): __tablename__ = "students" sid = Column(Intege ...

  7. sqlalchemy 学习--单表操作

    以下所有代码片段都使用了统一的引用,该引用如下: from sqlalchemy import create_engine, ForeignKey from sqlalchemy.ext.declar ...

  8. [转] fitnesse中的Map处理

    http://blog.csdn.net/doubeizhucele/article/details/42263887 fintesse会把!{}标记的变量视为HashTable对象,展现到页面上的将 ...

  9. zoj3662Math Magic

    Math Magic Time Limit: 3 Seconds       Memory Limit: 32768 KB Yesterday, my teacher taught us about ...

随机推荐

  1. 第二篇 Fiddler配置_浏览器&手机

    什么是Fiddler? 网络项目的开发和测试中,Fiddler是强大的抓包工具,它的原理是以web代理服务器的形式进行工作的 ,可以说是非常常用的手头工具了,本文就Fiddler使用和配置进行说明. ...

  2. [HNOI2004]打鼹鼠

    鼹鼠是一种很喜欢挖洞的动物,但每过一定的时间,它还是喜欢把头探出到地面上来透透气的.根据这个特点阿牛编写了一个打鼹鼠的游戏:在一个\(n*n\)的网格中,在某些时刻鼹鼠会在某一个网格探出头来透透气.你 ...

  3. ardupilot_gazebo仿真(四)

    ardupilot_gazebo仿真(四) 标签(空格分隔): 未分类 Multi-MAV simulation 参考官网给出的multi-vehicle-simulation的方法 在每次打开sim ...

  4. [Java-Idea]解决idea启动项目报错:Unable to open debugger port(127.0.0.1:53046):java.net.SocketException"socket closed

    命令行窗口,执行命令:netstat -aon|findstr 9030 查找占用端口的进程 taskkill -f -pid 11331

  5. HDU 4436 str2int(后缀自动机)(2012 Asia Tianjin Regional Contest)

    Problem Description In this problem, you are given several strings that contain only digits from '0' ...

  6. multi-tap

    multi-tap又称 multi-press . 是在手机,或者电视遥控上的keypad定义,有如下2类标准: 1. ITU-T E.161 2.T9 使用举例如下: Consider a typi ...

  7. php数据缓存到文件类设计

    // 自定义缓存类 class Cache_Filesystem { // 缓存写保存 function set ($key, $data, $ttl) { //打开文件为读/写模式 $h = fop ...

  8. 【bzoj1465/bzoj1045】糖果传递 数论

    题目描述 老师准备了一堆糖果, 恰好n个小朋友可以分到数目一样多的糖果. 老师要n个小朋友去拿糖果, 然后围着圆桌坐好, 第1个小朋友的左边是第n个小朋友, 其他第i个小朋友左边是第i-1个小朋友. ...

  9. [CF1076E]Vasya and a Tree

    题目大意:给定一棵以$1$为根的树,$m$次操作,第$i$次为对以$v_i$为根的深度小于等于$d_i$的子树的所有节点权值加$x_i$.最后输出每个节点的值 题解:可以把操作离线,每次开始遍历到一个 ...

  10. [洛谷P3521][POI2011]ROT-Tree Rotations

    题目大意:给一棵$n(n\leqslant2\times10^5)$个叶子的二叉树,可以交换每个点的左右子树,要求前序遍历叶子的逆序对最少.输出最少的逆序对个数 题解:线段树合并,对于每个节点求出交换 ...