poj 2065 高斯消元取模解方程组
来源:互联网 发布:360搜索引擎推广 知乎 编辑:程序博客网 时间:2024/06/05 05:59
题意:
a0*1^0 + a1*1^1+a2*1^2+........+an-1*1^(n-1) = f(1)
a0*2^0 + a1*2^1+a2*2^2+........+an-1*2^(n-1) = f(2)
......
a0*n^0 + a1*n^1+a2*n^2+........+an-1*n^(n-1) = f(n)
解这个方程组。
代码:
#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstring>#include <cmath>#include <stack>#include <vector>#include <queue>#include <map>#include <climits>#include <cassert>#define LL long longusing namespace std;const int inf = 0x3f3f3f3f;const double eps = 1e-8;const double pi = 4 * atan(1.0);const double ee = exp(1.0);const int maxn = 1000 + 10;int a[maxn][maxn]; //增广矩阵int x[maxn]; //解集bool freeX[maxn]; //标记解是否是自由变元int gcd(int a, int b){ return b ? gcd(b, a % b) : a;}int lcm(int a, int b){ return a / gcd(a, b) * b;}//高斯消元解方程组//返回值-2表示有浮点数解,无整数解//返回值-1表示无解,0表示有唯一解,大于0表示有无穷解,返回自由变元个数//有equ个方程,var个变元//增广矩阵行数[0, equ - 1]//增广矩阵列数[0, var]int gauss(int equ, int var, int mod){ for (int i = 0; i <= var; i++) { x[i] = 0; freeX[i] = true; } //转换为阶梯矩阵 //col表示当前正在处理的这一列 int col = 0; int row = 0; //maxR表示当前这个列中元素绝对值最大的行 int maxRow; for (; row < equ && col < var; row++, col++) { //枚举当前正在处理的行 //找到该col列元素绝对值最大的那行与第k行交换 maxRow = row; for (int i = row + 1; i < equ; i++) { if (abs(a[maxRow][col]) < abs(a[i][col])) { maxRow = i; } } if (maxRow != row) { //与第row行交换 for (int j = row; j < var + 1; j++) { swap(a[row][j], a[maxRow][j]); } } if (a[row][col] == 0) { //说明该col列第row行以下全是0,处理当前行的下一列 row--; continue; } for (int i = row + 1; i < equ; i++) { //枚举要删的行 if (a[i][col] != 0) { int LCM = lcm(abs(a[i][col]), abs(a[row][col])); int ta = LCM / abs(a[i][col]); int tb = LCM / abs(a[row][col]); //异号 if (a[i][col] * a[row][col] < 0) tb = -tb; for (int j = col; j < var + 1; j++) { a[i][j] = a[i][j] * ta - a[row][j] * tb; a[i][j] = (a[i][j] % mod + mod) % mod; } } } }// //1. 无解的情况: 化简的增广阵中存在(0, 0, ..., a)这样的行(a != 0). for (int i = row; i < equ; i++) { if (a[i][col] != 0) { return -1; } } // 2. 无穷解的情况: 在var * (var + 1)的增广阵中出现(0, 0, ..., 0)这样的行,即说明没有形成严格的上三角阵. // 出现的行数即为自由变元的个数. if (row < var) { // 首先,自由变元有var - k个,即不确定的变元至少有var - k个. for (int i = row - 1; i >= 0; i--) { // 第i行一定不会是(0, 0, ..., 0)的情况,因为这样的行是在第k行到第equ行. // 同样,第i行一定不会是(0, 0, ..., a), a != 0的情况,这样的无解的. // freeNum用于判断该行中的不确定的变元的个数,如果超过1个,则无法求解,它们仍然为不确定的变元. int freeNum = 0; int freeIndex = 0; for (int j = 0; j < var; j++) { if (a[i][j] != 0 && freeX[j]) { freeNum++; freeIndex = j; } } if (1 < freeNum)// 无法求解出确定的变元. continue; // 说明就只有一个不确定的变元freeIndex,那么可以求解出该变元,且该变元是确定的. int tmp = a[i][var]; for (int j = 0; j < var; j++) { if (a[i][j] != 0 && j != freeIndex) { tmp -= a[i][j] * x[j]; } tmp = (tmp % mod + mod) % mod; } x[freeIndex] = (tmp / a[i][freeIndex]) % mod; freeX[freeIndex] = false; } return var - row; } // 3. 唯一解的情况: 在var * (var + 1)的增广阵中形成严格的上三角阵. // 计算出Xn-1, Xn-2 ... X0. for (int i = var - 1; i >= 0; i--) { int tmp = a[i][var]; for (int j = i + 1; j < var; j++) { if (a[i][j] != 0) { tmp -= a[i][j] * x[j]; } tmp = (tmp % mod + mod) % mod; } while (tmp % a[i][i] != 0) tmp += mod; x[i] = (tmp / a[i][i]) % mod; } return 0;}LL pow_mod(LL a, LL n, LL mod){ if (n == 0) return 1; LL x = pow_mod(a, n >> 1, mod); LL res = x * x % mod; if (n % 2) res = res * a % mod; return res;}int main(){#ifdef LOCAL freopen("in.txt", "r", stdin);#endif // LOCAL char s[101]; int mod; int ncase; scanf("%d", &ncase); while (ncase--) { scanf("%d%s", &mod, s); int len = strlen(s); for (int i = 0; i < len; i++) { if (s[i] == '*') a[i][len] = 0; else a[i][len] = s[i] - 'a' + 1; for (int j = 0; j < len; j++) { a[i][j] = pow_mod(i + 1, j, mod); } } gauss(len, len, mod); for (int i = 0; i < len; i++) printf("%d%c", x[i], i == len - 1 ? '\n' : ' '); } return 0;}
0 0
- poj 2065 高斯消元取模解方程组
- POJ 2065 SETI(高斯消元解模方程组)
- poj 2065 SETI(高斯消元 解同于方程组)
- poj 2065 高斯消元(取模的方程组)
- POJ 2065 SETI(高斯消元解同余方程组)
- poj 1830 异或方程组
- POJ 1006 同余方程组
- POJ 1831 不定方程组 中文
- poj 2947 高斯消元求解方程组取模
- POJ 2947 高斯消元法解同余方程组
- POJ 2891 解线性同余方程组
- POJ 2891 解一元线性同余方程组
- Poj 1222 EXTENDED LIGHTS OUT (高斯消元解异或方程组 开关问题)
- poj 2947 Widget Factory(高斯消元解同余方程组)
- poj 1222 EXTENDED LIGHTS OUT (高斯消元解异或方程组 开关问题)
- POJ 1222 EXTENDED LIGHTS OUT 高斯消元解异或方程组
- [省选前题目整理][POJ 1830]开关问题(XOR方程组高斯消元)
- poj 2947 Widget Factory(高斯消元解同余方程组)
- uip-学习笔记(移植篇)
- Linux 安装仿宋字体
- C语言中的volatile关键字
- 6种常见的数据加载模式设计
- Linux Ftp
- poj 2065 高斯消元取模解方程组
- Gradle在大型Java项目上的应用
- Silverlight 2 (beta1)数据操作(6)——使用LINQ to SQL进行数据CRUD操作(下)
- 机器学习05(SVM理论推导)
- 别踩白块儿游戏源码Android版
- HTTP POST请求报文格式分析与Java实现文件上传
- 如何选择ppt转pdf的转换方式
- Java NIO框架Netty教程(四) – ServerBootStrap启动流程源码分析
- 三大WEB服务器对比分析(apache ,lighttpd,nginx)