How to solve this on spoj? - c++

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;
}

Related

Why these two binary-to-decimal conversion functions give different results? [duplicate]

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;
}

what is wrong with this code it shows sigtstp Runtime error in codechef compiler

#include<bits/stdc++.h>
using namespace std;
int main(){
int T,N;
cin>>T;
while(T--){
cin>>N;
int arr[N];
for(int i=0;i<N;i++){
cin>>arr[i];
}
sort(arr, arr+N);
int x=arr[0];
int y=arr[N/2];
int z=arr[N-1];
int a=((abs(x-y)+abs(y-z)+abs(z-x)));
cout<<a<<endl;
}
return 0;
}
As mentioned in the comments, you should avoid:
#include<bits/stdc++.h>
using namespace std;
VLA
However, as this kind of site unfortunately promotes this way of writing C++ code, and as N = 10^5 is not so large, it is unlikely that these are the reasons of the run time error.
In practice, the result is equal to 2*(max - min). As the absolute values are less or equal to 10^9, this implies that the result can be as large as 4 10^9 (assuming negative and positive inputs). This is too large for an int.
Moreover, sorting is useless here. You can just calculate the min and max values when reading the A values.
Another consequence is that you don't need any array, and therefore any VLA!

if i am taking A = 10^9 and B=10^9 than following code is giving negative output. why? It is giving me correct output for small integers

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;
}

Working on a solution for large inputs (10^36) of finding the number from given factorial (N)

int main()
{
unsigned long long int i,t,N;
cin>>t; //INPUT NUMBER OF TEST CASE
while(t--)
{
count=0;
cin>>N;
if(N==0 || N==1) //IF FACTORIAL=0 OR 1
cout<<"1"<<endl;
else
{
i=1;
while(N!=1)
{
i++;
N/=i; //REDUCING THE NUMBER
}
cout<<i<<endl;
}
}
return 0;
}
I coded this solution, which gives the correct output for small integers.
I am getting the partially correct output. (Time Limit Exceeded is 2nd case). I think that there can be another approach to solve this question of dynamic programming. Please correct me if I'm wrong.
#include <boost/multiprecision/cpp_int.hpp>
using namespace boost::multiprecision;
uint128_t N = 1;
After including the library in your program.
This stores the input without any problem.
ps: these libraries are not supported by all online judges.

C++ cout will not print [duplicate]

This question already has answers here:
std::cout won't print
(4 answers)
Closed 8 years ago.
What the title says, cout will not print anything! But main finished normally and returns a negative number, what's going on? Here is my code:
#include <iostream>
using namespace std;
unsigned long fibonacci(long unsigned int *);
int main(void)
{
cout<<"IT WILL NOT PRINT!!!!!";
unsigned long int fib[4000000];
cout<<"SUM OF EVEN FIBONACCI NUMBERS: "<<fibonacci(fib)<<endl;
return 0;
}
unsigned long fibonacci(unsigned long int *FIBO)
{
unsigned long i;
int sum=0,c=0, *EVEN = new int[2000000];
FIBO[0]=1; FIBO[1]=2;
for (i=2;i<3999999;i++){
FIBO[i]=FIBO[i-1]+FIBO[i-2];
if (FIBO[i]%2==0){
EVEN[c]=FIBO[i];
sum+=EVEN[c];
c++;
}
}
delete [] EVEN;
return sum;
}
std::cout by default is buffered. Without an explicit flush, you won't see anything printed until the internal buffers need to be flushed. This is done for performance reasons.
You can add a specific flush, like so:
std::cout<<"IT WILL NOT PRINT!!!!!" << std::endl;
That said, you're not seeing output because your program is crashing.
unsigned long int fib[4000000]; is going to require nearly 15MB of space (on a 32bit long int platform). There simply won't be enough stack space to allocate such a large block of memory within that storage duration.
For such large blocks you will need to dynamically allocate the block, or better yet:
std::vector<unsigned long int> fib(4000000);
But main finished normally and returns a negative number,
In your code main returns 0 and that is the only return path. So, the process exiting with a negative return code means that main() never finished.
Most likely, the array unsigned long int fib[4000000]; is too large for your system's default stack size, and the operating system handles this by making your process exit with that negative number.
To fix this, either make that array a lot smaller or remove it entirely; or use dynamic allocation.
Note that there is no need to even have this array (and no need for EVEN either). You're keeping the sum as you go, so you only need to keep the last 2 numbers ; not the entire history.
Let's take a look at this problem: we want to produce a sum of even Fibonacci numbers within a predefined range.
Rearranged with minor edits for clarity:
#include <iostream>
using namespace std;
unsigned long fibonacci(unsigned long int *FIBO)
{
unsigned long i;
int sum=0,c=0, *EVEN = new int[2000000];
FIBO[0]=1; FIBO[1]=2;
for (i=2;i<3999999;i++){
FIBO[i]=FIBO[i-1]+FIBO[i-2];
if (FIBO[i]%2==0){
EVEN[c]=FIBO[i];
sum+=EVEN[c];
c++;
}
}
delete [] EVEN;
return sum;
}
int main(void)
{
unsigned long int fib[4000000];
cout << "SUM OF EVEN FIBONACCI NUMBERS: " << fibonacci(fib) << endl;
return 0;
}
First, let's remove unneeded code in the function fibonacci. We don't need to store even numbers, so we'll remove the EVEN array.
unsigned long fibonacci(unsigned long *FIBO)
{
unsigned long i, sum=0;
FIBO[0]=1; FIBO[1]=2;
for (i=2;i<3999999;i++){
FIBO[i]=FIBO[i-1]+FIBO[i-2];
if (FIBO[i]%2==0){
sum += FIBO[i];
}
}
return sum;
}
We can get a bit more clever though- we can prove that every third Fibonacci number will be even. Starting from Fib(0)=0, this allows us to make a new function:
unsigned long sum_even_fibonacci(unsigned long n)
{
unsigned long i, a=0, b=1, c=1, sum=0;
for (i=0;i<n/3;i++){
a = b + c; // Fib(3i)
b = a + c; // Fib(3i+1)
c = a + b; // Fib(3i+2)
sum += a; // Fib(3i) will always be even- add it to sum.
}
return sum;
}
This should produce the sum of the even Fibonacci numbers within the first n numbers of the series.
The new program, in full:
#include <iostream>
using namespace std;
unsigned long sum_even_fibonacci(unsigned long n)
{
unsigned long i, a=0, b=1, c=1, sum=0;
for (i=0;i<n/3;i++){
a = b + c; // Fib(3i)
b = a + c; // Fib(3i+1)
c = a + b; // Fib(3i+2)
sum += a; // Fib(3i) will always be even- add it to sum.
}
return sum;
}
int main(void)
{
cout << "SUM OF EVEN FIBONACCI NUMBERS: " << sum_even_fibonacci(4000000) << endl;
return 0;
}