Square Difference CodeForces - 1033B

Alice has a lovely piece of cloth. It has the shape of a square with a side of length a centimeters. Bob also wants such piece of cloth. He would prefer a square with a side of length b centimeters (where b<a). Alice wanted to make Bob happy, so she cut the needed square out of the corner of her piece and gave it to Bob. Now she is left with an ugly L shaped cloth (see pictures below).

Alice would like to know whether the area of her cloth expressed in square centimeters is prime. Could you help her to determine it?

Input
The first line contains a number t (1≤t≤5) — the number of test cases.

Each of the next t lines describes the i-th test case. It contains two integers a and b (1≤b<a≤1011) — the side length of Alice’s square and the side length of the square that Bob wants.

Output
Print t lines, where the i-th line is the answer to the i-th test case. Print “YES” (without quotes) if the area of the remaining piece of cloth is prime, otherwise print “NO”.

You can print each letter in an arbitrary case (upper or lower).

Example
Input
4
6 5
16 13
61690850361 24777622630
34 33
Output
YES
NO
NO
YES

判断a2-b2是不是素数,看到数大先想的是欧拉筛。。。总是想暴力思维题。。。
思路:a2-b2=(a+b)(a-b),然后,素数的定义是是除了数字一和本身之外不能被其他任何的数字除尽,所以如果我们让a-b=1,a+b是素数,那么所求的a2-b2就可以输出YES。数大,记得用long long。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
bool isPrime(long long n) {
long long i;
if(n<2)
return false;
for(i = 2; i <= sqrt(n); i++) 
{
    if((n % i) == 0) 
    return false;
}
return true; 
}

int main()
{
int k;
scanf("%d",&k);
while(k--)
{
    int flag=0;
    long long a,b;
    scanf("%lld %lld",&a,&b);
    if((a-b)==1)
    {
        if(isPrime(a+b))
        flag=1;
    }
    if(flag)
    printf("YES\n");
    else
    {
        printf("NO\n");
    }   
}
return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值