CF226A.Flying Saucer Segments

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

An expedition group flew from planet ACM-1 to Earth in order to study the bipedal species (its representatives don't even have antennas on their heads!).

The flying saucer, on which the brave pioneers set off, consists of three sections. These sections are connected by a chain: the 1-st section is adjacent only to the 2-nd one, the 2-nd one — to the 1-st and the 3-rd ones, the 3-rd one — only to the 2-nd one. The transitions are possible only between the adjacent sections.

The spacecraft team consists of nn aliens. Each of them is given a rank — an integer from 11 to nn . The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section aa to section bb only if it is senior in rank to all aliens who are in the segments aa and bb (besides, the segments aa and bb are of course required to be adjacent). Any alien requires exactly 11 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.

Alien AA is senior in rank to alien BB , if the number indicating rank AA , is more than the corresponding number for BB .

At the moment the whole saucer team is in the 3-rd segment. They all need to move to the 1-st segment. One member of the crew, the alien with the identification number CFR-140, decided to calculate the minimum time (in minutes) they will need to perform this task.

Help CFR-140, figure out the minimum time (in minutes) that all the astronauts will need to move from the 3-rd segment to the 1-st one. Since this number can be rather large, count it modulo mm .

输入格式

The first line contains two space-separated integers: nn and mm (1<=n,m<=109)(1<=n,m<=10^{9}) — the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.

输出格式

Print a single number — the answer to the problem modulo mm .

输入输出样例

  • 输入#1

    1 10
    

    输出#1

    2
    
  • 输入#2

    3 8
    

    输出#2

    2
    

说明/提示

In the first sample the only crew member moves from segment 3 to segment 2, and then from segment 2 to segment 1 without any problems. Thus, the whole moving will take two minutes.

To briefly describe the movements in the second sample we will use value , which would correspond to an alien with rank ii moving from the segment in which it is at the moment, to the segment number jj . Using these values, we will describe the movements between the segments in the second sample: , , , , , , , , , , , , , , , , , , , , , , , , , ; In total: the aliens need 26 moves. The remainder after dividing 2626 by 88 equals 22 , so the answer to this test is 22 .

首页