SETI
Time Limit: 1000MS | Memory Limit: 30000KB | 64bit IO Format: %I64d & %I64u |
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, ...a n-1 the function f (k) = ∑ 0<=i<=n-1a ik i (mod 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 <= a i < 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 transcri_ption 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 transcri_ption 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.
Recently, it was discovered that if each message is assumed to be transmitted as a sequence of integers a0, a1, ...a n-1 the function f (k) = ∑ 0<=i<=n-1a ik i (mod 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 <= a i < 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 transcri_ption 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 transcri_ption 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 transcri_ption 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
//poj2065_SETI 高斯消元
/*
题目大意:给你一个素数P(P<=30000)和一串长为n的字符串str[]。字母'*'代表0,字母a-z分别代表1-26,这n个字符所代表的数字分别代表f(1)、f(2)....f(n)。
定义: f (k) = ∑0<=i<=n-1aiki (mod p) (1<=k<=n,0<=ai<P)
求a0、a1.....an-1。题目保证肯定有唯一解
解题思路:高斯消元。根据上面的公式显然可以列出有n个未知数的n个方程式:
a0*1^0 + a1*1^1+a2*1^2+........+an-1*1^(n-1) = f(1)
a0*2^0 + a1*2^1+a2*2^2+........+an-1*2^(n-1) = f(2)
......
a0*n^0 + a1*n^1+a2*n^2+........+an-1*n^(n-1) = f(n)
然后采用高斯消元法来解上面的方程组即可。
*/
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define N 100
int P,n;
int matrix[N][N],ans[N];
char str[N];
void init()
{
int i,j,k;
for(i = 0; i < n; i++)
if(str[i] == '*')
matrix[i][n] = 0;
else matrix[i][n] = str[i] - 'a' + 1;
for(i = 0; i < n; i++)
{
k = 1;
for(j = 0; j < n; j++)
{
matrix[i][j] = k;
k = (k*(i+1))%P;
}
}
}
int find(int a, int y,int P=100000)//如果方程中不需要取模则直接传入前两个参数
{
int i;
a = (a%P + P) % P; y = (y%P + P)%P;
for(i = 0; i < P; i++)
if((a*i)%P == y)
return i;
}
void gauss(int P=100000)//如果方程中不需要取模则不加参数
{
int i,j,k;
int row,col;
int a,b,c,sum;
row = 0; col = 0;
while(row < n && col < n)
{
for(i = row, j = -1; i < n; i++)
if(matrix[i][col] != 0)
{
j = i; break;
}
for(i = col; i < n; i++)
{
k = matrix[row][i];
matrix[row][i] = matrix[j][i];
matrix[j][i] = k;
}
a = matrix[row][col];
for(i = row+1; i < n; i++)
{
b = matrix[i][col];
for(j = col; j <= n; j++)
matrix[i][j] = (matrix[i][j]*a - matrix[row][j]*b)%P;
}
row++; col++;
}
for(i = n-1; i >= 0; i--)
{
sum = 0;
for(j = i+1; j < n; j++)
sum = (sum + matrix[i][j]*ans[j])%P;
ans[i] = find(matrix[i][i],matrix[i][n]-sum,P);
}
for(i = 0; i < n; i++)
{
printf("%d",ans[i]);
if(i == n-1) printf("\n");
else printf(" ");
}
}
int main()
{
int i,j,k;
int cases;
scanf("%d",&cases);
while(cases--)
{
scanf("%d %s",&P,str);
n = strlen(str);
init();
gauss(P);
}
return 0;
}