【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的长方形去填格子的空缺; 如果有填满的方案且方案 ...
随机推荐
- Python实现两已知排好序的列表合并成一个排好序的列表
#方法0.5--- lst1 = [1, 3, 7, 9, 12] lst2 = [4, 8, 9, 13, 15, 19] def merge(a, b): c = [] h = j = 0 whi ...
- Educational Codeforces Round 18 A
Description There are n cities situated along the main road of Berland. Cities are represented by th ...
- Dima and Magic Guitar CodeForces - 366E
Dima and Magic Guitar CodeForces - 366E 题意: http://blog.csdn.net/u011026968/article/details/38716425 ...
- DP HDOJ 5492 Find a path
题目传送门 题意:从(1, 1)走到(n, m),每次往右或往下走,问(N+M−1)∑(Ai−Aavg)2 的最小值 分析:展开式子得到(N+M−1)∑(Ai2) - (∑(Ai))2的最小值.用普通 ...
- Linux下文件权限的设置
文件/目录权限设置命令:chmod 这是Linux系统管理员最常用到的命令之一,它用于改变文件或目录的访问权限.该命令有两种用法: 用包含字母和操作符表达式的文字设定法 ) 其语法格式为:chmod ...
- 479 Largest Palindrome Product 最大回文数乘积
你需要找到由两个 n 位数的乘积组成的最大回文数.由于结果会很大,你只需返回最大回文数 mod 1337得到的结果.示例:输入: 2输出: 987解释: 99 x 91 = 9009, 9009 % ...
- Windows下降权MYSQL和apche的运行级别(普通用户权限运行)
1.MYSQL的降权运行 新建立一个用户比如mysql net user mysql microsoft /add net localgroup users mysql /del 不属于任何组 ...
- 【学习笔记】深入理解js原型和闭包(14)——从【自由变量】到【作用域链】
先解释一下什么是“自由变量”. 在A作用域中使用的变量x,却没有在A作用域中声明(即在其他作用域中声明的),对于A作用域来说,x就是一个自由变量.如下图 如上程序中,在调用fn()函数时,函数体中第6 ...
- NSString 与NSMutableString的区别
NSString 与NSMutableString的区别 Suppose You have a code like this NSString *s = [[NSString alloc] ...
- java实现单向链表的增、删、改、查
单向链表 作者:vashon package com.ywx.link; /** * 单向链表 * @author vashon * */ public class LinkTest { public ...