fork(1) download
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. // #define mod 1000000007
  5. ll mod,m;
  6. ll f(ll a,ll n, ll mod){
  7. ll res = a, ans =0;
  8. while(n){
  9. if(n%2) ans = (ans + res ) %mod;
  10. res = (res + res)%mod;
  11. n/=2;
  12. }
  13. return ans;
  14. }
  15. ll po(ll a,ll n){
  16. ll res=a, ans=1;
  17. while(n){
  18. if(n%2) ans=f(ans,res,mod);
  19. res=f(res,res,mod);
  20. n/=2;
  21. }
  22. return ans;
  23. }
  24. ll po_thuong(ll a,ll n){
  25. ll res=a, ans=1;
  26. while(n){
  27. if(n%2) ans=ans*res%mod;
  28. res=res*res%mod;
  29. n/=2;
  30. }
  31. return (ans+mod)%mod;
  32. }
  33. ll aka(ll p, ll alpha){
  34. if(alpha==0) return 1;
  35. if(alpha==1) return (p+1)%mod;
  36. if(alpha%2==1) return (f(p,aka(p,alpha-1),mod)+1)%mod;
  37. if(alpha%2==0) return (f((po(p,alpha/2)+1),((aka(p,alpha/2)-1+mod)%mod),mod)+1)%mod;
  38. }
  39. int main(){
  40. // cout<<aka(2,1);
  41. // 1+2+2^2+2^3
  42.  
  43.  
  44.  
  45.  
  46. ll x,n,m;
  47. cin>>x>>n>>m;
  48. ll p,q,len;
  49. mod = m;
  50. len = to_string(x).size();
  51. // cout<<len<<'\n';
  52. p = po(10,len);
  53. // cout<<p<<'\n';
  54. q=x%mod;
  55. // cout<<q<<'\n';
  56. ll f1 = q;
  57. // cout<<po(p,n-1)<<'\n';
  58. // cout<<"thuong: "<<po_thuong(p,n-1)<<'\n';
  59. ll ans = f(po(p,n-1),f1,mod);
  60. // cout<<ans<<'\n';
  61. ll ans1 = n>=2?f(q,aka(p,n-2),mod):0;
  62. // cout<<ans1<<'\n';
  63. ll ress = (ans+ans1)%mod;
  64. cout<<ress;
  65. }
Success #stdin #stdout 0s 4956KB
stdin
5 2 4
stdout
3