bhupixb

Power of large Numbers

Aug 11th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. // I can't tell you what it really is,
  2. // I can only tell you what it feels like.
  3. /*
  4. #include <ext/pb_ds/assoc_container.hpp>
  5. using namespace __gnu_pbds;
  6. typedef tree<int,null_type,less<int>,rb_tree_tag,
  7. tree_order_statistics_node_update> indexed_set;
  8. */
  9. #include <bits/stdc++.h>
  10. using namespace std;
  11. #define fast() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
  12. typedef long long ll;
  13. typedef unsigned long long ull;
  14. typedef long double ld;
  15. const long long int N=1e6 + 1;
  16. const long int x = 1e7;
  17. #define pb(x) push_back(x)
  18. #define sd(x) scanf("%d", &(x))
  19. #define I(x) scanf("%lld" , &x)
  20. #define A(s) s.begin(),s.end()
  21. #define L(x) (int)((x).size())
  22. #define V vector<int>
  23. #define VL vector<ll>
  24. #define S set<int>
  25. #define SL set<ll>
  26. #define Ml multiset < int >
  27. #define ML multiset < ll >
  28. #define in freopen ("in.txt", "r", stdin);
  29. #define out freopen ("out.txt", "w", stdout);
  30. #define f(i,p,n) for(int i=(p);i<n;i++)
  31. #define M map < ll , ll >
  32. #define F first
  33. #define SS second
  34. const long int mod = 1e9 + 7 ;
  35. ll po(ll a,ll b,ll mod) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
  36. //https://tp-iiita.quora.com/
  37. /*
  38. Fermat's Little Theorem: If P is a prime number, then for every integer A, according to Fermat’s little theorem,
  39.  
  40. AP≡A(modP)
  41.  
  42. Further, if A is not divisible by P,
  43.  
  44. Ap−1≡1(modP)
  45.  
  46. Solution: We cannot use modular exponentiation directly in this problem because BC is itself a very large number and will not fit even in long long. We’ll use Fermat’s little theorem and reduce the problem.
  47.  
  48. Let X=BC,
  49.  
  50. Now we have to calculate AX.
  51. Let X=BC,
  52.  
  53. Now we have to calculate AX.
  54.  
  55. Suppose Q=X/(M−1) and R=X%(M−1)
  56.  
  57. So, AX=AQ∗(M−1)+R
  58.  
  59. =AQ∗(M−1)∗AR
  60.  
  61. =(AM−1)Q∗AR
  62.  
  63. =1Q∗AR
  64.  
  65. [Using Fermat’s little theorem, M is prime and A is not divisible by M, so AM−1≡1(modM)]
  66.  
  67. =>AX=AR
  68.  
  69. Hence, for computing ABC, we first have to compute R=X%(M−1)=BC%(M−1) using modular exponentiation and after that we have to compute AR%M by using modular exponentiation again. Time Complexity will be O(logC+logM). */
  70. int main(int argc, char const *argv[]){
  71. fast();
  72. #ifndef ONLINE_JUDGE
  73. //freopen("in.txt", "r", stdin);
  74. //freopen("out.txt", "w", stdout);
  75. #endif
  76. int t;
  77. cin >> t;
  78. while(t--){
  79. string a , b;
  80. cin >> a >> b;
  81. ull res = 0 , num = 0;
  82. f(i,0,L(a)){
  83. res = (res * 10 + (a[i] - '0'))%mod;
  84. }
  85. f(i,0,L(b)){
  86. num = (num * 10 + (b[i] - '0'))% (mod - 1);
  87. }
  88. ull temp;
  89. temp = po(res , num , mod);
  90. cout << temp << '\n';
  91. }
  92.  
  93.  
  94.  
  95.  
  96.  
  97. return 0;
  98. }
Add Comment
Please, Sign In to add comment