http://poj.org/problem?id=2065

题目是要求

如果str[i] = '*'那就是等于0

求这n条方程在%p下的解。

我看了网上的题解说是高斯消元 + 扩展欧几里德。

然后我自己想了想,就用了高斯消元 + 费马小定理。因为%p是质数,所以很容易就用上了费马小定理,就是在除法的时候用一次就好了。还有就是两个模数相乘还要模一次。

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
int p;
const int maxn = 1e2;
char str[maxn];
int quick_pow(int a, int b, int MOD) { //求解 a^b%MOD的值
int base = a % MOD;
int ans = ; //相乘,所以这里是1
while (b) {
if (b & ) {
ans = (ans * base) % MOD; //如果这里是很大的数据,就要用quick_mul
}
base = (base * base) % MOD; //notice。注意这里,每次的base是自己base倍
b >>= ;
}
return ans;
} class GaussMatrix { //复杂度O(n3)
public:
int a[maxn][maxn];
int equ, val; //方程(行)个数,和变量(列)个数,其中第val个是b值,不能取
void init() {
for (int i = ; i <= equ; ++i) {
for (int j = ; j <= val; ++j) {
a[i][j] = 0.0;
}
}
}
void swapRow(int rowOne, int rowTwo) {
for (int i = ; i <= val; ++i) {
swap(a[rowOne][i], a[rowTwo][i]);
}
}
void swapCol(int colOne, int colTwo) {
for (int i = ; i <= equ; ++i) {
swap(a[i][colOne], a[i][colTwo]);
}
}
bool same(int x, int y) {
return x == y;
}
int guass() {
int k, col; // col,当前要处理的列, k当前处理的行
for (k = , col = ; k <= equ && col < val; ++k, ++col) { //col不能取到第val个
int maxRow = k; //选出列最大值所在的行,这样使得误差最小。(没懂)
for (int i = k + ; i <= equ; ++i) {
if (abs(a[i][col]) > abs(a[maxRow][col])) {
maxRow = i;
}
}
if (same(a[maxRow][col], )) { //如果在第k行以后,整一列都是0
--k; //则这个变量就是一个自由变量。
continue;
}
if (maxRow != k) swapRow(k, maxRow); // k是当前的最大行了
for (int i = col + ; i <= val; ++i) { //整一列约去系数
// a[k][i] /= a[k][col];
a[k][i] = (a[k][i] * quick_pow(a[k][col], p - , p)) % p;
}
a[k][col] = ; //第一个就要变成1了,然后它下面和上面的变成0
for (int i = ; i <= equ; ++i) {
if (i == k) continue; //当前这行,不操作
for (int j = col + ; j <= val; ++j) { //要使a[i][col] = 0,则需要a[i][col]倍
// a[i][j] -= a[i][col] * a[k][j]; //这一行减去相应的倍数
a[i][j] = (a[i][j] - (a[i][col] * a[k][j]) % p + p) % p;
}
a[i][col] = ;
}
// debug();
}
for (int res = k; res <= equ; ++res) {
if (!same(a[res][val], )) return -; //方程无解
}
return val - k; //自由变量个数
}
void debug() {
for (int i = ; i <= equ; ++i) {
for (int j = ; j <= val; ++j) {
printf("%d ", a[i][j]);
}
printf("\n");
}
printf("*******************************************\n\n");
}
} arr;
void init() {
arr.init();
int lenstr = strlen(str + );
arr.equ = lenstr, arr.val = lenstr + ;
int now, to = ;
for (int i = ; i <= lenstr; ++i) {
now = ;
for (int j = ; j <= lenstr; ++j) {
arr.a[i][j] = now;
now = now * to % p;
}
to++;
if (str[i] == '*') arr.a[i][lenstr + ] = ;
else arr.a[i][lenstr + ] = str[i] - 'a' + ;
}
// arr.debug();
}
void work() {
cin >> p >> str + ;
init();
int res = arr.guass();
// assert(res == 0);
int lenstr = strlen(str + );
for (int i = ; i <= lenstr; ++i) {
cout << arr.a[i][lenstr + ] << " ";
}
cout << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--) work();
return ;
}

