2. +
Problem
As the first step in algebra, students learn quadratic formulas and their factorization.
Often, the factorization is a severe burden for them. A large number of students
cannot master the factorization; such students cannot be aware of the elegance of
advanced algebra. It might be the case that the factorization increases the number
of people who hate mathematics.
Your job here is to write a program which helps students of an algebra course. Given
a quadratic formula, your program should report how the formula can be factorized
into two linear formulas. All coefficients of quadratic formulas and those of resultant
linear formulas are integers in this problem.
The coefficients a, b and c of a quadratic formula ax2 + bx + c are given. The values
of a, b and c are integers, and their absolute values do not exceed 10000. From
these values, your program is requested to find four integers p, q, r and s, such that
ax2 + bx + c = (px + q)(rx + s).
Since we are considering integer coefficients only, it is not always possible to
factorize a quadratic formula into linear formulas. If the factorization of the given
formula is impossible, your program should report that fact.
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1106
3. +
Problem
2次方程式の整数係数a, b, cが与えられる。これを因数分解し、
(px+q)(rx+s)としたときのp, q, r, sを答えよ。
但し、p, q, r, sは全て整数であること。また、0<p, 0<r, (r<p)or(p=r
and s<=q)を満たしていること。
そのような因数分解ができない場合はImpossibleと答えよ。
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1106
7. +
SourceCode
double D = b * b - 4 * a * c;
if (D < 0) {
return "Impossible";
} else if (D == 0) {
int A = 2 * a;
int B = -b;
long g = gcd(Math.abs(A), Math.abs(B));
A /= g;
B /= g;
return A + " " + (-B) + " " + A + " " + (-B);
} else {
int k;
for(k=1;k * k <= D;k++){
if (k * k == D){
break;
}
}
if (k * k > D) {
return "Impossible";
}
解の公式
解なし
最大公約数で割る
Dが平方数でない時、p,q,r,s
が整数になれないの
で”Impossible”
8. + int P = 2 * a;
int Q = -b + k;
int R = 2 * a;
int S = -b - k;
long g = gcd(Math.abs(P), Math.abs(Q));
P /= g;
Q /= g;
g = gcd(Math.abs(R), Math.abs(S));
R /= g;
S /= g;
Q = -Q;
S = -S;
if (P > R || (P == R && Q > S)) {
return P + " " + Q + " " + R + " " + S;
} else {
return R + " " + S + " " + P + " " + Q;
}
}
入れ替え作業