A. Binary Blocks
time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given an image, that can be represented with a 2-d n by m grid of pixels. Each pixel of the image is either on or off, denoted by the characters "0" or "1", respectively. You would like to compress this image. You want to choose an integer k > 1 and split the image into k by k blocks. If n and m are not divisible by k, the image is padded with only zeros on the right and bottom so that they are divisible by k. Each pixel in each individual block must have the same value. The given image may not be compressible in its current state. Find the minimum number of pixels you need to toggle (after padding) in order for the image to be compressible for some k. More specifically, the steps are to first choose k, then the image is padded with zeros, then, we can toggle the pixels so it is compressible for this k. The image must be compressible in that state.

Input

The first line of input will contain two integers n, m (2 ≤ n, m ≤ 2 500), the dimensions of the image.

The next n lines of input will contain a binary string with exactly m characters, representing the image.

Output

Print a single integer, the minimum number of pixels needed to toggle to make the image compressible.

Example
input
3 5
00100
10110
11001
output
5
Note

We first choose k = 2.

The image is padded as follows:

001000
101100
110010
000000

We can toggle the image to look as follows:

001100
001100
000000
000000

We can see that this image is compressible for k = 2.

题意:给你一个n*m的0,1表,然后让你找到一个整数K去划分这张图,然后可以改变每个划分的图的任意的值,使每一部分的的值相同

题解:枚举k,维护0,1表的前缀和,去快速计算每个分块的总和;下面代码

#include<algorithm>
#include<cstdio>
#include<cstring>
#include<iostream>
#define ll long long
using namespace std;
const int maxn=2e3+5e2+;
const int inf=0x3f3f3f3f;
int mp[maxn][maxn];
char a[maxn];
int n,m;
int main()
{
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++)
{
scanf("%s",a+);
for(int j=;j<=m;j++)
{
if(a[j]=='')
mp[i][j]=;
}
}
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
mp[i][j]=mp[i-][j]+mp[i][j-]+mp[i][j]-mp[i-][j-];
}
}
int ans=inf;
int len=max(n,m);
for(int k=;k<=len;k++)
{
int nn=n,mm=m;
if(n%k!=)nn=(n/k)*k+k;
if(m%k!=)mm=(m/k)*k+k;
int tmp1=;
for(int i=;i<=nn/k;i++)
for(int j=;j<=mm/k;j++)
{
int x1=i*k>n?n:i*k;
int y1=j*k>m?m:j*k;
int x2=i*k-k+>n?n:i*k-k+;
int y2=j*k-k+>m?m:j*k-k+;
int tmp=mp[x1][y1]+mp[x2-][y2-]-mp[x1][y2-]-mp[x2-][y1];
if(tmp<=k*k/)
{
tmp1+=(tmp);
}
else
{
tmp1+=(k*k-tmp);
}
}
ans=min(ans,tmp1);
} printf("%d\n",ans);
}

http://codeforces.com/contest/838/problem/A的更多相关文章

  1. codeforces.com/contest/325/problem/B

    http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...

  2. [E. Ehab's REAL Number Theory Problem](https://codeforces.com/contest/1325/problem/E) 数论+图论 求最小环

    E. Ehab's REAL Number Theory Problem 数论+图论 求最小环 题目大意: 给你一个n大小的数列,数列里的每一个元素满足以下要求: 数据范围是:\(1<=a_i& ...

  3. http://codeforces.com/contest/555/problem/B

    比赛时虽然贪了心,不过后面没想到怎么处理和set的排序方法忘了- -,其实是和优先队列的仿函数一样的... 比赛后用set pair过了... #include <bits/stdc++.h&g ...

  4. http://codeforces.com/contest/610/problem/D

    D. Vika and Segments time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  5. http://codeforces.com/contest/612/problem/D

    D. The Union of k-Segments time limit per test 4 seconds memory limit per test 256 megabytes input s ...

  6. http://codeforces.com/contest/536/problem/B

    B. Tavas and Malekas time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  7. http://codeforces.com/contest/535/problem/C

    C. Tavas and Karafs time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  8. http://codeforces.com/contest/402/problem/E

    E. Strictly Positive Matrix time limit per test 1 second memory limit per test 256 megabytes input s ...

  9. codeforces.com/contest/251/problem/C

    C. Number Transformation time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

随机推荐

  1. macaca测试web小例子

    上午刚把macaca的环境在公司的电脑上吧web 端的环境给搭建好,于是乎,看看网上的例子,看看官方的文档 https://macacajs.github.io/wd.py/ 可以在这个链接看到原滋原 ...

  2. [js高手之路]javascript腾讯面试题学习封装一个简易的异步队列

    这道js的面试题,是这样的,页面上有一个按钮,一个ul,点击按钮的时候,每隔1秒钟向ul的后面追加一个li, 一共追加10个,li的内容从0开始技术( 0, 1, 2, ....9 ),首先我们用闭包 ...

  3. 编译make的出错提示解决方案

    编译出错笔记:start.s:20: Error: no such instruction: `ldr r0,=WTCON' 错误:没有这样的指令 解决:编译文件后缀名必须为大写S,改为start.S ...

  4. wait与sellp方法区别

    Java Thread(线程)案例详解sleep和wait的区别    上次对Java Thread有了总体的概述与总结,当然大多都是理论上的,这次我将详解Thread中两个常用且容易疑惑的方法.并通 ...

  5. 交换机端口呈现err-disable的原因

    导致交换机端口呈现err-disable状态的原因有很多,为方便大家查询,特归纳如下:   1. duplex mismatch (A is correct) 2. port-channel misc ...

  6. python re group()

    python group() 正则表达式中,group()用来提出分组截获的字符串,()用来分组 import re a = "123abc456" print re.search ...

  7. MPLS VPN随堂笔记1

    MPLS VPN 基础 1.MPLS vpn架构的特点 1.1.允许不同CE传递相同私网路由 1.2.SP内部(所有P路由器)不需要学习CE路由 1.3.无安全保障但有带宽保障(跟SP租用服务) 2. ...

  8. 201521123016 《Java程序设计》第7周学习总结

    1. 本周学习总结 2. 书面作业 1.ArrayList代码分析 1.1 解释ArrayList的contains源代码 public boolean contains(Object o) { re ...

  9. 201521123093 java 第五周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 1.2 可选:使用常规方法总结其他上课内容. 答:接口:1.所有的默认方法都是public abstract; 2.属性都是p ...

  10. 201521123076《java程序设计》第四次总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. instanceof可以测试一个对象是否是某个类(或其父类),右边'is a?'左边关系. ...