Square Difference
Posted clb123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Square Difference相关的知识,希望对你有一定的参考价值。
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 (1≤t≤51≤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 (1≤b<a≤1011)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
4
6 5
16 13
61690850361 24777622630
34 33
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 62−52=36−25=1162−52=36−25=11, which is prime, so the answer is "YES".
In the second case, the area is 162−132=87162−132=87, which is divisible by 33.
In the third case, the area of the remaining piece is 616908503612−247776226302=3191830435068605713421616908503612−247776226302=3191830435068605713421. This number is not prime because 3191830435068605713421=36913227731⋅864684729913191830435068605713421=36913227731⋅86468472991.
In the last case, the area is 342−332=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的主要内容,如果未能解决你的问题,请参考以下文章
What's the difference between @Component, @Repository & @Service annotations in Spring?(代码片段
leetcode_1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold_[二维前缀和](代码片段
Topics in CS(difference between compile and interpret)
webstorms代码拉下来,代码没有改动,但是每个文件都变成蓝色 了,提交的时候显示没有改动No difference。