【Codeforces 1107D】Compression
【链接】 我是链接,点我呀:)
【题意】
题意
【题解】
先把所给的压缩形式的字符串转成二进制
然后对获得的01数组做一个前缀和(a[i][j]=以(i,j)为右下角,(1,1)为左上角的矩形内的数字的和)
这样就能O(1)复杂度获得一个长度为x的正方形的区间和了。
这样。我们直接暴力从1..n枚举n的因子x
显然每个因子x要进行(n/x)^2次判断。
有个性质
∑(n/x)^2=n^2*∑(1/(x^2))=n^2*(π^2/6)
所以实际上时间复杂度约等于O(n^2)
【代码】
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 5200;
int n;
int a[N+10][N+10];
char s[N+10][N+10];
int main(){
/*
4
3
3
3
3
*/
scanf("%d",&n);
for (int i = 1;i <= n;i++){
scanf("%s",s[i]);
for (int j = 0;j < n/4;j++){
int key = s[i][j]-'0';
if (s[i][j]>='A' && s[i][j]<='F'){
key = s[i][j]-'A'+10;
}
for (int k = 0;k < 4;k++){
a[i][j*4+4-k] = key%2;
key/=2;
}
}
}
for (int i = 1;i <= n;i++)
for (int j = 1;j <= n;j++)
a[i][j] = a[i-1][j]+a[i][j-1]-a[i-1][j-1]+a[i][j];
int ans = -1;
for (int x = 1;x <= n;x++)
if (n%x==0){
bool flag = true;
for (int i = x;i <= n;i+=x){
for (int j = x;j <= n;j+=x){
int x0 = i-x+1,y0 = j-x+1;
int x1 = i,y1 = j;
int num = a[x1][y1]-a[x0-1][y1]-a[x1][y0-1]+a[x0-1][y0-1];
if (num!=0 && num!=x*x){
flag = false;
break;
}
}
if (!flag) break;
}
if (flag){
ans = x;
}
}
printf("%d\n",ans);
return 0;
}
【Codeforces 1107D】Compression的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
- 【Codeforces 429D】 Tricky Function
[题目链接] http://codeforces.com/problemset/problem/429/D [算法] 令Si = A1 + A2 + ... + Ai(A的前缀和) 则g(i,j) = ...
- 【Codeforces 670C】 Cinema
[题目链接] http://codeforces.com/contest/670/problem/C [算法] 离散化 [代码] #include<bits/stdc++.h> using ...
- 【codeforces 515D】Drazil and Tiles
[题目链接]:http://codeforces.com/contest/515/problem/D [题意] 给你一个n*m的格子; 然后让你用1*2的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- Educational Codeforces Round 19 C
Description Petya recieved a gift of a string s with length up to 105 characters for his birthday. H ...
- AtCoder Beginner Contest 058 ABCD题
A - ι⊥l Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Three poles st ...
- Android-apk文件反编译
一:工具介绍及下载 1:apktool 作用:资源文件获取,可以提取出图片文件和布局文件进行使用查看 2:dex2jar 作用:将apk反编译成java源码(classes ...
- Coco dfs 或者 状压dp。...
C -- Coco Time Limit:1s Memory Limit:64MByte Submissions:148Solved:85 DESCRIPTION Coco just learned ...
- solrJ的查询->统计【转】
package com.fjsh.SearchJobsFirst; import java.text.SimpleDateFormat; import java.util.Calendar; impo ...
- PHP实现XML传输
sendXML.php <!--发送XML的页面--> <?php $xml_data = '<xml>...</xml>';//发送的xml $url ...
- 牛人cad二次开发网站(.net)
http://through-the-interface.typepad.com/through_the_interface/autocad_net/ http://through-the-inter ...
- 自己动手实现Spring IoC框架
钻研Spring 源码也有一段时间了,对Spring IoC的实现原理理解算是比较透彻了,要实现一款IoC容器,简单的概括无非需要以下几个步骤: 1.定义用来描述bean的配置的Java类,例如我们有 ...
- [Tunny]Git常用命令与入门
[黄映焜/Tunny,20140709] Git 仓库就是那个.git 目录,其中存放的是我们所提交的文档索引内容,Git 可基于文档索引内容对其所管理的文档进行内容追踪,从而实现文档的版本控制..g ...
- Javascript中setTimeout()以及clearTimeout( )的使用
setTimeout setTimeout( ) 是属于 window 的 method, 这是用来设定一个时间,时间到了, 就会执行一个指定的 方法.练习一:等候三秒才执行的 alert( )set ...