UVA 1563 - SETI

option=com_onlinejudge&Itemid=8&page=show_problem&category=520&problem=4338&mosmsg=Submission+received+with+ID+14015694" target="_blank" style="">题目链接

题意:依据题目那个式子。构造一个序列,能生成对应字符串

思路:依据式子能构造出n个方程。一共解n个未知量,利用高斯消元去解,中间过程有取摸过程。所以遇到除法的时候要使用逆元去搞

代码:

#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;
}

UVA 1563 - SETI (高斯消元+逆元)的更多相关文章

  1. POJ.2065.SETI(高斯消元 模线性方程组)

    题目链接 \(Description\) 求\(A_0,A_1,A_2,\cdots,A_{n-1}\),满足 \[A_0*1^0+A_1*1^1+\ldots+A_{n-1}*1^{n-1}\equ ...

  2. UVA 11542 - Square(高斯消元)

    UVA 11542 - Square 题目链接 题意:给定一些数字.保证这些数字质因子不会超过500,求这些数字中选出几个,乘积为全然平方数,问有几种选法 思路:对每一个数字分解成质因子后.发现假设要 ...

  3. poj 2065 SETI 高斯消元

    看题就知道要使用高斯消元求解! 代码如下: #include<iostream> #include<algorithm> #include<iomanip> #in ...

  4. POJ 2065 SETI [高斯消元同余]

    题意自己看,反正是裸题... 普通高斯消元全换成模意义下行了 模模模! #include <iostream> #include <cstdio> #include <c ...

  5. POJ2065 SETI 高斯消元

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2065 题意概括 多组数据,首先输入一个T表示数据组数,然后,每次输入一个质数,表示模数,然后,给出一 ...

  6. UVa 11542 Square (高斯消元)

    题意:给定 n 个数,从中选出一个,或者是多个,使得选出的整数的乘积是完全平方数,求一共有多少种选法,整数的素因子不大于 500. 析:从题目素因子不超过 500,就知道要把每个数进行分解.因为结果要 ...

  7. POJ SETI 高斯消元 + 费马小定理

    http://poj.org/problem?id=2065 题目是要求 如果str[i] = '*'那就是等于0 求这n条方程在%p下的解. 我看了网上的题解说是高斯消元 + 扩展欧几里德. 然后我 ...

  8. POJ2065 SETI(高斯消元 同模方程)

    (a1 * 1^0  +   a2 * 1^1  + ...  an * 1^n - 1) % P = f1 .... (a1 * n^0  +   a2 * n^1  + ...  an - 1 * ...

  9. POJ 2065 SETI 高斯消元解线性同余方程

    题意: 给出mod的大小,以及一个不大于70长度的字符串.每个字符代表一个数字,且为矩阵的增广列.系数矩阵如下 1^0 * a0 + 1^1 * a1 + ... + 1^(n-1) * an-1 = ...

随机推荐

  1. 83.个人信息维护页面 Extjs 页面

    1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8" ...

  2. 七牛php上传下载类,集成官方文档的方法

    <?phpuse Qiniu\Auth;use Qiniu\Storage\UploadManager;class qiniu{ public $_accesskey = null; publi ...

  3. linux更换阿里云的源的shell脚本

    #!/bin/bash##########################################Function: update source#Usage: bash update_sour ...

  4. C - cAPS lOCK

    Problem description wHAT DO WE NEED cAPS LOCK FOR? Caps lock is a computer keyboard key. Pressing it ...

  5. yaml标记语言的简介

    今天遇到yml这个文件,挺懵的.也是百度了一把. 这篇博文不错:http://www.ibm.com/developerworks/cn/xml/x-1103linrr/ 这图画得不错:http:// ...

  6. 获取Json中特定的值

      假如我们得到了一个json的数据:json===>   {"Head":{"TransCode":"X1009","Tr ...

  7. 【Oracle】闪回表

    语法: FLASHBACK TABLE [ schema. ] table [, [ schema. ] table ]... TO { { { SCN | TIMESTAMP } expr| RES ...

  8. 【技术累积】【点】【java】【19】访问权限

    java中的四种访问权限 范围如下表 权限 类内 同包 不同包子类 不同包非子类 Public ✔️ ✔️ ✔️ ✔️ 默认(Default) ✔️ ✔️ ️ Protected ✔️ ✔️ ✔️ P ...

  9. springboot 的 @Async

    /** * Created by zhiqi.shao on 2018/4/3. */ @EnableAsync @Configuration public class TaskPoolConfig ...

  10. Js 删除前弹出确认框

    <td align="center" valign="middle" class="black3"> <c:if test ...