Submission #682887

# Submission time Handle Problem Language Result Execution time Memory
682887 2023-01-17T07:32:56 Z iskhakkutbilim Chessboard (IZhO18_chessboard) C++14
47 / 100
93 ms 15152 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
#define ff first
#define ss second
#define all(a) a.begin(), a.end()
const int N = 1e5;
vector<int> g[N+1];
int odd[N+1], even[N+1];
int n, k;
int dp[1001][1001];
int isprime(int n){
	for(int i =2; i * i <= n; i++){
		if(n%i==0) return 0;
	}
	return 1;
}


int get(int x, int y, int x1, int y1){
	return dp[x1][y1] - dp[x-1][y1] - dp[x1][y-1]+dp[x-1][y-1];
}



/*
6 8
1 3 1 3
1 4 1 4
2 3 2 3
2 4 2 4
3 1 3 1
3 2 3 2
4 1 4 1
4 2 4 2
*/
main(){
	cin >> n >> k;
	int Pr = isprime(n);
	int x[k], y[k], x1[k], y1[k];
	for(int i = 0;i < k; i++){
		cin >> x[i] >> y[i] >> x1[i] >> y1[i];
		if(Pr==0) dp[x[i]][y[i]] = 1;
		odd[x[i]] += (y[i]%2==1);
		even[x[i]]+= (y[i]%2==0);
	}
	int answer = INT_MAX;
//	if(k == 0){
//		for(int i = 1;i * i < n*n; i++){
//			if(n%i==0){
//				int j = n / i, black = 0;
//				for(int c = 1; c <= j; c++){
//					if(c%2==1){
//						black+= (j / 2) * (i*i);
//					}else{
//						black+= (j-(j / 2))*(i*i);
//					}
//				}
//				answer = min(answer, black);
//			}
//		}
//		cout << answer;
//		return 0;
//	}
	if(Pr){
		int S = 0, S1 = 0;
		for(int i = 1;i <= n; i++){
			if(i%2==1){
				S+= even[i];
				S+= ((n+1)/2) - odd[i];
			}else{
				S+= odd[i];
				S+= (n / 2)-even[i];
			}
		}
		for(int i = 1;i <= n; i++){
			if(i%2==1){
				S1+= odd[i];
				S1+= (n / 2)-even[i];
			}else{
				S1+= even[i];
				S1+= ((n+1)/2) - odd[i];
			}
		}
		cout << min(S, S1);
		return 0;
	}
//	cout << "THE matrix is    " << '\n';
//	for(int i = 1;i <= n; i++){
//		for(int j = 1;j <= n; j++) cout << dp[i][j] << ' ';
//		cout << '\n';
//	}
//	cout << '\n' << '\n';
	for(int i = 1;i <= n; i++){
		for(int j = 1;j <= n; j++){
			dp[i][j]+= dp[i-1][j] + dp[i][j-1] - dp[i-1][j-1];

		}

	}
	
	
	
	for(int i = 1;i * i < n*n; i++){
		if(n%i==0){
			int j = n / i, need = 0;
			int col = 1, row = 1;
			for(int c = 1; c <= j; c++, row+= i, col = 1){
				if(c%2==1){
					for(int r = 1; r <= j; r++, col+= i){
						if(row + i-1 > n or col+i-1 > n) assert(false); 
						int black = get(row, col ,row+i-1, col+i-1);
//						if(i ==2)
//						cout << row << ' ' << col << " = " << black << '\n';
						int white = i*i - black;
						if(r%2==1){
							need+= black;
						}else{
							need+= white;
						}
					}					
				}else{
					for(int r = 1; r <= j; r++, col+= i){
						if(row + i-1 > n or col+i-1 > n) assert(false); 
						int black = get(row, col ,row+i-1, col+i-1);
//						if(i == 2)
//						cout << row << ' ' << col << " = " << black << '\n';
						int white = i*i - black;
						if(r%2==0){
							need+= black;
						}else{
							need+= white;
						}
					}
				}
			}
//			if(need == 14) cout << i << '\n';
			answer = min(answer, need);
			need = 0, col = 1, row = 1;
			for(int c = 1; c <= j; c++, row+= i, col = 1){
				if(c%2==0){
					for(int r = 1; r <= j; r++, col+= i){
						if(row + i-1 > n or col+i-1 > n) assert(false); 
						int black = get(row, col ,row+i-1, col+i-1);
						int white = i*i - black;
						if(r%2==1){
							need+= black;
						}else{
							need+= white;
						}
					}
				}else{
					for(int r = 1; r <= j; r++, col+= i){
						if(row + i-1 > n or col+i-1 > n) assert(false); 
						int black = get(row, col ,row+i-1, col+i-1);
						int white = i*i - black;
						if(r%2==0){
							need+= black;
						}else{
							need+= white;
						}
					}
				}
			}
			answer = min(answer, need);
		}
	}
	cout << answer;
	return 0;
}

