多久前做过的老题,今天打比赛时一直WA。。。原理很简单,就是拿区间两头端点a,b分别除以k再相加就行了,主要就是要0这个点。。太坑。。
主要看这几组数据:
1 -1 1 3
2 0 0 1
2 0 1 1
2 -1 1 1
#include<cstdio>
#include<algorithm>
using namespace std;
long long k,a,b,sum,t;
int main(){
while(~scanf("%lld%lld%lld",&k,&a,&b)){
sum=0;
if(a<=0&&b<=0){
t=-a;
a=-b;
b=t;
}
if(a<=0&&b>=0){
sum=1+b/k-a/k;
}
else{
sum=b/k-(a-1)/k;//a要减一。。
}
printf("%lld\n",sum);
}
return 0;
}