zoukankan      html  css  js  c++  java
  • HDU 3037 Saving Beans(Lucas定理模板题)

    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.
     
    题目大意:求C(n + m, m) % p
    思路:http://blog.csdn.net/acm_cxlove/article/details/7844973
     
    代码(1234MS):
     1 #include <cstdio>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <iostream>
     5 using namespace std;
     6 typedef long long LL;
     7 
     8 const int MAXN = 100010;
     9 
    10 int fac[MAXN];
    11 int n, m, p, T;
    12 
    13 int power(int x, int p, int mod) {
    14     int res = 1;
    15     while(p) {
    16         if(p & 1) res = (LL)res * x % mod;
    17         x = (LL)x * x % mod;
    18         p >>= 1;
    19     }
    20     return res;
    21 }
    22 
    23 void init_fac(int p) {
    24     fac[0] = 1;
    25     for(int i = 1; i <= p; ++i)
    26         fac[i] = (LL)fac[i - 1] * i % p;
    27 }
    28 
    29 int lucas(int n, int m, int p) {
    30     int res = 1;
    31     while(n && m) {
    32         int a = n % p, b = m % p;
    33         if(a < b) return 0;
    34         res = (LL)res * fac[a] * power((LL)fac[b] * fac[a - b] % p, p - 2, p) % p;//三次乘法注意
    35         n /= p;
    36         m /= p;
    37     }
    38     return res;
    39 }
    40 
    41 int main() {
    42     scanf("%d", &T);
    43     while(T--) {
    44         scanf("%d%d%d", &n, &m, &p);
    45         init_fac(p);
    46         printf("%d
    ", lucas(n + m, m, p));
    47     }
    48 }
    View Code
  • 相关阅读:
    Ajax加载数据的使用
    解决VS2012新建MVC4等项目时,收到此模板加载程序集“NuGet.VisualStudio.Interop…”的错误
    XSS攻击原理
    SQL语句:一张表和另一张表的多重匹配查询
    ASP.NET打开项目错误:将指定的计数添加到该信号量中会导致其超过最大计数。
    读取数据库的数据并转换成List<>
    11款样式新颖的 jQuery/CSS3 网页菜单
    网站提高速度的13个简易规则
    微软分布式缓存解决方案
    性能优化工具 MVC Mini Profiler
  • 原文地址:https://www.cnblogs.com/oyking/p/4101532.html
Copyright © 2011-2022 走看看