P2455蒟蒻20分代码求调

学术版

3blue1green @ 2024-11-29 20:15:27

想知道这个代码为什么会RE和TLE

思路就是高斯消元,但系数最后才变为整数

求各位神犇解答QAQ

代码如下:

#include <bits/stdc++.h>
using namespace std;

int n, matrix[55][55];
bool isjie = 1, wu[55];

int sgn(int x)
{
    if (x >= 0)
        return 1;
    else
        return -1;
}

void simplify(int a[])
{
    int gcd = abs(__gcd(a[1], a[2]));
    for (int i = 3; i <= n + 1; i++)
    {
        gcd = abs(__gcd(gcd, a[i]));
        if (gcd == 1)
            return;
    }
    gcd *= sgn(a[1]);
    for (int i = 1; i <= n + 1; i++)
    {
        a[i] /= gcd;
    }
}

void times(int a, int num) // 将a行乘num倍
{
    for (int i = 1; i <= n + 1; i++)
    {
        matrix[a][i] *= num;
    }
}

void add(int a, int b) // 把a行加到b行上
{
    for (int i = 1; i <= n + 1; i++)
    {
        matrix[b][i] += matrix[a][i];
    }
}

void swap(int a, int b) // 把a行和b行交换位置
{
    int temp;
    for (int i = 1; i <= n + 1; i++)
    {
        temp = matrix[a][i];
        matrix[a][i] = matrix[b][i];
        matrix[b][i] = temp;
    }
}

void print()
{
    double a, b;
    for (int i = 1; i <= n; i++)
    {
        a = matrix[i][n + 1];
        b = matrix[i][i];
        cout << "x" << i << "=";
        printf("%.2f\n", a / b);
    }
}

void solution()
{
    int temp[n + 2], tmp, gcd;
    for (int i = 1; i <= n; i++) // 化为倒三角形
    {
        while (matrix[i][i] == 0)
        {
            if (i == n)
            {
                isjie = 0;
                wu[i] = 1;
                break;
            }
            swap(i, i + 1);
        }
        if (!isjie)
            continue;
        for (int j = i + 1; j <= n; j++)
        {
            for (int k = 1; k <= n + 1; k++)
            {
                temp[k] = matrix[i][k];
            }
            times(i, -1 * matrix[j][i]);
            times(j, temp[i]);
            add(i, j);
            for (int k = 1; k <= n + 1; k++)
            {
                matrix[i][k] = temp[k];
            }
        }
    }
    if (!isjie)
    {
        for (int i = 1; i <= n; i++)
        {
            if (wu[i])
                if (matrix[i][n + 1])
                {
                    cout << "-1";
                    return;
                }
        }
        cout << "0";
        return;
    }
    for (int i = n; i > 0; i--) // 化出解
    {
        gcd = sgn(matrix[i][i]) * abs(__gcd(matrix[i][i], matrix[i][n + 1]));
        matrix[i][i] /= gcd;
        matrix[i][n + 1] /= gcd;
        for (int j = i - 1; j > 0; j--)
        {
            for (int k = 1; k <= n + 1; k++)
            {
                temp[k] = matrix[i][k];
            }
            times(i, -1 * matrix[j][i]);
            times(j, temp[i]);
            add(i, j);
            for (int k = 1; k <= n + 1; k++)
            {
                matrix[i][k] = temp[k];
            }
        }
    }
    print(); // 输出
}

int main()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n + 1; j++)
            cin >> matrix[i][j];
        simplify(matrix[i]);
    }
    solution();
    return 0;
}

|