Megavirus
Time limit1sMemory limit128 MB
Given k and n viruses from generation k in a binary tree, find the deepest generation whose node is an ancestor of all given viruses.
- Level
Medium4 of 10
- Topics
- Tree, Bit manipulation, Math, Brute force
- Solved
- No attempts yet
Problem
A hacker built a megavirus. Every copy of the virus has its own number, and the very first copy is numbered . Every minute a new generation of viruses is created. A virus numbered in generation produces two children in generation , numbered and . These two newly created viruses and are called the children of virus from generation . Virus together with its children, its children's children, and so on are called the descendants of (virus is also counted among its own descendants), and is called their ancestor; in particular, every virus is an ancestor of itself. Generation numbers start from . The viruses that live in each generation are:
- generation : virus
- generation : viruses ,
- generation : viruses , , ,
- generation : viruses , , , , , , ,
- ...
Given one generation number and the numbers of several viruses from that generation, compute the largest generation number that contains a common ancestor of all the given viruses, and print it.
Input
The first line contains two integers and separated by a space. () is the generation number, and () is the number of viruses to read. Each of the next lines contains the number of one virus. Since every virus belongs to generation , each number is an integer between and inclusive.
Output
Print, on a single line, the largest generation number that contains a common ancestor of all the viruses.