您的位置:首页 > 运维架构

1011_Xiao_Ming's_Hope

2012-08-11 20:38 363 查看


题目:

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: