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 |
贴个代妈//============================================================================ // Name : main2065.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> #include <string> using namespace std; int NI(int p, int q){ //p对q的逆 if(p > q) return NI(p%q, q); if(p == 1 || q == 1) return 1; int y = NI(q%p, p); int ans = ((q*y-1)/p)%q; return q-ans; } int main() { int cases; cin >> cases; for(int ii = 0; ii < cases; ii++){ int P; cin >> P; int T[100]; string s; cin >> s; int A[100]; int M[100][100]; int N = s.length(); for(int i = 1; i <= N; i++){ if(s[i-1] == '*') T[i] = 0; else T[i] = s[i-1] - 'a' + 1; } for(int i = 1; i <= N; i++) M[i][0] = 1; for(int i = 1; i <= N; i++){ for(int j = 0; j < N-1; j++){ M[i][j+1] = (M[i][j] * i)%P; } } //下面是搞死校园 int Z[100]; for(int i = 1; i <= N; i++) Z[i] = i; for(int i = 1; i < N; i++){ if(M[Z[i]][i-1] == 0){ int swap = i+1; for(; swap <= N; swap++){ if(M[Z[swap]][i-1] != 0) break; } int temp = Z[swap]; Z[swap] = Z[i]; Z[i] = temp; } for(int j = i; j <= N; j++){ if(M[Z[j]][i-1] == 0) continue; int ni = NI(M[Z[j]][i-1], P); for(int k = i; k < N; k++){ M[Z[j]][k] = (M[Z[j]][k] * ni) % P; } T[Z[j]] = (T[Z[j]] * ni) % P; M[Z[j]][i-1] = 1; } for(int j = i+1; j <= N; j++){ if(M[Z[j]][i-1] == 0) continue; for(int k = i-1; k < N; k++){ M[Z[j]][k] = (M[Z[j]][k] + P - M[Z[i]][k]) % P; } T[Z[j]] = (T[Z[j]] + P - T[Z[i]])%P; } }/* for(int i = 1; i <= N; i++){ for(int j = 0; j < N; j++){ cout << M[Z[i]][j] << " "; } cout << T[Z[i]] << endl; }*/ int res[100]; for(int i = N; i > 0; i--){ res[Z[i]] = T[Z[i]]; for(int j = i+1; j <= N; j++){ res[Z[i]] += res[Z[j]] * (P - M[Z[i]][j-1]); res[Z[i]] %= P; } //res[Z[i]] %= P; } for(int i = 1; i <= N; i++){ cout << res[i] << " "; } cout << endl; } //cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!! return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator