SlideShare a Scribd company logo
1 of 8
+ 
AizuOnlineJudge1106 
Factorization of Quadratic Formula 
ICPC AOJ Meeting 2014/10/20
+ 
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
+ 
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
+ 
SourceCode 
http://ideone.com/l634AC
+ 
Solution 
 a,b,cを受け付けた後、D=b^2-4*a*cの判別式を使って、解を何 
個持つか判定する。 
 D<0のとき、この方程式は解を持たないので”Impossible”を出 
力して終了する。 
 D=0のとき、この方程式は解を一つ持つ。この方程式の解は- 
2/(2*a)である。
+ 
Solution 
 D>0のとき、この方程式は解を二つ持つ。ただし、問題文の制 
約条件に「p,q,r,sは全て整数であること」とあるので 
Math.sqrt(D)が整数である必要がある。 
 更に、問題文に「0<p, 0<r, (r<p)or(p=r and s<=q)を満たしてい 
ること」とあるので、満たしていない場合、(p,q)と(r,s)を入れ 
替えて表示する。
+ 
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”
+ 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; 
} 
} 
入れ替え作業

More Related Content

More from nullzine

第二回ミーティングスライド
第二回ミーティングスライド第二回ミーティングスライド
第二回ミーティングスライドnullzine
 

More from nullzine (13)

Meeting19
Meeting19Meeting19
Meeting19
 
Meeting18
Meeting18Meeting18
Meeting18
 
Meeting13
Meeting13Meeting13
Meeting13
 
Meeting12
Meeting12Meeting12
Meeting12
 
Meeting11
Meeting11Meeting11
Meeting11
 
Meeting10
Meeting10Meeting10
Meeting10
 
Meeting8
Meeting8Meeting8
Meeting8
 
Meeting7
Meeting7Meeting7
Meeting7
 
Meeting6
Meeting6Meeting6
Meeting6
 
Meeting5
Meeting5Meeting5
Meeting5
 
Meeting4
Meeting4Meeting4
Meeting4
 
第二回ミーティングスライド
第二回ミーティングスライド第二回ミーティングスライド
第二回ミーティングスライド
 
Meeting1
Meeting1Meeting1
Meeting1
 

Meeting9

  • 1. + AizuOnlineJudge1106 Factorization of Quadratic Formula ICPC AOJ Meeting 2014/10/20
  • 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
  • 5. + Solution  a,b,cを受け付けた後、D=b^2-4*a*cの判別式を使って、解を何 個持つか判定する。  D<0のとき、この方程式は解を持たないので”Impossible”を出 力して終了する。  D=0のとき、この方程式は解を一つ持つ。この方程式の解は- 2/(2*a)である。
  • 6. + Solution  D>0のとき、この方程式は解を二つ持つ。ただし、問題文の制 約条件に「p,q,r,sは全て整数であること」とあるので Math.sqrt(D)が整数である必要がある。  更に、問題文に「0<p, 0<r, (r<p)or(p=r and s<=q)を満たしてい ること」とあるので、満たしていない場合、(p,q)と(r,s)を入れ 替えて表示する。
  • 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; } } 入れ替え作業