HDU 4349 Xiao Ming's Hope (Lucas定理详解+推理过程)

Problem Description

\quad Xiao Ming likes counting numbers very much, especially he is fond of counting odd numbers. Maybe he thinks it is the best way to show he is alone without a girl friend. The day 2011.11.11 comes. Seeing classmates walking with their girl friends, he coundn’t help running into his classroom, and then opened his maths book preparing to count odd numbers. He looked at his book, then he found a question " C n 0 + C n 1 + C n 1 + . . . + C n n = ? " C_n^0+C_n^1+C_n^1+...+C_n^n=?" Cn0+Cn1+Cn1+...+Cnn=?". Of course, Xiao Ming knew the answer, but he didn’t care about that , What he wanted to know was that how many odd numbers there were? Then he began to count odd numbers. When n is equal to 1 1 1, C 1 0 = C 1 1 = 1 C_1^0=C_1^1=1 C10=C11=1, there are 2 2 2 odd numbers. When n is equal to 2 , C 1 2 0 = C 2 2 = 1 2, C_12^0=C_2^2=1 2,C120=C22=1, there are 2 2 2 odd numbers… Suddenly, he found a girl was watching him counting odd numbers. In order to show his gifts on maths, he wrote several big numbers what n would be equal to, but he found it was impossible to finished his tasks, then he sent a piece of information to you, and wanted you a excellent programmer to help him, he really didn’t want to let her down. Can you help him?

Input

Each line contains a integer n ( 1 < = n < = 1 0 8 ) n(1<=n<=10^8) n(1<=n<=108)

Output

A single line with the number of odd numbers of C n 0 + C n 1 + C n 1 + . . . + C n n C_n^0+C_n^1+C_n^1+...+C_n^n Cn0+Cn1+Cn1+...+Cnn

Sample Input

1
2
11

Sample Output

2
2
8

实际上就是求 C n i % 2 = = 1 C_n^i\%2 == 1 Cni%2==1的个数,
应用Lucas定理, C n m = C n / p m / p ∗ C n % p m % p ( m o d ( p ) ) C_n^m=C_{n/p}^{m/p}∗C_{n\%p}^{m\%p}(mod(p)) Cnm=Cn/pm/pCn%pm%p(mod(p))

设:

\quad \quad \quad \quad \quad \quad \quad \quad n ( 10 ) = a 1 a 2 a k . . . . a k ( 2 ) n(10)=a_{1}a_{2}a_{k}....a_{k}(2) n(10)=a1a2ak....ak(2)

\quad \quad \quad \quad \quad \quad \quad \quad m ( 10 ) = b 1 b 2 b 3 . . . . b k ( 2 ) m(10)=b_{1}b_{2}b_{3}....b_{k}(2) m(10)=b1b2b3....bk(2)

则:

\quad \quad \quad \quad \quad \quad \quad \quad C n m ( m o d ) 2 = ∏ i = 2 k C a i b i C_n^m(mod)2=\prod_{i=2}^{k}{C_{a_{i}}^{b_{i}}} Cnm(mod)2=i=2kCaibi

如果:

\quad \quad \quad \quad \quad \quad \quad \quad C n m ( m o d ) 2 = 1 C_n^m(mod)2=1 Cnm(mod)2=1

∀ \forall i i i ∈ \in [ 1 , k ] [1,k] [1,k]

\quad \quad \quad \quad \quad \quad \quad \quad C a i b i = 1 C_{a_{i}}^{b_{i}}=1 Caibi=1

则:

  • C 0 0 = 1 , C 0 1 = 0 C_0^0= 1,C_0^1= 0 C00=1,C01=0即当 a i = 0 a_{i}=0 ai=0 b i b_{i} bi必为 0 0 0
  • C 1 0 = 1 , C 1 1 = 1 C_1^0 = 1, C_1^1 = 1 C10=1,C11=1即当 a i = 1 a_{i}=1 ai=1 b i b_{i} bi可以为 0 0 0,可以为 1 1 1

换句话说这道题就是转化为二进制来做,当 n n n的这一位为 0 0 0 m m m要求必须为 0 0 0,若是为 2 2 2则对 m m m没有要求,那么久统计有几位 1 1 1,一位 1 1 1是两种选择, c n t cnt cnt 1 1 1就是 2 c n t 2^{cnt} 2cnt种选择,也就是 2 c n t 2^{cnt} 2cnt位奇数。

AC代码:

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n,m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n,m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sc(n) scanf("%c",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
    int ret = 0, sgn = 1;
    char ch = getchar();
    while(ch < '0' || ch > '9')
    {
        if(ch == '-')
            sgn = -1;
        ch = getchar();
    }
    while (ch >= '0' && ch <= '9')
    {
        ret = ret*10 + ch - '0';
        ch = getchar();
    }
    return ret*sgn;
}
inline void Out(int a)    //Êä³öÍâ¹Ò
{
    if(a>9)
        Out(a/10);
    putchar(a%10+'0');
}

ll gcd(ll a,ll b)
{
    return b==0?a : gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
    return a*b/gcd(a,b);
}
///快速幂m^k%mod
int qpow(int m, int k, int mod)
{
    int res = 1, t = m;
    while (k)
    {
        if (k&1)
            res = res * t % mod;
        t = t * t % mod;
        k >>= 1;
    }
    return res;
}

// 快速幂求逆元
int Fermat(int a, int p) //费马求a关于b的逆元
{
    return qpow(a,p-2,p);
}


///扩展欧几里得
int exgcd(int a,int b,int &x,int &y)
{
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    int g=exgcd(b,a%b,x,y);
    int t=x;
    x=y;
    y=t-a/b*y;
    return g;
}

///使用ecgcd求a的逆元x
int mod_reverse(int a,int p)
{
    int d,x,y;
    d=exgcd(a,p,x,y);
    if(d==1)
        return (x%p+p)%p;
    else
        return -1;
}

///中国剩余定理模板
ll china(int a[],int b[],int n)//a[]为除数,b[]为余数
{
    int M=1,y,x=0;
    for(int i=0; i<n; ++i) //算出它们累乘的结果
        M*=a[i];
    for(int i=0; i<n; ++i)
    {
        int w=M/a[i];
        int tx=0;
        int t=exgcd(w,a[i],tx,y);//计算逆元
        x=(x+w*(b[i]/t)*x)%M;
    }
    return (x+M)%M;
}

int n,m;
int i,j,k;

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int cnt=0;
        while(n)
        {
            cnt+=(n&1);
            n>>=1;
        }
        printf("%d\n",1<<cnt);
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值