Gym 100500B
题目给了四个轮子,每个轮子上有按顺序排列的n个数,要求适当旋转每个轮子,使得四个轮子相同行数相加和相同。
首先,可以计算出每一行的和应该是多少,记为Sum。然后固定第一个轮子,二重循环枚举2、3轮子,然后O(n)判断1+2+3是否等于Sum-4,这样时间复杂度是O(n^3)。
那么,只要把判断过程复杂度尽量降低就行了。
假设每个轮子最大的数是W,那么我们可以把每个轮子看成一个W+1进制数,然后我们把这个数Hash出来,我们轮子每Shift一位,就相当于Hash把最高位减掉并在最低位加上它。然后,计算Hash(1)+HASH(2)+Hash(3)是否等于Hash(Sum...Sum), 其中Sum...Sum代表n个Sum的W+1进制数。这样做到了O(1)的判断。但是Hash可能会有重复,那么,我们用一个multimap来保存每一个Hash值对应的若干个可能的4轮子的位置,然后暴力判断这种状态是否真的可行就行了。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <map>
#define maxn 2200
#define w 4000001
#define p 1000000007 using namespace std;
multimap<long long,int> v;
multimap<long long,int>::iterator it; int a[][maxn];
long long hash[];
long long sum,mx,ss;
int n; bool judge(int i,int j,int k)
{
for (int o=;o<n;o++)
if (a[][o]+a[][(i+o)%n]+a[][(j+o)%n]+a[][(k+o)%n]!=sum) return ;
return ;
} bool solve()
{
for (int i=;i<n;i++)
{
for (int j=;j<n;j++)
{
long long h=(hash[]+hash[]+hash[])%p;
for (it=v.lower_bound(h);it!=v.upper_bound(h);it++)
{
int k=it->second;
if (judge(i,j,k)) return ;
}
hash[]=(hash[]*w%p+a[][j]*(-mx+p)%p)%p;
}
hash[]=(hash[]*w%p+a[][i]*(-mx+p)%p)%p;
}
return ;
} int main()
{
int Case;
scanf("%d",&Case);
for (int t=;t<=Case;t++)
{
sum=;
scanf("%d",&n);
memset(hash,,sizeof(hash));
for (int i=;i<=;i++)
for (int j=;j<n;j++)
{
scanf("%d",&a[i][j]);
hash[i]=(hash[i]*w%p+a[i][j])%p;
sum+=a[i][j];
}
sum=sum/n;
mx=,ss=;
for (int i=;i<n;i++) mx=(mx*w)%p,ss=(ss*w%p+sum)%p;
v.clear();
for (int i=;i<n;i++)
{
v.insert(make_pair((ss-hash[]+p)%p,i));
hash[]=(hash[]*w%p+a[][i]*(-mx+p)%p)%p;
}
if (solve()) printf("Case %d: Yes\n",t);
else printf("Case %d: No\n",t);
}
return ;
}
Gym 100500B的更多相关文章
- ACM: Gym 101047M Removing coins in Kem Kadrãn - 暴力
Gym 101047M Removing coins in Kem Kadrãn Time Limit:2000MS Memory Limit:65536KB 64bit IO Fo ...
- ACM: Gym 101047K Training with Phuket's larvae - 思维题
Gym 101047K Training with Phuket's larvae Time Limit:2000MS Memory Limit:65536KB 64bit IO F ...
- ACM: Gym 101047E Escape from Ayutthaya - BFS
Gym 101047E Escape from Ayutthaya Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I6 ...
- ACM: Gym 101047B Renzo and the palindromic decoration - 手速题
Gym 101047B Renzo and the palindromic decoration Time Limit:2000MS Memory Limit:65536KB 64 ...
- Gym 101102J---Divisible Numbers(反推技巧题)
题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...
- Gym 100917J---Judgement(01背包+bitset)
题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...
- Gym 100917J---dir -C(RMQ--ST)
题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...
- Gym 101102D---Rectangles(单调栈)
题目链接 http://codeforces.com/gym/101102/problem/D problem description Given an R×C grid with each cel ...
- Gym 101102C---Bored Judge(区间最大值)
题目链接 http://codeforces.com/gym/101102/problem/C problem description Judge Bahosain was bored at ACM ...
随机推荐
- inconfont 字体库应用
先去注册个号码,好像只可以用新浪微博登录哈,搞一个微博去. 第一就是点上面图标库,选择官方和所有都行. 恩接着点一个图标,他就自己跑到 第二个按钮哪里去了,在点第二个按钮,会出来一个创建项目,随便创建 ...
- [LintCode] Letter Combinations of a Phone Number 电话号码的字母组合
Given a digit string, return all possible letter combinations that the number could represent. A map ...
- Linux中下载、解压、安装文件
一.将解压包发送到linux服务器上: 1.在windos上下载好压缩包文件后,通过winscp等SFTP客户端传送给linux 2.在linux中通过wget命令直接下载 #wget [选项] [下 ...
- Daily Scrum 10.29
今天大家的工作做的还算不错,但是晚些时候遗憾的得知我们的吴文会同学生病住院了,所以她明天的任务暂时保留,再做调整.希望大家在努力学习工作的同时一定要注意身体啊! 下面是今天的Task统计:
- zk 隐藏网页文件后缀
前台(test.zul): <a label="隐藏地址" href="/Bandbox/test.html"/> web.xml添加 <se ...
- jQuery cdn使用介绍
如果您不希望下载并存放jQuery,那么也可以通过 CDN(内容分发网络)引用它,下面是具体实现,有类似需求的各位可以参考下哈,希望对你有所帮助 如果您不希望下载并存放 jQuery,那么也可以通 ...
- mysql远程连接提示无法连接,报1130错误
可能是你的帐号不允许从远程登陆,只能在localhost.这个时候只要在localhost的那台电脑,登入mysql后,更改 “mysql” 数据库里的 “user” 表里的 “host” 项,从”l ...
- 解决微信浏览器无法使用window.location.reload()刷新页面
解决方法: 使用 window.location.href=window.location.href+随机数 代替 window.location.reload(). function r ...
- BizTalk动手实验(十)业务活动监控(BAM)演示
1 课程简介 通过本课程熟悉业务活动监控(BAM)的使用及各组件的配置. (本环境为Windows 2008 32位操作系统环境 + Visual Studio 2010 + BizTalk 210) ...
- Java项目中的classpath
一. 首先 classpath 是指 :项目路径\target\classes目录: 二.解释classes含义: 1.存放各种资源配置文件 2.存放模板文件 3.存放class文件, 对应的是项目开 ...