当前位置:网站首页>7/28 高斯消元解线性方程组+高斯消元解异或线性方程组 +求组合数ii
7/28 高斯消元解线性方程组+高斯消元解异或线性方程组 +求组合数ii
2022-07-29 01:48:00 【咸蛋_dd】
1.高斯消元线性方程组
输入一个包含 nn 个方程 nn 个未知数的线性方程组。
方程组中的系数为实数。
求解这个方程组。
下图为一个包含 mm 个方程 nn 个未知数的线性方程组示例:
输入格式
第一行包含整数 nn。
接下来 nn 行,每行包含 n+1n+1 个实数,表示一个方程的 nn 个系数以及等号右侧的常数。
输出格式
如果给定线性方程组存在唯一解,则输出共 nn 行,其中第 ii 行输出第 ii 个未知数的解,结果保留两位小数。
如果给定线性方程组存在无数解,则输出
Infinite group solutions。如果给定线性方程组无解,则输出
No solution。数据范围
1≤n≤1001≤n≤100,
所有输入系数以及常数均保留两位小数,绝对值均不超过 100100。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N = 110;
const double eps = 1e-8;
int n;
double a[N][N];
int gauss() // 高斯消元,答案存于a[i][n]中,0 <= i < n
{
int c, r;
for (c = 0, r = 0; c < n; c ++ )
{
int t = r;
for (int i = r; i < n; i ++ ) // 找绝对值最大的行
if (fabs(a[i][c]) > fabs(a[t][c]))
t = i;
if (fabs(a[t][c]) < eps) continue;
for (int i = c; i <= n; i ++ ) swap(a[t][i], a[r][i]); // 将绝对值最大的行换到最顶端
for (int i = n; i >= c; i -- ) a[r][i] /= a[r][c]; // 将当前行的首位变成1
for (int i = r + 1; i < n; i ++ ) // 用当前行将下面所有的列消成0
if (fabs(a[i][c]) > eps)
for (int j = n; j >= c; j -- )
a[i][j] -= a[r][j] * a[i][c];
r ++ ;
}
if (r < n)
{
for (int i = r; i < n; i ++ )
if (fabs(a[i][n]) > eps)
return 2; // 无解
return 1; // 有无穷多组解
}
for (int i = n - 1; i >= 0; i -- )
for (int j = i + 1; j < n; j ++ )
a[i][n] -= a[i][j] * a[j][n];
return 0; // 有唯一解
}
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i ++ )
for (int j = 0; j < n + 1; j ++ )
scanf("%lf", &a[i][j]);
int t = gauss();
if (t == 2) puts("No solution");
else if (t == 1) puts("Infinite group solutions");
else
{
for (int i = 0; i < n; i ++ )
{
if (fabs(a[i][n]) < eps) a[i][n] = 0; // 去掉输出 -0.00 的情况
printf("%.2lf\n", a[i][n]);
}
}
return 0;
}2.高斯消元解异或线性方程组
与线性方程组类似,每次找一行不为0的,然后异或求解
输入一个包含 nn 个方程 nn 个未知数的异或线性方程组。
方程组中的系数和常数为 00 或 11,每个未知数的取值也为 00 或 11。
求解这个方程组。
异或线性方程组示例如下:
M[1][1]x[1] ^ M[1][2]x[2] ^ … ^ M[1][n]x[n] = B[1] M[2][1]x[1] ^ M[2][2]x[2] ^ … ^ M[2][n]x[n] = B[2] … M[n][1]x[1] ^ M[n][2]x[2] ^ … ^ M[n][n]x[n] = B[n]其中
^表示异或(XORXOR),M[i][j]M[i][j] 表示第 ii 个式子中 x[j]x[j] 的系数,B[i]B[i] 是第 ii 个方程右端的常数,取值均为 00 或 11。输入格式
第一行包含整数 nn。
接下来 nn 行,每行包含 n+1n+1 个整数 00 或 11,表示一个方程的 nn 个系数以及等号右侧的常数。
输出格式
如果给定线性方程组存在唯一解,则输出共 nn 行,其中第 ii 行输出第 ii 个未知数的解。
如果给定线性方程组存在多组解,则输出
Multiple sets of solutions。如果给定线性方程组无解,则输出
No solution。数据范围
1≤n≤1001≤n≤100
输入样例:
3 1 1 0 1 0 1 1 0 1 0 0 1输出样例:
1 0 0
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 110;
int n;
int a[N][N];
int gauss()
{
int c, r;
for (c = 0, r = 0; c < n; c ++ )
{
int t = r;
for (int i = r; i < n; i ++ )
if (a[i][c])
t = i;
if (!a[t][c]) continue;
for (int i = c; i <= n; i ++ ) swap(a[r][i], a[t][i]);
for (int i = r + 1; i < n; i ++ )
if (a[i][c])
for (int j = n; j >= c; j -- )
a[i][j] ^= a[r][j];
r ++ ;
}
if (r < n)
{
for (int i = r; i < n; i ++ )
if (a[i][n])
return 2;
return 1;
}
for (int i = n - 1; i >= 0; i -- )
for (int j = i + 1; j < n; j ++ )
a[i][n] ^= a[i][j] * a[j][n];
return 0;
}
int main()
{
cin >> n;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < n + 1; j ++ )
cin >> a[i][j];
int t = gauss();
if (t == 0)
{
for (int i = 0; i < n; i ++ ) cout << a[i][n] << endl;
}
else if (t == 1) puts("Multiple sets of solutions");
else puts("No solution");
return 0;
}3.求组合数 (求阶乘和阶乘的逆元)
给定 nn 组询问,每组询问给定两个整数 a,ba,b,请你输出 Cbamod(109+7)Cabmod(109+7) 的值。
输入格式
第一行包含整数 nn。
接下来 nn 行,每行包含一组 aa 和 bb。
输出格式
共 nn 行,每行输出一个询问的解。
数据范围
1≤n≤100001≤n≤10000,
1≤b≤a≤1051≤b≤a≤105输入样例:
3 3 1 5 3 2 2输出样例:
3 10 1
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 100010, mod = 1e9 + 7;
int fact[N], infact[N];
int qmi(int a, int k, int p)
{
int res = 1;
while (k)
{
if (k & 1) res = (LL)res * a % p;
a = (LL)a * a % p;
k >>= 1;
}
return res;
}
int main()
{
fact[0] = infact[0] = 1;
for (int i = 1; i < N; i ++ )
{
fact[i] = (LL)fact[i - 1] * i % mod;
infact[i] = (LL)infact[i - 1] * qmi(i, mod - 2, mod) % mod;
}
int n;
scanf("%d", &n);
while (n -- )
{
int a, b;
scanf("%d%d", &a, &b);
printf("%d\n", (LL)fact[a] * infact[b] % mod * infact[a - b] % mod);
}
return 0;
}
边栏推荐
- How much is the report development cost in the application system?
- Responsive dream weaving template home decoration building materials website
- Prometheus + alertmanager message alert
- 记一次 ERROR scheduler.AsyncEventQueue: Dropping event from queue shared导致OOM
- Summarize in the middle of the year | talk to yourself, live in the present, and count every step
- ResNet50+k折交叉验证+数据增强+画图(准确率、召回率、F值)
- 即时通讯场景下安全合规的实践和经验
- Responsive dream weaving template outdoor camping website
- Click back to the top JS
- QT source code analysis -- QObject (4)
猜你喜欢

