Bug in the following program could not be fixed C++ - c++

It might not be like other asked questions in stackoverflow. In this problem, it works fine, but in one case, it returns wrong answer. I'm trying to solve the logical issue of this program.
I wrote a program to calculate the sum of this:
x, n, a would be entered by the user:
Here is my program:
#include <iostream>
long long int unsigned fact (long long unsigned int a);
long long int unsigned comb (long long unsigned int n, long long unsigned int r);
long long unsigned intpower (long long unsigned int a, long long unsigned int n);
using namespace std;
int main()
{
int n;
long long unsigned int x, a;
cin >> a >> x >> n;
long long unsigned int sum = 0;
for (int i = 0; i <= n; i++) {
sum += comb(n, i)*intpower(x, i)*intpower(a, (n-i));
}
cout << sum;
return 0;
}
// Calculates Factorial
long long int unsigned fact (long long unsigned int a) {
long long int unsigned p = 1;
for (long long unsigned int i = 1; i <= a; i++) {
p *= i;
}
return p;
}
// Calculates the combination
long long int unsigned comb (long long unsigned int n, long long unsigned int r) {
return (fact(n)/fact(r)/fact(n-r));
}
long long unsigned intpower (long long unsigned int a, long long unsigned int n){
long long unsigned int p = 1;
for (long long unsigned int i = 1; i <=n ; i++){
p *= a;
}
return p;
}
But in one case, my program returns wrong answer. Here's the test done my a website that verifies the written programs for problems:
Do you guys have any idea why I got wrong answer in one test? The thing is I don't know what numbers would be entered in test 1, but there should be a logical issue that it gives wrong answer in one case.
Kind regards.

As the comments have pointed, the failing test case is most probably because of a corner-side with the maximum values of the inputs. The range that you can store in a long long int data type (if your compiler support the type) is from -9,223,372,036,854,775,807 to 9,223,372,036,854,775,807. It means that if in your case you have x, a and n as their maximum values, you will have an overflow. For an example, the output of your code with the following inputs is the same:
for:
int n = 10;
long long unsigned int x = 9999999999;
long long unsigned int a = 1000000000;
output is: 9223372036854775808
int n = 10;
long long unsigned int x = 1000000000;
long long unsigned int a = 1000000000;
the output is again: 9223372036854775808

Related

c++ segmentation fault, why using "long long" I dont get an answer?

Can somebody explain where the mistake is in this code? because when I use parameters like this a=425 b=9631 n=9876543215 I get "exited,segmentation fault code 139" :(
#include <iostream>
#include <iomanip>
using namespace std;
void ivedimas(long long &n, long long &a, long long &b);
long long fib(long long n, long long a, long long b);
void isvedimas(long long ats);
int main()
{
long long n,a,b;
ivedimas(n,a,b);
isvedimas(fib(n,a,b));
return 0;
}
void ivedimas(long long &n, long long &a, long long &b)
{
cin>>a>>b>>n;
}
long long fib(long long n,long long a, long long b)
{
long long c=b-a;
if (n==2)
return c;
return fib(n-1,b,c);
}
void isvedimas(long long ats)
{
cout<<ats<<endl;
}
Stack Overflow: every time the function calls itself it adds to the stack, so this solution works for small numbers, but anything too large it will fail.
Iterative solution:
long long fib(long long n, long long a, long long b)
{
if (n == 0) {return 0;}
if (n == 1) {return 1;}
long long t = 0;
long long j = 1;
for (int i = 2; i <= n; i++) {
int k = j;
j += t;
t = k;
}
return j;
}
This recursive version of Fibonacci is not good with large number. Each time it's called the recursion the stack increse and never decrese till the end. So you just terminate the process memory before the result is return.
You can try an iterative solution.
thank you, this is the solution:)) every 6th number in the sequence is the same :)
e.g. 10,8,-2,-10,-8,2|10,8,-2,-10,-8,2...
long long fib(long long n,long long a, long long b)
{ long long c=a;
if (n==0) return a;
if (n%6==1) return b;
for (int i=1;i<n%6;i++)
{
c=b-a;
a=b;
b=c;
}
return c;
}

Calculation of digits in 10^9 in C++

