Codeforece Gym 100500BConference Room

来源:互联网 发布:手机可以做淘宝客服吗 编辑:程序博客网 时间:2024/06/03 20:34

Gym 100500B Conference Room

Program: room.(c|cpp|java)
Input: room.in
One of the most committed, loyal, and hard-working volunteers in the group ACPC organizers is Loubna
Benguit. She showed real dedication and full commitment to the noble cause of the ICPC competition from
her day 1. Loubna is from Morocco. She has just graduated from ´Ecole nationale sup´erieure d’informatique
et d’analyse des syst`emes, last July. Last year was the final year of Loubna’s undergraduate journey, she
was supposed to deliver her final graduation project presentation by mid June, but as you know Loubna
was part of the ACPC team who traveled to Russia to attend the World Finals event.
Loubna was super lucky to have Dr Sidi Ali with her in Russia who was the one directly responsible for
grading her graduation project presentation, therefor she decided to present her graduation project there.
Coach Fegla reserved a room in Onegin Hotel in Yekaterinburg, Russia for the purpose of the presentation.
The next day when Coach Fegla and Loubna went to the room to prepare it for the presentation they
found out that it was locked. The lock was designed by Dr Sidi Ali as a test for Loubna, and it can only
be unlocked by a true programmer. The lock consisted of 4 sliders that can go either up or down like
stated in the image below. Each slider contains the same number of integers. You have to slide the four
sliders up or down to produce an equal sum for all the rows, like in the second image.
In the image above, you have to rotate the 2nd slider 4 times upward, the 3rd slider 1 time upward, and
the 4th slider 1 time downward. This way you can make the sum of each row equal to 12. Note that you
have made a total of 6 rotations.
Can you help Loubna and Fegla, to determine if it is possible to open the lock?

Input

The first line will be the number of test cases T. Each test case will start with an integer N , which
represents the number of integers on each of the sliders. Then it will be followed by 4 lines each line
containing N integers. The ith line 1 ≤ i ≤ 4 will have the N integers describing the ith slider.
1 ≤ T ≤ 100
1 ≤ N ≤ 1000
1 ≤ a single value within a slider ≤ 4, 000, 000

Output

For each test case print a single line containing:
Case_x:_y
x is the case number starting from 1.
y is "Yes"if the the lock can be opened, otherwise "No without the quotes.
Replace all underscores with spaces.

Examples
room.in 

3
8
5 4 3 1 4 3 2 3
3 1 2 3 1 5 2 4
3 1 2 2 3 3 5 4
1 5 4 2 3 4 3 5
8
8 9 1 9 1 8 4 3
9 5 2 5 9 3 7 3
4 3 2 8 8 1 7 9
1 2 6 1 5 3 6 5
8
1 7 2 2 3 1 1 19
2 22 23 21 6 7 4 7
21 5 3 18 4 4 5 20
5 4 4 6 4 5 22 6

Standard Output

Case 1: Yes
Case 2: No
Case 3: Yes


题意:给一个四列的密码转盘,如果每行数字的加和都相等,那么这个密码锁可以打开,否则就打不开。询问能否打开这个密码锁。

解法:固定第一列,然后枚举第二列和第三列,对第四列的所有情况都插入hash表,然后直接进行查找。这题的hash可以采用Rabbin-Karp的思想(安神66666),写出优美的hash之后就可以安心的A掉这道题了。


/* written by jiefangxuanyan */#include <bits/stdc++.h>using namespace std;const int N=1100,M=1000000007,V=4000001;int in[4][N];typedef long long LL;LL hash[4];int main(){    int fuck;    scanf("%d", &fuck);    for (int cas = 1; cas <= fuck; cas++)    {        int n;        memset(hash,0,sizeof(hash));        LL ss=0;        scanf("%d",&n);        for(int i=0;i<4;i++)        {            for(int j=0;j<n;j++)            {                scanf("%d",in[i]+j);                hash[i]=(hash[i]*V+in[i][j])%M;                ss+=in[i][j];            }        }        ss/=n;        LL top=1,sh=0;        for(int i=0;i<n;i++)        {            top=(top*V)%M;            sh=(sh*V+ss)%M;        }        typedef multimap<LL,int> MM;        MM mm;        for(int o3=0;o3<n;o3++)        {            mm.insert(make_pair((sh-hash[3]+M)%M,o3));            hash[3]=(hash[3]*V+in[3][o3]*((1-top+M)%M))%M;        }        for(int o1=0;o1<n;o1++)        {            for(int o2=0;o2<n;o2++)            {                LL h=(hash[0]+hash[1]+hash[2])%M;                for(MM::iterator it=mm.lower_bound(h);it!=mm.upper_bound(h);it++)                {                    int o3=it->second;                    for(int i=0;i<n;i++)                    {                        if(in[0][i]+in[1][(i+o1)%n]+in[2][(i+o2)%n]+in[3][(i+o3)%n]!=ss)                        {                            goto lblFail;                        }                    }                    printf("Case %d: Yes\n",cas);                    goto lblSucc;                    lblFail:                        ;                }                hash[2]=(hash[2]*V+in[2][o2]*((1-top+M)%M))%M;            }            hash[1]=(hash[1]*V+in[1][o1]*((1-top+M)%M))%M;        }        printf("Case %d: No\n",cas);        lblSucc:            ;    }}


0 0
原创粉丝点击