HDU 3037 saving beans fzu 2020 combo hit 2813 garden visiting hrbeu combo fzu 1564 combination

Source: Internet
Author: User

Returns the remainder of a combination.

P is not a prime number, P is a prime number

1) P is a prime number.

  1. Lucas Theorem
  2. M = Mk * P ^ K + MK-1 * P ^ K-1 +... + m1 * P + M0;
  3. N = NK * P ^ K + NK-1 * P ^ K-1 +... + N1 * P + N0;
  4. C (m, n) = C (MK, NK) * C (MK-1, NK-1) *... * C (M1, N1) * C (M0, N0 );
  5. [Topic]
  6. Evaluate the value of C (n + m, n) % P.
  7. Ensure that p is a prime number.

C (m, n) % P = m! /(N! * (M-N )!) % P

In this case, use the inverse element or extend Euclidean.

2) When P is an arbitrary number

 
hdu 3037
Method 1: # include <stdio. h> # define ll long # define nnum limit 1int num [nnum], X, Y; void Init (INT p) {int I; ll Te; num [0] = 1; for (I = 1; I <= P; I ++) {Te = (LL) I; Te = tE * num [I-1] % P; num [I] = (INT) Te ;}} int modular_exp (int A, int B, int c) {ll res, Te; Res = 1, TE = A % C; while (B) {If (B & 1) {res = res * te % C;} te = tE * te % C; B >>=1 ;} return (INT) res;} int gcd (int A, int B) {if (a <B) {A ^ = B, B ^ = A, a ^ = B;} If (B = 0) {return a;} return gcd (B, A % B );} void extend_gcd (int A, int B) {If (B = 0) {x = 1, y = 0; return;} extend_gcd (B, A % B ); int Tx = x; X = Y, y = TX-A/B * Y;} int C (int A, int B, int p) {If (B>) {return 0;} ll Te; Te = (LL) num [B]; Te = tE * num [A-B] % P; B = (INT) Te; A = num [a]; int d = gcd (a, B); A/= D, B/= D; Te = (LL) A; extend_gcd (B, p); X = (X % P + p) % P; Return (I NT) (Te * x % P);} void solve (ll n, ll M, int p) {ll ans; int A, B; ans = 1; while (N | M) {A = n % P, B = m % P; ans = ans * C (A, B, P) % P; N/= P, m/= P;} printf ("% i64d \ n", ANS);} int main () {# ifndef online_judgefreopen ("t.txt", "r", stdin ); # endifint T, P; ll n, m; while (scanf ("% d", & T )! = EOF) {While (t --) {scanf ("% i64d % i64d % d", & N, & M, & P); Init (P ); solve (n + M, m, p) ;}} return 0 ;}
Method 2:
#include<stdio.h>#define LL long long#define nnum 100001int num[nnum], x, y;void init(int p) {    int i;    LL te;    num[0] = 1;    for (i = 1; i <= p; i++) {        te = (LL) i;        te = te * num[i - 1] % p;        num[i] = (int) te;    }}int modular_exp(int a, int b, int c) {    LL res, te;    res = 1, te = a % c;    while (b) {        if (b & 1) {            res = res * te % c;        }        te = te * te % c;        b >>= 1;    }    return (int) res;}int C(int a, int b, int p) {    if (b > a) {        return 0;    }    LL te;    te = (LL) num[b];    te = te * num[a - b] % p;    b = te;    te = num[a];    return (int) (te * modular_exp(b, p - 2, p) % p);}void solve(LL n, LL m, int p) {    LL ans;    int a, b;    ans = 1;    while (n || m) {        a = n % p, b = m % p;        ans = ans * C(a, b, p) % p;        n /= p, m /= p;    }    printf("%I64d\n", ans);}int main() {#ifndef ONLINE_JUDGE    freopen("t.txt", "r", stdin);#endif    int T, p;    LL n, m;    while (scanf("%d", &T) != EOF) {        while (T--) {            scanf("%I64d %I64d %d", &n, &m, &p);            init(p);            solve(n + m, m, p);        }    }    return 0;} 
fzu 2020
#include<stdio.h>#define LL long longint modular_exp(int a, int b, int c) {LL res, te;te = a % c, res = 1;while (b) {if (b & 1) {res = res * te % c;}te = te * te % c;b >>= 1;}return (int) res;}int C(int n, int m, int p) {if (m > n) {return 0;}int i;LL res, a, b;res = 1, a = 1, b = 1;for (i = 0; i < m; i++) {a = a * (n - i) % p, b = b * (m - i) % p;}res = res * a * modular_exp(b, p - 2, p) % p;return (int) res;}void solve(int n, int m, int p) {LL ans;int a, b;ans = 1;while (n || m) {a = n % p, b = m % p;ans = ans * C(a, b, p) % p;n /= p, m /= p;}printf("%I64d\n", ans);}int main() {#ifndef ONLINE_JUDGEfreopen("t.txt", "r", stdin);#endifint T, m, n, p;while (scanf("%d", &T) != EOF) {while (T--) {scanf("%d %d %d", &n, &m, &p);solve(n, m, p);}}return 0;}

