题目给了四个轮子,每个轮子上有按顺序排列的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. jquery插件之拖拽

    该插件乃本博客作者所写,目的在于提升作者的js能力,也给一些js菜鸟在使用插件时提供一些便利,老鸟就悠然地飞过吧. 此插件旨在实现目前较为流行的拖拽效果,您可以根据自己的实际需求来设置被拖拽元素是否可 ...

  2. 完美扫描PHP特殊一句话后门

    <?php /********************** 作者 Spider 网上公布的各种PHP后门全军覆没 针对一些特殊变形的后门需要自己添加特征 误报率不到百分之一 ********** ...

  3. javascript平时小例子④(setInterval使用2)

    <!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>& ...

  4. 类似input框内最右边添加图标,有清空功能

    <html> <head> <meta http-equiv="Content-Type" content="text/html; char ...

  5. HDU1004之总是wa的细节问题

    #include <stdio.h> #include <string.h> int main() { ][]; int n, i, k, j, max, max_i; ){ ...

  6. BizTalk开发系列(三十六) Orchestration单实例执行

    BizTalk 是高效的消息处理引擎,采用多线程并发的方式来处理消息.也就是说当有消息被接收的时候就会产生一个新的消息处理实例.但有时目标系统可能并没有并发处理 的能力, 这时就需要在BizTalk中 ...

  7. “迷宫”sprint——6.8

    会议时间:2015.6.8 ,12:30——13:00 会议内容:开始第二阶段冲刺,分配任务. 我的任务:完成安卓环境搭建.

  8. IOS第八天(6:UITableViewController新浪微博, 模型和 控件位置封装一起statusFrame)

    *****HMViewController #import "HMViewController.h" #import "HMStatus.h" #import ...

  9. Leetcode | Find Minimum in Rotated Sorted Array I && II

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

  10. Linux内存管理基本概念

    1. 基本概念 1.1 地址 (1)逻辑地址:指由程序产生的与段相关的偏移地址部分.在C语言指针中,读取指针变量本身值(&操作),实际上这个值就是逻辑地址,它是相对于你当前进程数据段的地址.( ...