程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU 2650 A math problem 高斯整數判定

HDU 2650 A math problem 高斯整數判定

編輯:C++入門知識

HDU 2650 A math problem 高斯整數判定


 

 

我們把集合:data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573040.jpg叫做高斯整數環,其中Z表示通常的整數環,而用data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573053.jpg表示復數域上的整數環。

 

那麼什麼是環呢?就是通過加減乘三種運算後,仍然能滿足本身性質的就叫做環。

 

 

范的定義:設data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573067.jpgdata-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573057.jpg,定義a的范為data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573058.jpg

 

data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573068.jpg,則

 

(1)data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573084.jpg為非負整數,並且data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573049.jpg

 

(2)data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573036.jpg

 

(3)若data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573171.jpg,則data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573138.jpg

 

 

 

逆的定義:設data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573106.jpg,如果存在data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573107.jpg,使得data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573128.jpg,則稱data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573138.jpgdata-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573176.jpg中的乘法可逆元,簡稱可逆元,並且

data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573141.jpg叫做data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573158.jpg的逆。

 

高斯整數data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573144.jpg是可逆元的充要條件是:data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573199.jpgdata-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573253.jpg中只有4個可逆元,分別是:data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573258.jpgdata-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573242.jpg

 

 

定義:設data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573268.jpgdata-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573283.jpg是兩個非零高斯整數,如果存在可逆元data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573284.jpg,使得data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573280.jpg,則稱data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573219.jpgdata-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573207.jpg等價,並表示成data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573223.jpg,換句話說,

data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573338.jpgdata-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573336.jpg等價,是指data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573345.jpgdata-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573377.jpgdata-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573358.jpg或者data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573323.jpg

 

 

 

高斯素數

定義:設data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573329.jpgdata-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573355.jpg中的非零非可逆元,我們稱data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573337.jpg為高斯素數,是指data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573387.jpg的每個因子或者為可逆元,或者是與data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573469.jpg等價的高斯整數。

 

引理:

(1)設data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573410.jpg為高斯整數,並且data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573435.jpg為素數,則data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573492.jpg必定為高斯素數。

(2)若data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573436.jpg為高斯素數,則其共轭元data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573424.jpg也是高斯素數。

 

 

如何判斷一個高斯整數是否屬於高斯素數呢?可以用下面的方法:

 

高斯整數data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573473.jpg是素數當且僅當:

(1)a、b中有一個是零,另一個數的絕對值是形如4n+3的素數;

(2)a、b均不為零,而data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573449.jpg為素數;

 

有了這個結論,那麼我們就可以很輕松的解決HDU2650題了。

 

題目:A math problem

 

題意:給出data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573456.jpg,其中data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573460.jpg,判斷data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573434.jpg是否為高斯素數。

 

分析:其實就是上面的判斷高斯素數的方法,但是注意一點,這裡data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573499.jpg,而正常情況是data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573564.jpg,其實差不多一樣,

只是把data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573562.jpg為素數這個條件改為:data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573585.jpg為素數即可,那麼如果把題目描述改為data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573554.jpg呢?同樣的道理只需把

判斷條件改成data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573579.jpg為素數即可,由於data-cke-saved-src=https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012112573558.jpg很大,所以寫個Miller_Rabin吧。。。


 

 

import java.text.DecimalFormat;
import java.util.ArrayDeque;
import java.io.BufferedReader;  
import java.io.InputStreamReader;  
import java.io.PrintWriter;  
import java.math.BigInteger;  
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Deque;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Random;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Queue;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Main{
	long multi(long a,long b,long m)  
	{  
	     long ans=0;  
	     while(b>0)  
	     {  
	         if((b&1)!=0)  
	         {  
	             ans=(ans+a)%m;  
	             b--;  
	         }  
	         b/=2;
	         a=(a+a)%m;  
	     }  
	     return ans;  
	}  
	  
	long quick_mod(long a,long b,long m)  
	{  
	     long ans=1;  
	     a%=m;  
	     while(b>0)  
	     {  
	         if((b&1)!=0)  
	         {  
	             ans=multi(ans,a,m);  
	             b--;  
	         }  
	         b/=2;  
	         a=multi(a,a,m);  
	     }  
	     return ans;  
	}  
	  
	boolean MillarRabin(long n)  
	{  
	    if(n==2) return true;  
	    if(n<2||0==(n&1)) return false;  
	    long a,m=n-1,x,y = 0;  
	    int k=0;  
	    while((m&1)==0)  
	    {  
	        k++;  
	        m/=2;  
	    }  
	    for(int i=0;i<10;i++)  
	    {  
	        a=abs(rand.nextLong())%(n-1)+1; 
	        x=quick_mod(a,m,n);  
	        for(int j=0;j> 1;
			if (A[mid] <= val) {
				l = mid + 1;
			} else {
				pos = mid;
				r = mid - 1;
			}
		}
		return pos;
	}

	int Pow(int x, int y) {
		int ans = 1;
		while (y > 0) {
			if ((y & 1) > 0)
				ans *= x;
			y >>= 1;
			x = x * x;
		}
		return ans;
	}
	double Pow(double x, int y) {
		double ans = 1;
		while (y > 0) {
			if ((y & 1) > 0)
				ans *= x;
			y >>= 1;
			x = x * x;
		}
		return ans;
	}
	int Pow_Mod(int x, int y, int mod) {
		int ans = 1;
		while (y > 0) {
			if ((y & 1) > 0)
				ans *= x;
			ans %= mod;
			y >>= 1;
			x = x * x;
			x %= mod;
		}
		return ans;
	}
	long Pow(long x, long y) {
		long ans = 1;
		while (y > 0) {
			if ((y & 1) > 0)
				ans *= x;
			y >>= 1;
			x = x * x;
		}
		return ans;
	}
	long Pow_Mod(long x, long y, long mod) {
		long ans = 1;
		while (y > 0) {
			if ((y & 1) > 0)
				ans *= x;
			ans %= mod;
			y >>= 1;
			x = x * x;
			x %= mod;
		}
		return ans;
	}

	int Gcd(int x, int y){
		if(x>y){int tmp = x; x = y; y = tmp;}
		while(x>0){
			y %= x;
			int tmp = x; x = y; y = tmp;
		}
		return y;
	}
	long Gcd(long x, long y){
		if(x>y){long tmp = x; x = y; y = tmp;}
		while(x>0){
			y %= x;
			long tmp = x; x = y; y = tmp;
		}
		return y;
	}
	int Lcm(int x, int y){
		return x/Gcd(x, y)*y;
	}
	long Lcm(long x, long y){
		return x/Gcd(x, y)*y;
	}
	int max(int x, int y) {
		return x > y ? x : y;
	}

	int min(int x, int y) {
		return x < y ? x : y;
	}

	double max(double x, double y) {
		return x > y ? x : y;
	}

	double min(double x, double y) {
		return x < y ? x : y;
	}

	long max(long x, long y) {
		return x > y ? x : y;
	}

	long min(long x, long y) {
		return x < y ? x : y;
	}

	int abs(int x) {
		return x > 0 ? x : -x;
	}

	double abs(double x) {
		return x > 0 ? x : -x;
	}

	long abs(long x) {
		return x > 0 ? x : -x;
	}

	boolean zero(double x) {
		return abs(x) < eps;
	}
	double sin(double x){return Math.sin(x);}
	double cos(double x){return Math.cos(x);}
	double tan(double x){return Math.tan(x);}
	double sqrt(double x){return Math.sqrt(x);}
}


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved