HDU5615:Jam's math problem

本文介绍了一种解决特定形式的二次多项式因式分解问题的方法。提供了两种算法实现思路,一种是通过分解系数a和c的因子进行遍历匹配,另一种是利用判别式法判断是否存在正整数解。


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
 
方法一:

   分解a、c因子,然后遍历。数据比较小,不会超时。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<set>
#include<cmath>
#include<vector>
#define ll long long
#define MAXN 1000000
struct node{
    int m, k;
}km[MAXN];
struct node1{
    int p, q;
}pq[MAXN];
int main(){
    int t;
    int a, b, c, cnta, cntb, flag;
    scanf("%d", &t);
    while(t--){
        scanf("%d%d%d",&a, &b, &c);
        cnta= 0; cntb = flag = 0;
        for(int i = 1; i <= (int)sqrt(a); i ++){
            if(a%i==0) {
                pq[cnta].p = i;
                pq[cnta++].q = a/i;
            }
        }
        for(int i = 1; i <= (int)sqrt(c); i ++){
            if(c%i==0){
                km[cntb].k = i;
                km[cntb++].m = c/i;
            }
        }
        for(int i = 0; i < cnta; i++){
            int p, q;
            p = pq[i].p;
            q = pq[i].q;
            for(int j = 0; j < cntb; j++){
              int  k, m;
              k = km[j].k;
              m = km[j].m;
              if(p*m+q*k==b||p*k+q*m==b){
                  printf("YES\n");
                  flag = 1;
                  i = cnta; j = cntb;
              }
            }
        }
        if(flag==0) printf("NO\n");
    }
    return 0;
}

方法二:

      判别式法:x = (-b+cnt_1)/2*a,即(2*a*x+(b-cnt_1))(2*a*x+(b+cnt_1))==0

    下面问题就是  b-cnt_1 是否大于 0 ? 因为cnt_1*cnt_1==b*b-4*a*c;    a c不小于0 ,所以cnt_1 <= b 恒成立

.

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <cmath>
#include <iostream>
#define MAXN  1000000+10
#define ll long long
using namespace std;
int main(){
    ll t,a,b,c,cnt;
    scanf("%I64d",&t);
    while(t--){
        scanf("%I64d%I64d%I64d",&a,&b,&c);
        cnt = b*b - 4*a*c;
        if(cnt<0)  printf("NO\n");              //判别式小于0,方程无解
        else if(cnt==0)  printf("YES\n");       //判别式为0,方程有唯一解
        else{                                   
            int flag = 0;

            ll cnt_1 = sqrt(cnt);
            if(cnt_1*cnt_1==cnt) flag = 1;      //判别式可开方为整数

            if(flag == 0) printf("NO\n");
            else  printf("YES\n");

        }
    }
    return 0;
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值