#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define ub upper_bound
#define lb lower_bound
#define ll long long
#define ld long double
#define pii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define prc(n) fixed << setprecision(n)
#define fastios ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pi acos(-1);
const int inf = 1e9+7;
const int N = 1e3+5;
int x[N][N], pref[N][N];
ll ans, n, k;
void check(ll size){
//cout<<size<<"\n";
ll cnt = 0;
ll q = size * size;
for(int i=size;i<=n;i+=size){
for(int j=size;j<=n;j+=size){
if(((j/size) + (i/size))%2 == 1) cnt += q - (pref[i][j] - pref[i - size][j] - pref[i][j - size] + pref[i - size][j - size]);
else cnt += (pref[i][j] - pref[i - size][j] - pref[i][j - size] + pref[i - size][j - size]);
// cout<<cnt<<" ";
}
}
ans = min((ll)cnt, ans);
cnt = 0;
for(int i=size;i<=n;i+=size){
for(int j=size;j<=n;j+=size){
if(((j/size) + (i/size))%2 == 0) cnt += q - (pref[i][j] - pref[i - size][j] - pref[i][j - size] + pref[i - size][j - size]);
else cnt += (pref[i][j] - pref[i - size][j] - pref[i][j - size] + pref[i - size][j - size]);
// cout<<cnt<<" ";
}//cout<<"\n";
}
//cout<<cnt<<"\n";
ans = min((ll)cnt, ans);
}
void solve(){
fastios
ans = inf;
cin>>n>>k;
if(k == 0){
ans = n*n/2;
for(int i=2;i*1ll*i<=n;i++){
if(n % i == 0){
ll num = n/i;
ans = min(ans, num * num /2 * i * i);
num = i;
ans = min(ans, num * num /2 * (n/i) * (n/i));
}
}
cout<<ans<<"\n";
return;
}
for(int i=0;i<k;i++){
int a, b, c, d;
cin>>a>>b>>c>>d;
x[a][b] = 1;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
pref[i][j] = pref[i-1][j] + pref[i][j-1] + x[i][j] - pref[i-1][j-1];
}
}
for(int i=1;i * 1ll * i<=n;i++){
if(n % i == 0){
check(i);
if(i != 1 && n/i != i) check(n/i);
}
}
cout<<ans;
return;
}
/*
3 3
1 3 1 3
2 3 3 3
1 1 3 1
5 8
2 1 2 1
3 1 3 1
5 2 5 2
1 5 1 5
2 3 2 3
2 4 2 4
3 3 3 3
4 3 4 3
6 8
1 2 1 2
2 1 2 1
3 3 3 3
3 4 3 4
4 3 4 3
4 4 4 4
3 6 3 6
5 5 5 5
*/
int main(){
fastios
int t = 1;
//cin>>t;
while(t--) solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
492 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
876 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1004 KB |
Output is correct |
7 |
Correct |
1 ms |
1004 KB |
Output is correct |
8 |
Correct |
1 ms |
876 KB |
Output is correct |
9 |
Correct |
1 ms |
1132 KB |
Output is correct |
10 |
Correct |
1 ms |
876 KB |
Output is correct |
11 |
Correct |
1 ms |
876 KB |
Output is correct |
12 |
Correct |
1 ms |
748 KB |
Output is correct |
13 |
Correct |
1 ms |
876 KB |
Output is correct |
14 |
Correct |
1 ms |
1004 KB |
Output is correct |
15 |
Correct |
1 ms |
748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1004 KB |
Output is correct |
2 |
Correct |
1 ms |
748 KB |
Output is correct |
3 |
Correct |
1 ms |
876 KB |
Output is correct |
4 |
Correct |
1 ms |
1004 KB |
Output is correct |
5 |
Correct |
1 ms |
1004 KB |
Output is correct |
6 |
Correct |
1 ms |
1004 KB |
Output is correct |
7 |
Correct |
1 ms |
1004 KB |
Output is correct |
8 |
Correct |
1 ms |
876 KB |
Output is correct |
9 |
Correct |
1 ms |
1132 KB |
Output is correct |
10 |
Correct |
1 ms |
876 KB |
Output is correct |
11 |
Correct |
1 ms |
876 KB |
Output is correct |
12 |
Correct |
1 ms |
748 KB |
Output is correct |
13 |
Correct |
1 ms |
876 KB |
Output is correct |
14 |
Correct |
1 ms |
1004 KB |
Output is correct |
15 |
Correct |
1 ms |
748 KB |
Output is correct |
16 |
Correct |
32 ms |
7788 KB |
Output is correct |
17 |
Correct |
64 ms |
8172 KB |
Output is correct |
18 |
Correct |
57 ms |
8172 KB |
Output is correct |
19 |
Correct |
61 ms |
7928 KB |
Output is correct |
20 |
Correct |
75 ms |
7916 KB |
Output is correct |
21 |
Correct |
49 ms |
8172 KB |
Output is correct |
22 |
Correct |
26 ms |
5740 KB |
Output is correct |
23 |
Correct |
38 ms |
8044 KB |
Output is correct |
24 |
Correct |
52 ms |
8172 KB |
Output is correct |
25 |
Correct |
25 ms |
7916 KB |
Output is correct |
26 |
Correct |
39 ms |
7916 KB |
Output is correct |
27 |
Correct |
48 ms |
7916 KB |
Output is correct |
28 |
Correct |
63 ms |
7916 KB |
Output is correct |
29 |
Correct |
33 ms |
8044 KB |
Output is correct |
30 |
Correct |
25 ms |
7916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
492 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Runtime error |
1 ms |
492 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
10 |
Halted |
0 ms |
0 KB |
- |