Hit 2813

 
#include<stdio.h>#include<math.h>#include<string.h>#define LL long long#define nmax 200001int prime[nmax], flag[nmax], plen;void init() {memset(flag, -1, sizeof(flag));int i, j;for (i = 2, plen = 0; i < nmax; i++) {if (flag[i]) {prime[plen++] = i;}for (j = 0; (j < plen) && (i * prime[j] < nmax); j++) {flag[i * prime[j]] = 0;if ((i % prime[j]) == 0) {break;}}}}int modular_exp(int a, int b, int c) {LL res, te;res = 1, te = a % c;while (b) {if (b & 1) {res = res * te % c;}te = te * te % c;b >>= 1;}return res;}int getNum(int n, int m) {int res;res = 0;while (n) {res += n / m;n /= m;}return res;}void solve(int a, int b, int c) {int i, te;LL res;for (i = 0, res = 1; (i < plen) && (prime[i] <= a); i++) {te = getNum(a, prime[i]) - getNum(b, prime[i])- getNum(a - b, prime[i]);res = res * modular_exp(prime[i], te, c) % c;}printf("%lld\n", res);}int main() {#ifndef ONLINE_JUDGEfreopen("t.txt", "r", stdin);#endifinit();int t, a, b, c, i;while (scanf("%d", &t) != EOF) {for (i = 1; i <= t; i++) {scanf("%d %d %d", &a, &b, &c);solve(a + b - 2, b - 1, c);}}return 0;}

Number of hrbeu combinations

#include<stdio.h>#include<math.h>#include<string.h>#define LL long long#define nmax 100001int prime[nmax], flag[nmax], plen;void init() {memset(flag, -1, sizeof(flag));int i, j;for (i = 2, plen = 0; i < nmax; i++) {if (flag[i]) {prime[plen++] = i;}for (j = 0; (j < plen) && (i * prime[j] < nmax); j++) {flag[i * prime[j]] = 0;if ((i % prime[j]) == 0) {break;}}}}int modular_exp(int a, int b, int c) {LL res, te;res = 1, te = a % c;while (b) {if (b & 1) {res = res * te % c;}te = te * te % c;b >>= 1;}return res;}int getNum(int n, int m) {int res;res = 0;while (n) {res += n / m;n /= m;}return res;}int getMin(int a, int b) {return (a > b ? b : a);}void solve(int a, int b, int c) {int i, te;LL res;for (i = 0, res = 1; (i < plen) && (prime[i] <= a); i++) {te = getNum(a, prime[i]) - getNum(b, prime[i])- getNum(a - b, prime[i]);res = res * modular_exp(prime[i], te, c) % c;}printf("%lld\n", res);}int main() {#ifndef ONLINE_JUDGEfreopen("t.txt", "r", stdin);#endifinit();int t, a, b, c, i;while (scanf("%d", &t) != EOF) {for (i = 1; i <= t; i++) {scanf("%d %d %d", &a, &b, &c);solve(a + b, getMin(a, b), c);}}return 0;}

Fzu 1564.

#include<stdio.h>#include<string.h>#define nmax 1000001int prime[nmax], flag[nmax], plen, mark;void init() {memset(flag, -1, sizeof(flag));int i, j;for (i = 2, plen = 0; i < nmax; i++) {if (flag[i]) {prime[plen++] = i;}for (j = 0; (j < plen) && (i * prime[j] < nmax); j++) {flag[i * prime[j]] = 0;if (i % prime[j] == 0) {break;}}}}int getNum(int n, int m) {int res;res = 0;while (n) {res += n / m;n /= m;}return res;}/* int getNum(int n, int m) { int res; res = 0; while (n) { res += n % m; n /= m; } return res; } */void solve(int a, int b, int c) {int i, cnt, te;for (i = 0; (i < plen) && (prime[i] <= c); i++) {if (c % prime[i] == 0) {cnt = 0;while (c % prime[i] == 0) {c /= prime[i];cnt++;}te = getNum(a, prime[i]) - getNum(b, prime[i])- getNum(a - b, prime[i]);if (te < cnt) {mark = 1;return;}/*te = -getNum(a, prime[i]) + getNum(b, prime[i]) + getNum(a - b, prime[i]); if (te / (prime[i] - 1) < cnt) { mark = 1; return; }*/}}if (mark && (c > 1)) {te = getNum(a, prime[i]) - getNum(b, prime[i])- getNum(a - b, prime[i]);if (te < cnt) {mark = 1;return;}/*te = -getNum(a, c) + getNum(b, c) + getNum(a - b, c); if (te / (c - 1) < cnt) { mark = 1; return; }*/}}int main() {#ifndef ONLINE_JUDGEfreopen("t.txt", "r", stdin);#endifint t, a, b, c;init();while (scanf("%d", &t) != EOF) {while (t--) {scanf("%d %d %d", &a, &b, &c);mark = 0;solve(a, b, c);if (mark) {puts("No");} else {puts("Yes");}}}return 0;}

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.