#include <vector> #include <math.h> #include <string> #include <iostream> #include <sstream> #include <limits> #include <cstring> #include <array> //#define PA2016_TEST #ifdef PA2016_TEST #define ASSERT( condition ) if ( !( condition ) ) { __debugbreak(); } #define PAUSE_CONSOLE system( "pause" ); #define DEBUG_ONLY( line ) line; #else #define ASSERT( condition ) #define PAUSE_CONSOLE #define DEBUG_ONLY( line ) #endif typedef unsigned int uint; const uint MAX_ANSWER_LEN = 2000; typedef std::array< char, 200 > TProcessArray; TProcessArray g_toProcess; char g_answer[ MAX_ANSWER_LEN ] = { 0 }; struct SProcessedInfo { uint numOfOnes; uint numOfTwos; }; SProcessedInfo GenerateToProcessArray( uint num, TProcessArray& outProcessedArray, uint& outArraySize ) { outProcessedArray.fill( 0 ); uint numOfPlusone = 0; uint numOfTwos = 0; uint i = 0; while( num > 1 ) { if( num & 1 ) { num = num - 1; ++numOfPlusone; outProcessedArray[ i ] = 1; ++i; } num = num >> 1; ++numOfTwos; outProcessedArray[ i ] = 2; ++i; } outArraySize = i; SProcessedInfo info{ numOfPlusone, numOfTwos }; return info; } class Stream { public: Stream( char* stream, uint size ) : m_currStream( stream ) , m_orginalStream( stream ) , m_maxSize( size ) , m_currentSize( 0 ) { } void Push( const char oneChar ) { ASSERT( m_currentSize <= m_maxSize ); *m_currStream = oneChar; ++m_currStream; ++m_currentSize; } char* GetData() const { return m_orginalStream; } uint GetCurrSize() const { return m_currentSize; } private: char* m_currStream; char* m_orginalStream; uint m_currentSize; uint m_maxSize; }; const char* ProcessAnswer( const SProcessedInfo& info, const TProcessArray& outProcessedArray, uint arraySize ) { Stream stream( g_answer, MAX_ANSWER_LEN ); bool firstTwo = true; for( uint i = 0; i < info.numOfOnes; ++i ) stream.Push( '(' ); bool fix = false; for( int i = arraySize - 1; i > -1; --i ) { if( outProcessedArray[ i ] == 2 ) { if ( !firstTwo ) stream.Push( '*' ); firstTwo = false; stream.Push( '(' ); stream.Push( '1' ); stream.Push( '+' ); stream.Push( '1' ); stream.Push( ')' ); } else { stream.Push( '+' ); stream.Push( '1' ); if( i != 0 ) { stream.Push( ')' ); } else { fix = true; } } } stream.Push( 0 ); if( fix ) { for( uint i = 0; i < stream.GetCurrSize(); ++i ) { g_answer[ i ] = g_answer[ i + 1 ]; } } return stream.GetData(); } const char* Solve( uint number ) { if( number == 1 ) return "1"; uint size = 0; SProcessedInfo info = GenerateToProcessArray( number, g_toProcess, size ); if( info.numOfOnes + ( 2 * info.numOfTwos ) <= 100 ) { auto* txt = ProcessAnswer( info, g_toProcess, size ); return txt; } // Basically this looks impossible for number < 1 000 000 000.. return "NIE"; } void TestAll() { auto* bla1 = Solve( 29 ); auto* bla2 = Solve( 1000000000 ); auto* bla3 = Solve( 1000000000 - 5 ); auto* bla4 = Solve( 1 ); auto* bla5 = Solve( 2 ); auto* bla6 = Solve( 3 ); auto* bla7 = Solve( 4 ); auto* bla8 = Solve( 5 ); auto* bla9 = Solve( 6 ); auto* bla10 = Solve( 7 ); auto* bla11 = Solve( 8 ); auto* bla13 = Solve( 9 ); auto* prime1 = Solve( 104729 ); auto* prime2 = Solve( 999999733 ); auto* prime3 = Solve( 999996913 ); auto* prime4 = Solve( 999997021 ); } int main() { DEBUG_ONLY( TestAll() ); uint numOfTests = 0; std::cin >> numOfTests; std::vector< uint > numbers; numbers.reserve( numOfTests ); for( uint i = 0; i < numOfTests; ++i ) { uint number = 0; std::cin >> number; numbers.push_back( number ); } for( uint number : numbers ) { const char* txt = Solve( number ); std::cout << txt << "\n"; } return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | #include <vector> #include <math.h> #include <string> #include <iostream> #include <sstream> #include <limits> #include <cstring> #include <array> //#define PA2016_TEST #ifdef PA2016_TEST #define ASSERT( condition ) if ( !( condition ) ) { __debugbreak(); } #define PAUSE_CONSOLE system( "pause" ); #define DEBUG_ONLY( line ) line; #else #define ASSERT( condition ) #define PAUSE_CONSOLE #define DEBUG_ONLY( line ) #endif typedef unsigned int uint; const uint MAX_ANSWER_LEN = 2000; typedef std::array< char, 200 > TProcessArray; TProcessArray g_toProcess; char g_answer[ MAX_ANSWER_LEN ] = { 0 }; struct SProcessedInfo { uint numOfOnes; uint numOfTwos; }; SProcessedInfo GenerateToProcessArray( uint num, TProcessArray& outProcessedArray, uint& outArraySize ) { outProcessedArray.fill( 0 ); uint numOfPlusone = 0; uint numOfTwos = 0; uint i = 0; while( num > 1 ) { if( num & 1 ) { num = num - 1; ++numOfPlusone; outProcessedArray[ i ] = 1; ++i; } num = num >> 1; ++numOfTwos; outProcessedArray[ i ] = 2; ++i; } outArraySize = i; SProcessedInfo info{ numOfPlusone, numOfTwos }; return info; } class Stream { public: Stream( char* stream, uint size ) : m_currStream( stream ) , m_orginalStream( stream ) , m_maxSize( size ) , m_currentSize( 0 ) { } void Push( const char oneChar ) { ASSERT( m_currentSize <= m_maxSize ); *m_currStream = oneChar; ++m_currStream; ++m_currentSize; } char* GetData() const { return m_orginalStream; } uint GetCurrSize() const { return m_currentSize; } private: char* m_currStream; char* m_orginalStream; uint m_currentSize; uint m_maxSize; }; const char* ProcessAnswer( const SProcessedInfo& info, const TProcessArray& outProcessedArray, uint arraySize ) { Stream stream( g_answer, MAX_ANSWER_LEN ); bool firstTwo = true; for( uint i = 0; i < info.numOfOnes; ++i ) stream.Push( '(' ); bool fix = false; for( int i = arraySize - 1; i > -1; --i ) { if( outProcessedArray[ i ] == 2 ) { if ( !firstTwo ) stream.Push( '*' ); firstTwo = false; stream.Push( '(' ); stream.Push( '1' ); stream.Push( '+' ); stream.Push( '1' ); stream.Push( ')' ); } else { stream.Push( '+' ); stream.Push( '1' ); if( i != 0 ) { stream.Push( ')' ); } else { fix = true; } } } stream.Push( 0 ); if( fix ) { for( uint i = 0; i < stream.GetCurrSize(); ++i ) { g_answer[ i ] = g_answer[ i + 1 ]; } } return stream.GetData(); } const char* Solve( uint number ) { if( number == 1 ) return "1"; uint size = 0; SProcessedInfo info = GenerateToProcessArray( number, g_toProcess, size ); if( info.numOfOnes + ( 2 * info.numOfTwos ) <= 100 ) { auto* txt = ProcessAnswer( info, g_toProcess, size ); return txt; } // Basically this looks impossible for number < 1 000 000 000.. return "NIE"; } void TestAll() { auto* bla1 = Solve( 29 ); auto* bla2 = Solve( 1000000000 ); auto* bla3 = Solve( 1000000000 - 5 ); auto* bla4 = Solve( 1 ); auto* bla5 = Solve( 2 ); auto* bla6 = Solve( 3 ); auto* bla7 = Solve( 4 ); auto* bla8 = Solve( 5 ); auto* bla9 = Solve( 6 ); auto* bla10 = Solve( 7 ); auto* bla11 = Solve( 8 ); auto* bla13 = Solve( 9 ); auto* prime1 = Solve( 104729 ); auto* prime2 = Solve( 999999733 ); auto* prime3 = Solve( 999996913 ); auto* prime4 = Solve( 999997021 ); } int main() { DEBUG_ONLY( TestAll() ); uint numOfTests = 0; std::cin >> numOfTests; std::vector< uint > numbers; numbers.reserve( numOfTests ); for( uint i = 0; i < numOfTests; ++i ) { uint number = 0; std::cin >> number; numbers.push_back( number ); } for( uint number : numbers ) { const char* txt = Solve( number ); std::cout << txt << "\n"; } return 0; } |