质数分解问题,题意是给出一个数的指数分解形式,例如5 1 2 1.即x = 5 ^ 1 * 2 ^ 1 = 10,然后要求我们来求x - 1的质数的分解形式。输出使从大到小的输出
/*************************************************************************
> File Name: 516.cpp
> Author: Toy
> Mail: ycsgldy@163.com
> Created Time: 2013年06月02日 星期日 11时57分17秒
************************************************************************/
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <sstream>
#include <fstream>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <set>
using namespace std;
const int INF = 0x7fffffff;
typedef pair<int,int> II;
typedef vector<int> IV;
typedef vector<II> IIV;
typedef vector<bool> BV;
typedef long long i64;
typedef unsigned long long u64;
typedef unsigned int u32;
#define For(t,v,c) for(t::const_iterator v=c.begin(); v!=c.end(); ++v)
#define IsComp(n) (_c[n>>6]&(1<<((n>>1)&31)))
#define SetComp(n) _c[n>>6]|=(1<<((n>>1)&31))
const int MAXP = 46341; //sqrt(2^31)
const int SQRP = 216; //sqrt(MAX)
int _c[(MAXP>>6)+1];
IV primes;
IIV opt;
char ch[100];
int A[100];
void prime_sieve ( ) {
for ( int i = 3; i <= SQRP; i += 2 )
if ( !IsComp ( i ) ) for ( int j = i * i; j <= MAXP; j += i + i ) SetComp ( j );
primes.push_back ( 2 );
for ( int i = 3; i <= MAXP; i += 2 ) if ( !IsComp ( i ) ) primes.push_back ( i );
}
void prime_factorize ( int n, IIV &f ) {
int sn = sqrt ( n );
For ( IV, it, primes ) {
int prime = *it;
if ( prime > sn ) break; if ( n % prime ) continue;
int e = 0; for ( ; n % prime == 0; e++, n /= prime );
f.push_back ( II ( prime, e ) );
sn = sqrt ( n );
}
if ( n > 1 ) f.push_back ( II ( n, 1 ) );
}
int main ( ) {
prime_sieve();
while ( fgets ( ch, 100, stdin ) != NULL ) {
int len = strlen ( ch );
ch[--len] = '\0';
if ( ch[0] == '0' && ch[1] == '\0' ) break;
int tmp = 0, cnt = 0, num = 1;
for ( int i = 0; i < len; ++i ) {
if ( ch[i] != ' ' ) tmp = tmp * 10 + ch[i] - '0';
else A[cnt++] = tmp, tmp = 0;
}
A[cnt++] = tmp;
for ( int i = 0; i < cnt; i += 2 )
num *= pow ( A[i], A[i + 1] );
opt.clear();
prime_factorize ( --num, opt );
int tt = opt.size() - 1;
for ( int i = tt; i > 0; --i )
printf ( "%d %d ", opt[i].first, opt[i].second );
printf ( "%d %d\n", opt[0].first, opt[0].second );
}
return 0;
}