"poj2065" SETI

Source: Internet
Author: User

http://poj.org/problem?id=2065 (Topic link)

Test instructions

Test instructions half a day do not understand. Give you a prime number P (p<=30000) and a string of long n strings str[]. The letter ' * ' stands for 0, the letter A-Z represents 1-26, and the numbers represented by the n characters represent F (1), F (2) ... f (n) respectively. Definition: ${f (k) =\sum_{i=0}^{n-1}{a_ik^i~ (mod~p)}~ (1<=k<=n,0<=a_i<p)}$, A0, a1.....an-1. The title guarantee must have the only solution.

Solution

Direct Gaussian elimination, because it is a modular equation group, so in addition to the time to seek an inverse element.

Code
poj2065#include<algorithm> #include <iostream> #include <cstdlib> #include <cstring># include<cstdio> #include <cmath> #include <map> #define LL long long#define inf 2147483640#define Pi ACOs ( -1,0) #define FREE (a) freopen (a ".", "R", stdin), Freopen (a ". Out", "w", stdout); using namespace Std;const int maxn= 100;int A[maxn][maxn],n,p;char ch[maxn];int Power (int a,int b,int c) {int Res=1;while (b) {if (b&1) res=res*a%c;b>& gt;=1;a=a*a%c;} return res;} void Gauss () {for (int r,i=1;i<=n;i++) {r=i;for (int j=i+1;j<=n;j++) if (ABS (A[r][i]) <abs (A[j][i])) r=j;if (A[r] [i]==0) Continue;if (r!=i) for (int j=1;j<=n+1;j++) swap (a[i][j],a[r][j]), int inv=power (A[I][I],P-2,P); for (int j=1; j<=n;j++) if (j!=i) {for (int k=n+1;k>=i;k--) a[j][k]= (a[j][k]-(A[J][I]*INV)%p*a[i][k]%p+p)%P;}}} int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%d", &p), scanf ("%s", ch+1), N=strlen (ch+1); for (int i=1;i <=n;i++) {int tmp=1;for (int j=1;j<=n;j++) {a[i][j]=tmp;tmp=tmp*i%p;} a[i][n+1]=ch[i]== ' * '? 0:ch[i]-' a ' +1;} Gauss (); for (int i=1;i<=n;i++) {if (a[i][i]==0) {printf ("0"); continue;} int Inv=power (a[i][i],p-2,p);p rintf ("%d", inv*a[i][n+1]%p);}    Puts ("");} return 0;}

  

"poj2065" SETI

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.