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. runtime之方法的交换

    工作中没怎么用到runtime的东西,所以一直没怎么看,现在开始拿起来. runtime之方法的交换: 都知道OC中有category可以对已知类进行扩展,但是假如工程中需要修改某类的原方法,若用ca ...

  2. 常规问题解决:File "/usr/bin/yum", line 30 及 File "/usr/libexec/urlgrabber-ext-down", line 28

    在执行yum命令时忽然发现出现以下报错: # yum list File "/usr/bin/yum", line 30 except KeyboardInterrupt, e: ...

  3. hdu 1753 大明A+B(大数)

    题意:小数大数加法 思路:大数模板 #include<iostream> #include<stdio.h> #include<string.h> using na ...

  4. hdu-5761 Rower Bo(数学)

    题目链接: Rower Bo Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 131072/131072 K (Java/Others ...

  5. testing

    doctest_test.py def square(x): ''' 这里需要空格 >>> square(2) 4 啦啦 >>> square(3) 9 上不一行能 ...

  6. redis-cluster的实例动态调整内存

    当redis.conf中的最大内存配置为10G的时候,恰好程序已经写满了,但是物理主机是有内存的, 此时可以通过config set xxxx xxxx 来设置实例的内存大小,而不需要重启实例. 获取 ...

  7. 「LuoguP3376」 【模板】网络最大流

    题目描述 如题,给出一个网络图,以及其源点和汇点,求出其网络最大流. 输入输出格式 输入格式: 第一行包含四个正整数N.M.S.T,分别表示点的个数.有向边的个数.源点序号.汇点序号. 接下来M行每行 ...

  8. Fire Game

    Description Fat brother and Maze are playing a kind of special (hentai) game on an N*M board (N rows ...

  9. redis sentinel(哨兵)配置解读

    1 port <sentinel-port> :哨兵实例运行所在的端口(默认26379) 2 sentinel announce-ip:哨兵将会在gossip hello消息中使用指定的i ...

  10. ubuntu12.04下有线网无电缆插入问题

    解决方案: 1.查看是否没装网卡驱动: 2.检查网卡是否损坏: 3.检查网线是否损坏(注意:有时候不一定是网线损坏了,而是网线太细了,导通性不好.本人用细网线试了一下,windows下面可以连接网络, ...