Sei sulla pagina 1di 61

Fibonacci number index

You are given an integer from the Fibonacci sequence. Print index of given
Fibonacci number.

Input
The only line of input contains an integer n.

Output
Print index of given Fibonacci number.

Constraints
2 <= n <= 108

Example#1
Input
2

Output
3

Explanation: 2 is the third Fibonacci number: 1, 1, 2.

Example#2
Input
13

Output

Explanation: 13 is the 7th Fibonacci number: 1, 1, 2, 3, 5, 8, 13.


Unique pairs
You are given n integer pairs. Print the number of unique pairs.

Input
The first line of input contains integer n, the number of pairs.
Each of next k lines of input contains two space separated integers ai and b
i. (ai; b i) is the ith pair.

Output
Print the number of unique pairs.

Constraints
1 <= n <= 50
1 <= ai , bi <= 100

Example#1
Input
3
1 2
2 3
1 2

Output
2

Explanation: There are two unique pairs: (1; 2), (2; 3).

Example#2
Input
4
1 2
2 3
2 3
1 2
Output

Explanation: There are two unique pairs: (1; 2), (2; 3).

Unique pairs
You are given n integer pairs. Print the number of unique pairs.

Input
The first line of input contains integer n, the number of pairs.
Each of next k lines of input contains two space separated integers ai and b
i. (ai; b i) is the ith pair.

Output
Print the number of unique pairs.

Constraints
1 <= n <= 50
1 <= ai , bi <= 100

Example#1
Input
3
1 2
2 3
1 2
Output
2

Explanation: There are two unique pairs: (1; 2), (2; 3).

Example#2
Input
4
1 2
2 3
2 3
1 2

Output

Explanation: There are two unique pairs: (1; 2), (2; 3).

Joyful Number
A positive number is called joyful if it can be written as the sum of two or more
consecutive positive integers. Your job is to find the n th joyful number.

Input
The only line of input contains an integer n.

Output
Print the nth joyful number.

Constraints
0 <= n <= 106

Example#1
Input
2

Output
6

Explanation: 0) 3 = 1 + 2
1) 5 = 2 + 3
2) 6 = 1 + 2 + 3

Example#2
Input
3

Output

Explanation: 0) 3 = 1 + 2
1) 5 = 2 + 3
2) 6 = 1 + 2 + 3
3) 7 = 3 + 4

Large integer divisibility


You are given an integer as a string. Print "YES" (without quotes) if n is divisible
by 3, print "NO" (without quotes) otherwise.

Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.
Output
Print "YES" (without quotes) if n is divisible by 3, print "NO" (without quotes)
otherwise.

Constraints
1 <= n <= 100

Example#1
Input
4
3333

Output
YES

Explanation: 3333 / 3 = 1111(0)

Example#2
Input
4
1234

Output

NO

Explanation: 1234 is not divisible by 3.

Starts and ends with '0'


You are given a binary string, it contains only '0's and '1's. Count the number
of substrings that start and end with '0'.
Input
The first line of input contains an integer n, the length of the given string.
The second line of input contains a string.

Output
Print the number of substrings that start and end with '0'.

Constraints
1 <= n <= 100

Example#1
Input
4
0101

Output
3

Explanation: There are three substrings that start and end with '0': "0", "010".

Example#2
Input
4
1011

Output

Explanation: There is one substring that starts and ends with '0': "0".

Prime numbers
You are given a positive integer n. You must find the largest possible prime
number p, which satisfies the following:

 p = 2k - 1
 p<n

Input
The only line of input contains an integer n.

Output
Print p. If there is no such p, print -1.

Constraints
1 <= n <= 108

Example#1
Input
10

Output
7

Explanation: 7 is a prime number. 7 = 23 - 1.

Example#2
Input
40

Output
31

Explanation: 31 is a prime number. 31 = 25 - 1.


GCD in range
You are given four integers: n, k, a and b. find the greatest common divisor
(GCD) of n and k which is in range [a; b]. If there is no common divisor in
range [a; b], print 0.

Input
The only line of input contains four space separated integers: n, k, a and b.

Output
Print the greatest common divisor (GCD) of n and k which is in range [a; b].

Constraints
1 <= n <= 10000
1 <= k <= 10000
1 <= a <= 10000
1 <= b <= 10000

Example#1
Input
12 4 1 3

Output
2

Explanation: GCD(12, 4) in range [1; 3] is 2.

Example#2
Input
14 28 1 2

Output

Explanation: GCD(14, 28) in range [1; 2] is 2.


