poj_2065 SETI(高斯消元解同余方程组)
来源:互联网 发布:淘宝店怎么提升销量 编辑:程序博客网 时间:2024/05/01 19:47
SETI
Time Limit: 1000MS Memory Limit: 30000KTotal Submissions: 2032 Accepted: 1238
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.
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
331 aaa37 abc29 hello*earth
Sample Output
1 0 00 1 08 13 9 13 4 27 18 10 12 24 15
题意很清楚,就是解一个形为f (k) = ∑0<=i<=n-1aiki (mod p) 的同余方程组。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define FOP freopen("data.txt","r",stdin)#define FOP2 freopen("data1.txt","w",stdout)#define inf 0x3f3f3f3f#define maxn 100#define PI acos(-1.0)#define LL long longusing namespace std;int mod;typedef int Matrix[maxn][maxn];int x[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; //防溢出}//equ个方程,var个变元 A[r][c], 0<r<equ, 0<c<varint gauss_elimination(Matrix A, int equ, int var){ memset(x, 0, sizeof(x)); int row, col; //当前处理的行row和列col int max_r; for(row = 0, col = 0; row < equ && col < var; row++, col++) { if(A[row][col] == 0) { for(int r = row+1; r < equ; r++) if(A[r][col]) { for(int c = 0; c <= var; c++) swap(A[row][c],A[r][c]); break; } } //与第row+1~equ行进行消元 int LCM, ta, tb; for(int r = row+1; r < equ; r++) { if(A[r][col] == 0) continue; LCM = lcm(abs(A[r][col]),abs(A[row][col])); ta = LCM / abs(A[r][col]); tb = LCM / abs(A[row][col]); if(A[r][col] * A[row][col] < 0) tb = -tb;//异号的情况是相加 for(int c = col; c <= var; c++) { A[r][c] = A[r][c] * ta - A[row][c] * tb; A[r][c] = (A[r][c]%mod+mod)%mod; } } } int temp; for (int r = var - 1; r >= 0; r--) { temp = A[r][var]; for(int c = r + 1; c < var; c++) { if (A[r][c] != 0) temp -= x[c] * A[r][c]; temp = (temp%mod+mod)%mod; } while(temp % A[r][r] != 0) temp += mod; //剩余系的特殊处理 x[r] = (temp / A[r][r])%mod; } return 0;}int fastpow(int x,int n){ int ans = 1; while(n) { if(n&1)ans = ans*x%mod; x = x*x%mod; n >>= 1; } return ans;}Matrix A;char s[100];int main(){ int T; while(~scanf("%d", &T)) { while(T--) { memset(A, 0, sizeof(A)); scanf("%d%s", &mod, s); int len = strlen(s); for(int i = 0; i < len; i++) { for(int j = 0; j < len; j++) A[i][j] = fastpow(i+1, j); if(s[i] == '*') A[i][len] = 0; else A[i][len] = s[i]-'a'+1; } gauss_elimination(A, len, len); for(int i = 0; i < len-1; i++) printf("%d ", x[i]); printf("%d\n", x[len-1]); } } return 0;}
0 0
- poj_2065 SETI(高斯消元解同余方程组)
- POJ 2065 SETI(高斯消元解同余方程组)
- poj 2065 SETI(高斯消元 解同于方程组)
- poj 2947 Widget Factory(高斯消元解同余方程组)
- poj 2947 Widget Factory(高斯消元解同余方程组)
- POJ 2947 Widget Factory(高斯消元解同余方程组)
- 同余问题(3)一元线性同余方程组
- Biorhythms(一元线性同余方程组)
- 同余方程组问题
- 同余方程组求解
- 约瑟夫环and同余方程组模板(exgcd求解同余方程组)
- 【math】同余模方程组
- 解线性同余方程组
- HDU2185 解高次同余方程组
- 解线性同余方程组
- 求解线性同余方程组
- 线性同余方程组模板
- POJ 1006 同余方程组
- 判断用户输入的银行卡号是否正确--基于Luhn算法的格式校验
- UDP接收实例
- Android前端判断敏感词汇
- java.util.concurrent之ForkJoin
- Windows下查询域名的DNS TXT记录的命令
- poj_2065 SETI(高斯消元解同余方程组)
- hashmapper的使用以及json字符串与普通字符串的差异
- Linux下使用shell解压打包jar包
- 【算法模板】图论
- apache 配置动静分离,允许跨域, 并在反向代理的情况下维持默认主页
- pjsip
- Y2K Accounting Bug(POJ 2586)(贪心)
- 没有卑微
- 什么是hack技术