-
Notifications
You must be signed in to change notification settings - Fork 257
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fast Exponentiation Algorithms Added #2
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include<bits/stdc++.h> | ||
#define ll long long | ||
|
||
using namespace std; | ||
|
||
ll power(ll a,ll n) | ||
{ | ||
ll res=1; | ||
|
||
while(n) | ||
{ | ||
if(n&1) | ||
{ | ||
res=res*a; | ||
n--; | ||
} | ||
a*=a; | ||
n/=2; | ||
} | ||
return res; | ||
} | ||
|
||
int main() | ||
{ | ||
ll a,n; | ||
cin>>a>>n; | ||
cout<<power(a,n); | ||
return 0; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you possibly implement the unit test for this algorithm and put it in the corresponding C++ unit test folder? |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
//for more info refer : https://www.spoj.com/problems/MPOW/ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a meaningful header and also some informative comments to different parts of the code to be more readable. |
||
#include<bits/stdc++.h> | ||
#define ll long long | ||
#define MOD 1000000007 | ||
using namespace std; | ||
#define N 51 | ||
ll mat[51][51],I[51][51]; | ||
void show(ll A[][N],int dim) | ||
{ | ||
for(int i=0;i<dim;i++) | ||
{ | ||
for(int j=0;j<dim;j++) | ||
cout<<A[i][j]<<" "; | ||
cout<<"\n"; | ||
} | ||
|
||
} | ||
void mul(ll A[][N],ll B[][N],int dim) | ||
{ | ||
ll res[dim][dim]; | ||
for(int i=0;i<dim;i++) | ||
{ | ||
|
||
for(int j=0;j<dim;j++) | ||
{ | ||
res[i][j]=0; | ||
for(int k=0;k<dim;k++) | ||
res[i][j]=(res[i][j]%MOD+(A[i][k]%MOD * B[k][j]%MOD)% MOD)%MOD; | ||
} | ||
} | ||
for(int i=0;i<dim;i++) | ||
{ | ||
for(int j=0;j<dim;j++) | ||
{ | ||
A[i][j]=res[i][j]; | ||
} | ||
} | ||
} | ||
void power(ll A[][N],int dim,int n) | ||
{ | ||
for(int i=0;i<dim;i++) | ||
{ | ||
for(int j=0;j<dim;j++) | ||
if(i==j) | ||
I[i][j]=1; | ||
else I[i][j]=0; | ||
|
||
} | ||
while(n) | ||
{ | ||
if(n%2) | ||
mul(I,A,dim),n--; | ||
else | ||
mul(A,A,dim),n/=2; | ||
|
||
} | ||
show(I,dim); | ||
} | ||
|
||
int main() | ||
{ | ||
int t; | ||
cin>>t; | ||
while(t--) | ||
{ | ||
int m,n; | ||
cin>>m>>n; | ||
for(int i=0;i<m;i++) | ||
{ | ||
for(int j=0;j<m;j++) | ||
cin>>mat[i][j]; | ||
|
||
} | ||
power(mat,m,n); | ||
|
||
|
||
} | ||
return 0; | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you possibly implement the unit test for this algorithm and put it in the corresponding C++ unit test folder? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a meaningful header and also some informative comments to different parts of the code to be more readable.