題目:
hdu_4349_Xiao_Ming's_Hope
官方題解:本題為Lucas定理推導題,我們分析一下 C(n,m)%2,那麼由lucas定理,我們可以寫成二進位的形式觀察,比如 n=1001101,m是從000000到1001101的枚舉,我們知道在該定理中C(0,1)=0,因此如果n=1001101的0對應位置的m二進位位為1那麼C(n,m) % 2==0,因此m對應n為0的位置只能填0,而1的位置填0,填1都1(C(1,0)=C(1,1)=1),不影響結果為奇數,並且保證不會出n的範圍,因此所有的情況即是n中1位置對應m位置0,1的枚舉,那麼結果很明顯就是:2^(n中1的個數)
個人理解:正如題解所說,本題是Lucas定理的直接應用。當然也可以多寫幾個答案,看出答案與N的關係是2^(n中1的個數)。 Lucas定理:
For non-negative integers m and n and a prime p, the following congruence relation holds:
where
and
are the base p expansions of m and n respectively.
定理還不會證。
標程:
#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>using namespace std;int lowbit(int x) { return x & (-x);}int get_result(int n) { int res = 0; while (n) { n -= lowbit(n); res++; } return res;}int main() { // freopen("data.in", "r", stdin); // freopen("data.out", "w", stdout); int n; while (scanf("%d", &n) != EOF) { printf("%d\n", 1 << get_result(n)); } return 0;}