hoj 1069 PrimeLand

来源:互联网 发布:java set方法 编辑:程序博客网 时间:2024/05/01 14:57

还是把题粘过来:

Everybody in the Prime Land is using a prime base number system. In this system, each positive integer x is represented as follows: Let {pi}i=0 denote the increasing sequence of all prime numbers. We know that x > 1 can be represented in only one way in the form of product of powers of prime factors. This implies that there is an integer kx and uniquely determined integers ekx, ekx-1, ..., e1, e0, (ekx > 0), that x = p(ekx,kx)*p(ekx-1,kx-1)*...*p(e1,1)*p(e0,0). The sequence

(ekx, ekx-1, ... ,e1, e0)

is considered to be the representation of x in prime base number system.

It is really true that all numerical calculations in prime base number system can seem to us a little bit unusual, or even hard. In fact, the children in Prime Land learn to add to subtract numbers several years. On the other hand, multiplication and division is very simple.

Recently, somebody has returned from a holiday in the Computer Land where small smart things called computers have been used. It has turned out that they could be used to make addition and subtraction in prime base number system much easier. It has been decided to make an experiment and let a computer to do the operation ``minus one''.

Help people in the Prime Land and write a corresponding program.

For practical reasons we will write here the prime base representation as a sequence of such pi and ei from the prime base representation above for which ei > 0. We will keep decreasing order with regard to pi.


Input

The input consists of lines (at least one) each of which except the last contains prime base representation of just one positive integer greater than 2 and less or equal 32767. All numbers in the line are separated by one space. The last line contains number 0.


Output

The output contains one line for each but the last line of the input. If x is a positive integer contained in a line of the input, the line in the output will contain x - 1 in prime base representation. All numbers in the line are separated by one space. There is no line in the output corresponding to the last ``null'' line of the input.


Sample Input

17 15 1 2 1509 1 59 10


Sample Output

2 43 213 1 11 1 7 1 5 1 3 1 2 1

 

额,这道题,第一道关就是:题意。真的读不懂。。。。。

只有又seach in the Internet,了解题意是:每一对数,前一个是基数,后一个是幂,然后每对数相乘,取得的结果再减1,

即n = product -1 ;

然后对n分解质因数再按质因子从大到小排列输出。

了解题意后,遇到的问题有两个,一是怎么输入,感觉输入有点不好弄,

while(cin >> Num && Num != 0)     {          cin >> Pow ;           long long product = (long long)pow(Num,Pow) ;           while(cin >> Num >> Pow)               product*=(long long)pow(Num,Pow) ; 

这么弄只能输入第一对数,。。。,然后又“借鉴”了网上的,

while(getchar()!= '\n' && cin >> Num >> Pow)

这说明输入流如果不满足输入条件,输入应该是被ignore了。

第二个就是核心部分,所谓质因子分解。

原谅我的无知啊,竟然想到的是求出素数然后来分解。

额额。。。

不过sample都能过,提交上去却要超时,这个实在不知道怎么了。

然后。。。再次借鉴,其实就是挨个除,额,思维就是有差距。

最后贡献两次格式错误,终于AC,上午也完了。

附代码:

#include <iostream>#include <cmath>using namespace std ; int main(){     float Num ;      int Pow ;      while(cin >> Num && Num != 0)     {          cin >> Pow ;           long long product = (long long)pow(Num,Pow) ;           while(getchar()!= '\n' && cin >> Num >> Pow)               product*=(long long)pow(Num,Pow) ;           long long n = sum -1 ;           int factor[2][100] ;           memset(factor,0,sizeof(factor)) ;           int factorPos = 0 ;           int k = 2 ;           while(n != 1)          {              if(n%k == 0)              {                   if(factor[0][factorPos] != k && factor[0][factorPos]!= 0 )                   factorPos++ ;                    factor[0][factorPos] = k ;                    factor[1][factorPos]++ ;                    n/=k ;               }              else              {                   if(k != 2)                   k+=2 ;                    else                   k++ ;               }           }           for(int i = factorPos ; i >= 0 ; i--)          {               cout << factor[0][i] <<" " <<factor[1][i] ;               if(i != 0)               cout <<" " ;           }          cout <<endl ;      }     cout <<"" ; }