Académique Documents
Professionnel Documents
Culture Documents
Checking if a number is "Strictly not a Prime" or not involves considering all subsequences of the number.
One can generate all subsequences of a number containing N digits by considering the binary representations of the
So the trick is to run a loop from 1 to 2^N-1. Then, in each iteration check index variables's bits(1 bits) to decide
For eg,
12345678
So 5 can represent the subsequence 6,8 (Though while implementing you consider the representation backwards i.e
5 represents 1, 3)
One simple optimization that can be applied to this process is that if any of the digits of the number being tested is 2,
If a number is found to be "Strictly not a Prime" you can mark it in an array like IS_SNAP[n] = 1 ( IS_SNAP[for all
n] = 0 initially )
One can maintain a table COUNT[n] which gives the total count of the required numbers between 1 to N
A particular point to note is that, its not mentioned in the problem statement that, A <= B is always true, so before
divyanshu's solution submitted during the contest implements all these ideas,
http://www.spoj.pl/MAIN11/files/src/5518159/
Re-Arrange II
Let,
A subset of the given numbers can be represented by an integer "mask" in the sense that if the ith bit is set in binary
representation of mask then A[i] is included in the set which mask represents.
Let,
F[ mask ][ i ] = minimum P for a set of integers(which do not include A[i]) represented by mask followed by A[i].
Then,
else
minimum of all abs(A[i]-a[k]) * C[no. of elements in mask set i.e no. of on bits in mask] +
F[ mask&~(1<<k) ][ k ] for all k, such that the kth bit if 1 in the binary representation of mask
Note that, mask&~(1<<k) represents the operation of removing the A[k] from the set represented by mask
Solution : http://ideone.com/jf99v
Special string
http://community.topcoder.com/tc?module=Static&d1=match_editorials&d2=srm412
Trip
Basically what the problems asks is, print all distinct lcs of the given two strings in lexicographical order
1. Compute the dp[][] table using dynamic programming as in the original lcs problem.
Since we need to print the lcs's in lexicograhic order so while contructing solution from dp table we can do the following
things,
if (lcs[x][y] == lcs[i][j] - 1) { // make sure we can get a solution of the right length
Have a look at this tutorial which discusses all the above ideas,
http://www.codechef.com/wiki/tutorial-lcs-problem-revisited
Check out Vaibhav Mittal's solution during the contest which implements all the ideas,
http://www.spoj.pl/MAIN11/files/src/5517814/
SPOJ-KGSS
Lets cut to the chase, the question requires us to use segment trees for RMQ. So if u do not know how to use segment
trees for Range Minimum (maximum for this question) Query, here's the link to get started
http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=lowestCommonAncestor
I tried to solve it using sparse table first but got TLE so dont waste time on solving with other methods.
Make a tree whose nodes save just the index of maximum number in the node's range. Initialise the tree with the array
given. For queries, we need first two largest numbers in the given range. So first find the index of the largest number in the
range <x,y>, say p is that index. Then find the index of the second largest number using two queries, first in <x,p-1> ,
second in <p+1,y>. The max of the two would be the second largest number.
if(b==e)
ar[i]=v;
m[node]=b;
return ;
int mid=(b+e)/2;
if(i<=mid)
update(2*node,b,mid,i,v);
else
update(2*node+1,mid+1,e,i,v);
if(ar[m[2*node]]>ar[m[2*node+1]])
m[node]=m[2*node];
else
m[node]=m[2*node+1];
2) Efficient-
Make the tree with nodes having not just the index of largest number but also the maxsum which we need to find. This will
allow us to make only one query. In the update function, the nodes will save maxsum somewhat like this
struct node{
int sum,mx;
};
...
ICODER
It should be clear that adding any number to Register R and then taking MOD(65536), will not affect the number of values
that can be obtained. Try with some values if you are not sure.
MUL X
R=(R * X)MOD(65536)
65536=2^16
Two cases
1) a<=16, then R=(R*y)MOD(2^(16-a)). Maximum values which we can obtain are 2^(16-a).