Ultraviolet A 1563-SETI (Gaussian elimination yuan + reverse yuan)

Source: Internet
Author: User
Ultraviolet A 1563-SETI

Question Link

Create a sequence based on the formula of the question to generate the corresponding string.

Idea: according to the formula, we can construct n equations, which can solve n unknown numbers in total and describe them using Gaussian deyuan. The intermediate process involves a touch process, so when division is encountered, we need to use the reverse element.

Code:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 105;int pow_mod(int x, int k, int mod) {    int ans = 1;    while (k) {if (k&1) ans = ans * x % mod;x = x * x % mod;k >>= 1;    }    return ans;}int inv(int a, int n) {    return pow_mod(a, n - 2, n);}int t, p, n, A[N][N];char str[N];int hash(int c) {    if (c == '*') return 0;    return c - 'a' + 1;}void build() {    for (int i = 0; i < n; i++) {A[i][n] = hash(str[i]);int tmp = 1;for (int j = 0; j < n; j++) {    A[i][j] = tmp;    tmp = tmp * (i + 1) % p;}    }}void gauss() {    for (int i = 0; i < n; i++) {int r;for (r = i; r < n; i++)    if (A[r][i]) break;if (r == n) continue;for (int j = i; j <= n; j++) swap(A[r][j], A[i][j]);for (int j = 0; j < n; j++) {    if (i == j) continue;    if (A[j][i]) {int tmp = A[j][i] * inv(A[i][i], p) % p;for (int k = i; k <= n; k++) {    A[j][k] = (((A[j][k] - tmp * A[i][k]) % p) + p) % p;}    }}    }    for (int i = 0; i < n; i++)printf("%d%c", A[i][n] * inv(A[i][i], p) % p, i == n - 1 ? '\n' : ' ');}int main() {    scanf("%d", &t);    while (t--) {scanf("%d%s", &p, str);n = strlen(str);build();gauss();    }    return 0;}


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.