poj 1365 Prime Land

题意:给定你一个以质数为底表示的数;问你这个数减1,之后用质数为底的表示法。

例如:

17 1  = 17^1 =17 - 1;
5 1 2 1 = 5^1*2^1=10 - 1;

2 4 = 2^4 = 16
3 2 = 3^2= 9;

View Code
#include<iostream>

#include<cstdio>

#include<cstdlib>

#include<algorithm>

#include<cmath>

#include<cstring>

using namespace std;

int prime[4024],cnt;

void Prime(  )

{

   bool hash[20000] = { 0 };

   int t = ( int )sqrt( 32768.0 ) + 1;  

   for( int i = 3 ; i <= t; i +=2 )

   {

       if( !hash[i>>1] ) 

       {

           int tt = i << 1;//  = i * 2;

           for( int j = i*i ; j <= 32768 ; j += tt )    

                hash[j>>1] = true;    

       }        

   }    

   cnt = 0;

   prime[cnt++] = 2;

   t = 32768/2;

   for( int i = 1 ; i <= t ; i ++ )

   {

        if( !hash[i] )

        {

           prime[cnt++] = ( i<<1 ) + 1;    

        }        

   }

}

int Input( char str[])

{

   int sum = 1,num[2]={0},s;

   int t = 0;

   int len = strlen( str );

   for( int i = 0; i < len ; i ++ )

   {

       if( str[i] ==' '  )

       {

           if( t==1 )

           {

               s = 1;

               for( int j = 0 ;j < num[1] ; j ++ )

               {

                   sum *= num[0];    

               }

               num[0] = num[1] = 0;

               t = 0;

          }

          else t++;

       }    

       else num[t] = num[t]*10 + str[i] - '0';

   }

   for( int j = 0 ;j < num[1] ; j ++ )

   {

       sum *= num[0];    

   }

   return sum;

}

void Solve( int num )

{

    int a = 0;

    for( int i = cnt - 1; i >= 0 ; i -- )

    {    

        if( num % prime[i] == 0 )

        {

           int t = 0; 

           while( num % prime[i] == 0 )

           {

                 num /= prime[i];

                 t++;

           }          

          if( a==0 )printf( "%d %d",prime[i] ,t );

          else printf( " %d %d",prime[i],t );

          a++;    

        }    

    }

    puts( "" );

}

int main( )

{

    char str[1024];

    Prime();

   while( 1 )

   {

       gets( str );

       if(  str[0] == '0' ) break;

       int t = Input( str ) ;   

       Solve( t - 1 );        

   }

   return 0;

}

 

你可能感兴趣的:(Prim)