Topic:
Seti
Time Limit: 1000MS |
|
Memory Limit: 30000K |
Total Submissions: 1693 |
|
Accepted: 1054 |
Description
For some years, quite a lot of work have been put into listening to electromagnetic radio signals received from space, in O Rder to understand what civilizations in distant galaxies might is trying to tell us. One signal source that have been of particular interest to the scientists in Universit′e de technologie spatiale is the Neb ULA Stupidicus.
Recently, it was discovered the if each message are assumed to being transmitted as a sequence of integers a0, A1st e function f (k) =∑0<=i<=n-1aiki (mod p) always evaluates to values 0 <= f (k) <= 1 <= k <= N, p Rovided 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 is guaranteed to being larger than N as well as larger than 26. It is, however, known to never exceed 30 000.
These relationships altogether has been considered too peculiar for being pure coincidences, which calls for further INVe Stigation.
The linguists at the faculty of Langues et cultures extraterrestres transcribe these messages to strings in the 中文版 al Phabet 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 this 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 Valu E of F (k) at the end of the string.
The backward transcription procedure, has however, turned off to being too complex for the linguists of handle by themselves. You is therefore assigned the task of writing a program this converts a set of strings to their corresponding Extra Terr Estial number sequences.
Input
On the first line of the input there are a single positive integer N, telling the number of the 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 AC Tual string to is transcribed. The only allowed characters in the string is the lower case letters ' a '. Z ' and ' * ' (asterisk). No string would be longer than 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
Source
Northwestern Europe 2004
Test instructions: to n-mode equations, solve n unknown.
Idea: template problem, the solution can be solved by Gauss elimination element method. Pay attention to the processing of the mold.
Code:
#include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath > #include <climits> #include <algorithm> #include <vector> #include <string> #include < iostream> #include <sstream> #include <map> #include <set> #include <queue> #include < stack> #include <fstream> #include <numeric> #include <iomanip> #include <bitset> #include <list> #include <stdexcept> #include <functional> #include <utility> #include <ctime> Using namespace std, #define PB push_back#define MP make_pair#define REP (i,x,n) for (int i=x;i< (n); ++i) #define for (I,l, h) for (int i= (l); i<= (h), ++i) #define FORD (i,h,l) for (int i= (h); i>= (l); i) #define SZ (x) ((int) (x). Size ()) #define All (x) (x). Begin (), (x). End () #define RI (x) scanf ("%d", & (x)) #define RII (x, Y) scanf ("%d%d", & (X), & (Y)) # Define RIII (x, Y, z) scanf ("%d%d%d", & (X), & (Y), & (z)) #define DRI (x) int (x); scanf ("%d", &x) #define DRII (x, y) int X, y; scanf ("%d%d", &x, &y) #define DRIII (x, y, z) int x, y, Z; scanf ("%d%d%d", &x, &y, &z) #define OI (x) printf ("%d", x), #define RS (x) scanf ("%s", (x)) #define MS0 (x) memset ( (x), 0, sizeof ((x))) #define MS1 (x) memset ((x),-1, sizeof ((x))) #define LEN (x) strlen (x) #define F first#define S second#def Ine Swap (A, B) (a ^= B, b ^= A, a ^= b) #define Dpoint strcut node{int x, y} #define CMPD int cmp (const int &A,CONST int &B) {return a>b;}/* #ifdef HOME freopen ("In.txt", "R", stdin); #endif */const int MOD = 1e9+7;typedef vector<int> vi;typedef vector<string> vs;typedef vector<double> Vd;typedef Long Long ll;typedef pair<int,int> pii;//#define Homeint Scan () {int res = 0, ch, flag = 0;if (ch = getcha R ()) = = '-')//determine positive and negative flag = 1;else if (ch >= ' 0 ' && ch <= ' 9 ')//Get complete number res = CH-' 0 '; while ((ch = getchar ()) > = ' 0 ' && ch <= ' 9 ') res = res * + CH-' 0 '; return flag? -res:res;}/*---------------------Do----------HACK-----ME--------------------*/char str[80];int p;const int maxn=80 int a[maxn][maxn];//augmented matrix int x[maxn];//solution set BOOL free_x[maxn];//tag is indeterminate variable/*void Debug (void) {int I, J; for (i = 0, i < equ; i++) {for (j = 0, J < var + 1; j + +) {cout << a[i][j] <&l T " "; } cout << Endl; } cout << Endl;} */inline int gcd (int a,int b) {int t; while (b!=0) {t=b; B=a%b; a=t; } return A; The inline int LCM (int a,int b) {return A/GCD (A, a) *b;//first divide and then multiply the anti-overflow}//Gaussian elimination method to solve the equation set (Gauss-jordan elimination). (-2 means that there is a floating-point solution, but no integer solution,//-1 represents no solution, 0 means the unique solution, greater than 0 is the infinite solution, and returns the number of free arguments)//There are equ equations, var variable element. The number of augmented matrix rows is equ, 0 to Equ-1, and the number of columns is var+1, respectively, 0 to Var.int Gauss (int equ,int var) {int i,j,k; int max_r;//The row with the largest absolute value for the current column. int col;//the currently processed column int TA,TB; int LCM; int temp; int free_x_num; int free_index; for (int i=0;i<=var;i++) {x[i]=0; Free_x[i]=true; }//Convert to LadderArray. col=0; The currently processed column for (k = 0;k < equ && Col < var;k++,col++) {//enumerates the currently processed rows.//The row that finds the largest absolute value of the col column element is exchanged with the K line. To reduce the error when dividing) max_r=k; for (i=k+1;i<equ;i++) {if (ABS (A[i][col]) >abs (A[max_r][col])) max_r=i; } if (max_r!=k) {//is exchanged with line K. for (j=k;j<var+1;j++) swap (a[k][j],a[max_r][j]); } if (a[k][col]==0) {//indicates that the COL column K line below is all 0, then the next column of the current row is processed. k--; Continue } for (i=k+1;i<equ;i++) {//enumerates the rows to be deleted. if (a[i][col]!=0) {LCM = LCM (ABS (A[i][col]), ABS (A[k][col])); Ta = Lcm/abs (A[i][col]); TB = Lcm/abs (A[k][col]); if (a[i][col]*a[k][col]<0) tb=-tb;//, the case is added for (j=col;j<var+1;j++) { A[I][J] = (((long) A[i][j]*ta)%p-((Long Long) A[K][J]*TB%p)%p+p)%p; }}}}//Debug (); 1. The case of no solution: simplificationThe augmented array exists (0, 0, ..., a) such a line (a! = 0). for (i = k; i < equ; i++) {//For infinite solutions, if you want to determine which are free-variable, then the Exchange in the elementary row transformation will be affected, then the interchange should be recorded. if (A[i][col]! = 0) return-1; }//2. The case of Infinity: in the augmented array of Var * (var + 1) (0, 0, ..., 0) Such a line, that is to say, does not form a strict upper triangular array. and the number of rows that appear is the number of free arguments. if (K < Var) {///First, the free variable has var-k, that is, the indeterminate variable has at least var-k. for (i = k-1; I >= 0; i--) {//Line I will certainly not be (0, 0, ..., 0), because such a line is in line K to section equ. Similarly, line I must not be (0, 0, ..., a), A! = 0 case, such a non-solvable. Free_x_num = 0; Used to determine the number of indeterminate arguments in the row, if more than 1 are not solved, they are still indeterminate arguments. for (j = 0; J < var; j + +) {if (a[i][j]! = 0 && free_x[j]) free_x_num++, Free_index = J } if (Free_x_num > 1) continue; The determined variable cannot be solved. The description is only an indeterminate variable free_index, then the variable can be solved, and the variable is determined. temp = A[i][var]; for (j = 0; J < var; j + +) {if (a[i][j]! = 0 && J! = Free_index) Temp-= a[i][j] *X[J]; } X[free_index] = Temp/a[i][free_index]; To find out the variable element. Free_x[free_index] = 0; The variable is deterministic. } return var-k; There are var-k of free-variable elements. }//3. The only solution: a strict upper triangular array is formed in the augmented array of Var * (var + 1). Calculate the Xn-1, Xn-2 ... X0. for (i = var-1; I >= 0; i--) {temp = A[i][var]; for (j = i + 1; j < var; j + +) {if (a[i][j]! = 0) Temp = ((temp-a[i][j] * x[j])%p+p)%p; } for (int j=0;j<p;j++) if ((J*a[i][i])%p==temp) {x[i]=j; Break }} return 0;} int main () {int T; RI (T); while (t--) {ri (P); scanf ("%s", str); int N=strlen (str); for (int i=0;i<n;i++) {int k=1; for (int j=0;j<n;j++) {a[i][j]=k; K= (k* (i+1))%p; }} for (int i=0;i<n;i++) {if (str[i]!= ' * ') a[i][n]=str[i]-' a ' +1; else a[i][n]=0;} Gauss (N,n); for (int i=0;i<n-1;i++) printf ("%d", X[i]); printf ("%d\n", X[n-1]);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 2065 SETI (Gaussian elimination Chenmo equation Group)