Check powers
You are given two integers n and k. Print 1 if nk > kn, print -1 otherwise.

Input
The only line of input contains two space separated integers n and k.

Output
Print 1 if nk > kn, print -1 otherwise.

Constraints
1 <= n <= 105
1 <= k <= 105

Example#1
Input
3 4

Output
1

Explanation: 34 = 81, 43 = 64, 81 > 64.

Example#2
Input
1 2

Output

-1

Explanation: 12 = 1, 21 = 2, 1 < 2.
Factorial digits
You are given a positive integer n. You must find the number of digits in
factorial of n.

Input
The only line of input contains an integer n.

Output
Print the number of digits in factorial of n.

Constraints
1 <= n <= 105

Example#1
Input
5

Output
3

Explanation: 5! = 1 * 2 * 3 * 4 * 5 = 120. Number of digits in 120 is 3.

Example#2
Input
4

Output

Explanation: 4! = 1 * 2 * 3 * 4 = 24. Number of digits in 24 is 2.


Pernicious number
In number theory, a pernicious number is a positive integer where the digit sum of
its binary representation is prime.
You are given an integer n. Print "YES" (without quotes) if the given number is
pernicious number, print "NO" (without quotes) otherwise.

Input
The only line of input contains an integer n.

Output
Print "YES" (without quotes) if the given number is pernicious number, print "NO"
(without quotes) otherwise.

Constraints
1 <= n <= 28

Example#1
Input
3

Output
YES

Explanation: 3 is pernicious number. Binary representation of 3:


00000000000000000000000000000011. As you can see digit sum is 2, 2 is prime
number.

Example#2
Input
4

Output

NO
Explanation: 4 is not pernicious number. Binary representation of 4:
00000000000000000000000000000100. As you can see digit sum is 1, 1 is not
prime number.

Geometric Progression
You are given first term (a) and common ratio (r) of Geometric Progression.
Find nth term of the given Geometric Progression.

Input
The only line of input contains three space separated integers a, r and n.

Output
Print nth term of the given Geometric Progression.

Constraints
1 <= a, r, n <= 1000
It is guaranteed that result will not be greater than 108.

Example#1
Input
2 3 4

Output
54

Explanation:
The first term: 2
The second term: 2 * 3 = 6
The third term: 2 * 3 * 3 = 18
The fourth term: 2 * 3 * 3 * 3 = 54
Example#2
Input
5 5 2

Output

25

Explanation:
The first term: 5
The second term: 5 * 5 = 25

Factorial zeroes
You are given an integer n. Print the number of trailing zeroes in factorial of
n.

Input
The only line of input contains an integer n.

Output
Print the number of trailing zeroes in factorial of n.

Constraints
1 <= n <= 108

Example#1
Input
5
Output
1

Explanation: 5! = 1 * 2 * 3 * 4 * 5 = 120. 120 has 1 trailing zero.

Example#2
Input
10

Output

Explanation: 10! = 3628800. 3628800 has 2 trailing zeroes.

LCM
You are given an array consisting of n integers. Print LCM (Least Common
Multiple) of the given array.

Input
The first line of input contains an integer n, the number of elements in the
given array.
The second line of input contains n space separated integers, elements of
the given array.

Output
Print LCM (Largest Common Multiple) of the given array.

Constraints
1 <= n <= 10
1 <= arr[i] <= 100. arr[i] is the ith element of the given array.

Example#1
Input
3
1 2 3

Output
6

Explanation: LCM of the given array is 6. 6 = 0 mod 1, 6 = 0 mod 2, 6 = 0


mod 3.

Example#2
Input
3
2 4 8

Output

Explanation: LCM of the given array is 8. 8 = 0 mod 2, 8 = 0 mod 4, 8 = 0


mod 8.

Prime Number
For a given number N check if it is prime or not. A prime number is a number which
is only divisible by 1 and itself.

Input
First line contains an positive integer N.

Output
For each testcase, in a new line, print "Yes" if it is a prime number else print "No".

Constraints
1 <= N <= 100

Example#1
Input
5

Output
Yes

Product under modulo p


You are Given an array consisting of n positive integers. Find the product in
range [l, r] under modulo p, where p is a prime number.

Input
The firs line of input contains 4 integers: n, l, r and p, where n is the number
of elements in the given array, l and r - range, p - prime number.
The second line of input contains n space separated integers, elements of
the given array.

Output
Print the product in range [l, r] under modulo p, where p is a prime number.

