四平方和(二分)

四平方和定理,又称为拉格朗日定理:

每个正整数都可以表示为至多 44 个正整数的平方和。

如果把 00 包括进去,就正好可以表示为 44 个数的平方和。

比如:

5=02+02+12+225=02+02+12+22
7=12+12+12+227=12+12+12+22

对于一个给定的正整数,可能存在多种平方和的表示法。

要求你对 44 个数排序:

0≤a≤b≤c≤d0≤a≤b≤c≤d

并对所有的可能表示法按 a,b,c,da,b,c,d 为联合主键升序排列,最后输出第一个表示法。

输入格式

输入一个正整数 NN。

输出格式

输出4个非负整数,按从小到大排序,中间用空格分开。

数据范围

0<N<5∗1060<N<5∗106

输入样例:

5

输出样例:

0 0 1 2

#include<iostream>
#include<cstring>
using namespace std;

const int N = 5e6 + 10;

int C[N], D[N];
int main()
{
    int n;
    cin >> n;
    memset(C, -1, sizeof C);
    for (int c = 0; c * c <= n; c++)
    {
        for (int d = c; d * d + c * c <= n; d++)
        {
            int s = c * c + d * d;
            if (C[s] == -1)
            {
                C[s] = c, D[s] = d;
            }
        }
    }
    for (int a = 0; a * a <= n; a++)
    {
        for (int b = a; b * b + a * a <= n; b++)
        {
            int s = n - a * a - b * b;
            if (C[s] != -1)
            {
                cout << a << ' ' << b << ' ' << C[s] << ' ' << D[s] << endl;
                return 0;
            }
        }
    }
    return 0;
}

我的至尊代码!!!(有些数据就是不行。。。)

#include<iostream>
#include<queue>
#include<cstring>
#include<math.h>
#include<algorithm>
using namespace std;

const int N = 5e6 + 10;
int m, n;
int arr[4];
int d[3010][4];

int main()
{
    cin >> m;
    n = m;
    int a = sqrt(n);
    int sum = 0;
    int b;
    int ans = 0;
    int ans1 = 0;
    for (int i = 0; i < 4; i++)
    {
        //cout << a << ' ' << n << endl;
        arr[i] = a;
        sum += a * a;
        b = a;
        a = sqrt(n - a * a);
        n = n - b * b;
        if (i == 3 && sum == m)
        {
            sort(arr, arr + 4);
            d[ans1][0] = arr[0], d[ans1][1] = arr[1], d[ans1][2] = arr[2], d[ans1][3] = arr[3];
            ans1++;
            memset(arr, 0, sizeof arr);
            i = 0;
            ans++;
            a = sqrt(m) - ans;
            n = m;
            sum = 0;
            if (a == 0)break;
        }
        if (i == 3 && sum != m )
        {
            memset(arr, 0, sizeof arr);
            i = 0;
            ans++;
            a = sqrt(m) - ans;
            n = m;
            sum = 0;
            if (a == 0)break;
        }
    }
    int qwe[4] = { d[0][0],d[0][1],d[0][2],d[0][3] };
    for (int i = 1; i < ans1; i++)
    {
        if (d[i][0] < qwe[0])
        {
            qwe[0] = d[i][0], qwe[1] = d[i][1], qwe[2] = d[i][2], qwe[3] = d[i][3];
        }
        else if (d[i][0] == qwe[0])
        {
            if (d[i][1] < qwe[1])
            {
                qwe[0] = d[i][0], qwe[1] = d[i][1], qwe[2] = d[i][2], qwe[3] = d[i][3];
            }
            else if (d[i][1] == qwe[1])
            {
                if (d[i][2] < qwe[2])
                {
                    qwe[0] = d[i][0], qwe[1] = d[i][1], qwe[2] = d[i][2], qwe[3] = d[i][3];
                }
            }
        }
    }
    for (int i = 0; i < 4; i++)
    {
        cout << qwe[i] << ' ';
    }
    /*for (int i = 0; i < ans1; i++)
    {
        cout << d[i][0]<<' ' << d[i][1] << ' ' << d[i][2] << ' ' << d[i][3] << endl;
    }*/
    cout << endl;
    return 0;
}
  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值