This question already has answers here:
Is floating point math broken?
(31 answers)
Why is my integer math with std::pow giving the wrong answer?
(5 answers)
Closed 9 months ago.
This piece of code is taken from a bigger code, where it was creating a problem. I am able to reproduce the same error here.
The code requires unsigned long long integers to deal with large values. The requirement is to convert a given binary number (stored in a vector) to a decimal number (unsigned long long). It is supposed to be a simple code. But it turns out the function configuration_to_int_1 produces wrong result for some binary numbers. If you run the code, you will see that in the very last step of the loop, it adds an even and an odd number to give an even output, which is bizarre!! After spending an entire day to understand the root of this problem, I rewrote the function as configuration_to_int_2
where I have just stored the value of pow(2,i) in a long long integer at the beginning and used this variable whenever pow(2,i) was required. This gives me the correct value!!
Can anyone explain to me, what was going wrong in the first case? Thanks in advance. Here is the code:
#include <stdio.h>
#include <bitset>
#include <vector>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
unsigned long long hash=9007199256641537;
int NLL, Nphi;
NLL=3;
Nphi=18;
vector<unsigned short int> basis(3*Nphi,0);
unsigned long long int configuration_to_int_1(vector<unsigned short int> conf, int NLL, int Nphi);
unsigned long long int configuration_to_int_2(vector<unsigned short int> conf, int NLL, int Nphi);
void int_to_occupation(unsigned long long int num, int Nphi, int NLL, vector<unsigned short int>& occupation);
int_to_occupation(hash, Nphi, NLL, basis);
configuration_to_int_1(basis, NLL, Nphi);
configuration_to_int_2(basis, NLL, Nphi);
return 0;
}
void int_to_occupation(unsigned long long int num, int Nphi, int NLL, vector<unsigned short int>& occupation)
{
bitset<64> bb(num);
for (int r = 0; r < NLL*Nphi; ++r)
{
occupation[r] = bb[r];
}
}
unsigned long long int configuration_to_int_1(vector<unsigned short int> conf, int NLL, int Nphi)
{
unsigned long long int x=0;
for(int i=0;i< NLL*Nphi;i++)
{
if (conf[i]==1)
{
cout<<x<<" + "<<pow(2,i)<<" = ";
x=x+pow(2,i);
cout<<x<<endl;
}
}
return x;
}
unsigned long long int configuration_to_int_2(vector<unsigned short int> conf, int NLL, int Nphi)
{
unsigned long long int x=0,c;
for(int i=0;i< NLL*Nphi;i++)
{
if (conf[i]==1)
{
c=pow(2,i);
cout<<x<<" + "<<c<<" = ";
x=x+c;
cout<<x<<endl;
}
}
return x;
}
Related
reference problem https://www.codechef.com/problems/HBOB02
here in this problem i solved it using below code but firstly i used arrays instead of vector but there i got wrong answer. why so ????
#include <bits/stdc++.h>
#define mod 1000000007
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
long long n,k;
cin>>n>>k;
k=k-1;
vector<long long>arr(n); //if i use long long arr[n] here the solution is not accepted
for(long long i=0;i<n;i++)
{
cin>>arr[i];
}
vector<long long>ar(n); //also changing long long ar[n] here
ar[0]=arr[0];
for(long long i=1;i<n;i++)
{
ar[i]=arr[i]^arr[i-1];
}
long long cnt=0;
for(long long i=0;i<n;i++)
{
if(ar[i]&(1<<k))
cnt++;
}
cout<<cnt;
return 0;
}
Variable-length arrays are not part of the C++ standard, but some compilers provide them as an extension. If they get too large, the program breaks.
However, you don't even need to store all those numbers.
Since you're apparently into "competitive" programming, here is a solution that uses only a tiny fraction of your memory, and does one third as many loop iterations.
int main()
{
unsigned int n, k;
cin >> n >> k;
k -= 1;
int cnt = 0;
unsigned int last = 0;
for(int i = 0; i < n; i++)
{
unsigned int next;
cin >> next;
cnt += ((next^last) >> k) & 1;
last = next;
}
cout << cnt;
}
in>>n>>k;
vector<long long>arr(n); //if i use long long arr[n] here the solution is not accepted
The size of an array variable must be a compile time constant value in C++. For obvious reasons, a value read from user input at runtime is not a compile time constant value. As such, declaring the array suggested in the comment would make the program ill-formed.
A and B are integers ranging from 1 to 10^9 ,
pairs is a variable that contain value of the expression ((A/2)(B/2)+((A-(A/2))(B-(B/2))))
#include<iostream>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
long int A,B;
cin>>A>>B;
//cout<<A<<" "<<B<<endl;
long long int pairs = ((A/2)*(B/2)+((A-(A/2))*(B-(B/2))));
cout<<pairs<<"\n";
}
return 0;
}
In many implementations, a long int in C++ is just a 32 bit number, the max is 2,147,483,647 . So if A is 10^9 and b is also 10^9, their product is beyond the max value of a 32 bit number (in fact A and B can be much smaller than 10^9 such that their product is beyond 2.15 billion). Therefore the product overflowed. As suggested in the comment,
you can change the definition of A and B to long long int
#include<iostream>
using namespace std;
int main()
{
int T;
cin>>T;
while(T--)
{
long long int A,B; // here
cin>>A>>B;
//cout<<A<<" "<<B<<endl;
long long int pairs = ((A/2)*(B/2)+((A-(A/2))*(B-(B/2))));
cout<<pairs<<"\n";
}
return 0;
}
Recently I asked a question but about recursion that gives rise to this problem
Note-> count() The function returns the number of times the key K is present in the map container. It returns 1 if the key is present in the container as the map only contains a unique key. It returns 0 if the key is not present in the map container.
It is passing nearly all the test cases but it is failing 1 000 000 000
according to long long int range value it should pass this one also but it giving
some negative value as output ;
I think this is the problem with container map
Can anyone help me whats wrong with the map?
#include<bits/stdc++.h>
using namespace std;
map <long long int,long long int> dp;
int exchange(long long int n){
if(n<12)
return n;
if(dp.count(n))
return dp[n];
return dp[n] = exchange(n/2)+exchange(n/3)+exchange(n/4);
}
int main(){
// int t;
// cin>>t;
while(1){
// memset(dp,-1,sizeof(dp));
long long int n;
cin>>n;
cout<<exchange(n)<<endl;
}
}
The problem is your function exchange - it returns int, which is a narrower type than long long int.
First, this is bad: #include<bits/stdc++.h>
If you need map, use:
#include <map>
Then as for the types, if you need something that stored more than 2 billions, use a type you can make sure can hold it (especially for the return type!):
#include <iostream>
#include <map>
int64_t exchange(std::map& <int64_t, int64_t>, int64_t n){
if(n<12)
return n;
if(dp.count(n))
return dp[n];
return dp[n] = exchange(n/2)+exchange(n/3)+exchange(n/4);
}
int main(){
std::map <int64_t, int64_t> dp;
while(1){
int64_t n;
std::cin>>n;
std::cout<<exchange(dp, n)<<std::endl;
}
}
I am trying to solve this spoj problem.
Problem Statement : Certain positive integers have their decimal representation consisting only of ones and zeros, and having at least one digit one, e.g. 101. If a positive integer does not have such a property, one can try to multiply it by some positive integer to find out whether the product has this property.
I am trying BFS approach by starting with 1(Since first number should be one) and each step i am queuing num*10(to append a zero) and num*10+1(to append a one) and checking whether it is divisible by the input number.
I think my approach is right but i could not handle large integers. Already i am using unsigned long long int. I read few SO articles and forums for help but couldn't understand them. What i came to know was there was a way to do this without storing the entire number.
Could some one throw some light on it?
Below is the code I've tried
#include<iostream>
#include<cstdlib>
#include<sstream>
#include<queue>
using namespace std;
unsigned long long int BFS(unsigned long long int s,unsigned long long int k)
{
unsigned long long int x;
queue<unsigned long long int>q;
q.push(s);
while(!q.empty())
{
x=q.front();
q.pop();
if(x%k==0)
{
return x;
}
q.push(x*10);
q.push(x*10+1);
}
}
int main()
{
unsigned long long int t,n;
cin>>t;
while(t--)
{
cin>>n;
cout<<BFS(1,n)<<endl;
}
return 0;
}
Here's an example:
This function won't work with big numbers such as 900000000 but will work with smaller numbers like 800
code:
#include <vector>
#include <iostream>
#include <string> using namespace std;
long long int largest_prime_factor(long long int num){
vector<long long int >factors;
vector<long long int>primes;
for(long long int i=1;i<=num;i++){
if(num%i==0){
for(long long int a =1;a<=i;a++){
if(i%a==0){
factors.push_back(a);
}
}
if( (factors.size() == 2) && (factors[0] ==1)){
primes.push_back(i);
}
factors.clear();
}
}
reverse(primes.begin(),primes.end());
return primes[0]; }
int main(){
cout<<largest_prime_factor(600851475143)<<endl;
}
Try invoking the function with the following code:
cout<<largest_prime_factor(600851475143LL)<<endl;
If you simply mention the literal 600851475143, it wont be recognized as long long. you have to explicitly tell the compiler that treat the literal as long long by appending 'LL' at the end.