Compilation message

chessboard.cpp:38:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   38 | main(){
      | ^~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2660 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Correct 2 ms 3028 KB Output is correct
4 Correct 2 ms 3028 KB Output is correct
5 Correct 2 ms 2648 KB Output is correct
6 Correct 2 ms 3028 KB Output is correct
7 Correct 2 ms 3036 KB Output is correct
8 Correct 2 ms 3028 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 6960 KB Output is correct
2 Correct 18 ms 3744 KB Output is correct
3 Correct 42 ms 6112 KB Output is correct
4 Correct 40 ms 4880 KB Output is correct
5 Correct 56 ms 6476 KB Output is correct
6 Correct 37 ms 5232 KB Output is correct
7 Correct 8 ms 3412 KB Output is correct
8 Correct 36 ms 5256 KB Output is correct
9 Correct 87 ms 7988 KB Output is correct
10 Correct 51 ms 5948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 2 ms 2900 KB Output is correct
3 Correct 2 ms 3028 KB Output is correct
4 Correct 2 ms 3048 KB Output is correct
5 Correct 2 ms 3028 KB Output is correct
6 Correct 2 ms 3028 KB Output is correct
7 Correct 2 ms 3028 KB Output is correct
8 Correct 2 ms 2900 KB Output is correct
9 Correct 2 ms 3028 KB Output is correct
10 Correct 2 ms 2656 KB Output is correct
11 Correct 2 ms 2900 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2920 KB Output is correct
14 Correct 2 ms 3028 KB Output is correct
15 Correct 2 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3028 KB Output is correct
2 Correct 2 ms 2900 KB Output is correct
3 Correct 2 ms 3028 KB Output is correct
4 Correct 2 ms 3048 KB Output is correct
5 Correct 2 ms 3028 KB Output is correct
6 Correct 2 ms 3028 KB Output is correct
7 Correct 2 ms 3028 KB Output is correct
8 Correct 2 ms 2900 KB Output is correct
9 Correct 2 ms 3028 KB Output is correct
10 Correct 2 ms 2656 KB Output is correct
11 Correct 2 ms 2900 KB Output is correct
12 Correct 2 ms 2644 KB Output is correct
13 Correct 2 ms 2920 KB Output is correct
14 Correct 2 ms 3028 KB Output is correct
15 Correct 2 ms 2900 KB Output is correct
16 Correct 32 ms 11424 KB Output is correct
17 Correct 67 ms 6560 KB Output is correct
18 Correct 91 ms 15152 KB Output is correct
19 Correct 87 ms 14336 KB Output is correct
20 Correct 93 ms 14792 KB Output is correct
21 Correct 65 ms 6500 KB Output is correct
22 Correct 12 ms 10452 KB Output is correct
23 Correct 45 ms 12216 KB Output is correct
24 Correct 84 ms 14664 KB Output is correct
25 Correct 17 ms 10632 KB Output is correct
26 Correct 56 ms 12744 KB Output is correct
27 Correct 64 ms 13440 KB Output is correct
28 Correct 88 ms 14672 KB Output is correct
29 Correct 26 ms 4180 KB Output is correct
30 Correct 14 ms 10396 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 66 ms 6960 KB Output is correct
2 Correct 18 ms 3744 KB Output is correct
3 Correct 42 ms 6112 KB Output is correct
4 Correct 40 ms 4880 KB Output is correct
5 Correct 56 ms 6476 KB Output is correct
6 Correct 37 ms 5232 KB Output is correct
7 Correct 8 ms 3412 KB Output is correct
8 Correct 36 ms 5256 KB Output is correct
9 Correct 87 ms 7988 KB Output is correct
10 Correct 51 ms 5948 KB Output is correct
11 Correct 2 ms 3028 KB Output is correct
12 Correct 2 ms 2900 KB Output is correct
13 Correct 2 ms 3028 KB Output is correct
14 Correct 2 ms 3048 KB Output is correct
15 Correct 2 ms 3028 KB Output is correct
16 Correct 2 ms 3028 KB Output is correct
17 Correct 2 ms 3028 KB Output is correct
18 Correct 2 ms 2900 KB Output is correct
19 Correct 2 ms 3028 KB Output is correct
20 Correct 2 ms 2656 KB Output is correct
21 Correct 2 ms 2900 KB Output is correct
22 Correct 2 ms 2644 KB Output is correct
23 Correct 2 ms 2920 KB Output is correct
24 Correct 2 ms 3028 KB Output is correct
25 Correct 2 ms 2900 KB Output is correct
26 Correct 32 ms 11424 KB Output is correct
27 Correct 67 ms 6560 KB Output is correct
28 Correct 91 ms 15152 KB Output is correct
29 Correct 87 ms 14336 KB Output is correct
30 Correct 93 ms 14792 KB Output is correct
31 Correct 65 ms 6500 KB Output is correct
32 Correct 12 ms 10452 KB Output is correct
33 Correct 45 ms 12216 KB Output is correct
34 Correct 84 ms 14664 KB Output is correct
35 Correct 17 ms 10632 KB Output is correct
36 Correct 56 ms 12744 KB Output is correct
37 Correct 64 ms 13440 KB Output is correct
38 Correct 88 ms 14672 KB Output is correct
39 Correct 26 ms 4180 KB Output is correct
40 Correct 14 ms 10396 KB Output is correct
41 Runtime error 7 ms 10452 KB Execution killed with signal 11
42 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2660 KB Output is correct
2 Correct 2 ms 3028 KB Output is correct
3 Correct 2 ms 3028 KB Output is correct
4 Correct 2 ms 3028 KB Output is correct
5 Correct 2 ms 2648 KB Output is correct
6 Correct 2 ms 3028 KB Output is correct
7 Correct 2 ms 3036 KB Output is correct
8 Correct 2 ms 3028 KB Output is correct
9 Correct 66 ms 6960 KB Output is correct
10 Correct 18 ms 3744 KB Output is correct
11 Correct 42 ms 6112 KB Output is correct
12 Correct 40 ms 4880 KB Output is correct
13 Correct 56 ms 6476 KB Output is correct
14 Correct 37 ms 5232 KB Output is correct
15 Correct 8 ms 3412 KB Output is correct
16 Correct 36 ms 5256 KB Output is correct
17 Correct 87 ms 7988 KB Output is correct
18 Correct 51 ms 5948 KB Output is correct
19 Correct 2 ms 3028 KB Output is correct
20 Correct 2 ms 2900 KB Output is correct
21 Correct 2 ms 3028 KB Output is correct
22 Correct 2 ms 3048 KB Output is correct
23 Correct 2 ms 3028 KB Output is correct
24 Correct 2 ms 3028 KB Output is correct
25 Correct 2 ms 3028 KB Output is correct
26 Correct 2 ms 2900 KB Output is correct
27 Correct 2 ms 3028 KB Output is correct
28 Correct 2 ms 2656 KB Output is correct
29 Correct 2 ms 2900 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 2 ms 2920 KB Output is correct
32 Correct 2 ms 3028 KB Output is correct
33 Correct 2 ms 2900 KB Output is correct
34 Correct 32 ms 11424 KB Output is correct
35 Correct 67 ms 6560 KB Output is correct
36 Correct 91 ms 15152 KB Output is correct
37 Correct 87 ms 14336 KB Output is correct
38 Correct 93 ms 14792 KB Output is correct
39 Correct 65 ms 6500 KB Output is correct
40 Correct 12 ms 10452 KB Output is correct
41 Correct 45 ms 12216 KB Output is correct
42 Correct 84 ms 14664 KB Output is correct
43 Correct 17 ms 10632 KB Output is correct
44 Correct 56 ms 12744 KB Output is correct
45 Correct 64 ms 13440 KB Output is correct
46 Correct 88 ms 14672 KB Output is correct
47 Correct 26 ms 4180 KB Output is correct
48 Correct 14 ms 10396 KB Output is correct
49 Runtime error 7 ms 10452 KB Execution killed with signal 11
50 Halted 0 ms 0 KB -