POJ-2065 SETI Gaussian elimination, extended GCD

Source: Internet
Author: User

This question is given as follows:

F (1) = C1 (MOD) P
F (2) = c2 (MOD) P
F (3) = C3 (MOD) p...

F (1) = a () * X1 + a (1, 2) * X2 + a (1, 3) * x3...

Where a (I, j) = I ^ (J-1 ).

In the face of such an equation, our approach is to ignore the (MOD) P on the right side of the equation, because f (I) and CI are the same, therefore, multiplying a number at both ends of an equation does not affect the performance of a given equation. So the rest is the Gaussian elimination element. Because the solution is unique, the obtained coefficient matrix must be full rank. Next, you can use the extended GCD to solve unknown elements.

The Code is as follows:

# Include <cstdlib> # include <cstring> # include <algorithm> # include <cstdio> using namespace STD; typedef long int int64; char s [100]; int length, MOD; int64 X [100]; int _ POW (int A, int B) {// fast power int ret = 1; while (B) {If (B & 1) {RET * = A; RET % = MOD;} A * = A; A % = MOD; B >>= 1;} return ret ;} struct matrix {int64 A [100] [100]; void Init () {for (INT I = 1; I <= length; ++ I) {for (Int J = 1; j <= Length; ++ J) {A [I] [J] = _ POW (I, J-1 );} A [I] [Length + 1] = s [I] ;}} void rswap (INT X, int y, int S) {// used to exchange the following rows to the first row for int64 t; for (Int J = s; j <= Length + 1; ++ J) {T = A [x] [J]; A [x] [J] = A [y] [J]; A [y] [J] = T ;}} void multi (int x, int64 M, int s) {// multiply the same row by a certain number. Of course, for (Int J = s; j <= Length + 1; ++ J) {A [x] [J] * = m;} void relax (int x, int y, int S) {// iterate an equation into another expression for (Int J = S; j <= Length + 1; ++ J) {A [y] [J]-= A [x] [J]; A [y] [J] = (a [y] [J] % mod + mod) % MOD; A [x] [J] = (a [x] [J] % mod + mod) % mod ;}} void print () {for (INT I = 1; I <= length; ++ I) {for (Int J = 1; j <= Length + 1; ++ J) {printf ("% 5i64d ", A [I] [J]);} puts ("");} puts ("") ;}} m; int64 gcd (int64 A, int64 B) {int64 T; while (B) {T = B; B = A % B; A = T;} return a;} int64 lcm (int64 A, int64 B) {return /Gcd (a, B) * B;} int64 ext_gcd (int64 A, int64 B, int64 & X, int64 & Y) {int64 temp, RET; If (! B) {x = 1, y = 0; return a;} ret = ext_gcd (B, A % B, x, y); temp = x, x = Y, y = temp-A/B * Y; return ret;} void Gauss () {int I = 1, K; int64 a, B, sum; memset (x, 0, sizeof (x); For (Int J = 1; j <= length; ++ J) {for (k = I; k <= length; ++ K) {If (M. A [k] [J]) break;} If (k> length) continue; If (K! = I) {M. rswap (I, K, j) ;}for (k = I + 1; k <= length; ++ K) {If (M. A [k] [J]) {int64 LCM = lcm (M. A [I] [J], M. A [k] [J]); A = LCM/m. A [I] [J]; B = LCM/m. A [k] [J]; if (a> 1) M. multi (I, A, J); If (B> 1) M. multi (K, B, J); M. relax (I, K, j) ;}++ I ;}// M. print (); // there must be a unique solution, so no undefined determination for (k = I-1; k> = 1; -- k) {// Of course, here can be brute force to solve, after all, the given prime number is not very large int64 XX, YY, a, B, c = 0, L, G; For (Int J = k + 1; j <= length; ++ J) {C ++ = x [J] * m. A [k] [J];} A = m. A [k] [K], B = MOD; C = m. A [k] [Length + 1]-C; G = ext_gcd (a, B, XX, YY); L = B/g; XX * = C/g; xx = (XX % L + l) % L; X [k] = XX ;}for (Int J = 1; j <= length; ++ J) {printf (j = 1? "% I64d": "% i64d", X [J]);} puts ("") ;}int main () {int N; scanf ("% d ", & N); While (n --) {scanf ("% d % s", & mod, S + 1); length = strlen (S + 1 ); for (INT I = 1; I <= length; ++ I) {If (s [I]! = '*') {S [I]-= ('A'-1) ;}else {s [I] = 0 ;}} M. init (); Gauss ();} return 0 ;}

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.