Uvalive 3891 The Teacher ' s Side of Math

Source: Internet
Author: User

One of the tasks students routinely carry out in their mathematics classes are 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 have A given root. Ms. Galsone is a enthusiastic mathematics teacher who was bored with finding solutions of quadratic equations Simple as a + b<tex2html_verbatim_mark>. She wanted to make Higher-degree equations whose solutions is 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). Writing a program this 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> b<tex2html_verbatim_mark> are distinct prime Numbers, and m<tex2html_verbatim_mark> and n<tex2html_verbatim_mark> is integers greater than 1.

In this problem, you is asked to find t<tex2html_verbatim_mark> ' s minimal polynomial on intege RS, which is the polynomial F(X) = adxD + ad-1x D-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 S.
    4. F (X) <tex2html_verbatim_mark> is primitive. That is, coefficients a0,..., ad<tex2html_verbatim_mark> has no common divisors Greater than one.


For example, the minimal polynomial of + <tex2html_verbatim_mark> on integers is F(X) = C4>x4-10X2 + 1<tex2html_verbatim_mark>. Verifying F(t) = 0<tex2html_verbatim_mark> is as simple as the following (=, = <tex2h tml_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> are in fact minimum are a bit more dif Ficult. Fortunately, under the condition given in this problem, which are that a<tex2html_verbatim_mark> and b<tex2html_verbatim_mark> is distinct prime numbers and m<tex2html_verbatim_mark > n<tex2html_verbatim_mark> greater than one, the degree of the minimal polynomial is always c11>mn<tex2html_verbatim_mark>. Moreover, it's always monic. That is, the coefficient of it 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 is line represents + <tex2html_verbatim_mark>. The last dataset was followed by a and consisting of four zeros. Numbers in a single line is 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) & Lt;tex2html_verbatim_mark> and (231-1) <tex2html_verbatim_mark>, inclusive.

Output

For each dataset, output the coefficients of it 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 is printed without a sign (+ or-). Numbers in a, must is separated by a, and no other characters or extra spaces could appear in the out Put.

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.1415926535897932626using namespace Std;int gcd (int a, int b) {return a% b = = 0? b:gcd (b, a% b);} #define MAXN 25const 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 <=; 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


The idea here is relatively simple. Note that there is a negative 0 process that references someone else's code. According to the copy of the.
The idea is to simply record the various power combinations of a, B and determine the coefficients based on the combined number. The last is 0. Note that the highest item is 1;

Uvalive 3891 The Teacher ' s Side of Math

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.