POJ SETI 高斯消元 + 费马小定理的更多相关文章

  1. poj 3734 Blocks 快速幂+费马小定理+组合数学

    题目链接 题意:有一排砖,可以染红蓝绿黄四种不同的颜色,要求红和绿两种颜色砖的个数都是偶数,问一共有多少种方案,结果对10007取余. 题解:刚看这道题第一感觉是组合数学,正向推了一会还没等推出来队友 ...

  2. poj 1845 【数论:逆元,二分(乘法),拓展欧几里得,费马小定理】

    POJ 1845 题意不说了,网上一大堆.此题做了一天,必须要整理一下了. 刚开始用费马小定理做,WA.(poj敢说我代码WA???)(以下代码其实都不严谨,按照数据要求A是可以等于0的,那么结果自然 ...

  3. 【poj 1284】Primitive Roots(数论--欧拉函数 求原根个数){费马小定理、欧拉定理}

    题意:求奇质数 P 的原根个数.若 x 是 P 的原根,那么 x^k (k=1~p-1) 模 P 为1~p-1,且互不相同. (3≤ P<65536) 解法:有费马小定理:若 p 是质数,x^( ...

  4. hdu 4704 Sum (整数和分解+快速幂+费马小定理降幂)

    题意: 给n(1<n<),求(s1+s2+s3+...+sn)mod(1e9+7).其中si表示n由i个数相加而成的种数,如n=4,则s1=1,s2=3.                  ...

  5. nyoj1000_快速幂_费马小定理

    又见斐波那契数列 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 斐波那契数列大家应该很熟悉了吧.下面给大家引入一种新的斐波那契数列:M斐波那契数列. M斐波那契数列 ...

  6. 数论初步(费马小定理) - Happy 2004

    Description Consider a positive integer X,and let S be the sum of all positive integer divisors of 2 ...

  7. 【BZOJ1951】【SDOI2010】古代猪文 Lucas定理、中国剩余定理、exgcd、费马小定理

    Description “在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心……” ——选自猪王国民歌 很久很久以前,在山的那边 ...

  8. 数论 --- 费马小定理 + 快速幂 HDU 4704 Sum

    Sum Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=4704 Mean: 给定一个大整数N,求1到N中每个数的因式分解个数的 ...

  9. HDU 5667 Sequence 矩阵快速幂+费马小定理

    题目不难懂.式子是一个递推式,并且不难发现f[n]都是a的整数次幂.(f[1]=a0;f[2]=ab;f[3]=ab*f[2]c*f[1]...) 我们先只看指数部分,设h[n]. 则 h[1]=0; ...

随机推荐

  1. PHP截取中英文混合字符

    <?php //////////////////////////////////////////////////////////////////// // PHP截取中英文及标点符号混合的字符串 ...

  2. linux应用之gcc编译器的安装及使用

    gcc是linux系统下功能十分强大的编译器. 本人使用的是CentOS 6.6 64位系统,由于在安装系统的时候并没有勾选安装gcc编译器,因此需要自行安装gcc编译器. 使用yum安装gcc 对于 ...

  3. 编译thrift外篇-关于默认链接包-(使用mapkeeper运行leveldb成功)

    根据 https://stackoverflow.com/questions/9922949/how-to-print-the-ldlinker-search-path 使用 ldconfig -v ...

  4. hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)

    题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others) ...

  5. 小trick之mklink

    因为要看很多论文就下载安装了zotero,又因为文献库的文件夹在安装目录太深,找起来太麻烦,再加上是软件本身的安装目录,因此把论文都下载在默认文件中总会天然地产生不安全感,万一误删软件怎么办.所以在文 ...

  6. cygwin Could not create directory '/home/Administrator/.ssh'

    在cygwin下运行: ssh-keygen -C "634772208@qq.com" -t rsa 时,出现如下错误: cygwin Could not create dire ...

  7. Java笔记(四)

    13. 集合框架: 集合中存储的都是对象的引用(地址) 迭代器:集合的取出元素的方式 import java.util.ArrayList; import java.util.Iterator; pu ...

  8. 洛谷 1072 Hankson 的趣味题——质因数界限讨论

    题目:https://www.luogu.org/problemnew/show/P1072 思路是把每个数质因数分解,答案对于每个质因数的次数有选择的区间,通过这个计算. 指数的限制就是上限是b1, ...

  9. Ubuntu18.04 安装 JDK7

    直接下载jdk压缩包方式安装 1.官网下载JDK      地址: http://www.oracle.com/technetwork/articles/javase/index-jsp-138363 ...

  10. Jasper:API / 后向兼容性

    ylbtech-Jasper:API / 后向兼容性 1.返回顶部 1. 后向兼容性 为了给客户提供创新的业务解决方案,Cisco Jasper 会定期扩展我们 API 框架的功能.我们会尽最大努力确 ...