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)/~

猜你喜欢

转载自blog.csdn.net/starlet_kiss/article/details/83118106
今日推荐