#include<iostream>
#include<cstdio>
#define M 1000000007
using namespace std;
long long int power(int a,int b)
{
if(b==0)
return 1;
else if(b==1)
return a;
else if(b%2==0)
return power((a*a)%M,b/2);
else
return (power((a*a)%M,b/2)*a)%M;
}
In this function when I pass a=2, b>31, it always returns 0.for b=31 i got 147483634.can you tell where the problem is?
or can you tell another method for calculating large powers of a number.
In (a*a)%M, a*a probably overflows before computing the remainder. And starting with 2, the overflow producing 0 doesn't surprise me. You need to work with a type able to represent (M-1)*(M-1), i.e. 1000000012000000036 while int is commonly limited to 2147483647. long long (standard in C since 99 and C++ since 11, common extension elsewhere) is guaranteed to work.
Under <cmath> there's pow(x,y) in which x is the base and y is the exponent. x^y.
Reference here
When a is an int, a*a uses int-sized arithmetic. Try a*(long long)a instead, to use wider arithmetic that won't overflow.
Related
I have this simple program here which is returning 18446744073709551615:
#include <iostream>
using namespace std;
int main()
{
cout<<-(size_t)1;
cout<<0-(size_t)1;
return 0;
}
Additionally, I also have this program here which is returning the expected result 0:
#include <iostream>
using namespace std;
int main()
{
cout<<1-(size_t)1;
return 0;
}
Can someone explain to me what is happening here? If -(size_t)1 and 0-(size_t)1 are underflowing, why is 1-(size_t)1 fine?
why is 1-(size_t)1 fine?
Because it doesn't overflow. 1 is representable as std::size_t, and the result of subtraction is 0 which is also representable.
So shouldnt -(size_t)1 evaluate to large number
It would, as you've found out when you used the unary minus operator in the first eample. But you didn't use the unary minus operator in the second example. You used the binary minus operator.
then it will become 1+large number?
A subtraction doesn't "become" an addition.
That said, you'll find that 1 + std::size_t(-1) is also 0. This is because unsigned arithmetic is modular. -1 is congruent with some large value, and that large value + 1 is congruent with 0. This property of modular arithmetic is known as "compatibility with translation".
I am solving FAST MULTIPLICATION on SPOJ.
My solution looks like this:
#include<bits/stdc++.h>
using namespace std;
int max(int a,int b)
{
if(a>b) return a;
return b;
}
long karatsuba_multiply(int x,int y)
{
if(x<10 or y<10) return x*y;
int n=max(to_string(x).length(),to_string(y).length());
int m=(int)ceil(n/2.0);
long p=(long)pow(10,m);
long a=(long)(floor(x/p));
long b=x%p;
long c=(long)(y/p);
long d=y%p;
long ac=karatsuba_multiply(a,c);
long bd=karatsuba_multiply(b,d);
long adbc=karatsuba_multiply(a+b,c+d)-ac-bd;
return (long)(pow(10*1,2*m)*ac+pow(10*1,m)*adbc+bd);
}
int main()
{
int a,b,t;
cin>>t;
while(t--)
{
cin>>a>>b;
cout<<karatsuba_multiply(a,b)<<endl;
}
return 0;
}
This code is giving correct output on coding blocks IDE as well as other IDEs. But this solution is being marked wrong on SPOJ. Can anyone tell me what I am doing wrong?
C++ originally only supports the maximum length of unsigned long long integers as about 1.8e19.
According to the problem, the answer can shoot up to 1e100000000 which is far larger.
Ways to solve this are:
Use Strings to store integers and use the operations on strings to multiply. You can check this article
Use C++ boost library. This library supports integer operations beyond 1e19 limit.
Another method would be to use some other language which supports greater than 64-bit integer like Python or use BigInteger Class in Java
From the problem description:
Input
n [the number of multiplications <= 1000]
l1 l2 [numbers to multiply (at most 10000 decimal digits each)]
Text grouped in [ ] does not appear in the input file.
A number with 10000 decimal digits is too large to fit in an int for typical sizes of int. You need to use a differnt type for the input and to carry out the multiplication. There is no built in type that can store integers that large.
(calculating the sum of long long numbers)
whenever I input a long long number the program returns always -1 or -2 anyone knows why?
#include <iostream>
#include <math.h>
#include <string>
#include<iomanip>
using namespace std;
int main()
{
int n,i;
long long x1,x2,s;
do{
cin>>n;
}while(n<1);
long long t1[n];
long long t2[n];
string ch1,ch2;
i=0;
do{
do{
scanf("%lld %lld", &x1, &x2);
ch1=to_string(x1);
ch2=to_string(x2);
}while((ch1.length()>pow(10,5)) || (ch2.length()>pow(10,5)));
t1[i]=x1;
t2[i]=x2;
i++;
}while(i<n);
for(i=0;i<n;i++){
s=t1[i]+t2[i];
cout<<s<<endl;
}
return 0;
}
can't find a solution please help! and thx in advance!
example input:
1
14444444444444444111115556
55656464684646464646647676
output
-2
//edit:
Is there a way to calculate the sum of 100000 :) decimal digits numbers like the ones in the example using standard libraries ?
Fundamental integral types such as long long have limited range that they can represent. If a calculation causes results in a larger result than can be represented, it will overflow. Signed integer overflow has undefined behaviour in C++.
It is possible to represent an infinite range of numbers (in theory, memory limits the practice) by using an array of integers to represent different parts of the number. This technique is called arbitrary precision arithmetic.
There is no implementation of arbitrary precision types in the C++ standard library, so either you must implement one yourself, or you can use a library implemented by others.
i am begginner in c++
below is my code ,I am using fmod(),in which the value of k should be 0.23 but it is giving it 0
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int k;
k=fmod(234.23,3);
cout<<"k="<<k<<endl;
return 0;
}
I am using codeblock.
Is it a compiler problem ?
You are assigning the result to a variable of type int, and int by its very nature can only represented integer numbers.
You need to use a floating-point type, such as float or double.
You must use float instead of int for k. An integer can only hold exact numbers, but no fractions. For this use, float or double.
C++ is a strongly typed language.
The result of fmod(234.23, 3) is 0.23, but you use an int to store it, so 0.23 is converted to a integer 0.
You should declare as float k;
I wanted to write a program that computes the number of zones made by n lines.
The first example is my code, and the second is my friend's code. I think they are trying to do the same thing, but for the case n=65535 my code gives me the wrong answer. Where is the problem in my code?
my code:
#include<iostream>
using namespace std;
int main()
{
int n;
cin >> n;
unsigned long long ans;
ans = (n*(n + 1) / 2) + 1;
cout << ans << endl;
return 0;
}
my friend's code:
#include <iostream>
using namespace std;
int main(void){
double n,sum;
cin>>n;
sum=n*(n+1)/2+1;
cout<<(long)sum<<endl;
return 0;
}
In your code:
int n;
ans = (n*(n + 1) / 2) + 1;
All values in the calculation are ints: n is declared as int, and plain integer constants are ints as well. Therefore the result of this calculation will also be an int. The fact that you later assign this result to a long long variable doesn't change this.
Now the result of the multiplication 65535*65536 does not fit in a 32-bit signed int, so you get a nonsense answer. Fix your program by making n a 64-bit long long.
As #Dithermaster suggests, the problem here is probably one of integer overflow.
As it stands right now, your code doesn't actually make much sense. In particular, since you've defined n as an int, and all the integer literals in the expression: (n*(n + 1) / 2) + 1 are also small enough to fit in an int, the calculation will be carried out on ints, and then (after the calculation is complete) the result will be converted to long long and assigned to ans (because you've defined ans as a long long).
What you almost certainly want is to carry out the entire calculation on long long to avoid overflow. The most obvious way to do this would be to define n as a long long instead of an int.
Your friend has avoided this by defining n as a double. This works up to a point--a typical implementation of double has a 53-bit significand, so it can be used as (essentially) a 53-bit integer type. That's obviously quite a bit more than the 16 bits that's mandated for an int, but equally obviously less than the 64 bits mandated for a long long.
There's also no point in supporting n being negative, so you could consider defining n and ans as unsigned long long instead.