uva1563
https://vjudge.net/problem/UVA-1563
高斯消元解同余方程组 就是把原来的除法换成逆元,其他的都一样
#include<bits/stdc++.h>
using namespace std;
const int N = ;
int n, p;
int a[N][N];
char s[N];
int power(int x, int t)
{
int ret = ;
for(; t; t >>= , x = x * x % p) if(t & ) ret = ret * x % p;
// printf("ret=%d\n", ret);
return ret;
}
void build()
{
for(int i = ; i < n; ++i)
{
if(s[i + ] == '*') a[i][n] = ;
else a[i][n] = s[i + ] - 'a' + ;
}
for(int k = ; k < n; ++k)
for(int i = ; i < n; ++i)
a[k][i] = power(k + , i);
}
void gauss_jordan()
{
for(int now = ; now < n; ++now)
{
int x = now;
for(int i = now; i < n; ++i) if(abs(a[i][now]) > abs(a[x][now])) x = i;
for(int i = ; i <= n; ++i) swap(a[now][i], a[x][i]);
int inv = power(a[now][now], p - );
for(int i = now; i <= n; ++i) a[now][i] = a[now][i] * inv % p; // /a[now][now]
for(int i = ; i < n; ++i) if(i != now && a[i][now])
{
int t = a[i][now]; // a[i][j] = a[i][j] - t * a[now][j]
for(int j = ; j <= n; ++j) a[i][j] = ((a[i][j] % p - t * a[now][j] % p) % p + p)% p;
}
}
}
int main()
{
int T; scanf("%d", &T);
while(T--)
{
memset(a, , sizeof(a));
scanf("%d%s", &p, s + ); n = strlen(s + );
build();
gauss_jordan();
for(int i = ; i < n - ; ++i) printf("%d ", a[i][n]);
printf("%d\n", a[n - ][n]);
}
return ;
}
uva1563的更多相关文章
随机推荐
- C#DateTimeFormatInfo类
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAzAAAACdCAIAAADkAArhAAAgAElEQVR4nO1dWXbjug7UzrI0LS1L0/ ...
- POJ-2239 Selecting Courses,三维邻接矩阵实现,钻数据空子。
Selecting Courses Time Limit: 1000MS Memory Limit: 65536K Description It is well known that ...
- HDU 4465 递推与double的精确性
题目大意不多说了 这里用dp[i][0] 代表取完第一个盒子后第二个盒子剩 i 个的概率,对应期望就是dp[i][0] *i dp[i][1] 就代表取完第二个盒子后第一个盒子剩 i 个的概率 dp[ ...
- 2018/3/3 解析ThreadLocal源码
今天听到一个老哥说道ThreadLocal在源码设计上面的一些好处,于是决定把ThreadLocal源码彻底分析一下. 首先,我们来看下set方法 可以看到,这个方法里,先获得了当前线程,之后将当前线 ...
- SpringBoot学习day01
SpringBoot目的在于创建和启动新的基于Spring框架的项目.SpringBoot会选择最合适的Spring子项目和第三方开源库进行整合.大部分SpringBoot应用只需要非常少量的配置就可 ...
- Win7 文件加密存储操作后,如何在事后备份证书、秘钥
这个密钥的特点是只有在此系统下用此账户才可以修改,即便是你用此账户设置加密后删除此账户再重新建一个同名的账户依然无法修改.而且此密钥无法破解.所以一旦加密后,重装系统或者更换账户就无法修改了.唯一的办 ...
- 打造Spring Cloud构建微服务架构的最全资料
访问: https://git.oschina.net/didispace/SpringCloud-Learning http://blog.didispace.com/categories/Spri ...
- react的类型检查PropTypes自React v15.5起已弃用,请使用prop-types
最近使用React的类型检查PropTypes时,遇到错误:TypeError: Cannot read property 'array' of undefined 看了下自己的React版本: ...
- jmeter的Classpath即类或者jar包的搜索路径设置
对于master-slave模式,插件和依赖都需要放到slave上才能生效,并且需要重启slave使插件生效 查看配置文件:apache-jmeter-3.1/bin/jmeter.propertie ...
- 条款十六: 在operator=中对所有数据成员赋值
当涉及到继承时,派生类的赋值运算符也必须处理它的基类成员的赋值!否则,当派生类对象向另一个派生类对象赋值时,只有派生类部分赋值了.看看下面: class base { public: ): x(ini ...