poj 2065 SETI(高斯消元)

SETI
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 1469 Accepted: 899

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, ...a n-1 the function f (k) = ∑ 0<=i<=n-1a ik i (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 <= a i < 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


题意:求满足n条式子的a0……an的值,必定有解,需mod p

题解:高斯消元解题目给出的方程组,a0……an是未知数


#include<stdio.h>
#include<string.h>
char s[108];
int matrix[78][78];
int mod,ans[78];
int init()
{
    int n,i,j,temp,res;

    memset(matrix,0,sizeof(matrix));
    memset(ans,0,sizeof(ans));
    n=strlen(s);
    for(i=0; i<n; i++)
    {
        if(s[i]=='*') matrix[i][n]=0;
        else matrix[i][n]=s[i]-'a'+1;
    }
    for(i=0;i<n;i++)
    {
        temp=i+1,res=1;
        for(j=0;j<n;j++)
        {
            matrix[i][j]=res;
            res=(temp*res)%mod;
        }
    }

    return n;
}
int myfind(int x,int y)
{
    int i=0;

    x=(x%mod+mod)%mod;
    y=(y%mod+mod)%mod;
    for(;;i++)
    {
        if(x*i%mod==y) return i;
    }
}
void guass(int n)
{
    int i,j,x,y,t,a,b,sum;

    for(i=j=0;j<n;j++)
    {
        for(x=i;x<n;x++) if(matrix[x][j]) break;
        if(x>=n) continue;
        for(y=0;y<=n;y++) t=matrix[i][y],matrix[i][y]=matrix[x][y],matrix[x][y]=t;
        a=matrix[i][j];
        for(x=i+1;x<n;x++)
        {
            if(!matrix[x][j]) continue;
            b=matrix[x][j];
            for(y=0;y<=n;y++) matrix[x][y]=(matrix[x][y]*a-matrix[i][y]*b)%mod;
        }
        i++;
    }
    for(i=n-1;i>=0;i--)
    {
        sum=0;
        for(j=i+1;j<n;j++) sum=(sum+matrix[i][j]*ans[j])%mod;
        ans[i]=myfind(matrix[i][i],matrix[i][n]-sum);
    }
}
int main()
{
    int t,n,i;

    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%s",&mod,s);
        n=init();
        guass(n);
        for(i=0;i<n;i++)
        {
            printf("%d",ans[i]);
            if(i==n-1) printf("\n");
            else printf(" ");
        }
    }

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值