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 ...
随机推荐
- LeetCode 102. 二叉树的层次遍历(Binary Tree Level Order Traversal) 8
102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每 ...
- str.format() 格式化数字的多种方法
Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能. 基本语法是通过 {} 和 : 来代替以前的 % . format 函数可以接受不限个参数 ...
- Spring Boot使用@ConfigurationProperties注解获取配置文件中的属性值
注意:这种方式要提供属性的getter/setter方法—— 如果idea报错,提示没有相应的执行器,就需要在maven中添加: (虽然不配置代码也能正常运行,作用在下面会说明) 配置了该执行器后,在 ...
- VK Cup 2017 - Round 1 (CDE)
771C Bear and Tree Jumps 大意: 给定树,每步能走到距离不超过$k$的任意点,记$f(s,t)$为$s$到$t$的最少步数,求$\sum\limits_{s<t}f(s, ...
- UOJ #7 NOI2014购票(点分治+cdq分治+斜率优化+动态规划)
重写一遍很久以前写过的题. 考虑链上的问题.容易想到设f[i]为i到1的最少购票费用,转移有f[i]=min{f[j]+(dep[i]-dep[j])*p[i]+q[i]} (dep[i]-dep[j ...
- 在论坛中出现的比较难的sql问题:17(字符分拆2)
原文:在论坛中出现的比较难的sql问题:17(字符分拆2) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记解决的方法了. 所以,觉得有必要记录下来 ...
- sqlserver2008+日志收缩sql语句命令
USE[master] GO ALTER DATABASE 数据库 SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE 数据库 SET RECOVER ...
- windows下批处理保留指定日期下的文件
@echo offchcp 65001setlocal enabledelayedexpansion ::设置操作路径set "pic_dir=D:\465"echo 开始清理.. ...
- zabbix4.2的yum+mariadb方式部署安装
本文依据官方文档操作(英文4.2):https://www.zabbix.com/documentation/4.2/manual/installation/install_from_packages ...
- SEO基础知识
SEO: SEO是由英文Search Engine Optimization缩写而来, 中文意译为“搜索引擎优化”!SEO是指通过对网站进行站内优化和修复(网站Web结构调整.网站内容建设.网站代码优 ...