Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // I can't tell you what it really is,
- // I can only tell you what it feels like.
- /*
- #include <ext/pb_ds/assoc_container.hpp>
- using namespace __gnu_pbds;
- typedef tree<int,null_type,less<int>,rb_tree_tag,
- tree_order_statistics_node_update> indexed_set;
- */
- #include <bits/stdc++.h>
- using namespace std;
- #define fast() ios_base::sync_with_stdio(0);cin.tie(NULL);cout.tie(NULL)
- typedef long long ll;
- typedef unsigned long long ull;
- typedef long double ld;
- const long long int N=1e6 + 1;
- const long int x = 1e7;
- #define pb(x) push_back(x)
- #define sd(x) scanf("%d", &(x))
- #define I(x) scanf("%lld" , &x)
- #define A(s) s.begin(),s.end()
- #define L(x) (int)((x).size())
- #define V vector<int>
- #define VL vector<ll>
- #define S set<int>
- #define SL set<ll>
- #define Ml multiset < int >
- #define ML multiset < ll >
- #define in freopen ("in.txt", "r", stdin);
- #define out freopen ("out.txt", "w", stdout);
- #define f(i,p,n) for(int i=(p);i<n;i++)
- #define M map < ll , ll >
- #define F first
- #define SS second
- const long int mod = 1e9 + 7 ;
- 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;}
- //https://tp-iiita.quora.com/
- /*
- Fermat's Little Theorem: If P is a prime number, then for every integer A, according to Fermat’s little theorem,
- AP≡A(modP)
- Further, if A is not divisible by P,
- Ap−1≡1(modP)
- 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.
- Let X=BC,
- Now we have to calculate AX.
- Let X=BC,
- Now we have to calculate AX.
- Suppose Q=X/(M−1) and R=X%(M−1)
- So, AX=AQ∗(M−1)+R
- =AQ∗(M−1)∗AR
- =(AM−1)Q∗AR
- =1Q∗AR
- [Using Fermat’s little theorem, M is prime and A is not divisible by M, so AM−1≡1(modM)]
- =>AX=AR
- 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). */
- int main(int argc, char const *argv[]){
- fast();
- #ifndef ONLINE_JUDGE
- //freopen("in.txt", "r", stdin);
- //freopen("out.txt", "w", stdout);
- #endif
- int t;
- cin >> t;
- while(t--){
- string a , b;
- cin >> a >> b;
- ull res = 0 , num = 0;
- f(i,0,L(a)){
- res = (res * 10 + (a[i] - '0'))%mod;
- }
- f(i,0,L(b)){
- num = (num * 10 + (b[i] - '0'))% (mod - 1);
- }
- ull temp;
- temp = po(res , num , mod);
- cout << temp << '\n';
- }
- return 0;
- }
Add Comment
Please, Sign In to add comment