【CodeForces - 227C】Flying Saucer Segments (思维)

题干:

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 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441289.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

清楚linux缓存文件,Linux删除文件 清除缓存

相信很多测试 经常会经历开发叫你清除缓存这种事。那我们要怎么清呢&#xff1f;一、首先&#xff0c;确认你要清除的缓存在哪个目录下&#xff0c;然后切换到该目录下&#xff0c;比如 我现在知道我的的缓存目录是在newerp这个目录下&#xff0c;则如图二、然后 执行命令 rm -…

win10 linux安卓模拟器,genymotion安卓模拟器在Window10中使用的问题

最近一段时间&#xff0c;把系统升级到了win10&#xff0c;然后悲催的事情出现了&#xff0c;genymotion挂了&#xff0c;根本就不能启动&#xff0c;而且还是2.6版本的genymotion&#xff0c;下面我把遇到的问题总结一下&#xff1a;首先&#xff0c;在我的win10系统中&#x…

cross_compile = arm-linux-,cross compile grpc for arm

8种机械键盘轴体对比本人程序员&#xff0c;要买一个写代码的键盘&#xff0c;请问红轴和茶轴怎么选&#xff1f;This post will tell you how to cross compile gPRC static lib for ARM.前段时间尝试交叉编译gRPC遇到了不少的麻烦&#xff0c;写篇post记录一下。gRPCPreparat…

linux键盘映射默认,Linux 中的键盘映射

前面提到&#xff0c;X Window 直接处理了键盘的输入输出端口&#xff0c;因此&#xff0c;在 Linux 虚拟控制台下和 X Window 下使用不同的键盘映射方法。在 Lin对于英语来说&#xff0c;键盘上的字母键直接和英语字母表中的字母对应&#xff0c;但是对于非英语的语种来说&…

C语言用字符串sex储存,2005年计算机等级考试二级C语言全真标准预测试卷(2)

