# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
497898 | AQ0212 | Chessboard (IZhO18_chessboard) | C++17 | 322 ms | 5816 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <sstream>
#include <cstring>
#define ll long long int
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define all(x) x.begin(), x.end()
using namespace std;
ll inf2 = 3e18;
ll x1[ 100111 ], x2[ 100111 ];
ll y11[ 100111 ], y2[ 100111 ], cnt_move[ 100111 ], ans = inf2;
vector < ll > divv;
map < ll , ll > mp;
int main(){
ll n, m;
scanf("%lld%lld", &n, &m);
for(int i = 1; i <= m; i ++){
scanf("%lld%lld%lld%lld", &x1[ i ], &y11[ i ], &x2[ i ], &y2[ i ]);
}
divv.pb(1);
for(int i = 2; i <= ((ll)ceil(sqrt(n))); i ++){
if(n % i == 0 && !mp[ i ] && i != n){
divv.pb(i);
mp[ i ] ++;
if(!mp[ n / i ]){
divv.pb(n / i);
mp[ n / i ] ++;
}
}
}
for(int i = 0; i < divv.size(); i ++){
ll sqrs = n / divv[ i ];
cnt_move[ i ] = (sqrs & 1) ? ((((sqrs >> 1) * ((sqrs >> 1) + 1)) << 1) * (divv[ i ] * divv[ i ])) : (sqrs >> 1) * (sqrs) * (divv[ i ] * divv[ i ]);
cnt_move[ i ] = (n * n) - cnt_move[ i ];
}
for(int i = 1; i <= m; i ++){
for(int j = 0; j < divv.size(); j ++){
ll xstart = (x1[ i ] - 1) / (divv[ j ] * 2) * divv[ j ];
ll ystart = (y11[ i ] - 1) / (divv[ j ] * 2) * divv[ j ];
ll xend = x2[ i ] / (divv[ j ] * 2) * divv[ j ];
ll yend = y2[ i ] / (divv[ j ] * 2) * divv[ j ];
xstart += min((x1[ i ] - 1) % (divv[ j ] * 2), divv[ j ]);
ystart += min((y11[ i ] - 1) % (divv[ j ] * 2), divv[ j ]);
xend += min(x2[ i ] % (divv[ j ] * 2), divv[ j ]);
yend += min(y2[ i ] % (divv[ j ] * 2), divv[ j ]);
ll xtot = xend - xstart;
ll ytot = yend - ystart;
ll xadd = (x2[ i ] - x1[ i ] + 1) - xtot;
ll yadd = (y2[ i ] - y11[ i ] + 1) - ytot;
cnt_move[ j ] -= (xtot * ytot) + (xadd * yadd);
cnt_move[ j ] += (xtot * yadd) + (xadd * ytot);
}
}
for(int i = 0; i < divv.size(); i ++){
ans = min(ans, min(cnt_move[ i ], (n * n) - cnt_move[ i ]));
}
printf("%lld", ans);
}
/*
6 8
3 3 3 3
1 2 1 2
3 4 3 4
5 5 5 5
4 3 4 3
4 4 4 4
2 1 2 1
3 6 3 6
6 5
1 2 1 2
5 5 5 5
3 3 4 4
2 1 2 1
3 6 3 6
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |