zoukankan      html  css  js  c++  java
  • [HDU 3461] Saving Beans & 组合计数Lucas定理模板

    Saving Beans

    Time Limit: 6000/3000 MS (Java/Others)

    Memory Limit: 32768/32768 K (Java/Others)

    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 suppose that they will save beans in n different trees. However, since the food is not sufficient nowadays, they will get no more than m beans. They want to know that how many ways there are to save no more than m beans (they are the same) in n trees.
    Now they turn to you for help, you should give them the answer. The result may be extremely huge; you should output the result modulo p, because squirrels can’t recognize large numbers.
    Input
    The first line contains one integer T, means the number of cases.
    Then followed T lines, each line contains three integers n, m, p, means that squirrels will save no more than m same beans in n different trees, 1 <= n, m <= 1000000000, 1 < p < 100000 and p is guaranteed to be a prime.
    Output
    You should output the answer modulo p.
    Sample Input
    2
    1 2 5
    2 1 5
    Sample Output
    3
    3
    Hint
    Hint For sample 1, squirrels will put no more than 2 beans in one tree. Since trees are different, we can label them as 1, 2 … and so on. The 3 ways are: put no beans, put 1 bean in tree 1 and put 2 beans in tree 1. For sample 2, the 3 ways are: put no beans, put 1 bean in tree 1 and put 1 bean in tree 2.
    Source
    【题解】
    经典的组合计数,求C(n+m,m) mod p
    有个著名的定理 Lucas定理:C(n,m) mod p = C(n/p,m/p)*C(n%p,m%p)
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 long long f[100005];
     4 inline void getfact(long long p) {
     5     f[0]=1;
     6     for (int i=1;i<=p;++i)
     7         f[i]=(f[i-1]*i)%p;
     8 }
     9 inline long long powx(long long a,long long b,long long p) {
    10     long long ret=1;
    11     while(b) {
    12         if (b&1) ret=(ret*a)%p;
    13         a=(a*a)%p;
    14         b>>=1;
    15     }
    16     return ret;
    17 }
    18 inline long long lucas(long long n,long long m,long long p) {
    19     long long ret=1;
    20     while(n&&m) {
    21         long long a=n%p,b=m%p;
    22         if(a<b) return 0;
    23         ret=(ret*f[a]*powx((f[b]*f[a-b])%p,p-2,p))%p;
    24         n/=p;m/=p;
    25     }
    26     return ret;
    27 }
    28 int main() {
    29     int t;
    30     scanf("%d",&t);
    31     while(t--) {
    32         long long n,m,p;
    33         scanf("%I64d%I64d%I64d",&n,&m,&p);
    34         getfact(p);
    35         printf("%I64d
    ",lucas(n+m,m,p));
    36     }
    37     return 0;
    38 }
    View Code
  • 相关阅读:
    实用的设计模式【一】---类设计法则
    vimium 使用心得
    记一次给部门做分享的心得
    centos7安装docker和docker compose【转】
    docker 部署 jenkins
    centos删除docker0虚拟网卡
    CentOS7查看和关闭防火墙
    .Net Core Autofac实现依赖注入
    【转】Docker基础
    【转】使用Docker+Jenkins自动构建部署
  • 原文地址:https://www.cnblogs.com/TonyNeal/p/hdu3461.html
Copyright © 2011-2022 走看看