Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
贴个代码~~#include <iostream> #include <stdio.h> #include <fstream> #include <string.h> using namespace std; int p; long long A[310][310],x[310]; long long f(long long x,long long y) { long long tmp=1; while(y--) { tmp=tmp*x%p; } return tmp; } long long LCM(long long a,long long b) { long long a0=a,b0=b; while(b) { long long tmp=a; a=b; b=tmp%b; } return a0*b0/a; } long long abs(long long x) { if(x<0)x=-x; return x; } void swap(long long &x,long long &y) { long long tmp=x; x=y; y=tmp; } int gaos(int r,int c) { int i=1,j=1,l,k,id; while(i<=r&&j<=c) { id=i; for(l=1+i; l<=r; l++) { if(abs(A[l][j])>abs(A[id][j]))id=l; } if(A[id][j]) { for(l=j; l<=c+1; l++)swap(A[id][l],A[i][l]); for(l=i+1; l<=r; l++) { if(A[l][j]!=0) { long long lcm=LCM(abs(A[l][j]),abs(A[i][j])); long long ta=lcm/abs(A[l][j]),tb=lcm/abs(A[i][j]); if(A[i][j]*A[l][j]<0)tb=-tb; for(k=j; k<=c+1; k++)A[l][k]=(A[l][k]*ta-A[i][k]*tb)%p; } } i++; } j++; } k=i; for(i=k;i<=r;i++) { if(A[i][c+1])return -1; } if(k>=c+1) { memset(x,0,sizeof(x)); for(i=k-1;i>=1;i--) { long long tmp=A[i][c+1]; for(j=i+1;j<=c;j++) { tmp-=A[i][j]*x[j]; } while(tmp%A[i][i])tmp+=p; x[i]=tmp/A[i][i]; x[i]=(x[i]%p+p)%p; } } for(i=1;i<=c;i++) { printf("%lld ",x[i]); if(i==c)printf("\n"); } return 0; } int main() { //freopen("in.txt","r",stdin); int cas,n,j,i; char s1[100]; scanf("%d",&cas); while(cas--) { memset(A,0,sizeof(A)); scanf("%d %s",&p,s1); n=strlen(s1); for(i=0; i<n; i++) { if(s1[i]=='*')A[i+1][n+1]=0; else A[i+1][n+1]=(s1[i]-'a'+1); for(j=1; j<=n; j++)A[i+1][j]=f(i+1,j-1); } int ans=gaos(n,n); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator