codeforces 1066a Vova and Train

本文探讨了一道关于火车路径上可见灯塔数量的算法题。火车从起点出发,途中遇到每隔v个单位距离设置的灯塔,但由于固定停靠的火车遮挡,部分灯塔不可见。文章通过数学分析解决了这一问题,并提供了有效的代码实现。

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值