Topic links
Title Description
Description
Xiao Ming from A1 to An+1, he knew from A1 to A2, from A2 to A3,......, from an to an+1 have M road, and from A1 to An+1 are only these roads. Xiaoming want to know, from A1 to an+1 how many ways, because the answer may be very big, xiaoming as long as you output total program number mod K.
Enter a description
Input Description
Enter a total of 1 rows, three positive integers m,n,k
Output description
Output Description
Output total of 1 lines, indicating the answer
Sample input
Sample Input
3 2 100
Sample output
Sample Output
9
Data range and Tips
Data Size & Hint
Assuming that all roads from A1 to A2 are W1,W2,W3, all the way from A2 to A3 is W4,W5,W6
The scheme is as follows:
W1>>w4
W2>>w4
W3>>w4
W1>>w5
W2>>w5
W3>>w5
W1>>w6
W2>>w6
W3>>w6
A total of 9 options
For 100% of data, m,k≤1,000,000,000,n≤101,000,000
Test instructions is obviously m^n%k, but N is super big, so we use the formula. M^n%k = m^ (N%phi (k) +phi (k))%k. If you come out, it's amazing ...
#include <iostream>#include<vector>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<map>#include<Set>#include<string>#include<queue>#include<stack>#include<bitset>using namespacestd;#definePB (x) push_back (x)#definell Long Long#defineMK (x, y) make_pair (x, y)#defineLson L, M, rt<<1#defineMem (a) memset (a, 0, sizeof (a))#defineRson m+1, R, rt<<1|1#defineMem1 (a) memset (a,-1, sizeof (a))#defineMEM2 (a) memset (a, 0x3f, sizeof (a))#defineRep (i, N, a) for (int i = A; i<n; i++)#defineFi first#defineSe Secondtypedef pair<int,int>PLL;Const DoublePI = ACOs (-1.0);Const DoubleEPS = 1e-8;Const intMoD = 1e9+7;Const intINF =1061109567;Const intdir[][2] = { {-1,0}, {1,0}, {0, -1}, {0,1}};ll p;ll Get_phi (ll N) {ll res=n,i,j; for(i=2; i*i<=n;i++) { if(n%i==0) {n=n/i; while(n%i==0) n=n/i; Res=res/i* (I-1); } if(n< (i+1)) Break; } if(n>1) Res= res/n* (n1); returnRes;} ll Pow (ll A, ll b) {LL ret=1; while(b) {if(b&1) ret= ret*a%p; A= a*a%p; b>>=1; } returnret;}intMain () {intm; stringN; CIN>>m>>n>>p; ll Phi=Get_phi (P); LL TMP=0; for(inti =0; I<n.size (); i++) {tmp= tmp*Ten+n[i]-'0'; TMP%=Phi; } tmp+=Phi; ll ans= POW (1ll*m, tmp)%p; cout<<ans<<Endl; return 0;}
Codevs 1455 Path