Zoj 3557 how many sets II Lucas Theorem

Source: Internet
Author: User

The intervoid method is used to obtain the remainder of a large group.

# Include <cstdio> # include <cstring> using namespace STD; typedef long ll; // evaluate the integer x and y, so that ax + by = D, and | x | + | Y | minimum. Where d = gcd (a, B) void gcd (ll a, LL B, ll & D, ll & X, ll & Y) {If (! B) {d = A; X = 1; y = 0;} else {gcd (B, A % B, D, Y, X ); y-= x * (a/B);} // calculate the inverse of a under the modulo n. If there is no inverse, return-1 LL inv (ll a, ll N) {ll D, X, Y; gcd (A, N, D, x, y ); return d = 1? (X + n) % N:-1;} ll cm (ll n, ll M, ll p) {ll ans1 = 1, ans2 = 1; while (m) {ans1 = ans1 * n % P; ans2 = ans2 * m % P; n --; m --;} return ans1 * inv (ans2, p) % P ;} ll Lucas (ll n, ll M, ll p) {If (M = 0) return 1; return Lucas (N/P, M/P, P) * cm (N % P, M % P, P) % P;} int main () {ll n, m, p; while (scanf ("% LLD", & N, & M, & P )! = EOF) {printf ("% LLD \ n", Lucas (n-2 * m + 1 + M, m, p);} return 0 ;}


Zoj 3557 how many sets II 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.