HDU4349--Xiao Ming's Hope(数论)

输入一个n(1<=n<=108),求C(n,0),C(n,1),C(n,2)...C(n,n)有多少个奇数。

Lacus定理 http://blog.csdn.net/acm_cxlove/article/details/7844973

A、B是非负整数,p是质数。AB写成p进制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0]。

则组合数C(A,B)与C(a[n],b[n])*C(a[n-1],b[n-1])*...*C(a[0],b[0])  modp同

所以此题中 C(n, x) 将n和x化成2进制 C(0,0)=1,C(0,1)=0,C(1,0)=1,C(1,1)=1

所以如果C(n,x)为1,那么n为0的位置x必为0,n为1的位置x为0或1

答案为2^(n的二进制表示1的个数)

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main()
{
    int n, c;
    while (~scanf("%d", &n)) {
        c = 0;
        while (n) {
            if (n & 1) ++c;
            n >>= 1;
        }
        printf("%d\n", (int)pow(2, c));
    }
    return 0;
}

  

HDU4349--Xiao Ming's Hope(数论)

时间: 2024-07-30 01:56:05

HDU4349--Xiao Ming's Hope(数论)的相关文章

数论(Lucas定理) HDOJ 4349 Xiao Ming&#39;s Hope

题目传送门 题意:求C (n,0),C (n,1),C (n,2)...C (n,n)中奇数的个数 分析:Lucas 定理:A.B是非负整数,p是质数.AB写成p进制:A=a[n]a[n-1]...a[0],B=b[n]b[n-1]...b[0].则组合数C(A,B)与C(a[n],b[n])*C(a[n-1],b[n-1])*...*C(a[0],b[0])  mod p同.即:Lucas (n,m,p)=C (n%p,m%p) * Lucas (n/p,m/p,p)  我是打表找规律的,就是

HDU 4349 Xiao Ming&#39;s Hope (Lucas)

题意:给定一个 n,问你在 C(n, 0) - C(n , n) 中有多少个奇数. 析:Lucas定理,C(b[i], a[i]),只要不为0,那么就是奇数,然后b[i],是固定的,也就是说a[i] 只有 b[i]+1种情况.最后乘起来就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <

codeforces 4349 Xiao Ming&#39;s Hope lucas

题目链接 给一个n, 求C(n, 0), C(n, 1), ..........C(n, n)里面有多少个是奇数. 我们考虑lucas定理, C(n, m) %2= C(n%2, m%2)*C(n/2, m/2)%2,   C(n/2, m/2) = C(n/2%2, m/2%2)*C(n/2/2, m/2/2), 这样一直递归下去,直到m为0. 我们知道如果一个数是奇数, 那么它的所有因子都是奇数, 对应于上面的式子, n%2是偶数的时候, m%2也必须是偶数才可以, 而n%2是奇数的时候,

hdu 5433 Xiao Ming climbing(bfs+三维标记)

Problem Description Due to the curse made by the devil,Xiao Ming is stranded on a mountain and can hardly escape. This mountain is pretty strange that its underside is a rectangle which size is n∗m and every little part has a special coordinate(x,y)a

HDU 4349 Xiao Ming&#39;s Hope 找规律

原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4349 Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1723    Accepted Submission(s): 1144 Problem Description Xiao Ming likes coun

HDU 4349 Xiao Ming&#39;s Hope(数学题,Lucas定理)

解题思路: 深入理解lucas定理. Lucas定理:把n写成p进制a[n]a[n-1]a[n-2]...a[0],把m写成p进制b[n]b[n-1]b[n-2]...b[0],则C(n,m)与C(a[n],b[n])*C(a[n-1],b[n-1])*C(a[n-2],b[-2])*....*C(a[0],b[0])模p同余. 这题p为2,所以a[i]和b[i]为0或者1.又因为C(1,0), C(1,1), 即当n等于1时,结果才等于1. 所以写出n的二进制,m从0遍历到n,每一次遍历把m写

HDU 4349 Xiao Ming&#39;s Hope

很无语的一个题. 反正我后来看题解完全不是一个道上的. 要用什么组合数学的lucas定理. 表示自己就推了前面几个数然后找找规律. C(n, m) 就是 组合n取m: (m!(n-m!)/n!) 如果n==11 : C(11,0):C(11,1):C(11,2):C(11,3):C(11,4):C(11,5): 分别为 (1/1); (1 / 11) ; (11*10 / 2*1)  ;   (11*10*9 / 3*2*1); (11*10*9*8 / 4*3*2*1) ;  (11*10*9

hdu 5433 Xiao Ming climbing

题意:一张图,给出起点和终点,给一个fighting值,每走一步fighting消耗1,并且消耗(abs(H1−H2))/fighting 的 physical power.求起点到终点消耗最小的physical pow. 解:题目上有一句"At the biginning Xiao Ming has a fighting will k,if it turned to 0 Xiao Ming won't be able to fight with the devil,that means fai

Xiao Ming&#39;s Hope 卢卡斯定理的推广。

Xiao Ming's Hope 题目抽象:求C(n,0),C(n,1),……,C(n,n)中奇数的个数. 思路:考察C(n,m)%2. 由卢卡斯定理    C(A,B)=C(a[n-1],b[n-1])*C(a[n-2],b[n-2])* ……*C(a[0],b[0])  ,其中a[i]为A的p进制位 要使C(n,m)%2==1,  那么对于n上的1的位,m上对应的位可以为0,1,(c(1,0)=c(1,1)=1). 对于n上为0的位,m上对应的位必须为0.(c(0,0)=1,c(0,1)=0

hdu 4349 Xiao Ming&#39;s Hope 规律

Xiao Ming's Hope Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Xiao Ming likes counting numbers very much, especially he is fond of counting odd numbers. Maybe he thinks it is the best way to