poj2065-SETI

poj2065-SETI

SETI
Time Limit: 1000MS

Memory Limit: 30000K
Total Submissions: 2254

Accepted: 1375
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) = ∑0<=i<=n-1aiki (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 <= 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
Source
Northwestern Europe 2004

Northwestern Europe 2004

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int MAXN = 80;

int MOD, n;
char bri[MAXN];
int a[MAXN][MAXN], ans[MAXN];

void print(){
    //check a is ok ------------
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n+1; j++)
        cout<<a[i][j]<<" ";
      cout<<endl;
    }
    cout<<endl;
    //--------------------------
}

int FAST(int num, int m){
    int res = 1;
    while (m) {
        if (m & 1) res = (res * num) % MOD;
        num = (num * num) % MOD;
        m >>= 1;
    }
    return res;
}

void gauss(){
    int r = 1;
    //解增廣矩陣 
    for (int i = 1; i <= n; i++) {//枚舉主元位置 
      //找第i列主元非零的行與第r行交換。 
      for (int j = r; j <= n; j++)
        if (a[j][i]) {
          for (int k = 1; k <= n+1; k++)
            swap(a[j][k], a[r][k]);  
          break;
        } 

      //已經爲零不用計算了   
      if (!a[r][i]) continue;

      //化簡增廣矩陣 
      for (int j = 1; j <= n+1; j++)
        if (j != r && a[j][i]){
          int tmp = a[j][i];
          for (int k = 1; k <= n+1; k++)
            a[j][k] = (a[j][k] * a[r][i] - a[r][k] * tmp + MOD) % MOD;
        }
      r++;
    }
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        if (a[i][j])
          ans[i] = (a[i][n+1] * FAST(a[i][j], MOD-2)) % MOD;
}

int main()
{

    int flag;
    scanf("%d", &flag);
    while(flag--){
      scanf("%d%s", &MOD, bri);
      n = strlen(bri);
      for (int i = 1; i <= n; i++) {
        a[i][1] = 1; 
        for (int j = 2; j <= n; j++)
          a[i][j] = (a[i][j-1] * i) % MOD;
        if (bri[i-1] == '*') a[i][n+1] = 0;
        else a[i][n+1] = bri[i-1] - 'a' + 1;
      }
      gauss();
      for (int i = 1; i <= n; i++)
        printf("%d ", (ans[i] + MOD) % MOD);
      printf("\n");
    }
    return 0;
} 

題目大意
解一個
x1*i^0 + x2*i^1 + … + xn * i^(n-1) = f(i)
(i爲1…n的自然數)
的n元一次方程組,其中f(i)題目中給出a-1…z-26, *-0。結果mod質數,保證有解。

這道題是高斯消元的模板題,因爲是mod一個數,所以應該用兩行同時乘對應的主元位置的元素。從頭到尾掃一遍增廣矩陣做減法,而不能簡單的從主元位置作減法(廢話)。
還有要用到逆元,直接快速冪。
注意:結果modMOD

發佈了32 篇原創文章 · 獲贊 1 · 訪問量 3987
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章