Turing's Challenge
Time limit1sMemory limit1024 MB
Given X and N, pick indices i from 1 to N+1 so the product of the terms C(N, i-1) X^(i-1) is 2 mod 4, and maximize the index sum.
- Level
Medium7 of 10
- Topics
- Number theory, Bit manipulation, Greedy
- Solved
- No attempts yet
Problem
Knuth was reading through some of Turing's memoirs and found an interesting challenge that Turing had left for one of his successors. Knuth asked you, his best student, to write a program that solves it, but he plans to take the credit for the work. Co-authoring a paper with Knuth is to computer scientists what co-authoring a paper with Erdos is to mathematicians, so you accept the task. Help Knuth solve Turing's problem.
Given positive integers and , define the set as follows: , where .
The goal is to pick a set with such that , and the sum of the elements of is as large as possible. In other words, you choose terms from the binomial expansion of so that their product leaves remainder 2 when divided by 4, and the sum of their indices is maximal.
Find that maximal sum.
Input
The first line contains a positive integer (), the number of queries. Each of the next lines contains two space-separated integers and (, ).
Output
For each query, print the maximal sum of indices on its own line. If no such subset exists, print .