Toj 4111 combination number modulo violence decomposition

Source: Internet
Author: User

The main idea: the combination of the number of modulo, N and M is not large, p is large and composite.

Idea: Violent decomposition + fast power

Note: Violence is also different, decomposition factorization can use the following work function, written very ingenious, excerpt from the Internet.

1#include <iostream>2#include <cstring>3 using namespacestd;4 5typedefLong Longll;6 Constll mod = 1ll << +;7 Const intN =1000001;8 Const intM =100007;9 BOOLIs_prime[n];Ten intPrime[m]; One intp; A  - void Get() - { thememset (Is_prime,true,sizeof(Is_prime)); -is_prime[0] = is_prime[1] =false; -      for(inti =2; i < N; i++ ) -     { +         if(Is_prime[i]) -         { +             intj = i *i; A             if(J >= N) Break; at              Do -             { -IS_PRIME[J] =false; -J + =i; -} while(J <N); -         } in     } -p =0; to      for(inti =0; i < N; i++ ) +     { -         if(Is_prime[i]) the         { *prime[p++] =i; $         }Panax Notoginseng     } - } the  + ll Pow_mod (ll A, ll N) A { thell ans =1; +A = a%MoD; -      while(n) $     { $         if(N &1 ) -         { -Ans = ans * a%MoD; the         } -n = n >>1;WuyiA = a * a%MoD; the     } -     returnans; Wu } -  About ll Work (ll N, ll Q) $ { -ll ans =0; -      while(n) -     { AAns + = n/Q; +N/=Q; the     } -     returnans; $ } the  the ll C (ll N, ll M) the { thell ans =1; -      for(inti =0; I < P && Prime[i] <= N; i++ ) in     { thell x =Work (n, Prime[i]); thell y = Work (n-m, Prime[i]); Aboutll z =Work (M, Prime[i]); thex = x-(y +z); theAns = ans * pow_mod (Prime[i], x)%MoD; the     } +     returnans; - } the Bayi intMain () the { the     Get(); -     intT; -CIN >>T; the      while(t-- ) the     { the ll N, m; theCIN >> N >>m; -cout << C (n, m) <<Endl; the     } the     return 0; the}

Toj 4111 combination number modulo violence decomposition

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.