链接:

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\n", X[var-1]);
} return 0;
}

POJ-2065-SETI(高斯消元)的更多相关文章

  1. poj 2065 SETI 高斯消元

    看题就知道要使用高斯消元求解! 代码如下: #include<iostream> #include<algorithm> #include<iomanip> #in ...

  2. POJ 2065 SETI [高斯消元同余]

    题意自己看,反正是裸题... 普通高斯消元全换成模意义下行了 模模模! #include <iostream> #include <cstdio> #include <c ...

  3. POJ.2065.SETI(高斯消元 模线性方程组)

    题目链接 \(Description\) 求\(A_0,A_1,A_2,\cdots,A_{n-1}\),满足 \[A_0*1^0+A_1*1^1+\ldots+A_{n-1}*1^{n-1}\equ ...

  4. POJ 2065 SETI 高斯消元解线性同余方程

    题意: 给出mod的大小,以及一个不大于70长度的字符串.每个字符代表一个数字,且为矩阵的增广列.系数矩阵如下 1^0 * a0 + 1^1 * a1 + ... + 1^(n-1) * an-1 = ...

  5. POJ 2065 SETI (高斯消元 取模)

    题目链接 题意: 输入一个素数p和一个字符串s(只包含小写字母和‘*’),字符串中每个字符对应一个数字,'*'对应0,‘a’对应1,‘b’对应2.... 例如str[] = "abc&quo ...

  6. B - SETI POJ - 2065 (高斯消元)

    题目链接:https://vjudge.net/contest/276374#problem/B 题目大意: 输入一个素数p和一个字符串s(只包含小写字母和‘*’),字符串中每个字符对应一个数字,'* ...

  7. POJ SETI 高斯消元 + 费马小定理

    http://poj.org/problem?id=2065 题目是要求 如果str[i] = '*'那就是等于0 求这n条方程在%p下的解. 我看了网上的题解说是高斯消元 + 扩展欧几里德. 然后我 ...

  8. POJ 2947-Widget Factory(高斯消元解同余方程式)

    题目地址:id=2947">POJ 2947 题意:N种物品.M条记录,接写来M行,每行有K.Start,End,表述从星期Start到星期End,做了K件物品.接下来的K个数为物品的 ...

  9. POJ2065 SETI 高斯消元

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2065 题意概括 多组数据,首先输入一个T表示数据组数,然后,每次输入一个质数,表示模数,然后,给出一 ...

  10. UVA 1563 - SETI (高斯消元+逆元)

    UVA 1563 - SETI option=com_onlinejudge&Itemid=8&page=show_problem&category=520&probl ...

随机推荐

  1. LeetCode 112. 路径总和(Path Sum) 10

    112. 路径总和 112. Path Sum 题目描述 给定一个二叉树和一个目标和,判断该树中是否存在根节点到叶子节点的路径,这条路径上所有节点值相加等于目标和. 说明: 叶子节点是指没有子节点的节 ...

  2. 基于DNN的推荐算法总结

    1.早期的算法 深度学习在CTR预估应用的常见算法有Wide&Deep,DeepFM等. 这些方法一般的思路是:通过Embedding层,将高维离散特征转换为固定长度的连续特征,然后通过多个全 ...

  3. nmap使用帮助翻译

    Nmap 7.60 ( https://nmap.org )Usage: nmap [扫描类型] [操作] {目标说明}目标说明:  可以识别主机名.IP地址.网络,等等.  例如: scanme.n ...

  4. [v]Linux下安装Git

    Ubuntu12.04中默认没有安装Git.需要自行安装. 1. 安装Git 1.1 Ubuntu12.04下 可以使用apt-get方式安装,也可以下载源代码安装[1],我们这里使用apt-git安 ...

  5. springboot项目实用代码整理

    // 判断JSONOBJECT是否为空 CommonUtils.checkJSONObjectIsEmpty(storeInfo) // 判断字符串是否为空," "也为空 Stri ...

  6. 在论坛中出现的比较难的sql问题:21(递归问题 检索某个节点下所有叶子节点)

    原文:在论坛中出现的比较难的sql问题:21(递归问题 检索某个节点下所有叶子节点) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. ...

  7. Asp.Net Core 使用 MediatR

    Asp.Net Core 使用 MediatR 项目中使用了CQRS读写分离,增删改 的地方使用了 MediatR ,将进程内消息的发送和处理进行解耦.于是便有了这篇文章,整理并记录一下自己的学习.遇 ...

  8. iOS滤镜功能

    一.iOS自带滤镜 1.CoreImage 使用苹果自带的CoreImage框架对图片进行处理,用CoreImage框架里的CIFilter对图片进行滤镜处理, 首先我们应该了解下CoreImage框 ...

  9. XSS的类型

  10. ELK文档--ELK简介

    请参考:http://www.cnblogs.com/aresxin/p/8035137.html