Knockout, swiss and other kinds of tournaments

시간 제한0.5초메모리 제한2048 MB

요약
A승 또는 B패에 도달하면 탈락하는 (A, B)-토너먼트에서 모든 라운드의 짝짓기가 가능한 최소 참가자 수를 구한다.
난이도

어려움10점 중 8점

유형
동적 계획법, 조합론, 수학
정답자
아직 제출이 없습니다

문제

Game and sports tournaments are becoming increasingly common, serving as a way to test participants’ skills. The choice of the ideal format depends on the type of competition and the number of participants. For example, a “round-robin” format may be impractical when the number of participants is large, while a knockout tournament (“single-elimination”) can be frustrating when two strong players face each other early. The “Swiss-system” format is a good compromise and is used in several competitions. In this format, if there are NN participants, there will be about ⌈log⁡_2N⌉\left\lceil \log\_2 N \right\rceil rounds, in which players always face opponents with similar scores.

A similar format has been adopted in some games as an alternative to the Swiss system: we will call this format “(A,B)(A, B)-elimination”. In this format, every match has a winner and a loser (i.e., there are no ties), and players always face opponents with the same score. Each participant plays until reaching AA wins or BB losses, whichever comes first. This format has a very convenient property: the number of rounds does not depend on the number of participants. In addition to scalability, this property makes it easier to define the prize structure, since it is possible to predict how many participants will finish with each possible score.

Despite its advantages, this format has a drawback: it is not always feasible, as there may not be enough opponents to complete a round under the given restrictions. For example, if A=2A = 2 and B=2B = 2, and we have N=6N = 6 participants, after the first round there would be 33 players with 11 win and 00 losses. Then, in the second round, since this number is odd, it would not be possible to pair all players with opponents of the same score. On the other hand, if N=8N = 8, the pairing is possible.

You have been asked to determine, given the values of AA and BB, the smallest number of players such that all rounds of the tournament can be completed.

입력

The input consists of a single line containing two integers AA and BB (1≤A,B≤10181 ≤ A, B ≤ 10^{18}) separated by a space, defining the tournament format as described above.

출력

Your program should write a single line containing the smallest possible number of players in the tournament. Since the answer can be very large, print the answer modulo 109+710^9 + 7.

예제2

  1. 예제 1

    입력
    3 3
    
    예상 출력
    16
    
  2. 예제 2

    입력
    3 2
    
    예상 출력
    16