poj 3361 Gaussian Prime Factors 【高斯素数】

poj 3361 Gaussian Prime Factors 【高斯素数】

 

http://acm.pku.edu.cn/JudgeOnline/problem?id=3361

 

【题目大意】

求一个数的高斯素因子。

 

【提交情况】

Wa一次,没处理好输入输出。

 

【解题思路】

高斯素数有以下两个性质,a如果是高斯素数,那么需要存在x使得a=4x+3,a+bi如果是高斯素数,那么需要a^2+b^2是素数,所以这个题目只需要将输入进行素因数分解,如果得到的素数是4x+3的形式就保存,不是就用枚举,枚举方法为找到a,求得b看b是否是整数,找到a和b,保存a+bi和a-bi,最后自己写个排序函数进行排序即可。

 

【AC的代码】

#include <iostream>

#include <cstdio>

#include <algorithm>

#include <cmath>

using namespace std;

 

typedef long long llong;

 

struct complex

{

         llong a,b;

         char op;

}c[1100];

llong cp;

 

bool cmp(complex m,complex n)

{

         bool ret=0;

         if(m.a<n.a) ret=1;

         if(m.a==n.a&&m.b<n.b) ret=1;

         if(m.a==n.a&&m.b==n.b&&m.op=='+'&&n.op=='-') ret=1;

         return ret;

}

 

void getResult(llong p)

{

         llong i,j;

         if((p-3)%4)

         {

                   for(i=1;;i++)

                   {

                            j=llong(sqrt(double(p-i*i)));

                            if(i*i+j*j==p)

                            {

                                     c[cp].a=i,c[cp].b=j,c[cp++].op='+';

                                     c[cp].a=i;c[cp].b=j,c[cp++].op='-';

                                     break;

                            }

                   }

         }

         else c[cp].a=p,c[cp].b=0,c[cp++].op='*';

}

 

int main()

{

         llong in,num=0,i,j,k,tmpIn,isFir;

         while(scanf("%lld",&in)!=EOF)

         {

                   tmpIn=in; isFir=1; cp=0;

                   printf("Case #%lld: ",++num);

                   for(i=2;i*i<tmpIn;i++)

                   {

                            if(tmpIn%i==0)

                            {

                                     getResult(i);

                                     while(tmpIn%i==0) tmpIn/=i;

                            }

                   }

                   if(tmpIn!=1) getResult(tmpIn);

                   sort(c,c+cp,cmp);

                   for(i=0;i<cp;i++)

                   {

                            if(i) printf(", ");

                            if(!c[i].b) printf("%lld",c[i].a);

                            else if(c[i].b==1) printf("%lld%cj",c[i].a,c[i].op);

                            else printf("%lld%c%lldj",c[i].a,c[i].op,c[i].b);

                   }

                   printf("\n");

         }

         return 0;

}

posted on 2010-03-05 10:27  liugoodness  阅读(376)  评论(0)    收藏  举报

导航