POJ2065-SETI

来源:互联网 发布:sql触发器实例 old 编辑:程序博客网 时间:2024/06/12 17:34

SETI

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 2144 Accepted: 1318
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

题目大意:给出一个模数和一个字符串,字符串的长度为方程的个数,即n个方程n个未知数,字符串对应相应的数值。
解题思路:高斯消元

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<set>using namespace std;typedef long long LL;const int INF=0x3f3f3f3f;const int MAXN=100;int a[MAXN][MAXN];//增广矩阵int x[MAXN];//解集bool free_x[MAXN];//标记是否是不确定的变元char s[MAXN];int mod;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;}//先除后乘//equ个方程,var个变元,增广矩阵行[0,equ-1],列[0,var]//返回值:-2浮点数解无整数解;-1无解;0唯一解;>0无穷解,返回自由变元个数int Gauss(int equ,int var){    int i,j,k;    int max_r;//当前列绝对值最大的行    int col;//当前处理的列    int ta,tb;    int LCM;    int tmp;    int free_x_num;    int free_index;    for(int i=0;i<=var;++i)    {        x[i]=0;        free_x[i]=true;    }    //转换为阶梯阵    col=0;//当前处理的列    for(k=0;k<equ&&col<var;++k,++col)    {        //枚举当前处理的行        //找到该col列元素绝对值最大的那行与第k行交换        max_r=k;        for(i=k+1;i<equ;++i)        {            if(abs(a[i][col])>abs(a[max_r][col]))                max_r=i;        }        if(max_r!=k)        {            for(j=k;j<var+1;++j) swap(a[k][j],a[max_r][j]);        }        if(a[k][col]==0)        {            //说明该col列第k行以下全是0了,则处理当前行的下一列            --k;            continue;        }        for(i=k+1;i<equ;++i)        {            if(a[i][col]!=0)            {                LCM=lcm(abs(a[i][col]),abs(a[k][col]));                ta=LCM/abs(a[i][col]);                tb=LCM/abs(a[k][col]);                if(a[i][col]*a[k][col]<0) tb=-tb;                for(j=col;j<var+1;++j)                {                    //a[i][j]=a[i][j]*ta-a[k][j]*tb;                    a[i][j]=((a[i][j]*ta)%mod-(a[k][j]*tb)%mod+mod)%mod;                }            }        }    }    //无解,化简的增广矩阵中存在(0,0,...,a)这样的行    for(i=k;i<equ;++i)    {        if(a[i][col]!=0) return -1;    }    //无穷解,出现(0,0,...,0)这样的行,无严格上三角    if(k<var)    {        for(i=k-1;i>=0;--i)        {            free_x_num=0;            for(j=0;j<var;++j)            {                if(a[i][j]!=0&&free_x[j])                    ++free_x_num,free_index=j;            }            if(free_x_num>1) continue;//无法求解出确定的变元            tmp=a[i][var];            for(j=0;j<var;++j)            {                if(a[i][j]!=0&&j!=free_index)                    tmp-=a[i][j]*x[j];            }            x[free_index]=tmp/a[i][free_index];//求出该变元            free_x[free_index]=0;//该变元是确定的        }        return var-k;//自由变元有var-k个    }    //唯一解xn-1,xn-2,...,x0    for(i=var-1;i>=0;--i)    {        tmp=a[i][var];        for(j=i+1;j<var;++j)        {            if(a[i][j]!=0)            {                tmp-=a[i][j]*x[j];                //取模加上下面这句                tmp=(tmp%mod+mod)%mod;            }        }        while(tmp%a[i][i]) tmp+=mod;        //if(tmp%a[i][i]!=0) return -2;//说明有浮点数解,但无整数解        x[i]=(tmp/a[i][i])%mod;        //x[i]=tmp/a[i][i];    }    return 0;}//快速模幂int Q_mod(int a,int b,int m){    int res=1;    a%=m;    while(b)    {        if(b&1) {res*=a;res%=m;}        a*=a;a%=m;b>>=1;    }    return res;}int main(){    int T;    scanf("%d",&T);    while(T--)    {        scanf("%d%s",&mod,s);        int len=strlen(s);        memset(a,0,sizeof(s));        for(int i=0;i<len;++i)        {            if(s[i]=='*')  a[i][len]=0;            else a[i][len]=s[i]-'a'+1;            for(int j=0;j<len;++j) a[i][j]=Q_mod(i+1,j,mod);        }        Gauss(len,len);        for(int i=0;i<len;++i)        {            printf("%d%c",x[i],i==len-1?'\n':' ');        }    }    return 0;}
原创粉丝点击