How to avoid SEGMENTATION ERROR for the code below? - c++

I'm getting Segmentation error for the code below. This is a solution to the SPOJ problem "Coins".
I went through How to avoid SIGSEGV? and I made sure not to use uninitialized pointers, not to access out of memory etc (given n ≤ 109).
I know that an array a[1000000000] would lead to stack overflow, so I used std::map. Will a std::map ever lead to a stack overflow? What is wrong with my code?
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <map>
using namespace std;
map<unsigned long long int, unsigned long long int> a;
unsigned long long int dp(unsigned long long int n)
{
if (a.find(n) == a.end())
a[n] = dp(n/2) + dp(n/3) + dp(n/4);
return a[n];
}
int main()
{
for (unsigned long long int i = 1; i <= 24; i++) {
a[i] = i;
if (i == 12 || i == 24)
a[i] = i + 1;
}
unsigned long long int n = 0;
cin >> n;
while (!feof(stdin)) {
printf("%llu\n", dp(n));
cin >> n;
}
}

You get SIGSEGV on dp(0) call. It causes an infinite recursion.
By the way, your solution is wrong, for example the answer for 24 is not 25. Try to avoid magic constants, it is just enough to set a[0] = 0 and make a more accurate dp function:
uint32_t dp(uint32_t n) {
if (a.find(n) == a.end())
a[n] = max(n, dp(n / 2) + dp(n / 3) + dp(n / 4));
return a[n];
}
As can be seen above, 32-bit type is enough to store any possible answer.

Related

How to find the factorial of 100 in C++?

I am writing a program to calculate the factorial of 100. The code is as below. Notwithstanding, the output is 0 as the answer is too big. Is there any answer to display the exact answer? This is because even unsigned long long is not even able to display the factorial of 100. Thank you.
#include <iostream>
using namespace std;
int main()
{
int n,i,fact=1;
cout << "enter the number "<<endl;
cin>>n;
for(i=1;i<=n;i++)
{
fact=fact*i;
}
cout<<"the factorial is "<<fact<<endl;
}
This is a rather simple task. We can do it like we would do it on a piece of paper. We use a std::vector of digits to hold the number. Because the result will be already too big for an unsigned long long for 22!.
The answer will be exact.
With such an approach the calculation is simple. I do not even know what to explain further.
Please see the code:
#include <iostream>
#include <vector>
int main()
{
std::cout << "Calculate n! Enter n (max 10000): ";
if (unsigned int input{}; (std::cin >> input) && (input <= 10000)) {
// Here we store the resulting number as single digits
std::vector<unsigned int> result(3000, 0); // Magic number. Is big enough for 100000!
result.back() = 1; // Start calculation with 1 (from right to left)
// Multiply up to the given input value
for (unsigned int count = 2; count <= input; count++)
{
unsigned int sum{}, remainder{};
unsigned int i = result.size() - 1; // Calculate from right to left
while (i > 0)
{
// Simple multiplication like on a piece of paper
sum = result[i] * count + remainder;
result[i--] = sum % 10;
remainder = sum / 10;
}
}
// Show output. Supporess leading zeroes
bool showZeros{ false };
for (const unsigned int i : result) {
if ((i != 0) || showZeros) {
std::cout << i;
showZeros = true;
}
}
}
else std::cerr << "\nError: Wrong input.";
}
Developed and tested with Microsoft Visual Studio Community 2019, Version 16.8.2.
Additionally compiled and tested with clang11.0 and gcc10.2
Language: C++17
You can use C++ Boost Library to to manipulate such large numbers.
Here is the code:
#include <bits/stdc++.h>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using namespace boost::multiprecision;
cpp_int fact(int);
int main(){
cpp_int a=1;
int n;
cin>>n;
cout<<fact(n)<<endl;
}
cpp_int fact(int x){
if(x==1)
return 1;
cpp_int temp=1;
temp= x*fact(x-1);
return temp;
}

can anyone tell me why the fibonacci function does not work?

