一、题目

   SETI

二、分析  

  给定一个模数,一串字符串,字符串长度为N,相当于是N个方程的答案,而这N个方程中有N个未知数,要求的就是这N个未知数的值,很显然的高斯消元,遇到模数和除法,用逆元就好。

三、AC代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath> using namespace std;
#define ll long long
#define Min(a,b) ((a)>(b)?(b):(a))
#define Max(a,b) ((a)>(b)?(a):(b))
const int maxn = 1e2;
int p;
char s[maxn];
int a[maxn][maxn], x[maxn];
int n, equ, var; int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a%b);
} int lcm(int a, int b)
{
return a / gcd(a, b) * b;
} int inv(int a, int m)
{
if(a == 1)
return 1;
return inv(m%a, m) * (m - m/a)%m;
} int getInt(char c)
{
if(c == '*')
return 0;
else
return c - 'a' + 1;
} int Gauss()
{
int k, col, max_r;
for(k = 0, col = 0; k < equ && col < var; k++, col++)
{
max_r = k;
for(int i = k + 1; i < equ; i++)
{
if(fabs(a[i][col]) > fabs(a[max_r][col]))
max_r = i;
}
if(a[max_r][col] == 0)
{
k--;
continue;
}
if(max_r != k)
{
for(int i = col; i < (var + 1); i++)
{
swap(a[max_r][i], a[k][i]);
}
}
//消元
for(int i = k + 1; i < equ; i++)
{
if(a[i][col] != 0)
{
int LCM = lcm( fabs(a[i][col]), fabs(a[k][col]));
int ta = LCM / fabs(a[i][col]);
int tb = LCM / fabs(a[k][col]);
//异号
if(a[i][col]*a[k][col] < 0)
tb = -tb;
for(int j = col; j < (var + 1); j++)
{
a[i][j] = ((a[i][j] * ta - a[k][j] * tb) % p + p)%p;
a[i][col] = 0;
}
}
}
}
for(int i = k; i < equ; i++)
{
if(a[i][var+1] != 0)
return -1; //无解
}
//多解
if(k < var)
return var - k;
for(int i = var - 1; i >= 0; i--)
{
int tmp = a[i][var];
for(int j = i + 1; j < var ;j++)
{
if(a[i][j] != 0)
{
tmp -= a[i][j] * x[j];
}
tmp = (tmp % p + p) % p;
}
x[i] = (tmp * inv(a[i][i], p)) % p;
}
} void solve()
{
equ = n, var = n;
int res = Gauss();
for(int i = 0; i < n; i++)
{
if(i)
printf(" ");
printf("%d", x[i]);
}
printf("\n");
} int main()
{
int T;
// freopen("input.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
scanf("%d", &T);
while(T--)
{
scanf("%d %s", &p, s);
n = strlen(s);
memset(a, 0, sizeof(a));
for(int i = 0; i < n; i++)
{
int res = 1;
for(int j = 0; j < n; j++)
{
a[i][j] = res;
res = res * (i + 1) % p;
}
a[i][n] = getInt(s[i]);
}
solve();
}
return 0;
}

  

POJ_2065 SETI 【同余高斯消元】的更多相关文章

  1. HDU 3364 Lanterns (高斯消元)

    题意:有n个灯和m个开关,每个开关控制数个灯的状态改变,给出k条询问,问使灯的状态变为询问中的状态有多少种发法. 析:同余高斯消元法,模板题,将每个开关控制每个灯列成行列式,最终状态是结果列,同余高斯 ...

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

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

  3. HDU 3571 N-dimensional Sphere( 高斯消元+ 同余 )

    N-dimensional Sphere Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  4. poj 2065 SETI 高斯消元

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

  5. POJ2065 SETI 高斯消元

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

  6. 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 ...

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

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

  8. 高斯消元 分析 && 模板 (转载)

    转载自:http://hi.baidu.com/czyuan_acm/item/dce4e6f8a8c45f13d7ff8cda czyuan 先上模板: /* 用于求整数解得方程组. */ #inc ...

  9. POJ 1166 The Clocks (爆搜 || 高斯消元)

    题目链接 题意: 输入提供9个钟表的位置(钟表的位置只能是0点.3点.6点.9点,分别用0.1.2.3)表示.而题目又提供了9的步骤表示可以用来调正钟的位置,例如1 ABDE表示此步可以在第一.二.四 ...

随机推荐

  1. hdu5303贪心

    http://acm.hdu.edu.cn/showproblem.php?pid=5303 说一下题目大意.. 有一个长为L的环..你家在原点位置0,那么剩下L-1个点上种有一些树, 给你树的位置和 ...

  2. 记一次 Raven2 渗透(phpmailer漏洞+UDF提权)

    目录: 1. 寻找IP 2.dirb目录爆破 2.PHPMailer漏洞反弹得到shell 3.python版本的exp修改 4.查看wordpress的wp-config.php配置文件得到数据库账 ...

  3. Windows 10 Emoji shortcuts

    Windows 10 Emoji shortcuts Windows 10 Emoji 快捷方式 https://support.microsoft.com/en-us/windows/windows ...

  4. auto embedded component in an online code editor

    auto embedded component in an online code editor how to auto open a component in the third parts onl ...

  5. node.js 如何处理一个很大的文件

    node.js 如何处理一个很大的文件 思路 arraybuffer 数据分段 时间分片 多线程 web workers sevice workers node.js 如何处理一个很大的文件 http ...

  6. npm install 原理

    npm install 原理 https://docs.npmjs.com/about-npm/ npm consists of three distinct components: the webs ...

  7. 可视化埋点 & XPath

    可视化埋点 & XPath https://www.w3.org/TR/xpath-full-text-30/ 数据的准确性 采集时机 数据发送策略 full XPath demo XML & ...

  8. nasm astrlwr_s函数 x86

    xxx.asm %define p1 ebp+8 %define p2 ebp+12 %define p3 ebp+16 section .text global dllmain export ast ...

  9. 打造NGK生态星空计划,高倍币VAST即将震撼上线!

    援引华盛顿邮报.彭博社.路透社以及CNN等知名媒体的报道,NGK官方近日宣布,为了完善NGK生态星空计划,NGK官方近日即将推出SPC的子币VAST,以鼓励更多的生态建设者参与. NGK官方相关负责人 ...

  10. NGK是如何运用IPFS分布式存储的?

    整个夏季,除了天气的火热,还有的火热莫过于IPFS挖矿这个领域了.IPFS的概念火热到,你可以看到到处都在卖IPFS矿机.那么,是什么原因导致IPFS这么火呢?在这之前,我们先了解一下什么是IPFS技 ...