The Ball And Cups

#codeifyoucansolve
At the end of a busy day, The Chef and his assistants play a game together. The game is not just for fun but also used to decide who will have to clean the kitchen. The Chef is a Game Master, so his concern is how to manage the game but not how to win the game like his assistants do.

The game requires players to find the only ball under one of the N cups after their positions are changed in a special way. At the beginning of the game, The Chef places N cups in a row and put a ball under the C-th cup from the left (the cups are numbered from 1 to N). All players can see the initial position of the ball. Then Chef performs Q flip operations. Each flip operation is defined by two integers L and R such that 1 ≤ L ≤ R ≤ N and consists in reversing the segment [L, R] of cups. Namely, Chef swaps L-th and R-th cups, (L+1)-th and (R−1)-th cups, and so on. After performing all the operations Chef asks his assistants to choose a cup that they think the ball is under it. Who can guess the position of the ball will win the game, and of course, the others will have to clean the kitchen.

The Chef doesn’t want to check all the N cups at the end of the game. He notes down the value of C and the pairs (L, R) and asked you, the mastered programmer, to determine the cup that contains the ball.
Input

The first line of the input contains a single integer T, denoting the number of test cases. The description of T test cases follows. The first line of each test case contains three space-separated integers N, C and Q, denoting the total number of cups, the initial position of the ball and the number of flip operations Chef will perform. Each of the following Q lines contains two space-separated integers L and R, denoting the ends of the segment of the current flip operation.
Output

For each test case output on a separate line the final position of the ball.
Constraints

1 ≤ T ≤ 10
1 ≤ N ≤ 100000 (105)
1 ≤ C ≤ N
1 ≤ Q ≤ 10000 (104)
1 ≤ L ≤ R ≤ N

Example

Input:
1
5 2 3
1 4
3 5
1 5

Output:
1

Explanation

The row of cups at the beginning of the game and after each flip is shown below. Here ‘-‘ means an empty cup and ‘B’ is the cup that hides the ball, the segment of flip is marked bold.

-B—
–B–
—-B
B—-

Solution :

#include<stdio.h>
#define MAX 100000L

int main()
{
int T;
unsigned long N,C,Q,L,R;
scanf("%d",&T);
for(;T>0;T--)
{
scanf("%lu%lu%lu",&N,&C,&Q);
if(N>MAX)
break;
if(C>N||C<1L)
break;
if(Q>10000L)
break;
for(;Q>0;Q--)
{
scanf("%lu%lu",&L,&R);
if(R>N||L>R||L<1L)
break;
for(;L<R;L++,R--)
{
if(C==L)
C=R;
else if(C==R)
C=L;
}
}
printf("%lu\n",C);
}
return 0;
}

Compiled :
http://ideone.com/2ujyNZ

All square roots are periodic when written as continued fractions.

#codeifyoucansolve
All square roots are periodic when written as continued fractions.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        final int MAX=30000;
        int result=0;
        Scanner sc=new Scanner(System.in);
        int N=sc.nextInt();
        if(N>0&&N<=MAX)
        {       
            for(int i=2;i<=N;i++)
            {
           int lmt=(int)Math.sqrt(i);
                if(lmt*lmt==i)
                    continue;
           int period = 0;
           int d = 1;
           int m = 0;
           int a = lmt;      
                do{                
                    m = d*a - m;                    
                    d = (i - m * m) / d;
                    a = (lmt + m) / d;
                    period++;
                }while(a != 2*lmt);
            if((period%2)==1)
                result++;
            }
            System.out.print(result);
        }
    }
}

Powerful Digit.

#codeifyoucansolve
The 5−digit number, 16807=7^5, is also a fifth power. Similarly, the 9-digit number, 134217728=8^9, is a ninth power.

Given N, print the N−digit positive integers which are also an Nth power?

 
#include <stdio.h>
#include <math.h>
int main(void) {
unsigned long long int lb,ub,i,tmp;
int n;
scanf("%d",&n);
if(n>0&&n<=19)
{
lb=pow(10,(n-1));
ub=pow(10,n);
i=1LL;
while(1)
{
tmp=(long long int)pow(i,n);
 
if((n==19||n==18||n==17)&&tmp>=lb)
    {
       printf("%llu\n",tmp);
    break;
}
if(tmp>=lb&&tmp<ub)
{
   printf("%llu\n",tmp);
}
    if(tmp>=ub)
        break;
i++;
}
}
return 0;
}

Roy’s Life Cycle

#codeifyoucansolve
Roy is going through the dark times of his life. Recently his girl friend broke up with him and to overcome the pain of acute misery he decided to restrict himself to Eat-Sleep-Code life cycle. For N days he did nothing but eat, sleep and code.

A close friend of Roy kept an eye on him for last N days. For every single minute of the day, he kept track of Roy’s actions and prepared a log file.

The log file contains exactly N lines, each line contains a string of length 1440 ( i.e. number of minutes in 24 hours of the day).
The string is made of characters E, S, and C only; representing Eat, Sleep and Code respectively. ith character of the string represents what Roy was doing during ith minute of the day.

