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)=∑n−1i=0aiki(modp)
always evaluates to values
0<=f(k)<=26
for
1<=k<=n
, provided that the correct value of
p
is used.
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
..
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
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
Source
Northwestern Europe 2004
题目大意
给定一个素数
思路
将上面的方程组化成矩阵的形式就是:
其次,高斯消元(代入)的话就是解形如 a×x=bmodp 这样的同余方程,用逆元解即可。
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
const int maxn=70;
const int maxm=30000;
int n,p,inv[maxm+1];
long long gcd(long long a,long long b)
{
if(!b)
{
return a;
}
return gcd(b,a%b);
}
struct matrix
{
long long a[maxn+1][maxn+2];
inline int gauss()
{
for(register int i=1; i<n; ++i)
{
if(!a[i][i])
{
int j=i+1;
while(j<=n)
{
if(a[j][i])
{
break;
}
++j;
}
if(j>n)
{
return 1;
}
for(register int k=i; k<=n+1; ++k)
{
std::swap(a[j][k],a[i][k]);
}
}
for(register int j=i+1; j<=n; ++j)
{
if(!a[j][i])
{
continue;
}
if(a[j][i]%a[i][i])
{
int d=gcd(a[j][i],a[i][i])*a[i][i];
for(register int k=i; k<=n+1; ++k)
{
a[j][k]=a[j][k]*d;
}
}
int d=a[j][i]/a[i][i]%p;
for(register int k=i; k<=n+1; ++k)
{
a[j][k]-=d*a[i][k]%p;
a[j][k]=(a[j][k]%p+p)%p;
}
}
}
for(register int i=n; i>=1; --i)
{
a[i][n+1]=inv[a[i][i]]*a[i][n+1]%p;
a[i][i]=1;
for(register int j=1; j<i; ++j)
{
a[j][n+1]-=a[i][n+1]*a[j][i];
a[j][n+1]=(a[j][n+1]%p+p)%p;
a[j][i]=0;
}
}
return 0;
}
inline int print_ans()
{
for(register int i=1; i<n; ++i)
{
printf("%I64d ",a[i][n+1]);
}
printf("%I64d\n",a[n][n+1]);
return 0;
}
};
matrix t;
int ti;
char s[maxn+2];
int main()
{
scanf("%d",&ti);
while(ti--)
{
scanf("%d%s",&p,s+1);
n=strlen(s+1);
inv[1]=1;
for(register int i=2; i<=p; ++i)
{
inv[i]=(p-p/i)*inv[p%i]%p;
}
for(register int i=1; i<=n; ++i)
{
t.a[i][1]=1;
for(register int j=2; j<=n; ++j)
{
t.a[i][j]=t.a[i][j-1]*i%p;
}
t.a[i][n+1]=(s[i]=='*')?0:s[i]-'a'+1;
}
t.gauss();
t.print_ans();
}
return 0;
}