【poj2065】 SETI

來源:互聯網
上載者:User

標籤:tin   定義   names   power   log   素數   href   字元   字串   

http://poj.org/problem?id=2065 (題目連結)

題意

  題意半天看不懂。。給你一個素數P(P<=30000)和一串長為n的字串str[]。字母‘*‘代表0,字母a-z分別代表1-26,這n個字元所代表的數字分別代表f(1)、f(2)....f(n)。定義: ${f(k)=\sum_{i=0}^{n-1}{a_ik^i~(mod~p)}~(1<=k<=n,0<=a_i<P)}$,求a0、a1.....an-1。題目保證肯定有唯一解。

Solution

  直接高斯消元,因為是模方程組所以除的時候求個逆元即可。

代碼
// 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".in","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>>=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);printf("%d ",inv*a[i][n+1]%P);}puts("");}    return 0;}

  

【poj2065】 SETI

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.