Constraints
1 <= n <= 106
1 <= l <= r < n
1 <= p <= 108

Example#1
Input
4 1 2 5
2 5 6 9
Output
0

Explanation: arr(1, 2) = {5, 6}. 5 * 6 = 30. 30 % 5 = 0.

Example#2
Input
4 1 3 13
2 5 6 9

Output

10

Explanation: arr(1, 3) = {5, 6, 9}. 5 * 6 * 9 = 270. 270 % 13 = 10.

Digits GCD
You are given an integer n. Print GCD (Greatest Common Divisor) of its
digits.

Input
The only line of input contains an integer n.

Output
Print GCD (Greatest Common Divisor) of digits of n.

Constraints
1 <= n <= 108

Example#1
Input
34

Output
1

Explanation: GCD(3, 4) = 1

Example#2
Input
68

Output

Explanation: GCD(6, 8) = 2

A ladder
John is standing at the bottom of the ladder and wants to reach the top. There
are n stairs and in one move he can climb either one stair or two stairs at a
time. Print the number of ways, John can reach the top.

Input
The only line of input contains an integer n, the number of stairs.

Output
Print the number of ways, John can reach the top.

Constraints
1 <= n <= 30

Example#1
Input
3

Output
3

Explanation: If there are 3 stairs, there are 3 ways of reaching the top:
1) 0 -> 1 -> 2 -> 3
2) 0 -> 1 -> 3
3) 0 -> 2 -> 3

Example#2
Input
2

Output

Explanation: If there are 2 stairs, there are 2 ways of reaching the top:
1) 0 -> 1 -> 2
2) 0 -> 2

Last digits
You are given three positive integers n, a and b. Print last n digits of a b.

Input
The only line of input contains three space separated integers n, a and b.

Output
Print last n digits of ab.

Constraints
1 <= a, b < 20
1 <= n <= length(ab)

Example#1
Input
1 3 3

Output
7

Explanation: 33 = 3 * 3 * 3 = 27. The last one digit of 27 is 7.

Example#2
Input
2 4 2

Output

16

Explanation: 42 = 4 * 4 =16. The last two digits of 16 is 16.

Sexy prime
In mathematics, sexy primes are prime numbers that differ from each other
by six.
You are given a range of the form [l, r]. Print the number of all the sexy prime
pairs in the given range.

Input
The only line of input contains two space separated integers l and r.

Output
Print the number of all the sexy prime pairs in the given range.

Constraints
1 <= l <= r <= 1000

Example#1
Input
5 12

Output
1

Explanation: There is only one sexy prime pair in range [5; 12] (5; 11). 5 is
prime, 11 is prime, 5 + 6 = 11.

Example#2
Input
14 22

Output

Explanation: There are no sexy prime pairs in the given range.


Check Arithmetic Progression
You are given three non-negative integers a1, d and k. a1 is the first element
of an Arithmetic Progression and d is the difference of the Arithmetic
Progression. Print "YES" (without quotes) if k is a part of the given Arithmetic
Progression, print "NO" (without quotes) otherwise.

Input
The only line of input contains three space separated integers a1, d and k.

Output
Print "YES" (without quotes) if k is part of given Arithmetic Progression, print
"NO" (without quotes) otherwise.

Constraints
1 <= a1 <= 1000
1 <= d <= 100

Example#1
Input
1 2 5

Output
YES

Explanation: In this case the first element is 1 and d is 2. 1 + 2 + 2 = 5.

Example#2
Input
1 2 6

Output

NO
Explanation: In this case the first element is 1 and d is 2. Thus 6 is not a part
of the given Arithmetic Progression

Matrix Path Sum


For a given matrix with positive integers and dimensions of N*M, find the
maximum path sum in matrix.
The maximum path is sum of elements from first row to last row.
Consider that you are able to move only down or diagonally.

Input
The first line contains two space separated integers N and M
Each of the next N lines contains M space separated integers

Output
Single integer as sum of path members

Constraints
(1 <= N,M <= 100)

Example#1
Input
4 4
1 1 100 1
1 100 1 1
1 1 100 1
1 1 1 100

Output
400

Explanation: 100 + 100 + 100 + 100 = 400

Example#2
Input
4 4
1 1 1 100
100 1 1 1
1 1 1 100
100 1 1 1

Output

202

Explanation: We can only move from 100 to 1 to 100 to 1 = 202

Spiral Matrix
Given a 2D array, print it in spiral form starting from very first element.
For example if array is :
1 2 3

