题目描述

Li Zhixiang have already been in “Friendship” ocean-going freighter for three months. The excitement has gradually disappeared. He stands on the board, holding the railing and watching the dazzling ocean in the sun silently. Day after day, the same scenery is monotonous and tasteless, even the merry seagulls following the freighter cannot arouse his interest. Hearing the footsteps behind, he turns back to see the old captain is coming towards him. The captain has understood his idea, however, he starts a new topic with the young man.

“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.”

输入

There are several test cases. In the first line of each case, a number n indicates that there are n equations. The following n lines, each line has n+1 numbers, ai1,ai2,ai3…..ain, bi(1<= i <=n), these numbers indicate the coefficients of systems of the equations. ai1*x1+ai2*x2+......ain*xn=bi. Input is terminated by the end of file.

输出

For each given systems of equations, if there are solutions, output n solutions in the order of appearance in the equations(n<=100),  each solution number is in one line. If solution is not integer, show it in fraction. If no solution, output “No solution.” Leave a blank line after each case.

样例输入

  1. 2
  2. 1000000000000000000000000 1000000000000000000000000 1000000000000000000000000
  3. -1000000000000000000000000 1000000000000000000000000 0
  4. 1
  5. 0 4

样例输出

  1. 1/2
  2. 1/2
  3.  
  4. No solution.
  1. 大数分数高斯消元
  1. import java.math.BigInteger;
  2. import java.util.Scanner;
  3.  
  4. class Number{
  5. BigInteger a,b;
  6. Number() {
  7. a=BigInteger.valueOf(1);
  8. b=BigInteger.valueOf(1);
  9. }
  10.  
  11. Number(BigInteger x,BigInteger y) {
  12. a=x;
  13. b=y;
  14. }
  15.  
  16. Number sub(Number x){
  17. Number c=new Number();
  18. c.b=b.multiply(x.b);
  19. c.a=a.multiply(x.b).subtract(x.a.multiply(b));
  20. BigInteger d=c.a.gcd(c.b);
  21. if (d.compareTo(BigInteger.valueOf(0))!=0){
  22. c.a=c.a.divide(d); c.b=c.b.divide(d);
  23. }
  24. return c;
  25. }
  26.  
  27. Number mul(Number x){
  28. Number c=new Number();
  29. c.b=b.multiply(x.b);
  30. c.a=a.multiply(x.a);
  31. BigInteger d=c.a.gcd(c.b);
  32. if (d.compareTo(BigInteger.valueOf(0))!=0){
  33. c.a=c.a.divide(d); c.b=c.b.divide(d);
  34. }
  35. return c;
  36. }
  37.  
  38. Number div(Number x) {
  39. Number c=new Number();
  40. c.b=b.multiply(x.a);
  41. c.a=a.multiply(x.b);
  42. BigInteger d=c.a.gcd(c.b);
  43. if (d.compareTo(BigInteger.valueOf(0))!=0){
  44. c.a=c.a.divide(d); c.b=c.b.divide(d);
  45. }
  46. return c;
  47. }
  48.  
  49. int com(Number x) {
  50. BigInteger p=a.multiply(x.b);
  51. BigInteger q=x.a.multiply(b);
  52. if (p.compareTo(BigInteger.valueOf(0))<0) p=p.multiply(BigInteger.valueOf(-1));
  53. if (q.compareTo(BigInteger.valueOf(0))<0) q=q.multiply(BigInteger.valueOf(-1));
  54.  
  55. return p.compareTo(q);
  56. }
  57. }
  58. public class Main {
  59.  
  60. public static boolean Guss(int n,Number a[][],Number b[]){
  61. int k=1,col=1;
  62. while (k<=n && col<=n) {
  63. int max_r=k;
  64. for (int i=k+1;i<=n;i++)
  65. if (a[i][col].com(a[max_r][col])>0)
  66. max_r=i;
  67. if (a[max_r][col].com(new Number(BigInteger.valueOf(0),BigInteger.valueOf(1)))==0) return false;
  68. if (k!=max_r) {
  69. for (int j=col;j<=n;j++) {
  70. Number tmp=a[k][j];
  71. a[k][j]=a[max_r][j];
  72. a[max_r][j]=tmp;
  73. }
  74. Number tmp=b[k]; b[k]=b[max_r]; b[max_r]=tmp;
  75. }
  76.  
  77. b[k]=b[k].div(a[k][col]);
  78. for (int j=col+1;j<=n;j++) a[k][j]=a[k][j].div(a[k][col]);
  79. a[k][col].a=BigInteger.valueOf(1);
  80. a[k][col].b=BigInteger.valueOf(1);
  81.  
  82. for (int i=1;i<=n;i++) {
  83. if (i!=k) {
  84. b[i]=b[i].sub(b[k].mul(a[i][col]));
  85. for (int j=col+1;j<=n;j++) a[i][j]=a[i][j].sub(a[k][j].mul(a[i][col]));
  86. a[i][col].a=BigInteger.valueOf(0);
  87. }
  88. }
  89. k++; col++;
  90. }
  91. return true;
  92. }
  93.  
  94. public static void main(String[] args) {
  95. Number a[][] = new Number[105][105];
  96. Number b[] = new Number[105];
  97.  
  98. for (int i=1;i<=100;i++) {
  99. for (int j=1;j<=100;j++) a[i][j]=new Number();
  100. b[i]=new Number();
  101. }
  102. int n;
  103. Scanner in = new Scanner(System.in);
  104. while (in.hasNext()) {
  105. n=in.nextInt();
  106. for (int i=1;i<=n;i++){
  107. for (int j=1;j<=n;j++){
  108. a[i][j].a = in.nextBigInteger();
  109. a[i][j].b = BigInteger.valueOf(1);
  110. }
  111. b[i].a=in.nextBigInteger();
  112. b[i].b=BigInteger.valueOf(1);
  113. }
  114.  
  115. if (Guss(n,a,b)==true) {
  116. for (int i=1;i<=n;i++) {
  117. BigInteger d=b[i].a.gcd(b[i].b);
  118. if (d.compareTo(BigInteger.valueOf(0))!=0){
  119. b[i].a=b[i].a.divide(d); b[i].b=b[i].b.divide(d);
  120. }
  121. // System.out.println(1+" "+b[i].b+" "+b[i].b.compareTo(BigInteger.valueOf(0)));
  122. if (b[i].b.compareTo(BigInteger.valueOf(0))<0){
  123. // System.out.println("*");
  124. b[i].b=b[i].b.multiply(BigInteger.valueOf(-1));
  125. b[i].a=b[i].a.multiply(BigInteger.valueOf(-1));
  126. }
  127. // System.out.println(2+" "+b[i].b+" "+b[i].b.compareTo(BigInteger.valueOf(0)));
  128. if (b[i].a.compareTo(BigInteger.valueOf(0))==0) b[i].b=BigInteger.valueOf(1);
  129. if (b[i].b.compareTo(BigInteger.valueOf(1))==0) System.out.println(b[i].a);
  130. else System.out.println(b[i].a+"/"+b[i].b);
  131. }
  132. } else System.out.println("No solution.");
  133.  
  134. System.out.println();
  135. }
  136. }
  137. }
  1.  

