uva 1397 - The Teacher's Side of Math(高斯消元)
来源:互联网 发布:火车票选座位软件 编辑:程序博客网 时间:2024/06/04 18:17
题目链接:uva 1397 - The Teacher's Side of Math
题目大意:给出一个方程的解x=a1/m+b1/n,求原方程(给出系数即可)
解题思路:因为方程肯定等于0,所以对于各个系数ax/mby/n都会等于0,于是可以根据这个列出方程,注意最高项的系数始终为1.
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int maxc = 20;const int maxn = 40;typedef long long ll;typedef double Mat[maxn][maxn];ll A, M, B, N, C[maxc+5][maxc+5];Mat G;void getc (int n) { for (int i = 0; i <= n; i++) { C[i][0] = C[i][i] = 1; for (int j = 1; j < i; j++) C[i][j] = C[i-1][j-1] + C[i-1][j]; }}void init () { memset(G, 0, sizeof(G)); int n = N * M; for (int i = 0; i <= n; i++) { for (int j = 0; j <= i; j++) { int l = j, r = i - j; double tmp = C[i][l] * pow(A * 1.0, l / M) * pow(B * 1.0, r / N); l %= M; r %= N; G[l*N+r][i] += tmp; } } G[n][n] = 1; G[n][n+1] = 1;}void gauss_elimin (int n) { /* for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) printf("%lf ", G[i][j]); printf("\n"); } */ for (int i = 0; i < n; i++) { int r = i; for (int j = i + 1; j < n; j++) if (fabs(G[j][i]) > fabs(G[r][i])) r = j; if (r != i) { for (int j = 0; j <= n; j++) swap(G[r][j], G[i][j]); } if (fabs(G[i][i]) < 1e-9) continue; for (int k = i + 1; k < n; k++) { double f = G[k][i] / G[i][i]; for (int j = 0; j <= n; j++) G[k][j] -= G[i][j] * f; } } for (int i = n-1; i >= 0; i--) { for (int j = i+1; j < n; j++) G[i][n] -= G[j][j] * G[i][j]; G[i][i] = G[i][n] / G[i][i]; if (fabs(G[i][i]) < 1e-9) G[i][i] = 0; } printf("%.0f", G[n-1][n-1]); for (int i = n-2; i >= 0; i--) printf(" %.0f", G[i][i]); printf("\n");}int main () { getc(maxc); while (scanf("%lld%lld%lld%lld", &A, &M, &B, &N) == 4 && A + M + B + N) { init(); gauss_elimin (N * M + 1); } return 0;}
1 0
- UVA 1397 - The Teacher's Side of Math(高斯消元)
- uva 1397 - The Teacher's Side of Math(高斯消元)
- POJ 3526 The Teacher’s Side of Math 高斯消元
- POJ 3526 The Teacher’s Side of Math 已翻译
- uva 1489 - Math teacher's homework(数位dp)
- [POJ3986]Math teacher's homework && 数位DP
- 数位dp-hdu-3693-Math teacher's homework
- hdu 3693 Math teacher's homework(数位dp)
- HDU 3693 Math teacher's homework (数位DP?)
- POJ 3986 Math teacher's homework 已被翻译
- The Other Side of Diversity
- The near side of the pie.
- Running Multiple Versions of the Framework Side-by-Side
- uva 825 Walking on the Safe Side
- UVA 825 Walking on the Safe Side
- UVa 825 - Walking on the Safe Side
- uva 825 - Walking on the Safe Side
- UVa 825 - Walking on the Safe Side
- 杭电2189 悼念512汶川大地震遇难同胞——来生一起走
- struts2<s:textfield>value默认值的问题
- css3实现美化菜单设计
- VS2008中 string 类型转化成char* 时出现 “初始化”: 无法从“const char *”转换为“char *”
- hdu-oj 1286 找新朋友
- uva 1397 - The Teacher's Side of Math(高斯消元)
- UVa 11754 - Code Feat (中国剩余定理 枚举)
- 毕业两年有余
- BaseAdapter的使用
- (leetcode)Sort List
- 排序算法
- LayoutInflater作用及使用
- hdu 1466 计算直线的交点数 (简单的动态规划)
- 【ArcGis】ArcObjects SDK(AE)10.1在vs2012安装的方法