1. 程式人生 > >codeforces 1066a Vova and Train

codeforces 1066a Vova and Train

Vova plans to go to the conference by train. Initially, the train is at the point 1 and the destination point of the path is the point L. The speed of the train is 1 length unit per minute (i.e. at the first minute the train is at the point 1, at the second minute — at the point 2 and so on).

There are lanterns on the path. They are placed at the points with coordinates divisible by v (i.e. the first lantern is at the point v, the second is at the point 2v and so on).

There is also exactly one standing train which occupies all the points from l to r inclusive.

Vova can see the lantern at the point p if p
p is divisible by v and there is no standing train at this position (p∉[l;r]). Thus, if the point with the lantern is one of the points covered by the standing train, Vova can’t see this lantern.

Your problem is to say the number of lanterns Vova will see during the path. Vova plans to go to t
t different conferences, so you should answer t independent queries.

題意很好懂,分析下來就是一個數學問題。
一開始用的模擬,超時了,嚶嚶嚶。在分析一下就是數學問題,好好分析一下。
程式碼如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;

ll dis,v,l,r;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%I64d%I64d%I64d%I64d",&dis,&v,&l,&r);
		ll sum=dis/v;
		ll ans=0;
		ll mod=l%v;
		if(mod==0)
		{
			printf("%I64d\n",sum-((r-l)/v+1));
		}
		else
		{
			printf("%I64d\n",sum-(r-l+mod)/v);
		}
	}
}

努力加油a啊,(o)/~