Ultraviolet A 684-Integral Determinant (determining factor)
Question connection: Ultraviolet A 684-Integral Determinant
The value of the determinant is given.
Solution: Convert the determinant into an upper triangle. The value is the product of the element on the diagonal line. Because it is an integer, the score is used.
# Include
# Include
# Include using namespace std; typedef long type; struct Fraction {type member; // molecule; type denominator; // denominator; Fraction (type member = 0, type denominator = 1); void operator = (type x) {this-> set (x, 1);} Fraction operator * (const Fraction & u ); fraction operator/(const Fraction & u); Fraction operator + (const Fraction & u); Fraction operator-(const Fraction & u); void set (type member, type Denominator) ;}; inline type gcd (type a, type B) {return B == 0? (A> 0? A:-a): gcd (B, a % B);} inline type lcm (type a, type B) {return a/gcd (a, B) * B ;} /* Code *//////////////////////////////////// /// // const int maxn = 105; typedef long ll; int N; Fraction A [maxn] [maxn];/* bool cmp (const Fraction & a, const Fraction & B) {ll p =. member * B. denominator; ll q =. denominator * B. member; if (p <0) p =-p; if (q <0) q =-q; return p> q;} */inline vo Id self_swap (Fraction & a, Fraction & B) {Fraction tmp = a; a = B; B = tmp;} ll solve () {int sign = 1; fraction ret = 1; for (int I = 0; I <N; I ++) {// printf ("% d! \ N ", I); int r = I; for (int j = I + 1; j <N; j ++) if (A [j] [I]. member) r = j; if (r! = I) {for (int j = 0; j <N; j ++) self_swap (A [I] [j], A [r] [j]); sign * =-1;} if (A [I] [I]. member = 0 | A [I] [I]. denominator = 0) return 0; for (int j = I + 1; j <N; j ++) {Fraction f = A [j] [I]/A [I] [I]; for (int k = N-1; k> = 0; k --) {A [j] [k] = A [j] [k]-(A [I] [k] * f );}} ret = ret * A [I] [I];}/* for (int I = 0; I <N; I ++) {for (int j = 0; j <N; j ++) printf ("% lld/% lld", A [I] [j]. member, A [I] [j]. denominator); printf ("\ n");} */if (ret. denominator <0) sign * =-1; return ret. member * sign;} int main () {while (scanf ("% d", & N) = 1 & N) {ll x; for (int I = 0; I <N; I ++) {for (int j = 0; j <N; j ++) {scanf ("% lld", & x ); A [I] [j] = x ;}} printf ("% lld \ n", solve () ;}printf ("* \ n"); return 0 ;} //////////////////////////////////////// //// // Fraction:: Fraction (type member, type denominator) {this-> set (member, denominator);} Fraction: operator * (const Fraction & u) {type tmp_p = gcd (member, u. denominator); type tmp_q = gcd (u. member, denominator); return Fraction (member/tmp_p) * (u. member/tmp_q), (denominator/tmp_q) * (u. denominator/tmp_p);} Fraction: operator/(const Fraction & u) {type tmp_p = gcd (member, u. member); type tmp_q = gcd (denominator, u. denominator); return Fraction (member/tmp_p) * (u. denominator/tmp_q), (denominator/tmp_q) * (u. member/tmp_p);} Fraction: operator + (const Fraction & u) {type tmp_l = lcm (denominator, u. denominator); return Fraction (tmp_l/denominator * member + tmp_l/u. denominator * u. member, tmp_l);} Fraction: operator-(const Fraction & u) {type tmp_l = lcm (denominator, u. denominator); return Fraction (tmp_l/denominator * member-tmp_l/u. denominator * u. member, tmp_l);} void Fraction: set (type member, type denominator) {if (denominator = 0) {denominator = 1; member = 0 ;} type tmp_d = gcd (member, denominator); this-> member = member/tmp_d; this-> denominator = denominator/tmp_d ;}