HDU 3037 Saving Beans (number theory, Lucas theorem)

Source: Internet
Author: User

Test instructions: Ask how many ways to put in n trees with no more than M seeds.

Analysis: Test instructions can be converted to X1 + x2 +. + xn = m, how many solutions, and then using the combined knowledge to get the answer is C (N+m, M).

Then we ask for this value directly, so we can use the Lucas theorem to break down the combination number, which is Lucas (N,M,P) =c (n%p,m%p) * Lucas (N/P,M/P,P).

And then we can do it according to the Fermat theorem.

The code is as follows:

The first type:

#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <string> #include < cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include < queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include < cmath> #include <stack> #include <unordered_map>//#include <tr1/unordered_map> #define Freopenr  Freopen ("In.txt", "R", stdin) #define FREOPENW freopen ("OUT.txt", "w", stdout) using namespace std;//using namespace std:: Tr1;typedef Long Long ll;typedef pair<int, int> p;const int inf = 0x3f3f3f3f;const double inf = 0x3f3f3f3f3f3f;cons T LL LNF = 0x3f3f3f3f3f3f;const Double PI = ACOs ( -1.0); const double EPS = 1e-8;const int maxn = 10005;const LL mod = 10000 000000007;const int N = 1e6 + 5;const int dr[] = {-1, 0, 1, 0, 1, 1,-1, -1};const int dc[] = {0, 1, 0,-1, 1,-1, 1,-1}; const int Hr[]= {-2,-2,-1,-1, 1, 1, 2, 2};const int hc[]= {-1, 1,-2, 2,-2, 2,-1, 1};const char *hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "10 00 "," 1001 "," 1010 "," 1011 "," 1100 "," 1101 "," 1110 "," 1111 "}; ll EXGCD (ll a,ll b,ll &x,ll &y) {ll d = a;if (b!=0) {D=EXGCD (b,a%b,y,x); y-= (A/b) *x;} else{x=1;y=0;} return D;} ll Mod_inverse (ll a,ll m) {ll x,y;exgcd (a,m,x,y); return (m+x%m)%m;} inline ll gcd (ll A, ll b) {return b = = 0? a:gcd (b, a%b);} int n, m;const int mon[] = {0, 31, 29, 31, 30, 31, 0, +, +,, +, +, +,, N, H, H, C, h, 31};inline int Min (int a, int b) {return a < b? A:b;} inline int Max (int a, int b) {return a > b a:b;} inline ll Min (ll A, ll b) {return a < b a:b;} inline ll Max (ll A, ll b) {return a > b a:b;} inline bool Is_in (int r, int c) {return R >= 0 && r < n && C >= 0 && C < m;} LL fact[100005];    ll Mod_fact (ll N, ll P, ll &e) {e = 0;    if (!n) return 1; LL Res = Mod_fact (n/p, P, E);    E + = n/p;    if (n/p% 2 = 0) return res * (p-fact[n%p])% p; return res * fact[n%p]% p;}    ll Mod_comb (ll N, ll K, ll P) {if (N < 0 | | K < 0 | | n < k) return 0;    LL E1, E2, E3;    LL A1 = Mod_fact (n, p, E1);    LL A2 = Mod_fact (K, p, E2);    LL a3 = mod_fact (N-k, p, E3);    if (E1 > E2+e3) return 0; Return A1 * Mod_inverse (A2*a3%p, p)% p;}    int main () {fact[0] = 1;  int T;    Cin >> T;        while (t--) {LL p, m, N;        scanf ("%i64d%i64d%i64d", &n, &m, &p);        for (int i = 1; i < P; ++i) fact[i] = fact[i-1] * (LL) I% P;    printf ("%i64d\n", Mod_comb (N+m, M, p)); } return 0;}

  Second type:

#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <string> #include < cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include < queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include < cmath> #include <stack> #include <unordered_map>//#include <tr1/unordered_map> #define Freopenr  Freopen ("In.txt", "R", stdin) #define FREOPENW freopen ("OUT.txt", "w", stdout) using namespace std;//using namespace std:: Tr1;typedef Long Long ll;typedef pair<int, int> p;const int inf = 0x3f3f3f3f;const double inf = 0x3f3f3f3f3f3f;cons T LL LNF = 0x3f3f3f3f3f3f;const Double PI = ACOs ( -1.0); const double EPS = 1e-8;const int maxn = 10005;const LL mod = 10000 000000007;const int N = 1e6 + 5;const int dr[] = {-1, 0, 1, 0, 1, 1,-1, -1};const int dc[] = {0, 1, 0,-1, 1,-1, 1,-1}; const int Hr[]= {-2,-2,-1,-1, 1, 1, 2, 2};const int hc[]= {-1, 1,-2, 2,-2, 2,-1, 1};const char *hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "10 XX "," 1001 "," 1010 "," 1011 "," 1100 "," 1101 "," 1110 "," 1111 "};inline ll gcd (ll A, ll b) {return b = = 0? a:gcd (b, a%b);  }int N, m;const int mon[] = {0, 31, 29, 31, 30, 31, 0, +, +,, +, +, +, +, +, 31};const int monn[] N, H, H, C, h, 31};inline int Min (int a, int b) {return a < b? A:b;} inline int Max (int a, int b) {return a > b a:b;} inline ll Min (ll A, ll b) {return a < b a:b;} inline ll Max (ll A, ll b) {return a > b a:b;} inline bool Is_in (int r, int c) {return R >= 0 && r < n && C >= 0 && C < m;} LL fact[100005]; LL p;    ll Quick_pow (ll A, ll b) {ll ans = 1LL;    A%= p;        while (b) {if (b & 1) ans = ans * a% P;        A = a * a% p;    b >>= 1; } return ans;    ll C (ll N, ll m) {if (n < m) return 0; return Fact[n] * QUICK_POW (Fact[M]*fact[n-m], p-2)% p;}    ll Lucas (ll N, ll m) {if (!m) return 1LL; Return C (n%p, m%p) * Lucas (n/p, m/p)% p;}    int main () {fact[0] = 1;  int T;    Cin >> T;        while (t--) {LL m, n;        scanf ("%i64d%i64d%i64d", &n, &m, &p);        for (int i = 1; i < P; ++i) fact[i] = fact[i-1] * (LL) I% P;    printf ("%i64d\n", Lucas (N+m, M)); } return 0;}

  Third Type:

#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <cstdio> #include <string> #include < cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include < queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include < cmath> #include <stack> #include <unordered_map>//#include <tr1/unordered_map> #define Freopenr  Freopen ("In.txt", "R", stdin) #define FREOPENW freopen ("OUT.txt", "w", stdout) using namespace std;//using namespace std:: Tr1;typedef Long Long ll;typedef pair<int, int> p;const int inf = 0x3f3f3f3f;const double inf = 0x3f3f3f3f3f3f;cons T LL LNF = 0x3f3f3f3f3f3f;const Double PI = ACOs ( -1.0); const double EPS = 1e-8;const int maxn = 10005;const LL mod = 10000 000000007;const int N = 1e6 + 5;const int dr[] = {-1, 0, 1, 0, 1, 1,-1, -1};const int dc[] = {0, 1, 0,-1, 1,-1, 1,-1}; const int Hr[]= {-2,-2,-1,-1, 1, 1, 2, 2};const int hc[]= {-1, 1,-2, 2,-2, 2,-1, 1};const char *hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "10 XX "," 1001 "," 1010 "," 1011 "," 1100 "," 1101 "," 1110 "," 1111 "};inline ll gcd (ll A, ll b) {return b = = 0? a:gcd (b, a%b);  }int N, m;const int mon[] = {0, 31, 29, 31, 30, 31, 0, +, +,, +, +, +, +, +, 31};const int monn[] N, H, H, C, h, 31};inline int Min (int a, int b) {return a < b? A:b;} inline int Max (int a, int b) {return a > b a:b;} inline ll Min (ll A, ll b) {return a < b a:b;} inline ll Max (ll A, ll b) {return a > b a:b;} inline bool Is_in (int r, int c) {return R >= 0 && r < n && C >= 0 && C < m;} LL p;    ll Quick_pow (ll A, ll b) {ll ans = 1LL;    A%= p;        while (b) {if (b & 1) ans = ans * a% P;        A = a * a% p;    b >>= 1; } return ans;    ll C (ll N, ll m) {if (n < m) return 0;    LL a = 1, b = 1; while (m) {a = A * n% P;        b = b * M% p;  --m;    --n; } return a * QUICK_POW (b, p-2)% p;}    ll Lucas (ll N, ll m) {if (!m) return 1LL; Return C (n%p, m%p) * Lucas (n/p, m/p)% p;}  int main () {int T;    Cin >> T;        while (t--) {LL m, n;        scanf ("%i64d%i64d%i64d", &n, &m, &p);    printf ("%i64d\n", Lucas (N+m, M)); } return 0;}

HDU 3037 Saving Beans (number theory, 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.