I was trying to find the lcm of two numbers and for one of the input cases (28851539 and 1183019) my program returns a negative value . Apparently it is not able to compute (28851529*1183019)/9 .
#include <iostream>
long long gcd(int a, int b) {
long long int temp;
if(a%b==0)
{
return b;
}
else
{
temp=a%b;
return gcd(b,temp);
}
}
long long lcm(int a, int b , int g) {
//std::cout<<g;
long long int f=(a*b)/g;
return f;
}
int main() {
long long int a, b;
std::cin >> a >> b;
long long int g = gcd(a,b);
long long int q=lcm(a, b, g);
std::cout << q << std::endl;
return 0;
}
How do i compute that accurately ?
You problem is
long long int f=(a*b)/g;
since all of the types in (a*b)/g are int then this will be calculated as an int and if an int is 16 or 32 bits then it will overflow. Do note that since you have signed types this is actually undefined behavior. To get around this you either need to make a, b or g a long long int or you can change the parameters of the function to make them all long long ints.
long long int lcm(long long int a, long long int b , long long int g)
I would also suggest you use an unsigned long long int if you are not dealing with negative numbers. If you are not then you can use the type uint64_t from <cstdint> otherwise int64_t to make the type names shorter.

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

Printing (Factorial of 2^n)/(2^n -1)mod m in C++

How can we store and print factorial(2^n) / (2^n -1))mod1000000009 in C++.Here n can be as large as 20. When I try to print this using the following code, it shows segmentation fault for n=20
#include
#include
using namespace std;
long int factorial(int n)
{
if(n<=1){return 1;}
else
return (n%1000000009)*(factorial(n-1))%1000000009;
}
int main()
{
int K;
long long int numofmatches=0;
long long int denominator=0;
long long int factor=0;
long long int times=0;
long long int players=0;
cin>>K;
if(K==1)
{
cout<<2<<endl<<2<<endl;
return 0;
}
else
{
denominator=pow(2,K);
cout<<"Denominator="<<denominator<<endl;
numofmatches=factorial(denominator)%1000000009;
denominator-=1;
cout<<"numberofmatches="<<numofmatches<<endl;
cout<<"Denominator="<<denominator<<endl;
factor=numofmatches/denominator;
cout<<"Factor="<<factor<<endl;
while(times<=denominator)
{
cout<<(times*factor)<<endl;
++times;
}
}
return 0;
}
First of all, note that (2^n)! / (2^n-1) is equal to (2^n-2)! x 2^n.
Now, (2^20-2)! by itself is already an extremely large number to calculate.
What you can do instead, is to modulo the intermediate result with 1000000009 after every multiplication:
#define MAX ((1<<20)-2)
unsigned long long res = 1;
for (unsigned int i=1; i<=MAX; i++)
res = (res*i)%1000000009;
res = (res*(MAX+2))%1000000009;
If you want to iterate all values of n between 1 and 20, then you can use:
#define MAX_N 20
unsigned int arr[MAX_N+1] = {0};
void Func()
{
unsigned int i = 1;
unsigned long long res = 1;
for (int n=1; n<=MAX_N; n++)
{
unsigned int max = (1<<n)-2;
for (; i<=max; i++)
res = (res*i)%1000000009;
arr[n] = (unsigned int)((res*(max+2))%1000000009);
}
}
BTW, for any n larger than 29 the result will simply be 0, as (2^30-2) is larger than 1000000009.
So (2^30-2)! is divisible by 1000000009, and therefore, (2^30-2)! mod 1000000009 equals 0.

Curious behaviour of inverse modulo

I wrote the following code to calculate n!modulo p...Given that n and p are close...but its running in a rather funny way, cant figure out the bug..There is some overflow somewhere..The constraints are 1 < P <= 2*10^9
1 <= N <= 2*10^9
though it runs fine for few cases...what could be the error.I have used
(a/b)mod p = ((a mod p)*(b^(p-2))mod p)mod p
as p is prime....and wilsons theorem that (p-1)! mod p = p-1
#include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
unsigned int pow(unsigned int a, unsigned n,unsigned int p) {
unsigned int ret = 1;
while(n) {
if((n&1)== 1) ret=(ret*a)%p;
a=a%p;
a=(a*a)%p;
n=n>>1;
}
return ret;
}
int main(){_
int t;
cin>>t;
while(t--){
unsigned int n,p;
long long int r;
cin>>n>>p;
r=p-1;
if(n>=p){
cout<<"0\n";
}
else{
for(unsigned int i=p-1;i>n;i--){
r=((long long)r*pow(i,p-2,p))%p;
}
cout<<r<<"\n";
}
}
return 0;
}
21! is 51090942171709440000, while 2^64 is only 1844674407370955161: so if unsigned long long is a 64-bit quantity (as is likely), it doesn't fit.