zoukankan      html  css  js  c++  java
  • HDU 3073 Saving Beans

    Saving Beans

    Time Limit: 3000ms
    Memory Limit: 32768KB
    This problem will be judged on HDU. Original ID: 3037
    64-bit integer IO format: %I64d      Java class name: Main
     
    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


    解题:Lucas 求组合数取模

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 LL F[100010] = {1};
     5 void init(LL mod) {
     6     for(int i = 1; i <= mod; ++i)
     7         F[i] = F[i-1]*i%mod;
     8 }
     9 LL gcd(LL a,LL b,LL &x,LL &y) {
    10     if(!b) {
    11         x = 1;
    12         y = 0;
    13         return a;
    14     }
    15     LL ret = gcd(b,a%b,y,x);
    16     y -= x*(a/b);
    17     return ret;
    18 }
    19 LL Inv(LL b,LL mod) {
    20     LL x,y,d = gcd(b,mod,x,y);
    21     return d == 1?(x%mod + mod)%mod:-1;
    22 }
    23 LL inv(LL b,LL mod) {
    24     if(b == 1) return 1;
    25     return inv(mod%b,mod)*(mod-mod/b)%mod;
    26 }
    27 LL Lucas(LL n,LL m,LL mod) {
    28     LL ret = 1;
    29     while(n && m) {
    30         LL a = n%mod;
    31         LL b = m%mod;
    32         if(a < b) return 0;
    33         ret = ret*F[a]%mod*Inv(F[b]*F[a-b]%mod,mod)%mod;
    34         n /= mod;
    35         m /= mod;
    36     }
    37     return ret;
    38 }
    39 int main() {
    40     int kase,n,m,mod;
    41     scanf("%d",&kase);
    42     while(kase--) {
    43         scanf("%d%d%d",&n,&m,&mod);
    44         init(mod);
    45         printf("%I64d
    ",Lucas(n+m,n,mod));
    46     }
    47     return 0;
    48 }
    View Code
  • 相关阅读:
    git 多个commit合并一个
    前端本地proxy跨域代理配置
    .net core指定环境类型
    使用流程引擎整体解决方案
    通用流程相关方法说明及调用事例
    流程引擎及流程设计器的嵌入方式
    可视化流程设计——流程设计器演示(基于Silverlight)
    通用流程设计
    动态网站后台解决方案
    timestamp时间格式
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4729400.html
Copyright © 2011-2022 走看看