码迷,mamicode.com
首页 > 其他好文 > 详细

Square Difference

时间:2019-10-28 21:21:27      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:wiki   min   graph   pictures   long   with   tps   str   lin   

Alice has a lovely piece of cloth. It has the shape of a square with a side of length aa centimeters. Bob also wants such piece of cloth. He would prefer a squarewith a side of length bb centimeters (where b<ab<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 tt (1t51≤t≤5) — the number of test cases.

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

Output

Print tt lines, where the ii-th line is the answer to the ii-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

Note

The figure below depicts the first test case. The blue part corresponds to the piece which belongs to Bob, and the red part is the piece that Alice keeps for herself. The area of the red part is 6252=3625=1162−52=36−25=11, which is prime, so the answer is "YES".

技术图片

In the second case, the area is 162132=87162−132=87, which is divisible by 33.

技术图片

In the third case, the area of the remaining piece is 616908503612247776226302=3191830435068605713421616908503612−247776226302=3191830435068605713421. This number is not prime because 3191830435068605713421=36913227731864684729913191830435068605713421=36913227731⋅86468472991.

In the last case, the area is 342332=67342−332=67.

 

思路:思维题

技术图片

 

所以要想a的平方减b的平方等于素数,只能是(a+b)为素数,(a-b)等于1才行。

(注:数据较大,全开ll)。

(忽略我cin cout跟scanf混着用(这个习惯不太好),??)

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int t;
    scanf("%d",&t);
    while(t--) {
        ll a,b;
        scanf("%lld%lld",&a,&b);
        ll sum = a+b;
        int ans = 1;
        for(ll i = 2; i <= sqrt(sum); i++) {
            if(sum%i == 0) {
                ans = 0;
                break;
            }
        } 
        if(ans == 1&&(a-b) == 1) {
            cout<<"YES"<<endl;
        }
        else {
            cout<<"NO"<<endl;
        }
    }
    return 0;;
}

  

 

Square Difference

标签:wiki   min   graph   pictures   long   with   tps   str   lin   

原文地址:https://www.cnblogs.com/clb123/p/11754657.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!