Submission #677092

# Submission time Handle Problem Language Result Execution time Memory
677092 2023-01-02T08:57:54 Z amir Strange Device (APIO19_strange_device) C++17
100 / 100
472 ms 69776 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll ;
typedef pair<int , int> pii ;
typedef pair<ll , ll> pll ;
#define ps push_back
#define pb pop_back
#define mp make_pair
#define all(x) x.begin() , x.end() 
#define sz(x) (int)x.size() 
#define debug(a) cout<<"debug: "<<(#a)<<" = "<<a<<'\n'
const ll maxn = 1e6 + 6 , MX = 1e18+3 ;
ll A , B , S ;
ll n ;
ll l[maxn] , r[maxn] ;
vector<pair<ll , ll > > v ;


ll Gcd(ll a , ll b){
	if (b > a) swap(a , b) ;
	if (b == 0){
		return a ;
	}
	return Gcd(b , (a%b)) ;
}

void calS(){
	ll G = Gcd(A , B+1) ;
	ll X = (A / G) ;
	if (X > (MX/B)){
		S = MX ;
	}
	else {
		S = (X*B) ;
	}
}


int main()
{
    ios::sync_with_stdio(false) ; 
	cin.tie(NULL) ;
	cout.tie(NULL) ;
	cin >> n >> A >> B ;
	calS() ;
	for (int i = 0 ;i < n;  i++){
		cin >> l[i] >> r[i]  ;
	}
	ll ans = 0 ;
	for (int i = 0 ;i < n ;i++){
		ll len = r[i]-l[i]+1 ;
		if (len >= S){
			cout << S << '\n' ;
			return 0 ;
		}
		ll x = (l[i] % S) ;
		ll y = (r[i] % S) ;
		if (x <= y){
			v.ps(mp(x , y)) ;
		}
		else {
			v.ps(mp(x , S-1)) ;
			v.ps(mp(0 , y)) ;
		}
	}
	sort(all(v)) ;
	for (int i = 0 ;i < sz(v) ; i++){
		int j = i ; 
		pair<ll , ll > a = v[j] ;
		bool flag = true ;
		while(flag && j < sz(v)){
			if (v[j].first <= a.second+1){
				a.second = max(v[j].second , a.second) ;
				j ++ ;
			}
			else{
				flag = false ;
			}
		}
		i = j-1 ;
		ans += (a.second-a.first+1) ;
	}
	cout << ans << '\n' ;
}






# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 5 ms 1260 KB Output is correct
3 Correct 5 ms 1236 KB Output is correct
4 Correct 0 ms 328 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 4 ms 1216 KB Output is correct
17 Correct 44 ms 7752 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 376 KB Output is correct
5 Correct 303 ms 57712 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 405 ms 51060 KB Output is correct
3 Correct 392 ms 69540 KB Output is correct
4 Correct 400 ms 69556 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 405 ms 51060 KB Output is correct
3 Correct 392 ms 69540 KB Output is correct
4 Correct 400 ms 69556 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 390 ms 69776 KB Output is correct
7 Correct 391 ms 69596 KB Output is correct
8 Correct 397 ms 69652 KB Output is correct
9 Correct 442 ms 69652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 405 ms 51060 KB Output is correct
3 Correct 392 ms 69540 KB Output is correct
4 Correct 400 ms 69556 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 46 ms 7752 KB Output is correct
7 Correct 42 ms 7756 KB Output is correct
8 Correct 39 ms 7752 KB Output is correct
9 Correct 41 ms 7708 KB Output is correct
10 Correct 44 ms 7820 KB Output is correct
11 Correct 41 ms 7752 KB Output is correct
12 Correct 39 ms 7704 KB Output is correct
13 Correct 41 ms 7784 KB Output is correct
14 Correct 39 ms 7756 KB Output is correct
15 Correct 45 ms 7776 KB Output is correct
16 Correct 44 ms 7880 KB Output is correct
17 Correct 40 ms 7752 KB Output is correct
18 Correct 405 ms 69660 KB Output is correct
19 Correct 407 ms 69688 KB Output is correct
20 Correct 450 ms 69668 KB Output is correct
21 Correct 44 ms 7756 KB Output is correct
22 Correct 38 ms 7800 KB Output is correct
23 Correct 121 ms 26560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 42 ms 7772 KB Output is correct
3 Correct 42 ms 7732 KB Output is correct
4 Correct 460 ms 69604 KB Output is correct
5 Correct 44 ms 7668 KB Output is correct
6 Correct 42 ms 7808 KB Output is correct
7 Correct 42 ms 7780 KB Output is correct
8 Correct 45 ms 7756 KB Output is correct
9 Correct 40 ms 7784 KB Output is correct
10 Correct 40 ms 7768 KB Output is correct
11 Correct 41 ms 7760 KB Output is correct
12 Correct 39 ms 7720 KB Output is correct
13 Correct 43 ms 7752 KB Output is correct
14 Correct 430 ms 69660 KB Output is correct
15 Correct 42 ms 7816 KB Output is correct
16 Correct 406 ms 69632 KB Output is correct
17 Correct 401 ms 69600 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 5 ms 1260 KB Output is correct
3 Correct 5 ms 1236 KB Output is correct
4 Correct 0 ms 328 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 1 ms 324 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 212 KB Output is correct
11 Correct 1 ms 324 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 0 ms 340 KB Output is correct
16 Correct 4 ms 1216 KB Output is correct
17 Correct 44 ms 7752 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
21 Correct 1 ms 324 KB Output is correct
22 Correct 1 ms 212 KB Output is correct
23 Correct 1 ms 328 KB Output is correct
24 Correct 0 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 376 KB Output is correct
28 Correct 303 ms 57712 KB Output is correct
29 Correct 0 ms 340 KB Output is correct
30 Correct 405 ms 51060 KB Output is correct
31 Correct 392 ms 69540 KB Output is correct
32 Correct 400 ms 69556 KB Output is correct
33 Correct 0 ms 212 KB Output is correct
34 Correct 390 ms 69776 KB Output is correct
35 Correct 391 ms 69596 KB Output is correct
36 Correct 397 ms 69652 KB Output is correct
37 Correct 442 ms 69652 KB Output is correct
38 Correct 0 ms 212 KB Output is correct
39 Correct 46 ms 7752 KB Output is correct
40 Correct 42 ms 7756 KB Output is correct
41 Correct 39 ms 7752 KB Output is correct
42 Correct 41 ms 7708 KB Output is correct
43 Correct 44 ms 7820 KB Output is correct
44 Correct 41 ms 7752 KB Output is correct
45 Correct 39 ms 7704 KB Output is correct
46 Correct 41 ms 7784 KB Output is correct
47 Correct 39 ms 7756 KB Output is correct
48 Correct 45 ms 7776 KB Output is correct
49 Correct 44 ms 7880 KB Output is correct
50 Correct 40 ms 7752 KB Output is correct
51 Correct 405 ms 69660 KB Output is correct
52 Correct 407 ms 69688 KB Output is correct
53 Correct 450 ms 69668 KB Output is correct
54 Correct 44 ms 7756 KB Output is correct
55 Correct 38 ms 7800 KB Output is correct
56 Correct 121 ms 26560 KB Output is correct
57 Correct 1 ms 212 KB Output is correct
58 Correct 42 ms 7772 KB Output is correct
59 Correct 42 ms 7732 KB Output is correct
60 Correct 460 ms 69604 KB Output is correct
61 Correct 44 ms 7668 KB Output is correct
62 Correct 42 ms 7808 KB Output is correct
63 Correct 42 ms 7780 KB Output is correct
64 Correct 45 ms 7756 KB Output is correct
65 Correct 40 ms 7784 KB Output is correct
66 Correct 40 ms 7768 KB Output is correct
67 Correct 41 ms 7760 KB Output is correct
68 Correct 39 ms 7720 KB Output is correct
69 Correct 43 ms 7752 KB Output is correct
70 Correct 430 ms 69660 KB Output is correct
71 Correct 42 ms 7816 KB Output is correct
72 Correct 406 ms 69632 KB Output is correct
73 Correct 401 ms 69600 KB Output is correct
74 Correct 0 ms 212 KB Output is correct
75 Correct 0 ms 212 KB Output is correct
76 Correct 0 ms 212 KB Output is correct
77 Correct 0 ms 212 KB Output is correct
78 Correct 1 ms 320 KB Output is correct
79 Correct 4 ms 1244 KB Output is correct
80 Correct 472 ms 69704 KB Output is correct
81 Correct 462 ms 69672 KB Output is correct
82 Correct 428 ms 69492 KB Output is correct
83 Correct 434 ms 69240 KB Output is correct
84 Correct 446 ms 68820 KB Output is correct
85 Correct 438 ms 68644 KB Output is correct
86 Correct 127 ms 26604 KB Output is correct
87 Correct 1 ms 328 KB Output is correct