POJ 2065 SETI(高斯消元)

46 篇文章 0 订阅
23 篇文章 0 订阅

看懂题目之后方程就很好列出来了啊:

题目大意:给你一个素数P(P<=30000)和一串长为n的字符串str[]。字母'*'代表0,字母a-z分别代表1-26,这n个字符所代表的数字分别代表f(1)、f(2)....f(n)。

定义: f (k) = ∑0<=i<=n-1aiki (mod p) (1<=k<=n,0<=ai<P)

求a0、a1.....an-1。题目保证肯定有唯一解

所以可以得到一个公式:

a0*1^0 + a1*1^1+a2*1^2+........+an-1*1^(n-1) = f(1)

a0*2^0 + a1*2^1+a2*2^2+........+an-1*2^(n-1) = f(2)

......

a0*n^0 + a1*n^1+a2*n^2+........+an-1*n^(n-1) = f(n)

列出来之后高斯消元解出来每一个未知数的系数。
取模问题在最后迭代系数的时候要加上k*p保证可以取余=0。
SETI
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 1502 Accepted: 923

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
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
#define eps 1e-10
///#define M 1000100
#define LL __int64
///#define LL long long
#define INF 0x7fffffff
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)

const int maxn = 340;

using namespace std;

int a[maxn][maxn];
int x[maxn];
int equ, var;
char str[maxn];

int LCM(int a, int b)
{
    return (a/(__gcd(a, b)))*b;
}

int Gauss(int mod)
{
    int row, col, max_r;
    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; 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;
            int l = LCM(abs(a[row][col]), abs(a[i][col]));
            int ta = l/a[i][col];
            int tb = l/a[row][col];
            if(ta*tb < 0) tb *= -1;///判断是否异号
            for(int j = col; j <= var; j++)
                a[i][j] = ((a[i][j]*ta - a[row][j]*tb)%mod + mod)%mod;
        }
        row++;
        col++;
    }
    for(int i = row; i < equ; i++)///无解的情况;
        if(a[i][col] != 0) return -1;
    if(row < var)///多组解的情况
        return var-row;
    for(int i = var-1; i >= 0; i--)///唯一解的情况,根据上三角阵,迭代求出每一次的值
    {
        int tmp = a[i][var];
        for(int j = i+1; j < var; j++)
            if(a[i][j] != 0) tmp = ((tmp-a[i][j]*x[j])%mod + mod)%mod;
        while(tmp%a[i][i] != 0)
            tmp += mod;
        x[i] = tmp/a[i][i]%mod;
    }
    return 0;
}

int Get_Mod(int x, int t, int mod)
{
    int tmp = 1;
    for(int i = 1; i <= t; i++)
    {
        tmp *= x;
        tmp %= mod;
    }
    return tmp;
}

int Get(char st)
{
    if(st == '*')
        return 0;
    return st-'a'+1;
}

void init()
{
    memset(x, 0, sizeof(x));
    memset(a, 0, sizeof(a));
}

int main()
{
    int T;
    cin >>T;
    while(T--)
    {
        init();
        int mod;
        scanf("%d %s",&mod, str);
        int n = strlen(str);
        equ = var = n;
        for(int i = 0; i < n; i++)
        {
            a[i][n] = Get(str[i]);
            for(int j = 0; j < n; j++)
                a[i][j] = Get_Mod(i+1, j, mod);
        }
        Gauss(mod);
        for(int i = 0; i < n-1; i++)
            cout<<x[i]<<" ";
        cout<<x[n-1]<<endl;
    }
    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值