Topic Connection:
http://poj.org/problem?id=2065
Title Description:
Given the same length of the same cipher, each letter F (k) of the cipher is converted by the plaintext AI according to the F (k) =∑0<=i<=n-1a i *ki (mod p), known cipher, to find out the plaintext?
Problem Solving Ideas:
Using Gaussian elimination, it is important to model transformation, listed to augment the matrix problem is not far away from AC. The augmented matrix of this topic is:
a0*1^0 + a1*1^1 + a2*1^2 + ... + an*1^n = f (1) (mod p);
a0*2^0 + a1*2^1 + a2*2^2 + ... + an*2^n = f (2) (mod p);
a0*3^0 + a1*3^1 + a2*3^2 + ... + an*3^n = f (3) (mod p);
...
...
...
a0* (n-2) ^0 + a1* (n-2) ^1 + a2* (n-2) ^2 + ... + an* (n-2) ^n = f (n-2) (mod p);
a0* (n-1) ^0 + a1* (n-1) ^1 + a2* (n-2) ^2 + ... + an* (n-1) ^n = f (n-1) (mod p);
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5 using namespacestd;6 Const intMAXN = the;7 typedef __int64 LL;8 LL DET[MAXN][MAXN], X[MAXN];9 int var, Equ, p;Ten voidGauss () One { A intK, col; - for(k=col=0; k<equ&&col<var; k++,col++) - { the intMin_i =K; - for(inti=k+1; i<equ; i++)//Reduce the occurrence of errors - if(Det[min_i][col] <Det[i][col]) -Min_i =i; + if(Min_i! =k) - for(intI=col; i<=var; i++) + swap (Det[k][i], det[min_i][i]); A if(Det[k][col] = =0) at { -K--; - Continue; - } - for(inti=k+1; i<equ; i++) - if(Det[i][col]) in { - intx, y;//Prevent accuracy errors tox =Det[k][col]; +y =Det[i][col]; - for(intJ=col; j<=var; J + +) theDET[I][J] = (((det[i][j]*x-det[k][j]*y)% p) + p)%p; * } $ }Panax Notoginseng for(inti=k-1; i>=0; i--) - { theLL temp = det[i][var]; + for(intj=i+1; j<var; J + +) Atemp = ((temp-det[i][j]*x[j])% p + p)%p; the while(temp%Det[i][i]) +Temp + =p;//Guarantee the accuracy of the results -X[i] = ((Temp/det[i][i])%p + p)%p; $ } $ } - intMain () - { the intT; -scanf ("%d", &t);Wuyi while(T--) the { - CharSTR[MAXN]; Wuscanf ("%d%s", &p, str); - var= Equ =strlen (str); About for(intI=0; i<equ; i++) $ { -LL num =1; - for(intj=0; j<equ; J + +) - { ADET[I][J] =num; +num = (num * (i +1)) %p; the } - if(Str[i] = ='*') $det[i][var] =0; the Else thedet[i][var] = Str[i]-'a'+1; the } the Gauss (); - for(intI=0; i<var-1; i++) inprintf ("%i64d", X[i]); theprintf ("%i64d\n", x[var-1]); the } About return 0; the}
Poj 2065 SETI (Gaussian elimination)