[CodeForces] [597A] Divisibility

Problem Description

Loading...

Problem Analysis

Force it into a one-liner AC

First, we know
there are n/k numbers divisible by k in 0~n
so when the whole interval is greater than 0
we have b/k-(a-1)/k
similarly, when the whole interval is less than 0
a/k-(b+1)/k
when the interval crosses 0
then add 1 on top of a/k+b/k (0/k is also divisible)

Also note the case where a/k<0 or b/k<0
so just add an absolute value.

Finally, the data range is relatively large
so __int64 can be used.

Use algorithm as the header for abs
because abs in math.h seems to be of int type.

ccpp
1
2
3
4
5
6
7
8
9
#include<stdio.h>
#include<algorithm>
using namespace std;
int main() {
	__int64 k,a,b;
	while(scanf("%I64d %I64d %I64d",&k,&a,&b)!=EOF)
		printf("%I64d\n",!((a>0&&b>0)||(a<0&&b<0))?abs(a/k)+abs(b/k)+1:(a>0?abs(b/k)-abs((a-1)/k):abs(a/k)-abs((b+1)/k)));
	return 0;
}

Problem link: 【CodeForces】[597A]Divisibility

Loading...