456 Spiraled form will be 1 -> 2 -> 3 -> 6 -> 9 -> 8 -> 7 -> 4 -> 5
7 8 9

Input
The first line contains two space separated integers N and M
Each of the next N lines contains M space separated integers

Output
Spiraled 2D array

Constraints
(1 <= N,M <= 100)

Example#1
Input
4 4
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4

Output
1 2 3 4 4 4 4 3 2 1 1 1 2 3 3 2

Explanation: Same technique as in above example

Example#2
Input
1 3
1 2 3

Output

1 2 3

Explanation: While we have 1 x 3 matrix, spiral will be first and only row

Row with Maximum number of 1s in Matrix


For a given 2D array , where each row is sorted. Find the row with max
number of 1s.
Consider that the elements of matrix are only 0s and 1s.

Input
The first line contains two space separated integers N and M
Each of the next N lines contains M space separated integers

Output
Index of row with maximum number of 1s.

Constraints
(1 <= N,M <= 100)

Example#1
Input
3 4
0 0 1 1
0 0 0 1
0 0 0 1

Output
0

Explanation: Row with index 0 has maximum number of 1s.

Example#2
Input
2 2
0 1
1 1

Output

Explanation: Row with index 1 has maximum number of 1s.

Totient Function
Totient function for an input K is count of numbers in range (1, K) that are
relatively prime to K i.e the numbers whose GCD which K is 1, you can read
more about the function on wikipedia

Input
The first and only line contains single integer K

Output
Totient number t(K)

Constraints
( 1 <= K <= 1010)

Example#1
Input
6

Output
2

Explanation:
The formula basically says that the value of t(K) is equal to K multiplied by
product of (1 – 1/p) for all prime factors p of K. For example value of t(6) = 6
* (1-1/2) * (1 – 1/3) = 2.

Example#2
Input
10

Output

Explanation: 10 * (1-1/2) * (1-1/5) = 4

Two Array Merge


For a given two array of size N and M, find number of ways we can merge
the given arrays such that order of members of each one doesn't change.
Input
The first line contains single integer N
The second line contains single integer M

Output
Number of ways we can merge the given arrays

Constraints
( 1 <= N, M <= 104)

Example#1
Input
2
2

Output
6

Explanation:
Consider that first array is {1, 2} and second is {3, 4}, possible merges will
be:
1. {1, 2, 3, 4}
2. {1, 3, 2, 4}
3. {3, 4, 1, 2}
4. {3, 1, 4, 2}
5. {1, 3, 4, 2}
6. {3, 1, 2, 4}

Example#2
Input
1
2
Output

Explanation:
Consider that first array is {1} and second is {2,3}, possible merges will be:
1. {1, 2, 3}
2. {2, 3, 1}
3. {2, 1, 3}

Product Triplets
For a given array, find how many triples of indices (i < j < k), such that arr[i]
* arr[j] * arr[k] is minimum

Input
The first line contains single integer N
The second line contains N space separated integers

Output
Number of minimum triplet products

Constraints
(3 <= N <= 106)

Example#1
Input
6
3 3 1 2 7 9

Output
2

Explanation: There are two possible triplets with minimum product


1. 1, 2, 3
2. 1, 3, 2

Example#2
Input
4
2 2 2 2

Output

Explanation: There are four possible triplets with minimum product


1. 2, 2, 2 (with indexes 0,1,2)
1. 2, 2, 2 (with indexes 0,1,3)
1. 2, 2, 2 (with indexes 0,2,3)
1. 2, 2, 2 (with indexes 1,2,3)

Stick Cutting
For a given stick of size N, find the number of ways it can be cut into K pieces

Input
The first line contains single integer N
The second line contains single integer K

Output
Number of ways stick can be cut into K pieces.

Constraints
(1 <= N <= 105)

Example#1
Input
4
2

Output
3

Explanation: Pieces will be :


1. - | - - - {1, 3}
2. - - | - - {2, 2}
3. - - - | - {3, 1}

Example#2
Input
3
2

Output

Explanation: Pieces will be :


1. - | - - {1, 2}
2. - - | - {2, 1}

Binomial Expansion Series


For a given integers A, X, and N, print terms of binomial expression series.

(A+X)n = nC0AnX0 + nC1An-1X1 + nC2An-2X2 +….+ nCnA0Xn

Input
The first line contains single integer A
The second line contains single integer X
The third line contains single integer N
Output
N space separated integer as series of binomial expression

Constraints
(1 <= A,X <= 104)
(1 <= N <= 10)

