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. 关于S50卡密钥A和密钥B

    关于S50卡密钥A和密钥B 1. Mifare_Std 卡片的密钥属性取决于控制字.控制字的默认值是“FF078069”,此时 A密钥:不可被读出,有全部权限. B密钥:可被读出,没有任何权限. 2. ...

  2. 机器学习: Canonical Correlation Analysis 典型相关分析

    Canonical Correlation Analysis(CCA)典型相关分析也是一种常用的降维算法.我们知道,PCA(Principal Component Analysis) 主分量分析将数据 ...

  3. openssl生成公钥私钥对 加解密

    在计算机软件开发世界中,编程语言种类极多,数据在各种语言的表现形式可能有所差异,但数据本身的处理可能,或者说本质上是完全一样的:比如数据在某个算法中的运算过程是一样的.在这里,我以加密与解密来作为例子 ...

  4. Swift引用计数器

    ARC概述 和4.2+版本的Xcode对OC的支持一样,Swift也是使用ARC来管理内存,文档是这么描述的: Swift uses Automatic Reference Counting(ARC) ...

  5. NMS 原理 了解

    NMS 原理:对于Bounding Box的列表B及其对应的置信度S,采用下面的计算方式.选择具有最大score的检测框M,将其从B集合中移除并加入到最终的检测结果D中.通常将B中剩余检测框中与M的I ...

  6. 从MySQL获取数据

    安装 PM> install-package newtonsoft.json PM> install-package mysql.data string connectionString ...

  7. 杂项:MIS

    ylbtech-杂项:MIS 1.返回顶部 1. 管理信息系统(Management Information System,简称MIS)是一个以人为主导,利用计算机硬件.软件.网络通信设备以及其他办公 ...

  8. ceph部署与问题

    一.基本情况:物理设备:4台惠普dl360,4个千兆网卡 4个1T盘操作系统统一为:CentOS 7.2.1511ceph版本:10.2.3ceph-deploy版本:1.5.36网络情况:192.1 ...

  9. 1.25-1.26 Coordinator数据集和oozie bundle

    一.Coordinator数据集 二.oozie bundle

  10. 使用Bootstrap实现表格列的显示与隐藏

    来人,上效果图 走官方通道 1.引入样式文件(去github下载样式文件) <!--插件开--> <link rel="stylesheet" href=&quo ...