HDU 6441 - Find Integer - [费马大定理][2018CCPC网络选拔赛第4题]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6441

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)

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 $a^n + b^n = c^n$.

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

题意:

给出 $n$ 和 $a$ (0≤n≤1e9,3≤a≤4e4),要求你给出 $b$ 和 $c$ 满足 $a^n + b^n = c^n$。

题解:

根据费马大定理,$n > 2$ 时 $a^n + b^n = c^n$ 没有整数解,所以只需要计算 $n = 0,1,2$ 这三种情况:

1、$n = 0$,任何的正整数 $b,c$ 都无法使等式成立。

2、$n = 1$,任意取。

3、$n = 2$,$a^2 = \left( {c + b} \right)\left( {c - b} \right)$,分两种情况讨论:

      若 $a$ 为奇数,则 $a^2$ 也为奇数,则取 $b = \frac{{a^2 - 1}}{2},c = \frac{{a^2 + 1}}{2}$;

      若 $a$ 为偶数,则 $a^2$ 必然是 $4$ 的倍数,则取 $b = \frac{{a^2 - 4}}{4},c = \frac{{a^2 + 4}}{4}$。

      

AC代码:

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

ll a,n;

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        scanf("%lld%d",&n,&a);

        if(n==0 || n>2) printf("-1 -1\n");
        if(n==1) printf("1 %lld\n",a+1);
        if(n==2)
        {
            if(a%2==1) printf("%lld %lld\n",(a*a-1)/2,(a*a+1)/2);
            else printf("%lld %lld\n",(a*a-4)/4,(a*a+4)/4);
        }
    }
}

原文地址:https://www.cnblogs.com/dilthey/p/9577477.html

时间: 2024-08-27 01:44:37

HDU 6441 - Find Integer - [费马大定理][2018CCPC网络选拔赛第4题]的相关文章

HDU 6447 - YJJ&#39;s Salesman - [树状数组优化DP][2018CCPC网络选拔赛第10题]

Problem DescriptionYJJ is a salesman who has traveled through western country. YJJ is always on journey. Either is he at the destination, or on the way to destination.One day, he is going to travel from city A to southeastern city B. Let us assume th

[HDU 6447][YJJ&#39;s Salesman][2018CCPC网络选拔赛 1010][离散化+线段树+DP]

链接: http://acm.hdu.edu.cn/showproblem.php?pid=6447 题意: 左上角(0,0),右下角(10^9,10^9)的网格,其中有n(1<=n<=10^5)个方格内有权值. 一次只能沿右,下,右下三个方向走一个格子,只有沿右下方向走到格子里才可以获得权值. 问从(0,0)到(10^9,10^9)的路径最大权值是多少. 思路: 网格路径权值问题,第一感考虑DP,x从上往下,y从左往右刷表,状态转移方程为dp[i][j]=max(dp[i-1][j],dp[

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha&#39;s staff(几何找规律)

Problem Description "You shall not pass!"After shouted out that,the Force Staff appered in CaoHaha's hand.As we all know,the Force Staff is a staff with infinity power.If you can use it skillful,it may help you to do whatever you want.But now,hi

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6154 CaoHaha&#39;s staff 思维

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6154 题意:在笛卡尔坐标系下,画一个面积至少为  n 的简单多边形,每次只能画一条边或者一个格子的对角线,问至少要画几条. 解法:如果一个斜着的矩形长宽分别是 a,b,那么它的面积是 2ab.最优解肯定是离 sqrt(n/2)很近的位置.想想 n=5 时答案为什么是7 然后在那个小范围内枚举一下就好了.我给一张做题时画的图 #include <bits/stdc++.h> using namesp

HDU 5833 Zhu and 772002(高斯消元)——2016中国大学生程序设计竞赛 - 网络选拔赛

传送门 Zhu and 772002 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 48    Accepted Submission(s): 16 Problem Description Zhu and 772002 are both good at math. One day, Zhu wants to test the abili

HDU 5832 A water problem(取模~)—— 2016中国大学生程序设计竞赛 - 网络选拔赛

传送门 A water problem Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 60    Accepted Submission(s): 37 Problem Description Two planets named Haha and Xixi in the universe and they were created wit

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6156 数位DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:如题. 解法:数位DP,暴力枚举进制之后,就转化成了求L,R区间的回文数的个数,这个直接做一个数位DP就好了.dp[jz][start][cur][state]表示jz进制下以start位起始到cur位状态为state(1表示已经回文,0表示没有回文)时回文数的个数. #include <bits/stdc++.h> using namespace std; typedef long

2018中国大学生程序设计竞赛 - 网络选拔赛 1010 YJJ&#39;s Salesman 【离散化+树状数组维护区间最大值】

题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6447 YJJ's Salesman Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 919    Accepted Submission(s): 290 Problem Description YJJ is a salesman who h

2016 ccpc 网络选拔赛 F. Robots

Robots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 60    Accepted Submission(s): 13 Problem Description QXJ has N robots on the plane, the i-th is at (xi,yi), numbereded 1 to N. Every robot