Poj 2605 SETI (Math _ Gaussian deyuan)

Source: Internet
Author: User

Given a prime number p and a string str. F [I] = Num (str [I]), Num (str [I]) indicates 0 when str [I] is, str [I] is str [I]-a + 1 when it is a-z.
Then we obtain the n (str [I] length) equations. The format of each equations is (a1 * k ^ 0 + a2 * k ^ 1 +... + an * k ^ (n-1) mod p = f [k] mod p (1 <= k <= n). ai is an unknown number. Let's solve this equations, output n solutions.

Problem-solving ideas: the bare Gaussian deyuan, I copied a template, but I still won't talk about the core idea of Gaussian deyuan, the line generation teacher died early.
Let's take a look at the general steps of the Gaussian elimination class I know: 1. Use question information to establish equations 2. Convert the equations into augmented matrices 3. Convert the augmented Matrices Into row step matrices 4. solve the problems. Step 2 is both a key and a difficulty. You can apply the template later. For detailed analysis, see this article:
Test data:
3
29 hello * earth
Out: 8 13 9 13 4 27 18 10 12 24 15

C producer code:
[Cpp]
# Include <iostream>
# Include <string>
# Include <string. h>
# Include <cmath>
# Include <stdio. h>
# Include <algorithm>
Using namespace std;
# Define MAX 71
 
 
Char str [MAX];
Int n, m, p, x [MAX];
Int mat [MAX] [MAX];
 
 
Void Initial (){

Int I, j, k = 1;
Memset (x, 0, sizeof (x ));
For (I = 0; I <n; ++ I ){

Mat [I] [m] = str [I] = '*'? 0: str [I]-'A' + 1;
 
For (k = 1, j = 0; j <m; ++ j)
Mat [I] [j] = k, k = k * (I + 1) % p;
// For (j = 0; j <m; ++ j)
// Printf ("% d % c", mat [I] [j], j = M-1? '\ N ':'');
}
}
Inline int gcd (int x, int y ){

Int r = x % y;
While (r ){

X = y, y = r;
R = x % y;
}
Return y;
}
Inline int lcm (int x, int y ){
 
Return x/gcd (x, y) * y;
}
Void Gauss (){
 
Int I, j, row, max_r, col;
Int ta, tb, temp, LCM;
Int free_x_num, free_index;
 
 
Col = row = 0;
For (; row <n & col <m; ++ row, ++ col ){
 
Max_r = row;
For (I = row + 1; I <n; ++ I)
If (abs (mat [I] [col])> abs (mat [max_r] [col])
Max_r = I;
 
 
If (max_r! = Row)
For (j = row; j <= m; ++ j)
Swap (mat [row] [j], mat [max_r] [j]);
If (mat [row] [col] = 0 ){
 
Row --;
Continue;
}
 
 
For (I = row + 1; I <n; ++ I)
If (mat [I] [col]! = 0 ){
 
LCM = lcm (abs (mat [I] [col]), abs (mat [row] [col]);
Ta = LCM/abs (mat [I] [col]);
Tb = LCM/abs (mat [row] [col]);
If (mat [I] [col] * mat [row] [col] <0)
Tb =-tb;
For (j = col; j <= m; ++ j)
Mat [I] [j] = (mat [I] [j] * ta-mat [row] [j] * tb) % p;
}
}
// Printf ("yes, you can ac \ n ");
 
 
For (I = m-1; I> = 0; -- I ){
 
Temp = mat [I] [m];
For (j = I + 1; j <m; ++ j)
If (mat [I] [j]! = 0)
Temp = (temp-mat [I] [j] * x [j]) % p + p) % p;
While (temp % mat [I] [I]) temp = temp + p;
X [I] = (temp/mat [I] [I] + p) % p;
}
}
 
 
Int main () www.2cto.com
{
Int I, j, k, t;

 
Scanf ("% d", & t );
While (t --){

Scanf ("% d % s", & p, str );
N = strlen (str );
M = n, Initial ();
 
 
Gauss ();
For (I = 0; I <m; ++ I)
Printf ("% d % c", x [I], I = M-1? '\ N ':'');
}
}

Author: woshi250hua

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.