package main import fmt github com skorobogatov input math big func ma

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"fmt"
"github.com/skorobogatov/input"
"math/big"
)
func main() {
n:= 0
fmt.Scanf("%d",&n)
a:=make([][]*big.Rat,0)
b:=make([]*big.Rat,n,n)
for i := 0; i < n; i++ {
mas := make([]*big.Rat, n, n)
a = append(a, mas)
}
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
var q int64
input.Scanf("%d", &q)
a[i][j] = big.NewRat(q,1)
}
var q int64
input.Scanf("%d", &q)
b[i] = big.NewRat(q,1)
}
fmt.Printf("vvod:\n%v\n%v\n",a,b)
//diagonalizewoutnul
for i:=0;i<n;i++ {
if a[i][i].Sign()==0 {
for j:=i+1;j<n;j++ {
if i!=j {
if a[j][i].Sign()!=0 /*&& a[i][j]!=0 */{
for k:=0;k<n;k++ {
temp:=a[j][k]
a[j][k]=a[i][k]
a[i][k]=temp
}
temp:=b[j]
b[j]=b[i]
b[i]=temp
break
}
}
}
}
}
fmt.Printf("bez nul na diag:\n%v\n%v\n",a,b)
//gauss
for k:=0;k<n;k++ {
for i:=k+1;i<n;i++ {
if a[k][k].Sign()==0 {
fmt.Printf("nosol")
return
}
m:=big.NewRat(1,1)
m=m.Quo(a[i][k],a[k][k])
for j:=k;j<n;j++ {
f:=big.NewRat(1,1)
a[i][j]=a[i][j].Sub(a[i][j],f.Mul(m,a[k][j]))
}
f:=big.NewRat(1,1)
b[i]=b[i].Sub(b[i],f.Mul(m,b[k]))
}
}
fmt.Printf("gauss:\n%v\n%v\n",a,b)
x:=make([]*big.Rat,n,n)
for i:=0;i<n;i++ {
x[i]=big.NewRat(0,1)
}
for i:=n-1;i>=0;i-- {
var s *big.Rat
s=big.NewRat(0,1)
for j:=i;j<n;j++ {
f:=big.NewRat(0,1)
s=s.Add(s,f.Mul(a[i][j],x[j]))
}
f:=big.NewRat(0,1)
//g:=big.NewRat(0,1)
//g=s
fmt.Printf("%v\n,%v\n",f.Sub(b[i],s.Neg(s)),a[i][i])
x[i]=x[i].Quo(f.Sub(b[i],s.Neg(s)),a[i][i])
fmt.Printf("%v\n,%v\n",a,b)
}
fmt.Printf("solutions:\n%v\n",x)
}