-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
exponetial for very big number.txt
72 lines (67 loc) · 1.62 KB
/
exponetial for very big number.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//question from https://cses.fi/problemset/task/1095
*****FAILED ATTEMPT ****
#include <iostream>
#include <utility>
#include <boost/multiprecision/cpp_int.hpp>
using boost::multiprecision::cpp_int;
using namespace std;
int main()
{
cpp_int n;
cin>>n;
//n=n%1000000007;
cpp_int temp =pow(2,n) ;
cout<<temp % 1000000007;
// cout<<temp;
return 0;
}
*****ANSWER*****
//topic from --- https://www.geeksforgeeks.org/modulo-power-for-large-numbers-represented-as-strings/?ref=rp
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
const ll MOD = 1e9 + 7;
// Returns modulo exponentiation for two numbers
// represented as long long int. It is used by
// powerStrings(). Its complexity is log(n)
ll powerLL(ll x, ll n)
{
ll result = 1;
while (n)
{
if (n & 1)
result = result * x % MOD;
n = n / 2;
x = x * x % MOD;
}
return result;
}
// Returns modulo exponentiation for two numbers
// represented as strings. It is used by
// powerStrings()
ll powerStrings(string sa, string sb)
{
// We convert strings to number
ll a = 0, b = 0;
// calculating a % MOD
for (int i = 0; i < sa.length(); i++)
a = (a * 10 + (sa[i] - '0')) % MOD;
// calculating b % (MOD - 1)
for (int i = 0; i < sb.length(); i++)
b = (b * 10 + (sb[i] - '0')) % (MOD - 1);
// Now a and b are long long int. We
// calculate a^b using modulo exponentiation
return powerLL(a, b);
}
int main()
{
int t;
cin>>t;
while(t--)
{
string sa,sb;
cin>>sa>>sb;
cout << powerStrings(sa, sb) << endl;
}
return 0;
}