求<=N的平方数

考试的最后一题。。又是数学题。。。(又是宛如智障)
x2+y2=z2
可以有:
x=a2b2
y=2ab
z=a2+b2
然后直接预处理 = = 、
哎!;

#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int>PII;
typedef long long LL;

const int N=1e6+10;

struct asd{
    LL a,b,c;
}ans[N],q[N];
int ans_num;
LL num;
bool cmp(asd x,asd y)
{
    if(x.a == y.a){
        if(x.b == y.b) return x.c < y.c;
        return x.b < y.b;
    }
    return x.a < y.a;
}

void init(){
    LL x,y,z;
    ans_num = 0;
    for(LL i=2;i<=1000000;i++){
        for(LL j = 1;j < i;j++){
            x = i * i - j * j;
            y = 2LL * i * j;
            z = i * i + j * j;
            if(z > 1000000) break;
            if(__gcd(x, __gcd(y, x)) != 1) continue;
            if(x > y) swap(x, y);
            ans[ans_num].a = x;
            ans[ans_num].b = y;
            ans[ans_num].c = z;
            ans_num++;
        }
    }
    sort(ans, ans+ans_num,cmp);
}

LL n,Left, Right;

int main(){
    init();

    while(~scanf("%lld%lld%lld",&n,&Left,&Right)){
            num = 0;
            for(int i=0;i<ans_num;i++){
                if(ans[i].c <= n){
                    num++;
                    q[num] = ans[i];
                }
            }
            sort(q+1,q+num+1,cmp);
            if(Left>num){
                puts("0");
            }
            else{
                int cas=1;
                num = min(num, Right);
                printf("%d\n",num - Left + 1);
                for(int i=Left;i <= num;i++)
                    printf("Case: %d a=%lld,b=%lld,c=%lld\n",cas++,q[i].a,q[i].b,q[i].c);
            }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值