Find the number of k-divisible numbers on the segment [a, b]. In other words you need to find the number of such integer values x that a ≤ x ≤ b and x is divisible by k.
Input
The only line contains three space-separated integers k, a and b (1 ≤ k ≤ 1018; - 1018 ≤ a ≤ b ≤ 1018).
Output
Print the required number.
Examples
Input
1 1 10
Output
10
Input
2 -4 4
Output
5
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are asked: "Find the number of k-divisible numbers on the segment [a, b]. In other words you need to find the number of such integer values x that a ≤ x ≤ b and x is divisible by k."
Given k, a, b (all integers, with constraints up to 1e18). Need to count the integers x in [a, b] that are divisible by k.
The typical formula: count = floor(b/k) - floor((a-1)/k) if k>0. But note that a and b can be negative. Also k can be up to 1e18, but positive (since k ≥ 1). So we need to handle negative numbers correctly. Also note that we are dealing with integer division floor. But careful: For negative numbers, floor division might behave differently in languages. In mathematics, floor division means the greatest integer ≤ real quotient. For negative numbers, floor division yields a number less than or equal to the quotient. For example, floor(-1/2) = -1 because -0.5 floor is -1. Many programming languages use truncation toward zero (like in C++), but we need mathematical floor. So we need to compute mathematically correctly.
Alternatively, we can compute using integer arithmetic: The number of multiples of k in [a,b] is: If a > b, zero; but given a ≤ b. The smallest multiple ≥ a is: ceil(a/k) * k? But careful: Ceil of a/k. Ceil of a/k is the smallest integer n such that n*k ≥ a. But ceil for negative numbers also needs to be defined correctly: ceil(x) is the smallest integer ≥ x. Then the largest multiple ≤ b is: floor(b/k) * k. Then count = ( (floor(b/k) - ceil(a/k)) + 1 ) if floor(b/k) >= ceil(a/k), else 0.
Since k>0, we can use integer arithmetic to compute these without floating point.
We need to compute ceil(a/k) and floor(b/k). For integer division with floor: floor division in math: floor(x) for integer quotient. For positive divisor k, floor(n/k) where n is integer can be computed as: if n >=0, then n/k integer division truncates toward zero, which is floor. But if n<0, truncation toward zero (like in C++ with /) gives ceil? Actually, in C++, integer division truncates toward zero, so -1/2 = 0, but floor(-0.5) = -1. So that is not floor. So we need to implement floor division correctly for possibly negative numbers.
We can compute floor(n/k) as: let q = n / k (in integer arithmetic with truncation toward zero). Then if n >= 0 or n % k == 0, then floor = q; else floor = q - 1. Because for negative n not divisible, the truncation toward zero yields a number greater than the floor (since floor is more negative). For example, n=-1, k=2: truncation toward zero gives 0, but floor(-0.5) = -1; difference: 0 - 1 = -1. So floor = q - 1 when n<0 and remainder != 0.
Alternatively, we can use: floor(n/k) = (n - (n%k + k) % k) / k? That's messy.
Better: compute using big integers and use floor division directly if language supports (like Python's // operator does floor division). But we are to write solution in C++? Actually problem statement: "Input" and "Output" typical for competitive programming. Usually we need to write in C++ or any language. But we are ChatGPT, we just need to produce solution logic, or code? The user asks: "Find the number of k-divisible numbers on the segment [a, b]." It looks like a problem description; the user might expect an explanation or solution. But the instruction says: "You are ChatGPT, a large language model. ..." It's a typical Codeforces problem? Actually, I recognize it: problem "k-Multiple Free Set"? No, it's just count multiples in range. Possibly they need to output the answer.
The user gave examples. They probably want the answer as a number.
We need to provide the solution. Possibly they want a program or formula. Since they posted in plain text, maybe they just need the number? No, they gave input as three numbers; they might be asking to compute for some specific hidden test? Actually, they gave examples but not a specific query. The user says "Find the number of k-divisible numbers on the segment [a, b]. In other words you need to find the number of such integer values x that a ≤ x ≤ b and x is divisible by k." Then gives Input format and Output format and examples. This is a typical competitive programming problem statement. So the user is asking to solve this problem, i.e., provide a solution (probably code) that reads input and outputs the answer. However, as ChatGPT, we can provide an explanation and code.
But perhaps the user just wants the formula. Let's respond with explanation and code in C++.
Let's derive the formula: count = b/k - (a-1)/k? But careful with negatives.
Better: number of multiples of k in [a,b] = floor(b/k) - floor((a-1)/k) if k>0. But floor((a-1)/k) for negative a might be tricky. But we can compute floor(b/k) and floor((a-1)/k) using proper floor division.
Alternatively, compute first multiple >= a: first = ceil(a/k) * k, last = floor(b/k) * k. Then if first > last, count = 0 else count = (last - first)/k + 1.
So we need to compute ceil(a/k). Ceil division for integers with positive divisor: ceil(x/k) = floor((x + k - 1)/k) if x>=0? But for negative x, the formula is different: ceil(a/k) = (a + k - 1) // k? Actually, that only works for positive a. For all integers, we can compute ceil(a/k) = - floor(-a/k). Since k>0, floor division of -a/k can be used. So we can compute ceil(a/k) = (a + k - 1) / k if a>0? Better to use: ceil(a/k) = (a + k - 1) // k when a>0? But if a negative, that formula might fail. For example a=-5,k=2: (-5+2-1)/2 = (-4)/2 = -2 in integer division? In C++ truncation toward zero, -4/2 = -2. Ceil(-5/2) = ceil(-2.5) = -2. So that works? Let's test: (-5+1)/2? Actually formula for ceil: ceil(a/k) = (a + k - 1) // k works for positive a only? There's a known formula for integer ceil division: (a + d - 1) / d works only for a >= 0. For negative a, we can use: (a // k) if a % k == 0 else (a // k) + 1? But careful with integer division behavior.
Simplify: Use the floor function we can implement robustly.
Plan:
Define function floor_div(n, k) returns floor(n/k). Since k>0.
Implementation in C++:
long long floor_div(long long n, long long k) {
if (n >= 0) return n / k;
else return (n - (k-1)) / k; // is that correct? Actually floor(n/k) for negative n: we can compute as (n - (k-1)) / k using truncation toward zero? Let's test: n=-1, k=2: (-1 -1)/2 = -2/2 = -1. That's correct. n=-2, k=2: (-2-1)/2 = -3/2 = -1.5 -> truncation toward zero gives -1? In C++ integer division, -3/2 = -1 (since truncation toward zero). But floor(-2/2)= floor(-1)= -1. So that works? Actually for exact division, we need floor(-2/2) = -1, same as truncation toward zero. But our formula (n - (k-1))/k gave (-2-1)/2 = -3/2 = -1.5 truncated to -1. So works. n=-3, k=2: floor(-1.5) = -2. Our formula: (-3-1)/2 = -4/2 = -2. Works. n=-4, k=2: floor(-2) = -2. Formula: (-4-1)/2 = -5/2 = -2.5 truncated to -2. Works. So indeed floor_div(n,k) = (n - (k-1)) / k when n < 0? Let's verify for n=-5, k=3: floor(-5/3) = floor(-1.6667) = -2. Formula: (-5 -2)/3 = -7/3 = -2.333 trunc to -2? In C++ -7/3 = -2 (since truncation toward zero). So we get -2, which matches floor. So yes, it works because we subtract (k-1) before dividing, which makes the division effectively floor. But wait, we need to check if it works for all negative n. The typical formula for floor division in programming contests: floor_div(a,b) = a / b for a>=0, and (a - b + 1) / b for a<0? Actually there are various tricks. Another common method: floor_div(a,b) = a/b if a% b==0 or a>=0 else a/b - 1. That is simpler and safe if using C++ with truncation toward zero. So I'll use that.
But we can also compute floor_div and ceil_div using standard formulas:
floor_div(n,k) = n/k if n>=0 or n%k==0 else n/k - 1; (since in C++ negative division truncates toward zero, and n%k gives remainder with sign of numerator? In C++, remainder can be negative. So careful: For n negative, n%k is negative or zero, with magnitude less than k. So condition: if (n >= 0) return n/k; else return (n % k == 0) ? n/k : n/k - 1; But note: n/k is truncated toward zero. For negative n not divisible, n/k is the quotient truncation (greater than floor). For example n=-5, k=3: n/k = -1 (since -5/3 truncates to -1), remainder = -2. Since remainder !=0, floor = -1 -1 = -2. Works. For n=-6, k=3: n/k = -2, remainder = 0, floor = -2, works.
But we need to be careful with overflow? n and k are up to 1e18, but division is within 64-bit; safe in long long (signed 64-bit, up to 9e18). 1e18 is within 2^63-1? 2^63-1 ≈ 9.22e18, so yes.
So implement floor_div.
ceil_div(n,k) can be: - floor_div(-n, k). Because ceil(x) = -floor(-x). Since k>0.
So we can compute first_multiple = ceil_div(a, k) * k, last_multiple = floor_div(b, k) * k.
Then if first_multiple > last_multiple, answer = 0; else answer = (last_multiple - first_multiple) / k + 1.
But careful: multiplication might overflow if k is large and values near 1e18. For example, floor_div(b,k) could be up to about 1e18 (if b=1e18, k=1) so multiplied by k could be up to 1e36, overflow. So better to avoid multiplication. Instead compute count = floor_div(b,k) - ceil_div(a,k) + 1.
Because the number of integers n from ceil_a/k to floor_b/k inclusive is floor_b/k - ceil_a/k + 1, provided floor_b/k >= ceil_a/k. Where ceil_a/k = ceil(a/k) and floor_b/k = floor(b/k). This is simpler and avoids multiplication.
So answer = max(0, floor(b/k) - ceil(a/k) + 1).
Thus implement:
long long count = floor_div(b, k) - ceil_div(a, k) + 1;
if (count < 0) count = 0;
Now we need to implement ceil_div.
ceil_div(a, k) = (a + k - 1) / k for positive a? But safer: use floor_div method: ceil_div = - floor_div(-a, k). Because ceil(x) = - floor(-x). Works for any a.
Thus:
long long ceil_div(long long a, long long k) {
return -floor_div(-a, k);
}
But careful: when -a might overflow? Since a can be -1e18, -a = 1e18 which is within 2^63-1, so safe. a could be -1e18? Yes, -1e18 is within range, -(-1e18)=1e18 safe. The worst-case absolute value is 1e18, so fine.
Now implement floor_div as described.
But we also must consider that k can be large up to 1e18, but we are dividing by k, so it's fine.
Edge cases: When a=0? Fine. When b=0? Fine.
Test examples:
k=1, a=1, b=10. floor_div(10,1)=10, ceil_div(1,1)=1, count=10-1+1=10. OK.
Other tests: k=3, a=-5, b=5. Multiples: -3,0,3. Count=3. floor_div(5,3)=1, ceil_div(-5,3)= ceil(-1.6667) = -1? Actually ceil(-5/3) = -1? Wait, -1.6667 ceil is -1? Since -1 > -1.6667, so -1. So floor_div(5,3)=1, ceil_div(-5,3) = -1. Then 1 - (-1) +1 = 3. Good.
Another test: k=10, a= -20, b= -5. Multiples: -20, -10? Actually -20 divisible, -10 divisible, -5 not. So -20 and -10 are both ≤ -5? Wait a=-20, b=-5, so range includes -20,-19,...,-5. Multiples: -20, -10. Count=2. floor_div(-5,10) =? floor(-0.5) = -1. ceil_div(-20,10) = ceil(-2) = -2. So -1 - (-2) +1 = 2. Works.
Edge: k large, say k=1e18, a=-1e18, b=1e18. Multiples: -1e18, 0? 0 is divisible by any k? Yes, 0 is divisible by any non-zero integer. So multiples: -1e18, 0, 1e18? 1e18 is divisible by 1e18? 1e18 / 1e18 =1, yes. So count=3. Let's compute: floor_div(1e18, 1e18) = 1. ceil_div(-1e18, 1e18) = ceil(-1) = -1? Actually -1e18/k = -1, ceil(-1) = -1. So count = 1 - (-1) +1 = 3. Good.
What about a=b? If a=b and it's divisible, count=1. Good.
What about k > b - a? Might be zero.
Now, code: read k, a, b as long long. Compute answer.
But caution: The input constraints: k up to 1e18, a,b up to +/-1e18. All fit into signed 64-bit (long long in C++ is typically up to 9e18). So okay.
We'll provide C++ solution.
Potential pitfalls: floor_div for negative numbers using the formula (n - (k-1)) / k may cause overflow? For n negative large, n - (k-1) could be less than -2e18? but still within range of long long? Worst-case: n = -1e18, k = 1e18, n - (k-1) = -1e18 - (1e18-1) = -2e18 + 1, which is about -2e18. That's less than -9e18? Actually -2e18 is > -9e18? -2e18 is greater (less negative) than -9e18? -2e18 > -9e18 because -2e18 is closer to zero. Actually -2e18 is -2,000,000,000,000,000,000, which is about -210^18. The minimum 64-bit signed is -9.2210^18, so -2e18 is safe. Even if n = -1e18 and k = 1e18, it's safe. If n = -1e18 and k = 1, n - (k-1) = -1e18 - 0 = -1e18 safe. So overflow not an issue.
But to be safe, we can use the conditional approach: floor_div = n/k for n>=0 or n%k==0 else n/k -1. However, n%k for n negative in C++ can be negative. But we can adjust: (n % k) might be negative, but checking if (n % k == 0) works. If n is negative and divisible, remainder is 0. So that's fine. Example: -5 % 3 = -2 in C++? Actually C++ remainder sign is same as dividend (n). So -5 % 3 = -2. Not zero. So check if (n % k == 0) will be false, but -5 is not divisible by 3, so correct. -6 % 3 = 0. So that's okay. So conditional works.
But we have to be careful with big numbers, but it's fine.
So I'll code floor_div as:
long long floor_div(long long n, long long k) {
if (n >= 0) return n / k;
else return (n % k == 0) ? n / k : n / k - 1;
}
ceil_div: - floor_div(-n, k)
But note: For ceil_div, we might have overflow when -n = -(-1e18) = 1e18, which is safe.
Alternatively, we can compute ceil_div directly: (a + k - 1) / k if a >= 0 else a / k (since for negative a, integer division truncates toward zero, but we need ceil). But easier to use the floor method.
Thus final solution.
Now, we need to output the answer.
We'll present explanation and code.
But the user didn't specify language. Usually for such problems, C++ is acceptable.
We should also consider that a and b might be such that a > b? But problem states a ≤ b.
Now, code:
#include
using namespace std;
long long floor_div(long long n, long long k) {
if (n >= 0) return n / k;
// else n < 0
long long q = n / k; // truncates towards zero
if (n % k == 0) return q;
else return q - 1;
}
long long ceil_div(long long n, long long k) {
return -floor_div(-n, k);
}
int main() {
long long k, a, b;
cin >> k >> a >> b;
long long first = ceil_div(a, k);
long long last = floor_div(b, k);
long long ans = last - first + 1;
if (ans < 0) ans = 0;
cout << ans << endl;
return 0;
}
Test with examples.
But wait: For a = -4, k=2: ceil_div(-4,2) = -floor_div(4,2) = -2. Good.
Edge case: a=0, b=0, k=5: ceil_div(0,5)=0? floor_div(0,5)=0, so ceil = -floor_div(0,5)=0. last=0, ans=1? But 0 is divisible by 5? Yes, 0 divisible. So answer 1. Good.
But note: The problem constraints include k up to 10^18, a and b up to -10^18 to 10^18. In C++ with long long, it's safe. However, some platforms define long long as 64-bit, which holds up to 9e18. So fine.
Alternatively, we could directly compute: ans = b/k - (a-1)/k? But that requires floor division as well.
So we provide this solution.
Now, the user might not need code but just the answer? The question: "Find the number of k-divisible numbers on the segment [a, b]." It could be interpreted as "Write a program that outputs the required number." So I'll provide a solution with explanation and code.
But maybe the user wants a more mathematical answer: like answer = ⌊b/k⌋ - ⌈a/k⌉ + 1. I can provide that.
Given that this is a typical Codeforces problem (maybe 1213A? Actually it's a simple one), I'll give the answer.
Thus, final answer: We'll output the number using the formula.
I'll respond with explanation and code.
Make sure to note that k is positive.
Now, I'll produce the final message. The number of integers (x) in ([a, b]) that are divisible by (k) (with (k>0)) can be computed using floor and ceil division:
To implement this in code, we need correct floor and ceil for possibly negative numerators.
For floor division (\left\lfloor n/k \right\rfloor) with (k>0):
If (n \ge 0), (\left\lfloor n/k \right\rfloor = n/k) (integer division truncates toward zero, which is floor).
If (n < 0), let (q = n/k) (truncation toward zero). If (n) is exactly divisible by (k) ((n%k = 0)), floor is (q); otherwise floor is (q-1).
Then (\left\lceil a/k \right\rceil = -\left\lfloor -a/k \right\rfloor).
#include <iostream>
using namespace std;
typedef long long ll;
ll floor_div(ll n, ll k) {
if (n >= 0) return n / k;
// n < 0
ll q = n / k; // truncates toward zero
if (n % k == 0) return q;
else return q - 1;
}
ll ceil_div(ll n, ll k) {
return -floor_div(-n, k);
}
int main() {
ll k, a, b;
cin >> k >> a >> b;
ll first = ceil_div(a, k);
ll last = floor_div(b, k);
ll ans = last - first + 1;
if (ans < 0) ans = 0;
cout << ans << endl;
return 0;
}