Example#1
Input

2 2 4

Output

16 64 96 64 16

Explanation: (A+X)n = nC0AnX0 + nC1An-1X1 + nC2An-2X2 +….+ nCnA0Xn

Example#2
Input

1
1
5

Output

1 5 10 10 5 1

Explanation:

Narayana
For a given integers N and K, find Narayana number which is given by :
In combinatorics, the Narayana numbers N(n, k), n = 1, 2, 3 ..., 1 ≤ k ≤ n,
form a triangular array of natural numbers, called Narayana triangle, that
occur in various counting problems. N(n, k) formula is based on the
expression:

Input
The first line contains single integer N
The second line contains single integer K

Output
Narayana number - N(n, k)

Constraints
Value of N(N, K)

Example#1
Input
5
3

Output
20

Explanation: See above formula

Example#2
Input
6
6

Output
1

Explanation: See above formula

Straight roads
You are working for a company which constructs roads. At the moment you are
given a task: There are n cities. You must determine whether it is possible to
construct two roads (not necessarily distinct) and satisfy the following:

 If we consider roads as lines on Cartesian plane – Each of them must be straight


line.
 If we consider cities as points on Cartesian plane, every point lies on at least
one of the lines.

Assume that each point is distinct and coordinates of each them are integers.

Input
The first line of input contains integer n.
Next n lines contains two integers xi and yi – coordinates of i point.
th

Output
Print “YES” (Without quotes) if it is possible construct roads, “NO” (Without
quotes) otherwise.

Constraints
0 < n <= 100000.
-100000000 <=xi <= 100000000
-100000000 <=yi <= 100000000

Example #1
Input
5
2 9
-1 -4
-3 -8
-4 -8
7 2

Output
NO

There is no way to draw straight lines and satisfy above rules.

Example #2
Input
4
1 1
2 2
3 4
2 1

Output
YES
We can draw 2 straight lines such that every point lies on them.

Add two numbers without using arithmetic operations


Problem text...

Input
Input description...

Output
Output description...

Constraints
Constraints details...

Example#1
Input
Input data...

Output
Output data...

Explanation:

Example#2
Input
Input data...

Output

Output data...

Explanation:

Check if a number is pentagonal


Problem text...

Input
Input description...

Output
Output description...

Constraints
Constraints details...

Example#1
Input
Input data...

Output
Output data...

Explanation:

Example#2
Input
Input data...

Output

Output data...

Explanation:

Count Derangements
You are given N balls numbered from 1 to N and there are N baskets
numbered from 1 to N in front of you, the ‘i’th basket is meant for the ‘i’th ball.
The task is to calculate the number of ways in which none of the balls goes
into their respective basket.

Input
Each test case consist of a single line containing an integer N.

Output
Corresponding to each test case, print the desired output in a new line. Since
the answer can be large, print it modulo 109 + 7.
Constraints
1 ≤ N ≤ 105

Example#1
Input
2

Output
1

Explanation: Ball 1 will go in basket 2 , ball 2 will go into basket 1. So answer


is 1.

Example#2
Input
3

Output

Explanation: There are two ways in which no ball goes into respective
basket.
1. Ball 1 will go in basket 2 , ball 2 will go in basket 3 and ball 3 will go into
basket 1.
2. Ball 1 will go in basket 3 , ball 2 will go in basket 1 and ball 3 will go into
basket 2. So answer is 2.

Lucky sequence
John has the integer n and wants to generate lucky arrays with the length n. He thinks
that an array is lucky if it contains only integers from 1 to n and meets one of the
two conditions:
1) Each element, starting from the second one, is no more than the preceding one.
2) Each element, starting from the second one, is no less than the preceding one.
John started generating lucky arrays with the size n. How many different arrays can
be generated.

Input
The only line of input contains an integer n which is the size of the array.

Output
Print the answer modulo 1000000007.

Constraints
1 ≤n≤ 100000

Example#1
Input
1

Output
1

Just one way: 1

Example#2
Input
2

Output

Four ways: 12, 21, 11, 22

Perfect subset
We call a subset Perfect, if the product of all of its elements is the square of
some positive integer. For example, {2, 2} is a perfect subset, because 2 x 2
= 4. You are given an array of n integers. Your task is to find the number of
different ways to select a perfect subset.

Input
The first line of input contains integer n, number of elements in the array.
The second line contains n space separated integers (k1, k2, ..., kn).

Output
Print one integer — the number of different ways to choose some elements
so that their product is a square of a certain integer modulo 109 + 7.

