我们把集合:HDU 2650 A math problem (高斯整数环)_#include叫做高斯整数环,其中Z表示通常的整数环,而用HDU 2650 A math problem (高斯整数环)_ios_02表示复数域上的整数环。

 

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

 

 

范的定义:设HDU 2650 A math problem (高斯整数环)_#include_03HDU 2650 A math problem (高斯整数环)_#include_04,定义a的范为HDU 2650 A math problem (高斯整数环)_ide_05

 

HDU 2650 A math problem (高斯整数环)_ide_06,则

 

(1)HDU 2650 A math problem (高斯整数环)_ide_07为非负整数,并且HDU 2650 A math problem (高斯整数环)_ide_08

 

(2)HDU 2650 A math problem (高斯整数环)_i++_09

 

(3)若HDU 2650 A math problem (高斯整数环)_ios_10,则HDU 2650 A math problem (高斯整数环)_#include_11

 

 

 

逆的定义:设HDU 2650 A math problem (高斯整数环)_i++_12,如果存在HDU 2650 A math problem (高斯整数环)_素数分解_13,使得HDU 2650 A math problem (高斯整数环)_ide_14,则称HDU 2650 A math problem (高斯整数环)_i++_15HDU 2650 A math problem (高斯整数环)_ios_16中的乘法可逆元,简称可逆元,并且

HDU 2650 A math problem (高斯整数环)_ios_17叫做HDU 2650 A math problem (高斯整数环)_#include_18的逆。

 

高斯整数HDU 2650 A math problem (高斯整数环)_素数分解_19是可逆元的充要条件是:HDU 2650 A math problem (高斯整数环)_ide_20。    HDU 2650 A math problem (高斯整数环)_i++_21中只有4个可逆元,分别是:HDU 2650 A math problem (高斯整数环)_素数分解_22HDU 2650 A math problem (高斯整数环)_#include_23

 

 

定义:设HDU 2650 A math problem (高斯整数环)_i++_24HDU 2650 A math problem (高斯整数环)_ide_25是两个非零高斯整数,如果存在可逆元HDU 2650 A math problem (高斯整数环)_ios_26,使得HDU 2650 A math problem (高斯整数环)_#include_27,则称HDU 2650 A math problem (高斯整数环)_i++_28HDU 2650 A math problem (高斯整数环)_#include_29等价,并表示成HDU 2650 A math problem (高斯整数环)_ide_30,换句话说,

HDU 2650 A math problem (高斯整数环)_ios_31HDU 2650 A math problem (高斯整数环)_素数分解_32等价,是指HDU 2650 A math problem (高斯整数环)_i++_33HDU 2650 A math problem (高斯整数环)_ios_34HDU 2650 A math problem (高斯整数环)_ios_35或者HDU 2650 A math problem (高斯整数环)_i++_36

 

 

 

高斯素数

定义:设HDU 2650 A math problem (高斯整数环)_ide_37HDU 2650 A math problem (高斯整数环)_#include_38中的非零非可逆元,我们称HDU 2650 A math problem (高斯整数环)_ide_39为高斯素数,是指HDU 2650 A math problem (高斯整数环)_素数分解_40的每个因子或者为可逆元,或者是与HDU 2650 A math problem (高斯整数环)_素数分解_41等价的高斯整数。

 

引理:

(1)设HDU 2650 A math problem (高斯整数环)_ide_42为高斯整数,并且HDU 2650 A math problem (高斯整数环)_i++_43为素数,则HDU 2650 A math problem (高斯整数环)_#include_44必定为高斯素数。

(2)若HDU 2650 A math problem (高斯整数环)_ios_45为高斯素数,则其共轭元HDU 2650 A math problem (高斯整数环)_i++_46也是高斯素数。

 

 

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

 

高斯整数HDU 2650 A math problem (高斯整数环)_ide_47是素数当且仅当:

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

(2)a、b均不为零,而HDU 2650 A math problem (高斯整数环)_ios_48为素数;

 

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

 

