• 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
  • 相关阅读:
    《软件工程》团队第一阶段Sprint检查表
    灭霸第一阶段绩效评估
    【Copy攻城狮日志】docker搭建jenkins拉取svn代码打包vue项目部署到nginx
    前端移动App开发环境搭建
    【Copy攻城狮日志】Node快速重命名文件,告别Potplay字幕困扰问题
    centos部署yapi爬坑记
    mint-ui之picker爬坑记
    前端内网穿透,localtunnel你值得拥有!
    Visual Studio Live Share不完全指北
    jq跑马灯效果
  • 原文地址:https://www.cnblogs.com/oyking/p/4101532.html
Copyright © 2020-2023  润新知