Skip to content

Commit 2d561a7

Browse files
committed
srm 559 div 1 250
1 parent 2916e54 commit 2d561a7

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

HyperKnight.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <queue>
8+
#include <cstring>
9+
#include <climits>
10+
#include <cstdio>
11+
#include <numeric>
12+
using namespace std;
13+
typedef pair<int,int> pi;
14+
typedef set<int> si;
15+
typedef vector<int> vi;
16+
typedef vector<vi> vvi;
17+
typedef vector<string> vs;
18+
typedef long long ll;
19+
#define sz size()
20+
#define mp make_pair
21+
#define pb push_back
22+
#define inf 1000000000
23+
#define ri(a, b) for(int i=((int)(a)); i < ((int)(b)); i++) // i -> [a, b)
24+
#define rie(a, b) for(int i=((int)(a)); i <= ((int)(b)); i++) // i -> [a, b]
25+
#define rj(a, b) for(int j=((int)(a)); j < ((int)(b)); j++) // j -> [a, b)
26+
#define rje(a, b) for(int j=((int)(a)); j <= ((int)(b)); j++) // j -> [a, b]
27+
#define rk(a, b) for(int k=((int)(a)); k < ((int)(b)); k++) // k -> [a, b)
28+
#define rke(a, b) for(int k=((int)(a)); k <= ((int)(b)); k++) // k -> [a, b]
29+
#define fi(b) for(int i=0; i < ((int)(b)); i++) // i -> [0, b)
30+
#define fie(b) for(int i=0; i <= ((int)(b)); i++) // i -> [0, b]
31+
#define fj(b) for(int j=0; j < ((int)(b)); j++) // j -> [0, b)
32+
#define fje(b) for(int j=0; j <= ((int)(b)); j++) // j -> [0, b]
33+
#define fk(b) for(int k=0; k < ((int)(b)); k++) // k -> [0, b)
34+
#define fke(b) for(int k=0; k < ((int)(b)); k++) // k -> [0, b]
35+
#define fle(b) for(int l=0; l <= ((int)(b)); l++) // l -> [0, b]
36+
#define vaild(x, y) ( 0 <= (x) && (x) < n && 0 <= (y) && (y) < m )
37+
ll n, m;
38+
39+
class HyperKnight {
40+
public:
41+
ll countCells(int aa, int bb, int numRows, int numColumns, int k) {
42+
ll a = aa, b = bb;
43+
n = numRows; m = numColumns;
44+
if(a < b) swap(a, b);
45+
ll ret = 0;
46+
switch(k){
47+
case 2 :
48+
ret = 4*b*b;
49+
break;
50+
case 3 :
51+
ret = 8*b*(a-b);
52+
break;
53+
case 4 :
54+
ret = 2*b*(m+n-4*a) + 4*(a-b)*(a-b);
55+
break;
56+
case 6 :
57+
ret = 2*(a-b)*(m+n-4*a);
58+
break;
59+
case 8 :
60+
ret = (m-2*a)*(n-2*a);
61+
break;
62+
}
63+
return ret;
64+
}
65+
66+
// BEGIN CUT HERE
67+
public:
68+
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); }
69+
private:
70+
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
71+
void verify_case(int Case, const long long &Expected, const long long &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
72+
void test_case_0() { int Arg0 = 2; int Arg1 = 1; int Arg2 = 8; int Arg3 = 8; int Arg4 = 4; long long Arg5 = 20LL; verify_case(0, Arg5, countCells(Arg0, Arg1, Arg2, Arg3, Arg4)); }
73+
void test_case_1() { int Arg0 = 3; int Arg1 = 2; int Arg2 = 8; int Arg3 = 8; int Arg4 = 2; long long Arg5 = 16LL; verify_case(1, Arg5, countCells(Arg0, Arg1, Arg2, Arg3, Arg4)); }
74+
void test_case_2() { int Arg0 = 1; int Arg1 = 3; int Arg2 = 7; int Arg3 = 11; int Arg4 = 0; long long Arg5 = 0LL; verify_case(2, Arg5, countCells(Arg0, Arg1, Arg2, Arg3, Arg4)); }
75+
void test_case_3() { int Arg0 = 3; int Arg1 = 2; int Arg2 = 10; int Arg3 = 20; int Arg4 = 8; long long Arg5 = 56LL; verify_case(3, Arg5, countCells(Arg0, Arg1, Arg2, Arg3, Arg4)); }
76+
void test_case_4() { int Arg0 = 1; int Arg1 = 4; int Arg2 = 100; int Arg3 = 10; int Arg4 = 6; long long Arg5 = 564LL; verify_case(4, Arg5, countCells(Arg0, Arg1, Arg2, Arg3, Arg4)); }
77+
void test_case_5() { int Arg0 = 2; int Arg1 = 3; int Arg2 = 1000000000; int Arg3 = 1000000000; int Arg4 = 8; long long Arg5 = 999999988000000036LL; verify_case(5, Arg5, countCells(Arg0, Arg1, Arg2, Arg3, Arg4)); }
78+
79+
// END CUT HERE
80+
81+
};
82+
83+
// BEGIN CUT HERE
84+
int main()
85+
{
86+
HyperKnight ___test;
87+
___test.run_test(-1);
88+
}
89+
// END CUT HERE
90+