Constraints
1 <= n <= 100000.
1 <= ki <= 50

Example #1
Input:
1
64

Output:
1

Just one way.

Example #2
Input:
2
70 70

Output:

1
Just one way. 70 x 70

The Silver Permutations


Lets define two types of permutations:
1) The Golden Permutation p of size n is an array such that every integer
from 1 to n occurs exactly once in this array.
2) Let's call a permutation The Silver Permutation if there exist at
least n - k indices such that pi = i (1 ≤ i ≤ n).
Your task is to count the number of The Silver Permutations for given
numbers n and k.

Input
The first line contains two integers n and k.

Output
Print the number of The Silver Permutations for given n and k.

Constraints
4 ≤n≤ 1000.
1 ≤k≤ 4.

Example #1
Input:
4 2

Output:
7

Example #2
Input:
400 1
Output:

There is only one permutation.

Damaged indicators
John is a famous inventor. Robot JR004 is one of his inventions. JR004 has
a sequence of n indicators for communication. Unfortunately some indicators
of JR004 are damaged at the moment and need to be repaired. JR004 can
self-repair. At each step it can repair a damaged indicator if there is at least
one adjacent indicator which is not damaged.
John wants to determine how many different ways are there, to repair all
damaged indicators. He is not good at programming and needs your help.
Write a program which takes two integers – n and m, and an array of integers
- arr. n is the number of indicators. m is the number of indicators which
are not damaged. arr is the array of integers containing indexes of indicators
which are not damaged. Your program must return the number of different
possible ways to repair all the indicators modulo 1000000007.

Input
The first line of input contains two integers n and m. n - number of indicators.
m – number of indicators which are not damaged.
The second line of input contains m space separated integers k 1, k2, …, kn –
indexes of indicators which are not damaged.

Output
Print the number of different possible ways to repair all the indicators modulo
1000000007.

Constraints
0 < n < 1000.
0 < m <= n.
0 < ki <= n
Example #1
Input
4 4
1 2 3 4

Output
1

None of them are damaged. No need of repair. Just 1 way.

Example #2
Input
5 3
1 2 5

Output
2

Way 1: 3 4
Way 2: 4 3

Euler's Totient function


In number theory , Euler's Totient function counts the positive integers up to a
given integer n that are relatively prime to n - the number of integers k in the range
1 ≤ k ≤ n for which the greatest common divisor GCD(n, k) is equal to 1.
You are given a positive integer n, compute Euler’s Totient function for n.

Input
The only line of input contains an integer n.

Output
Print the value returned by Euler’s Totient function for n.

Constraints
1 <= n <= 100000

Example#1
Input
3

Output
2

Explanation: There are two positive integers (1 <= k <= 3) that are relatively prime
to 3: GCD(1, 3) = 1, GCD(2, 3) = 1.

Example#2
Input
8

Output

Explanation: There are four positive integers (1 <= k <= 8) that are relatively prime
to 3: GCD(1, 8) = 1, GCD(3, 8) = 1, GCD(5, 8) = 1, GCD(7, 8) = 1,

The best friend


John found another Greek mathematician’s manuscript. These Greek
mathematicians believed that every positive integer n has the best friend k
(positive integer). To determine whether k is the best friend of n we can
use golden operations.
Let’s assume that we have pair of numbers x and y. we can get new pair of
numbers using two types of golden operations:
(x, y) -> (x + y, y)
(x, y) -> (x, x + y)
If the initial pair is (1, 1), for every positive integer n, there is positive integer
k (the best friend of n), which is the least number of golden operations
needed to transform (1,1) into the pair where at least one number equals n.
Your task is to help John, who wants to write a program which takes n as an
argument and calculates k (the best friend of n).
Input
The only line of input contains positive integer n.

Output
Print the integer k (the best friend of n)

Constraints
1 <= n <= 1000000

Example #1
Input:
1

Output:
0

We do not need to make any operations, because (1, 1) is an initial pair and
n is 1

Example #2
Input:
2

Output:
1

(1, 1) -> (1, 2)

Degree of Fibonacci
John loves mathematics very much. Recently he learnt about the Degree of
Fibonacci. If we are given a sequence of integers, we call the Degree of
Fibonacci a longest prefix of the sequence, whose members satisfy the
following: ai = ai - 1 + ai - 2, where i >= 3 (We start counting from 1).
At the moment John has an array consisting of n integers. He wants to
rearrange the elements of the array in such way that the Degree of Fibonacci
is maximal. He wants to write a program which takes integer n and array of
n integers as arguments and calculates the maximum possible Degree of
Fibonacci for the sequence which is generated by the elements of the given
array. He is not good at programming and needs your help!

