Skip to main content

Posts

Showing posts with the label solve

SPOJ - ENIGMATH solution C++

Problem Statement: ENIGMATH - PLAY WITH MATH Heuristic: Ax - By = 0   has two possible cases:   a can divide b or if b can divide a  and you can figure out the rest.  a or b cannot divide the other so it will be A B - B A = 0, but we still have to find the minimum x & y.

SPOJ 95 - STPAR - Street Parade

Problem Statement Heuristics:   Let, N be the number which is next in fixed order. Take an input X , if it is equal to N then next number will be n++. If not, then search for N in the side lane and after finishing the search, put X in the side lane. After all X is processed but N is not found in either X or side lane, then the order is not possible. Solution:

SPOJ 5 - PALIN

Problem Statement Explanation is here Sample Solution : #include<bits/stdc++.h> using namespace std; char k[1000005]; int l,i,j,mid; bool chkNine() {     cout<<""; //for SPOJ's internal problem, this line is needed     for(i=0;i<l;i++)     {         if( k[i]!='9' ) return false;     } } void nextPalin() {     int f=0;     l=strlen(k);     if(chkNine()) {         memset(k,'0',l);         k[0] ='1';         k[l]='1';         k[l+1]='\0';         return;     }     // calc mid,i,j     if(l%2==0)     {         i= ( l/2 )-1;      ...

SPOJ 394- ACODE

Problem Statement As from the problem-tag, you can see this is a DP( Dynamic Programming ) problem. Don't know Dynamic Programming? Here are some articles: Codechef , TopCoder . Try to solve a sample problem, for example, Fibonacci Numbers. Let's start cracking the problem. To solve a DP problem, the hardest and important part is to find the relation between previous or next steps and current step . We need to find out how the solution of the current step depends on its previous steps or next steps. Looking at:      2 5 1 1 4        from left to right Step 1: You can only interpret this as 2(B). Decoding- B EAAD  So, DP[1] = 1 Step 2: This can be 5(E) or collaborating with 2, it can be 25(Y). Result is BE AAD + Y AAD . So, DP[2] = 2 Step 3: Can only be interpreted as 1(A). 51 is not valid, as stated in problem. BEA AD + YA AD . DP[3] = 2.  Step 4: Will be interpreted as 1(A). BEAA D + YAA D+... ...

SPOJ 4300 - Rectangles

Problem Statement Hint: If the input is 15 squares rectangles are-                 1x1 1x2 1x3 ...................................1x15                        2x2 2x3 ............................2x7                                   3x3   ....................3x5                          can't make 3x2 cause 3x2 and 2x3 are same . can't make 2x8 cause that requires 16 squares. can't make 4x4 cause that also requires 16 square.  Solution: #include<stdio.h> int main() {     int n,c=0,i,j;     scanf("%d",&n);     for(i=1;i*i<= n;i++)    ...

Bitwise Sieve w/ code sample

Sieve of Eratosthenes is explained HERE Bitwise sieve is an optimized implementation of  Sieve of Eratosthenes. It does the same thing but more memory efficient because it uses logic operations( &,|,>>,<< ) on binary numbers. In normal implementation, we use an array as flag to check if a number is prime or not.we know index of an array ( i.e  myarray[n]  ) is 32 bit long. If an array is 10 index long, that it has 32x10(=320) bits.  We use bits of that array as flag. that way we can flag numbers upto 320 as prime or not prime. i.e, how to check the flag status of 51 in myarray[ n ] ? We'll divide 5 by 32, that will give us the index number of the certain bit, then we'll mod 5 by 32, that will give us the certain bit position to check for 0 or 1. Remember bit positions are defined from right to left, starting from 0. Meaning LST = 0 and MST = 31. So we need to define two functions, one to set that certain bit as 0 or 1, another to check if...

SPOJ 3410 - SAMER08F - Feynman

Problem Statement Explanation:   How many squares a (4x4) square holds? the answer is- 16 (1x1) squares + 4 (2x2) squares, 9 (3x3) squares, 1 (4x4) squares Meaning, the answer is n²+ (n-1)²+(n-2)²+.......+1² which is also equivalent to    Solution: #include<stdio.h> int main() {     int n,x;     while(scanf("%d",&n)&&n!=0)     {         x=(n*(n+1)*((2*n)+1))/6;         printf("%d\n",x);     }     return 0; }  

SPOJ 1112 - NSTEPS

Problem Statement Explanation : This might seem like a math problem. But it's not. There's a pattern in here. Firstly, figure out when output has no number. If you can figure it out then try to recognise the relation of the output with co-ordinates (x, y) Solution is given below: #include<stdio.h> int main() {     int t,x,y;     scanf("%d",&t ) ;     while(t--&&t>=0)     {         scanf("%d%d",&x,&y);         if(y==x||y==x-2)         {             if(x%2==0) printf("%d\n",x+y);             else printf("%d\n",x+y-1);         }         else         {   ...