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;
}
但是没看懂(哭泣),有没有大佬可以解答一下 !