https://www.hackerrank.com/contests/hourrank-11/challenges/strange-code
原题不难,读懂题意即可,不过我自己原来的做法比较暴力,疯狂tle…看到正解以后感动得掉下了眼泪…
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
long long t, i, j, time;
cin >> t;
for (i = 1;; i<<=1)
if (3*((1<<i)-1)>=t)
break;
for (j = i>>1;j<=i; j++)
if (3 * ((1 << j) - 1) >= t)
break;
long long ans = 3 * (1 << (j - 1));
long long cha = t - 3 * ((1 <<( j - 1)) - 1) - 1;
cout << ans - cha << endl;
return 0;
}
正解
#include <iostream>
#include<cstdio>
using namespace std;
long long n;
int main() {
cin >> n;
long long P = 3;
while (n > P){
n -= P;
P *= 2;
}
cout << P - n + 1 << endl;
return 0;
}