题干:

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 n aliens. Each of them is given a rank — an integer from 1 to n. The ranks of all astronauts are distinct. The rules established on the Saucer, state that an alien may move from section a to section b only if it is senior in rank to all aliens who are in the segments a and b (besides, the segments a and b are of course required to be adjacent). Any alien requires exactly 1 minute to make a move. Besides, safety regulations require that no more than one alien moved at the same minute along the ship.

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

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 m.

Input

The first line contains two space-separated integers: n and m (1 ≤ n, m ≤ 109) — the number of aliens on the saucer and the number, modulo which you should print the answer, correspondingly.

Output

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

Examples

Input

1 10

Output

2

Input

3 8

Output

2

Note

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 i moving from the segment in which it is at the moment, to the segment number j. 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 26 by 8 equals 2, so the answer to this test is 2.

解题报告:

找规律、、走一遍流程会发现是有递推关系的,再将关系化简一下就是一个3^n次方。。

AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
ll n,m;
ll qpow(ll a,ll k) {ll res = 1;while(k) {if(k&1) res = (res*a)%m;k>>=1;a=(a*a)%m;}return res-1;
}
int main()
{cin>>n>>m;printf("%lld\n",(qpow(3,n)+m)%m);return 0 ;
}

【CodeForces - 227C】Flying Saucer Segments (思维)相关推荐

  1. Codeforces 895 B XK Segments 思维 二分

    题目链接: http://codeforces.com/contest/895/problem/B 题目描述: 给你长度为n的数列a,然后让你找出所有的对(i,j)满足ai≤aj并且[ai,aj]中能 ...

  2. Codeforces 1077B Disturbed People(思维题)

    Codeforces 1077B Disturbed People(思维题) There is a house with nn flats situated on the main street of ...

  3. iText和flying saucer结合生成pdf的技术

    原博文地址 http://blog.csdn.net/shanliangliuxing/article/details/6833471 下面是我自己利用flying saucer技术生成pdf文档的实 ...

  4. flying saucer做导PDF踩过的坑~

    最近公司要求做一个导出PDF报表的功能.由于时间比较紧张,而且导出的内容暂时为一个报表而已,所以我采用了flying saucer+freemaker来做. flying sauce源码:https: ...

  5. Flying Saucer 不支持中文,换行,粗体,CheckBox多选框的解决方案

    最近要生成打印版的保单信息,内容比较多,也比较复杂,iText直接生成的话,想必花很多时间,而且可能也很难维护,偶然看到了HTML 在 Fly Saucer的帮助下能转换成PDF,解析CSS还不错,顿 ...

  6. Flying Saucer生成pdf报表

    使用Flying Saucer生成pdf报表: 1.首先看一下Flying Saucer的官方介绍: https://github.com/flyingsaucerproject/flyingsauc ...

  7. flyingsaucer转换多个html,Flying Saucer实现html转pdf(一些有关问题,持续更新)

    Flying Saucer实现html转pdf(一些问题,持续更新) Flying Saucer实现html转pdf(及一些问题,持续更新) 关于FlyingSaucer的使用官网上有详细的介绍. 官 ...

  8. itext转html为pdf 锚点,flying saucer html转pdf经验分享

    对比了很多生成pdf的方案,最终还是决定使用flying saucer,网上能搜出N多相关文章,但是照着做了会出现各种问题,还是得自己一点点摸索,前后花了两天时间才达到自己的目标,下面把过程中的经验总 ...

  9. flyingsaucer转换多个html,用 flying saucer 生成pdf文件 Css及html的问题

    我用  flying saucer  生成PDF文件,因为要生成文章,而文章是由tinymce发布的.可能有些文章发布出去后格式有问题,比如说标签不规范,有的没有闭合. 这样 flying sauce ...

最新文章

  1. reorder-list——链表、快慢指针、逆转链表、链表合并
  2. 第一章计算机基础知识第一节,第一章 计算机基础知识 第一节
  3. 注解@SuppressWarnings(unused)
  4. 【鸿蒙 HarmonyOS】界面跳转 ( AbilitySlice 之间的界面跳转 | AbilitySlice 之间的值传递 )
  5. backdrop-filter 和filter 写出高斯模糊效果 以及两者区别
  6. pycharm弹出提示信息Server's certificate is not trusted
  7. python中国内源_【文】Python更换国内镜像源
  8. Go的strconv二
  9. SAP HANA Hint简介
  10. 登录centos虚拟机后显示-bash-4.1
  11. 6面向对象的程序设计
  12. Win11任务栏空白怎么办 Win11任务栏空白解决办法
  13. C#学习(6)——LINQ学习
  14. python pandas csv时间聚合_pandas中简单统计分组聚合函数的介绍
  15. PHP 下载远程图片
  16. wordpress入门基本操作,网站安全防护及常用插件(建站必看教程)
  17. Tomcat优化大全,进来看了,真就会了
  18. 利用python转载朋友微信表情包
  19. 基于Html的个人展示网站设计与实现
  20. 【考研数学】微分中值定理计算极限专题

热门文章

  1. 广州.NET俱乐部 VSTS活动报道
  2. 动态规划——莱文斯坦距离
  3. 算术类型转换、整型提升
  4. 字节跳动专家会_字节跳动招聘直播策略运营专家/经理/海外财务AR BP,ACCA优先...
  5. php页面怎么去登录,php中登录后跳转回原来要访问的页面实例
  6. SOCKET bind INADDR_LOOPBACK和INADDR_ANY的区别
  7. 设备I/O之OVERLAPPED
  8. python的read函数_Python Pandas pandas.read_sql函数方法的使用
  9. css 商城 两列_【云控基础】HTML+CSS基础入门
  10. rabbitmq 消费端代码获取队列名称_C#调用RabbitMQ实现消息队列的示例代码