题意不好理解啊..
以下摘自discuss
已知任意一个大于1的数可以表示成一些素数的乘积,即x=p1^e1*p2^e2……
pn^en (pi 为素数,ei 为对应素数的个数),现给你x的表示,要你求x-1的
表示。
例:输入:5 1 2 1 则x=5^1*2^1=10,所以x-1=9,9可以表示成:9=3^2
输出:3 2
思路:反复试除
#include < stdio.h >
#include < string .h >
#include < math.h >
const int MAXN = 33333 ;
int prime[MAXN],num;
bool is [MAXN];
int getPri(){
int i, j, k = 0 ;
int s, e = ( int )(sqrt(MAXN + 0.0 ) + 1 );
memset( is , 1 , sizeof ( is ));
prime[k ++ ] = 2 ; // 2是素数
is [ 0 ] = is [ 1 ] = false ;
for (i = 4 ; i < MAXN; i += 2 ) // 筛掉偶数
is [i] = false ;
for (i = 3 ; i < e; i += 2 ){ // i 奇数
if ( is [i]){
prime[k ++ ] = i;
for (s = i * 2 , j = i * i; j < MAXN; j += s)
is [j] = false ;
}
}
for (; i < MAXN; i += 2 )
if ( is [i])
prime[k ++ ] = i;
return k;
}
void solve( int x){
bool first = true ;
for ( int i = num - 1 ; i >= 0 ; -- i){
int cnt = 0 ;
while ( x % prime[i] == 0 ){
cnt ++ ;
x /= prime[i];
}
if ( cnt ){
if ( first ){
printf( " %d %d " ,prime[i], cnt);
first = false ;
}
else
printf( " %d %d " ,prime[i], cnt);
}
}
printf( " \n " );
}
int main(){
num = getPri();
int p, e, sum;
while ( true ){
sum = 1 ;
while ( true ){
scanf( " %d " , & p);
if (p == 0 )
return 0 ;
scanf( " %d " , & e);
sum *= ( int )pow(( double )p,e);
if (getchar() == ' \n ' )
break ;
}
// printf("sum:%d",sum);
solve(sum - 1 );
}
return 0 ;
}