UVA - 1415 Gauss Prime(高斯素数)
生活随笔
收集整理的這篇文章主要介紹了
UVA - 1415 Gauss Prime(高斯素数)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
題目鏈接:點擊查看
題目大意:給出一組a和b,判斷a+b*sqrt(-2)是不是高斯素數
題目分析:對于一般的復數a+b*i,我們判斷其為高斯整數的條件為a和b都為整數即可
而判斷其為高斯素數的條件是,它不能再分解為其他高斯整數的乘積(0,1,-1除外),因為0,-1和1不能被視為高斯素數,但在這個題目中sqrt(-2)是高斯素數
所以高斯素數有以下兩個性質:
現在回到這個題目上來,這個題目將sqrt(-1)變為了sqrt(-2),也就是說分為了兩種情況討論:
因為這個題目中的b保證不等于0了,所以就不用判斷這個條件了
實現代碼:用試除法模擬即可
#include<iostream> #include<cstdio> #include<string> #include<ctime> #include<cstring> #include<algorithm> #include<stack> #include<queue> #include<map> #include<sstream> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=15;bool is_prim(int x) {for(int i=2;i*i<=x;i++)if(x%i==0)return false;return true; } int main() { // freopen("input.txt","r",stdin);int w;cin>>w;while(w--){int a,b;scanf("%d%d",&a,&b);if(a==0){printf("No\n");continue;}if(is_prim(a*a+b*b*2))printf("Yes\n");elseprintf("No\n");}return 0; }?
總結
以上是生活随笔為你收集整理的UVA - 1415 Gauss Prime(高斯素数)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ZOJ - 3593 One Perso
- 下一篇: POJ - 1381 Secret Co