SETI - POJ 2065 高斯消元

本文探讨了在天文学中通过电磁信号研究外星文明的可能性,具体阐述了一种利用数学函数和模运算来解析接收信号的方法,进而将数字序列转换为英文字母和特殊字符,以便更易于理解外星信息。文章详细介绍了该过程,包括素数选择、序列生成、转换算法以及实际应用案例,旨在提供一种科学手段来破译遥远宇宙的潜在信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SETI
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 1698 Accepted: 1056

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

题意:给你一个用于取模的素数p,还有f(1)-f(n)的值, f (k) = ∑0<=i<=n-1aiki (mod p),求所有a的值,题目保证有解。

思路:首先用高斯消元得到ai%p=k,然后用扩展欧几里得得到ai的值。其实0到p-1直接枚举也能过。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
int T,t,n,m;
int a[110][110],MOD,x[110];
char s[110];
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;
}
void ex_gcd(int a,int b,int &d,int &x,int &y)
{
    if(!b)
    {
        x=1;
        y=0;
        d=a;
    }
    else
    {
        ex_gcd(b,a%b,d,y,x);
        y-=x*(a/b);
    }
}
int solve(int a,int c)
{
    int b=MOD,d,x,y,e;
    ex_gcd(a,b,d,x,y);
    e=abs(b/d);
    return (x*c/d%e+e)%e;
}
int gauss(int N,int M)
{
    int i,j,k,max_r,col,temp,LCM,ta,tb;
    memset(x,0,sizeof(x));
    k=1;col=1;
    for(;k<=N && col<=M;k++,col++)
    {
        max_r=k;
        for(i=k+1;i<=N;i++)
           if(abs(a[i][col])>abs(a[max_r][col]))
             max_r=i;
        if(max_r!=k)
          for(j=k;j<=M+1;j++)
             swap(a[k][j],a[max_r][j]);
        if(a[k][col]==0)
        {
            k--;
            continue;
        }
        for(i=k+1;i<=N;i++)
           if(a[i][col]!=0)
           {
               LCM=lcm(abs(a[k][col]),abs(a[i][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<=M+1;j++)
                  a[i][j]=((a[i][j]*ta-a[k][j]*tb)%MOD+MOD)%MOD;
           }
    }
    for(i=M;i>=1;i--)
    {
        temp=a[i][M+1];
        for(j=i+1;j<=M;j++)
           if(a[i][j]!=0)
             temp=((temp-a[i][j]*x[j])%MOD+MOD)%MOD;
        x[i]=solve(a[i][i],temp);
    }
}
int main()
{
    int i,j,k;
    scanf("%d",&T);
    for(t=1;t<=T;t++)
    {
        scanf("%d",&MOD);
        scanf("%s",s+1);
        n=strlen(s+1);
        for(i=1;i<=n;i++)
        {
            k=1;
            for(j=1;j<=n;j++)
            {
                a[i][j]=k;
                k=k*i%MOD;
            }
            if(s[i]=='*')
              a[i][n+1]=0;
            else
              a[i][n+1]=s[i]-'a'+1;
        }
        gauss(n,n);
        printf("%d",x[1]);
        for(i=2;i<=n;i++)
           printf(" %d",x[i]);
        printf("\n");
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值