Nick is attracted by everything unconventional. He doesn't like decimal number system any more, and he decided to study other number systems. A number system with base b caught his attention. Before he starts studying it, he wants to write in his notepad all the numbers of length n without leading zeros in this number system. Each page in Nick's notepad has enough space for c numbers exactly. Nick writes every suitable number only once, starting with the first clean page and leaving no clean spaces. Nick never writes number 0 as he has unpleasant memories about zero divide.
Would you help Nick find out how many numbers will be written on the last page.
The only input line contains three space-separated integers b, n and c (2 ≤ b < 10106, 1 ≤ n < 10106, 1 ≤ c ≤ 109). You may consider that Nick has infinite patience, endless amount of paper and representations of digits as characters. The numbers doesn't contain leading zeros.
In the only line output the amount of numbers written on the same page as the last number.
2 3 3
1
2 3 4
4
In both samples there are exactly 4 numbers of length 3 in binary number system. In the first sample Nick writes 3 numbers on the first page and 1 on the second page. In the second sample all the 4 numbers can be written on the first page.
题意:求[ (b-1) *(b)^(n-1)]%c;
思路:指数循环节;
#include<bits/stdc++.h> using namespace std; #define ll __int64 #define esp 0.00000000001 const int N=1e5+10,M=1e6+10,inf=1e9+10; const int mod=1000000007; #define MAXN 10000010 ll quickpow(ll x,ll y,ll z) { ll ans=1; while(y) { if(y&1) ans*=x,ans%=z; x*=x; x%=z; y>>=1; } return ans; } ll phi(ll n) { ll i,rea=n; for(i=2;i*i<=n;i++) { if(n%i==0) { rea=rea-rea/i; while(n%i==0) n/=i; } } if(n>1) rea=rea-rea/n; return rea; } char a[M]; char b[M]; int main() { ll x,y,z,i,t; while(~scanf("%s%s%I64d",b,a,&z)) { t=strlen(b); ll x=0; for(i=0;i<t;i++) x=(x*10+b[i]-'0')%z; if(x==0) x=z; t=strlen(a); ll p=phi(z); ll ans=0; int flag=0; for(i=0;i<t;i++) { ans=(ans*10+a[i]-'0'); if(ans>=p) flag=1; ans%=p; } if(flag) ans+=p; ans--; ll gg=(quickpow(x,ans,z)*(x-1))%z; if(gg) printf("%I64d ",gg); else printf("%I64d ",z); } return 0; }