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&#39;s Side of Math(高斯消元)的更多相关文章

  1. UVA 10828 - Back to Kernighan-Ritchie(概率+高斯消元)

    UVA 10828 - Back to Kernighan-Ritchie 题目链接 题意:给图一个流程图,有结点的流程,每次进入下一个流程概率是均等的,有q次询问,求出每次询问结点的运行期望 思路: ...

  2. uva 1560 - Extended Lights Out(枚举 | 高斯消元)

    题目链接:uva 1560 - Extended Lights Out 题目大意:给定一个5∗6的矩阵,每一个位置上有一个灯和开关,初始矩阵表示灯的亮暗情况,假设按了这个位置的开关,将会导致周围包含自 ...

  3. UVA 11542 - Square(高斯消元)

    UVA 11542 - Square 题目链接 题意:给定一些数字.保证这些数字质因子不会超过500,求这些数字中选出几个,乘积为全然平方数,问有几种选法 思路:对每一个数字分解成质因子后.发现假设要 ...

  4. uva 10808 - Rational Resistors(基尔霍夫定律+高斯消元)

    题目链接:uva 10808 - Rational Resistors 题目大意:给出一个博阿含n个节点,m条导线的电阻网络,求节点a和b之间的等效电阻. 解题思路:基尔霍夫定律,不论什么一点的电流向 ...

  5. UVA 1564 - Widget Factory(高斯消元)

    UVA 1564 - Widget Factory 题目链接 题意:n种零件, 给定m个制作时间.每段时间制作k个零件,每种零件有一个制作时间,每段时间用Mon到Sun表示,求每一个零件的制作时间.还 ...

  6. UVA 1358 - Generator(dp+高斯消元+KMP)

    UVA 1358 - Generator option=com_onlinejudge&Itemid=8&page=show_problem&category=524& ...

  7. UVa 10828 Back to Kernighan-Ritchie 高斯消元+概率DP

    题目来源:UVa 10828 Back to Kernighan-Ritchie 题意:从1開始 每次等概率从一个点到和他相邻的点 有向 走到不能走停止 求停止时每一个点的期望 思路:写出方程消元 方 ...

  8. UVA 1563 - SETI (高斯消元+逆元)

    UVA 1563 - SETI option=com_onlinejudge&Itemid=8&page=show_problem&category=520&probl ...

  9. UVA 12849 Mother’s Jam Puzzle( 高斯消元 )

    题目: http://uva.onlinejudge.org/external/128/12849.pdf #include <bits/stdc++.h> using namespace ...

随机推荐

  1. lua 之 三木运算符

    在c语言中我三目运算符这么写: a?b:c 例如: max = a>b?a:b; 在lua中我们这么写 max = a>b and a or b 运行如下:

  2. CRegKey

    1.简介 CRegKey提供了对系统注册表的操作方法,通过CRegKey类,可以方便的打开注册表的某个分支或子键(CRegKey::Open),可以方便的修改一个键的键值(CRegKey::SetVa ...

  3. 单页vue路由router

    Vue.js + vue-router 可以很简单的实现单页应用. <router-link> 是一个组件,该组件用于设置一个导航链接,切换不同 HTML 内容. to 属性为目标地址, ...

  4. 模板—trie图

    做了某题之后发现trie的AC自动机太垃圾了,动不动就TLE,然后我就去学了trie图. #include<iostream> #include<cstdio> using n ...

  5. Delphi新注释

    标准请看帮助文件里的:XML Documentation Comments 个人常用 <summary></summary>:摘要 /// <summary> // ...

  6. php代码中注释的含义

    最近在梳理和优化手上的项目代码,这个项目已经走过好几任了,每一任的开发人员多多少少都有一些差异和各自的习惯,所以代码逻辑和写法上都有点[乱]. 在代码中,注释是一个非常重要的信息,更何况是接手其他人的 ...

  7. python各种推导式分析

    推导式comprehensions(又称解析式),是Python的一种独有特性.推导式是可以从一个数据序列构建另一个新的数据序列的结构体. 共有三种推导,在Python2和3中都有支持: 列表(lis ...

  8. JS实现标签页切换效果

    本文实例为大家分享了JS标签页切换的具体代码,供大家参考,具体内容如下   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 ...

  9. C#NumberFormatInfo类

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA0sAAAD2CAIAAACImosXAAAgAElEQVR4nOy9V3Nk13X+vTt3owPSJJ ...

  10. Spring核心技术(七)——Spring容器的扩展

    本文将讨论如何关于在Spring生命周期中扩展Spring中的Bean功能. 容器的扩展 通常来说,开发者不需要通过继承ApplicationContext来实现自己的子类扩展功能.但是Spring ...