#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const int MAXN = 2e6+5;
map<ll, bool> visited;
vector< pair<ll, ll> > imp;
ll n, a, b;
int main()
{
/*for(ll a = 1; a <= 20; a++)
{
for(ll b = 1; b <= 20; b++)
{
map<pair<ll, ll> , bool> used;
ll time = 0;
while(!used[{(time + time / b) % a, time % b}])
{
used[{(time + time / b) % a, time % b}] = true;
time++;
}
cout << a << " " << b << " " << time << "\n";
}
}*/
cin >> n >> a >> b;
ll sum = 0;
ll big = 1;
map<pair<ll, ll>, bool> used;
ll time = 0;
while(!used[{(time + time / b) % a, time % b}])
{
used[{(time + time / b) % a, time % b}] = true;
time++;
}
big = time;
/*if(2e18 / b < a)
big = 2e18;
else
big = a * b;*/
for(int i = 1; i <= n; i++)
{
ll f, s;
cin >> f >> s;
if(s - f >= big - 1)
{
imp.push_back({0, 1});
imp.push_back({big - 1, 2});
} else
{
if(f % big <= s % big)
{
imp.push_back({f % big, 1});
imp.push_back({s % big, 2});
} else
{
imp.push_back({0, 1});
imp.push_back({s % big, 2});
imp.push_back({f % big, 1});
imp.push_back({big - 1, 2});
}
}
}
// cout << seg[0].sum << "\n";
sort(imp.begin(), imp.end());
ll cnt = 0;
ll ans = 0;
ll last = -1;
for(pair<ll, ll> e : imp)
{
//cout << ans << " " << cnt << " " << last << "\n";
if(last != -1 && cnt > 0)
{
ans += e.first - last;
if(!visited[last])
{
ans++;
visited[last] = true;
}
visited[e.first] = true;
}
if(e.second == 1)
{
cnt++;
last = e.first;
} else
{
cnt--;
last = e.first;
}
}
cout << ans << "\n";
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:27:8: warning: unused variable 'sum' [-Wunused-variable]
27 | ll sum = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2217 ms |
407060 KB |
Output is correct |
3 |
Correct |
1943 ms |
312632 KB |
Output is correct |
4 |
Correct |
4 ms |
1364 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
5 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
40 ms |
7756 KB |
Output is correct |
16 |
Correct |
69 ms |
13908 KB |
Output is correct |
17 |
Correct |
886 ms |
137096 KB |
Output is correct |
18 |
Runtime error |
3973 ms |
524288 KB |
Execution killed with signal 9 |
19 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Runtime error |
4634 ms |
524288 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
301 ms |
60736 KB |
Output is correct |
3 |
Correct |
332 ms |
61484 KB |
Output is correct |
4 |
Correct |
294 ms |
51156 KB |
Output is correct |
5 |
Correct |
1512 ms |
95996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2507 ms |
173268 KB |
Output is correct |
3 |
Runtime error |
3920 ms |
524288 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2507 ms |
173268 KB |
Output is correct |
3 |
Runtime error |
3920 ms |
524288 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
2507 ms |
173268 KB |
Output is correct |
3 |
Runtime error |
3920 ms |
524288 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
296 KB |
Output is correct |
2 |
Runtime error |
2186 ms |
524288 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2217 ms |
407060 KB |
Output is correct |
3 |
Correct |
1943 ms |
312632 KB |
Output is correct |
4 |
Correct |
4 ms |
1364 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
300 KB |
Output is correct |
7 |
Correct |
2 ms |
596 KB |
Output is correct |
8 |
Correct |
2 ms |
340 KB |
Output is correct |
9 |
Correct |
5 ms |
1108 KB |
Output is correct |
10 |
Correct |
1 ms |
296 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
40 ms |
7756 KB |
Output is correct |
16 |
Correct |
69 ms |
13908 KB |
Output is correct |
17 |
Correct |
886 ms |
137096 KB |
Output is correct |
18 |
Runtime error |
3973 ms |
524288 KB |
Execution killed with signal 9 |
19 |
Halted |
0 ms |
0 KB |
- |