zoukankan      html  css  js  c++  java
  • POJ-2065-SETI(高斯消元)

    链接:

    https://vjudge.net/problem/POJ-2065

    题意:

    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.

    思路:

    建立方程组,在取模的情况下, 除法逆元处理即可。

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<math.h>
    #include<vector>
    
    using namespace std;
    typedef long long LL;
    const int INF = 1e9;
    
    const int MAXN = 100+10;
    
    int equ, var, p;
    int Mar[MAXN][MAXN];
    int X[MAXN];
    char s[MAXN];
    
    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;
    }
    
    int PowMod(int a, int b, int c)
    {
        int res = 1;
        while(b > 0)
        {
            if (b&1)
                res = res*a%c;
            a = a*a%c;
            b >>= 1;
        }
        return res;
    }
    
    int Inv(int a, int p)
    {
        int inv = PowMod(a, p-2, p);
        return inv;
    }
    
    int Gauss()
    {
        int row, col;
        row = col = 0;
        while(row < equ && col < var)
        {
            int max_r = row;//Max Line
            for (int i = row+1;i < equ;i++)
            {
                if (abs(Mar[i][col]) > abs(Mar[max_r][col]))
                    max_r = i;
            }
            if (max_r != row)
            {
                for (int j = col;j < var+1;j++)
                    swap(Mar[row][j], Mar[max_r][j]);
            }
            if (Mar[row][col] == 0)
            {
                col++;
                continue;
            }
            for (int i = row+1;i < equ;i++)
            {
                if (Mar[i][col] == 0)
                    continue;
                int LCM = Lcm(Mar[row][col], Mar[i][col]);
                int ta = LCM/Mar[i][col];
                int tb = LCM/Mar[row][col];
                if (Mar[i][col]*Mar[row][col] < 0)
                    tb = -tb;
                for (int j = col;j < var+1;j++)
                    Mar[i][j] = ((Mar[i][j]*ta-Mar[row][j]*tb)%p+p)%p;
            }
            row++;
            col++;
        }
        for (int i = row;i < var;i++)
        {
            if (Mar[i][var] != 0)
                return -1;
        }
        if (row < var)
            return var-row;
        for (int i = var-1;i >= 0;i--)
        {
            int tmp = Mar[i][var];
            for (int j = i+1;j < var;j++)
                tmp = ((tmp-Mar[i][j]*X[j])%p+p)%p;
            X[i] = (tmp*Inv(Mar[i][i], p))%p;
        }
        return 0;
    }
    
    int main()
    {
        int t;
        scanf("%d", &t);
        while(t--)
        {
            scanf("%d", &p);
            scanf("%s", s);
            equ = var = strlen(s);
            for (int i = 0;i < equ;i++)
            {
                for (int j = 0;j < var;j++)
                    Mar[i][j] = PowMod(i+1, j, p);
                if (s[i] == '*')
                    Mar[i][var] = 0;
                else
                    Mar[i][var] = s[i]-'a'+1;
            }
            Gauss();
            for (int i = 0;i < var-1;i++)
                printf("%d ", X[i]);
            printf("%d
    ", X[var-1]);
        }
    
        return 0;
    }
    
  • 相关阅读:
    代理的原理
    nodemon:让node自动重启
    http与https的区别
    Content-Type:几种常用数据编码格式
    vue ssr服务端渲染
    vue 实现文件上传和文件下载
    vue element-ui表格里时间戳转换成时间显示
    vue npm start 自动打开网页
    yearProgress.vue
    vuejs中class与style的绑定
  • 原文地址:https://www.cnblogs.com/YDDDD/p/11795814.html
Copyright © 2011-2022 走看看