Input
The first line of input contains integer n.
The second line of input contains n space separated integers.

Output
Print the maximum possible Degree of Fibonacci for the sequence generated
by the given numbers.

Constraints
2 <= n <= 1000
-1000000 <= arr[i] <= 1000000. arr[i] is the element of the array.

Example #1
Input
4
1 2 3 1

Output
4

We can create the following sequence: 1 1 2 3. 1 + 1 = 2. 1 + 2 = 3.

Example #2
Input
4
1 -10 1 2

Output
3

We can create the following sequence: 1 1 2 -10. 1 + 1 = 2.

Greedy calculator
John is a famous inventor. Recently he invented a calculator. This calculator
is capable of just adding two numbers and is "greedy": If we try to sum up
two numbers with this calculator, it will choose one of such base k positional
notation, that the number of symbols in a result will be maximal. For example:
If the numbers are 9 and 7, it will choose base 10 positional notation and the
length of the result will be 2 (910 + 710 = 1610). Or if the numbers are 11 and
11, it will choose base 2 positional notation and the length of a result will be
3 (112 + 112 = 1102). Maximal number of symbols in a result for these
examples are 2 and 3.
Sarah wants to use John's calculator, but before she wants to know the
length of a result. Help Sarah, write a program, which takes two integers m
and n as arguments, and prints the length of the result returned by the greedy
calculator.

Input
The only line of input contains two space separated integers, n and m.

Output
Print the length of the result returned by greedy calculator.

Constraints
The only line of input contains two space separated integers - n and m.

Example #1
Input
84 89
Output
3

8410 + 8910 = 17310


Maybe, It is not necessary to chose exactly base 10 numeral system and
maybe there are other numeral systems where the number of symbols in a
result is 3, but the maximal number of symbols in a result can not be more
than 3.
Example #2
Input
1 2

Output
2

13 + 23 = 103.
Maybe, It is not necessary to chose exactly base 3 numeral system and
maybe there are other numeral systems where the number of symbols in a
result is 3, but the maximal number of symbols in a result can not be more
than 2.

Squareland
Squareland is a country which has a shape of a n x n square. Each (i, j) cell
of the Squareland may be a city or a village.
Write a program which takes a description of the Squareland and determines
if it is possible to walk from any city to another city using a path of side-
adjacent cities changing the direction at most once during the path.

Input
The first line of input contains integer n. n x n is the size of the Squareland.
Each of next n lines contains n space separated characters 'C' or 'V'.
Character 'C' denotes a city and character 'V' denotes village.

Output
Print "YES" (without quotes) if it is possible to walk from any city to another
city using a path of side-adjacent cities changing the direction at most once
during the path, "NO" (without quotes) otherwise.

Constraints
1 <= n <= 50.
It is guaranteed, that there is at least one city in the Squareland.

Example#1
Input
4
C C C C
C C C C
V C V V
V V V V

Output
YES

Explanation: We are able to walk from any city to another city using a path
of side-adjacent cities changing the direction at most once during the path.

Example#2
Input
4
C C C C
C V C C
V V V C
V V V V

Output

NO

Explanation: It is not possible to walk from any city to another city using a
path of side-adjacent cities changing the direction at most once during the
path. For example, we need to change the direction from (1, 2) to (3, 2) twice.
We must change the direction at (1, 1) and at (3, 1).
C(1,1) C(2,1) C(3,1) C(4,1)
C(1,2) V(2,2) C(3,2) C(4,2)
V(1,3) V(2,3) V(3,3) C(4,3)
V(1,4) V(2,4) V(3,4) V(4,4)

Number of subsets and mean


Let Max be the maximum possible mean of a multiset of values obtained
from an array.
Let Min be the minimum possible mean of a multiset of values obtained from
the same array. Note that in a multiset values may repeat.
The task is to find the number of multisets with mean as Max and the
number of multisets with mean as Min. The answer may be too large so
output the number of multisets modulo 109+7.

Input
The first line of each test case contains a positive integer N which denotes
the size of the array. In the next line are N space separated positive integers
which are the value of array A.

Output
Print two space separated integers which denote the number of multisets
with mean as Max and the number of multisets with mean as Min. The output
should be done modulo 109+7.

Constraints
1 ≤ N ≤ 1000
1≤A[i] ≤1000

