Submission #857306

# Submission time Handle Problem Language Result Execution time Memory
857306 2023-10-05T20:37:10 Z NaimSS Chessboard (IZhO18_chessboard) C++14
70 / 100
1178 ms 6544 KB
#include <bits/stdc++.h>
#include <iostream>
// #define int long long
#define ld long double
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define pb push_back
#define mp(a,b) make_pair(a,b)
#define ms(v,x) memset(v,x,sizeof(v))
#define all(v) v.begin(),v.end()
#define ff first
#define ss second
#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define per(i, a, b) for(int i = b-1; i>=a ; i--)
#define trav(a, x) for(auto& a : x)
#define allin(a , x) for(auto a : x)
#define Unique(v) sort(all(v)),v.erase(unique(all(v)),v.end());
#define sz(v) ((int)v.size())
using namespace std;
typedef vector<int> vi;
#define y1 abacaba
//#define left oooooopss
#define db(x) cerr << #x <<" == "<<x << endl;
#define db2(x,y) cerr<<#x <<" == "<<x<<", "<<#y<<" == "<<y<<endl;
#define db3(x,y,z) cerr << #x<<" == "<<x<<", "<<#y<<" == "<<y<<", "<<#z<<" == "<<z<<endl;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<ll> vl;
// std::mt19937_64 rng64((int) std::chrono::steady_clock::now().time_since_epoch().count());
std::mt19937 rng(
  
//  (int) std::chrono::steady_clock::now().time_since_epoch().count()
   1239010
);
ll cdiv(ll a, ll b) { return a/b+((a^b)>0&&a%b); } // divide a by b rounded up
ll fdiv(ll a, ll b) { return a/b-((a^b)<0&&a%b); } // divide a by b rounded down
//inline ll mod(ll n, ll m ){ ll ret = n%m; if(ret < 0) ret += m; return ret; }
ll gcd(ll a, ll b){return (b == 0LL ? a : gcd(b, a%b));}
ll exp(ll b,ll e,ll m){
    b%=m;
    ll ans = 1;
    for (; e; b = b * b % m, e /= 2)
        if (e & 1) ans = ans * b % m;
    return ans;
}
// debug:
template<class T>void print_vector(vector<T> &v){
    rep(i,0,sz(v))cout<<v[i]<<" \n"[i==sz(v)-1];
}
void dbg_out() {cerr << endl; }
template<typename Head, typename ... Tail> void dbg_out(Head H,Tail... T){
    cerr << ' ' << H;
    dbg_out(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__), cerr << endl
#else
#define dbg(...) 42
#endif
//
const int N = 100100;
ll x[N][2] , y[N][2];
int n,k;
ll solve(int d,int ini){
	ll L = n / d;
	ll totW = (L * L + 1)/2 * d * d;
	ll totB = (L*L)/2 * d * d;
	if(ini==1)swap(totW,totB);
	ll res = totB;
	auto add = [&](ll totW,ll totB,int inic){
		if(inic)swap(totW,totB);
		res -= totB;
		res += totW;
		return ;
	};
	auto go2 = [&](pll P1,pll P2){
		if(P1.ff <= P2.ff && P1.ss <= P2.ss){
			int inic = ((P1.ff/d + P1.ss/d)& 1)^ini;
			ll l = P2.ff - P1.ff + 1;
			
			if(P1.ss%d != 0){
				ll nxt = P1.ss + (d - P1.ss%d) - 1;
				nxt = min(nxt, P2.ss);
				ll h = (nxt - P1.ss + 1);
				ll q = h * l;
				// dbg((P1.ff/d) + (P2.ff/d));
				// dbg(inic, q);
				add(q, 0, inic);
				P1.ss = nxt + 1;
			}
			if(P2.ss % d != d-1){
				ll prv = max(P1.ss, (P2.ss - (P2.ss%d) - 1) + 1);
				ll h = (P2.ss - prv + 1);
				ll q = h * l;
				int iniC = ini^((prv/d + P1.ff/d)&1);
				add(q,0,iniC);
				P2.ss = prv-1;
			}
			if(P1.ff <= P2.ff && P1.ss <= P2.ss){
				int inic = ((P1.ff/d + P1.ss/d)& 1)^ini;
				ll h = (P2.ss - P1.ss + 1);
				dbg("here");cerr.flush();
				assert(h%d==0);
				h/=d;
				ll totW = (h + 1)/2 * l * d;
				ll totB = h/2 * l * d;
				add(totW, totB, inic);
			}
		}
	};
	auto go = [&](pll P1, pll P2){
		if(P1.ff <= P2.ff && P1.ss <= P2.ss){
			int inic = ((P1.ff/d + P1.ss/d)& 1)^ini;
			ll l = P2.ff - P1.ff + 1;
			ll h = P2.ss - P1.ss + 1;
			dbg("go", h,l,d);
			assert(l%d == 0);
			l/=d;
			ll totW = (l + 1)/2 * l * d;
			ll totB = l/2 * l * d;
			add(totW,totB,inic);
		}
	};
	rep(i,0,k){
		ll cur = res;

		pll p1;
		if(x[i][0] % d != 0){
			p1.ff = x[i][0] + (d - (x[i][0]%d));
		}else p1.ff = x[i][0];
		p1.ss = y[i][0] + (y[i][0]%d==0 ? 0 : d - (y[i][0]%d));
		pll p2;
		p2.ff = x[i][1] - (x[i][1]%d == d-1 ? 0 : (x[i][1]%d) + 1);
		p2.ss = y[i][1] - (y[i][1]%d == d-1 ? 0 : (y[i][1]%d) + 1);
		
		if(p1.ff <= p2.ff && p1.ss <= p2.ss){
			// all inside:
			ll l = (p2.ff - p1.ff + 1)/d;
			ll h = (p2.ss - p1.ss + 1)/d;
			int inic =(((p1.ff/d)+(p1.ss/d)) &1)^ini;
			ll totW = (l*h + 1)/2 * d * d;
			ll totB = (l*h)/2 * d * d;
			dbg(totW,totB);
			add(totW,totB,inic);
		}
		// upper:
		pll P1 = pll(x[i][0],y[i][0]);
		pll P2 = pll(min(x[i][1],p1.ff -1), y[i][1]);
		go2(P1,P2);
		// lower
		P1 = pll(p2.ff + 1, y[i][0]), P2 = pll(x[i][1], y[i][1]);
		if(P1.ff >= x[i][0])go2(P1,P2);
		// left:
		P1 = pll(p1.ff,y[i][0]), P2 = pll(p2.ff-1, p1.ss-1);
		go(P1,P2);
		// right:
		P1 = pll(p2.ff, p2.ss + 1), P2 = pll(p2.ff-1,y[i][1]);
		go(P1,P2);
		// dbg(res - cur, x[i][0], y[i][0]);
	}
	dbg(d, ini , res);
	
	return res;
}

int32_t main(){
    fastio;
	cin >> n >> k;
	rep(i,0,k){
		rep(j,0,2){
			cin >> x[i][j] >> y[i][j];
			x[i][j]--,y[i][j]--;
		}
	}

	ll res = n * (ll) n;
	for(int d=1;d < n;d++){
		if(n%d == 0){
			res = min(res, solve(d,0));
			res = min(res, solve(d,1));
		}
	}
	cout << res << endl;
	// math -> gcd it all
    // Did you swap N,M? N=1?
}

Compilation message

chessboard.cpp: In lambda function:
chessboard.cpp:58:18: warning: statement has no effect [-Wunused-value]
   58 | #define dbg(...) 42
      |                  ^~
chessboard.cpp:102:5: note: in expansion of macro 'dbg'
  102 |     dbg("here");cerr.flush();
      |     ^~~
chessboard.cpp: In lambda function:
chessboard.cpp:58:18: warning: statement has no effect [-Wunused-value]
   58 | #define dbg(...) 42
      |                  ^~
chessboard.cpp:116:4: note: in expansion of macro 'dbg'
  116 |    dbg("go", h,l,d);
      |    ^~~
chessboard.cpp:115:7: warning: unused variable 'h' [-Wunused-variable]
  115 |    ll h = P2.ss - P1.ss + 1;
      |       ^
chessboard.cpp: In function 'll solve(int, int)':
chessboard.cpp:58:18: warning: statement has no effect [-Wunused-value]
   58 | #define dbg(...) 42
      |                  ^~
chessboard.cpp:143:4: note: in expansion of macro 'dbg'
  143 |    dbg(totW,totB);
      |    ^~~
chessboard.cpp:125:6: warning: unused variable 'cur' [-Wunused-variable]
  125 |   ll cur = res;
      |      ^~~
chessboard.cpp:58:18: warning: statement has no effect [-Wunused-value]
   58 | #define dbg(...) 42
      |                  ^~
chessboard.cpp:161:2: note: in expansion of macro 'dbg'
  161 |  dbg(d, ini , res);
      |  ^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 2944 KB Output is correct
2 Correct 6 ms 2700 KB Output is correct
3 Correct 14 ms 2652 KB Output is correct
4 Correct 15 ms 2696 KB Output is correct
5 Correct 19 ms 2652 KB Output is correct
6 Correct 13 ms 2652 KB Output is correct
7 Correct 3 ms 2396 KB Output is correct
8 Correct 12 ms 2652 KB Output is correct
9 Correct 31 ms 3408 KB Output is correct
10 Correct 18 ms 2652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2532 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2396 KB Output is correct
2 Correct 1 ms 2396 KB Output is correct
3 Correct 1 ms 2396 KB Output is correct
4 Correct 1 ms 2392 KB Output is correct
5 Correct 1 ms 2396 KB Output is correct
6 Correct 1 ms 2396 KB Output is correct
7 Correct 1 ms 2392 KB Output is correct
8 Correct 1 ms 2396 KB Output is correct
9 Correct 1 ms 2396 KB Output is correct
10 Correct 0 ms 2396 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2532 KB Output is correct
14 Correct 1 ms 2396 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 13 ms 2652 KB Output is correct
17 Correct 25 ms 3164 KB Output is correct
18 Correct 45 ms 3416 KB Output is correct
19 Correct 220 ms 3164 KB Output is correct
20 Correct 247 ms 3552 KB Output is correct
21 Correct 23 ms 3160 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 34 ms 2652 KB Output is correct
24 Correct 42 ms 3420 KB Output is correct
25 Correct 7 ms 2652 KB Output is correct
26 Correct 26 ms 2908 KB Output is correct
27 Correct 43 ms 2904 KB Output is correct
28 Correct 43 ms 3420 KB Output is correct
29 Correct 10 ms 2648 KB Output is correct
30 Correct 2 ms 2396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 2944 KB Output is correct
2 Correct 6 ms 2700 KB Output is correct
3 Correct 14 ms 2652 KB Output is correct
4 Correct 15 ms 2696 KB Output is correct
5 Correct 19 ms 2652 KB Output is correct
6 Correct 13 ms 2652 KB Output is correct
7 Correct 3 ms 2396 KB Output is correct
8 Correct 12 ms 2652 KB Output is correct
9 Correct 31 ms 3408 KB Output is correct
10 Correct 18 ms 2652 KB Output is correct
11 Correct 1 ms 2396 KB Output is correct
12 Correct 1 ms 2396 KB Output is correct
13 Correct 1 ms 2396 KB Output is correct
14 Correct 1 ms 2392 KB Output is correct
15 Correct 1 ms 2396 KB Output is correct
16 Correct 1 ms 2396 KB Output is correct
17 Correct 1 ms 2392 KB Output is correct
18 Correct 1 ms 2396 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 0 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2396 KB Output is correct
23 Correct 1 ms 2532 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 1 ms 2396 KB Output is correct
26 Correct 13 ms 2652 KB Output is correct
27 Correct 25 ms 3164 KB Output is correct
28 Correct 45 ms 3416 KB Output is correct
29 Correct 220 ms 3164 KB Output is correct
30 Correct 247 ms 3552 KB Output is correct
31 Correct 23 ms 3160 KB Output is correct
32 Correct 1 ms 2396 KB Output is correct
33 Correct 34 ms 2652 KB Output is correct
34 Correct 42 ms 3420 KB Output is correct
35 Correct 7 ms 2652 KB Output is correct
36 Correct 26 ms 2908 KB Output is correct
37 Correct 43 ms 2904 KB Output is correct
38 Correct 43 ms 3420 KB Output is correct
39 Correct 10 ms 2648 KB Output is correct
40 Correct 2 ms 2396 KB Output is correct
41 Correct 196 ms 3304 KB Output is correct
42 Correct 50 ms 3408 KB Output is correct
43 Correct 106 ms 3460 KB Output is correct
44 Correct 48 ms 3412 KB Output is correct
45 Correct 33 ms 3416 KB Output is correct
46 Correct 220 ms 3460 KB Output is correct
47 Correct 29 ms 3160 KB Output is correct
48 Correct 79 ms 3400 KB Output is correct
49 Correct 45 ms 3320 KB Output is correct
50 Correct 1072 ms 3420 KB Output is correct
51 Correct 1145 ms 3524 KB Output is correct
52 Correct 1059 ms 3668 KB Output is correct
53 Correct 1134 ms 3524 KB Output is correct
54 Correct 1055 ms 3404 KB Output is correct
55 Correct 1178 ms 3632 KB Output is correct
56 Correct 1014 ms 3356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 22 ms 2944 KB Output is correct
10 Correct 6 ms 2700 KB Output is correct
11 Correct 14 ms 2652 KB Output is correct
12 Correct 15 ms 2696 KB Output is correct
13 Correct 19 ms 2652 KB Output is correct
14 Correct 13 ms 2652 KB Output is correct
15 Correct 3 ms 2396 KB Output is correct
16 Correct 12 ms 2652 KB Output is correct
17 Correct 31 ms 3408 KB Output is correct
18 Correct 18 ms 2652 KB Output is correct
19 Correct 1 ms 2396 KB Output is correct
20 Correct 1 ms 2396 KB Output is correct
21 Correct 1 ms 2396 KB Output is correct
22 Correct 1 ms 2392 KB Output is correct
23 Correct 1 ms 2396 KB Output is correct
24 Correct 1 ms 2396 KB Output is correct
25 Correct 1 ms 2392 KB Output is correct
26 Correct 1 ms 2396 KB Output is correct
27 Correct 1 ms 2396 KB Output is correct
28 Correct 0 ms 2396 KB Output is correct
29 Correct 1 ms 2396 KB Output is correct
30 Correct 1 ms 2396 KB Output is correct
31 Correct 1 ms 2532 KB Output is correct
32 Correct 1 ms 2396 KB Output is correct
33 Correct 1 ms 2396 KB Output is correct
34 Correct 13 ms 2652 KB Output is correct
35 Correct 25 ms 3164 KB Output is correct
36 Correct 45 ms 3416 KB Output is correct
37 Correct 220 ms 3164 KB Output is correct
38 Correct 247 ms 3552 KB Output is correct
39 Correct 23 ms 3160 KB Output is correct
40 Correct 1 ms 2396 KB Output is correct
41 Correct 34 ms 2652 KB Output is correct
42 Correct 42 ms 3420 KB Output is correct
43 Correct 7 ms 2652 KB Output is correct
44 Correct 26 ms 2908 KB Output is correct
45 Correct 43 ms 2904 KB Output is correct
46 Correct 43 ms 3420 KB Output is correct
47 Correct 10 ms 2648 KB Output is correct
48 Correct 2 ms 2396 KB Output is correct
49 Correct 196 ms 3304 KB Output is correct
50 Correct 50 ms 3408 KB Output is correct
51 Correct 106 ms 3460 KB Output is correct
52 Correct 48 ms 3412 KB Output is correct
53 Correct 33 ms 3416 KB Output is correct
54 Correct 220 ms 3460 KB Output is correct
55 Correct 29 ms 3160 KB Output is correct
56 Correct 79 ms 3400 KB Output is correct
57 Correct 45 ms 3320 KB Output is correct
58 Correct 1072 ms 3420 KB Output is correct
59 Correct 1145 ms 3524 KB Output is correct
60 Correct 1059 ms 3668 KB Output is correct
61 Correct 1134 ms 3524 KB Output is correct
62 Correct 1055 ms 3404 KB Output is correct
63 Correct 1178 ms 3632 KB Output is correct
64 Correct 1014 ms 3356 KB Output is correct
65 Correct 0 ms 2396 KB Output is correct
66 Correct 1 ms 2396 KB Output is correct
67 Runtime error 35 ms 6544 KB Execution killed with signal 6
68 Halted 0 ms 0 KB -