数学部分讲稿

[pdf-embedder url=”https://mhy12345.xyz/wp-content/uploads/securepdfs/2018/07/数学-讲义-v1.1.pdf” title=”数学 讲义 v1.1″]

[pdf-embedder url=”https://mhy12345.xyz/wp-content/uploads/securepdfs/2018/07/数学-习题.pdf” title=”数学 习题”]

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cassert>
using namespace std;
#define MOD 100000007

struct matrix {
		static const int maxn = 2;
		int n,m;
		int mat[maxn][maxn];
		matrix() {
				memset(mat,0,sizeof(mat));
		}
		void initEye(int n) {
				this->n = this->m = n;
				memset(mat,0,sizeof(mat));
				for (int i=0;i<n;i++)
						mat[i][i] = 1;
		}
		void initStart() {
				this->n = 2;
				this->m = 1;
				memset(mat,0,sizeof(mat));
				mat[0][0] = mat[1][0] = 1;
		}
		void initFib() {
				this->n = this->m = 2;
				mat[0][0] = 0;
				mat[1][0] = mat[0][1] = mat[1][1] = 1;
		}
		void print() {
				cout<<"MATRIX "<<n<<" "<<m<<endl;
				for (int i=0;i<n;i++) {
						for (int j=0;j<m;j++) {
								cout<<mat[i][j]<<"\t";
						}
						cout<<endl;
				}
		}
};

matrix operator* (const matrix& m1,const matrix& m2) {
		assert(m1.m == m2.n);
		matrix res;
		res.n = m1.n;
		res.m = m2.m;
		for (int i=0;i<m1.n;i++) {
				for (int j=0;j<m2.m;j++) {
						for (int k=0;k<m1.m;k++) {
								res.mat[i][j] = (res.mat[i][j] + (long long)m1.mat[i][k]*m2.mat[k][j])%MOD;
						}
				}
		}
		return res;
}
matrix pow_mod(matrix x,int y) {
		matrix res;
		res.initEye(2);
		while(y) {
				if (y&1) 
						res = res * x;
				x = x*x;
				y>>=1;
		}
		return res;
}

int main() {
		matrix x;
		matrix T;
		x.initStart();
		x.print();
		T.initFib();
		T.print();
		(pow_mod(T,60000000)*x).print();
		while (1);
		
}

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cassert>
#include<cmath>
using namespace std;
#define MOD 100000007
#define eps 1e-7

struct matrix {
	static const int maxn = 20;
	int n,m;
	double mat[maxn][maxn];
	matrix() {
		memset(mat,0,sizeof(mat));
	}
	void print() {
		cout<<"MATRIX "<<n<<" "<<m<<endl;
		for (int i=0;i<n;i++) {
			for (int j=0;j<m;j++) {
				cout<<mat[i][j]<<"\t";
			}
			cout<<endl;
		}
	}
	void random(int n) {
		this->n = n;
		this->m = n;
		for (int i=0;i<n;i++)
			for (int j=0;j<n;j++)
				mat[i][j] = rand()%100;
	}
	void initSquare() {
		this->n = 4;
		this->m = 4;
		memset(mat,0,sizeof(mat));
		mat[0][1] = mat[0][3] = 1;
		mat[1][0] = mat[1][2] = 1;
		mat[2][1] = mat[2][3] = 1;
		mat[3][0] = mat[3][2] = 1;
		mat[0][0] = mat[1][1] = mat[2][2] = mat[3][3] = -2;
		this->n --;//去一行
		this->m --;//去一列
	}
	double gauss() {
		double ans = 1;
		for (int i=0;i<n;i++) {
			int sid = -1;
			for (int j=i;j<n;j++)
				if (abs(mat[j][i]) > eps)
				{
					sid = j;
					break;
				}
			if (sid == -1)
				continue;
			if (sid != i) {
				for (int j=0;j<n;j++)
				{
					swap(mat[sid][j],mat[i][j]);
					ans = - ans;
				}
			}
			for (int j=i+1;j<n;j++) {
				double ratio = mat[j][i]/mat[i][i];
				for (int k=0;k<n;k++) {
					mat[j][k] -= mat[i][k] * ratio;
				}
			}
			//print();
		}
		for (int i=0;i<n;i++)
			ans *= mat[i][i];
		return abs(ans);
	}
};


int main() {
	srand(1);
	matrix T;
	//T.random(2);
	T.initSquare();
	T.print();
	double ans = T.gauss();
	T.print();
	cout<<ans<<endl;
}

更多课件在这里

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据