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 ...
随机推荐
- git实现码云的上传和下载
上传步骤: 1.码云上新建一个项目 XXXX? ?(项目名) 2.本地创建一个文件夹E:/XXXX,然后使用git bash? ?? 3.cd 到本地文件夹中E:/XXXX? //如果是在创建的文件中 ...
- 使用TypeScript创建Vue项目
Vue的灵活性总是让代码看起来非常洗练,对TypeScript来说也是一种挑战, 好在Vue对TypeScript进行了一次全方位的适配. 相对于React严谨的代码,Redux啰嗦的样板代码,Vue ...
- JUC之AbstractQueuedSynchronizer原理分析 - 独占/共享模式
1. 简介 AbstractQueuedSynchronizer (抽象队列同步器,以下简称 AQS)出现在 JDK 1.5 中,由大师 Doug Lea 所创作.AQS 是很多同步器的基础框架. R ...
- ant 节点和属性
任务和javac命令是相似,它编译两种类型的java文件1)没有被编译的java文件2)曾经编译过,但是class文件版本和当前对应的java文件版本不匹配的java文件. 1)javac命令支持的参 ...
- laravel框架视图中常用的逻辑结构forlese,foreach,ifelse等
if 和else @if($name === 1) 这个数字是1 @else 这个数字非1 @endif switch @switch($name) @case(1) 变量name == 1 @bre ...
- Linux 进程地址空间及原理
1.程序地址空间 首先,我们先看学c/c++时候学到的程序内存布局: 准确地说,程序地址空间其实就是进程的地址空间,实际就是pcb中的mm_struct. 接下来,我们用fork()演示一下 ...
- python检测挖矿特征的几种方式
电脑性能上: ①cpu和内存使用率(常见): python 实时得到cpu和内存的使用情况方法_python_脚本之家https://www.jb51.net/article/141835.htm ② ...
- em...刚打完一点cf。。 有点子感悟
首先,下笔一定要读清楚题目. 情况多考虑一下. 这几次的模拟赛,分类思想很重要,往往一大坨东西扔给你,你不去尝试分类的话就很难整理清楚.
- 在论坛中出现的比较难的sql问题:17(字符分拆2)
原文:在论坛中出现的比较难的sql问题:17(字符分拆2) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有必要记录下来 ...
- SqlServer2008 / SqlServer2012 禁用windows登录,sa忘记密码或密码过期如何登陆
以管理员身份运行cmd 1.cmd 下 停止SqlServer服务,net stop mssqlserver: 2.新建windows账号test,加入administrators组里,授予管理员权 ...