高斯消元法
来源:互联网 发布:中国数据分析行业峰会 编辑:程序博客网 时间:2024/05/16 05:06
Describe
对于一个n个变量的n个方程的式子,惊醒求解!
经过了…
变成了…
最后就是…
代码(普通高斯消元)
#include <iostream>#include <stdio.h>#include <algorithm>#include <cstring>#define ll long long#define N 205using namespace std;int n;double a[N][N],val[N],score[N];void Gossi(){ for(int i=1; i<=n; i++) // ²Ù×÷µÄµÚ¼¸¸ö Êý×Ö ±ÈÈç 1 2 3 4 ... for(int j=i+1; j<=n; j++) // ö¾ÙÏÂÃæµÄÁÐ i+1 i+2 i+3... { int loc=i+1; while(!a[i][i]&&loc<=n) { swap(a[i],a[loc]); loc++; } double res=a[i][i]/a[j][i]; for(int v=i+1; v<=n; v++) a[j][v]=(a[j][v]*res)-a[i][v]; score[j]=res*score[j]-score[i]; } val[n]=score[n]/a[n][n]; for(int i=n-1; i>=1; i--) { double res=0; for(int j=i+1; j<=n; j++) res+=val[j]*a[i][j]; val[i]=(score[i]-res)/a[i][i]; }}int main(){ scanf("%d",&n); for(int i=1; i<=n; i++) { for(int j=1; j<=n; j++) scanf("%lf",&a[i][j]); scanf("%lf",&score[i]); } Gossi(); for(int i=1; i<=n; i++) printf("%.2lf\n",val[i]); return 0;}
费马小定理(Gauss)
#include <bits/stdc++.h>#define LL long long#define int long longusing namespace std;const int m1 = (1e9 + 7);const int m2 = (1e9 + 9);const int maxn = 210;LL m,temp,a[maxn][maxn],b[maxn][maxn],A[maxn],B[maxn];int n; char s;LL po(LL x,int y,int mo){ if (y == 1) return x; LL res = po(x,y >> 1,mo); res = (res * res) % mo; if (y & 1) res = (res * x) % mo; return res;}signed main(){ scanf("%d",&n); for (int i = 1;i <= n;++i) { for (int j = 1;j <= n;++j) scanf("%lld",&a[i][j]),b[i][j]=a[i][j]; LL x1 = 0, x2 = 0; while ( (s = getchar())&&!isdigit(s)); while (isdigit(s)) { x1 = (x1 * 10 + s - '0')%m1; x2 = (x2 * 10 + s - '0')%m2; s = getchar(); } a[i][0]=m1 - x1; b[i][0]=m2 - x2; } for (int i = 1;i < n;++i) { LL x=po(a[i][i],m1-2,m1); a[i][0]=a[i][0]*x%m1; for (int j = i;j <= n;++j) a[i][j]=a[i][j]*x%m1; for (int j = i + 1;j <= n;++j) { x=a[j][i]; a[j][0] = (a[j][0]-x*a[i][0]%m1)%m1; for (int k = i;k <= n;++k) a[j][k]=(a[j][k]-x*a[i][k]%m1)%m1; } } for (int i = n;~i;--i) { A[i]=-a[i][0] % m1 * po(a[i][i],m1-2,m1)%m1; for (int j = 1;j < i;++j) a[j][0]=(a[j][0]+a[j][i]*A[i]%m1)%m1; } for (int i = 1;i < n;++i) { int x=po(b[i][i],m2-2,m2); b[i][0]=(b[i][0]*x + m2)%m2; for (int j = i;j <= n;++j) b[i][j]=(b[i][j]*x + m2)%m2; for (int j = i + 1;j <= n;++j){ x = b[j][i]; b[j][0] = (b[j][0]-x*b[i][0]%m2 + m2)%m2; for (int k = i;k <= n;++k) b[j][k]=(b[j][k] - x * b[i][k]%m2 + m2)%m2; } } for (int i = n;i > 0;--i){ B[i] = (-b[i][0])*po(b[i][i],m2-2,m2)%m2; for (int j=1;j<i;++j) b[j][0]=(b[j][0]+b[j][i]*B[i]%m2)%m2; } int x = (m1 + 1) / 2; m=1ll*m1*m2; for (int i=1;i<=n;++i) { temp=(A[i]*m2+B[i]*m1)%m; if (temp<0) temp+=m; printf("%lld\n",(temp*x-(LL)((double)temp/m*x)* m + m)%m); } return 0;}
阅读全文
1 0
- 高斯消元法
- 高斯消元法
- 高斯消元法
- 高斯消元法
- 高斯消元法
- 高斯消元法
- 高斯消元法
- 高斯消元法
- 高斯消元法
- 高斯消元法
- 高斯消元法
- 高斯消元法
- 高斯消元法
- 高斯消元法
- 高斯消元法
- 高斯消元法
- 高斯消元法
- 高斯消元法
- 160
- 通过IDEA创建最基础的SSH框架流程
- sql数据处理,各种条件语句
- 17.10.21B组题解
- maven通过maven repository添加依赖包
- 高斯消元法
- 【C++】构造函数 and 析构函数
- 构建高性能的ASP.NET应用(15)
- Android设计模式之——单例模式
- 《阿里巴巴JAVA开发手册》规约扫描插件,阿里大牛心血,JAVA学习者的福音
- Dagger2的一个简单Demo
- tkinter: 布局管理 (Layout management) 之 .grid()
- 如何把组织的基本构成单位连接起来
- [实例]餐厅订餐单单