HDU 2650 A math problem 高斯整数判定

题目链接:点击打开链接

转自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);}
}
时间: 2024-10-06 10:17:58

HDU 2650 A math problem 高斯整数判定的相关文章

HDU 6182 A Math Problem 水题

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=6182 题目描述: 输入N, 输出满足k^k <= N 的 k的个数 解题思路: 水水更健康 代码: #include <iostream> #include <cstdio> #include <string> #include <vector> #include <cstring> #include <iterator> #in

Hdu 1757A Simple Math Problem矩阵

构造个矩阵搞下就行了 a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 1  0   0   0  0   0   0  0   0  0 0  1   0   0  0   0   0  0   0  0 0  0   1   0  0   0   0  0   0  0 0  0   0   1  0   0   0  0   0  0 0  0   0   0  1   0   0  0   0  0 0  0   0   0  0   1   0  0   0  0 0  0

【HDU 5105】Math Problem

题意 f(x)=|ax3+bx2+cx+d| 求f(x)在L≤x≤R的最大值. 分析 参数有可能是0,注意分类讨论 1.当a=0时 b=0,f为一次函数(c≠0)或者常数函数(c=0),最大值点在区间端点. b≠0,f为二次函数,最大值点在区间端点或者x0=c/(2*b),当L≤x0≤R时,ans=max{f(L),f(R),f(x0)}. 2.当a≠0时,f为三次函数 最大值点在区间端点或者导函数的零点x1,x2. 注意x1,x2是否在[L,R]区间. 代码 #include<cstdio>

hdu 5615 Jam&#39;s math problem(十字相乘判定)

d. Jam有道数学题想向你请教一下,他刚刚学会因式分解比如说,x^2+6x+5=(x+1)(x+5) 就好像形如 ax^2+bx+c => pqx^2+(qk+mp)x+km=(px+k)(qx+m) 但是他很蠢,他只会做p,q,m,kp,q,m,k为正整数的题目 请你帮助他,问可不可以分解 题意就是问一个一元二次方程能不能进行十字相乘的分解? s. 官方题解:第一道题比较简单,可以说是简单的模拟题,我们考虑到a,b,c都是10^9??的,所以我们决定要把时间复杂度降下来, 对于每一个数,因为

hdu 5170 GTY&#39;s math problem(水,,数学,,)

题意: 给a,b,c,d. 比较a^b和c^d的大小 思路: 比较log(a^b)和log(c^d)的大小 代码: int a,b,c,d; int main(){ while(scanf("%d%d%d%d",&a,&b,&c,&d)!=EOF){ double x1 = b*log((double)a); double x2 = d*log((double)c); if(fabs(x1-x2)<eps){ puts("=")

hdu 1757 A Simple Math Problem (乘法矩阵)

A Simple Math Problem Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2441    Accepted Submission(s): 1415 Problem Description Lele now is thinking about a simple function f(x).If x < 10 f(x) =

HDU 5615 Jam&#39;s math problem

Jam's math problem Problem Description Jam has a math problem. He just learned factorization.He is trying to factorize ax^2+bx+c into the form of pqx^2+(qk+mp)x+km=(px+k)(qx+m).He could only solve the problem in which p,q,m,k are positive numbers.Ple

BestCoder Round #70 Jam&#39;s math problem(hdu 5615)

Problem Description Jam has a math problem. He just learned factorization. He is trying to factorize ax^2+bx+cax?2??+bx+c into the form of pqx^2+(qk+mp)x+km=(px+k)(qx+m)pqx?2??+(qk+mp)x+km=(px+k)(qx+m). He could only solve the problem in which p,q,m,

HDU 5055 Bob and math problem(构造)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5055 Problem Description Recently, Bob has been thinking about a math problem. There are N Digits, each digit is between 0 and 9. You need to use this N Digits to constitute an Integer. This Integer need