AtCoder Beginning Contest 334(1)

文章讲述了在一个无限向东向西延伸的道路上,Snukewill按照每`M`米间隔设置圣诞树。Takahashi和Aoki位于坐标`L`和`R`处,要求找出他们之间(包括自身位置)设置的圣诞树数量。给出了两个C++代码片段用于解决此问题,但存在效率问题。
摘要由CSDN通过智能技术生成

 

B - Christmas Trees Editorial

Score : 250250 points

Problem Statement

There is a road that stretches infinitely to the east and west, and the coordinate of a point located �x meters to the east from a certain reference point on this road is defined as �x. In particular, the coordinate of a point located �x meters to the west from the reference point is −�−x.

Snuke will set up Christmas trees at points on the road at intervals of �M meters, starting from a point with coordinate �A. In other words, he will set up a Christmas tree at each point that can be expressed as �+��A+kM using some integer �k.

Takahashi and Aoki are standing at points with coordinates �L and �R (�≤�)(L≤R), respectively. Find the number of Christmas trees that will be set up between Takahashi and Aoki (including the points where they are standing).

Constraints

  • −1018≤�≤1018−1018≤A≤1018
  • 1≤�≤1091≤M≤109
  • −1018≤�≤�≤1018−1018≤L≤R≤1018
  • All input values are integers.

有些符号乱码,题干截屏如下

#include<iostream>
using namespace std;
int main()
{
    long long a, m, l, r, i, cnt = 0;
    cin >> a >> m >> l >> r;
    for (i = 0;; i++)
    {
        if ((a + i * m > l || a + i * m == l) && (a + i * m < r || a + i * m == r))
        {
            cnt++;
        }
        
        if (a + i * m > r)
            break;

    }
    for (i =-1;; i--)
    {
        if ((a + i * m > l || a + i * m == l) && (a + i * m < r || a + i * m == r))
        {
            cnt++;
        }

        if (a + i * m <l)
            break;
    }
    cout << cnt;
    return 0;
}

使用无脑方法发现有些数据超时

给出的答案

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
ll floor(ll x, ll m) {
    ll r = (x % m + m) % m;
    return (x - r) / m;
}
int main() {
    ll a, m, l, r;
    cin >> a >> m >> l >> r;
    l -= a, r -= a;
    cout << floor(r, m) - floor(l - 1, m) << endl;
}

 

但是没看懂(哭泣),有没有大佬可以解答一下 !

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值