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
  • 相关阅读:
    前端方便面
    在页面未加载完之前显示loading动画
    块级格式化上下文(BFC)
    css预编译--sass进阶篇
    IPhoneX网页布局简介
    kotlin回调函数作为参数block: T.() -> Unit和block: () -> Unit的区别
    flutter显示参数提示的快捷键
    LinuxC线程pthread线程同步进程同步-互斥量、信号量、条件变量、读写锁、文件锁
    flutter实现页面跳转的两种路由
    android开发FontMetrics的理解
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4729400.html
Copyright © 2011-2022 走看看