I'm trying to understand why this programm after 2^20-1 value goes in overflow. All my variables are declared unsigned long long, but when I enter 1048756 which is 2^20 it goes in overflow , instead of converting it in a binary number. I thought that the range of u-l-l was 2^64-1.
I included the limits.h library and the maximum value was 8 bytes.This is the code :
#include <stdio.h>
int main(){
unsigned long long n = 100000000;
printf("%llu \n",decimal_binary(n));
return 0;
}
unsigned long long decimal_binary(unsigned long long n)
{
unsigned long long rem, i=1, binary=0;
while (n!=0)
{
rem=n%2;
n/=2;
binary+=rem*i;
i*=10;
}
return binary;
}
And the output is :
14184298036271661312 (Which is not a binary number obviously)
18446744073709551615 // 2^64-1
100000000000000000000 // 2^20 in your funny "decimal binary"
See the problem now?
By the way, if you want to get platform dependence out of this, use uint64_t from stdint.h instead of unsigned long long.
It seems like what you really want is to output a number in binary format. You can't put the conversion back into an integer type like that. You need to construct a string:
void decimal_binary(unsigned long long n, char str[])
{
unsigned long long rem, len=0, temp, i;
while (n!=0)
{
rem=n%2;
n/=2;
// put the binary digit into the string
str[len++] = rem ? '1' : '0';
}
str[len] = '\x0';
// the digits were inserted in reverse order, so reverse the string.
for (i=0;i<=len/2;i++) {
temp = str[i];
str[i] = str[len-1-i];
str[len-1-i] = temp;
}
}
int main(void){
char buff[200];
unsigned long long n = 100000000;
decimal_binary(n,buff);
printf("%s \n",buff);
return 0;
}
Output:
101111101011110000100000000
Let your code detect about when math is getting too big.
#include <stdio.h>
#include <limits.h>
unsigned long long decimal_binary(unsigned long long n)
{
unsigned long long rem, i=1, binary=0, n0;
n0 = n;
while (n!=0)
{
rem=n%2;
n/=2;
binary+=rem*i;
if (i > ULLONG_MAX/10) {
printf("OF %llu %llu\n", n0 , i);
return 0;
}
i*=10;
}
return binary;
}
int main(){
unsigned long long n = 100000000;
printf("%llu %llu\n",n,decimal_binary(n));
n = 100000;
printf("%llu %llu\n",n, decimal_binary(n));
return 0;
}
Output
OF 100000000 10000000000000000000
100000000 0
100000 11000011010100000
main()
{
unsigned long long n = 100000000;
int i;
for(i=sizeof(unsigned long long int)*8-1;i>=0;printf("%llu",n>>i--&1));`
}
OUTPUT::00000000 00000000 00000000 00000000 00000101 11110101 11100001 00000000
Related
This is from a dp question and the logic seems to work for unsigned int but shows the following error when using long long int even though long long int is much bigger than unsigned int.
Error: signed integer overflow: 83700400893462717 + 9207044098280898870 cannot be represented in type 'long long'
What is interesting is than the code works fine even for unsigned long long int.
int numDistinct(string s, string t) {
int n = t.length() , m = s.length();
vector<vector<unsigned long long int>> dp(t.size() + 1, vector<unsigned long long int>(s.size() + 1));
dp[n][m] = (long long int)1;
for(int i=n ; i>=0 ; i--){
for(int j=m-1 ; j>=0 ; j--){
if(i == n){
dp[i][j] = (long long int)1;
}else if(t[i] == s[j]){
dp[i][j] = dp[i+1][j+1] + dp[i][j+1];
}else{
dp[i][j] = dp[i][j+1];
}
}
}
return dp[0][0];
}
This question is from leetcode code called Distinct Subsequences
https://leetcode.com/problems/distinct-subsequences/
As pointed in comments unsigned numbers can't overflow, but instead wrap around using the properties of modulo operation.
For instance, when unsigned int is 32 bits, then the result would be: (a + b) mod 2^32.
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
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;
}
How to find count of numbers between a and b (inclusive) which contains 0 as their digit. I am not able to get this with the idea which i have used below in my code, it becomes very complex in case of leading zeroes
for ex if a=1 and b=200 total number should be 29 but i am getting 39.
Can anyone suggest an efficient way of doing it?
constraints:
1<=a<=b<=10^17
code:
long long int F(long long int dig, long long int a, long long int num)
{
if(dig == 0) {
if(a>0)
return 1;
else
return 0;
}
if(mem[dig][a])
return D[dig][a];
mem[dig][a] = 1;
long long int ret = 0;
for(long long int i = 0;i<=9;i++) {
if(i==num)
ret=(ret+F(dig-1,a+1,num));
else
ret=(ret+F(dig-1,a,num));
}
D[dig][a] = ret;
return ret;
}
long long int solve(long long int x,long long int num)
{
char cad[100];
long long int ret = 0;
long long int a=0,b=0,c=0,j;
sprintf(cad,"%lld",x);
int len = strlen(cad);
long long int qued = len;
for(long long int i = 0;i < len;i++) {
qued--;
long long int d = cad[i] - '0';
for(j=0;j <d;j++) {
if(j==num) {
if(num==0 && i==0)
a=a+0;
else
a=a+1;
ret=(ret+F(qued,a,num));
}
else
ret=(ret+F(qued,a,num));
}
if(d==num)
a=a+1;
}
return ret;
}
solution is -> solve(b+1,0)-solve(a,0)
but i am getting wrong answer with this
the link from which i got the above idea is http://codeforces.com/blog/entry/8221
A Simple way to do it would be:
inList = [1, 2, ... n]
outList = []
for i in inList:
for j in len(i):
if i%10 || i==0:
//there is a 0
outList.add(i)
break;
if(i<10)
//i cannot contain a 0
break;
i=i/10
Probibly only works with positive ints, but its trivial to account for negitive numbers.
Simple and efficient O(10N) or O(17N) according to your constraints
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.