HDU 2650 A math problem 高斯整数判定

来源:互联网 发布:oracle无法打开sql脚本 编辑:程序博客网 时间:2024/06/04 17:41

题目链接:点击打开链接

转自Acdreamers:点击打开链接

我们把集合:叫做高斯整数环,其中Z表示通常的整数环,而用表示复数域上的整数环。

 

那么什么是环呢?就是通过加减乘三种运算后,仍然能满足本身性质的就叫做环。

 

 

范的定义:设,定义a的范为

 

,则

 

(1)为非负整数,并且

 

(2)

 

(3)若,则

 

 

 

逆的定义:设,如果存在,使得,则称中的乘法可逆元,简称可逆元,并且

叫做的逆。

 

高斯整数是可逆元的充要条件是:。    中只有4个可逆元,分别是:

 

 

定义:设是两个非零高斯整数,如果存在可逆元,使得,则称等价,并表示成,换句话说,

等价,是指或者

 

 

 

高斯素数

定义:设中的非零非可逆元,我们称为高斯素数,是指的每个因子或者为可逆元,或者是与等价的高斯整数。

 

引理:

(1)设为高斯整数,并且为素数,则必定为高斯素数。

(2)若为高斯素数,则其共轭元也是高斯素数。

 

 

如何判断一个高斯整数是否属于高斯素数呢?可以用下面的方法:

 

高斯整数是素数当且仅当:

(1)a、b中有一个是零,另一个数的绝对值是形如4n+3的素数;

(2)a、b均不为零,而为素数;

 

有了这个结论,那么我们就可以很轻松的解决HDU2650题了。

 

题目:A math problem

 

题意:给出,其中,判断是否为高斯素数。

 

分析:其实就是上面的判断高斯素数的方法,但是注意一点,这里,而正常情况是,其实差不多一样,

只是把为素数这个条件改为:为素数即可,那么如果把题目描述改为呢?同样的道理只需把

判断条件改成为素数即可,由于很大,所以写个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<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;  }  Random rand;long a, b;void work() throws Exception{rand = new Random(100);while(cin.hasNext()){a = cin.nextLong(); b = cin.nextLong();if(a==0L){if((b%4L)==3L && MillarRabin(b))System.out.println("Yes");else System.out.println("No");}else {long tmp = a*a+2L*b*b;if(MillarRabin(tmp))System.out.println("Yes");else System.out.println("No");}}}    public static void main(String[] args) throws Exception{        Main wo = new Main();   // in = new BufferedReader(new InputStreamReader(System.in));        cin = new Scanner(System.in);    out = new PrintWriter(System.out);  //  in = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));  //  out = new PrintWriter(new File("output.txt"));        wo.work();        out.close();    }static int N = 3*100050;static int M = N*N * 10;DecimalFormat df=new DecimalFormat("0.0000");static long inf = 1000000000000L;static long inf64 = (long) 1e18*2;static double eps = 1e-8;static double Pi = Math.PI;static int mod = 2520 ;private String Next() throws Exception{    while (str == null || !str.hasMoreElements())        str = new StringTokenizer(in.readLine());    return str.nextToken();    }    private int Int() throws Exception{    return Integer.parseInt(Next());    }    private long Long() throws Exception{    return Long.parseLong(Next());    }    StringTokenizer str;    static Scanner cin;    static BufferedReader in;    static PrintWriter out;    /*class Edge{int from, to, nex;Edge(){}Edge(int from, int to, int nex){this.from = from;this.to = to;this.nex = nex;}}Edge[] edge = new Edge[M<<1];int[] head = new int[N];int edgenum;void init_edge(){for(int i = 0; i < N; i++)head[i] = -1; edgenum = 0;}void add(int u, int v){edge[edgenum] = new Edge(u, v, head[u]);head[u] = edgenum++;}/**/int upper_bound(int[] A, int l, int r, int val) {// upper_bound(A+l,A+r,val)-A;int pos = r;r--;while (l <= r) {int mid = (l + r) >> 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);}}


0 0
原创粉丝点击