If you find all the small divisors of n that are less than sqrt(n), you can find the rest of them dividing n by the small ones.
By the way, this problem is widely known and googlable :) You can, for example, check out this link: http://stackoverflow.com/questions/26753839/efficiently-getting-all-divisors-of-a-given-number
Try coming up either with greedy algorithm or with two pointers algorithm.
Try to prove the following greedy: in each step we can choose the cheapest remaining mouse. If there is a computer left that has only one type of port suitable for this mouse, plug it there. Else if there is a computer with both types, plug it there. Else discard this mouse.
Try to also come up with the two pointers solution. If you cannot, it is described under the next spoiler.
Sort all of the USB mouses and all of the PS/2 mouses so that the price is non-descending. Then you will need to buy some prefix of USB mouses and some prefix of PS/2 mouses. Iterate over the number of USB mouses from 0 to their count. Now, the more USB mouses you buy and plug into computers, the less PS/2 mouses you will be able to buy, because the number of computers will only be decreasing. So you should move the first pointer forward, and in every iteration move the second pointer backwards until you reach such amount that it is possible to plug both USB and PC/2 mouses in.
Try thinking not about erasing a substring from B, but rather picking some number of characters (possibly zero) from the left, and some from the right.
Two pointers
For every prefix of B, count how big of a prefix of A you will require. Call these values p[i]. Put infinity in the cells where even whole A is not enough.
Same for every suffix of B count the length of the required suffix of A. Call these values s[i].
Now try increasing the length of prefix of B, while decreasing the length of the suffix until p[pref_len] + s[suf_len] is less or equal to the length of A.
The toughest thing about this task, is that you can go to the left. Try to come up with something to handle that.
Try to prove that in optimal solution you don't need to go more than one cell to the left before coming back.
Try to come up with a solution where you iterate over each frequency
Try to group stations that will be on the left side in a pair in one vector, and stations that will be on the right side in a pair into another.
Iterate over each frequncy. Suppose you are now on frequency i. Put all radio stations with frequencly i in the left vector, and all radio stations with frequencies i - k..i + k into the right vector. Notice, that the total size of all vectors you get this way is no more than (2 * k + 2) * n, because every radiostation will be one time in the left vector and at most 2 * k + 1 times in the right vector.
Now we need to calculate the number of possible pairs where left radio station is from vector left and right radio station is from vector right.
Sort stations in the left vector by position. Sort stations in the right vector by left bound of their range. Iterate over the stations from the left vector. Now, as you do that, larger and larger prefix of the right vector will have stations with their left bound less or equal to the coordinate of the currently processed station from the left vector. For each new such station you should add 1 to some RSQ structure (easiest is fenwick tree) to the position of this station. Since positions are up to 109, you will have to compress the coordinates (for example, use index of station in the sorted order instead of it's coordinate). Can you see how to query this fenwick tree to get the number of stations that match the currently processed station from the left vector?
No nested spoilers. It's serious business here!
One of the possible ways to make your life easier is to count the number of automorphisms of tree T. This way you will be able to first calculate the number of labeled matchings of vertices of tree T to the vertices of tree S, and then divide this number by the number of automorphisms.
Although solution that I will describe does not use this! :D
First, remember that every automorphism has a fixed point. Either a vertex or an edge. This is called center of the tree, and you can find it by first finding the diameter of the tree. It's middle vertex (or an edge, if there are two middle vertices) is the center of the tree.
Let's root T at it's center. Now let's enumerate subtrees (rooted ones!) of T in such a way that if two subtress are isomorphic they will receive the same number and vice versa. You can do it in a single dfs processing subtrees bottom-up. These numbers will correspond to different isomorphisms.
Now you can do a dp with memoization to calculate for each subtree of S rooted at some directed edge the number of ways to "attach" each of the isomorphisms from above to this subtree. This can be done by going through all immediate children of the currently processed vertex of S and doing a bitmask DP, where bits are immediate children of the root of currently processed isomophism of T. 1 means it is "attached" to some children in S, 0 means not.
One of the problems here is that root of current isomorphism of T can have isomorphic children, and if we shuffle how they are attached to children in S we will still receive the same way to cover S with T, so we will calculate some ways twice or more. The solution is to sort children by their isomorphism number and when processing a bitmask, never put 1 to the bit that has a 0 bit to the left that corresponds to the child with the same isomorphism number. This way you will match such children in a fixed order and won't calculate anything unnecessary.