HDU-5615(Jam's math problem)(方程求解)

HDU-5615-Jam's math problem(方程求解)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 870    Accepted Submission(s): 415


Problem Description
Jam has a math problem. He just learned factorization.
He is trying to factorize ax2+bx+c into the form of pqx2+(qk+mp)x+km=(px+k)(qx+m).
He could only solve the problem in which p,q,m,k are positive numbers.
Please help him determine whether the expression could be factorized with p,q,m,k being postive.
 

Input
The first line is a number T, means there are T(1T100) cases

Each case has one line,the line has 3 numbers a,b,c(1a,b,c100000000)
 

Output
You should output the "YES" or "NO".
 

Sample Input
2 1 6 5 1 6 4
 

Sample Output
YES NO
Hint
The first case turn $x^2+6*x+5$ into $(x+1)(x+5)$
 
感覺這道題有點坑,可能是我英語不太好吧,  p,q,m,k are positive numbers。 p,q,m,k 不應該是正數嗎?可是按正數處理,樣例都不滿足。最後看了別人的題解才知道,當作整數處理。
一元二次方程的解:x1=(-b-sqrt(b*b-4*a*c))/(2*a)、x2=(-b+sqrt(b*b-4*a*c))/(2*a),
剛開始面對這道題也不知道要如何求解,最後看了同學的題解恍然大悟,而且方法很簡單。
x1=(-1)*(k/p),x2=(-1)*(m/q).
即:(-1)*(k/p)=(-b-sqrt(b*b-4*a*c))/(2*a)(-1)*(m/q)=(-b+sqrt(b*b-4*a*c))/(2*a)
,因k、p、m、q是整數,(2*a)、-b也是整數只要sqrt(b*b-4*a*c)爲整數就滿足,輸出YES,否則輸出NO.
 
My  solution:

/*2016.3.15*/

#include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std;
int main()
{
	long long a,b,c,q,p,k,m,n,h;
	double j;
	scanf("%I64d",&n);
	while(n--)
	{
		scanf("%I64d%I64d%I64d",&a,&b,&c);
		j=b*b-4*a*c;
		//m=b*b-4*a*c;
		if(j>=0)
		{
			j=sqrt(j);//q=sqrt(m) 
			h=(int)j;//強制類型轉換 
			if(j-h)//if(q*q!=m) 
			printf("NO\n");
			else
			printf("YES\n");
		}
		else
		printf("NO\n");
	}	
	return 0;
 } 




發佈了144 篇原創文章 · 獲贊 10 · 訪問量 8萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章