TLE in case of unsigned - time

I was solving a question on spoj and got TLE when I used unsigned long long while it got accepted when I used long long.
Here is the link to the problem.Click Here
Can someone please tell the reason.

I think the possible reason you get a TLE when using unsigned long long is because the question mentions that the end of the input is supposed to be a -1, so when you try to store a -1 in an unsigned long long it will be stored as a positive number (Something like this http://ideone.com/r4Pvp0). So, your loop for taking the user input never ends because you will never encounter a negative number and hence the TLE.
Something similar to your case ::
int main() {
unsigned int a = -10;
if(a < 0)
cout << "Hello";
else
cout << "Bye";
return 0;
}
Just using int a would print Hello, but using unsigned int screws it, and the output would be Bye. See this :: http://ideone.com/99qq2U
So you got to stick to long long if you want to end the user input with a -1.

Related

Count of subsetsum problem, how to debug where the solution is not working?

So I'm trying to solve this problem :
Given an array arr[] of integers and an integer sum, the task is to
count all subsets of the given array with a sum equal to a given sum.
Note: Answer can be very large, so, output answer modulo 10^9+7
Now this is the solution I'm trying out here:
class Solution{
private:
vector<vector<int>> t;
public:
Solution() {
t.resize(1001, vector<int> (1001,-1));
}
int perfectSum(int arr[], int n, int sum)
{
long long int result = fmod(sumrecursive(arr, n, sum),(pow(10,9)+7));
return result;
}
long long int sumrecursive(int arr[], int n, int sum){
if(sum==0){
return 1;
}
if(n==0){
return 0;
}
if(t[n][sum] != -1){
return t[n][sum];
}
if(arr[n-1]>sum){
return t[n][sum] = sumrecursive(arr, n-1, sum);
} else {
return t[n][sum] = sumrecursive(arr,n-1, sum-arr[n-1]) + sumrecursive(arr, n-1, sum);
}
}
};
Now this code is not working after some certain input:
I don't know on how to proceed in solving this problem at this point. Ideally according to the code I have written the input is within the grasp of the code and output should've been correct but unfortunately it is not the case. I wanted to ask if someone could spot on where the problem might be in the code or guide me on how to debug where the problem is in the code.
You are probably encountering integer overflow along the way.
You are taking the mod only right before ending the function, but your cache is of type int, so when placing too big numbers - you are losing some data due to overflow.

Vector with unsigned int

My teacher assigned this question for me, where i have to create a code for fibonacci, i know how to make a fibonacci, but i can't really see what am i going to do with this code that he gave and said that it has to be on my code.
Can anyone make a code that satisfy this?
I'm really trying to understand, but i've tried googleing it, watching some yt tutorials, but couldnt understand what is this supose to do.
#include "function.h"
std::vector<unsigned int> fib_below_n( unsigned int n )
{
// ALL: add your code here
// ALL: This is just a STUB. Change the RETURN for what you judge right.
return std::vector<unsigned int>{};
}
Here's some more details to get you started:
std::vector<unsigned int> retval;
unsigned prev= 1;
unsigned current = 1;
// loop to generate fibonacci numbers
while (current < n) {
retval.push_back (current); /// <<<<< here
// >> generate the next number
}
return retval;

program that converts spelled out numbers in to its digit form. (c++)

I am trying to make a program that converts numbers in digit form to spelled out numbers, and also from spelled-out numbers in to digit numbers (numbers from 0-10). So far my program can do the digit to spelled-out convertion, but I am not sure how to do it the other way around. I am grateful for any help. This is my code so far:
#include "std_lib_facilities.h"
using namespace std;
int main()
{
vector <string> string_val = {"zeroe","one","two","three","four","five","six","seven" ,"eight","nine"};
int integer =0;
string spelled_integer;
cout<<"Enter a number bethween '0' and '10'\n";
while(cin>>integer)
{
if(integer<10)
cout<<"your number in spelled out form is: "<<string_val[integer] <<'\n';
else
cout<<"your number is either to low, or to big, try again.\n";
}
}
As #pm100 suggested in a comment, an std::map is the perfect solution for you. Here is a sample:
int main()
{
std::map<std::string, int> numbers {
{"zero", 0},
{"one", 1},
{"two", 2}
};
std::cout << numbers["two"];
}
The above sample prints 2. For the other way around (the one you've already done) I would also suggest using a map. They are very powerful containers worth exploring.

How to avoid precision problems in C++ while using double and long double variables?

I have a C++ code below,
#include <iostream>
#include <cstdio>
#include <math.h>
using namespace std;
int main ()
{
unsigned long long dec,len;
long double dbl;
while (cin >> dec)
{
len = log10(dec)+1;
dbl = (long double) (dec);
while (len--)
dbl /= 10.0;
dbl += 1e-9;
printf ("%llu in int = %.20Lf in long double. :)\n",dec,dbl);
}
return 0;
}
In this code I wanted to convert an integer to a floating-point number. But for some inputs it gave some precision errors. So I added 1e-9 before printing the result. But still it is showing errors for all the inputs, actually I got some extra digits in the result. Some of them are given below,
stdin
1
12
123
1234
12345
123456
1234567
12345678
123456789
1234567890
stdout
1 in int = 0.10000000100000000000 in long double. :)
12 in int = 0.12000000100000000001 in long double. :)
123 in int = 0.12300000100000000000 in long double. :)
1234 in int = 0.12340000100000000000 in long double. :)
12345 in int = 0.12345000099999999999 in long double. :)
123456 in int = 0.12345600100000000000 in long double. :)
1234567 in int = 0.12345670100000000000 in long double. :)
12345678 in int = 0.12345678099999999998 in long double. :)
123456789 in int = 0.12345679000000000001 in long double. :)
1234567890 in int = 0.12345679000000000001 in long double. :)
Is there any way to avoid or get rid of these errors? :)
No, there is no way around it. A floating point number is basically a fraction with a power of 2 as the denominator. This means that the only non-integers that can be represented exactly are multiples of a (negative) power of 2, i.e. a multiple of 1/2, or of 1/16, or of 1/1048576, or...
Now, 10 has two prime factors; 2 and 5. Thus 1/10 cannot be expressed as a fractional number with a power of 2 as the denominator. You will always end up with a rounding error. By repeatedly dividing by 10, you even make this slightly worse, so one "solution" would be to rather than dividing dbl by 10 repeatedly keeping a separate counter multiplier:
double multiplier = 1;
while (len--)
multiplier *= 10.;
dbl /= multiplier;
Note that I don't say this will solve the problem, but it might make things slightly more stable. Assuming that you can represent a decimal number exactly in floating point remains wrong.

