题目给了四个轮子,每个轮子上有按顺序排列的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的更多相关文章

  1. 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 ...

  2. ACM: Gym 101047K Training with Phuket's larvae - 思维题

     Gym 101047K Training with Phuket's larvae Time Limit:2000MS     Memory Limit:65536KB     64bit IO F ...

  3. ACM: Gym 101047E Escape from Ayutthaya - BFS

    Gym 101047E Escape from Ayutthaya Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I6 ...

  4. ACM: Gym 101047B Renzo and the palindromic decoration - 手速题

     Gym 101047B  Renzo and the palindromic decoration Time Limit:2000MS     Memory Limit:65536KB     64 ...

  5. Gym 101102J---Divisible Numbers(反推技巧题)

    题目链接 http://codeforces.com/gym/101102/problem/J Description standard input/output You are given an a ...

  6. Gym 100917J---Judgement(01背包+bitset)

    题目链接 http://codeforces.com/gym/100917/problem/J Description standard input/outputStatements The jury ...

  7. Gym 100917J---dir -C(RMQ--ST)

    题目链接 http://codeforces.com/gym/100917/problem/D problem description Famous Berland coder and IT mana ...

  8. Gym 101102D---Rectangles(单调栈)

    题目链接 http://codeforces.com/gym/101102/problem/D problem  description Given an R×C grid with each cel ...

  9. Gym 101102C---Bored Judge(区间最大值)

    题目链接 http://codeforces.com/gym/101102/problem/C problem description Judge Bahosain was bored at ACM ...

随机推荐

  1. js判断微信浏览器

    function is_weixin(){ //检查是否是微信浏览器 var ua = navigator.userAgent.toLowerCase(); if(ua.match(/MicroMes ...

  2. 李洪强iOS经典面试题138-Block

    李洪强iOS经典面试题138-Block   Block Block底层原理实现 首先我们来看四个函数 void test1() { int a = 10; void (^block)() = ^{ ...

  3. Winform Combox DataSource 之不显示 displayemember 内容

    刚开始学习数据绑定的东西, private void Form1_Load(object sender, EventArgs e) { IList<TLayer> tt = new Lis ...

  4. Hack技术

    Hack技术 1.IE条件注释法,微软官方推荐的hack方式. 只在IE下生效 <!--[if IE]> <link rel="stylesheet" href= ...

  5. C# MD5加密的方法+一般处理程序使用Session+后台Json序列化

    1.MD5加密 string md5Str = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(s ...

  6. zju(6)中断控制实验

    1.实验目的 1.学习和掌握Linux下中断驱动的写法: 二.实验内容 1.编写EduKit-IV实验箱Linux操作系统下按键key的驱动: 2.编写EduKit-IV实验箱Linux操作系统下按键 ...

  7. 怎么配置Java环境变量?

    右键计算机 -> 属性 -> 高级系统设置 -> 环境变量,   在系统环境变量添加以下三条变量. 1. PATH, 配置JDK命令文件的位置. 输入“%JAVA_HOME%\bin ...

  8. 关于格式转换 “%a.bs”

    这个形式的格式转换符用于输出, 如果a <= b,  那么输出的字符串串长大于等于a, 小于b; 否则, 输出的串长按照a指定的输出. (不够,用空格补齐)

  9. SVN 远程无法联通

    远程花生壳搭建之后,配置在服务器iis上的其他的网站都能访问,局域网都可以,就是SVN远程连接不通. 没有搞过这样的问题,一下就不知道怎么办了.网上也没有人搞过,然后想到以前公司的一个大神搞过,然后请 ...

  10. String-自定义功能

    <script> /* *发现js中的String对象有限,想要对字符串操作的其他功能. *比如:去除字符串两端的空格.这时只能自.定义 */ //去除字符串两端的空格 function ...