码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 1397 - The Teacher's Side of Math(高斯消元)

时间:2014-08-09 23:21:45      阅读:485      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   os   io   for   ar   问题   

UVA 1397 - The Teacher‘s Side of Math

题目链接

题意:给定一个x=a1/m+b1/n,求原方程组

思路:由于m*n最多20,所有最高项只有20,然后可以把每个此项拆分,之后得到n种不同无理数,每一项为0,就可以设系数为变元,构造方程进行高斯消元

一开始用longlong爆了,换成分数写法也爆了,又不想改高精度,最后是机智的用了double型过的,不过用double精度问题,所以高斯消元的姿势要正确,并且最后输出要注意-0的情况

代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;

typedef long long ll;

const int N = 25;
const double eps = 1e-9;

ll a, m, b, n, C[N][N];
int hash[N][N], tot;

double A[N][N];

void build() {
    memset(A, 0, sizeof(A));
    A[0][0] = 1;
    for (int i = 1; i <= tot; 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;
	    A[hash[l][r]][i] += tmp;
	}
    }
    A[tot][tot] = 1;
    A[tot][tot + 1] = 1;
    tot++;
}

void getC() {
    for (int i = 0; i <= 20; 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 gethash() {
    tot = 0;
    for (int i = 0; i < m; i++) {
	for (int j = 0; j < n; j++) {
	    hash[i][j] = tot++;
	}
    }
}

void print(double x) {
    char s[100];
    sprintf(s, "%.0lf", x);
    if (strcmp(s, "-0") == 0) printf(" %s", s + 1);
    else printf(" %s", s);
}

void gauss() {
    for (int i = 0; i < tot; i++) {
	int r = i;
	for (int j = i + 1; j < tot; j++) {
	    if (fabs(A[j][i]) > fabs(A[r][i]))
		r = j;
	}
	if (fabs(A[r][i]) < eps) continue;
	for (int j = i; j <= tot; j++)
	    swap(A[r][j], A[i][j]);
	for (int j = 0; j < tot; j++) {
	    if (i == j) continue;
	    if (fabs(A[j][i]) >= eps) {
		double tmp = A[j][i] / A[i][i];
		for (int k = i; k <= tot; k++)
		    A[j][k] -= tmp * A[i][k];
	    }
	}
    }
    printf("1");
    for (int i = tot - 2; i >= 0; i--)
	print(A[i][tot] / A[i][i]);
    printf("\n");
}

int main() {
    getC();
    while (~scanf("%lld%lld%lld%lld", &a, &m, &b, &n)) {
	if (!a && !m && !b && !n) break;
	gethash();
	build();
	gauss();
    }
    return 0;
}


UVA 1397 - The Teacher's Side of Math(高斯消元),布布扣,bubuko.com

UVA 1397 - The Teacher's Side of Math(高斯消元)

标签:style   http   color   os   io   for   ar   问题   

原文地址:http://blog.csdn.net/accelerator_/article/details/38460727

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!