2018CCPC - 网络赛 1004 Find Integer

博客围绕算法题“Find Integer”展开,题目要求根据费马大定理表达式,给定a和n求满足公式的b和c。题解指出n>2或n==0时输出“-1 -1”,n=1时输出第一组解,n=2时输出勾股数,还介绍了求勾股数的打表法思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Find Integer

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 0 Accepted Submission(s): 0Special Judge

Problem Description

people in USSS love math very much, and there is a famous math problem .
give you two integers n,a,you are required to find 2 integers b,c such that an+bn=cn.

Input

one line contains one integer T;(1≤T≤1000000)
next T lines contains two integers n,a;(0≤n≤1000,000,000,3≤a≤40000)

Output

print two integers b,c if b,c exits;(1≤b,c≤1000,000,000);
else print two integers -1 -1 instead.

Sample Input

1 2 3

Sample Output

4 5

题解

本题给出一个费马大定理的表达式 —— a n + b n = c n , ( a , b , c ) ∈ Z a^n+b^n=c^n,(a,b,c) \in Z an+bn=cn,(a,b,c)Z。 题目意思是给定该公式中的a和n,求满足公式的b和c并输出,若不存在满足公式的b和c,则输出"-1 -1"。所以当n>2或n==0时输出"-1 -1";当n=1时输出满足方程的第一组解:1 a+1;当n=2时输出勾股数。
求勾股数:这种前n项固定的情况一般可以采用打表法。三层循环肯定是不行的,这么大数据量肯定tle。 a 2 = c 2 − b 2 = ( c + b ) ( c − b ) a^2=c^2-b^2=(c+b)(c-b) a2=c2b2=(c+b)(cb)。令 x = c + b , y = c − b x=c+b,y=c-b x=c+b,y=cb,于是 a 2 = x y a^2=xy a2=xy。从x着手,当满足a2可以整除x时就可以求出符合条件的b,c。
代码如下:

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

struct node{
    ll b,c;
}gg[40007];

void init(){
    memset(gg, 0, sizeof(gg));
    for( ll i=3; i<=40000; i++ ){
        for( ll j=1; j<i; j++ ){
            if( i*i%j==0 ){   //若a^2可以整除(c+b),则此式成立
                ll x = j;
                ll y = i*i/j;
                if( (y-x)%2==0 ){   //若b为整数,则c也是整数
                    gg[i].c=(x+y)/2;
                    gg[i].b=(y-x)/2;
                    break;
                }
            }
        }
    }
}
int main()
{
    init();
    int t;
    scanf("%d", &t);
    while( t-- ){
        ll a, n;
        scanf("%lld%lld", &n, &a);
        if( n>2 || n==0 ){
            printf("-1 -1\n");
            continue;
        }
        else if( n==1 ){
            printf("1 %lld\n", a+1);
            continue;
        }
        else{
            if( gg[a].b ){
                printf("%lld %lld\n", gg[a].b, gg[a].c);
            }
            else
                printf("-1 -1\n");
        }

    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值