• 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 }
  • 相关阅读:
    2017年度最具商业价值人工智能公司TOP50 榜单发布
    滑动swipe的妙用
    UE3优化
    UE4 框架
    制作HUD
    Component概念
    手游记事
    C++与UnrealScript脚本交互
    unreal Script(US)一些注意事项
    UDK游戏打包详解
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3705763.html
Copyright © 2020-2023  润新知