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))