"Activity recommendation" rush rush! 2022 international open source Festival has new content

Responsive dream weaving template outdoor camping website

关于高并发,我想聊一聊。

The problem of modifying the coordinate system of point cloud image loaded by ndtmatching function in autoware

Day 14: continued day 13 label related knowledge

结合Retrofit 改造OKHttp 缓存

聊聊 Feign 的实现原理

Try to understand the essence of low code platform design from another angle

Interprocess communication - detailed explanation of the pipeline (explanation of graphic cases)

外包公司“混”了2年,我只认真做了5件事,如今顺利拿到字节 Offer。
随机推荐
Three methods of STM32 delay function
Excel 打开包含汉字的 csv 文件出现乱码该怎么办?
awvs无法启动问题
当Synchronized遇到这玩意儿,有个大坑,要注意
“12306”的架构到底有多牛逼?
聊聊接口性能优化的11个小技巧
【RT学习笔记1】RT-Thread外设例程——控制Led灯闪烁
Click the button to slide to the specified position
向量相似度评估方法
Detailed explanation of IVX low code platform series -- Overview (II)
The financing demand of 129 million yuan was released, and the roadshow of the Dake city project continued to irrigate the "good seedlings" of scientific innovation
字符流综合练习解题过程
关于高并发,我想聊一聊。
Data security and privacy computing summit - development and application of security intersection in privacy Computing: Learning
ES6事件绑定(v-on用法)
virsh console连接失败问题
Navigation -- realize data transmission and data sharing between fragments
Realization of digital tube display based on C51
ES6 syntax extension
[upload picture 2-cropable]