Roy’s friend is now interested in finding out the maximum of longest coding streak of the day – X.
He also wants to find the longest coding streak of N days – Y.
Coding streak means number of C’s without any E or S in between

    #include <stdio.h>
    #define C_LENGTH 1440
    #define MAXDAY 365
    int main()
    {
    int N,i,j,max,count,maxperday,maxall;
    char work[MAXDAY][C_LENGTH],ch;
    maxall=maxperday=max=count=0;
    scanf("%d",&N);
    if(N>0&&N<=MAXDAY)
    {
    for(i=0;i<N;i++)
    {
    for(j=0;j<C_LENGTH;j++)
    {
    scanf("%c",&ch);
    if(ch=='S'||ch=='E'||ch=='C')
    {
    work[i][j]=ch;
    }
    else
    j=j-1;
    }
    }
    for(i=0;i<N;i++)
    {
    for(j=0;j<C_LENGTH;j++)
    {
    if(work[i][j]=='C')
    {
    count++;
    max++;
    }
    if(work[i][j]!='C')
    {
    if(count>maxperday)
    maxperday=count;
    if(max>maxall)
    maxall=max;
    max=0;
    count=0;
    }
    }
    if(count>maxperday)
    {
    maxperday=count;
    count=0;
    }
    }
    }
    printf("%d %d",maxperday,maxall);
    }

Max XOR

Given two integers: L and R, find the maximal values of A xor B given, L ≤ A ≤ B ≤ R.
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
/*
* Complete the function below.
*/

static int maxXor(int l, int r) {
int max=0,tmp;
for(int i=l;i<=(r+1);i++)
{
if(i==r)
{
l+=1;
i=l;
}
if(l>=r)
break;
tmp=l^i;
if(tmp>max)
max=tmp;
}
return max;
}

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int res;
int _l;
_l = Integer.parseInt(in.nextLine());

int _r;
_r = Integer.parseInt(in.nextLine());

res = maxXor(_l, _r);
System.out.println(res);

}
}

Utopian tree

The Utopian tree goes through 2 cycles of growth every year. The first growth cycle occurs during the spring, when it doubles in height. The second growth cycle occurs during the summer, when its height increases by 1 meter. Now, a new Utopian tree sapling is planted at the onset of the spring. Its height is 1 meter. Can you find the height of the tree after N growth cycles?

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int T,g=1,i;
T=sc.nextInt();
if(T>0&&T<=10)
{
int n;
for(i=0;i<T;i++)
{
n=sc.nextInt();
if(n>=0&&n<=60)
{

for(int j=0;j<n;j++)
{
if(j%2==0)
g=2*g;
else
g=g+1;

}
System.out.println(g);
g=1;
}
}
}
}
}

Given a string containing characters A and B only, he wants to change it into a string he likes.

Strings in which consecutive characters are different. For example, he likes ABABA, while he doesn’t like ABAA. Given a string containing characters A and B only, he wants to change it into a string he likes. To do this, he is allowed to delete the characters in the string. Your task is to find the minimum number of required deletions.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int T;
String str;
int d=0;
T=sc.nextInt();

if(T<=10&&T>0)
{

for(int k=0;k<T;k++){
str=sc.next();

for(int i=0;i<(str.length()-1);)
{
char ch=str.charAt(i);

int j=i+1;
while((j<str.length())&&str.charAt(j)==ch)
{

d++;
j++;
}
i=j;
}
System.out.println(d);
d=0;
}
}

}
}

Generate all prime numbers between two given numbers!

#include <stdio.h>
#include <math.h>
int main(void) {
unsigned long long int m,n,i,j,sq;
int T,k;
scanf(“%d”,&T);
if(T>0&&T<=10)
{
for(k=0;k<T;k++)
{
scanf(“%llu”,&m);
scanf(“%llu”,&n);
if(n>0&&n<=1000000000LL)
{
if(m>0&&m<=n)
{
if((n-m)<=1000000LL)
{
for(i=m;i<=n;i++)
{
if(i==1)
continue;
else if(i==2||i==3)
{
printf(“%llu\n”,i);
}
else
{
//printf(“%llu “,i);

if(i%2!=0LL)
{
sq=(long long int)sqrt(i);
for(j=2LL;j<=sq;j++)
{
if(i%j==0LL)
{
break;
}
}
if(j>sq)
printf(“%llu\n”,i);
}
}
}
printf(“\n”);
}
}
}
}
}
return 0;
}

Maximum Value of Unsigned DataType of C Program.

  • #include <stdio.h>
  • #include<math.h>
  • int main(void) {
  • unsigned long long int u;
  • unsigned long int k;
  • unsigned int j;
  • k=pow(2,32)1;
  • j=pow(2,16)1;
  • u=pow(2,64)1;
  • printf(“%llu %lu %u”,u,k,j);
  • return 0;
  • }

Find the Number of Zero in Factorial upto 100.

#include
#define MAXT 100000

main()
{
     unsigned long int T,i;
     unsigned long long int N[MAXT],div,totZero;
     totZero=0;
     div=5;
     scanf("%lu",&T);
if(T>0&&T<MAXT)
{
  for(i=0;i<T;i++)
     scanf("%llu",&N[i]);
for(i=0;i0&&N[i]<1000000000)
   {
      while(div<=N[i])
     {
        totZero=totZero+N[i]/div;
        div*=5;
   }
       printf("\n%llu",totZero);
       totZero=0;
      div=5;
   }
  }
}
return 0;
}