HDU 3037 Saving Beans (direct application of Lucas theorem)

Source: Internet
Author: User

Problem Solving Ideas:

Direct C (n+m, m)% p, because n, M, p are very large, so we should use the Lucas theorem to solve the problem of large combination number of modulus.

#include <string.h> #include <iostream> #include <algorithm> #include <vector> #include < queue> #include <set> #include <map> #include <string> #include <math.h> #include <cstdio > #include <stdlib.h> #include <time.h> #include <assert.h> #define LL long long#define for (i, X, y) for (int i=x;i<=y;i++) using namespace std;const int maxn = 100000 + 10; ll Pow_mod (ll A, ll B, ll MoD) {ll ret = 1;while (b) {if (b & 1) ret = ret * A% Mod;a = a * A% mod;b >>= 1;} return ret;} ll fac[maxn];void Init (ll p) {fac[0] = 1; For (I, 1, p) fac[i] = (fac[i-1] * i)% P;} ll Lucas (ll N, ll M, ll P) {LL ret = 1;while (n && m) {ll a = n p, b = m% p;if (A < b) return 0;ret = (RET * FAC [A] * POW_MOD (fac[b] * fac[a-b]% P, p-2, p))% P;n/= p, M/= p;} return ret;} int main () {int t;scanf ("%d", &t), while (t--) {int n, m, p;scanf ("%d%d%d", &n, &m, &p), init (p);p rintf ("%d\ n ", (int) Lucas (n + M, M, p)); return 0;}

HDU 3037 Saving Beans (direct application of Lucas theorem)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.