I know number greater than INT64_MAX will wrap around negative, So how to compare when sum overflow, that is sum greater than INT64_MAX.
#include <iostream>
using namespace std;
int main() {
int64_t a = INT64_MAX;
int64_t b = 1;
// cin >> a >> b;
if (a + b <= INT64_MAX) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
First compare b to either INT64_MIN - a or INT64_MAX - a before the addition to prevent undefined behavior (UB) of signed integer overflow.
// True when sum overflows.
bool is_undefined_add64(int64_t a, int64_t b) {
return (a < 0) ? (b < INT64_MIN - a) : (b > INT64_MAX - a);
}
Worst case: 2 compares.
For div, mul, sub
Before compare, check a + b to see if it will overflow.
int is_overflow(int64_t a, int64_t b) {
if (((b > 0) && (a > (INT64_MAX - b))) ||
((b < 0) && (a < (INT64_MIN - b)))) {
return 1;
}
return 0;
}
Related
I was trying to build 17bit adder, when overflow occurs it should round off should appear just like int32.
eg: In int32 add, If a = 2^31 -1
int res = a+1
res= -2^31-1
Code I tried, this is not working & is there a better way. Do I need to convert decimal to binary & then perform 17bit operation
int addOvf(int32_t result, int32_t a, int32_t b)
{
int max = (-(0x01<<16))
int min = ((0x01<<16) -1)
int range_17bit = (0x01<<17);
if (a >= 0 && b >= 0 && (a > max - b)) {
printf("...OVERFLOW.........a=%0d b=%0d",a,b);
}
else if (a < 0 && b < 0 && (a < min - b)) {
printf("...UNDERFLOW.........a=%0d b=%0d",a,b);
}
result = a+b;
if(result<min) {
while(result<min){ result=result + range_17bit; }
}
else if(result>min){
while(result>max){ result=result - range_17bit; }
}
return result;
}
int main()
{
int32_t res,x,y;
x=-65536;
y=-1;
res =addOvf(res,x,y);
printf("Value of x=%0d y=%0d res=%0d",x,y,res);
return 0;
}
You have your constants for max/min int17 reversed and off by one. They should be
max_int17 = (1 << 16) - 1 = 65535
and
min_int17 = -(1 << 16) = -65536.
Then I believe that max_int_n + m == min_int_n + (m-1) and min_int_n - m == max_int_n - (m-1), where n is the bit count and m is some integer in [min_int_n, ... ,max_int_n]. So putting that all together the function to treat two int32's as though they are int17's and add them would be like
int32_t add_as_int17(int32_t a, int32_t b) {
static const int32_t max_int17 = (1 << 16) - 1;
static const int32_t min_int17 = -(1 << 16);
auto sum = a + b;
if (sum < min_int17) {
auto m = min_int17 - sum;
return max_int17 - (m - 1);
} else if (sum > max_int17) {
auto m = sum - max_int17;
return min_int17 + (m - 1);
}
return sum;
}
There is probably some more clever way to do that but I believe the above is correct, assuming I understand what you want.
Among the given input of two numbers, check if the second number is exactly the next prime number of the first number. If so return "YES" else "NO".
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int nextPrime(int x){
int y =x;
for(int i=2; i <=sqrt(y); i++){
if(y%i == 0){
y = y+2;
nextPrime(y);
return (y);
}
}
return y;
}
int main()
{
int n,m, x(0);
cin >> n >> m;
x = n+2;
if(n = 2 && m == 3){
cout << "YES\n";
exit(0);
}
nextPrime(x) == m ? cout << "YES\n" : cout << "NO\n";
return 0;
}
Where is my code running wrong? It only returns true if next number is either +2 or +4.
Maybe it has something to do with return statement.
I can tell you two things you are doing wrong:
Enter 2 4 and you will check 4, 6, 8, 10, 12, 14, 16, 18, ... for primality forever.
The other thing is
y = y+2;
nextPrime(y);
return (y);
should just be
return nextPrime(y + 2);
Beyond that your loop is highly inefficient:
for(int i=2; i <=sqrt(y); i++){
Handle even numbers as special case and then use
for(int i=3; i * i <= y; i += 2){
Using a different primality test would also be faster. For example Miller-Rabin primality test:
#include <iostream>
#include <cstdint>
#include <array>
#include <ranges>
#include <cassert>
#include <bitset>
#include <bit>
// square and multiply algorithm for a^d mod n
uint32_t pow_n(uint32_t a, uint32_t d, uint32_t n) {
if (d == 0) __builtin_unreachable();
unsigned shift = std::countl_zero(d) + 1;
uint32_t t = a;
int32_t m = d << shift;
for (unsigned i = 32 - shift; i > 0; --i) {
t = ((uint64_t)t * t) % n;
if (m < 0) t = ((uint64_t)t * a) % n;
m <<= 1;
}
return t;
}
bool test(uint32_t n, unsigned s, uint32_t d, uint32_t a) {
uint32_t x = pow_n(a, d, n);
//std::cout << " x = " << x << std::endl;
if (x == 1 || x == n - 1) return true;
for (unsigned i = 1; i < s; ++i) {
x = ((uint64_t)x * x) % n;
if (x == n - 1) return true;
}
return false;
}
bool is_prime(uint32_t n) {
static const std::array witnesses{2u, 3u, 5u, 7u, 11u};
static const std::array bounds{
2'047u, 1'373'653u, 25'326'001u, 3'215'031'751u, UINT_MAX
};
static_assert(witnesses.size() == bounds.size());
if (n == 2) return true; // 2 is prime
if (n % 2 == 0) return false; // other even numbers are not
if (n <= witnesses.back()) { // I know the first few primes
return (std::ranges::find(witnesses, n) != std::end(witnesses));
}
// write n = 2^s * d + 1 with d odd
unsigned s = 0;
uint32_t d = n - 1;
while (d % 2 == 0) {
++s;
d /= 2;
}
// test widtnesses until the bounds say it's a sure thing
auto it = bounds.cbegin();
for (auto a : witnesses) {
//std::cout << a << " ";
if (!test(n, s, d, a)) return false;
if (n < *it++) return true;
}
return true;
}
And yes, that is an awful lot of code but it runs very few times.
Something to do with the return statement
I would say so
y = y+2;
nextPrime(y);
return (y);
can be replaced with
return nextPrime(y + 2);
Your version calls nextPrime but fails to do anything with the return value, instead it just returns y.
It would be more usual to code the nextPrime function with another loop, instead of writing a recursive function.
Problem Link - https://cses.fi/problemset/task/1712
input -
1
7
8
10
Expected Output - 928742408
My output - 989820350
point that is confusing me - Out of 100s of inputs, in only 1 or 2 test cases my code is providing wrong output, if the code is wrong shouldn't it give wrong output for everything?
My code -
#include <iostream>
#include <algorithm>
typedef unsigned long long ull;
constexpr auto N = 1000000007;
using namespace std;
ull binpow(ull base, ull pwr) {
base %= N;
ull res = 1;
while (pwr > 0) {
if (pwr & 1)
res = res * base % N;
base = base * base % N;
pwr >>= 1;
}
return res;
}
ull meth(ull a, ull b, ull c) {
if (a == 0 && (b == 0 || c == 0))
return 1;
if (b == 0 && c == 0)
return 1;
if (c == 0)
return a;
ull pwr = binpow(b, c);
ull result = binpow(a, pwr);
return result;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
ull a, b, c, n;
cin >> n;
for (ull i = 0; i < n; i++) {
cin >> a >> b >> c;
cout << meth(a, b, c) << "\n";
}
return 0;
}
`
Your solution is based on an incorrect mathematical assumption. If you want to compute abc mod m you can't reduce the exponent bc mod 109 + 7. In other words, abc mod m != abc mod m mod m. Instead, you can reduce it mod 109 + 6 which works because of Fermat's little theorem. Therefore, you need to compute your exponent bc under a different modulus.
For reference
Change
ull pwr = binpow(b, c);
To a pwr = bc calculation.
810 --> 1,073,741,824
71,073,741,824 mod 100000007 --> 928742408
if the code is wrong shouldn't it give wrong output for everything?
Likely the other bc were always < 100000007
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int a, b;
cin >> a >> b;
if (a == b) cout << 0 << endl;
else cout << 1 + int((a < b) ^ ((b - a) & 1)) << endl;
}
return 0;
}
please someone describe the above statement.how this statement works and what is the result of this statement.
this code is snippet from codeforces.
link:https://codeforces.com/blog/entry/74224
Let's make it step by step.
c = (b - a) & 1 - check if last bit of b - a is set, same as check that b - a is odd.
d = (a < b) ^ c - returns true if int(a < b) != c.
1 + int(d) - should be clear
Then we have
(b - a) - odd (b - a) - even
(a < b) 1 2
(a >= b) 2 1
I'm trying to find the sum of all divisors of c in a give range a, b a <= b.
I've tried to loop from a to b and sum all divisors of c, but this seems inefficient, because the absolute difference between a and b can be 10^9.
Is there a way that reduces the time complexity of this approach?
int a, b, c;
cin >> a >> b >> c;
long long sum = 0;
for (int i = a; i <= b; i++) {
if (i % c == 0) {
ans += i;
}
}
cout << sum << endl;
Note: the question is unclear whether we need to sum divisors (in the description) or divisible integers (in the code sample). The answer sums up divisible items.
This is simple.
Find from, the smallest value such that from % c == 0 && from >= a
Find to, the largest value such that to % c == 0 && to <= b
.
int n = (to - from) / c + 1;
return n * (to + from) / 2;
Return to - from + c. Take care of boundary conditions when to could overflow your type and from can underflow.
To find from do something like:
if (c < 0) c *= -1; // works unless c == MIN_INT
if (a % c == 0)
from = a;
else if (a >= 0)
from = (a / c * c) + c
else
from = a / c * c;
Similarly for to, but accounting for the fact that we need to round down, and not up.
Also, need to handle the case of a > b separately.
EDIT
Here is the complete code with no loops, recursion, or containers. It runs in O(1):
int a, b, c;
std::cin >> a >> b >> c;
if (!std::cin) {
std::cout << "input error\n";
return 0;
}
if (c < 0) c*= -1;
const int from = [a,c] {
// no rounding needed
if (a % c == 0) return a;
// division rounds down to zero
if (a > 0) return (1 + a / c) * c;
// division rounds up to zero
return a / c * c;
}();
const int to = [b,c] {
// no rounding needed
if (b % c == 0) return b;
// division rounds down to zero
if (b > 0) return (b / c) * c;
// division rounds up to zero
return (b / c - 1) * c;
}();
int64_t sum = 0;
if (from <= to)
{
const int n = (to - from) / c + 1;
sum = n * (to + from) / 2;
}
std::cout << sum << '\n';
Identify all the prime numbers that are divisors of c first. That will leave you with a list of numbers [w,x,y,z…]. Then keep a hash table set of all multiples of integers in this list that are also divisors.
int a, b, c;
cin >> a >> b >> c;
long long sum = 0;
std::vector<int> all_prime_factors = // Get all prime factors of c
std::unordered_set<int> factorSet;
for (int primefactor : all_prime_factors)
{
int factor = primefactor;
while (factor <= b)
{
if (factor % c == 0)
factorSet.insert(factor);
factor += primefactor;
}
}
for (int x : factorSet)
{
sum += x;
}
cout << sum << endl;