fzu2020 (c (n,m)%p, where n, M, p (1 <= m <= n <= 10^9, M <= 10^4, M < p < 10^9, p is prime))

Source: Internet
Author: User

The basic template problem is to count the number of P occurrences in the numerator denominator, and then the inverse element is obtained.

////main.cpp//fzu2020////Created by Chenga on 15/12/27.//Copyright (c) 2015 chenhuan001. All rights reserved.//#include<iostream>#include<stdio.h>#include<string.h>#include<string>#include<stdlib.h>using namespaceStd;typedefLong Longll;//ax + by = gcd (A, b)//incoming fixed value A, B. Put back D=GCD (A, B), X, yvoidEXTENDGCD (ll a,ll b,ll &d,ll &x,ll &y) {    if(b==0) {d=a;x=1; y=0;return;} EXTENDGCD (B,a%b,d,y,x); Y-=x* (A/b);}//ax=1 (mod M), gcd (a,m) ==1//Input: 10^18>=a,m>=1//output: The range of return x is [1,m-1]ll Getni (ll A,ll M) {ll Rex=0, rey=0; LL TD=0;    EXTENDGCD (A,m,td,rex,rey); return(rex%m+m)%M;}intMainintargcConst Char*argv[]) {    intT; CIN>>u;  while(t--)    {        intn,m,p; scanf ("%d%d%d",&n,&m,&p); intCnt=0;//count the number of factors p        Long Longup=1;  for(intI=0; i<m;i++)        {            intTN = ni; if(tn%p==0 )            {                 while(tn%p==0) {tn/=p; CNT++; }} up= (up*tn)%p; }                Long Longdn=1;  for(intI=1; i<=m;i++)        {            inttm=i; if(tm%p==0 )            {                 while(tm%p==0) {TM/=p; CNT--; }} DN= (DN*TM)%p; }        if(CNT! =0) {printf ("0\n"); Continue; } cout<< getni (DN, p) *up%p<<Endl; }    return 0;}

fzu2020 (c (n,m)%p, where n, M, p (1 <= m <= n <= 10^9, M <= 10^4, M < p < 10^9, p is prime))

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.