题目:A math problem

 

题意:给出HDU 2650 A math problem (高斯整数环)_i++_49,其中HDU 2650 A math problem (高斯整数环)_#include_50,判断HDU 2650 A math problem (高斯整数环)_ios_51是否为高斯素数。

 

分析:其实就是上面的判断高斯素数的方法,但是注意一点,这里HDU 2650 A math problem (高斯整数环)_ide_52,而正常情况是HDU 2650 A math problem (高斯整数环)_#include_53,其实差不多一样,

只是把HDU 2650 A math problem (高斯整数环)_#include_54为素数这个条件改为:HDU 2650 A math problem (高斯整数环)_#include_55为素数即可,那么如果把题目描述改为HDU 2650 A math problem (高斯整数环)_ios_56呢?同样的道理只需把

判断条件改成HDU 2650 A math problem (高斯整数环)_ios_57为素数即可,由于HDU 2650 A math problem (高斯整数环)_素数分解_58很大,所以写个Miller_Rabin吧。。。


 

#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;
}


 

以下代码的功能是找一个整数的所有高斯整数的素因子。

 

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <math.h>

using namespace std;
const int N=100005;

int prime[N], p[N],k;
int pri[N],top;
int n;

struct point
{
    int a;
    int b;
    char oper;
}s[N];

int num;
//筛选素数

void isprime()
{
    k=0;
    int i,j;
    memset(prime,true,sizeof(prime));
    for(i=2;i<N;i++)
    {
        if(prime[i])
        {
            p[k++]=i;
            for(j=i+i;j<N;j+=i)
            {
                prime[j]=false;
            }
        }
    }
}

//素因子分解
void Divide(int n)
{
    int i;
    top=0;
    for(i=0; i<k; i++)
    {
        if(n%p[i]==0)
        {
            pri[top++]=p[i];
            n/=p[i];
            while(n%p[i]==0)
            {
                pri[top++]=p[i];
                n/=p[i];
            }
        }
    }
    if(n>1)
        pri[top++]=n;
}

//高斯素数分解
void Part(int prime)
{
    int i;
    if(prime==2)
    {
        s[num].a = 1;
        s[num].b = 1;
        s[num++].oper = '+';
        s[num].a = 1;
        s[num].b = 1;
        s[num++].oper = '-';
    }
    else if((prime - 1)%4==0)
    {
        for(i=1;;i++)
        {
            int u=int(sqrt(prime-i*i*1.0)+1e-5);
            if(u*u+i*i==prime)
            {
                s[num].a = i;
                s[num].b = u;
                s[num++].oper='+';
                s[num].a = i;
                s[num].b = u;
                s[num++].oper='-';
                break;
            }
        }
    }
    else
    {
        s[num].a=prime;
        s[num++].b=0;
    }
}

int cmp(const void *a, const void *b)
{
    point *c = (point *)a;
    point *d = (point *)b;
    if(c->a != d->a)
        return c->a - d->a;
    if(c->b != d->b)
        return c->b - d->b;
    return c->oper == '-' ? 1 : -1;
}

void Print(int key)
{
    printf("%d", s[key].a );
    if(s[key].b == 0)
        return;
    if(s[key].b == 1)
        printf("%cj", s[key].oper);
    else
        printf("%c%dj", s[key].oper, s[key].b);
}

int main()
{
    isprime();
    int i, cas=1;
    while(~scanf("%d", &n))
    {
        num = 0;
        Divide(n);
        for(i=0;i<top;i++)
            Part(pri[i]);
        qsort(s, num, sizeof(point), cmp);
        printf("Case #%d: ", cas++);
        Print(0);
        for(i=1; i<num; i++)
        {
            if(s[i].a==s[i-1].a
                    &&s[i].b==s[i-1].b
                    &&s[i].oper==s[i-1].oper)
                continue;
            if(i)
                printf(", ");
            Print(i);
        }
        puts("");
    }
    return 0;
}