AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 问答
    • 吐槽
  • App
  • 登录/注册

AcWing 1221. 四平方和    原题链接    简单

作者: 作者的头像   AKG ,  2023-03-19 15:01:31 ,  所有人可见 ,  阅读 21


1


枚举一下a,b存入表里面,然后枚举c,d,查表看一看存不存在合法的a,b

(暴力枚举) $O(n)$

直接开个pair就可以做到o(1)查询,o(1)存入

时间复杂度

两层循环最大分别为sqrt(n)
复杂度为o(n);

C++ 代码

#include<bits/stdc++.h>
using namespace std;
const int N = 5e6+5;
pair<int,int> t[N];
#define ft first
#define sd second
int main()
{
    for(int i = 0;i<=N;i++)t[i].ft=-1,t[i].sd=-1;
    int n;cin>>n;

    for(int a = 0;a*a<=n;a++)
    for(int b = a;a*a+b*b<=n;b++)
    {
        int s = a*a+b*b;
        if(t[s].ft==-1)t[s].ft = a,t[s].sd = b;
    }

    for(int c = 0;c*c<=n;c++)
    for(int d = c;c*c+d*d<=n;d++)
    {
        int s = n-c*c-d*d;
        if(t[s].ft!=-1)
        {
            cout<<c<<' '<<d<<' '<<t[s].ft<<' '<<t[s].sd;
            return 0;
        }
    }
}

0 评论

你确定删除吗?
1024
x

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息