题目给了四个轮子,每个轮子上有按顺序排列的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. DropDownList 控件不能触发SelectedIndexChanged 事件

    相信DropDownList 控件不能触发SelectedIndexChanged 事件已经不是什么新鲜事情了,原因也无外乎以下几种: 1.DropDownList 控件的属性 AutoPostBac ...

  2. 创建 maven 本地仓库

    在 pom.xml 添加依赖包的时候,有时候会提示无法从 http://repo1.maven.org/maven2/ 获取的情况,这时可配置个本地仓库: 从网上下载 maven 仓库网站源码包 Ne ...

  3. Qt 控件随窗口缩放

    在Qt的界面设计中,我们有时候希望窗口在最大化的时候,上面的控件也跟着缩放,那么我们就需要调整控件的SizePolicy属性,关于这个属性的讲解请参见我之前的博客Qt SizePolicy 属性,由于 ...

  4. 如何在maven工程中加载oracle驱动

    maven中引入oracle驱动报错Missing artifact com.oracle:ojdbc14:jar:10.2.0.4.0 时间:2015年09月22日  浏览:9361次 在maven ...

  5. codeforces480E Parking Lot

    题目大意:给一个点阵,其中有的地方没有点,操作是去掉某个点,并询问当前点阵中最大的正方形 若没有修改的话,裸dp 加上修改,可以考虑时光倒流,这样答案就是递增的 可以用并查集维护点的连通性,O^2的 ...

  6. IOS第15天(3,事件处理,手势处理)

    7> 手势识别    使用UIImageView原因:之前既能看见图片,又能监听点击的只有UIButton,学了手势,我们的UIImageView也可以.    * tap(代理:左边不能点,右 ...

  7. poj2305-Basic remains(进制转换 + 大整数取模)

    进制转换 + 大整数取模一,题意: 在b进制下,求p%m,再装换成b进制输出. 其中p为b进制大数1000位以内,m为b进制数9位以内二,思路: 1,以字符串的形式输入p,m; 2,转换:字符串-&g ...

  8. EmguCV 简单图形绘制

    一.圆 public static void cvCircle( IntPtr img, System.Drawing.Point center, //Center of the circle int ...

  9. mysql5.5手册读书日记(2)

    <?php /* * * MySQL_5.5中文参考手册 485开始 * * mysql> SELECT CASE 1 WHEN 1 THEN 'one' -> WHEN 2 THE ...

  10. CSS成长之路----知识点篇

    一,ul--li列表水平排列,切居中对齐 主要是利用Li标签中的display属性.设置为display:inline,使li纵向排布,且能够使用text-align:center;使其居中显示 HT ...