Submission #81946

# Submission time Handle Problem Language Result Execution time Memory
81946 2018-10-28T08:26:21 Z Good Chessboard (IZhO18_chessboard) C++11
70 / 100
807 ms 62880 KB
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define ff first
#define ss second
#define Maxn 100009
#define ll long long
#define pb push_back
#define Inf 1000000009
#define ppb() pop_back()
#define pii pair <int , int>
#define mid(x, y) (x + y) / 2
#define all(x) x.begin(),x.end()
#define llInf 1000000000000000009
#define tr(i, c) for(__typeof(c).begin() i = (c).begin() ; i != (c).end() ; i++)
using namespace std;
using namespace __gnu_pbds;
typedef tree <int, null_type, less <int>, rb_tree_tag, tree_order_statistics_node_update> order;

int n, k;
pii x[Maxn];
pii y[Maxn];
ll ans = llInf;

vector <int> A;

bool calc (int x, int y, int z) {
	x --, y --;
	return ((x / z) % 2) ^ ((y / z) % 2);
}


int main () {
	//freopen ("file.in", "r", stdin);
	//freopen ("file.out", "w", stdout);
	
 	//srand ((unsigned) time ( NULL ));
	//int randomNumber = rand() % 10 + 1;

	scanf ("%d%d", &n, &k);

	for (int i = 1; i <= k; i++)
		scanf ("%d%d%d%d", &x[i].ff, &y[i].ff, &x[i].ss, &y[i].ss);	


	for (int i = 1; i < n; i++)
		if (!(n % i))
			A.pb (i);

	for (auto j: A) {
		ll v = j;
		ll u = n / j;
		
		ll sm1[2], sm2[2];
		sm1[0] = sm2[1] = 0;
		
		sm1[1] = (((u * u) + 1) / 2) * v * v;
		sm2[0] = ((u * u) / 2) * v * v;
																
		for (int i = 1; i <= k; i++) {
			ll a = min ((j - (x[i].ff - 1) % j) % j, x[i].ss - x[i].ff + 1);
			ll b = x[i].ss % j;
			ll c = min ((j - (y[i].ff - 1) % j) % j, y[i].ss - y[i].ff + 1);
			ll d = y[i].ss % j;
			
			if (x[i].ff + a > x[i].ss)
				b = 0;
			if (y[i].ff + c > y[i].ss)
				d = 0;	
						
			bool e = calc (x[i].ff, y[i].ff, j);
			sm1[e] += a * c, sm2[e] += a * c;
			
			e = calc (x[i].ff, y[i].ff + c, j);
			ll t = (y[i].ss - y[i].ff - c + 1) / j;
			ll r = (t + 1) / 2 * a * j;
			sm1[e] += r, sm2[e] += r;
			r = t / 2 * a * j;
			sm1[!e] += r, sm2[!e] += r;
			
			e = calc (x[i].ff, y[i].ss, j);
			sm1[e] += a * d, sm2[e] += a * d;
						
			ll xx1 = x[i].ff + a;
			if (xx1 > x[i].ss)
				continue;
			
			e = calc (x[i].ss, y[i].ff, j);
			sm1[e] += b * c, sm2[e] += b * c;
			
			e = calc (x[i].ss, y[i].ff + c, j);
			r = (t + 1) / 2 * b * j;
			sm1[e] += r, sm2[e] += r;
			r = t / 2 * b * j;
			sm1[!e] += r, sm2[!e] += r;
			
			e = calc (x[i].ss, y[i].ss, j);
			sm1[e] += b * d, sm2[e] += b * d;			
			
			ll xx2 = x[i].ss - b;
			if (xx1 > xx2)
				continue;
			
			e = calc (xx1, y[i].ff, j);
			t = (xx2 - xx1 + 1) / j;
			r = (t + 1) / 2 * c * j;
			sm1[e] += r, sm2[e] += r;
			r = t / 2 * c * j;
			sm1[!e] += r, sm2[!e] += r;
			
			ll yy1 = y[i].ff + c;
			if (yy1 > y[i].ss)
				continue;
			
			e = calc (xx1, y[i].ss, j);
			r = (t + 1) / 2 * d * j;
			sm1[e] += r, sm2[e] += r;
			r = t / 2 * d * j;
			sm1[!e] += r, sm2[!e] += r;
			
			ll yy2 = y[i].ss - d;
			if (yy1 > yy2)
				continue;
									
			e = calc (xx1, yy1, j);
			t = (yy2 - yy1 + 1) / j;
			ll t1 = (xx2 - xx1 + 1) / j;
			r = ((t + 1) * (t1 + 1) / 4 + t * t1 / 4) * j * j;
			sm1[e] += r, sm2[e] += r;
			r = (t * (t1 + 1) / 4 + (t + 1) * t1 / 4) * j * j;
			sm1[!e] += r, sm2[!e] += r;
		}
		
		ans = min (ans, min (sm1[1] - sm1[0], sm2[0] - sm2[1]));
	}	

	printf ("%lld\n", ans);
	return 0;
}

