SETI--高斯消元

来源:互联网 发布:java线程池使用demo 编辑:程序博客网 时间:2024/06/05 00:51

SETI
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 2030 Accepted: 1236

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

331 aaa37 abc29 hello*earth

Sample Output

1 0 00 1 08 13 9 13 4 27 18 10 12 24 15

题目链接:http://poj.org/problem?id=2065


这个高斯消元快搞死我了,各种bug各种改。。。。

附上大神的题意:

题意:

输入一个素数p和一个字符串s(只包含小写字母和‘*’),字符串中每个字符对应一个数字,'*'对应0,‘a’对应1,‘b’对应2....

例如str[] = "abc", 那么说明 n=3, 字符串所对应的数列为1, 2, 3。

题目中定义了一个函数:

a0*1^0 + a1*1^1+a2*1^2+........+an-1*1^(n-1) = f(1)(mod p), f(1) = str[0] = a = 1;
a0*2^0 + a1*2^1+a2*2^2+........+an-1*2^(n-1) = f(2)(mod p), f(2) = str[1] = b = 2;
..........
a0*n^0 + a1*n^1+a2*n^2+........+an-1*n^(n-1) = f(n)(mod p),f(n) = str[n-1] = ````

求出 a0,a1,a2....an-1.


思路:若字符串长度为n,那么这是一个含有n个方程n个未知数的线性方程组。所以只需把相应的系数转为成矩阵解方程组。高斯消元+同余方程。。

具体高斯消元的讲解看我上一篇博客

代码:

#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>using namespace std;int p;char s[100];int equ,var;int a[100][100];int x[100];int lcm(int a,int b){    return a*b/__gcd(a,b);}int mod_exp(int a,int b,int c){//快速幂取模    int res=1;    int t=a%c;    while(b){        if(b&1){            res=res*t%c;        }        t=t*t%c;        b>>=1;    }    return res;}int Gauss(){    int row,col,max_r;    int ta,tb,tmp,l;    row=col=0;    while(row<equ&&col<var){        max_r=row;        for(int i=row+1;i<equ;i++){            if(abs(a[i][col])>abs(a[max_r][col])){                max_r=i;            }        }        if(max_r!=row){            for(int j=col;j<var+1;j++){                swap(a[row][j],a[max_r][j]);            }        }        if(a[row][col]==0){            col++;            continue;        }        for(int i=row+1;i<equ;i++){            if(a[i][col]==0){                continue;            }            l=lcm(abs(a[i][col]),abs(a[row][col]));            ta=l/a[i][col];            tb=l/a[row][col];            if(a[i][col]*a[row][col]<0)                tb=-tb;            for(int j=col;j<var+1;j++){                a[i][j]=((a[i][j]*ta-a[row][j]*tb)%p+p)%p;            }        }        row++;        col++;    }    for(int i=row;i<var;i++){//无解        if(a[i][var]!=0){            return -1;        }    }    if(row<var)//输出自由变元的个数        return var-row;    for(int i=var-1;i>=0;i--){        tmp=a[i][var];        for(int j=i+1;j<var;j++){            tmp=((tmp-a[i][j]*x[j])%p+p)%p;        }        while(tmp%a[i][i]!=0){            tmp+=p;        }        x[i]=(tmp/a[i][i])%p;    }    return 0;}int main(){    int t;    scanf("%d",&t);    while(t--){        scanf("%d %s",&p,s);        equ=var=strlen(s);        for(int i=0;i<equ;i++){            if(s[i]=='*'){                a[i][var]=0;            }            else                a[i][var]=s[i]-'a'+1;            for(int j=0;j<var;j++){                a[i][j]=mod_exp(i+1,j,p);            }        }        Gauss();        for(int i=0;i<var-1;i++){            printf("%d ",x[i]);        }        printf("%d\n",x[var-1]);    }    return 0;}




0 0
原创粉丝点击