#include <iostream> #include <stdio.h> #include <algorithm> #include <string> #include <cmath> #define For(i, j, n) for(int i = j ; i <= n ; ++i) using namespace std; typedef unsigned long long Ull; const unsigned long long LIM = 18446744073709551615ull; Ull k; int n; int main() { cin >> n >> k; Ull res = 0; while(n) { //cout << "\nk :" << k << endl; Ull now = 1ull << (n - 1); //cout << "\nnow :" << now << endl; if(k < now) putchar('0'); else putchar('1'); if(k >= now) //k = (1 << n) - k - 1; { if(n == 64) k = LIM - k; else k = (1ull << n) - k - 1; } n--; } return 0; }
1.因為n最大是64,會溢位unsigned long long,所以要先打表,打出2^64-1
2.(1ull<<n)的ull不能去掉:雖然說C++在不同資料型別運算的時候會自動轉換,但是要注意:
(1<<n)是被括號包起來的,所以它會最優先運算,而1預設是int型別,這裡位移位數太多,直接就先溢位,全0了,後面再轉unsigned long long也來不及