前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >AcWing 1221. 四平方和 (哈希 or 二分 )

AcWing 1221. 四平方和 (哈希 or 二分 )

作者头像
glm233
发布2021-03-23 11:58:32
3430
发布2021-03-23 11:58:32
举报
文章被收录于专栏:glm的全栈学习之路

思路:

这题其实很毒瘤了,因为正常中途相遇法 n方枚举然后哈希一下其实理论上可以过就是因为时间复杂度太高了, 后面还是写了二分才过,好迷啊

代码语言:javascript
复制
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;
const int N=5e6+10;
int n,cnt;
struct node{
    int val,x,y;
    bool operator<(const node& a)const {
        if(val==a.val)return x<a.x;
        return val<a.val;
    }
}p[N];
ostream & operator<<(ostream & os,node &a){
    cout<<a.val<<" "<<a.x<<" "<<a.y<<endl;
}
int main(){
    cin>>n;
    for(int c=0;c*c<=n;c++){
        for(int d=c;c*c+d*d<=n;d++){
            p[cnt++]={c*c+d*d,c,d};
        }
    }
    
    sort(p,p+cnt);
    for(int a=0;a*a<=n;a++){
        for(int b=a;b*b+a*a<=n;b++){
            int test=n-a*a-b*b;
            //cout<<test<<endl;
            int l=0,r=cnt-1;
            while(l<r){
                int mid=l+r+1>>1;
                if(p[mid].val<=test){
                    l=mid;
                }
                else r=mid-1;
            }
            if(p[l].val==test){
                while(p[l].val==test)l--;
                l++;
                printf("%d %d %d %d\n",a,b,p[l].x,p[l].y);
                exit(0);
            }
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021/03/20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档