Compilation message

chessboard.cpp: In function 'int main()':
chessboard.cpp:40:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf ("%d%d", &n, &k);
  ~~~~~~^~~~~~~~~~~~~~~~
chessboard.cpp:43:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf ("%d%d%d%d", &x[i].ff, &y[i].ff, &x[i].ss, &y[i].ss); 
   ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 456 KB Output is correct
4 Correct 2 ms 456 KB Output is correct
5 Correct 2 ms 548 KB Output is correct
6 Correct 2 ms 576 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 780 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 3096 KB Output is correct
2 Correct 11 ms 3096 KB Output is correct
3 Correct 27 ms 4044 KB Output is correct
4 Correct 27 ms 4984 KB Output is correct
5 Correct 36 ms 6404 KB Output is correct
6 Correct 26 ms 6880 KB Output is correct
7 Correct 7 ms 6880 KB Output is correct
8 Correct 40 ms 7868 KB Output is correct
9 Correct 55 ms 10880 KB Output is correct
10 Correct 32 ms 11400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11400 KB Output is correct
2 Correct 2 ms 11400 KB Output is correct
3 Correct 2 ms 11400 KB Output is correct
4 Correct 3 ms 11400 KB Output is correct
5 Correct 3 ms 11400 KB Output is correct
6 Correct 3 ms 11400 KB Output is correct
7 Correct 3 ms 11400 KB Output is correct
8 Correct 2 ms 11400 KB Output is correct
9 Correct 3 ms 11400 KB Output is correct
10 Correct 2 ms 11400 KB Output is correct
11 Correct 2 ms 11400 KB Output is correct
12 Correct 2 ms 11400 KB Output is correct
13 Correct 4 ms 11400 KB Output is correct
14 Correct 3 ms 11400 KB Output is correct
15 Correct 3 ms 11400 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 11400 KB Output is correct
2 Correct 2 ms 11400 KB Output is correct
3 Correct 2 ms 11400 KB Output is correct
4 Correct 3 ms 11400 KB Output is correct
5 Correct 3 ms 11400 KB Output is correct
6 Correct 3 ms 11400 KB Output is correct
7 Correct 3 ms 11400 KB Output is correct
8 Correct 2 ms 11400 KB Output is correct
9 Correct 3 ms 11400 KB Output is correct
10 Correct 2 ms 11400 KB Output is correct
11 Correct 2 ms 11400 KB Output is correct
12 Correct 2 ms 11400 KB Output is correct
13 Correct 4 ms 11400 KB Output is correct
14 Correct 3 ms 11400 KB Output is correct
15 Correct 3 ms 11400 KB Output is correct
16 Correct 20 ms 11552 KB Output is correct
17 Correct 48 ms 13820 KB Output is correct
18 Correct 65 ms 15720 KB Output is correct
19 Correct 181 ms 16988 KB Output is correct
20 Correct 205 ms 18816 KB Output is correct
21 Correct 46 ms 19772 KB Output is correct
22 Correct 3 ms 19772 KB Output is correct
23 Correct 42 ms 19812 KB Output is correct
24 Correct 63 ms 22036 KB Output is correct
25 Correct 10 ms 22036 KB Output is correct
26 Correct 41 ms 22608 KB Output is correct
27 Correct 60 ms 23780 KB Output is correct
28 Correct 77 ms 25776 KB Output is correct
29 Correct 21 ms 25776 KB Output is correct
30 Correct 4 ms 25776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 3096 KB Output is correct
2 Correct 11 ms 3096 KB Output is correct
3 Correct 27 ms 4044 KB Output is correct
4 Correct 27 ms 4984 KB Output is correct
5 Correct 36 ms 6404 KB Output is correct
6 Correct 26 ms 6880 KB Output is correct
7 Correct 7 ms 6880 KB Output is correct
8 Correct 40 ms 7868 KB Output is correct
9 Correct 55 ms 10880 KB Output is correct
10 Correct 32 ms 11400 KB Output is correct
11 Correct 2 ms 11400 KB Output is correct
12 Correct 2 ms 11400 KB Output is correct
13 Correct 2 ms 11400 KB Output is correct
14 Correct 3 ms 11400 KB Output is correct
15 Correct 3 ms 11400 KB Output is correct
16 Correct 3 ms 11400 KB Output is correct
17 Correct 3 ms 11400 KB Output is correct
18 Correct 2 ms 11400 KB Output is correct
19 Correct 3 ms 11400 KB Output is correct
20 Correct 2 ms 11400 KB Output is correct
21 Correct 2 ms 11400 KB Output is correct
22 Correct 2 ms 11400 KB Output is correct
23 Correct 4 ms 11400 KB Output is correct
24 Correct 3 ms 11400 KB Output is correct
25 Correct 3 ms 11400 KB Output is correct
26 Correct 20 ms 11552 KB Output is correct
27 Correct 48 ms 13820 KB Output is correct
28 Correct 65 ms 15720 KB Output is correct
29 Correct 181 ms 16988 KB Output is correct
30 Correct 205 ms 18816 KB Output is correct
31 Correct 46 ms 19772 KB Output is correct
32 Correct 3 ms 19772 KB Output is correct
33 Correct 42 ms 19812 KB Output is correct
34 Correct 63 ms 22036 KB Output is correct
35 Correct 10 ms 22036 KB Output is correct
36 Correct 41 ms 22608 KB Output is correct
37 Correct 60 ms 23780 KB Output is correct
38 Correct 77 ms 25776 KB Output is correct
39 Correct 21 ms 25776 KB Output is correct
40 Correct 4 ms 25776 KB Output is correct
41 Correct 157 ms 28160 KB Output is correct
42 Correct 75 ms 30656 KB Output is correct
43 Correct 100 ms 32396 KB Output is correct
44 Correct 68 ms 34680 KB Output is correct
45 Correct 67 ms 36956 KB Output is correct
46 Correct 189 ms 36964 KB Output is correct
47 Correct 60 ms 36964 KB Output is correct
48 Correct 89 ms 36964 KB Output is correct
49 Correct 65 ms 36964 KB Output is correct
50 Correct 703 ms 36964 KB Output is correct
51 Correct 723 ms 37024 KB Output is correct
52 Correct 698 ms 37024 KB Output is correct
53 Correct 731 ms 37060 KB Output is correct
54 Correct 675 ms 37060 KB Output is correct
55 Correct 751 ms 37188 KB Output is correct
56 Correct 656 ms 37188 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 256 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 2 ms 456 KB Output is correct
4 Correct 2 ms 456 KB Output is correct
5 Correct 2 ms 548 KB Output is correct
6 Correct 2 ms 576 KB Output is correct
7 Correct 2 ms 640 KB Output is correct
8 Correct 2 ms 780 KB Output is correct
9 Correct 40 ms 3096 KB Output is correct
10 Correct 11 ms 3096 KB Output is correct
11 Correct 27 ms 4044 KB Output is correct
12 Correct 27 ms 4984 KB Output is correct
13 Correct 36 ms 6404 KB Output is correct
14 Correct 26 ms 6880 KB Output is correct
15 Correct 7 ms 6880 KB Output is correct
16 Correct 40 ms 7868 KB Output is correct
17 Correct 55 ms 10880 KB Output is correct
18 Correct 32 ms 11400 KB Output is correct
19 Correct 2 ms 11400 KB Output is correct
20 Correct 2 ms 11400 KB Output is correct
21 Correct 2 ms 11400 KB Output is correct
22 Correct 3 ms 11400 KB Output is correct
23 Correct 3 ms 11400 KB Output is correct
24 Correct 3 ms 11400 KB Output is correct
25 Correct 3 ms 11400 KB Output is correct
26 Correct 2 ms 11400 KB Output is correct
27 Correct 3 ms 11400 KB Output is correct
28 Correct 2 ms 11400 KB Output is correct
29 Correct 2 ms 11400 KB Output is correct
30 Correct 2 ms 11400 KB Output is correct
31 Correct 4 ms 11400 KB Output is correct
32 Correct 3 ms 11400 KB Output is correct
33 Correct 3 ms 11400 KB Output is correct
34 Correct 20 ms 11552 KB Output is correct
35 Correct 48 ms 13820 KB Output is correct
36 Correct 65 ms 15720 KB Output is correct
37 Correct 181 ms 16988 KB Output is correct
38 Correct 205 ms 18816 KB Output is correct
39 Correct 46 ms 19772 KB Output is correct
40 Correct 3 ms 19772 KB Output is correct
41 Correct 42 ms 19812 KB Output is correct
42 Correct 63 ms 22036 KB Output is correct
43 Correct 10 ms 22036 KB Output is correct
44 Correct 41 ms 22608 KB Output is correct
45 Correct 60 ms 23780 KB Output is correct
46 Correct 77 ms 25776 KB Output is correct
47 Correct 21 ms 25776 KB Output is correct
48 Correct 4 ms 25776 KB Output is correct
49 Correct 157 ms 28160 KB Output is correct
50 Correct 75 ms 30656 KB Output is correct
51 Correct 100 ms 32396 KB Output is correct
52 Correct 68 ms 34680 KB Output is correct
53 Correct 67 ms 36956 KB Output is correct
54 Correct 189 ms 36964 KB Output is correct
55 Correct 60 ms 36964 KB Output is correct
56 Correct 89 ms 36964 KB Output is correct
57 Correct 65 ms 36964 KB Output is correct
58 Correct 703 ms 36964 KB Output is correct
59 Correct 723 ms 37024 KB Output is correct
60 Correct 698 ms 37024 KB Output is correct
61 Correct 731 ms 37060 KB Output is correct
62 Correct 675 ms 37060 KB Output is correct
63 Correct 751 ms 37188 KB Output is correct
64 Correct 656 ms 37188 KB Output is correct
65 Correct 2 ms 37188 KB Output is correct
66 Correct 2 ms 37188 KB Output is correct
67 Correct 787 ms 39232 KB Output is correct
68 Correct 807 ms 41436 KB Output is correct
69 Correct 673 ms 43220 KB Output is correct
70 Correct 729 ms 45556 KB Output is correct
71 Correct 742 ms 47788 KB Output is correct
72 Correct 719 ms 49896 KB Output is correct
73 Correct 700 ms 51760 KB Output is correct
74 Correct 785 ms 54020 KB Output is correct
75 Correct 744 ms 56208 KB Output is correct
76 Correct 794 ms 58488 KB Output is correct
77 Correct 158 ms 60876 KB Output is correct
78 Incorrect 72 ms 62880 KB Output isn't correct
79 Halted 0 ms 0 KB -