-3

The below code is to calculate 2^n where n is equal to 1 <= n <= 10^5. So to calculate such large numbers I have used concept of modular exponentian. The code is giving correct output but due to large number of test cases it is exceeding the time limit. I am not getting a way to minimize the solution so it consumes less time. As the "algo" function is called as many times as the number of test cases. So I want to put the logic used in "algo" function in the main() function so it consumes time less than 1 sec and also gives the correct output. Here "t" represents number of test cases and it's value is 1 <= t <= 10^5.

Any suggestions from your side would be of great help!!

#include<iostream>
#include<math.h>

using namespace std;

int algo(int x, int y){
  long m = 1000000007;
  if(y == 0){
    return 1;
  }

  int k = algo(x,y/2);

  if (y % 2 == 1){
    return ((((1ll * k * k) % m) * x) % m);
  } else if (y % 2 == 0){
    return ((1ll * k * k) % m);
  }
  
}

int main(void)
{
    int n, t, k;
    cin>>t; //t = number of test cases
    for ( k = 0; k < t; k++)
    {
        cin >> n;  //power of 2 

        cout<<"the value after algo is: "<<algo(2,n)<<endl;

    }
    return 0;
}
Lars Nielsen
  • 2,005
  • 2
  • 25
  • 48
  • @UlrichEckhardt - my thoughts exactly. That's why I just ran it through a pretty-printer. – selbie May 10 '21 at 07:48
  • 1
    10^5 isn't that big, have you considered memoization or even precalculation of all the possible results? – Bob__ May 10 '21 at 08:02
  • Use `'\n'` instead of `endl`. The latter causes a flush, which might slow down anything consuming the data (especially if the data is being written to a file on a slow disk or over a network). It seems a bit strange to use recursion to solve this problem, too. Stylistically, you should not use `else if (y % 2 == 0)` -- instead, just use `else`, or simply nothing at all. – paddy May 10 '21 at 08:02
  • (a ⋅ b) mod m = [(a mod m) ⋅ (b mod m)] mod m (from wikipedia) you can surely leverage this identity to do much better than that. https://en.wikipedia.org/wiki/Modular_exponentiation – Alessandro Teruzzi May 10 '21 at 08:06

3 Answers3

0

You can make use of binary shifts to find powers of two

#include <iostream>
using namespace std;
int main()
{
    unsigned long long u = 1, w = 2, n = 10, p = 1000000007, r;
    //n -> power of two 
    while (n != 0)
    {
        if ((n & 0x1) != 0)
            u = (u * w) % p;

        if ((n >>= 1) != 0)
            w = (w * w) % p;
    }
    r = (unsigned long)u;
    cout << r;
    return 0;
}
Tharun K
  • 1,160
  • 1
  • 7
  • 20
0

This is the function that I often use to calculate
Any integer X raised to power Y modulo M

C++ Function to calculate (X^Y) mod M

int power(int x, int y, const int mod = 1e9+7)
{
    int result = 1;
    x = x % mod;
    if (x == 0)
        return 0;
    while (y > 0)
    {
        if (y & 1)
            result = ( (result % mod) * (x % mod) ) % mod;
        y = y >> 1; // y = y / 2
        x = ( (x % mod) * (x % mod) ) % mod;
    }
    return result;
}
  • Remove the Mod if you don't want.
  • Time Complexity of this Function is O(log2(Y))
  • There can be a case of over flow so use int , long , long long etc as per your need.
Mr.HITMAN
  • 157
  • 11
-1

Well your variables won't sustain the boundary test cases, introducing 2^10000, 1 <= n <= 10^5. RIP algorithms

19950631168807583848837421626835850838234968318861924548520089498529438830221946631919961684036194597899331129423209124271556491349413781117593785932096323957855730046793794526765246551266059895520550086918193311542508608460618104685509074866089624888090489894838009253941633257850621568309473902556912388065225096643874441046759871626985453222868538161694315775629640762836880760732228535091641476183956381458969463899410840960536267821064621427333394036525565649530603142680234969400335934316651459297773279665775606172582031407994198179607378245683762280037302885487251900834464581454650557929601414833921615734588139257095379769119277800826957735674444123062018757836325502728323789270710373802866393031428133241401624195671690574061419654342324638801248856147305207431992259611796250130992860241708340807605932320161268492288496255841312844061536738951487114256315111089745514203313820202931640957596464756010405845841566072044962867016515061920631004186422275908670900574606417856951911456055068251250406007519842261898059237118054444788072906395242548339221982707404473162376760846613033778706039803413197133493654622700563169937455508241780972810983291314403571877524768509857276937926433221599399876886660808368837838027643282775172273657572744784112294389733810861607423253291974813120197604178281965697475898164531258434135959862784130128185406283476649088690521047580882615823961985770122407044330583075869039319604603404973156583208672105913300903752823415539745394397715257455290510212310947321610753474825740775273986348298498340756937955646638621874569499279016572103701364433135817214311791398222983845847334440270964182851005072927748364550578634501100852987812389473928699540834346158807043959118985815145779177143619698728131459483783202081474982171858011389071228250905826817436220577475921417653715687725614904582904992461028630081535583308130101987675856234343538955409175623400844887526162643568648833519463720377293240094456246923254350400678027273837755376406726898636241037491410966718557050759098100246789880178271925953381282421954028302759408448955014676668389697996886241636313376393903373455801407636741877711055384225739499110186468219696581651485130494222369947714763069155468217682876200362777257723781365331611196811280792669481887201298643660768551639860534602297871557517947385246369446923087894265948217008051120322365496288169035739121368338393591756418733850510970271613915439590991598154654417336311656936031122249937969999226781732358023111862644575299135758175008199839236284615249881088960232244362173771618086357015468484058622329792853875623486556440536962622018963571028812361567512543338303270029097668650568557157505516727518899194129711337690149916181315171544007728650573189557450920330185304847113818315407324053319038462084036421763703911550639789000742853672196280903477974533320468368795868580237952218629120080742819551317948157624448298518461509704888027274721574688131594750409732115080498190455803416826949787141316063210686391511681774304792596709376

Fear not my friend, someone did tried to solve the problem https://www.quora.com/What-is-2-raised-to-the-power-of-50-000, you are looking for Piyush Michael's answer , here is his sample code

#include <stdio.h> 
int main() 
{ 
 int ul=16,000; 
 int rs=50,000; 
 int s=0,carry[ul],i,j,k,ar[ul]; 
 ar[0]=2; 
 for(i=1;i<ul;i++)ar[i]=0; 
 for(j=1;j<rs;j++) 
 {for(k=0;k<ul;k++)carry[k]=0; 
  for(i=0;i<ul;i++) 
  {ar[i]=ar[i]*2+carry[i]; 
   if(ar[i]>9) 
   {carry[i+1]=ar[i]/10; 
    ar[i]=ar[i]%10; 
   } 
  } 
 } 
 for(j=ul-1;j>=0;j--)printf("%d",ar[j]); 
 for(i=0;i<ul-1;i++)s+=ar[i]; 
 printf("\n\n%d",s); 
}
saumitra mallick
  • 395
  • 2
  • 11