SETI
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total Submissions: 1706 |
|
Accepted: 1063 |
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 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
第一道1A的高斯消元
题意:给你一个素数p和一个字符串str,字符a~z价值分别为1~26,*价值为0,记字符串str长度为n。
已知对于每一位字符k(0 <= k < n-1),都有 f (k) = ∑0<=i<=n-1aiki (mod p) 。现在问你a[0] a[1] ... a[n-1]的解,题目保证有唯一解。
思路:n个变元列出n个方程,直接就可以高斯了。注意要用快速幂对k^i取模,还有高斯消元的回代过程,一定要保证最后有解。
AC代码:
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#define MAXN 100
using namespace std;
int a[MAXN][MAXN];
int x[MAXN];
int p;
char str[MAXN];
int equ, var;
int pow_mod(int a, int n)
{
int ans = 1;
while(n)
{
if(n & 1)
ans = ans * a % p;
a = a * a % p;
n >>= 1;
}
return ans;
}
void init_a()
{
scanf("%d%s", &p, str);
int len = strlen(str);
var = equ = len;
memset(a, 0, sizeof(a));
for(int i = 0; i < len; i++)
{
if(str[i] == '*')
a[i][var] = 0;
else
a[i][var] = str[i] - 'a' + 1;
a[i][var] %= p;
for(int j = 0; j < len; j++)
a[i][j] = pow_mod(i+1, j);
}
}
int gcd(int a, int b){
return b == 0 ? a : gcd(b, a%b);
}
int lcm(int a, int b){
return a / gcd(a, b) * b;
}
void Gauss()
{
int max_r, k;
int col = 0;
for(k = 0; k < equ && col < var; k++, col++)
{
max_r = k;
for(int i = k+1; i < equ; i++)
if(abs(a[i][col]) > abs(a[max_r][col]))
max_r = i;
if(k != max_r)
for(int i = col; i < var+1; i++)
swap(a[k][i], a[max_r][i]);
if(a[k][col] == 0)
{
k--;
continue;
}
for(int i = k+1; i < equ; i++)
{
if(a[i][col])
{
int LCM = lcm(a[k][col], a[i][col]);
int ta = LCM / abs(a[i][col]);
int tb = LCM / abs(a[k][col]);
if(a[k][col] * a[i][col] < 0)
tb = -tb;
for(int j = col; j < var+1; j++)
a[i][j] = ((a[i][j] * ta - a[k][j] * tb) % p + p) % p;
}
}
}
for(int i = var-1; i >= 0; i--)
{
int temp = a[i][var];
for(int j = i+1; j < var; j++)
if(a[i][j])
temp = ((temp - x[j] * a[i][j]) % p + p) % p;
while(temp % a[i][i]) temp += p;//题目保证有解 必须进行到能够整除
x[i] = temp / a[i][i] % p;
}
for(int i = 0; i < var; i++)
{
if(i) printf(" ");
printf("%d", x[i]);
}
printf("\n");
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
{
init_a();
Gauss();
}
return 0;
}