this will be O(log(n) * log(n)) solution
On Fri, Jan 21, 2011 at 4:29 PM, abhijith reddy <[email protected]>wrote:
> Below is code for modular exponentation in general
>
> // (a^b)%c
> int modexp(int a,int b,int c)
> {
> int ans=1;
> while(b)
> {
> if(b&1) ans=(ans*a)%c;
> a=(a*a)%c;
> b>>=1;
> }
> return ans;
>
> }
>
>
> On Fri, Jan 21, 2011 at 3:27 PM, juver++ <[email protected]> wrote:
>
>> int l = 0, r = ...;
>> while (l < r) {
>> int m = (l + r) / 2;
>> int p = power(3, m);
>> if (p > M) r = m - 1;
>> else if (p < M) l = m + 1;
>> else print 3^m = M;
>> }
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected]<algogeeks%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected]<algogeeks%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.