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 ...
随机推荐
- ASP.NET请求过程-从源码角度研究MVC路由、Handler、控制器
路由常用对象 RouteBase 用作表示 ASP.NET 路由的所有类的基类. 就是路由的一个基础抽象类. // // 摘要: // 用作表示 ASP.NET 路由的所有类的基类. [ ...
- java抽象类及接口
Java抽象类: 抽象类特点:抽象类除了不能实例化对象之外,类的其它功能依然存在,成员变量.成员方法和构造方法的访问方式和普通类一样. 由于抽象类不能实例化对象,所以抽象类必须被extends [抽象 ...
- Struts笔记5
文件下载 1.写action类 package com.gyf.web.action; import java.io.File; import java.io.FileInputStream; imp ...
- Python进阶:值传递,引用传递?不存在的,是赋值传递
Python的变量及其赋值 c/c++/c#/java 里面都有值传递,引用传递的概念,在Python中是如何的? 例 a = 1 b = a print(id(a),id(b)) #14072334 ...
- springboot2.0+mybatis多数据源集成
最近在学springboot,把学的记录下来.主要有springboot2.0+mybatis多数据源集成,logback日志集成,springboot单元测试. 一.代码结构如下 二.pom.xml ...
- Go part 6 接口,接口排序,接口嵌套组合,接口与类型转换,接口断言
接口 接口是一种协议,比如一个汽车的协议,就应该有 “行驶”,“按喇叭”,“开远光” 等功能(方法),这就是实现汽车的协议规范,完成了汽车的协议规范,就实现了汽车的接口,然后使用接口 接口的定义:本身 ...
- 【转载】C#通过StartWith和EndWith方法判断字符串是否以特定字符开始或者结束
C#开发过程中针对字符串String类型的操作是常见操作,有时候业务需要判断某个字符串是否以特定字符开头或者特定字符结束,此时就可使用StartsWith方法来判断目标字符串是否以特定字符串开头,通过 ...
- c#的异步处理思路和vue前端中异步处理思路比较
前语:目前工作在做的项目是前端基于vue的组件式开发,通过api接口调用,后端数据逻辑是一个c#实现的WCF服务 1.总结自己在c# .NET 4.5后的新异步方式 async搭配await来实现 ...
- element-ui默认样式修改
来自 :https://blog.csdn.net/wangguoyu1996/article/details/81394707 侵删 我们在使用element-ui的时候经常会遇到需要修改组件默认样 ...
- [转]github 上传project代码
原文地址:https://www.cnblogs.com/f1194361820/p/4741558.html 1)将远程仓库纳入管理 其实就是添加远程仓库,在你已有的本地仓库目录下执行如下命令: $ ...