Example#1
Input
5
3 1 2 3 4

Output
1 1

Number of digits in product


You are given two integers n and k. Find the number of digits in the product
of n and k.

Input
The only line of input contains two space separated integers, n and k.

Output
Print the number of digits in the product of n and k.

Constraints
1 <= n, k <= 108

Example#1
Input
4 5

Output
20

Explanation: 4 * 5 = 20. There are two digits in 20 ('2' and '0').

Example#2
Input
10 10

Output

Explanation: 10 * 10 = 100. There are three digits in 100 ('1', '0' and '0').

Non square
You are given a positive integer n. Print the nth number which is not a perfect
square among natural numbers (1, 2, 3, … ).

Input
The only line of input contains an integer n.

Output
Print the nth number which is not a perfect square among natural numbers (1,
2, 3, … ).

Constraints
1 <= n <= 108

Example#1
Input
4

Output
6

Explanation: The fourth number which is not a perfect square is 6: 1, 2, 3, 4,


5, 6.
Example#2
Input
8

Output

11

Explanation: The 8th number which is not a perfect square is 11: 1, 2, 3, 4, 5,


6, 7, 8, 9, 10, 11.

isPower
You are given a positive integer n. Print "YES" (without quotes) if n can be
expressed as ab where b > 1 and a > 0. a and b both are positive integers. Otherwise
print "NO" (without quotes).

Input
The only line of input contains an integer n.

Output
Print "YES" (without quotes) if n can be expressed as ab where b > 1 and a > 0. a and
b both are integers. Otherwise print "NO" (without quotes).

Constraints
1 <= n<= 108

Example#1
Input
27

Output
YES
Explanation: 27 = 33

Example#2
Input
10

Output

NO

Explanation: 10 can be expressed as ab.

Minimum number to represent range [1; n]


You are given an integer n. We need to find k integers such that:

 by adding all of them we get n.


 We can represent any integer in range [1; n] as a sum of these numbers.

You must print the minimum value of k.

Input
The only line of input contains an integer n.

Output
Print the minimum value of k.

Constraints
1 <= n <= 108

Example#1
Input
6

Output
3

Explanation: We need only 3 integers (1, 2, 3):


1
2
3
4 = 3 + 1
5 = 3 + 2
6=3+2+1

Example#2
Input
8

Output
4

Explanation: We need only four integers (1, 1, 2, 4)


1
2
3 = 2 + 1
4
5 = 4 + 1
6 = 4 + 2
7 = 4 + 2 + 1
8=4+2+1+1

n as sum of 1, 3, 4
You are given an integer n. Print the number of ways to express n as sum of
1, 3 and 4.

Input
The only line of input contains a single integer.

Output
Print the number of ways to express n as sum of 1, 3 and 4.

Constraints
4 <= n <= 35

Example#1
Input
5

Output
6

Explanation: There are six ways:


5 = 1 + 1 + 3
5 = 1 + 3 + 1
5 = 3 + 1 + 1
5 = 1 + 4
5 = 4 + 1
5 = 1 + 1 + 1 + 1 + 1

Example#2
Input
4

Output

Explanation: There are 4 ways:


4 = 1 + 3
4 = 3 + 1
4 = 1 + 1 + 1 + 1
4=4

Paint stairs
You are given n stairs and 2 colors red, and blue. You should paint given
stairs by given colors (each step by each color).
There is only one constraint: We cannot paint two steps directly after each
other with a red color.
Print the number of ways we can paint n stairs.

Input
The only line of input contains integer n.

Output
Print the number of ways we can paint n stairs.

Constraints
1 <= n <= 35

Example#1
Input
3

Output
5

Explanation: There are 5 ways:


1) red, blue, blue
2) blue, red, blue
3) blue, blue, red
4) blue, blue, blue
5) red, blue, red

Example#2
Input
2

Output
3

Explanation: There are three ways:


1) red, blue
2) blue, red
3) blue, blue

n as sum of odd positive integers


You are given a positive integer n. Print total number of ways to express n
as sum of odd positive integers.

Input
The only line of input contains an integer n.

Output
Print total number of ways to express n as sum of odd positive integers.

Constraints
1 <= n <= 35

Example#1
Input
3

Output
2

Explanation: There are two ways:


1) 3 = 3
2) 3 = 1 + 1 + 1

Example#2
Input
4

Output

Explanation: There are three ways:


1) 4 = 1 + 1 + 1 + 1
2) 4 = 3 + 1
3) 4 = 1 + 3

Potrebbero piacerti anche