POJ - 2065 SETI

SETI

👉传送门

Time Limit: 1000MS
Memory Limit: 30000K

Description

For some years, quite a lot of work has been put into listening to electromagnetic radio signals received from space, in order to understand what civilizations in distant galaxies might be trying to tell us. One signal source that has been of particular interest to the scientists at Universit´e de Technologie Spatiale is the Nebula Stupidicus.
Recently, it was discovered that if each message is assumed to be transmitted as a sequence of integers a0, a1, ...an-1 the function $f(k)=\sum\limits_{i=0}^{n-1}a_ik^i \pmod p$ always evaluates to values 0 <= f (k) <= 26 for 1 <= k <= n, provided that the correct value of p is used. n is of course the length of the transmitted message, and the ai denote integers such that 0 <= ai < p. p is a prime number that is guaranteed to be larger than n as well as larger than 26. It is, however, known to never exceed 30 000.
These relationships altogether have been considered too peculiar for being pure coincidences, which calls for further investigation.
The linguists at the faculty of Langues et Cultures Extraterrestres transcribe these messages to strings in the English alphabet to make the messages easier to handle while trying to interpret their meanings. The transcription procedure simply assigns the letters a..z to the values 1..26 that f (k) might evaluate to, such that 1 = a, 2 = b etc. The value 0 is transcribed to '*' (an asterisk). While transcribing messages, the linguists simply loop from k = 1 to n, and append the character corresponding to the value of f (k) at the end of the string.
The backward transcription procedure, has however, turned out to be too complex for the linguists to handle by themselves. You are therefore assigned the task of writing a program that converts a set of strings to their corresponding Extra Terrestial number sequences.

Input

On the first line of the input there is a single positive integer N, telling the number of test cases to follow. Each case consists of one line containing the value of p to use during the transcription of the string, followed by the actual string to be transcribed. The only allowed characters in the string are the lower case letters 'a'..'z' and '*' (asterisk). No string will be longer than 70 characters.

Output

For each transcribed string, output a line with the corresponding list of integers, separated by space, with each integer given in the order of ascending values of i.

Sample Input

3
31 aaa
37 abc
29 hello*earth

Sample Output

1 0 0
0 1 0
8 13 9 13 4 27 18 10 12 24 15

Tips

题意

给定一个素数 $p$ 和一组系数 $b$ ,请你求解同余方程组 $$\left\{\begin{aligned}
1^0 a_0+1^1 a_1+&\cdots+1^{n-1} a_{n-1}\equiv b_0&\pmod p\\
2^0 a_0+2^1 a_1+&\cdots+2^{n-1} a_{n-1}\equiv b_1&\pmod p\\
&\cdots\\
n^0 a_0+n^1 a_1+&\cdots+n^{n-1} a_{n-1}\equiv b_{n-1}&\pmod p
\end{aligned}\right.$$ 即,求解线性方程组 $$
\left[\begin{array}{cccc}
1^0 & 1^1 & \cdots & 1^{n-1}\\
2^0 & 2^1 & \cdots & 2^{n-1}\\
&&\cdots\\
n^0 & n^1 & \cdots & n^{n-1}
\end{array}\right]
\left[\begin{array}{cccc}
a_0\\
a_1\\
\vdots\\
a_{n-1}
\end{array}\right]\equiv
\left[\begin{array}{cccc}
b_0\\
b_1\\
\vdots\\
b_{n-1}
\end{array}\right]\pmod p
$$

题解

应用高斯消元法解方程即可。

步骤如下:

第一步,逐个消元,将系数矩阵转化为上三角矩阵。这一步的具体步骤为:

从第一行第一列开始,选择这一列中绝对值最大的一行,然后将其与当前处理的行交换。若这一行是 $0$ ,则表示余下的行都是 $0$ ,则转而处理下一列。否则,用这一行将它下面的数字都消成 $0$ 。

第二步,判断解的情况。若下面的全零行仍含有非零系数,则无解。若方程组个数小于未知数个数,则有多解。否则有一组定解,利用上三角矩阵解出即可。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <vector>
 5 using namespace std;
 6 const int MAXN=100;
 7 int n,p;
 8 char str[MAXN];
 9 vector<int> A[MAXN];
10 int X[MAXN];
11 int gauss(int raw,int col){
12     int r,c=0;
13     for (r=0;r<raw&&c<col;++r,++c){
14         int max_raw=r;
15         for (int i=r+1;i<raw;++i){
16             if (abs(A[max_raw][c])<abs(A[i][c]))
17                 max_raw=i;
18         }
19         if (max_raw!=r)
20             swap(A[max_raw],A[r]);
21         if (A[r][c]==0){
22             --r;
23             continue;
24         }
25         for (int i=r+1;i<raw;++i){
26             if (A[i][c]){
27                 int ta=abs(A[r][c]);
28                 int tb=abs(A[i][c]);
29                 if (A[i][c]*A[r][c]<0)
30                     tb=-tb;
31                 for (int j=c;j<=col;++j)
32                     A[i][j]=((A[i][j]*ta-A[r][j]*tb)%p+p)%p;
33             }
34         }
35     }
36     for (int i=r;i<raw;++i)
37         if (A[i][c]) return -1;
38     if (r<col) return col-r;
39     fill(X,X+col+1,0);
40     for (int i=col-1;i>=0;--i){
41         int tmp=A[i][col];
42         for (int j=i+1;j<col;++j){
43             if (A[i][j]) tmp-=A[i][j]*X[j];
44             tmp=(tmp%p+p)%p;
45         }
46         while (tmp%A[i][i]) tmp+=p;
47         X[i]=(tmp/A[i][i])%p;
48     }
49     return 0;
50 }
51 int len;
52 int main(){
53     scanf("%d",&n);
54     while (n--){
55         scanf("%d%s",&p,str);
56         len=strlen(str);
57         for (int i=0;i<len;++i)
58             A[i].resize(len+1);
59         for (int i=0;i<len;++i){
60             if (str[i]=='*') A[i][len]=0;
61             else A[i][len]=str[i]-'a'+1;
62             A[i][0]=1;
63             for (int j=1;j<len;++j)
64                 A[i][j]=(A[i][j-1]*(i+1))%p;
65         }
66         gauss(len,len);
67         printf("%d",X[0]);
68         for (int i=1;i<len;++i)
69             printf(" %d",X[i]);
70         puts("");
71     }
72     return 0;
73 }
View Code
posted @ 2019-08-03 16:36  Evlpsrfc  阅读(155)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end
代码高亮
代码高亮end