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)