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. haproxy参数解析

    HAProxy工作于隧道模式,其仅检查每一个连接的第一个请求, 1. option abortonclose #当服务器负载过高时,将自动关闭队列中处理时间较长的连接请求 2. option http ...

  2. codevs 1012 最大公约数和最小公倍数问题

    题目描述 Description 输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数 条件:  1.P,Q是正整 ...

  3. RightScale发布2017年度云调查报告

    RightScale最近发布了他们的年度云报告(RightScale 2017云现状报告,RightScale 2017 State of the Cloud Report),这份报告包括了云计算在采 ...

  4. Keras 可视化 model

    参考:https://keras.io/visualization/ error解决参考:http://blog.csdn.net/wangjian1204/article/details/50346 ...

  5. 单次目标检测器-YOLO简介

    YOLO 在卷积层之后使用了 DarkNet 来做特征检测. 然而,它并没有使用多尺度特征图来做独立的检测.相反,它将特征图部分平滑化,并将其和另一个较低分辨率的特征图拼接.例如,YOLO 将一个 2 ...

  6. Ubuntu12.04下安装VirtualBox

    目录: 安装虚拟机VirtualBox 虚拟机VirtualBox安装win7全过程 虚拟机共享文件夹.U盘 一.安装虚拟机VirtualBox VirtualBox下载地址:https://www. ...

  7. 确认框的使用。弹出一个确认框,Ajax提交一个请求,刷新页面。

    视图: <td> @if (item.ReviewProject.DeclareState == DeclareOrReviewState.正在进行 && !item.Is ...

  8. nohup、&、tail 在服务启动中的用法

    在利用命令行启动各类服务的时候,控制台一般会显示相关日志信息,如果weblogic的启动,我们可能有这样需求:启动服务器后即使关闭控制服务器依然运行 ,日志不但可以在控制台显示同时也记录里后台日志文件 ...

  9. python 之单例

    # 单例模式 class MySQL: __instance = None def __init__(self): self.host = '127.0.0.1' self.port = 3306 @ ...

  10. c++函数模板1

    1 定义: 函数模板 只适用于参数个数相同但是类型不同 而且函数体相同的情况 2 这个例子没有使用模板的情况 #include <iostream> using namespace std ...