Let's assume that we are given a matrix b of size x × y, let's determine the operation of mirroring matrix b. The mirroring of matrix b is a2x × y matrix c which has the following properties:
- the upper half of matrix c (rows with numbers from 1 to x) exactly matches b;
- the lower half of matrix c (rows with numbers from x + 1 to 2x) is symmetric to the upper one; the symmetry line is the line that separates two halves (the line that goes in the middle, between rows x and x + 1).
Sereja has an n × m matrix a. He wants to find such matrix b, that it can be transformed into matrix a, if we'll perform on it several(possibly zero) mirrorings. What minimum number of rows can such matrix contain?
The first line contains two integers, n and m (1 ≤ n, m ≤ 100). Each of the next n lines contains m integers — the elements of matrix a. The i-th line contains integers ai1, ai2, ..., aim (0 ≤ aij ≤ 1) — the i-th row of the matrix a.
In the single line, print the answer to the problem — the minimum number of rows of matrix b.
4 3 0 0 1 1 1 0 1 1 0 0 0 1
2
3 3 0 0 0 0 0 0 0 0 0
3
8 1 0 1 1 0 0 1 1 0
2
#include<iostream>
using namespace std;
const int M = 220;
int nums[M][M], res, n,m;
void findans(int a)
{
if(a % 2)
return ;
for(int i = 0, j = a - 1; i < a / 2; i++, j--)
for(int k = 0; k < m; k++)
if(nums[i][k] != nums[j][k])
return ;
res /= 2;
findans(a / 2);
}
main()
{
cin >> n >> m;
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++)
cin >> nums[i][j];
res = n;
findans(res);
cout << res << endl;
}
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int M = 256;
int s[M], a[M], in[M], out[M];
main()
{
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
cin >> a[i];
s[i] = s[i - 1] + a[i];
}
int ans = -1024;
for(int i = 1; i <= n; i++)
for(int j = i; j <= n; j++)
{
int sum = s[j] - s[i-1];
int cnt1 = 0, cnt2 = 0;
for(int k = i; k <= j; k++)
in[cnt1++] = a[k];
for(int k = 1; k < i; k++)
out[cnt2++] = a[k];
for(int k = j + 1; k <= n; k++)
out[cnt2++] = a[k];
sort(in, in + cnt1);
sort(out, out + cnt2);
int q = 0, p = cnt2 - 1;
while(q < m && q < j - i + 1 && p >= 0 && in[q] < out[p])
sum = sum - in[q++] + out[p--];
ans = max(ans, sum);
}
cout << ans << endl;
}