一、选择题(1&#xff5e;40题每题1分&#xff0c;41&#xff5e;50题每题2分&#xff0c;共60分)1.微型计算机的运算器、控制器及内存储器组合在一起&#xff0c;称之为()(本题分值&#xff1a;1分)A.ALUB.CPUC.MPUD.主机【正确答案】D2.下列存储器中&#xff0c;存取速度最快…

c语言全局变量SQR,c语言a/=SQR(k+m);是什么意思?

满意答案NightmareJJ2013.03.16采纳率&#xff1a;47% 等级&#xff1a;12已帮助&#xff1a;11233人先看第一个main() // 主函数{printf("%ld\n", fun(3));} //输出fun(3)这个函数的返回值&#xff0c;3就是下一个//函数中的n 值。long fun (int n) //定义fun函数…

【POJ - 1850】Code (组合数学,字符串另类排序)

题干&#xff1a; Transmitting and memorizing information is a task that requires different coding systems for the best use of the available space. A well known system is that one where a number is associated to a character sequence. It is considered that …

c语言蓝牙接收6,终于搞定了通过两路蓝牙接收数据

一直想做无线传感器&#xff0c;通过蓝牙来接收数据&#xff0c;无奈因为arduino接收串口数据的一些问题&#xff0c;一直搁到现在。因为学校里给学生开了选修课&#xff0c;所以手边有一些nano和mega可以使用&#xff0c;所以就做了用两个nano加上两个蓝牙模块来发射数据&…

【POJ - 1942 】Paths on a Grid (组合数学,求组合数的无数种方法)

题干&#xff1a; Imagine you are attending your math lesson at school. Once again, you are bored because your teacher tells things that you already mastered years ago (this time hes explaining that (ab) 2a 22abb 2). So you decide to waste your time with d…

兔子吃萝卜的c语言编程,狼追兔子的c语言实现

满意答案16guoyuming2013.03.05采纳率&#xff1a;49% 等级&#xff1a;13已帮助&#xff1a;8005人用单链表实现#include #includetypedef struct node{int cave;struct node * next;}node,*LinkList;void main(){int i0,j,count1; // 初始值为1&#xff1b;LinkList L,p,h…

android 动态换肤框架,GitHub - ss520k/Android-Skin-Loader: 一个通过动态加载本地皮肤包进行换肤的皮肤框架...

Android-Skin-Loader更新日志导入到Android Studio&#xff0c;使用gradle构建皮肤包(见7. 皮肤包是什么&#xff1f;如何生成&#xff1f;)(2015-12-02)解决Fragment换肤在某些版本的support-v4包下失效的问题(感谢javake同学)(2015-12-02)对textColor加入selector类型的资源的…

android吸附菜单,Android仿微博、人人Feed详情页吸附导航栏

仿微博、人人的feed详情页面&#xff1a;Listview上下滑动&#xff0c;导航栏view可吸附在顶部的效果。一、实现效果上图&#xff1a;效果图.gif欢迎拍砖&#xff0c;拍拍更进步。没有对比&#xff0c;怎么会有伤害&#xff0c;下面是 微博、人人的Feed详情页&#xff1a;微博、…

android 居右属性,使用layoutDirection属性设置布局靠左或靠右

通过设置layoutDirection属性值为mx.core.LayoutDirection.RTL(右到左)或mx.core.LayoutDirection.LTR(左到右)&#xff0c;使布局为靠左或靠右(如下图)。该属性可设置3种值&#xff0c;LayoutDirection.RTL、LayoutDirection.LTR和null(ILayoutDirectionElement时)/undefined(…

html表格全屏显示,tableView滑动全屏显示

今天要分享的一个效果是在一个页面弹出视图展示一个tableview&#xff0c;然后手指滑动tableview时&#xff0c;视图随着tableview偏移量增加而慢慢增加&#xff0c;到达临界时&#xff0c;全屏显示&#xff0c;然后再次向下滑动时&#xff0c;当偏移量到达临界点&#xff0c;视…

大量html乱码seo,HTTPS改造之后网页错位乱码,影响SEO和正常访问,应该这样改

有一些朋友可能不太知道https改造怎么做&#xff0c;就学着网站的步骤进行&#xff0c;实际操作过程中可能会遇到很多问题。比如说有的会出现网页错位、页面乱码、后台功能无法使用的情况。昨天我们就有一个客户他自己做了https改造&#xff0c;但是造成后台无法上传图片的情况…

微型计算机中最小的单位,微型计算机中最小的数据单位是

微型计算机中最小的数据单位是比特。微型计算机&#xff0c;是指由微处理器作为CPU的计算机。由大规模集成电路组成的、体积较小的电子计算机。由微处理机(核心)、存储片、输入和输出片、系统总线等组成。特点是体积小、灵活性大、价格便宜、使用方便。这类计算机的普遍特征就是…

琴生不等式一般形式_001.二次函数、方程和不等式知识点

学法指导&#xff1a;本专题讲授不等式内容&#xff0c;这部分内容是学生的难点&#xff0c;为此有几点说明&#xff1a;1.把握好学习的难度。按教材内不等式部分展现的内容看&#xff0c;它很简单&#xff0c;但学过的知道&#xff0c;这部分内容很难&#xff0c;直白的讲&…

伺服电机停的时候会冲一下_造成伺服电机抖动的原因竟然是它!内附解决方法...

伺服电机(servo motor )是指在伺服系统中控制机械元件运转的发动机&#xff0c;是一种补助马达间接变速装置。它可使控制速度&#xff0c;位置精度非常准确&#xff0c;可以将电压信号转化为转矩和转速以驱动控制对象。伺服电机转子转速受输入信号控制&#xff0c;并能快速反应…

【 HDU - 5363】Key Set(水题,快速幂,组合数学)

题干&#xff1a; soda has a set SS with nn integers {1,2,…,n}{1,2,…,n}. A set is called key set if the sum of integers in the set is an even number. He wants to know how many nonempty subsets of SS are key set. Input There are multiple test cases. The…

ajax 更新模型数据_DuangDuangDuang,重点来啦!高薪全靠它——百战Web前端课程更新03.11...

百战程序员九大专业运营&#xff0c;周周有课程更新&#xff0c;保持行业领先。本次更新课程Web前端第三十阶段经典面试题解析章节1—5及课程资料。本次更新可谓是诚意满满&#xff0c;针对市场面试需要&#xff0c;总结经典面试题集&#xff0c;为你揭开企业技术要求的神秘面纱…