Guest User

Solution to KBIGNUM

a guest
Feb 6th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.09 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define sz(a) int((a).size())
  3. #define pb push_back
  4. #define eb emplace_back
  5. #define mp make_pair
  6. #define mt make_tuple
  7. #define F first
  8. #define S second
  9. #define ld(a) while(a--)
  10. #define tci(v,i) for(auto i=v.begin();i!=v.end();i++)
  11. #define tcf(v,i) for(auto i : v)
  12. #define all(v) v.begin(),v.end()
  13. #define rep(i,start,lim) for(long long (i)=(start);i<(lim);i++)
  14. #define repd(i,start,lim) for(long long (i)=(start);i>=(lim);i--)
  15. #define present(c,x) (find(all(c),x) != (c).end())
  16. #define sync ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  17. #define osit ostream_iterator
  18. #define INF         0x3f3f3f3f
  19. #define LLINF       1000111000111000111LL
  20. #define PI          3.14159265358979323
  21. #define endl '\n'
  22. #ifdef WIN32
  23. #define getchar_unlocked getchar
  24. #endif
  25. #define gc getchar
  26. #define trace1(x)                cerr<<#x<<": "<<x<<endl
  27. #define trace2(x, y)             cerr<<#x<<": "<<x<<" | "<<#y<<": "<<y<<endl
  28. #define trace3(x, y, z)          cerr<<#x<<":" <<x<<" | "<<#y<<": "<<y<<" | "<<#z<<": "<<z<<endl
  29. #define trace4(a, b, c, d)       cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<endl
  30. #define trace5(a, b, c, d, e)    cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<endl
  31. #define trace6(a, b, c, d, e, f) cerr<<#a<<": "<<a<<" | "<<#b<<": "<<b<<" | "<<#c<<": "<<c<<" | "<<#d<<": "<<d<<" | "<<#e<< ": "<<e<<" | "<<#f<<": "<<f<<endl
  32. #define N 1000006
  33. using namespace std;
  34. typedef vector<int> vi;
  35. typedef vector<vi> vvi;
  36. typedef long long int ll;
  37. typedef vector<long long int> vll;
  38. typedef vector<vll> vvll;
  39. typedef long double ld;
  40. typedef pair<int,int> ii;
  41. typedef vector<ii> vii;
  42. typedef vector<vii> vvii;
  43. typedef tuple<int,int,int> iii;
  44. typedef set<int> si;
  45. typedef complex<double> pnt;
  46. typedef vector<pnt> vpnt;
  47. typedef priority_queue<ii,vii,greater<ii> > spq;
  48. const ll MOD=100000000000007LL;
  49. template<typename T> T sqr(T x){return x*x;}
  50. template<typename T> T gcd(T a,T b){if(a==0) return b; return gcd(b%a,a);}
  51. template<typename T>void read(T &x) { register T c = gc(); x = 0; int t = 0; if (c == '-') t = 1, c = gc(); for (; (c < 48 || c>57); c = gc()); for (; c > 47 && c < 58; c = gc()) { x = (x << 1) + (x << 3) + c - 48; }if (t) x = -x; }
  52. template<typename T> T power(T x,T y,ll m=MOD){T ans=1;while(y>0){if(y%2==1) ans=(ans*x)%m;y/=2;x=(x*x)%m;}return ans%m;} //using this instead of inbuilt pow due to precision issues in that
  53. template<typename T> ll roundp(T x){ll ans=x;if(x-floor(x)<=0.001) ans=floor(x);else if(ceil(x)-x<=0.001) ans=ceil(x);return ans;}
  54.  
  55. ll solve(ll x,ll n,ll m){
  56. //  trace3(x,n,m);
  57.     if(n==0) return 1LL;
  58.     if(n==1) return (1LL+x)%m;
  59.     if(n%2==0){
  60.         ll t1=solve((x*x)%m,n/2LL-1LL,m);
  61.         t1=(t1*(1LL+x))%m;
  62.         t1=(t1+power(x,n,m))%m;
  63.         return t1;
  64.     }
  65.     else{
  66.         ll t1=solve((x*x)%m,n/2LL,m);
  67.         t1=(t1*(1LL+x))%m;
  68.         return t1;
  69.     }
  70. }
  71. int main(){
  72.     sync;
  73.     int t; cin>>t;
  74.     ld(t){
  75.         ll a,n,m,res,siz; string s;
  76.          cin>>a>>n>>m;
  77.         res=a%m;
  78.         s=to_string(a); siz=s.size();
  79.         ll ans=solve(power(10LL,siz),n-1,m);
  80.     //  trace2(ans,res);
  81.         cout<<(ans*res)%m<<endl;
  82.     }
  83. }
Add Comment
Please, Sign In to add comment