#include <iostream> #include <cstdio> using namespace std; #define LONG long long LONG maxPos = 1LL; LONG onePos = 0LL; LONG theRest = 0LL; LONG theMask = 1LL; int last = 0; bool testPos(long chars, LONG pos) { for(long idx = 1L; idx <= chars && idx < 63L; ++idx) { onePos += theMask; theMask <<= 1; } maxPos = onePos * 3LL; if(pos > maxPos) return false; return true; } char first(LONG position) { if(position > (2LL * onePos)) { last = 3; theRest = position - (2LL * onePos); return 'c'; } else if(position > onePos) { last = 2; theRest = position - onePos; return 'b'; } theRest = position; last = 1; return 'a'; } char next() { char ret = 'x'; if(theMask & theRest) { switch(last) { case 3: last = 2; ret = 'b'; break; default: last = 3; ret = 'c'; } } else { switch(last) { case 1: last = 2; ret = 'b'; break; default: last = 1; ret = 'a'; } } return ret; } int main(int argc, char* argv[]) { long charCount = 0L; LONG position = 0LL; scanf("%ld %lld", &charCount, &position); if(!testPos(charCount, position)) { printf("NIE\n"); } else { printf("%c", first(position)); if(theRest > 0LL) { while(!(theMask & theRest)) theMask >>= 1; theMask >>= 1; while(theMask) { printf("%c", next()); theMask >>= 1; } } printf("\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 | #include <iostream> #include <cstdio> using namespace std; #define LONG long long LONG maxPos = 1LL; LONG onePos = 0LL; LONG theRest = 0LL; LONG theMask = 1LL; int last = 0; bool testPos(long chars, LONG pos) { for(long idx = 1L; idx <= chars && idx < 63L; ++idx) { onePos += theMask; theMask <<= 1; } maxPos = onePos * 3LL; if(pos > maxPos) return false; return true; } char first(LONG position) { if(position > (2LL * onePos)) { last = 3; theRest = position - (2LL * onePos); return 'c'; } else if(position > onePos) { last = 2; theRest = position - onePos; return 'b'; } theRest = position; last = 1; return 'a'; } char next() { char ret = 'x'; if(theMask & theRest) { switch(last) { case 3: last = 2; ret = 'b'; break; default: last = 3; ret = 'c'; } } else { switch(last) { case 1: last = 2; ret = 'b'; break; default: last = 1; ret = 'a'; } } return ret; } int main(int argc, char* argv[]) { long charCount = 0L; LONG position = 0LL; scanf("%ld %lld", &charCount, &position); if(!testPos(charCount, position)) { printf("NIE\n"); } else { printf("%c", first(position)); if(theRest > 0LL) { while(!(theMask & theRest)) theMask >>= 1; theMask >>= 1; while(theMask) { printf("%c", next()); theMask >>= 1; } } printf("\n"); } return 0; } |