POJ 2065 SETI(高斯消元)
来源:互联网 发布:网络教育 学籍 编辑:程序博客网 时间:2024/05/16 08:16
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-1ai*
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
331 aaa37 abc29 hello*earth
Sample Output
1 0 00 1 08 13 9 13 4 27 18 10 12 24 15
题目大意
主要是题目中给出的公式
解题思路
根据公式很容易便可列出方程:
……
利用高斯消元解方程即可。
代码实现
#include <iostream>#include<cstdio>#include<cstring>#include<cmath>using namespace std;#define maxn 75char str[maxn];int a[maxn][maxn],ans[maxn];int p;int quick_pow(int a,int b){ int result=1; while(b!=0) { if(b%2==1) result=((result%p)*(a%p))%p; a=((a%p)*(a%p))%p; b/=2; } return result;}int gcd(int a,int b){ int t; while(b!=0) { t=b; b=a%b; a=t; } return a;}int lcm(int a,int b){ return a/gcd(a,b)*b;}int Gauss(int equ,int var){ int i,j,k; int max_r; int col; int ta,tb; int LCM; int temp; for(i=0; i<=var; i++) { ans[i]=0; } col=0; for(k = 0; k < equ && col < var; k++,col++) { max_r=k; for(i=k+1; i<equ; i++) { if(fabs(a[i][col])>fabs(a[max_r][col])) max_r=i; } if(max_r!=k) { for(j=k; j<var+1; j++) swap(a[k][j],a[max_r][j]); } if(a[k][col]==0) { k--; continue; } for(i=k+1; i<equ; i++) { if(a[i][col]!=0) { LCM = lcm(fabs(a[i][col]),fabs(a[k][col])); ta = LCM/fabs(a[i][col]); tb = LCM/fabs(a[k][col]); if(a[i][col]*a[k][col]<0)tb=-tb; for(j=col; j<var+1; j++) { a[i][j] = ((a[i][j]*ta%p-a[k][j]*tb)%p+p)%p; } } } } for (i = var - 1; i >= 0; i--) { temp = a[i][var]; for (j = i + 1; j < var; j++) { if (a[i][j] != 0) temp -= a[i][j] * ans[j]; temp=(temp%p+p)%p; } while(temp%a[i][i]!=0) temp+=p; ans[i] = (temp / a[i][i])%p; } return 0;}int main(){ int T; scanf("%d",&T); while(T--) { scanf("%d %s",&p,str); int length=strlen(str); for(int i=0;i<length;i++) { if(str[i]=='*') a[i][length]=0; else a[i][length]=str[i]-'a'+1; } for(int i=0;i<length;i++) { for(int j=0;j<length;j++) { a[i][j]=quick_pow(i+1,j); } } Gauss(length,length); for(int i=0;i<length-1;i++) printf("%d ",ans[i]); printf("%d\n",ans[length-1]); } return 0;}
- POJ 2065 SETI 高斯消元
- poj 2065 SETI 高斯消元
- POJ 2065 SETI(高斯消元)
- 【高斯消元】【poj 2065】SETI
- poj 2065 SETI (高斯消元)
- poj 2065 SETI(高斯消元)
- POJ 2065 SETI(高斯消元)
- SETI - POJ 2065 高斯消元
- POJ 2065 SETI(高斯消元)
- poj 2065 SETI(高斯消元)
- poj 2065 SETI(高斯消元)
- poj 2065 SETI(高斯消元)
- POJ 2065 SETI(高斯消元)
- poj 2065 SETI (高斯消元)
- poj 2065 SETI(高斯消元)
- [高斯消元+拓展欧几里得] poj 2065 SETI
- poj 2065 SETI(数学:高斯消元)
- 文章标题 POJ 2065 :SETI (高斯消元)
- jQuery选择器 标签选择元素+css简单添加移除操作
- 杭电acm—1236 排名
- 简易五子棋,供参考
- JSON 数据解析时遇到ValueError: No JSON object could be decoded
- 数据库的设计(E-R图,数据库模型图,三大范式)
- POJ 2065 SETI(高斯消元)
- VS2010 教程:创建一个 WPF 应用程序 (第一节)
- 中国剩余定理 [mi不互质]
- ios UICollectionViewCell个数小于等于3,顺序错位
- Numpy 布尔索引
- SpringCloud分布式开发五大神兽
- gcc与__cplusplus宏
- 方法的覆盖(重写)
- "大数据如何帮助小数据?" 主题报告速记与评述(二)