zoukankan      html  css  js  c++  java
  • CodeForces

    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

    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=11, which is prime, so the answer is “YES”.
    In the second case, the area is 162−132=87, which is divisible by 3.
    In the last case, the area is 342−332=67.

    题目大意:给出两个数,判断两个数的平方差是不是为素数。

    解题思路:这道题1e9,直接a平方-b平方肯定是不行的,我们可以把它化成完全平方的形式(a+b)x (a-b) ,但是这样判断也会超时,很显然这种方法也不行,我们就可以判断a-b是不是等于1,如果不等于1那么这个数一定不是素数,因为最后的数是(a+b)x (a-b) ,如果a-b不等于1就说明这个数有除了1和它本身之外的因子,肯定不行。如果a-b等于1则判断a+b是不是素数就很简单了。AC代码:

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cmath>
    using namespace std;
    using LL = long long;
    bool isprime(LL a)
    {
    	if(a==1)
    	  return false;
    	for(LL i=2;i<=sqrt(a);i++)
    	  if(a%i==0)
    	    return false;
    	return true;
    }
    int main()
    {
    	ios::sync_with_stdio(false);
    	int t;
    	cin>>t;
    	while(t--)
    	{
    		LL a,b;
    		cin>>a>>b;
    		if((a-b)==1)
    		{
    			LL s=a+b;
    		if(isprime(s))
    		  cout<<"YES"<<endl;
    		else  
    		  cout<<"NO"<<endl;
    		}
    		else
    		  cout<<"NO"<<endl;
    	}
    	//system("pause");
    	return 0;
    }
    
  • 相关阅读:
    volume 方式使用 Secret【转】
    查看 Secret【转】
    用 k8s 管理机密信息【转】
    MySQL 如何使用 PV 和 PVC?【转】
    【docker问题】Client.Timeout exceeded while awaiting headers
    PV 动态供给【转】
    回收 PV【转】
    NFS PersistentVolume【转】
    PV & PVC【转】
    IO流中的常见问题
  • 原文地址:https://www.cnblogs.com/Hayasaka/p/14294300.html
Copyright © 2011-2022 走看看