http://acm.tju.edu.cn/toj/showp3914.html
http://blog.csdn.net/wmn_wmn/article/details/7840539
http://en.wikipedia.org/wiki/Lucas'_theorem
http://blog.csdn.net/wukonwukon/article/details/7341270
TLE1 (linear complexity)
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
int n;
while (cin >> n) {
int ans = 1, cnt = 0;
for (int i = 1; i <= n; ++i) {
int t = i;
while ((t & 1) == 0)
--cnt, t = t >> 1;
t = n - i + 1;
while ((t & 1) == 0)
++cnt, t = t >> 1;
if (cnt == 0)
++ans;
}
printf("%d\n", ans);
}
return 0;
}
TLE2 (do not use decimal for "cnt")
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
int n;
while (cin >> n) {
int ans = 1, cnt = 0;
for (int i = 1; i <= n; ++i) {
int t = i;
cnt -= (t ^ (t & (t - 1)));
t = n - i + 1;
cnt += (t ^ (t & (t - 1)));
ans += (cnt == 0);
}
printf("%d\n", ans);
}
return 0;
}
AC (logarithmic complexity, Lucas' theorem)
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
int n;
while (cin >> n) {
int ans = 1;
while (n) {
n &= (n - 1);
ans = ans << 1;
}
printf("%d\n", ans);
}
return 0;
}
(remove the rightmost '1' in binary representation of n: n &= (n - 1))