248913591 Here I use directly pow function in cout; so it gives a value which is in power. But when I store the it one integer varible than it gives direct value. so in contest time or problem solving time if we use directly pow function in cout it will gives error. have anyone opinion about this to ignore this problem using pow function directly in cout .
Wrong Solution
#include<bits/stdc++.h>
using namespace std;
int main(){
int t;
cin>>t;
while(t--){
long long n;
cin>>n;
int a=log2(n);
int ans=pow(2,a);
cout<<ans<<endl;
}
return 0;
}
Right solution
~~~~~ ~~~~~ #include<bits/stdc++.h> using namespace std; int main(){ int t; cin>>t; while(t--){ long long n; cin>>n; int a=log2(n); int ans=pow(2,a); cout<<ans<<endl; } return 0; } ~~~~~ ~~~~~
Auto comment: topic has been updated by Kn0wthyself (previous revision, new revision, compare).
Do not use built in pow function provided by cpp as it does not provide accurate answer for larger powers plus it can cause overflow errors if you want your answer taken modulo with some M (usually 1e9+7), instead use binary exponentiation which provides accurate answer in O(log n) time and it also gives you answer taken modulo with some integer.
You can reffer to this page if you do not know about binary exponentiation.
Use Binary Exponentiation instead. Can refer to the Binary exponentiation code below:
long long binpow(long long a, long long b)
{
}
1) What data type returned by the
pow
function do you see here?2) Do you know how floating point values are printed by cout?