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\n", X[var-1]);
}
return 0;
}
POJ-2065-SETI(高斯消元)的更多相关文章
- poj 2065 SETI 高斯消元
看题就知道要使用高斯消元求解! 代码如下: #include<iostream> #include<algorithm> #include<iomanip> #in ...
- POJ 2065 SETI [高斯消元同余]
题意自己看,反正是裸题... 普通高斯消元全换成模意义下行了 模模模! #include <iostream> #include <cstdio> #include <c ...
- 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 ...
- POJ 2065 SETI 高斯消元解线性同余方程
题意: 给出mod的大小,以及一个不大于70长度的字符串.每个字符代表一个数字,且为矩阵的增广列.系数矩阵如下 1^0 * a0 + 1^1 * a1 + ... + 1^(n-1) * an-1 = ...
- POJ 2065 SETI (高斯消元 取模)
题目链接 题意: 输入一个素数p和一个字符串s(只包含小写字母和‘*’),字符串中每个字符对应一个数字,'*'对应0,‘a’对应1,‘b’对应2.... 例如str[] = "abc&quo ...
- B - SETI POJ - 2065 (高斯消元)
题目链接:https://vjudge.net/contest/276374#problem/B 题目大意: 输入一个素数p和一个字符串s(只包含小写字母和‘*’),字符串中每个字符对应一个数字,'* ...
- POJ SETI 高斯消元 + 费马小定理
http://poj.org/problem?id=2065 题目是要求 如果str[i] = '*'那就是等于0 求这n条方程在%p下的解. 我看了网上的题解说是高斯消元 + 扩展欧几里德. 然后我 ...
- POJ 2947-Widget Factory(高斯消元解同余方程式)
题目地址:id=2947">POJ 2947 题意:N种物品.M条记录,接写来M行,每行有K.Start,End,表述从星期Start到星期End,做了K件物品.接下来的K个数为物品的 ...
- POJ2065 SETI 高斯消元
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2065 题意概括 多组数据,首先输入一个T表示数据组数,然后,每次输入一个质数,表示模数,然后,给出一 ...
- UVA 1563 - SETI (高斯消元+逆元)
UVA 1563 - SETI option=com_onlinejudge&Itemid=8&page=show_problem&category=520&probl ...
随机推荐
- [转帖]java架构之路-(面试篇)JVM虚拟机面试大全
java架构之路-(面试篇)JVM虚拟机面试大全 https://www.cnblogs.com/cxiaocai/p/11634918.html 下文连接比较多啊,都是我过整理的博客,很多答案都 ...
- [Visual Studio] - 使用 Fiddler 时,禁止监控 VSHub 请求的方法
背景 VS + Fiddler 调试 WebAPI,监控请求包含大量 VSHub Request.http://localhost:49161/vshub/bb195f2e0d5c4765b9411f ...
- 史上最全最新java面试题合集二(附答案)
下面小编整理了本套java面试题全集,分享给大家,希望对大家的java学习和就业面试有所帮助. 51.类ExampleA继承Exception,类ExampleB继承ExampleA. 有如下代码片断 ...
- Android--DES加密
Base64.java import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputS ...
- 使用 react 的 hooks 进行全局的状态管理
使用 react 的 hooks 进行全局的状态管理 React 最新正式版已经支持了 Hooks API,先快速过一下新的 API 和大概的用法. // useState,简单粗暴,setState ...
- 运输计划[二分答案 LCA 树上差分]
也许更好的阅读体验 \(\mathcal{Description}\) 原题链接 概括一下题意 给一颗有\(n\)个点带边权的树,有\(m\)个询问,每次询问\(u,v\)两点间的权值和,你可以将树中 ...
- 记录MindSphere On Cloud Foundry的一次尝试过程
试验背景: 开始时间:2019年12月11日 结束时间:2019年12月13日 自己编写一个后台程序,尝试推送到Cloud Foundry上,并开放从MindSphere以外访问的权限. 程序实现以下 ...
- 【SQL Server DBA】维护语句:删除并创建外键约束、获取建表语句
原文:[SQL Server DBA]维护语句:删除并创建外键约束.获取建表语句 1.删除外键约束,建立外键约束 先建立3个表: /* drop table tb drop table tb_b dr ...
- Bootstrap4 入门
http://www.runoob.com/bootstrap4/bootstrap4-navs.html 共五个部分 1 <!DOCTYPE html> <html lang=&q ...
- MLP神经网络实例--手写识别
1.导入MNIST数据集 直接使用fetch_mldata会报错,错误信息是python3.7把fetch_mldata方法移除了,所以需要单独下载数据集从这个网站上下载数据集: https://gi ...