It's opening night at the opera, and your friend is the prima donna (the lead female singer). You will not be in the audience, but you want to make sure she receives a standing ovation -- with every audience member standing up and clapping their hands for her.
Initially, the entire audience is seated. Everyone in the audience has a shyness level. An audience member with shyness level Si will wait until at least Si other audience members have already stood up to clap, and if so, she will immediately stand up and clap. If Si = 0 , then the audience member will always stand up and clap immediately, regardless of what anyone else does. For example, an audience member with Si = 2 will be seated at the beginning, but will stand up to clap later after she sees at least two other people standing and clapping.
You know the shyness level of everyone in the audience, and you are prepared to invite additional friends of the prima donna to be in the audience to ensure that everyone in the crowd stands up and claps in the end. Each of these friends may have any shyness value that you wish, not necessarily the same. What is the minimum number of friends that you need to invite to guarantee a standing ovation?
The first line of the input gives the number of test cases, T. T test cases follow. Each consists of one line with Smax, the maximum shyness level of the shyest person in the audience, followed by a string of Smax + 1 single digits. The kth digit of this string (counting starting from 0) represents how many people in the audience have shyness level k. For example, the string "409" would mean that there were four audience members with Si = 0 and nine audience members with Si = 2 (and none with Si = 1 or any other value). Note that there will initially always be between 0 and 9 people with each shyness level.
The string will never end in a 0. Note that this implies that there will always be at least one person in the audience.
For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the minimum number of friends you must invite.
1 ≤ T ≤ 100.
0 ≤ Smax ≤ 6.
0 ≤ Smax ≤ 1000.
At the Infinite House of Pancakes, there are only finitely many pancakes, but there are infinitely many diners who would be willing to eat them! When the restaurant opens for breakfast, among the infinitely many diners, exactly D have non-empty plates; the ith of these has Pi pancakes on his or her plate. Everyone else has an empty plate.
Normally, every minute, every diner with a non-empty plate will eat one pancake from his or her plate. However, some minutes may be special. In a special minute, the head server asks for the diners' attention, chooses a diner with a non-empty plate, and carefully lifts some number of pancakes off of that diner's plate and moves those pancakes onto one other diner's (empty or non-empty) plate. No diners eat during a special minute, because it would be rude.
You are the head server on duty this morning, and it is your job to decide which minutes, if any, will be special, and which pancakes will move where. That is, every minute, you can decide to either do nothing and let the diners eat, or declare a special minute and interrupt the diners to make a single movement of one or more pancakes, as described above.
Breakfast ends when there are no more pancakes left to eat. How quickly can you make that happen?
The first line of the input gives the number of test cases, T. T test cases follow. Each consists of one line with D, the number of diners with non-empty plates, followed by another line with D space-separated integers representing the numbers of pancakes on those diners' plates.
For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is the smallest number of minutes needed to finish the breakfast.
1 ≤ T ≤ 100.
1 ≤ D ≤ 6.
1 ≤ Pi ≤ 9.
1 ≤ D ≤ 1000.
1 ≤ Pi ≤ 1000.
The Dutch computer scientist Edsger Dijkstra made many important contributions to the field, including the shortest path finding algorithm that bears his name. This problem is not about that algorithm.
You were marked down one point on an algorithms exam for misspelling "Dijkstra" -- between D
and stra
, you wrote some number of characters, each of which was either i
, j
, or k
. You are prepared to argue to get your point back using quaternions, an actual number system (extended from complex numbers) with the following multiplicative structure:
To multiply one quaternion by another, look at the row for the first quaternion and the column for the second quaternion. For example, to multiply i by j, look in the row for i and the column for j to find that the answer is k. To multiply j by i, look in the row for j and the column for i to find that the answer is -k.
As you can see from the above examples, the quaternions are not commutative -- that is, there are some a and b for which a * b != b * a. However they are associative -- for any a,b, and c, it's true that a * (b * c) = (a * b) * c.
Negative signs before quaternions work as they normally do -- for any quaternions a andb, it's true that -a * -b = a * b, and -a * b = a * -b = -(a * b).
You want to argue that your misspelling was equivalent to the correct spelling ijk
by showing that you can split your string of i
s, j
s, and k
s in two places, forming three substrings, such that the leftmost substring reduces (under quaternion multiplication) to i, the middle substring reduces to j, and the right substring reduces to k. (For example, jij
would be interpreted as j * i * j; j * i is -k, and -k * j is i, so jij
reduces to i.) If this is possible, you will get your point back. Can you find a way to do it?
The first line of the input gives the number of test cases, T. T test cases follow. Each consists of one line with two space-separated integers L and X, followed by another line with L characters, all of which are i
, j
, or k
. Note that the string never contains negative signs, 1
s, or any other characters. The string that you are to evaluate is the given string ofL characters repeated X times. For instance, for L = 4, X = 3, and the given string kiij
, your input string would be kiijkiijkiij
.
For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is either YES
or NO
, depending on whether the string can be broken into three parts that reduce to i, j, and k, in that order, as described above.
1 ≤ T ≤ 100.
1 ≤ L ≤ 10000.
1 ≤ X ≤ 10000.
1 ≤ L * X ≤ 10000.
1 ≤ X ≤ 1012.
1 ≤ L * X ≤ 1016.
i
,
j
, and
k
.
k
,
j
,
i
, and this does not satisfy the conditions.
jijijijijiji
. It can be split into
jij
(which reduces to
i
),
iji
(which reduces to
j
), and
jijiji
(which reduces to
k
).
#include <cstdio> #include <iostream> #include <string> #include <iterator> #include <algorithm> #include <vector> #include <cstring> #include <array> #include <queue> #include <set> #include <map> using namespace std; const int MAXN = 10005; typedef long long LL; int ad[300]; int mmp[9][10] = {{}, {0,1,2,3,4},{0,2,-1,4,-3},{0,3,-4,-1,2},{0,4,3,-2,-1} }; struct node { int n,f; node operator * (const node &a) { node res = node {mmp[n][a.n],f*a.f}; if (res.n < 0) res.f = -res.f, res.n = -res.n; return res; } bool operator == (const node &a) const { return n == a.n && f == a.f; } }; node mpow(node a, LL n) { node p = a, res {1,1}; while (n) { if (n & 1LL) res = res*p; p = p*p; n >>= 1; } return res; } node pow(node a, int n) { node res {1,1}; if (n <= 0) return res; n %= 4; for (LL i = 0; i< n; ++i) res = res * a; return res; } node num[MAXN], lbg[MAXN], rbg[MAXN]; char s[MAXN]; int solve() { int n; LL m; scanf("%d%lld", &n, &m); scanf("%s", s+1); node seg {1,1}; rbg[0] = {1,1}; for (int i = 1; i<= n; ++i) { num[i] = node {ad[s[i]], 1}; seg = seg * num[i]; rbg[i] = rbg[i-1]*num[i]; } lbg[n+1] = {1,1}; for (int i = n; i>0; --i) { lbg[i] = num[i]*lbg[i+1]; } node sum {1,1}; LL df = (m*n - 4LL*n)/(4LL*n); int mn = m*n-4LL*n*df; for (int i = 1, ii; i<= mn; ++i) { ii = (i-1)%n + 1; sum = sum * num[ii]; if (sum == node {2,1}) { node bsum {1,1}; for (int j = i+1, jj; j<= mn; ++j) { jj = (j-1)%n + 1; bsum = bsum*num[jj]; if (bsum == node {3,1}) { if (j == mn) continue; int kk = (j)%n + 1; node csum = lbg[kk]*pow(seg, (mn-j-1)/n); if (csum == node{4,1}) return 1; } } } } return 0; } int main() { freopen("C-large.in", "r",stdin); //freopen("in.txt", "r",stdin); freopen("out.txt", "w", stdout); int t; scanf("%d", &t); ad['1']=1; ad['i']=2; ad['j']=3; ad['k']=4; for (int _ = 1; _<= t; ++_) { printf("Case #%d: ", _); if (solve()) puts("YES"); else puts("NO"); } return 0; }
An N-omino is a two-dimensional shape formed by joining N unit cells fully along their edges in some way. More formally, a 1-omino is a 1x1 unit square, and an N-omino is an (N-1)omino with one or more of its edges joined to an adjacent 1x1 unit square. For the purpose of this problem, we consider two N-ominoes to be the same if one can be transformed into the other via reflection and/or rotation. For example, these are the five possible 4-ominoes:
And here are some of the 108 possible 7-ominoes:
Richard and Gabriel are going to play a game with the following rules, for some predetermined values of X, R, and C:
1. Richard will choose any one of the possible X-ominoes.
2. Gabriel must use at least one copy of that X-omino, along with arbitrarily many copies of any X-ominoes (which can include the one Richard chose), to completely fill in an R-by- C grid, with no overlaps and no spillover. That is, every cell must be covered by exactly one of the X cells making up an X-omino, and no X-omino can extend outside the grid. Gabriel is allowed to rotate or reflect as many of the X-ominoes as he wants, including the one Richard chose. If Gabriel can completely fill in the grid, he wins; otherwise, Richard wins.
Given particular values X, R, and C, can Richard choose an X-omino that will ensure that he wins, or is Gabriel guaranteed to win no matter what Richard chooses?
The first line of the input gives the number of test cases, T. T lines follow. Each contains three space-separated integers: X, R, and C.
For each test case, output one line containing "Case #x: y", where x is the test case number (starting from 1) and y is either RICHARD (if there is at least one choice that ensures victory for Richard) or GABRIEL (if Gabriel will win no matter what Richard chooses).
T = 64.
1 ≤ X, R, C ≤ 4.
1 ≤ T ≤ 100.
1 ≤ X, R, C ≤ 20.
#include <cstdio> #include <iostream> #include <string> #include <iterator> #include <algorithm> #include <vector> #include <cstring> #include <array> #include <queue> #include <set> #include <map> using namespace std; const int MAXN = 50; int vis[MAXN][MAXN]; int r, c, N; inline int isoverborder(int x, int y, int xx = 0) { return x<xx || x >= r || y < 0 || y >= c; } const int dir[4][2] = {0,1,0,-1,-1,0,1,0}; int bx; int da[MAXN][2], nd; int mvis[MAXN][MAXN]; int bfs(int x, int y) { static int qq[500][2]; int head = 0, tail = 0; qq[tail][0] = x; qq[tail][1] = y; ++tail; int cnt = 0; while (head < tail) { int xx = qq[head][0]; int yy = qq[head][1]; ++head; if (mvis[xx][yy]) continue; mvis[xx][yy] = 1; ++cnt; for (int i = 0; i< 4; ++i) { int a = xx+dir[i][0], b = yy+dir[i][1]; if (isoverborder(a,b) || mvis[a][b]) continue; qq[tail][0] = a; qq[tail++][1] = b; } } return cnt; } int msearch() { int cnt = 0; for (int i = 0; i< r; ++i) { for (int j = 0; j< c; ++j) { if (mvis[i][j]) continue; if (bfs(i, j) % N != 0) return 0; } } return 1; } void zhuan(int nd) { for (int i = 0, j; i< nd; ++i) { j = da[i][0]; da[i][0] = -da[i][1]; da[i][1] = j; } } int caonima[MAXN][2]; int dfs1(int nd) { if (nd == N) { memcpy(caonima, da, sizeof da); for (int dg = 0; dg < 4; ++dg) { zhuan(nd); int mxx=da[0][0], mnx=da[0][0], mxy=da[0][1], mny=da[0][1]; for (int i = 1; i< N; ++i) { mxx = max(mxx, da[i][0]); mnx = min(mnx, da[i][0]); mxy = max(mxy, da[i][1]); mny = min(mny, da[i][1]); } for (int i = 0; i< r; ++i) { for (int j = 0; j< c; ++j) { // 找起点 if ((mxx + i) < r &&(mnx + i) >= 0 && (mxy+j) < c && (mny+j)>=0) { memset(mvis, 0, sizeof mvis); for (int u = 0; u< N; ++u) { mvis[i+da[u][0]][j+da[u][1]] = 1; } // 剩余的联通区域内未覆盖的数量都是N的倍数 if (msearch()) { memcpy(da, caonima, sizeof da); return 0; } } } } } return 1; } for (int i = 0; i< nd; ++i) { int x = da[i][0], y = da[i][1]; for (int j = 0; j< 4; ++j) { int xx = x+dir[j][0], yy = y+dir[j][1]; if (xx>=0 && !vis[xx+20][yy+20]) { vis[xx+20][20+yy] = 1; da[nd][0] = xx; da[nd][1] = yy; if (dfs1(nd+1)) return 1; vis[xx+20][20+yy] = 0; } } } return 0; } int solve() { scanf("%d%d%d", &N, &r, &c); //printf("(%d,%d,%d) ", N, r, c); if (r*c % N!=0) return 1; if (N == 1) return 0; if (N >= 7) return 1; da[0][0] = da[0][1] = 0; memset(vis, 0, sizeof vis); vis[20][20] = 1; return dfs1(1); } int main() { freopen("D-large.in", "r",stdin); //freopen("in.txt", "r",stdin); freopen("out.txt", "w", stdout); int t; scanf("%d", &t); for (int _ = 1; _<= t; ++_) { printf("Case #%d: ", _); if (solve()) puts("RICHARD"); else puts("GABRIEL"); } return 0; }