当前位置:网站首页>7/28 Gauss elimination to solve linear equations + Gauss elimination to solve XOR linear equations + find the combination number II
7/28 Gauss elimination to solve linear equations + Gauss elimination to solve XOR linear equations + find the combination number II
2022-07-29 02:25:00 【Salted egg_ dd】
1. Gauss elimination linear equations
Enter a include nn An equation nn A system of linear equations with unknown numbers .
The coefficients in the equations are real numbers .
Solving this system of equations .
The following figure shows an example that contains mm An equation nn An example of a system of linear equations with unknown numbers :
Input format
The first line contains integers nn.
Next nn That's ok , Each row contains n+1n+1 A real number , That represents an equation nn A coefficient and a constant to the right of the equal sign .
Output format
If a given system of linear equations has a unique solution , A total of nn That's ok , Among them the first ii Line output the ii The solution of an unknown number , Two decimal places are reserved for the result .
If there are countless solutions to a given system of linear equations , The output
Infinite group solutions.If a given system of linear equations has no solution , The output
No solution.Data range
1≤n≤1001≤n≤100,
All input coefficients and constants have two decimal places , The absolute values do not exceed 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() // Gauss elimination , The answer lies in a[i][n] in ,0 <= i < n
{
int c, r;
for (c = 0, r = 0; c < n; c ++ )
{
int t = r;
for (int i = r; i < n; i ++ ) // Find the row with the largest absolute value
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]); // Change the line with the largest absolute value to the top
for (int i = n; i >= c; i -- ) a[r][i] /= a[r][c]; // Change the first place of the current line to 1
for (int i = r + 1; i < n; i ++ ) // Use the current row to eliminate all the following columns into 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; // unsolvable
return 1; // There are infinite sets of solutions
}
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; // There is a unique solution
}
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; // Remove the output -0.00 The situation of
printf("%.2lf\n", a[i][n]);
}
}
return 0;
}2. Gauss elimination is used to solve XOR linear equations
Similar to linear equations , One line at a time is not 0 Of , Then XOR solution
Enter a include nn An equation nn A system of XOR linear equations with unknown numbers .
The coefficients and constants in the equations are 00 or 11, The value of each unknown is also 00 or 11.
Solving this system of equations .
Examples of XOR linear equations are as follows :
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]among
^Express exclusive or (XORXOR),M[i][j]M[i][j] It means the first one ii In this equation x[j]x[j] The coefficient of ,B[i]B[i] It's No ii The constant at the right end of an equation , All values are 00 or 11.Input format
The first line contains integers nn.
Next nn That's ok , Each row contains n+1n+1 It's an integer 00 or 11, That represents an equation nn A coefficient and a constant to the right of the equal sign .
Output format
If a given system of linear equations has a unique solution , A total of nn That's ok , Among them the first ii Line output the ii The solution of an unknown number .
If there are multiple solutions for a given system of linear equations , The output
Multiple sets of solutions.If a given system of linear equations has no solution , The output
No solution.Data range
1≤n≤1001≤n≤100
sample input :
3 1 1 0 1 0 1 1 0 1 0 0 1sample output :
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. Find the combination number ( Find factorials and inverse elements of factorials )
Given nn Group inquiry , Each group is given two integers a,ba,b, Please output Cbamod(109+7)Cabmod(109+7) Value .
Input format
The first line contains integers nn.
Next nn That's ok , Each line contains a set of aa and bb.
Output format
common nn That's ok , Each line outputs a solution to the query .
Data range
1≤n≤100001≤n≤10000,
1≤b≤a≤1051≤b≤a≤105sample input :
3 3 1 5 3 2 2sample output :
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;
}
边栏推荐
- 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
- Kubesphere-多节点安装
- Problems encountered in special flow & properties property set instances and Solutions
- STM32 DMA receives serial port data
- Full solution of 3D model format | including more than 70 kinds of RVT, 3ds, DWG, FBX, IFC, osgb, obj, etc
- Excel uses countif statistics
- What is scope and scope chain
- 千万不要把Request传递到异步线程里面,有坑
- ES2022 的 8 个实用的新功能
- 会议OA之会议通知
猜你喜欢
![[mqtt from introduction to improvement series | 09] Wireshark packet capturing and analyzing mqtt messages](/img/dd/c7ad9addb0d15611d996db87bf469f.png)
[mqtt from introduction to improvement series | 09] Wireshark packet capturing and analyzing mqtt messages

autoware中ndtmatching功能加载点云图坐标系修正的问题

Character flow comprehensive exercise problem solving process

Awvs cannot start problem

当Synchronized遇到这玩意儿,有个大坑,要注意

C language improvement (I)

裂开了,一次连接池参数导致的雪崩问题

特殊流&Properties属性集实例遇到的问题及解决方法

Keil5 open the engineering prompt not found device solution

What should I do if excel opens a CSV file containing Chinese characters and there is garbled code?
随机推荐
ResNet50+k折交叉验证+数据增强+画图(准确率、召回率、F值)
Jmeter之BeanShell生成MD5加密数据写入数据库
Experiment 2: Arduino's tricolor lamp experiment
MySQL之数据查询(多表查询)
基于C51控制蜂鸣器
In 2022, the official data of programming language ranking came, which was an eye opener
"Wei Lai Cup" 2022 Niuke summer multi school training camp 2, sign in question GJK
How to customize a new tab in Duoyu security browser?
"Activity recommendation" rush rush! 2022 international open source Festival has new content
Realization of digital tube display based on C51
Excel uses countif statistics
应用系统中的报表开发成本值多少?
QT source code analysis -- QObject (4)
Website Collection
“蔚来杯“2022牛客暑期多校训练营3,签到题CAJHF
关于高并发,我想聊一聊。
Work queue_ queue
如何利用 RPA 实现自动化获客?
ES6 syntax extension
Character flow comprehensive exercise problem solving process
