SETI
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 1735   Accepted: 1085

Description

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, ...an-1 the function f (k) = ∑0<=i<=n-1aiki (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 <= ai < 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.

Input

On the first line of the input there is a single positive integer N, telling the number of test cases to follow. Each case consists of one line containing the value of p to use during the transcription of the string, followed by the actual string to be transcribed.
The only allowed characters in the string are the lower case letters 'a'..'z' and '*' (asterisk). No string will be longer than 70 characters.

Output

For each transcribed string, output a line with the corresponding list of integers, separated by space, with each integer given in the order of ascending values of i.

Sample Input

3
31 aaa
37 abc
29 hello*earth

Sample Output

1 0 0
0 1 0
8 13 9 13 4 27 18 10 12 24 15

题意:

表示最开始并没有看懂题目是什么意思,那一串字母代表f[i]的值

f(k) = ∑0<=i<=n-1aiki (mod p)转换成方程组便是,

a0*1^0 + a1*1^1+a2*1^2+........+an-1*1^(n-1) = f(1)

a0*2^0 + a1*2^1+a2*2^2+........+an-1*2^(n-1) = f(2)

......

a0*n^0 + a1*n^1+a2*n^2+........+an-1*n^(n-1) = f(n)

然后利用高斯消元求解 

/*
poj 2065
解对mod取模的方程组
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld; using namespace std;
const int maxn = 105; int equ,var;
int a[maxn][maxn];
int b[maxn][maxn];
int x[maxn];
int free_x[maxn];
int free_num;
int n;
void debug()
{
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= n; j++)
printf("%d ",a[i][j]);
printf("\n");
}
} int gcd(int a,int b)
{
while(b)
{
int tmp = b;
b = a%b;
a = tmp;
}
return a;
} int lcm(int a,int b)
{
return a/gcd(a,b)*b;
} int Gauss(int mod)
{
int max_r,col,k;
free_num = 0;
for(k = 0,col = 0; k < equ && col < var; k++,col++)
{
max_r = k;
for(int i = k+1; i < equ; i++)
{
if(abs(a[i][col]) > abs(a[max_r][col]))
max_r = i;
}
if(a[max_r][col] == 0)
{
k --;
free_x[free_num++] = col;
continue;
}
if(max_r != k)
{
for(int j = col; j < var+1; j++)
swap(a[k][j],a[max_r][j]); }
for(int i = k + 1; i < equ; i++)
{
if(a[i][col] != 0)
{
int LCM = lcm(abs(a[i][col]),abs(a[k][col]));
int ta = LCM / abs(a[i][col]);
int tb = LCM / abs(a[k][col]);
if(a[i][col] * a[k][col] < 0) tb = -tb;
for(int j = col; j < var+1; j++)
{
a[i][j] = ((a[i][j]*ta - a[k][j]*tb)%mod+mod)%mod;
}
}
} }
for(int i = k; i < equ; i++)
if(a[i][col] != 0)
return -1;
if(k < var) return var-k; for(int i = var-1; i >= 0; i--)
{
ll temp = a[i][var];
for(int j = i +1; j < var; j++)
temp =((temp- a[i][j]*x[j])%mod+mod)%mod;
while(temp % a[i][i]) temp += mod;
temp /= a[i][i];
temp %= mod; x[i] = temp;
}
return 0; } void ini()
{
memset(a,0,sizeof(a));
memset(x,0,sizeof(x));
equ = n;
var = n;
} char str[105];
int main()
{
int T,p;
scanf("%d",&T);
while(T--)
{
scanf("%d",&p);
scanf("%s",str);
n = strlen(str);
ini();
for(int i=0; i<n; i++)
{
if(str[i]=='*')
a[i][n]=0;
else
a[i][n]=str[i]-'a'+1;
a[i][0]=1;
for(int j=1; j<n; j++)
a[i][j]=(a[i][j-1]*(i+1))%p;
} //debug();
Gauss(p); for(int i = 0; i < n-1; i++)
printf("%d ",x[i]);
printf("%d\n",x[n-1]);
}
return 0;
}

  

poj 2065 高斯消元(取模的方程组)的更多相关文章

  1. POJ 2065 高斯消元求解问题

    题目大意: f[k] = ∑a[i]*k^i % p 每一个f[k]的值就是字符串上第 k 个元素映射的值,*代表f[k] = 0 , 字母代表f[k] = str[i]-'a'+1 把每一个k^i求 ...

  2. 2017湘潭赛 A题 Determinant (高斯消元取模)

    链接 http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1260 今年湘潭的A题 题意不难 大意是把n*(n+1)矩阵去掉某一列 ...

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

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

  4. 【poj1830-开关问题】高斯消元求解异或方程组

    第一道高斯消元题目~ 题目:有N个相同的开关,每个开关都与某些开关有着联系,每当你打开或者关闭某个开关的时候,其他的与此开关相关联的开关也会相应地发生变化,即这些相联系的开关的状态如果原来为开就变为关 ...

  5. bzoj千题计划187:bzoj1770: [Usaco2009 Nov]lights 燈 (高斯消元解异或方程组+枚举自由元)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1770 a[i][j] 表示i对j有影响 高斯消元解异或方程组 然后dfs枚举自由元确定最优解 #in ...

  6. 【BZOJ】2466: [中山市选2009]树 高斯消元解异或方程组

    [题意]给定一棵树的灯,按一次x改变与x距离<=1的点的状态,求全0到全1的最少次数.n<=100. [算法]高斯消元解异或方程组 [题解]设f[i]=0/1表示是否按第i个点的按钮,根据 ...

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

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

  8. POJ 1222 POJ 1830 POJ 1681 POJ 1753 POJ 3185 高斯消元求解一类开关问题

    http://poj.org/problem?id=1222 http://poj.org/problem?id=1830 http://poj.org/problem?id=1681 http:// ...

  9. POJ 1222 EXTENDED LIGHTS OUT(高斯消元解异或方程组)

    EXTENDED LIGHTS OUT Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10835   Accepted: 6 ...

随机推荐

  1. Linux 下的权限改变与目录配置

    Linux 下的权限改变与目录配置 ./代表本目录的意思. (1):用户与用户组, 1:文件所有者,文件被某一用户所有 2:用户组:    对文件给与一个或者多个用户权限配置 3:其它人: (2):l ...

  2. python全栈开发-Day13 内置函数

    一.内置函数 注意:内置函数id()可以返回一个对象的身份,返回值为整数. 这个整数通常对应与该对象在内存中的位置,但这与python的具体实现有关,不应该作为对身份的定义,即不够精准,最精准的还是以 ...

  3. css3动画transition详解

    一.transition-property 语法: transition-property : none | all | [ <IDENT> ] [ ',' <IDENT> ] ...

  4. 工频相位无线同步模块PSYN5000系列在高压设备状态检测和局部放电故障定位的应用方案

    关键词: PSYN5000,无线同步模块,工频相位,局部放电,在线监测,高压设备,设备状态,故障定位. 前言: 在电力监测领域,出于方便和安全考虑,有些系统不得不采用无线通信的方式,在这样一个无线通信 ...

  5. api-gateway实践(16)【租户模块:修改api定义】通过mq通知【开发者模块:更新开发者集市】

    一.订阅关系 二.接收消息 dev模块接收更新本地集市

  6. Spring Security 入门(1-3-3)Spring Security - logout 退出登录

    要实现退出登录的功能我们需要在 http 元素下定义 logout 元素,这样 Spring Security 将自动为我们添加用于处理退出登录的过滤器 LogoutFilter 到 FilterCh ...

  7. 开发技巧(3-1)Eclipse查找关键字

    1.选择资源目录->选择search-file菜单 2.在弹出的对话框中, 输入要[搜索的字符串],选择[selected resources],点击[search]按钮 3.搜索结果

  8. 未能加载文件或程序集“ RevitAPIUI.dll”

    revit二次开发中遇到的问题 RevitAPIUI.dll 只能 Native Library 中执行: 脱离了Native Library,API是跑不起来的 . 检查程序流程:登录,配置,启动r ...

  9. linux 安装jdk以及nginx详细过程

    一.安装jdk 1:首先下载jdk到本地,然后通过git 上传到linux服务器上 2:进入目录usr,并创建目录java,将jdk的压缩文件移动到该目录下 cd /usr mkdir java mv ...

  10. AFNetWorking常用方法

    NSURLConnection,主要对NSURLConnection进行了进一步的封装,包含以下核心的类: AFURLConnectionOperation AFHTTPRequestOperatio ...