Friday, 8 July 2016

Prime Ring Problem (UVA 524, HDU 1016, UVALive 5270, ZOJ 1457, SCU 1545, Regionals 1996 >> Asia - Shanghai)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / uva), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

int ring[20], n;
bool isPrime[33], taken[20];

void print()
{
    cout<<ring[1];
    for(int i=2;i<=n;i++)
        cout<<" "<<ring[i];
    cout<<"\n";
return;
}

void backTrack(int tot, int lst) //(total taken already, last taken number)
{
    if(tot==n)
    {
        if(isPrime[lst+1])
            print();
        return;
    }

    for(int i=2;i<=n;i++)
        if(!taken[i])
            if(isPrime[lst+i])
            {
                taken[i]=1;
                ring[tot+1]=i;
                backTrack(tot+1, i);
                taken[i]=0;
            }
return;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int kk=1, tc, m;
    string s;
    isPrime[2]=isPrime[3]=isPrime[5]=isPrime[7]=isPrime[11]=isPrime[13]=isPrime[17]=isPrime[19]=isPrime[23]=isPrime[29]=isPrime[31]=1;
    while(cin>>n)
    {
        CLR(taken);
        CLR(ring);
        ring[1]=taken[1]=1;//first circle fixed
        if(kk>1) cout<<"\n";
        cout<<"Case "<<kk++<<":\n";
        backTrack(1, 1); //first circle taken
    }
return 0;
}

Thursday, 9 June 2016

Getting started with Codeforces: Part 5: Solving your second problem (Watermelon) in codeforces.

Actually this problem is easier than the first one.

You will be given a number, you have to print YES if the number can be divided into two even number, NO otherwise.

Solution strategy:

can you divide 8 into two even numbers?
Yes, because 2+6 or 4+4 
can you divide 9 into two even numbers? try all possible combination, the answer will be NO.

So, what we get so far is, if the number is even, we can divide it into two even numbers and if it's odd, we can not divide it into two even numbers.

Wait.... There is a critical case. How about 2?
even it's an even number, we can not divide it into two even number. only possible way is 1+1 where both are odd numbers. remember 0 is not considered here because it's not positive even number.

Getting started with Codeforces: Part 4: Solving your first problem (Theatre Square) in codeforces.

First of all you have to understand the problem completely. I suggest to read part 3 for that.

Let's solve a mini problem first. We consider a line instead of a matrix.

To cover a line m, with length of 6 by stick a with length of 4, we need at least 2 sticks. See image below:

Let's have another example:  m=13, a=3

we see that at least 5 stick requires to cover the line fully. if the line length were 12, only 4 stick would be enough. 
Now we can understand and calculate visually, can we calculate required number of stick mathematically? 

yes, we can. For m=13 and a=3, required stick is simply  m/a >> 13/3 >> 4.33
Wait a minutes, we can not take 0.33 stick as we are not allowed to break it. In this case, 5 stick is needed. 
For m=12 and a=3, required stick is m/a=12/3=4.0, as no partial part needed, the answer is 4. 

Simplest way to handle this with Integer data type is: 
If the line length is divisible by stick length the answer is m/a, 
If there is some reminder the answer is m/a+1. 

In C++, 
Answer  = m /a;
 If(m%a != 0) Answer++;
Simple, right?

Now, tell me how many cell in the following matrix?
36? how did you calculate that? You have calculated the number of row (6) and column (6) and multiplied them (6*6=36) instead of counting all the cell one by one, right? 

We can apply this trick solving the main problem. We will count how many square require to cover vertically and how many horizontally and then multiply them to get the answer. 

For m=6, n=6, a=4. 
Vertically requires m/a = 6/4 = 1.5 = 2 (round up) 
Horizontally requires n/a = 6/4 =1.5 = 2 (round up) 
So, total minimum required square is 2*2 = 4. 
That's it...... 

Nope, That's not all actually, you will get "Wrong Answer" verdict. Look at the constraints. 
what if the input is m=10^9, n=10^9 and a=1? Answer will be 10^18 which is out of Integer range, in that case, you need to use "long long" data type instead of "integer". 

Getting started with Codeforces: Part 3: Understanding your first problem (Theatre Square) in codeforces.

As you have found that Theatre Square is the easiest problem according to sort described in Part 2.
Let's try to understand the problem in simplest way:

There is a m*n size rectangle and a*a size square given, you have to fill the rectangle using minimum number of square. 

Let's understand the problem in details with sample input: m=6, n=6, a=4. 
Considering the rectangle is colored red and square colored green as below. 

Remember, You have to use the square straight and can not break the square. You have to cover the rectangle fully, no problem if you cover more. 

Trying with 1 square: 
We see the rectangle (red color) is not covered completely. 

Trying with 2 square: 
still not covered. 

Trying with 3 square:
You can see there is 4 red units is not covered yet. 

Trying with 4 square: 
CIAO!!, now the red rectangle is completely covered. Thus we need minimum of 4 square of given size to cover the given rectangle fully. That's why the sample answer is 4


Tuesday, 7 June 2016

Getting started with Codeforces: Part 2: Find out easiest problem.

It's always better to start with the easiest problems first. To find easiest problems, follow the steps.

Step 1: First of all, login to you account. [read previous post for registration and login]

Step 2: Click on "PROBLEMSET" tab. or go http://codeforces.com/problemset directly.


Step 2: You will see a list of problems sorted according to the problem added date.


Step 3: But this is not the easiest list. Problems with maximum solve is the easiest. to sort the problem list according to maximum number of solve, click on the Solved button as marked red in the picture below.


You will notice the list has changed. This is the easiest list. You can also find the list by the following link.
http://codeforces.com/problemset?order=BY_SOLVED_DESC

Step 4: Click on the first problem. That's the easiest to start with.


Next Post: Understanding your first problem (Theatre Square) in codeforces.