cf-gym100500-b Conference Room

题意:给你4个1000长度的数组,每个数组可以转动(比如a[1] = a[3],a[2] = a[1] ,a[3] = a[2]) ,问能否有种情况使得任意的i(1-n)a[i]+b[i]+c[i]+d[i] = sum,sum是自己给的一个值。
思路:

  1. 能想通sum是求出来的,及所有的4个数组的累加和被n除。
  2. 那么我们把一个数组转动1000次每次每一位和sum做差压入set里,只要剩下三个数组hash值相加可以等于set里一个数则输出Yes
    另一种解法:https://www.cnblogs.com/nervendnig/p/11637240.html
    代码:
#include <bits/stdc++.h>
using namespace std;
#define ull long long
#define forn(i,n) for(int i = 0;i<n;++i)
#define for1(i,n) for(int i=1;i<=n;++i)
#define IO ios::sync_with_stdio(false);cin.tie(0)
const int maxn = 1e3+5;
const int seed = 1e9+123;
int a[4][maxn];
ull b[2][maxn];

int main(){
    IO;
    int t;cin>>t;
    for1(casee,t){
        int n;cin>>n;
        long long sum = 0;
        forn(i,4){
            forn(j,n){
                cin>>a[i][j];
                sum+=a[i][j];
            }
        }
        bool win = 0;
        if(sum%n==0){
            sum/=n;
            set<ull>s;
            forn(i,n){
                ull h = 0;
                forn(j,n)  h = h*seed+(sum-a[3][(i+j)%n]);
                s.insert(h);
            }
            ull h = 0;
            forn(i,n) h = h*seed+a[0][i];
            forn(i,n) {
                ull hh = 0;
                forn(j,n){
                    hh = hh*seed+a[1][(i+j)%n];
                }
                b[0][i] = hh;
            }
            forn(i,n) {
                ull hh = 0;
                forn(j,n){
                    hh = hh*seed+a[2][(i+j)%n];
                }
                b[1][i] = hh;
            }
            forn(i,n){
                forn(j,n){
                    if(s.find(h+b[0][i]+b[1][j])!=s.end()){
                        win = 1;
                        break;
                    }
                }
                if(win) break;
            }
        }
        cout<<"Case "<<casee<<": ";
        if(win) cout<<"Yes"<<'\n';
        else cout<<"No"<<'\n';
    }   
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值