Wednesday, 15 January 2014

Multiplication of two long numbers modulo a long number.

LL multiplication (LL a, LL b, LL mod) {
    LL res = (a * ((long double) b / (long double) mod));
    // put the number in long double, and then reduce the value to LL, forget 
    res = a * b - res * mod;
    if (res >= mod) res -= mod;
    if (res < 0) res += mod;
    return res;

Tuesday, 14 January 2014

WCP #3 And Editorials

WPC #3 was held on this sunday.
Problems have been moved to spoj.

Editorials: (these editorials are merely hints, You need to work things yourself for final answers)
Hope you will like them. Leave a comment if something is unclear or needs to be modified, Also leave your opinion about the contest.

1. Gopu and Palindromes: (
     Consider the string s. Let it has some consecutive characters of length L, Then you can directly replace L by just 1 character. After such replacements just check if the string is palindrome or not?
     eg. s = "ababbbaaa".
           s = "ababa". Replace bbb with b and aaa with a.
     You need to do this in O(n).

2.Gopu and Validity of Arrangement: (
   You can easily prove that if answer exists then it is unique, Only condition you need to check that any ith indexed person does not say that $\ge$ i persons are before him. You can relate this problem with permutations.

3. Gopu and Digits Divisibility: (
   You can easily do a brute force. Think about it why this works, Hint: for checking divisibility by 9, sum of digits should be divisible by 9. (Though hint is not of great use in the proof).

4. Gopu and Function: (
    Value of n will be atleast halved in each computation of f. As Take the best cases when n = 2*p where p is prime, Sum of divisors is 2 + p which is 2 + n / 2. In other cases, sum would be even less than n/2. For finding prime divisors of n, use simple sqrt algorithm. Final complexity would be O($\sqrt[2]{n}$ logn).

5. Gopu and Create Collections Part two (
   You can use dp over the tree for solving this. You can use a greedy algo also. Sort the numbers in increasing order, go from right to left and for each number K if you find a number K/2 which is not yet taken then take it and add it to answer.
   You can also use greedy algorithm by constructing the binary tree explicitly and then going from leaves to the root of the tree.

6. Gopu and Combinatorics on Graphs: (
  This is standard problem. You should know Then figuring out things wont be tough. I dont want to give formulla as I want you to work it out.

7. Gopu and Counting bitwise prime numbers: (
   This problem had one slow solution:
     use dp(i, tight): where i denotes the position in the binary representation of the number (i goes from most significant digit to lower), tight denotes wheteher the current number has overshoot the number n or n. See my slow code
    You can optimize it slightly and get it passed. Basically think in terms of combinatorics, You will get the idea.