第四题怎么做,枚举因子一直过不了,大佬帮忙看看

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define int long long

void solve()
{
    int a,b,c;
    cin >> a >> b >>c;
    
    if(c == 0) {
        cout << a << " " << b << " " << 1 << " " << 0 << "\n";
        return;
    }
    
    
    int aa = abs(a);
    int bb = abs(b);
    int cc = abs(c);
    
    vector<int> pa;
//     if (a == 0) {
//         pa.push_bacK(0);
//     }
    for (int i=1;i*i<=aa;i++) {
        if (aa % i == 0) {
            pa.push_back(i);
            if (i * i != aa) {
                pa.push_back(aa/i);
            }
            
            if (a < 0) {
                pa.push_back(-i);
                if (i * i != aa) {
                    pa.push_back(-aa/i);
                }
            }
        }
    }

    vector<int> pc;
    
//     if (c == 0) {
//         pc.push_bacK(0);
//     }
    for (int i=1;i*i<=cc;i++) {
        if (cc % i == 0) {
            pc.push_back(i);
            
            if (i * i != cc) {
                pa.push_back(cc/i);
            }
            if (c < 0) {
                pc.push_back(-i);
                if (i * i != cc) {
                    pa.push_back(-cc/i);
                }
            }
        }
    }

    for (const auto &x : pa) {
        for (const auto &y : pc) {
            if (y * y * a + x * x * c == x * y * b) {
               cout << a / x << " " << c / y << " " << x << " " << y << "\n";
                return ;
            }
        }
    }
    
    cout << "NO\n";
}

signed main() {
    int _t;
    cin>>_t;
    while(_t--) {
        solve(); 
    }
}
全部评论
负数乘负数也可能是正数 1 1 -2 1
1 回复 分享
发布于 2024-07-07 21:18 北京
c等于0的时候 b又不一定等于a
1 回复 分享
发布于 2024-07-07 21:17 湖南
水神也要刷题咩?
点赞 回复 分享
发布于 2024-07-07 21:46 上海

相关推荐

评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务