zoukankan      html  css  js  c++  java
  • hdu 3037 Saving Beans

    Saving Beans

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 2079    Accepted Submission(s): 748


    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.
     
     
     
    2 3 107 ==>10 ==>C(5,2)
     
    { 0,0
       0,1  1,0
       1,1, 2,0  0,2
       3,0  0,3   1,2  2,1
    }
     
     1 #include<iostream>
     2 #include<stdio.h>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<vector>
     6 using namespace std;
     7 typedef __int64 LL;
     8 
     9 LL dp[100002];
    10 
    11 void init(LL p){
    12     int i;
    13     dp[0]=1;
    14     for(i=1;i<=p;i++)
    15         dp[i]=(dp[i-1]*i)%p;
    16 }
    17 LL pow_mod(LL a,LL n,LL p)
    18 {
    19     LL ans=1;
    20     while(n)
    21     {
    22         if(n&1) ans=(ans*a)%p;
    23         n=n>>1;
    24         a=(a*a)%p;
    25     }
    26     return ans;
    27 }
    28 LL C(LL a,LL b,LL p)
    29 {
    30     if(a<b) return 0;
    31     if(b>a-b) b=a-b;
    32     LL sum1=dp[a];
    33     LL sum2=(dp[b]*dp[a-b])%p;
    34     sum1=(sum1*pow_mod(sum2,p-2,p));
    35     return sum1;
    36 }
    37 LL Lucas(LL n,LL m,LL p)
    38 {
    39     LL ans=1;
    40     while(n&&m&&ans)
    41     {
    42         ans=(ans*C(n%p,m%p,p))%p;
    43         n=n/p;
    44         m=m/p;
    45     }
    46     return ans;
    47 }
    48 int main()
    49 {
    50     int T;
    51     LL n,m,p;
    52     scanf("%d",&T);
    53     while(T--)
    54     {
    55         scanf("%I64d%I64d%I64d",&n,&m,&p);
    56         init(p);
    57         if(n>m)swap(n,m);
    58         LL ans= Lucas(n+m,m,p);
    59         printf("%I64d
    ",ans);
    60     }
    61     return 0;
    62 }
  • 相关阅读:
    CefSharp.v49.0.1浏览器控件完全WPF版,实现禁止弹出新窗口,在同一窗口打开链接,并且支持带type="POST" target="_blank"的链接
    C#动态调用WebService
    WPF实现窗体中的悬浮按钮
    Oracle树结构查询按层级排序
    WPF自定义TabControl样式
    WPF自定义Window窗体样式
    C# 实现图片压缩
    C# 图片反色处理 图片夜间模式
    C#中多线程中变量研究
    EasyNetQ操作RabbitMQ(高级消息队列)
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3705763.html
Copyright © 2011-2022 走看看