ICPC2008哈尔滨-E-Gauss Elimination
题目描述
“Do you know how far our voyage is?” The captain asks. Li Zhixiang feels ashamed because he can not answer. Then the captain says with a smile, “5050 miles. Do you still remember the story of 5050?” This time the young man really blushes. The old captain continues saying:” You definitely know the story of 5050. When the German mathematician, “the prince of mathematicians”, Gauss was 10 years old …” Young man remembers this story and goes on to tell, “ When Gauss was 10 years old, he could add a list of integers from 1 to 100 in a few seconds, which shocked the teachers.” The old captain adds, “Gauss has many other stories like this. When he entered the university at the age of 17, he was able to construct heptadecagon by compass and straightedge. His university teachers were also impressed by his ability. Not only could college graduate students fail to do it, but also they felt hard to understand Gauss’s constructing process.”
At this time, vice-captain greets the old captain. The old captain says to Li Zhixiang: “Come over to my office tonight, let’s continue the conversation.” It is still calm and tranquil in the evening. The freighter travels smoothly on the sea in the silver moonlight. The captain tells the young man the following words.
Among the mathematicians through the ages, there are three greatest mathematicians: Archimedes, Newton and Gauss. Most of Gauss’s mathematical achievements are difficult to understand. Nevertheless, there are some comparatively easy. For instance, when it comes to solving multivariate system of linear equations, there is a solution called “Gauss Elimination”. In the navigation business, many problems can be solved by “Gauss elimination”. If you are interested in it, I will show you a simple question. Try it.”
输入
输出
样例输入
2
1000000000000000000000000 1000000000000000000000000 1000000000000000000000000
-1000000000000000000000000 1000000000000000000000000 0
1
0 4
样例输出
1/2
1/2 No solution.
大数分数高斯消元
import java.math.BigInteger;
import java.util.Scanner; class Number{
BigInteger a,b;
Number() {
a=BigInteger.valueOf(1);
b=BigInteger.valueOf(1);
} Number(BigInteger x,BigInteger y) {
a=x;
b=y;
} Number sub(Number x){
Number c=new Number();
c.b=b.multiply(x.b);
c.a=a.multiply(x.b).subtract(x.a.multiply(b));
BigInteger d=c.a.gcd(c.b);
if (d.compareTo(BigInteger.valueOf(0))!=0){
c.a=c.a.divide(d); c.b=c.b.divide(d);
}
return c;
} Number mul(Number x){
Number c=new Number();
c.b=b.multiply(x.b);
c.a=a.multiply(x.a);
BigInteger d=c.a.gcd(c.b);
if (d.compareTo(BigInteger.valueOf(0))!=0){
c.a=c.a.divide(d); c.b=c.b.divide(d);
}
return c;
} Number div(Number x) {
Number c=new Number();
c.b=b.multiply(x.a);
c.a=a.multiply(x.b);
BigInteger d=c.a.gcd(c.b);
if (d.compareTo(BigInteger.valueOf(0))!=0){
c.a=c.a.divide(d); c.b=c.b.divide(d);
}
return c;
} int com(Number x) {
BigInteger p=a.multiply(x.b);
BigInteger q=x.a.multiply(b);
if (p.compareTo(BigInteger.valueOf(0))<0) p=p.multiply(BigInteger.valueOf(-1));
if (q.compareTo(BigInteger.valueOf(0))<0) q=q.multiply(BigInteger.valueOf(-1)); return p.compareTo(q);
}
}
public class Main { public static boolean Guss(int n,Number a[][],Number b[]){
int k=1,col=1;
while (k<=n && col<=n) {
int max_r=k;
for (int i=k+1;i<=n;i++)
if (a[i][col].com(a[max_r][col])>0)
max_r=i;
if (a[max_r][col].com(new Number(BigInteger.valueOf(0),BigInteger.valueOf(1)))==0) return false;
if (k!=max_r) {
for (int j=col;j<=n;j++) {
Number tmp=a[k][j];
a[k][j]=a[max_r][j];
a[max_r][j]=tmp;
}
Number tmp=b[k]; b[k]=b[max_r]; b[max_r]=tmp;
} b[k]=b[k].div(a[k][col]);
for (int j=col+1;j<=n;j++) a[k][j]=a[k][j].div(a[k][col]);
a[k][col].a=BigInteger.valueOf(1);
a[k][col].b=BigInteger.valueOf(1); for (int i=1;i<=n;i++) {
if (i!=k) {
b[i]=b[i].sub(b[k].mul(a[i][col]));
for (int j=col+1;j<=n;j++) a[i][j]=a[i][j].sub(a[k][j].mul(a[i][col]));
a[i][col].a=BigInteger.valueOf(0);
}
}
k++; col++;
}
return true;
} public static void main(String[] args) {
Number a[][] = new Number[105][105];
Number b[] = new Number[105]; for (int i=1;i<=100;i++) {
for (int j=1;j<=100;j++) a[i][j]=new Number();
b[i]=new Number();
}
int n;
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
n=in.nextInt();
for (int i=1;i<=n;i++){
for (int j=1;j<=n;j++){
a[i][j].a = in.nextBigInteger();
a[i][j].b = BigInteger.valueOf(1);
}
b[i].a=in.nextBigInteger();
b[i].b=BigInteger.valueOf(1);
} if (Guss(n,a,b)==true) {
for (int i=1;i<=n;i++) {
BigInteger d=b[i].a.gcd(b[i].b);
if (d.compareTo(BigInteger.valueOf(0))!=0){
b[i].a=b[i].a.divide(d); b[i].b=b[i].b.divide(d);
}
// System.out.println(1+" "+b[i].b+" "+b[i].b.compareTo(BigInteger.valueOf(0)));
if (b[i].b.compareTo(BigInteger.valueOf(0))<0){
// System.out.println("*");
b[i].b=b[i].b.multiply(BigInteger.valueOf(-1));
b[i].a=b[i].a.multiply(BigInteger.valueOf(-1));
}
// System.out.println(2+" "+b[i].b+" "+b[i].b.compareTo(BigInteger.valueOf(0)));
if (b[i].a.compareTo(BigInteger.valueOf(0))==0) b[i].b=BigInteger.valueOf(1);
if (b[i].b.compareTo(BigInteger.valueOf(1))==0) System.out.println(b[i].a);
else System.out.println(b[i].a+"/"+b[i].b);
}
} else System.out.println("No solution."); System.out.println();
}
}
}
ICPC2008哈尔滨-E-Gauss Elimination的更多相关文章
- Gauss elimination Template
Gauss elimination : #include <iostream> #include <cstdlib> #include <cstring> #inc ...
- 高斯消元法(Gauss Elimination)【超详解&模板】
高斯消元法,是线性代数中的一个算法,可用来求解线性方程组,并可以求出矩阵的秩,以及求出可逆方阵的逆矩阵.高斯消元法的原理是:若用初等行变换将增广矩阵 化为 ,则AX = B与CX = D是同解方程组. ...
- HDU2449 Gauss Elimination 高斯消元 高精度 (C++ AC代码)
原文链接https://www.cnblogs.com/zhouzhendong/p/HDU2449.html 题目传送门 - HDU2449 题意 高精度高斯消元. 输入 $n$ 个 $n$ 元方程 ...
- ICPC2008哈尔滨-A-Array Without Local Maximums
题目描述 Ivan unexpectedly saw a present from one of his previous birthdays. It is array of n numbers fr ...
- LU分解(1)
1/6 LU 分解 LU 分解可以写成A = LU,这里的L代表下三角矩阵,U代表上三角矩阵.对应的matlab代码如下: function[L, U] =zlu(A) % ZLU ...
- 线性代数-矩阵-【5】矩阵化简 C和C++实现
点击这里可以跳转至 [1]矩阵汇总:http://www.cnblogs.com/HongYi-Liang/p/7287369.html [2]矩阵生成:http://www.cnblogs.com/ ...
- 线性代数-矩阵-【1】矩阵汇总 C和C++的实现
矩阵的知识点之多足以写成一本线性代数. 在C++中,我们把矩阵封装成类.. 程序清单: Matrix.h//未完待续 #ifndef _MATRIX_H #define _MATRIX_H #incl ...
- 高斯消元 & 线性基【学习笔记】
高斯消元 & 线性基 本来说不写了,但还是写点吧 [update 2017-02-18]现在发现真的有好多需要思考的地方,网上很多代码感觉都是错误的,虽然题目通过了 [update 2017- ...
- bingoyes' tiny dream
Gauss Elimination bool Gauss(){ int now=1,nxt; double t; R(i,1,n){ //enumerate the column for(nxt=no ...
随机推荐
- 数据库并发及锁机制及Hibernate锁实现
数据库事务的定义 数据库事务(Database Transaction),是指作为单个逻辑工作单元执行的一系列操作.一个逻辑工作单元要成为事务,必须满足所谓的ACID(原子性.一致性.隔离性和持久性) ...
- Android No static field XXX of type I in class Lcom/XXX/R$id错
问题复现: 问题原因: 出现这样的情况,你先检查你的依赖工程(module)的对应布局layout/xxx.xml是否跟主项目的layout重名,你点开R文件的时候,你会发现你的布局发生了错乱,导致你 ...
- 循环结构select 语法
- private: CRITICAL_SECTION m_cs;
CRITICAL_SECTION m_cs; //临界区 私有数据成员 pop 数据结构 push 临界区 保护 2.类CCriticalSection的对象表示一个“临界区”,它是一个用于同 ...
- Solr是什么?
么是Solr?Solr是什么? 答:Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口. Solr是一个高性能,采用Java开发,基于Lucene的全文搜索服 ...
- 禁用ubuntu启用虚拟内存swap
一.不重启电脑,禁用启用swap,立刻生效 # 禁用命令 sudo swapoff -a # 启用命令 sudo swapon -a # 查看交换分区的状态 sudo free -m 二.重新启动电脑 ...
- OpenCV常用基本处理函数(1)读写
图像的基本操作 cv.imread() 读取图片 cv.imshow() 显示图片 cv2.imwrite() 保存图像 使用摄像头捕获实时图像 OpenCV 为这中应用提供了 ...
- 西电源ubuntu12
deb http://linux.xidian.edu.cn/mirrors/ubuntu/ precise main restricted universe multiverse #deb-src ...
- java基础学习笔记二(接口、super、this)
一.super 和 this的用法 主要解释一下引用构造函数的用法 super(参数):调用父类中的某一个构造函数(应该为构造函数中的第一条语句) this(参数):调用本类中另一种形式的构造函数(应 ...
- 【leetcode】993. Cousins in Binary Tree
题目如下: In a binary tree, the root node is at depth 0, and children of each depth k node are at depth ...