POJ2065 SETI

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,...an1 the function f(k)=n1i=0aiki(modp) 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 30000 .
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

Source

Northwestern Europe 2004

题目大意

给定一个素数p和一个字符串串 str 。令 fi=Num(stri) Num(stri) 表示当 stri 为*的时候为 0 stri为a-z的时候为 stri'a'+1 .接着得到 n stri的长度)个方程组,每个方程格式为

(k0×a1+k1×a2++kn1×an)modp=fkmodp(1kn)
ai 为未知量,让我们解这个方程组。

思路

将上面的方程组化成矩阵的形式就是:

1020n01121n11n12n1nn1f1f2fn
首先知道同余方程组可以互相相减,那么高斯消元的第一步(化成右上三角形式)与普通的高斯消元一样;
其次,高斯消元(代入)的话就是解形如 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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值