UVA 1397 - The Teacher's Side of Math(高斯消元)
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(高斯消元)的更多相关文章
- UVA 10828 - Back to Kernighan-Ritchie(概率+高斯消元)
UVA 10828 - Back to Kernighan-Ritchie 题目链接 题意:给图一个流程图,有结点的流程,每次进入下一个流程概率是均等的,有q次询问,求出每次询问结点的运行期望 思路: ...
- uva 1560 - Extended Lights Out(枚举 | 高斯消元)
题目链接:uva 1560 - Extended Lights Out 题目大意:给定一个5∗6的矩阵,每一个位置上有一个灯和开关,初始矩阵表示灯的亮暗情况,假设按了这个位置的开关,将会导致周围包含自 ...
- UVA 11542 - Square(高斯消元)
UVA 11542 - Square 题目链接 题意:给定一些数字.保证这些数字质因子不会超过500,求这些数字中选出几个,乘积为全然平方数,问有几种选法 思路:对每一个数字分解成质因子后.发现假设要 ...
- uva 10808 - Rational Resistors(基尔霍夫定律+高斯消元)
题目链接:uva 10808 - Rational Resistors 题目大意:给出一个博阿含n个节点,m条导线的电阻网络,求节点a和b之间的等效电阻. 解题思路:基尔霍夫定律,不论什么一点的电流向 ...
- UVA 1564 - Widget Factory(高斯消元)
UVA 1564 - Widget Factory 题目链接 题意:n种零件, 给定m个制作时间.每段时间制作k个零件,每种零件有一个制作时间,每段时间用Mon到Sun表示,求每一个零件的制作时间.还 ...
- UVA 1358 - Generator(dp+高斯消元+KMP)
UVA 1358 - Generator option=com_onlinejudge&Itemid=8&page=show_problem&category=524& ...
- UVa 10828 Back to Kernighan-Ritchie 高斯消元+概率DP
题目来源:UVa 10828 Back to Kernighan-Ritchie 题意:从1開始 每次等概率从一个点到和他相邻的点 有向 走到不能走停止 求停止时每一个点的期望 思路:写出方程消元 方 ...
- UVA 1563 - SETI (高斯消元+逆元)
UVA 1563 - SETI option=com_onlinejudge&Itemid=8&page=show_problem&category=520&probl ...
- UVA 12849 Mother’s Jam Puzzle( 高斯消元 )
题目: http://uva.onlinejudge.org/external/128/12849.pdf #include <bits/stdc++.h> using namespace ...
随机推荐
- 谷歌全屏脚本 start chrome.exe --kiosk http://www.baidu.com
start chrome.exe --kiosk http://www.baidu.com
- java_lock锁
lock锁是一个接口,jdk5.0新增的接口: 在线程中创建一个他的实现类对象Reentrantlock,默认为fals可以改为true,改为true后是有序的 把操作共享资源的代码放入try中,在t ...
- [Luogu] P1441 砝码称重
题目描述 现有n个砝码,重量分别为a1,a2,a3,……,an,在去掉m个砝码后,问最多能称量出多少不同的重量(不包括0). 题目分析 因为读错题WAWA大哭. 先dfs枚举选的砝码,满足条件时进行d ...
- MySQL与MyBatis中的查询记录
1.时间段查询 MySQL:select * from table where ctime >= CURDATE() and ctime <DATE_SUB(CURDATE(),INTER ...
- 【转】vfork 和 fork的区别
fork()与vfock()都是创建一个进程,那他们有什么区别呢?总结有以下三点区别: 1. fork ():子进程拷贝父进程的数据段,代码段 vfork ( ):子进程与父进程共享数据段 ...
- 配置Mysql审计
mysql-audit.json:Mysql审计日志 插件下载地址: https://bintray.com/mcafee/mysql-audit-plugin/release/1.1.4-725#f ...
- eclipse导入项目时报错不能运行问题的一个记录
一直用学校的云桌面,但是还是有一些地方不是很方便,必须要校园网以及需要离线保存: 碰到的问题:重新安装和云桌面一样版本的jdk9.0.4,以及tomcat9.0.12,以及eclipse-oxygen ...
- 2018/08/23 cstring中memset()函数的运用
好多东西其实以前已经查过了,然后当时理解的还行,可是过段时间没用有些又会忘记,然后又去找资料又查,浪费了不少的时间和精力,所以,我,曾国强,今天起,要好好做笔记了! 今天复习第一个知识点,为什么要叫复 ...
- springData Jpa 快速入门
前言: 数据持久化的操作,一般都要由我们自己一步步的去编程实现,mybatis通过我们编写xml实现,hibernate也要配置对应的xml然后通过创建session执行crud操作.那么有没有这样一 ...
- Thinkphp5.0 的视图view的模板布局
Thinkphp5.0 的视图view的模板布局 使用include,文件包含: <!-- 头部 --> <div class="header"> {inc ...