HyperKnight.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td>Fernando loves to play chess. One day he decided to play chess on an unusually large rectangular board. To compensate for the board's size he also decided to change the distance a knight can move in a single jump.
2+
<br></br><br></br>
3+
To describe the moves easily, we will now introduce a coordinate system. Each cell of the chessboard can be described using two integers (r,c): its row number and its column number. Now, if we have a piece at (r,c), the move (x,y) takes the piece to the cell (r+x,c+y).
4+
<br></br><br></br>
5+
The new chess piece will be called an (<b>a</b>,<b>b</b>)-hyperknight. The hyperknight always has 8 possible moves: (+<b>a</b>,+<b>b</b>), (+<b>a</b>,-<b>b</b>), (-<b>a</b>,+<b>b</b>), (-<b>a</b>,-<b>b</b>), (+<b>b</b>,+<b>a</b>), (+<b>b</b>,-<b>a</b>), (-<b>b</b>,+<b>a</b>), and (-<b>b</b>,-<b>a</b>). Note that the original chess knight is a (2,1)-hyperknight.
6+
<br></br><br></br>
7+
Of course, as the chessboard is finite, it is not always possible to make each of the 8 moves. Some of them may cause our hyperknight to leave the chessboard. A move is called <i>valid</i> if the destination cell is on the chessboard. Fernando would like to know the number of cells on his board such that his hyperknight will have exactly <b>k</b> valid moves from that cell.
8+
<br></br><br></br>
9+
You are given the ints <b>a</b>, <b>b</b>, <b>numRows</b>, <b>numColumns</b> and <b>k</b>. The values <b>numRows</b> and <b>numColumns</b> define the number of rows and number of columns on the chessboard, respectively. The other three values were already explained above. Compute and return the number of cells on the chessboard that have exactly <b>k</b> valid (<b>a</b>,<b>b</b>)-hyperknight moves.</td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Class:</td><td>HyperKnight</td></tr><tr><td>Method:</td><td>countCells</td></tr><tr><td>Parameters:</td><td>int, int, int, int, int</td></tr><tr><td>Returns:</td><td>long long</td></tr><tr><td>Method signature:</td><td>long long countCells(int a, int b, int numRows, int numColumns, int k)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td colspan="2"><h3>Limits</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Time limit (s):</td><td>2.000</td></tr><tr><td>Memory limit (MB):</td><td>64</td></tr></table></td></tr><tr><td colspan="2"><h3>Notes</h3></td></tr><tr><td align="center" valign="top">-</td><td>If you wish, you may assume that the rows are numbered 0 through <b>numRows</b>-1 and columns 0 through <b>numColumns</b>-1. However, note that the actual row/column numbers do not matter, as long as they are consecutive.</td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>a</b> will be between 1 and 1,000,000,000 (10^9), inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>b</b> will be between 1 and 1,000,000,000 (10^9), inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>a</b> will not be equal to <b>b</b>.</td></tr><tr><td align="center" valign="top">-</td><td><b>numRows</b> will be between 1 and 1,000,000,000 (10^9), inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>numColumns</b> will be between 1 and 1,000,000,000 (10^9), inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>2*max(<b>a</b>,<b>b</b>) will be strictly less than min(<b>numRows</b>,<b>numColumns</b>).</td></tr><tr><td align="center" valign="top">-</td><td><b>k</b> will be between 0 and 8, inclusive.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>2</pre></td></tr><tr><td><pre>1</pre></td></tr><tr><td><pre>8</pre></td></tr><tr><td><pre>8</pre></td></tr><tr><td><pre>4</pre></td></tr></table></td></tr><tr><td><pre>Returns: 20</pre></td></tr><tr><td><table><tr><td colspan="2">This is a standard chessboard. We have a traditional chess knight and we are looking for cells such that the knight has exactly 4 valid moves.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>3</pre></td></tr><tr><td><pre>2</pre></td></tr><tr><td><pre>8</pre></td></tr><tr><td><pre>8</pre></td></tr><tr><td><pre>2</pre></td></tr></table></td></tr><tr><td><pre>Returns: 16</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>1</pre></td></tr><tr><td><pre>3</pre></td></tr><tr><td><pre>7</pre></td></tr><tr><td><pre>11</pre></td></tr><tr><td><pre>0</pre></td></tr></table></td></tr><tr><td><pre>Returns: 0</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>3</pre></td></tr><tr><td><pre>2</pre></td></tr><tr><td><pre>10</pre></td></tr><tr><td><pre>20</pre></td></tr><tr><td><pre>8</pre></td></tr></table></td></tr><tr><td><pre>Returns: 56</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>1</pre></td></tr><tr><td><pre>4</pre></td></tr><tr><td><pre>100</pre></td></tr><tr><td><pre>10</pre></td></tr><tr><td><pre>6</pre></td></tr></table></td></tr><tr><td><pre>Returns: 564</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">5)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>2</pre></td></tr><tr><td><pre>3</pre></td></tr><tr><td><pre>1000000000</pre></td></tr><tr><td><pre>1000000000</pre></td></tr><tr><td><pre>8</pre></td></tr></table></td></tr><tr><td><pre>Returns: 999999988000000036</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>

0 commit comments

Comments
 (0)