UVA 1397 - The Teacher's Side of Math

题目链接

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

思路:因为m*n最多20,全部最高项仅仅有20。然后能够把每一个此项拆分。之后得到n种不同无理数,每一项为0。就能够设系数为变元。构造方程进行高斯消元

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

代码:

  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cmath>
  4. #include <algorithm>
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8.  
  9. const int N = 25;
  10. const double eps = 1e-9;
  11.  
  12. ll a, m, b, n, C[N][N];
  13. int hash[N][N], tot;
  14.  
  15. double A[N][N];
  16.  
  17. void build() {
  18. memset(A, 0, sizeof(A));
  19. A[0][0] = 1;
  20. for (int i = 1; i <= tot; i++) {
  21. for (int j = 0; j <= i; j++) {
  22. int l = j, r = i - j;
  23. double tmp = C[i][l] * pow(a * 1.0, l / m) * pow(b * 1.0, r / n);
  24. l %= m; r %= n;
  25. A[hash[l][r]][i] += tmp;
  26. }
  27. }
  28. A[tot][tot] = 1;
  29. A[tot][tot + 1] = 1;
  30. tot++;
  31. }
  32.  
  33. void getC() {
  34. for (int i = 0; i <= 20; i++) {
  35. C[i][0] = C[i][i] = 1;
  36. for (int j = 1; j < i; j++)
  37. C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
  38. }
  39. }
  40.  
  41. void gethash() {
  42. tot = 0;
  43. for (int i = 0; i < m; i++) {
  44. for (int j = 0; j < n; j++) {
  45. hash[i][j] = tot++;
  46. }
  47. }
  48. }
  49.  
  50. void print(double x) {
  51. char s[100];
  52. sprintf(s, "%.0lf", x);
  53. if (strcmp(s, "-0") == 0) printf(" %s", s + 1);
  54. else printf(" %s", s);
  55. }
  56.  
  57. void gauss() {
  58. for (int i = 0; i < tot; i++) {
  59. int r = i;
  60. for (int j = i + 1; j < tot; j++) {
  61. if (fabs(A[j][i]) > fabs(A[r][i]))
  62. r = j;
  63. }
  64. if (fabs(A[r][i]) < eps) continue;
  65. for (int j = i; j <= tot; j++)
  66. swap(A[r][j], A[i][j]);
  67. for (int j = 0; j < tot; j++) {
  68. if (i == j) continue;
  69. if (fabs(A[j][i]) >= eps) {
  70. double tmp = A[j][i] / A[i][i];
  71. for (int k = i; k <= tot; k++)
  72. A[j][k] -= tmp * A[i][k];
  73. }
  74. }
  75. }
  76. printf("1");
  77. for (int i = tot - 2; i >= 0; i--)
  78. print(A[i][tot] / A[i][i]);
  79. printf("\n");
  80. }
  81.  
  82. int main() {
  83. getC();
  84. while (~scanf("%lld%lld%lld%lld", &a, &m, &b, &n)) {
  85. if (!a && !m && !b && !n) break;
  86. gethash();
  87. build();
  88. gauss();
  89. }
  90. return 0;
  91. }

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. 谷歌全屏脚本 start chrome.exe --kiosk http://www.baidu.com

    start chrome.exe --kiosk http://www.baidu.com

  2. java_lock锁

    lock锁是一个接口,jdk5.0新增的接口: 在线程中创建一个他的实现类对象Reentrantlock,默认为fals可以改为true,改为true后是有序的 把操作共享资源的代码放入try中,在t ...

  3. [Luogu] P1441 砝码称重

    题目描述 现有n个砝码,重量分别为a1,a2,a3,……,an,在去掉m个砝码后,问最多能称量出多少不同的重量(不包括0). 题目分析 因为读错题WAWA大哭. 先dfs枚举选的砝码,满足条件时进行d ...

  4. MySQL与MyBatis中的查询记录

    1.时间段查询 MySQL:select * from table where ctime >= CURDATE() and ctime <DATE_SUB(CURDATE(),INTER ...

  5. 【转】vfork 和 fork的区别

    fork()与vfock()都是创建一个进程,那他们有什么区别呢?总结有以下三点区别: 1.  fork  ():子进程拷贝父进程的数据段,代码段     vfork ( ):子进程与父进程共享数据段 ...

  6. 配置Mysql审计

    mysql-audit.json:Mysql审计日志 插件下载地址: https://bintray.com/mcafee/mysql-audit-plugin/release/1.1.4-725#f ...

  7. eclipse导入项目时报错不能运行问题的一个记录

    一直用学校的云桌面,但是还是有一些地方不是很方便,必须要校园网以及需要离线保存: 碰到的问题:重新安装和云桌面一样版本的jdk9.0.4,以及tomcat9.0.12,以及eclipse-oxygen ...

  8. 2018/08/23 cstring中memset()函数的运用

    好多东西其实以前已经查过了,然后当时理解的还行,可是过段时间没用有些又会忘记,然后又去找资料又查,浪费了不少的时间和精力,所以,我,曾国强,今天起,要好好做笔记了! 今天复习第一个知识点,为什么要叫复 ...

  9. springData Jpa 快速入门

    前言: 数据持久化的操作,一般都要由我们自己一步步的去编程实现,mybatis通过我们编写xml实现,hibernate也要配置对应的xml然后通过创建session执行crud操作.那么有没有这样一 ...

  10. Thinkphp5.0 的视图view的模板布局

    Thinkphp5.0 的视图view的模板布局 使用include,文件包含: <!-- 头部 --> <div class="header"> {inc ...