instruction
stringlengths
0
29.8k
⌀
Universality of NOT and CNOT
Given an adjacency matrix $A_G$ of an undirected graph $G$, it is easy and straightforward to compute the characteristic polynomial $\chi_G(\lambda)$. What about the other way around? The problem can be formulated as follows. > **Problem** Given a polynomial $\chi_G(\lambda)$, decide whether there is a graph $G$ wit...
Let's have two statements 1. **(value > 10)** 2. **(value < 25)** And a list of items with the following values - 10 - 20 - 30 This is what a truth table would give Item Value (value > 10) (value < 25) ---- ----- ------------ ------------ 1 10 FALSE TRUE 2...
Given the phrase "Where NONE of the following are TRUE" and two statements how should a boolean logic be composed?
|logic|
|algorithms|efficiency|memory-management|big-data|
The [Radon transform](https://en.wikipedia.org/wiki/Radon_transform) is used to take 2d projections of an object and create a 3d representation. It seems like it would be possible to apply such a transform in 3d graphics in games (although possibly too slow to be practical). For example, a very simple way to disp...
I was asked a question on how to use a pair of Queues to create a Stack and how to use a pair of Stacks to create a Queue. Any thoughts on how I would do this? Right now I don't even know where to start.
Using Queues for a Stack and Stacks for a Queue
|data-structures|priority-queues|stack|
Database books are good example. However, have a look at the field I/O efficient data structures (and algorithms). To my knowledge, there are some courses about this topic, but very few books. Check this book: U. Meyer, P. Sanders, and J. Sibeyn (eds.), Algorithms for Memory Hierarchies, Lecture Notes in Computer ...
Two stacks to implements a queue -- always fill the stack A in stack B: the last element in stack A is popped (now you got FIFO) -- return elements from stack B to stack A. Same idea is applied to queues.
I have been thinking about why the dynamic programming approach to finding the optimal matrix chain order is better than a brute force approach that finds the optimal order by exploring all nested orders. The more I think about it, the more I feel strongly that the dynamic programming solution considers all possible ch...
Dynamic Programming Solution for Optimal Matrix Chain Multiplication Order
|dynamic-programming|matrices|
To answer the question in the comment above- Two queues to implement a stack(Assume Queue A is filled with elements in LIFO order and Queue B is empty): PUSH: Empty Queue A into Queue B. LIFO order will be persisted in Queue B. Now insert the new element into Queue A. Now empty Queue B into Queue A. POP: Deque...
null
This question assumes the definition of a complete binary tree to be<sup>†</sup>: >A binary tree $T$ with $N$ levels is complete if all levels except possibly the last are completely full, and the last level has all its nodes to the left side. The following is an excerpt from *[Algorithms][1]*: >It ($\log N$) ...
What is the depth of a complete binary tree with $N$ nodes?
|data-structures|binary-trees|
null
Consider how a complete binary tree of height $h$ is constructed, one vertex at the root level, two at the first level below the root, four at the second level below, and so on, until the $h^{th}$ level, which has at least one vertex, but at most twice as many as the previous level. Note that the number of vertices at...
The [Radon transform](https://en.wikipedia.org/wiki/Radon_transform) is used to take 2d projections of an object and create a 3d representation. It seems like it would be possible to apply such a transform in 3d graphics in games (although possibly too slow to be practical). For example, a very simple way to disp...
I'm trying to find CFG's that generate a regular language over the alphabet {a b} I believe I got this one right: All strings that end in b and have an **even** number of b's in total: S -> SS S -> YbYb | empty Y -> aY | empty However, Im not sure how to accomplish this with an odd number of b'...
Context Free Grammars
|automata|finite-automata|context-free|
> **Possible Duplicate:** > [Dynamic programming table for finding similar substrings is too large](http://cs.stackexchange.com/questions/4994/dynamic-programming-table-for-finding-similar-substrings-is-too-large) <!-- End of automatically inserted text --> Say I have a sequence $S$ of 0s and 1s such as $S =...
I'm trying to find CFG's that generate a regular language over the alphabet {a b} I believe I got this one right: All strings that end in b and have an **even** number of b's in total: $\qquad S \to SS \\ \qquad S \to YbYb \mid \varepsilon \\ \qquad Y \to aY \mid \varepsilon$ However, Im not sure how to acco...
null
> **Substring Diff** > Given two strings of length $n$, $P = p_1\dots p_n$ and $Q = q_1 \dots q_n$, we define $M(i, j, L)$ as the number of > mismatches between $p_i \dots p_{i+L-1}$ and $q_j \dots q_{j+L-1}$. In set > notation, $M(i, j, L)$ refers to the size of the set $\{0 \leq x < L \mid p_{i + x} \neq q_{j + ...
Assuming $\Sigma = \{a, b\}$ this probably should do the trick $\qquad S \to Yb\\ \qquad Y \to YXbXb \mid X \mid \varepsilon \\ \qquad X \to Xa \mid \varepsilon$ This should work because Y always has even number of *b*'s and *S* adds one *b* at the end which makes number of *b* odd.
There is an easy and very natural way how to derive a context-free grammar $G=(V,\Sigma,R,S)$ from a DEA $M=(Q,\Sigma,\delta,q_0,F)$. We set * $V=Q$ * $S= q_0$ * $R$ consists of the following rules: * for all $X\in Q$ and $a\in \Sigma$ with $Y=\delta(X,a)$ we add the rule $X \to aY$ * for all $X\in F$ ...
The correct meaning is: <code>"Where (NOT (value > 10)) AND (NOT (value < 25))"</code>. Applying one of the [De Morgan's laws][1]: 1) <code>P AND Q <=> NOT( (NOT P) OR (NOT Q) )</code><br> 2) <code>P OR Q <=> NOT( (NOT P) AND (NOT Q) )</code><br> we get: <code>"Where NOT ( (NOT(NOT(value > 10))) OR (...
Is the complement of a Non-Recognizable language 1. Recognizable **Always** 2. Non-Recognizable **Always** 3. May be Recognizable, May be Non-recognizable. I.e cant comment. A mathematical proof would be of great help since im unable to think of any way to prove this. I did some research on this and found...
If a Language is Non-Recognizable then what about its complement?
|computability|turing-machines|
Is the complement of a Non-Recognizable language 1. Recognizable **Always** 2. Non-Recognizable **Always** 3. May be Recognizable, May be Non-recognizable. I.e cant comment. A mathematical proof would be of great help since im unable to think of any way to prove this. I did some research on this and found...
Is the complement of a Non-Recognizable language 1. Recognizable **Always** 2. Non-Recognizable **Always** 3. May be Recognizable, May be Non-recognizable. I.e cant comment. A mathematical proof would be of great help since im unable to think of any way to prove this. I did some research on this and found...
Is the complement of a Non-Recognizable language 1. Recognizable 2. Non-Recognizable 3. May be Recognizable, May be Non-recognizable. I.e cant comment. A mathematical proof would be of great help since im unable to think of any way to prove this. I did some research on this and found below examples. Im sp...
I want to filter efficiently a list of integers for duplicates in a way that only the resulting set needs to be stored. One way this can be seen: - we have a range of integers $S = \{1, \dots{}, N\}$ with $N$ big (say $2^{40}$) - we have a function $f : S \to S$ with, supposedly, many collisions (the images ...
null
Assuming that you can prove statement 1 and 2 above, you have just presented a proof. You're trying to show that there exists a language which is non-recognizable such that its complement is recognizable. You then prove that $!A(TM)$ has this property. Then you want to show that there exists a non-recognizable language...
null
Instancing in GPU's, from my little knowledge, simply draw the same object at multiple locations. Each "instance" is just a copy of the same model at a different location. Any local effects, such as animation, would effect all instances. The performance improvement comes from gpu being able to deal with virtually all t...
Extreme instancing for computer graphics
|graphics|computer-games|
ERROR: type should be string, got "https://www.interviewstreet.com/challenges/dashboard/#problem/4f1c88e0dec8a\r\n\r\nI wrote a DP solution but with O(N^5).\r\n\r\n<pre><code>\r\n/* Enter your code here. Read input from STDIN. Print output to STDOUT */\r\n\r\n#include&lt;stdio.h&gt;\r\n#include&lt;stdlib.h&gt;\r\n\r\nlong ways(long ***DP, char **Chess, int X, int Y, int S, int N, int M);\r\n\r\nint main()\r\n{\r\n int T;\r\n scanf(\"%d\", &T);\r\n while(T>0)\r\n {\r\n int N, M, S;\r\n scanf(\"%d\", &N);\r\n scanf(\"%d\", &M);\r\n scanf(\"%d\", &S);\r\n long ***DP = (long ***) malloc(sizeof(long **) * N);\r\n int i,j,k;\r\n char **Chess = (char **)malloc(sizeof(char *) *N);\r\n int Xstart;\r\n int Ystart; //printf(\"N=%dM=%dS=%d\\n\", N, M, S);\r\n for(i=0;i<N;i++)\r\n {\r\n Chess[i] = (char *)malloc(sizeof(char) *N);\r\n DP[i] = (long **)malloc(sizeof(long *) * N);\r\n for(j=0;j<N;j++)\r\n { //printf(\"i=%dj=%d\\n\", i, j);\r\n DP[i][j] = (long *)malloc(sizeof(long) *(M+1));\r\n char a;\r\n scanf(\" %c\", &a);\r\n if(a=='L')\r\n {\r\n Xstart = i;\r\n Ystart = j;\r\n }\r\n Chess[i][j] = a;\r\n //printf(\"jf=%d\\n\", j);\r\n }\r\n }\r\n \r\n for(i=0;i<N;i++)\r\n {\r\n for(j=0;j<N;j++)\r\n {\r\n for(k=1;k<M+1;k++)\r\n {\r\n DP[i][j][k] = -1;\r\n }\r\n DP[i][j][0] = 1;\r\n }\r\n }\r\n \r\n printf(\"%ld\\n\", ways(DP, Chess, Xstart, Ystart, S, N, M));\r\n T--;\r\n }\r\n}\r\n\r\nlong ways(long ***DP, char **Chess, int X, int Y, int s, int N, int M)\r\n{\r\n if(DP[X][Y][M] !=-1)\r\n {\r\n //printf(\"X=%d Y=%d M=%d Val=%ld\\n\", X, Y, M, DP[X][Y][M]);\r\n return DP[X][Y][M];\r\n }\r\n else\r\n {\r\n long sum1 = 0;\r\n\r\n \r\n int S,k;\r\n sum1 += ways(DP, Chess, X, Y, s, N, M-1);\r\n\r\n for(S=1;S<=s;S++)\r\n {\r\n for(k=0;k<=S;k++)\r\n {\r\n if(k!=0 && (S-k)!=0)\r\n {\r\n if(X+k<N && Y+(S-k) <N && Chess[X+k][Y+(S-k)] != 'P')\r\n {\r\n Chess[X+k][Y+(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X+k, Y+(S-k), s, N, M-1);\r\n Chess[X+k][Y+(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n if(X+k<N && Y-(S-k)>=0 && Chess[X+k][Y-(S-k)] != 'P')\r\n {\r\n Chess[X+k][Y-(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X+k, Y-(S-k), s, N, M-1);\r\n Chess[X+k][Y-(S-k)] = '.';\r\n Chess[X][Y] = 'L'; \r\n }\r\n if(X-k>=0 && Y+(S-k) <N && Chess[X-k][Y+(S-k)] != 'P')\r\n {\r\n Chess[X-k][Y+(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X-k, Y+(S-k), s, N, M-1);\r\n Chess[X-k][Y+(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n if(X-k>=0 && Y-(S-k) >=0 && Chess[X-k][Y-(S-k)] != 'P')\r\n {\r\n Chess[X-k][Y-(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X-k, Y-(S-k), s, N, M-1);\r\n Chess[X-k][Y-(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n\r\n \r\n }\r\n else if(k==0 && (S-k)!=0)\r\n {\r\n if(Y+(S-k)<N && Chess[X][Y+(S-k)] != 'P')\r\n {\r\n Chess[X][Y+(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X, Y+(S-k), s, N, M-1);\r\n Chess[X][Y+(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n if(Y-(S-k)>=0 && Chess[X][Y-(S-k)] != 'P')\r\n {\r\n Chess[X][Y-(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X, Y-(S-k), s, N, M-1);\r\n Chess[X][Y-(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n\r\n \r\n }\r\n else if(k!=0 && (S-k)==0)\r\n {\r\n if(X+k<N && Chess[X+k][Y] != 'P')\r\n {\r\n Chess[X+k][Y] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X+k, Y, s, N, M-1);\r\n Chess[X+k][Y] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n if(X-k>=0 && Chess[X-k][Y] != 'P')\r\n {\r\n Chess[X-k][Y] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X-k, Y, s, N, M-1);\r\n Chess[X-k][Y] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n\r\n \r\n }\r\n }\r\n }\r\n //printf(\"X=%d Y=%d M=%d Val=%ld\\n\", X, Y, M, sum);\r\n DP[X][Y][M] = sum1;\r\n return sum1;\r\n } \r\n}\r\n</code></pre>"
How to do Fairy Chess problem in O(N^3)?
|algorithms|data-structures|dynamic-programming|
ERROR: type should be string, got "https://www.interviewstreet.com/challenges/dashboard/#problem/4f1c88e0dec8a\r\n\r\nFairy Chess (35 Points)\r\n\r\nYou have a N * N chess board. An S-leaper is a chess piece which can move from square (x1,y1) on the board to any other square (x2,y2) if |x1 - x2| + |y1 - y2| <= S. The chess board may also contain some pawns. The leaper cannot land on the same square as a pawn. In how many ways can a leaper move M times on the board?\r\n\r\nInput:\r\nThe first line contains the number of test cases T. T cases follow. Each case contains integers N, M and S on the first line. The next N lines contains N characters each. The ith character on the jth line is a '.' if the corresponding chess square is empty, 'P' if there is a pawn, or 'L' if the leaper is situated on that square.\r\n\r\nOutput:\r\nFor each case, output the number of ways the leaper can make M moves. Output each answer modulo 1000000007.\r\n\r\nConstraints:\r\n1 <= T <= 10\r\n1 <= S <= N <= 200\r\n1 <= M <= 200\r\nThere will be exactly one 'L' character on the board.\r\n\r\nSample Input:\r\n3\r\n4 1 1\r\n....\r\n.L..\r\n.P..\r\n....\r\n3 2 1\r\n...\r\n...\r\n..L\r\n4 3 2\r\n....\r\n...L\r\n..P.\r\nP...\r\n\r\nSample Output:\r\n4\r\n11\r\n385\r\n\r\n\r\n\r\nI wrote a DP solution but with O(N^5).\r\n\r\n<pre><code>\r\n/* Enter your code here. Read input from STDIN. Print output to STDOUT */\r\n\r\n#include&lt;stdio.h&gt;\r\n#include&lt;stdlib.h&gt;\r\n\r\nlong ways(long ***DP, char **Chess, int X, int Y, int S, int N, int M);\r\n\r\nint main()\r\n{\r\n int T;\r\n scanf(\"%d\", &T);\r\n while(T>0)\r\n {\r\n int N, M, S;\r\n scanf(\"%d\", &N);\r\n scanf(\"%d\", &M);\r\n scanf(\"%d\", &S);\r\n long ***DP = (long ***) malloc(sizeof(long **) * N);\r\n int i,j,k;\r\n char **Chess = (char **)malloc(sizeof(char *) *N);\r\n int Xstart;\r\n int Ystart; //printf(\"N=%dM=%dS=%d\\n\", N, M, S);\r\n for(i=0;i<N;i++)\r\n {\r\n Chess[i] = (char *)malloc(sizeof(char) *N);\r\n DP[i] = (long **)malloc(sizeof(long *) * N);\r\n for(j=0;j<N;j++)\r\n { //printf(\"i=%dj=%d\\n\", i, j);\r\n DP[i][j] = (long *)malloc(sizeof(long) *(M+1));\r\n char a;\r\n scanf(\" %c\", &a);\r\n if(a=='L')\r\n {\r\n Xstart = i;\r\n Ystart = j;\r\n }\r\n Chess[i][j] = a;\r\n //printf(\"jf=%d\\n\", j);\r\n }\r\n }\r\n \r\n for(i=0;i<N;i++)\r\n {\r\n for(j=0;j<N;j++)\r\n {\r\n for(k=1;k<M+1;k++)\r\n {\r\n DP[i][j][k] = -1;\r\n }\r\n DP[i][j][0] = 1;\r\n }\r\n }\r\n \r\n printf(\"%ld\\n\", ways(DP, Chess, Xstart, Ystart, S, N, M));\r\n T--;\r\n }\r\n}\r\n</code></pre>\r\n<pre><code>\r\nlong ways(long ***DP, char **Chess, int X, int Y, int s, int N, int M)\r\n{\r\n if(DP[X][Y][M] !=-1)\r\n {\r\n //printf(\"X=%d Y=%d M=%d Val=%ld\\n\", X, Y, M, DP[X][Y][M]);\r\n return DP[X][Y][M];\r\n }\r\n else\r\n {\r\n long sum1 = 0;\r\n\r\n \r\n int S,k;\r\n sum1 += ways(DP, Chess, X, Y, s, N, M-1);\r\n\r\n for(S=1;S<=s;S++)\r\n {\r\n for(k=0;k<=S;k++)\r\n {\r\n if(k!=0 && (S-k)!=0)\r\n {\r\n if(X+k<N && Y+(S-k) <N && Chess[X+k][Y+(S-k)] != 'P')\r\n {\r\n Chess[X+k][Y+(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X+k, Y+(S-k), s, N, M-1);\r\n Chess[X+k][Y+(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n if(X+k<N && Y-(S-k)>=0 && Chess[X+k][Y-(S-k)] != 'P')\r\n {\r\n Chess[X+k][Y-(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X+k, Y-(S-k), s, N, M-1);\r\n Chess[X+k][Y-(S-k)] = '.';\r\n Chess[X][Y] = 'L'; \r\n }\r\n if(X-k>=0 && Y+(S-k) <N && Chess[X-k][Y+(S-k)] != 'P')\r\n {\r\n Chess[X-k][Y+(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X-k, Y+(S-k), s, N, M-1);\r\n Chess[X-k][Y+(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n if(X-k>=0 && Y-(S-k) >=0 && Chess[X-k][Y-(S-k)] != 'P')\r\n {\r\n Chess[X-k][Y-(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X-k, Y-(S-k), s, N, M-1);\r\n Chess[X-k][Y-(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n\r\n \r\n }\r\n else if(k==0 && (S-k)!=0)\r\n {\r\n if(Y+(S-k)<N && Chess[X][Y+(S-k)] != 'P')\r\n {\r\n Chess[X][Y+(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X, Y+(S-k), s, N, M-1);\r\n Chess[X][Y+(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n if(Y-(S-k)>=0 && Chess[X][Y-(S-k)] != 'P')\r\n {\r\n Chess[X][Y-(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X, Y-(S-k), s, N, M-1);\r\n Chess[X][Y-(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n\r\n \r\n }\r\n else if(k!=0 && (S-k)==0)\r\n {\r\n if(X+k<N && Chess[X+k][Y] != 'P')\r\n {\r\n Chess[X+k][Y] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X+k, Y, s, N, M-1);\r\n Chess[X+k][Y] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n if(X-k>=0 && Chess[X-k][Y] != 'P')\r\n {\r\n Chess[X-k][Y] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X-k, Y, s, N, M-1);\r\n Chess[X-k][Y] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n\r\n \r\n }\r\n }\r\n }\r\n //printf(\"X=%d Y=%d M=%d Val=%ld\\n\", X, Y, M, sum);\r\n DP[X][Y][M] = sum1;\r\n return sum1;\r\n } \r\n}\r\n</code></pre>"
Your example may be too small. Consider the dynamic programming algorithm on 6 terms. Let's say that (A2 (A3 A4)) is better than ((A2 A3) A4). The dynamic programming algorithm is not going to compute any of the orders: > (A1 (((A2 A3) A4) A5)) A6 ((A1 ((A2 A3) A4)) A5) A6 (A1 ((A2 A3) A4)) (A5 A6) A1 (((...
A big hint, since this is 'homework' of a sort: this (obviously?) isn't actually anything resembling a chess problem; instead, it's a _graph theory_ problem in disguise. Consider the graph with $n^2-P$ nodes, one for each cell of the board, and an edge between two nodes if they're a leaper-distance away; then the ques...
**Inputs.** I am given a finite set $S$ of symbols. I know there should exist some total order $<$ on $S$, but I'm not given this ordering and it could be anything. I am also given a collection of assertions. Each assertion takes the form $s_1<s_2<\cdots<s_m$, where $s_1,\dots,s_m$ form a subset of the symbols of ...
Given many partial orders, check them for consistency and report any that are not consistent
|algorithms|applied-theory|
Consider the following specification technique. A specification consists of a finite set of triples $\langle C, A, C' \rangle$, where $A$ is the name of an action and $C, C'$ are conditions, that is, propositional formulas over propositional variables. Example rules (in a more suggestive notation): - $C_1 ...
Spot the formalism
|logic|model-checking|linear-temporal-logic|hoare-logic|
A big hint, since this is 'homework' of a sort: this (obviously?) isn't actually anything resembling a chess problem; instead, it's a _graph theory_ problem in disguise. Consider the graph with $n^2-P$ nodes, one for each cell of the board, and an edge between two nodes if they're a leaper-distance away; then the ques...
Spot the formalism (some kind of process logic)
I thought I'd jot down the two approaches that have occurred to me, but maybe you can do better. **Approach 1.** Here's a randomized procedure that, given an ordering of the assertions, will emit a candidate set of assertions labelled as "probably-erroneous" such that the remainder are all self-consistent. We can r...
I thought I'd jot down the two approaches that have occurred to me, but maybe you can do better. **Approach 1.** Here's a randomized procedure that, given an ordering of the assertions, will emit a candidate set of assertions labelled as "probably-erroneous" such that the remainder are all self-consistent. We can r...
ERROR: type should be string, got "https://www.interviewstreet.com/challenges/dashboard/#problem/4f1c88e0dec8a\r\n\r\n> Fairy Chess (35 Points)\r\n\r\n> You have a $N \\times N$ chess board. An $S$-leaper is a chess piece which can move from square $(x_1,y_1)$ on the board to any other square $(x_2,y_2)$ if $|x_1 - x_2| + |y_1 - y_2| \\le S$. The chess board may also contain some pawns. The leaper cannot land on the same square as a pawn. In how many ways can a leaper move $M$ times on the board?\r\n\r\n> Input:\r\nThe first line contains the number of test cases $T$. $T$ cases follow. Each case contains integers $N$, $M$ and $S$ on the first line. The next $N$ lines contains $N$ characters each. The $i$th character on the $j$th line is a `.` if the corresponding chess square is empty, `P` if there is a pawn, or `L` if the leaper is situated on that square.\r\n\r\n> Output:\r\nFor each case, output the number of ways the leaper can make $M$ moves. Output each answer modulo 1000000007.\r\n\r\n> Constraints:\r\n$$\\begin{gather}\r\n1 \\le T \\le 10 \\\\\r\n1 \\le S \\le N \\le 200 \\\\\r\n1 \\le M \\le 200 \\\\\r\n\\end{gather}$$\r\nThere will be exactly one `L` character on the board.\r\n\r\n> Sample Input:\r\n> \r\n 3\r\n 4 1 1\r\n ....\r\n .L..\r\n .P..\r\n ....\r\n 3 2 1\r\n ...\r\n ...\r\n ..L\r\n 4 3 2\r\n ....\r\n ...L\r\n ..P.\r\n P...\r\n\r\n> Sample Output:\r\n> \r\n 4\r\n 11\r\n 385\r\n\r\n\r\nI wrote a DP solution but with O(N^5).\r\n\r\n /* Enter your code here. Read input from STDIN. Print output to STDOUT */\r\n \r\n #include <stdio.h>\r\n #include <stdlib.h>\r\n \r\n long ways(long ***DP, char **Chess, int X, int Y, int S, int N, int M);\r\n \r\n int main()\r\n {\r\n int T;\r\n scanf(\"%d\", &T);\r\n while(T>0)\r\n {\r\n int N, M, S;\r\n scanf(\"%d\", &N);\r\n scanf(\"%d\", &M);\r\n scanf(\"%d\", &S);\r\n long ***DP = (long ***) malloc(sizeof(long **) * N);\r\n int i,j,k;\r\n char **Chess = (char **)malloc(sizeof(char *) *N);\r\n int Xstart;\r\n int Ystart; //printf(\"N=%dM=%dS=%d\\n\", N, M, S);\r\n for(i=0;i<N;i++)\r\n {\r\n Chess[i] = (char *)malloc(sizeof(char) *N);\r\n DP[i] = (long **)malloc(sizeof(long *) * N);\r\n for(j=0;j<N;j++)\r\n { //printf(\"i=%dj=%d\\n\", i, j);\r\n DP[i][j] = (long *)malloc(sizeof(long) *(M+1));\r\n char a;\r\n scanf(\" %c\", &a);\r\n if(a=='L')\r\n {\r\n Xstart = i;\r\n Ystart = j;\r\n }\r\n Chess[i][j] = a;\r\n //printf(\"jf=%d\\n\", j);\r\n }\r\n }\r\n \r\n for(i=0;i<N;i++)\r\n {\r\n for(j=0;j<N;j++)\r\n {\r\n for(k=1;k<M+1;k++)\r\n {\r\n DP[i][j][k] = -1;\r\n }\r\n DP[i][j][0] = 1;\r\n }\r\n }\r\n \r\n printf(\"%ld\\n\", ways(DP, Chess, Xstart, Ystart, S, N, M));\r\n T--;\r\n }\r\n }\r\n\r\n long ways(long ***DP, char **Chess, int X, int Y, int s, int N, int M)\r\n {\r\n if(DP[X][Y][M] !=-1)\r\n {\r\n //printf(\"X=%d Y=%d M=%d Val=%ld\\n\", X, Y, M, DP[X][Y][M]);\r\n return DP[X][Y][M];\r\n }\r\n else\r\n {\r\n long sum1 = 0;\r\n \r\n \r\n int S,k;\r\n sum1 += ways(DP, Chess, X, Y, s, N, M-1);\r\n \r\n for(S=1;S<=s;S++)\r\n {\r\n for(k=0;k<=S;k++)\r\n {\r\n if(k!=0 && (S-k)!=0)\r\n {\r\n if(X+k<N && Y+(S-k) <N && Chess[X+k][Y+(S-k)] != 'P')\r\n {\r\n Chess[X+k][Y+(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X+k, Y+(S-k), s, N, M-1);\r\n Chess[X+k][Y+(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n if(X+k<N && Y-(S-k)>=0 && Chess[X+k][Y-(S-k)] != 'P')\r\n {\r\n Chess[X+k][Y-(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X+k, Y-(S-k), s, N, M-1);\r\n Chess[X+k][Y-(S-k)] = '.';\r\n Chess[X][Y] = 'L'; \r\n }\r\n if(X-k>=0 && Y+(S-k) <N && Chess[X-k][Y+(S-k)] != 'P')\r\n {\r\n Chess[X-k][Y+(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X-k, Y+(S-k), s, N, M-1);\r\n Chess[X-k][Y+(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n if(X-k>=0 && Y-(S-k) >=0 && Chess[X-k][Y-(S-k)] != 'P')\r\n {\r\n Chess[X-k][Y-(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X-k, Y-(S-k), s, N, M-1);\r\n Chess[X-k][Y-(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n \r\n \r\n }\r\n else if(k==0 && (S-k)!=0)\r\n {\r\n if(Y+(S-k)<N && Chess[X][Y+(S-k)] != 'P')\r\n {\r\n Chess[X][Y+(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X, Y+(S-k), s, N, M-1);\r\n Chess[X][Y+(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n if(Y-(S-k)>=0 && Chess[X][Y-(S-k)] != 'P')\r\n {\r\n Chess[X][Y-(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X, Y-(S-k), s, N, M-1);\r\n Chess[X][Y-(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n \r\n \r\n }\r\n else if(k!=0 && (S-k)==0)\r\n {\r\n if(X+k<N && Chess[X+k][Y] != 'P')\r\n {\r\n Chess[X+k][Y] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X+k, Y, s, N, M-1);\r\n Chess[X+k][Y] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n if(X-k>=0 && Chess[X-k][Y] != 'P')\r\n {\r\n Chess[X-k][Y] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X-k, Y, s, N, M-1);\r\n Chess[X-k][Y] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n \r\n \r\n }\r\n }\r\n }\r\n //printf(\"X=%d Y=%d M=%d Val=%ld\\n\", X, Y, M, sum);\r\n DP[X][Y][M] = sum1;\r\n return sum1;\r\n } \r\n }\r\n"
|algorithms|graphs|approximation|finite-sets|partial-order|
ERROR: type should be string, got "https://www.interviewstreet.com/challenges/dashboard/#problem/4f1c88e0dec8a\r\n\r\n> Fairy Chess (35 Points)\r\n\r\n> You have a $N \\times N$ chess board. An $S$-leaper is a chess piece which can move from square $(x_1,y_1)$ on the board to any other square $(x_2,y_2)$ if $|x_1 - x_2| + |y_1 - y_2| \\le S$. The chess board may also contain some pawns. The leaper cannot land on the same square as a pawn. In how many ways can a leaper move $M$ times on the board?\r\n\r\n> Input:\r\nThe first line contains the number of test cases $T$. $T$ cases follow. Each case contains integers $N$, $M$ and $S$ on the first line. The next $N$ lines contains $N$ characters each. The $i$th character on the $j$th line is a `.` if the corresponding chess square is empty, `P` if there is a pawn, or `L` if the leaper is situated on that square.\r\n\r\n> Output:\r\nFor each case, output the number of ways the leaper can make $M$ moves. Output each answer modulo 1000000007.\r\n\r\n> Constraints:\r\n$$\\begin{gather}\r\n1 \\le T \\le 10 \\\\\r\n1 \\le S \\le N \\le 200 \\\\\r\n1 \\le M \\le 200 \\\\\r\n\\end{gather}$$\r\nThere will be exactly one `L` character on the board.\r\n\r\n> Sample Input:\r\n> \r\n 3\r\n 4 1 1\r\n ....\r\n .L..\r\n .P..\r\n ....\r\n 3 2 1\r\n ...\r\n ...\r\n ..L\r\n 4 3 2\r\n ....\r\n ...L\r\n ..P.\r\n P...\r\n\r\n> Sample Output:\r\n> \r\n 4\r\n 11\r\n 385\r\n\r\n\r\nI wrote a DP solution but with O(N^5).\r\n>Psuedocode for ways function:\r\n\r\n Memoize[Xmax][Ymax][Nmax];\r\n \r\n ways(int X, int Y, int M) // (X,Y) current co-ordinates and M is number of moves to make\r\n {\r\n if(Memoize[X][Y][M] != -1) // != -1 means we already have the result\r\n return Memoize[X][Y][M];\r\n sum=0;\r\n for all (u,v) such that |X-u| + |Y-v| <= S\r\n sum += ways(u, v, M-1);\r\n \r\n Memoize[X][Y][M] = sum;\r\n return sum;\r\n \r\n }\r\n\r\n>Code:\r\n\r\n /* Enter your code here. Read input from STDIN. Print output to STDOUT */\r\n \r\n #include <stdio.h>\r\n #include <stdlib.h>\r\n \r\n long ways(long ***DP, char **Chess, int X, int Y, int S, int N, int M);\r\n \r\n int main()\r\n {\r\n int T;\r\n scanf(\"%d\", &T);\r\n while(T>0)\r\n {\r\n int N, M, S;\r\n scanf(\"%d\", &N);\r\n scanf(\"%d\", &M);\r\n scanf(\"%d\", &S);\r\n long ***DP = (long ***) malloc(sizeof(long **) * N);\r\n int i,j,k;\r\n char **Chess = (char **)malloc(sizeof(char *) *N);\r\n int Xstart;\r\n int Ystart; //printf(\"N=%dM=%dS=%d\\n\", N, M, S);\r\n for(i=0;i<N;i++)\r\n {\r\n Chess[i] = (char *)malloc(sizeof(char) *N);\r\n DP[i] = (long **)malloc(sizeof(long *) * N);\r\n for(j=0;j<N;j++)\r\n { //printf(\"i=%dj=%d\\n\", i, j);\r\n DP[i][j] = (long *)malloc(sizeof(long) *(M+1));\r\n char a;\r\n scanf(\" %c\", &a);\r\n if(a=='L')\r\n {\r\n Xstart = i;\r\n Ystart = j;\r\n }\r\n Chess[i][j] = a;\r\n //printf(\"jf=%d\\n\", j);\r\n }\r\n }\r\n \r\n for(i=0;i<N;i++)\r\n {\r\n for(j=0;j<N;j++)\r\n {\r\n for(k=1;k<M+1;k++)\r\n {\r\n DP[i][j][k] = -1;\r\n }\r\n DP[i][j][0] = 1;\r\n }\r\n }\r\n \r\n printf(\"%ld\\n\", ways(DP, Chess, Xstart, Ystart, S, N, M));\r\n T--;\r\n }\r\n }\r\n\r\n long ways(long ***DP, char **Chess, int X, int Y, int s, int N, int M)\r\n {\r\n if(DP[X][Y][M] !=-1)\r\n {\r\n //printf(\"X=%d Y=%d M=%d Val=%ld\\n\", X, Y, M, DP[X][Y][M]);\r\n return DP[X][Y][M];\r\n }\r\n else\r\n {\r\n long sum1 = 0;\r\n \r\n \r\n int S,k;\r\n sum1 += ways(DP, Chess, X, Y, s, N, M-1);\r\n \r\n for(S=1;S<=s;S++)\r\n {\r\n for(k=0;k<=S;k++)\r\n {\r\n if(k!=0 && (S-k)!=0)\r\n {\r\n if(X+k<N && Y+(S-k) <N && Chess[X+k][Y+(S-k)] != 'P')\r\n {\r\n Chess[X+k][Y+(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X+k, Y+(S-k), s, N, M-1);\r\n Chess[X+k][Y+(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n if(X+k<N && Y-(S-k)>=0 && Chess[X+k][Y-(S-k)] != 'P')\r\n {\r\n Chess[X+k][Y-(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X+k, Y-(S-k), s, N, M-1);\r\n Chess[X+k][Y-(S-k)] = '.';\r\n Chess[X][Y] = 'L'; \r\n }\r\n if(X-k>=0 && Y+(S-k) <N && Chess[X-k][Y+(S-k)] != 'P')\r\n {\r\n Chess[X-k][Y+(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X-k, Y+(S-k), s, N, M-1);\r\n Chess[X-k][Y+(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n if(X-k>=0 && Y-(S-k) >=0 && Chess[X-k][Y-(S-k)] != 'P')\r\n {\r\n Chess[X-k][Y-(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X-k, Y-(S-k), s, N, M-1);\r\n Chess[X-k][Y-(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n \r\n \r\n }\r\n else if(k==0 && (S-k)!=0)\r\n {\r\n if(Y+(S-k)<N && Chess[X][Y+(S-k)] != 'P')\r\n {\r\n Chess[X][Y+(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X, Y+(S-k), s, N, M-1);\r\n Chess[X][Y+(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n if(Y-(S-k)>=0 && Chess[X][Y-(S-k)] != 'P')\r\n {\r\n Chess[X][Y-(S-k)] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X, Y-(S-k), s, N, M-1);\r\n Chess[X][Y-(S-k)] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n \r\n \r\n }\r\n else if(k!=0 && (S-k)==0)\r\n {\r\n if(X+k<N && Chess[X+k][Y] != 'P')\r\n {\r\n Chess[X+k][Y] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X+k, Y, s, N, M-1);\r\n Chess[X+k][Y] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n if(X-k>=0 && Chess[X-k][Y] != 'P')\r\n {\r\n Chess[X-k][Y] = 'L';\r\n Chess[X][Y] = '.';\r\n sum1 += ways(DP, Chess, X-k, Y, s, N, M-1);\r\n Chess[X-k][Y] = '.';\r\n Chess[X][Y] = 'L';\r\n }\r\n \r\n \r\n }\r\n }\r\n }\r\n //printf(\"X=%d Y=%d M=%d Val=%ld\\n\", X, Y, M, sum);\r\n DP[X][Y][M] = sum1;\r\n return sum1;\r\n } \r\n }\r\n"
A big hint, since this is 'homework' of a sort: this (obviously?) isn't actually anything resembling a chess problem; instead, it's a _graph theory_ problem in disguise. Consider the graph with $n^2-P$ nodes, one for each cell of the board, and an edge between two nodes if they're a leaper-distance away; then the ques...
I'm looking to work out the big-O notation for the following: $$\frac{n^{s + 1} - 1}{n - 1} - 1$$ I have a feeling the result is $O\left( n^s \right)$ but I'm not sure how to prove it. Any help greatly appreciated! :) I tried to tag this as homework as is required by etiquette on Stack Overflow but there doesn...
Finding big O notation of function with two parameters
|time-complexity|
Some of modifications of *O* described in [Concrete Mathematics: Foundation for Computer Science][1] says: $ \qquad O(f(n)) + O(g(n)) = O(\mid f(n)\mid + \mid g(n) \mid) \qquad (9.22)\\ \qquad O(f(n))O(g(n)) = O(f(n)g(n)) \qquad \qquad \qquad (9.26) $ And using some basic knowledge of *O* notation and functio...
In some literature, as well as in [Wikipedia][1], the middle term parameter *m* of Mersenne twister is called "number of parallel sequences". Why? What is meant here by "parallel sequences"? [1]: http://en.wikipedia.org/wiki/Mersenne_twister
Mersenne twister middle word
|pseudo-random-generators|
null
Let f(N) be the average number of full nodes (nodes with two children) in an N-node binary search tree. 1. Determine the values of f(0) and f(1). 2. Given that for N > 1, f(N) = (N-2)/N + 1/N[summation from i=0 to N-1(f(i) + f(N - i - 1))], show that f(N) =(N - 2)/3 3. Using this information, show that the...
Let f(N) be the average number of full nodes (nodes with two children) in an N-node binary search tree
|data-structures|
This sounds like weighted FAS (Feedback Arc Set). Find a minimal (weight) feedback arc set in a directed graph and report the assertions that contributed to the wrongly directed arcs. For each pair of symbols s and t, there is an arc saying how many assertions have s < t and how many assertions have t < s. This ...
I've been studying the use of OpenGL and DirectX and even though I am at an intermediate level, I believe this is where the use of bump maps, light maps etc comes into play. These are encapsulated in the idea of a Shader. The hardest part about the shader and lighting to work correctly with the 3D object with respect t...
I'm looking to work out the big-O notation for the following: $$\frac{n^{s + 1} - 1}{n - 1} - 1$$ I have a feeling the result is $O\left( n^s \right)$ but I'm not sure how to prove it. Any help greatly appreciated! :)
How to approach it? It's always the same answer - **go back to the definitions**. the definition of PRF, talks about a *family* of functions $\{f_k\}$. The key $k$ just selects one specific function out of this big family. Next, for $\{f_k\}$ to be a PRF family,the definition requires that any PPT algorithm $A$ w...
|terminology|cryptography|pseudo-random-generators|
Imagine your solution now is Gn = Gn-1 + Gn-2 + Gn-3 + Gn-4 The solution just changes to [ Gn Gn-1 Gn-2 ] = [ Gn-1 Gn-2 Gn-3 ] [ 1 1 0 ] [ 1 0 1 ] [ 1 0 0 ] Similarly [ Gn-1 Gn-2 Gn-3 ] = [ Gn-2 Gn-3 Gn-4 ] [ 1 1...
Imagine your solution now is Gn = Gn-1 + Gn-2 + Gn-3 The solution just changes to [ Gn Gn-1 Gn-2 ] = [ Gn-1 Gn-2 Gn-3 ] [ 1 1 0 ] [ 1 0 1 ] [ 1 0 0 ] Similarly [ Gn-1 Gn-2 Gn-3 ] = [ Gn-2 Gn-3 Gn-4 ] [ 1 1 0 ] ...
(Not a comp.scientist, but have the basic research. Please excuse me if I've overlooked anything obvious.) In my variant of the problem I have a set A of agents and a set (of possibly different cardinality) T of tasks. Each agent needs to be assigned exactly n or n+1 tasks, and each task needs to be assigned to exac...
A variant of the Assignment Problem
|optimization|linear-programming|assignment-problem|
I was studying operating system concepts from galvin's sixth edition and i have some questions about the flow of execution of a program. A figure explains the processing of the user program as [this][1] We get an executable binary file when we reach **linkage editor** point. As the book says, **"The program must be ...
Questions on Program execution flow
|operating-systems|memory-management|virtual-memory|
I will try (1) and go for infinite (the bonus). The finite case is called the *code problem*: finding possible decompositions is like decoding. Its classical algorithm is called *Sardinas–Patterson*. It is not immediately clear it is a finite search space. The lengths of the decomposition might be long. Still it is ...
First of all, you are fundamentally confusing the purpose of RAM and hard disk. RAM is the only part of memory that CPU can access. Hard disk exists to expand the capacity of RAM as well as store the data permanently (data on RAM is transient). How it works - 1. The computer generates an executable and stores it ...
First of all, you are fundamentally confusing the purpose of RAM and hard disk. RAM is the only part of memory that CPU can access. Hard disk exists to expand the capacity of RAM as well as store the data permanently (data on RAM is transient). How it works - 1. The computer generates an executable and stores it ...
First of all, you are fundamentally confusing the purpose of RAM and hard disk. RAM is the only part of memory that CPU can access. Hard disk exists to expand the capacity of RAM as well as store the data permanently (data on RAM is transient). How it works - 1. The computer generates an executable and stores it ...
From [Van Emde Boas trees lecture][1]: > We will use the idea of superimposing a tree of degree u^1/2 on top of > a bit vector, but shrink the universe size recursively by a square > root at each tree level. The u^1/2 items on the first level each hold > structures of u^1/4 items, which hold structures of u^1/8 i...
Shrinking the universe concept in Van Emde Boas tree
|algorithms|data-structures|search-trees|trees|
In order to achieve the time complexity of O(lg lg u) for Van Emde Boas tree I read in [this lecture][1] that the the universe size u is chosen as `u = 2^2^k` for some integer k for Van Emde Boas trees. Why choose `u` to be of this specific form ? [1]:http://fileadmin.cs.lth.se/cs/Personal/Rolf_Karlsson/lect12.pdf