PKU 1730

Perfect Pth Powers

Type:挺BT的数学题

解法:X = 2p1×3p2×5p3....(2,3,5...为素因子),求p1,p2,p3...的最大公约数

Key:由于题目说是"have magnitude(值) at least 2", "(32-bit)", 所以需要用64位整数,并且要考虑负数的情况,很多人都卡在这上面(是题目叙述不清,还是英文差?)。

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
#include < stdio.h >
#include
< string .h >
#define NL 150000

bool p0[NL];
int pnum[NL / 9 ], np;

void prim()
{
__int64 i, j;
memset(p0,
0 , sizeof (p0));
for (i = 2 ; i < NL / 2 ; i ++ ) {
if ( ! p0[i]) {
j
= i * i;
while (j < NL) {
p0[j]
= 1 ;
j
+= i;
}
}
}
np
= 0 ;
for (i = 2 ; i < NL; i ++ ) {
if ( ! p0[i]) pnum[np ++ ] = ( int )i;
}
}

int gcd( int a, int b)
{
while (b) {
int t = a;
a
= b;
b
= t % b;
}
return a;
}

int main()
{
prim();
int cnt[ 1000 ];
__int64 y;
while (scanf( " %I64d " , & y) != EOF ){
if ( ! y) break ;
int i = 0 ;
bool suc = false , pos = false ;
int fact, c = 0 ;
if (y < 0 ) { y = - y; pos = true ; }
__int64 t
= pnum[i];
while (t * t <= y) {
if (y % pnum[i] == 0 ) {
suc
= true ;
fact
= pnum[i];
cnt[c]
= 0 ;
while (y > 1 && y % fact == 0 ) {
y
/= fact;
cnt[c]
++ ;
}
c
++ ;
if (y == 1 ) {
break ;
}
}
i
++ ;
t
= pnum[i];
}
int gc = cnt[ 0 ];
for (i = 1 ; i < c; i ++ )
gc
= gcd(gc, cnt[i]);
if (suc) {
if ( ! pos) printf( " %d\n " , gc);
else {
while (gc > 1 && ! (gc & 1 )) gc /= 2 ;
printf(
" %d\n " , gc);
}
}
else printf( " 1\n " );
}
return 0 ;
}

 

转载于:https://www.cnblogs.com/superbin/archive/2010/06/15/1758697.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值