Find Integer - hdu6441 - 费马大定理+奇偶数列法则

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

思路:

输入n,

  1. 若n>2由费马大定理知无解。输出-1 -1,n=0时也无解,输出-1 -1
  2. 若n=1,则a+b=c,输出1 a+1
  3. 若n=2,则a^2+b^2=c^2,由奇偶数列法则得到:
  • a=2n+1 (n=1,2,3…)

{  b= n^2+(n+1)^2-1  
   c= n^2+(n+1)^2 

  • a=2n(n=1,2,3…)

{  b= n^2 -1 
   c= n^2+1 

证明:https://blog.csdn.net/m0_37579232/article/details/82083210

代码如下:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
using namespace std;
#define ll long long
int main(){
    int t;
	ll a,b,c,n;
    scanf("%d",&t);
    while(t--){
    	scanf("%lld%lld",&n,&a);
    	if(n>2||n==0)printf("-1 -1\n");
    	else if(n==1)printf("1 %d\n",a+1);
    	else {
    		ll m=a/2;
    		if(a%2){
    			b=m*m+(m+1)*(m+1)-1;
				c=m*m+(m+1)*(m+1);
			}
			else{
				b=m*m-1;
				c=m*m+1;
			} 
			printf("%lld %lld\n",b,c);
		}
	}
}

猜你喜欢

转载自blog.csdn.net/m0_37579232/article/details/82083241