I want to have it add every once a parameter it has not yet found to the map, but when I try numbers higher than 47 it gives me negative numbers, clearly impossible
#include <map>
using namespace std;
//memoization
map<unsigned int, unsigned int> memo;
map<unsigned int, unsigned int>::iterator it;
int fibonacci(int n)
{
it = memo.find(n);
if (it != memo.end())
{
cout << it->first<<endl;
return memo.at(n);
}
if (n <= 2)
{
return 1;
}
memo.insert({ n, fibonacci(n - 1) + fibonacci(n - 2) });
cout << "----"<<n<<endl;
return memo.at(n);
}
int main()
{
cout<<fibonacci(48);
}
First, let's take care of the negative numbers. Comments above explained that you have an overflow of the 32-bit int.
However, you could stretch your code a bit further would you not convert an unsigned int you calculate to a signed one you return from that function.
The solution is to use larger types, like unsigned long long int, AKA uint64_t
UPDATE
There are a few things sub-optimal in accepted answer.
As I noted in a comment there, the code is searching the map twice: it = memo.find(n); and memo[n];; should just return it->second;
There is no need for the key in that map to be 64-bit wide; 32 bit is enough to overflow 64-bit Fibonacci.
As the order of map keys is not important (you only do insert / lookup), the unordered_map will perform better (constant time instead of logarithmic).
You may notice that the memo is filled in order, and accessed by the index. Much better container here will be a vector with a "free" insert and lookup.
The calling function should NOT fill in memo, as it's not its business.
Here is my version:
#include <vector>
#include <iostream>
//memoization
static std::vector<uint64_t> memo = { 0, 0, 1 };
uint64_t fibonacci(unsigned int n) {
if (n < memo.size())
return memo[n];
memo.push_back(fibonacci(n - 1) + fibonacci(n - 2));
return memo[n];
}
int main() {
std::cout << fibonacci(32'000);
}
but when I try numbers higher than 47 it gives me negative numbers,
clearly impossible
unsigned int size is {0 to 4,294,967,295}, 48th Fibonacci is 4,807,526,976
Fixed code
#include <map>
#include <iostream>
#include <cstdint>
using namespace std;
//memoization
map<int64_t , int64_t> memo;
map<int64_t , int64_t>::iterator it;
int64_t fibonacci(int64_t n) {
it = memo.find(n);
if (it != memo.end()) {
return it->second;;
}
memo.insert({n, fibonacci(n - 1) + fibonacci(n - 2)});
return memo[n];
}
int main() {
memo.insert({0, 0});
memo.insert({1, 1});
memo.insert({2, 1});
cout << fibonacci(50);
}

Find the number of ways the N balls could be placed in the M boxes

You have N different balls numbered from 1 to N, and M different boxes numbered from 1 to M.
Input:
First line of input contains the number of test cases T. After that, next T lines contain the value of N and M.
Output:
For each test case, print the answer. As it can be very large, you should print it modulo 10^9 + 7.
I tried the below code, but it gives an error:
#include<iostream>
#include<cmath>
#include<math.h>
using namespace std;
int main()
{
unsigned short int T;
unsigned long int N,M;
cin>>T;
for (int i = 0; i < T; i++)
{
cin>>N>>M;
long int res;
res= pow(M,N);
int c=0;
c=pow(10,9);
res=res%(c + 7);
cout<<res<<endl;
}
return 0;
}
You must be facing integer overflow problem, that's why you must have been getting wrong answer.
Do the following steps to fix this problem.
change the unsigned long to long long or unsigned long long. (Why? Think).
Use the logarithmic user-defined function to calculate the value of the res = pow(M,N) along with the modulo consideration side-by-side. This will boost up your program.
See my code snippet to check what changes to be made:
#include<iostream>
#define MOD 1000000007
int main() {
unsigned short int T;
unsigned long long N , M , result;
unsigned long long power(unsigned long long, unsigned long long); /*prototype of power*/
std::cin>>T;
for (int i = 0; i < T; i++) {
std::cin >> N >> M;
result = power(M , N);
std::cout << result << std::endl;
}
return 0;
}
unsigned long long power(unsigned long long M, unsigned long long N) {
if(N == 0) {
return 1;
}
unsigned long long result = power(M , N/2);
result = (result * result) % MOD;
if(N%2 == 1) {
result = (result * M) % MOD;
}
return result;
}

Converting an array of 2 digit numbers into an integer (C++)

Is it possible to take an array filled with 2 digit numbers e.g.
[10,11,12,13,...]
and multiply each element in the list by 100^(position in the array) and sum the result so that:
mysteryFunction[10,11,12] //The function performs 10*100^0 + 11*100^1 + 12*100^3
= 121110
and also
mysteryFunction[10,11,12,13]
= 13121110
when I do not know the number of elements in the array?
(yes, the reverse of order is intended but not 100% necessary, and just in case you missed it the first time the numbers will always be 2 digits)
Just for a bit of background to the problem: this is to try to improve my attempt at an RSA encryption program, at the moment I am multiplying each member of the array by 100^(the position of the number) written out each time which means that each word which I use to encrypt must be a certain length.
For example to encrypt "ab" I have converted it to an array [10,11] but need to convert it to 1110 before I can put it through the RSA algorithm. I would need to adjust my code for if I then wanted to use a three letter word, again for a four letter word etc. which I'm sure you will agree is not ideal. My code is nothing like industry standard but I am happy to upload it should anyone want to see it (I have also already managed this in Haskell if anyone would like to see that). I thought that the background information was necessary just so that I don't get hundreds of downvotes from people thinking that I'm trying to trick them into doing homework for me. Thank you very much for any help, I really do appreciate it!
EDIT: Thank you for all of the answers! They perfectly answer the question that I asked but I am having problems incorporating them into my current program, if I post my code so far would you be able to help? When I tried to include the answer provided I got an error message (I can't vote up because I don't have enough reputation, sorry that I haven't accepted any answers yet).
#include <iostream>
#include <string>
#include <math.h>
int returnVal (char x)
{
return (int) x;
}
unsigned long long modExp(unsigned long long b, unsigned long long e, unsigned long long m)
{
unsigned long long remainder;
int x = 1;
while (e != 0)
{
remainder = e % 2;
e= e/2;
if (remainder == 1)
x = (x * b) % m;
b= (b * b) % m;
}
return x;
}
int main()
{
unsigned long long p = 80001;
unsigned long long q = 70021;
int e = 7;
unsigned long long n = p * q;
std::string foo = "ab";
for (int i = 0; i < foo.length(); i++);
{
std::cout << modExp (returnVal((foo[0]) - 87) + returnVal (foo[1] -87) * 100, e, n);
}
}
If you want to use plain C-style arrays, you will have to separately know the number of entries. With this approach, your mysterious function might be defined like this:
unsigned mysteryFunction(unsigned numbers[], size_t n)
{
unsigned result = 0;
unsigned factor = 1;
for (size_t i = 0; i < n; ++i)
{
result += factor * numbers[i];
factor *= 100;
}
return result;
}
You can test this code with the following:
#include <iostream>
int main()
{
unsigned ar[] = {10, 11, 12, 13};
std::cout << mysteryFunction(ar, 4) << "\n";
return 0;
}
On the other hand, if you want to utilize the STL's vector class, you won't separately need the size. The code itself won't need too many changes.
Also note that the built-in integer types cannot handle very large numbers, so you might want to look into an arbitrary precision number library, like GMP.
EDIT: Here's a version of the function which accepts a std::string and uses the characters' ASCII values minus 87 as the numbers:
unsigned mysteryFunction(const std::string& input)
{
unsigned result = 0;
unsigned factor = 1;
for (size_t i = 0; i < input.size(); ++i)
{
result += factor * (input[i] - 87);
factor *= 100;
}
return result;
}
The test code becomes:
#include <iostream>
#include <string>
int main()
{
std::string myString = "abcde";
std::cout << mysteryFunction(myString) << "\n";
return 0;
}
The program prints: 1413121110
As benedek mentioned, here's an implementation using dynamic arrays via std::vector.
unsigned mystery(std::vector<unsigned> vect)
{
unsigned result = 0;
unsigned factor = 1;
for (auto& item : vect)
{
result += factor * item;
factor *= 100;
}
return result;
}
void main(void)
{
std::vector<unsigned> ar;
ar.push_back(10);
ar.push_back(11);
ar.push_back(12);
ar.push_back(13);
std::cout << mystery(ar);
}
I would like to suggest the following solutions.
You could use standard algorithm std::accumulate declared in header <numeric>
For example
#include <iostream>
#include <numeric>
int main()
{
unsigned int a[] = { 10, 11, 12, 13 };
unsigned long long i = 1;
unsigned long long s =
std::accumulate( std::begin( a ), std::end( a ), 0ull,
[&]( unsigned long long acc, unsigned int x )
{
return ( acc += x * i, i *= 100, acc );
} );
std::cout << "s = " << s << std::endl;
return 0;
}
The output is
s = 13121110
The same can be done with using the range based for statement
#include <iostream>
#include <numeric>
int main()
{
unsigned int a[] = { 10, 11, 12, 13 };
unsigned long long i = 1;
unsigned long long s = 0;
for ( unsigned int x : a )
{
s += x * i; i *= 100;
}
std::cout << "s = " << s << std::endl;
return 0;
}
You could also write a separate function
unsigned long long mysteryFunction( const unsigned int a[], size_t n )
{
unsigned long long s = 0;
unsigned long long i = 1;
for ( size_t k = 0; k < n; k++ )
{
s += a[k] * i; i *= 100;
}
return s;
}
Also think about using std::string instead of integral numbers to keep an encrypted result.

Bytelandian gold coins

I am solving this problem -> http://www.spoj.com/problems/COINS/ . A very simple DP problem with a very straight forward DP approach.I found enough hints in the problem statement to use DP. All the test cases are running perfectly in my compiler but I am getting a WA in SPOJ. My code is as follows :
My code
#include <cstdio>
#include <map>
#include <cstring>
#include<algorithm>
using namespace std;
map< long long,long long > data;
map < long long,long long> :: iterator p;
int max(int a,int b)
{
if(a>b)return a;
return b;
}
long long calc(int n)
{
long long c;
if(n==0 || n==1 || n==2)
return n;
p = data.find(n);
if(p==data.end())
{
c = max(n, calc(n/2) + calc(n/3) + calc(n/4));
data.insert(p, pair < long long, long long > (n, c));
return c;
}
else return (*p).second;
}
int main()
{
int t;
long long n;
scanf("%d",&t);
if(t>10)return 0;
while(t--)
{
scanf("%lld",&n);
if(n<0 || n>1000000000)
break;
data.clear();
printf("%lld",calc(n));
}
return 0;
}
I am finding it really difficult for me to figure out where I am going wrong!
A test case which contradicts my code would also do.
Perhaps a stack overflow in calculate. The recursion is killing your program :-)
Or simply the fact that calculate(1000000000) is too much slow.
Use Dynamic programming , store your result in an array . Since the value can go upto 10^9, and you cann't take array of that size , just take array of size till 10^6 and store their result and rest value calculate using simple recursion .
Here is a solution in python
import sys
mydict = {}
def count(n):
if n <= 5:
return n
elif n in mydict.keys():
return mydict[n]
else:
k=max(n,count(n / 2) + count(n / 3) + count(n / 4))
mydict[n]=k
return mydict[n]
for line in sys.stdin:
res = count(int(line))
print(int(res))
Here's my solution, uses dp :
#include<bits/stdc++.h>
using namespace std;
map<long long int,long long int> m;
long long int dp(long long int k){
long long int a;
if(k==0){
return 0;
}
a=m[k];
/* if(k<12){
return k;
} */
if(k<12){
return k;
}
else if(a==0){
a=max(k,dp(k/2)+dp(k/4)+dp(k/3));
m[k]=a;
}
return a;
}
int main(){
long long int n,t;
while(scanf("%lld",&n)>0){
t=dp(n);
cout << t << endl;
}
return 0;
}