How do the conversions between signed, unsigned and float types work?

The compiler I use is g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4.
I compile my programs with the following command:
g++ -std=c++11 -pedantic -Wall program.cpp
The program no. 1.:
#include <iostream>
using namespace std;
int main() {
unsigned int b;
b = -54;
cout << b << endl;
return 0;
}
The program prints 4294967242 and this is the value I expected, because this is the case when we assign an out-of-range value to a variable of unsigned type, so the result is the remainder of a modulo division.
The program no. 2.:
#include <iostream>
using namespace std;
int main() {
unsigned int b;
b = 54.1234;
cout << b << endl;
return 0;
}
The program prints 54, and this is also OK, because the stored value is the part before the decimal point, and the franctional part is truncated.
The program no. 3.:
#include <iostream>
using namespace std;
int main() {
unsigned int b;
b = -54.1234;
cout << b << endl;
return 0;
}
Here during compilation I get the warning "overflow in implicit constant conversion".
And the program prints 0. Why is it so? I thought that it will do the truncation of the fractional part (as in program 2) and then store the result of the modulo division (as in program 1).
But if I write program no. 4.:
program no. 4.
#include <iostream>
using namespace std;
int main() {
unsigned int b;
float k = -54.1234;
b = k;
cout << b << endl;
return 0;
}
then I get no warning, and I get the result (expected by me) 4294967242, which is the result of the modulo division.
I would be grateful if somebody can explain it to me.
Why doesn't the program no. 3 behave like program no. 4? Why don't I get a warning when compiling program no. 1, but I get one when compiling program no. 3.?
According to the standard (ยง[conv.fpint]).
A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be represented in the destination type.
So, your -54.1234 is truncated to -54. Since that can't be represented in an unsigned, you get undefined behavior.
When converting floating point numbers to integers, C and C++ round floating point numbers towards zero. The rounded result must then be representable in the destination type.
As a result, for 32 bit unsigned int the conversion is guaranteed to give the correct result if -1 < x < 2^32. For smaller numbers there are no guarantees. Since numbers between -1 and 0 must be rounded to zero, and numbers -1 and smaller have no requirements, it wouldn't be surprising if the compiler checks whether x < 0 and gives a result of 0 in that case. (The compiler might check whether x < 1 and give a result of 0; this handles very small positive numbers as well).

Resources