#include <cstdio> #include <iostream> #define DBG(X) using namespace std; char sout[1000001]; unsigned long long dp[1000001]; unsigned long long maxPot60() { unsigned long long p = 1; int w = 1; while (w <= 60) { p *= 2; DBG(cout << w << " " << p << endl;) w++; } return p; } int main() { int n; unsigned long long k; scanf("%d %llu", &n, &k); DBG(printf("%llu\n", maxPot60()); printf("%d %llu\n", n, k); cout << maxPot60() << endl;) unsigned long long all = 0; unsigned long long pot = 1ULL; int wykl = 1; while (pot <= k && wykl < n) { pot *= 2ULL; pot++; wykl++; } pot *= 3ULL; if (pot < k) { printf("NIE\n"); return 0; } dp[1] = 1ULL; for (int i = 2; i <= n; i++) { if (dp[i - 1] == maxPot60()) { dp[i] = dp[i - 1]; } else { dp[i] = dp[i - 1] * 2 + 1; } } int cnt = 0; char last = 'a'; char tab1[4] = "baa"; char tab2[4] = "ccb"; DBG(cout << "dp " << dp[n] << endl;) if (dp[n] >= k) { last = 'a'; //k -= dp[n]; k--; } else if (2 * dp[n] >= k) { last = 'b'; k -= dp[n]; k--; } else { last = 'c'; k -= 2 * dp[n]; k--; } DBG(cout << "last " << last << " k = " << k << endl;) dp[0] = 0; sout[0] = last; if (k) for (int i = n - 1; i >= 1; i--) { if (dp[i] > k) { last = tab1[(last - 'a') % 3]; sout[n - i] = last; k--; } else if (dp[i] == k) { last = tab1[(last - 'a') % 3]; sout[n - i] = last; //k -= dp[i]; //k--; //break; } else if (dp[i] < k) { last = tab2[(last - 'a') % 3]; sout[n - i] = last; k -= dp[i]; k--; } DBG(cout << "dp " << dp[i] << " last " << last << " k = " << k << endl;) // if (k==1) break; //else break; if (!k) { break; } } printf("%s\n", sout); 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 | #include <cstdio> #include <iostream> #define DBG(X) using namespace std; char sout[1000001]; unsigned long long dp[1000001]; unsigned long long maxPot60() { unsigned long long p = 1; int w = 1; while (w <= 60) { p *= 2; DBG(cout << w << " " << p << endl;) w++; } return p; } int main() { int n; unsigned long long k; scanf("%d %llu", &n, &k); DBG(printf("%llu\n", maxPot60()); printf("%d %llu\n", n, k); cout << maxPot60() << endl;) unsigned long long all = 0; unsigned long long pot = 1ULL; int wykl = 1; while (pot <= k && wykl < n) { pot *= 2ULL; pot++; wykl++; } pot *= 3ULL; if (pot < k) { printf("NIE\n"); return 0; } dp[1] = 1ULL; for (int i = 2; i <= n; i++) { if (dp[i - 1] == maxPot60()) { dp[i] = dp[i - 1]; } else { dp[i] = dp[i - 1] * 2 + 1; } } int cnt = 0; char last = 'a'; char tab1[4] = "baa"; char tab2[4] = "ccb"; DBG(cout << "dp " << dp[n] << endl;) if (dp[n] >= k) { last = 'a'; //k -= dp[n]; k--; } else if (2 * dp[n] >= k) { last = 'b'; k -= dp[n]; k--; } else { last = 'c'; k -= 2 * dp[n]; k--; } DBG(cout << "last " << last << " k = " << k << endl;) dp[0] = 0; sout[0] = last; if (k) for (int i = n - 1; i >= 1; i--) { if (dp[i] > k) { last = tab1[(last - 'a') % 3]; sout[n - i] = last; k--; } else if (dp[i] == k) { last = tab1[(last - 'a') % 3]; sout[n - i] = last; //k -= dp[i]; //k--; //break; } else if (dp[i] < k) { last = tab2[(last - 'a') % 3]; sout[n - i] = last; k -= dp[i]; k--; } DBG(cout << "dp " << dp[i] << " last " << last << " k = " << k << endl;) // if (k==1) break; //else break; if (!k) { break; } } printf("%s\n", sout); return 0; } |