hdu2650
来源:互联网 发布:淘宝面膜便宜的原因 编辑:程序博客网 时间:2024/06/05 18:05
A math problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 233 Accepted Submission(s): 112
Problem Description
Dandelion's poor at math , recently her friend asks her a math problem. Now she wants you to help her. The problem is : give you a number a+b*j ,
j=√ -2,If this number can only be divided by 1 and itself or -1 and the negative of itself . Please print Yes ,else print No.
j=√ -2,If this number can only be divided by 1 and itself or -1 and the negative of itself . Please print Yes ,else print No.
Input
Each line contains two integer a and b (0<=a<=100000 , 0<b<=100000 ).
Output
Yes or No for each case.
Sample Input
5 13 4
Sample Output
NoYesHint 5+j can be divided into (1-j)*(1+2j).AC代码:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>const int Times=10;using namespace std;typedef long long LL;LL multi(LL a,LL b,LL m){ LL ans=0; while(b) { if(b&1) { ans=(ans+a)%m; b--; } b>>=1; a=(a+a)%m; } return ans;}LL quick_mod(LL a,LL b,LL m){ LL ans=1; a%=m; while(b) { if(b&1) { ans=multi(ans,a,m); b--; } b>>=1; a=multi(a,a,m); } return ans;}bool Miller_Rabin(LL n){ if(n==2) return true; if(n<2||!(n&1)) return false; LL a,m=n-1,x,y; int k=0; while((m&1)==0) { k++; m>>=1; } for(int i=0;i<Times;i++) { a=rand()%(n-1)+1; x=quick_mod(a,m,n); for(int j=0;j<k;j++) { y=multi(x,x,n); if(y==1&&x!=1&&x!=n-1) return false; x=y; } if(y!=1) return false; } return true;}int main(){ LL a,b; while(~scanf("%I64d%I64d",&a,&b)) { if(a==0) { if(b%4==3&&Miller_Rabin(b)) puts("Yes"); else puts("No"); } else { LL t=a*a+2*b*b; if(Miller_Rabin(t)) puts("Yes"); else puts("No"); } } return 0;}