HDU 3037 Saving Beans 多重集合的结合 lucas定理

  题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3037

  题目描述: 要求求x1 + x2 + x3 + ...... + xn <= m 非负整数解的个数, 结果对P取模, 输入的变量是n, m, p, P一定是素数

  解题思路: x1 + ... + xn = m 非负整数解的个数是C(n+m-1, n) , 所以答案就是 C(n+0-1, 0) + C(n+1-1, 1) + ...... C(n+m-1, n) 对P取模,

         由于组合数公式C(n, m) = C(n-1, m-1) + C(n-1, m) 所以该答案两两合并得C(n+m, n) , 又因为n, m 都非常大, 所以想到Lucas定理, 自己还不知道Lucas的原理, 健完身回来看

  代码:

#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <cstring>
#include <iterator>
#include <cmath>
#include <algorithm>
#include <stack>
#include <deque>
#include <map>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define sca(x) scanf("%d",&x)
#define de printf("=======\n")
typedef long long ll;
using namespace std;

const int maxn = 100005;
ll n, m, p;
ll f[maxn];

void init( ll p ) {
    f[0] = 1;
    for( int i = 1; i <= p; i++ ) {
        f[i] = f[i-1] * i % p;
    }
}

ll q_power(ll a, ll b, ll p) {
    ll ret = 1;
    while( b ) {
        if( b & 1 ) ret = ret * a % p;
        b >>= 1;
        a = a * a % p;
    }
    return ret % p;
}

ll lucas( ll n, ll m, ll p ) {
    ll ans = 1;
    while( n && m ) {
        ll nn = n % p, mm = m % p;
        if( nn < mm ) return 0;
        ans = ans * f[nn] * q_power(f[mm]*f[nn-mm]%p, p-2, p) % p;
        n /= p;
        m /= p;
    }
    return ans;
}

int main() {
    int t;
    sca(t);
    while( t-- ) {
        scanf( "%I64d%I64d%I64d", &n, &m, &p );
        init(p);
        printf( "%I64d\n", lucas(n+m, n, p) );
    }
    return 0;
}

  思考: 自己一开始只做到了答案加和那一步, 以为答案是用到逆元呢, 结果一看n, m的取值范围傻眼了, 其实化简到那个式子应该能想到最终等式的.........没化简出来最终的等式是真的菜, 其实我感觉那个公式有点Dp 的意思噢.........lucas自己一直听说今天终于了解了一些了, 继续加油, 健身回来之后去了解Lucas的原理

时间: 2024-11-05 13:31:44

HDU 3037 Saving Beans 多重集合的结合 lucas定理的相关文章

HDU 3037 Saving Beans (Lucas定理)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3037 推出公式为C(n + m, m) % p, 用Lucas定理求大组合数取模的值 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int t; long long n, m, p; long long pow(long long n, long lo

hdu 3037 Saving Beans(组合数学)

hdu 3037 Saving Beans 题目大意:n个数,和不大于m的情况,结果模掉p,p保证为素数. 解题思路:隔板法,C(nn+m)多选的一块保证了n个数的和小于等于m.可是n,m非常大,所以用到Lucas定理. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; ll n, m, p; ll qPow (ll a

HDU 3037 Saving Beans(lucas定理)

题目大意:豆子数i (1~m)分到n颗树上.  树可以为空,那么对于每个i,分配方式是 C(n+i-1,n-1)......于是我用for(i=0-->m)做,不幸超时,m太大. 不过竟然公式可以化简: for(int i=0;i<=m;i++) C(n+i-1,n-1)=C(n+i-1,i) 组合原理: 公式 C(n,k) = C(n-1,k)+C(n-1,k-1) C(n-1,0)+C(n,1)+...+C(n+m-1,m) = C(n,0)+C(n,1)+C(n+1,2)+...+C(n

hdu 3037 Saving Beans 组合数取模模板题。。

Saving Beans Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2707    Accepted Submission(s): 1014 Problem Description Although winter is far away, squirrels have to work day and night to save b

[ACM] hdu 3037 Saving Beans (Lucas定理,组合数取模)

Saving Beans Problem Description Although winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold days. After some time the squirrel family thinks that they have to solve a probl

HDU 3037 Saving Beans (Lucas法则)

主题链接:pid=3037">http://acm.hdu.edu.cn/showproblem.php?pid=3037 推出公式为C(n + m, m) % p. 用Lucas定理求大组合数取模的值 代码: #include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int t; long long n, m, p; long long pow(lo

HDU 3037 Saving Beans (隔板法+Lucas定理)

<题目链接> 题目大意:用$n$颗树保存不超过$m$颗豆子($n,m\leq10^9$)(不一定所有的树都有豆子保存),问你总共有多少种情况.答案对p取模(p保证是个素数). 解题分析:可以转化成 将$n$个相同的球放入$m$个集合中,有的集合中的球数可能为0的等价问题.很明显这可以用隔板法解决,答案为$C(n+m-1,m-1)$则题目解的个数可以转换成求: $C(n+m-1,0)+C(n+m-1,1)+C(n+m-1,2)+……+C(n+m-1,m-1)$ 利用组合数公式 $C(n,k) =

HDU 3037 Saving Beans

/* hdu3037 http://acm.hdu.edu.cn/showproblem.php?pid=3037 lucas 模板题 */ #include <cstdio> #include <cmath> const long long Nmax=100005; long long p; long long ex_gcd(long long a,long long b,long long &x,long long &y)//solve x,y in a*x+b

HDU 3037 Saving Beans(Lucas定理模板题)

Problem Description Although winter is far away, squirrels have to work day and night to save beans. They need plenty of food to get through those long cold days. After some time the squirrel family thinks that they have to solve a problem. They supp