ICPC2008哈尔滨-E-Gauss Elimination的更多相关文章

  1. Gauss elimination Template

    Gauss elimination : #include <iostream> #include <cstdlib> #include <cstring> #inc ...

  2. 高斯消元法(Gauss Elimination)【超详解&模板】

    高斯消元法,是线性代数中的一个算法,可用来求解线性方程组,并可以求出矩阵的秩,以及求出可逆方阵的逆矩阵.高斯消元法的原理是:若用初等行变换将增广矩阵 化为 ,则AX = B与CX = D是同解方程组. ...

  3. HDU2449 Gauss Elimination 高斯消元 高精度 (C++ AC代码)

    原文链接https://www.cnblogs.com/zhouzhendong/p/HDU2449.html 题目传送门 - HDU2449 题意 高精度高斯消元. 输入 $n$ 个 $n$ 元方程 ...

  4. ICPC2008哈尔滨-A-Array Without Local Maximums

    题目描述 Ivan unexpectedly saw a present from one of his previous birthdays. It is array of n numbers fr ...

  5. LU分解(1)

    1/6 LU 分解          LU 分解可以写成A = LU,这里的L代表下三角矩阵,U代表上三角矩阵.对应的matlab代码如下: function[L, U] =zlu(A) % ZLU ...

  6. 线性代数-矩阵-【5】矩阵化简 C和C++实现

    点击这里可以跳转至 [1]矩阵汇总:http://www.cnblogs.com/HongYi-Liang/p/7287369.html [2]矩阵生成:http://www.cnblogs.com/ ...

  7. 线性代数-矩阵-【1】矩阵汇总 C和C++的实现

    矩阵的知识点之多足以写成一本线性代数. 在C++中,我们把矩阵封装成类.. 程序清单: Matrix.h//未完待续 #ifndef _MATRIX_H #define _MATRIX_H #incl ...

  8. 高斯消元 & 线性基【学习笔记】

    高斯消元 & 线性基 本来说不写了,但还是写点吧 [update 2017-02-18]现在发现真的有好多需要思考的地方,网上很多代码感觉都是错误的,虽然题目通过了 [update 2017- ...

  9. bingoyes' tiny dream

    Gauss Elimination bool Gauss(){ int now=1,nxt; double t; R(i,1,n){ //enumerate the column for(nxt=no ...

随机推荐

  1. 想实现网页滚动一定距离底部弹出div

    <script type="text/javascript"> $(window).scroll(function () { if ($(this).scrollTop ...

  2. WPFのStyle TargetType的不同写法

    一.隐式写法 <Style TargetType="TextBlock"> <Setter Property="/> </Style> ...

  3. Android---OS

    一.概述 基于Linux的OS,内置JVM 二.发展史 二.系统架构

  4. Vue.js(五)

    前后端交互概述与URL地址格式 JS中常见的异步调用: 定时任务 ajax 事件函数 接口调用方式: 原生ajax 基于jQuery的ajax fetch axios url 地址格式: 传统的url ...

  5. Python--字符编码、文字处理、函数

    了解字符编码的知识储备 我们日常用到的文本编辑器有nodepad++,pycharm,word等等,用他们存取文件的过程大致类似,需要知道打开编辑器就打开了启动了一个进程,是在内存中的,所以在编辑器编 ...

  6. Hibernate 单项多对1

    自己理解: 单向1对多. 一个客户可以发出多个订单.但是一个订单只属于一个客户. 写对象的时候.在多的那个类对象把1的作为自己的一个属性. 写配置文件 <many-to-one name=1的属 ...

  7. 小程序推送消息(Template)

    最近搞小程序模拟推送消息,才发现小程序推送消息接口准备下线. 请注意,小程序模板消息接口将于2020年1月10日下线,开发者可使用订阅消息功能 咱们现在有需求,所以不管下不下,完成再说. 一:”获取a ...

  8. shell脚本学习(1)入门

    1脚本语言和编译型语言的区别:编译型的要从源码转换成目标代码,多运行于底层.脚本语言有解释器读入程序代码, 转成内部形式再执行. 2脚本语言,写的时间快,一般有awk,pwel, python Rub ...

  9. 【PowerOJ1740&网络流24题】圆桌聚餐(最大流)

    题意: 来自n个不同国家的代表开会,每个国家代表数为ci 会场有m张圆桌,每张桌子可容纳mi人 不希望有同一个国家的代表在同一张桌子上就餐 设计一个合法方案 (n,m<=300) 思路:最大流, ...

  10. mysql全家桶(二)数据操作

    一.数据操作1.增#新增insert into 表名(字段列表) values(值列表);INSERT INTO table_name ( field1, field2,...fieldN ) VAL ...