fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. //while declaration no need to name the variable
  5. //vector<vector<ll> >v2; ///taking a vector v2
  6. vector<vector<ll> >matmul(vector<vector<ll> >v1,vector<vector<ll> >v2,ll M)
  7. {
  8. int n=v1.size();
  9. vector<vector<ll> >c(n,vector<ll>(n));
  10. for(int i=0;i<n;i++)
  11. {
  12. for(int j=0;j<n;j++)
  13. {
  14. for(int k=0;k<n;k++)
  15. {
  16. c[i][j]=(c[i][j]+v1[i][k]*v2[k][j])%M;
  17. }
  18. }
  19. }
  20. return c;
  21. }
  22. vector<vector<ll> > matpow(vector<vector<ll> >v,ll n,ll M)
  23. {
  24. int size=v.size();
  25. vector<vector<ll> >v2(n,vector<ll>(n));
  26. for(ll i=0;i<n;i++)
  27. v2[i][i]=1;
  28. while(n)
  29. {
  30. if(n%2)
  31. v2=matmul(v2,v,M);
  32. n/=2;
  33. v=matmul(v,v,M);
  34. }
  35. return v2;
  36. }
  37. int main()
  38. {
  39. int t;
  40. ll a,n,m,xx=1;
  41. cin>>t;
  42. for(int i=0;i<t;i++)
  43. {
  44. cin>>a>>n>>m;
  45. vector<vector<ll> >v(2,std::vector<ll>(2)); ///
  46. ll temp=a;int dig=0;
  47. while(temp!=0)
  48. {
  49. dig++;
  50. xx*=10;
  51. temp/=10;
  52. }
  53. v[0][0]=xx%m;
  54. v[0][1]=1;
  55. v[1][0]=0;
  56. v[1][1]=1;
  57. v=matpow(v,n-1,m);
  58. cout<<((v[0][0]+v[0][1])%m*(a%m))%m<<endl;
  59.  
  60. xx=1;
  61. }
  62. return 0;
  63. }
Runtime error #stdin #stdout #stderr 0s 80768KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc