#include<bits/stdc++.h>
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
*/
using namespace std;
#pragma GCC optimize ("unroll-loops,Ofast,O3")
#pragma GCC target("avx,avx2,fma")
#define F first
#define S second
#define sz(x) (int)x.size()
#define pb push_back
#define int long long
#define eb emplace_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define NFS ios_base::sync_with_stdio(0) , cin.tie(0) , cout.tie(0) ;
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)
//#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
typedef long long ll;
const int E = 1e6+777;
const long long inf = 1e18+777;
const int N = 1e5+777;
const int MOD = 1e9+7;
vector<int> div(int x){
vector<int> res = {1};
for(int i = 2; i * i <= x; ++i){
if(x % i == 0){
res.pb(i);
if(x / i != i)
res.pb(x/i);
}
}
sort(all(res));
return res;
}
struct block{
int x1, y1, x2, y2;
void read(){
cin >> x1 >> y1 >> x2 >> y2;
}
}a[N];
int n, k;
int get1(){
int b[N][2]{};
for(int i = 1; i <= k; ++i){
++b[a[i].x1][a[i].y1&1];
}
int cnt1 = n * n / 2 + (n & 1);
int cnt2 = n * n - cnt1;
int sum1 = 0, sum2 = 0;
for(int i = 1; i <= n; ++i){
if(i & 1){
sum1 += b[i][1];
sum2 += b[i][0];
}else{
sum1 += b[i][0];
sum2 += b[i][1];
}
}
return min(cnt1 - sum1 + sum2, cnt2 - sum2 + sum1);
}
int pref[2111][2111], ar[2111][2111];
void precalc(){
for(int l = 1; l <= k; ++l){
ar[a[l].x1][a[l].y1] = 1;
}
pref[1][1] = ar[1][1];
for(int i = 2; i <= n; i++) {
pref[i][1] = pref[i-1][1] + ar[i][1];
}
for(int i = 2; i <= n; i++) {
pref[1][i] = pref[1][i-1] + ar[1][i];
}
for (int i = 2; i <= n; i++) {
for (int j = 2; j <= n; j++) {
pref[i][j] = pref[i-1][j] + pref[i][j-1] - pref[i-1][j-1] + ar[i][j];
}
}
// cerr << "array\n";
// for(int i = 1; i <= n; ++i){
// for(int j = 1; j <= n; ++j){
// cerr << ar[i][j] << ' ';
// }cerr << '\n';
// }cerr << '\n';
// cerr << "pref\n";
// for(int i = 1; i <= n; ++i){
// for(int j = 1; j <= n; ++j){
// cerr << pref[i][j] << ' ';
// }cerr << '\n';
// }cerr << '\n';
}
long long query(int x1, int y1, int x2, int y2) {
return pref[x2][y2] - pref[x1 - 1][y2] - pref[x2][y1 - 1] + pref[x1 - 1][y1 - 1];
}
int get2(vector<int> &d, int res = LLONG_MAX){
precalc();
for(auto x : d){
int sum1 = 0, sum2 = 0, cnt1 = 0, cnt2 = 0;
bool ok = 1;
for(int i = 1; i <= n; i += x){
for(int j = 1; j <= n; j += x){
int x1 = i, y1 = j, x2 = i + x - 1, y2 = j + x - 1;
int cur = query(x1, y1, x2, y2);
cerr << i << ' ' << j << ": " << ok << '\n';
if(ok & 1){
sum1 += cur;
cnt1 += x * x;
}else{
sum2 += cur;
cnt2 += x * x;
}
ok ^= 1;
}
if((n / x) % 2 == 0) ok ^= 1;
// cerr <<"2i: " << i << " ok: " << ok << '\n';
}
// cerr << "X: " << x << " cnt1: " << cnt1 << " sum1: " << sum1 << " cnt2: " << cnt2 << " sum2: " << sum2 << '\n';
res = min({res, cnt1 - sum1 + sum2, cnt2 - sum2 + sum1});
}
return res;
}
signed main(){auto solve=[&](){
cin >> n >> k;
for(int i = 1; i <= k; ++i){
a[i].read();
}
vector<int> d = div(n);
// cerr << "D: ";for(auto it : d){
// cerr << it << ' ';
// }cerr << '\n';
if(sz(d) == 1){
cout << get1() << '\n';
}else{
cout << get2(d);
}
};NFS;solve();}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1868 KB |
Output is correct |
2 |
Correct |
117 ms |
844 KB |
Output is correct |
3 |
Correct |
97 ms |
860 KB |
Output is correct |
4 |
Correct |
102 ms |
884 KB |
Output is correct |
5 |
Correct |
1 ms |
1868 KB |
Output is correct |
6 |
Correct |
107 ms |
860 KB |
Output is correct |
7 |
Correct |
67 ms |
836 KB |
Output is correct |
8 |
Correct |
91 ms |
820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
3776 KB |
Output is correct |
2 |
Correct |
11 ms |
2400 KB |
Output is correct |
3 |
Correct |
13 ms |
3112 KB |
Output is correct |
4 |
Correct |
18 ms |
3276 KB |
Output is correct |
5 |
Correct |
18 ms |
3532 KB |
Output is correct |
6 |
Correct |
13 ms |
2992 KB |
Output is correct |
7 |
Correct |
5 ms |
2124 KB |
Output is correct |
8 |
Correct |
13 ms |
2892 KB |
Output is correct |
9 |
Correct |
27 ms |
4688 KB |
Output is correct |
10 |
Correct |
15 ms |
3400 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
1096 KB |
Output is correct |
2 |
Correct |
51 ms |
796 KB |
Output is correct |
3 |
Correct |
77 ms |
1056 KB |
Output is correct |
4 |
Correct |
93 ms |
1100 KB |
Output is correct |
5 |
Correct |
46 ms |
1092 KB |
Output is correct |
6 |
Correct |
56 ms |
1076 KB |
Output is correct |
7 |
Correct |
62 ms |
1204 KB |
Output is correct |
8 |
Correct |
34 ms |
844 KB |
Output is correct |
9 |
Correct |
79 ms |
1344 KB |
Output is correct |
10 |
Correct |
1 ms |
1868 KB |
Output is correct |
11 |
Correct |
31 ms |
956 KB |
Output is correct |
12 |
Correct |
1 ms |
1868 KB |
Output is correct |
13 |
Correct |
45 ms |
980 KB |
Output is correct |
14 |
Correct |
81 ms |
1088 KB |
Output is correct |
15 |
Correct |
24 ms |
844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
1096 KB |
Output is correct |
2 |
Correct |
51 ms |
796 KB |
Output is correct |
3 |
Correct |
77 ms |
1056 KB |
Output is correct |
4 |
Correct |
93 ms |
1100 KB |
Output is correct |
5 |
Correct |
46 ms |
1092 KB |
Output is correct |
6 |
Correct |
56 ms |
1076 KB |
Output is correct |
7 |
Correct |
62 ms |
1204 KB |
Output is correct |
8 |
Correct |
34 ms |
844 KB |
Output is correct |
9 |
Correct |
79 ms |
1344 KB |
Output is correct |
10 |
Correct |
1 ms |
1868 KB |
Output is correct |
11 |
Correct |
31 ms |
956 KB |
Output is correct |
12 |
Correct |
1 ms |
1868 KB |
Output is correct |
13 |
Correct |
45 ms |
980 KB |
Output is correct |
14 |
Correct |
81 ms |
1088 KB |
Output is correct |
15 |
Correct |
24 ms |
844 KB |
Output is correct |
16 |
Execution timed out |
2062 ms |
25124 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
3776 KB |
Output is correct |
2 |
Correct |
11 ms |
2400 KB |
Output is correct |
3 |
Correct |
13 ms |
3112 KB |
Output is correct |
4 |
Correct |
18 ms |
3276 KB |
Output is correct |
5 |
Correct |
18 ms |
3532 KB |
Output is correct |
6 |
Correct |
13 ms |
2992 KB |
Output is correct |
7 |
Correct |
5 ms |
2124 KB |
Output is correct |
8 |
Correct |
13 ms |
2892 KB |
Output is correct |
9 |
Correct |
27 ms |
4688 KB |
Output is correct |
10 |
Correct |
15 ms |
3400 KB |
Output is correct |
11 |
Correct |
66 ms |
1096 KB |
Output is correct |
12 |
Correct |
51 ms |
796 KB |
Output is correct |
13 |
Correct |
77 ms |
1056 KB |
Output is correct |
14 |
Correct |
93 ms |
1100 KB |
Output is correct |
15 |
Correct |
46 ms |
1092 KB |
Output is correct |
16 |
Correct |
56 ms |
1076 KB |
Output is correct |
17 |
Correct |
62 ms |
1204 KB |
Output is correct |
18 |
Correct |
34 ms |
844 KB |
Output is correct |
19 |
Correct |
79 ms |
1344 KB |
Output is correct |
20 |
Correct |
1 ms |
1868 KB |
Output is correct |
21 |
Correct |
31 ms |
956 KB |
Output is correct |
22 |
Correct |
1 ms |
1868 KB |
Output is correct |
23 |
Correct |
45 ms |
980 KB |
Output is correct |
24 |
Correct |
81 ms |
1088 KB |
Output is correct |
25 |
Correct |
24 ms |
844 KB |
Output is correct |
26 |
Execution timed out |
2062 ms |
25124 KB |
Time limit exceeded |
27 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1868 KB |
Output is correct |
2 |
Correct |
117 ms |
844 KB |
Output is correct |
3 |
Correct |
97 ms |
860 KB |
Output is correct |
4 |
Correct |
102 ms |
884 KB |
Output is correct |
5 |
Correct |
1 ms |
1868 KB |
Output is correct |
6 |
Correct |
107 ms |
860 KB |
Output is correct |
7 |
Correct |
67 ms |
836 KB |
Output is correct |
8 |
Correct |
91 ms |
820 KB |
Output is correct |
9 |
Correct |
19 ms |
3776 KB |
Output is correct |
10 |
Correct |
11 ms |
2400 KB |
Output is correct |
11 |
Correct |
13 ms |
3112 KB |
Output is correct |
12 |
Correct |
18 ms |
3276 KB |
Output is correct |
13 |
Correct |
18 ms |
3532 KB |
Output is correct |
14 |
Correct |
13 ms |
2992 KB |
Output is correct |
15 |
Correct |
5 ms |
2124 KB |
Output is correct |
16 |
Correct |
13 ms |
2892 KB |
Output is correct |
17 |
Correct |
27 ms |
4688 KB |
Output is correct |
18 |
Correct |
15 ms |
3400 KB |
Output is correct |
19 |
Correct |
66 ms |
1096 KB |
Output is correct |
20 |
Correct |
51 ms |
796 KB |
Output is correct |
21 |
Correct |
77 ms |
1056 KB |
Output is correct |
22 |
Correct |
93 ms |
1100 KB |
Output is correct |
23 |
Correct |
46 ms |
1092 KB |
Output is correct |
24 |
Correct |
56 ms |
1076 KB |
Output is correct |
25 |
Correct |
62 ms |
1204 KB |
Output is correct |
26 |
Correct |
34 ms |
844 KB |
Output is correct |
27 |
Correct |
79 ms |
1344 KB |
Output is correct |
28 |
Correct |
1 ms |
1868 KB |
Output is correct |
29 |
Correct |
31 ms |
956 KB |
Output is correct |
30 |
Correct |
1 ms |
1868 KB |
Output is correct |
31 |
Correct |
45 ms |
980 KB |
Output is correct |
32 |
Correct |
81 ms |
1088 KB |
Output is correct |
33 |
Correct |
24 ms |
844 KB |
Output is correct |
34 |
Execution timed out |
2062 ms |
25124 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |