#include <stdlib.h> #include <stdio.h> #include <iostream> typedef long long int int64; #define MAX_CONSIDERED_N 60 void solve( int n, int64 k ) { // Is there solution? const int clampedN = ( n > MAX_CONSIDERED_N ) ? MAX_CONSIDERED_N : n; int64 numWordsOfMaxLengthN = ( ( int64 ) 1 << clampedN ) - 1; if ( k > numWordsOfMaxLengthN * 3 ) { std::cout << "NIE"; return; } // Figure out the first letter char charIndex = ( char ) ( ( k - 1 ) / numWordsOfMaxLengthN ); k -= charIndex * numWordsOfMaxLengthN; std::cout << ( char ) ( 'a' + charIndex ); // Figure out remaining letters while ( k > 1 ) { // Pick appropriate letter const int clampedN = ( n > MAX_CONSIDERED_N ) ? MAX_CONSIDERED_N : n; const int64 half = ( int64 ) 1 << ( clampedN - 1 ); if ( k <= half ) { charIndex = charIndex ? 0 : 1; --k; } else { charIndex = ( charIndex == 2 ) ? 1 : 2; k -= half; } std::cout << ( char ) ( 'a' + charIndex ); --n; } } int main() { #if 0 for ( int64 k = 1; k <= 100; ++k ) { solve( 4, k ); std::cout << std::endl; } #endif int n; int64 k; std::cin >> n >> k; solve( n, k ); 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 | #include <stdlib.h> #include <stdio.h> #include <iostream> typedef long long int int64; #define MAX_CONSIDERED_N 60 void solve( int n, int64 k ) { // Is there solution? const int clampedN = ( n > MAX_CONSIDERED_N ) ? MAX_CONSIDERED_N : n; int64 numWordsOfMaxLengthN = ( ( int64 ) 1 << clampedN ) - 1; if ( k > numWordsOfMaxLengthN * 3 ) { std::cout << "NIE"; return; } // Figure out the first letter char charIndex = ( char ) ( ( k - 1 ) / numWordsOfMaxLengthN ); k -= charIndex * numWordsOfMaxLengthN; std::cout << ( char ) ( 'a' + charIndex ); // Figure out remaining letters while ( k > 1 ) { // Pick appropriate letter const int clampedN = ( n > MAX_CONSIDERED_N ) ? MAX_CONSIDERED_N : n; const int64 half = ( int64 ) 1 << ( clampedN - 1 ); if ( k <= half ) { charIndex = charIndex ? 0 : 1; --k; } else { charIndex = ( charIndex == 2 ) ? 1 : 2; k -= half; } std::cout << ( char ) ( 'a' + charIndex ); --n; } } int main() { #if 0 for ( int64 k = 1; k <= 100; ++k ) { solve( 4, k ); std::cout << std::endl; } #endif int n; int64 k; std::cin >> n >> k; solve( n, k ); return 0; } |