【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 -…

【HDU - 2546】饭卡 (dp,0-1背包,贪心思想)

电子科大本部食堂的饭卡有一种很诡异的设计&#xff0c;即在购买之前判断余额。如果购买一个商品之前&#xff0c;卡上的剩余金额大于或等于5元&#xff0c;就一定可以购买成功&#xff08;即使购买后卡上余额为负&#xff09;&#xff0c;否则无法购买&#xff08;即使金额足够…

linux的程序员计算器,linux中的计算器

windows系统安装时会自带计算器&#xff0c;在cmd中运行calc即可打开。那么linux系统中有没有计算器呢&#xff1f;答案是肯定的。linux下的图形计算器linux系统一般也会默认安装一个图形界面的计算器&#xff0c;例如红帽系统默认安装的是gcalctool。假如在一个图形桌面环境中…

【HDU - 薛猫猫杯程序设计网络赛】【题解】

A 爬山 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 0 Accepted Submission(s): 0 Problem Description 小Z准备去爬山&#xff0c;在他的面前有N座山&#xff0c;每座山都有对应的高度。他想选择两座高…

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

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

【CodeForces - 706C】Hard problem(dp,字典序)

题干&#xff1a; Vasiliy is fond of solving different tasks. Today he found one he wasnt able to solve himself, so he asks you to help. Vasiliy is given n strings consisting of lowercase English letters. He wants them to be sorted in lexicographical orde…

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…

【CodeForces - 349B】Color the Fence (贪心,填数)

题干&#xff1a; Igor has fallen in love with Tanya. Now Igor wants to show his feelings and write a number on the fence opposite to Tanyas house. Igor thinks that the larger the number is, the more chance to win Tanyas heart he has. Unfortunately, Igor …

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

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

【POJ - 2301 】Beat the Spread! (简单数学)

题干&#xff1a; Superbowl Sunday is nearly here. In order to pass the time waiting for the half-time commercials and wardrobe malfunctions, the local hackers have organized a betting pool on the game. Members place their bets on the sum of the two final s…

Linux x8664汇编,Linux Udis86 反汇编引擎使用

前两篇说了capstone/beaengine,这节一起用一用经典的udis86;github:https://github.com/vmt/udis860x01:udis86相比于前面两个&#xff0c;用起来还是比较简单的&#xff0c;使用文档如下所示&#xff1a;Getting StartedBuilding and Installing udis86----------------------…

【HDU - 2200】Eddy's AC难题(简单组合数学)

题干&#xff1a; Eddy是个ACMer,他不仅喜欢做ACM题,而且对于Ranklist中每个人的ac数量也有一定的研究,他在无聊时经常在纸上把Ranklist上每个人的ac题目的数量摘录下来&#xff0c;然后从中选择一部分人(或者全部)按照ac的数量分成两组进行比较&#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;存取速度最快…

【POJ - 3320 】Jessica's Reading Problem (尺取,哈希)

题干&#xff1a; Jessicas a very lovely girl wooed by lots of boys. Recently she has a problem. The final exam is coming, yet she has spent little time on it. If she wants to pass it, she has to master all ideas included in a very thick text book. The aut…

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…

编译原理last集c语言,编译原理作业集-第七章.doc

编译原理作业集-第七章第七章 语义分析和中间代码产生本章要点1. 中间语言&#xff0c;各种常见中间语言形式&#xff1b;2. 说明语句赋值语句布尔表达式控制语句的翻译&#xff1b;3. 过程调用的处理&#xff1b;4. 类型检查&#xff1b;本章目标掌握和理解中间语言&#xff0…

【CodeForces - 244A 】Dividing Orange (构造,水题)

题干&#xff1a; One day Ms Swan bought an orange in a shop. The orange consisted of nk segments, numbered with integers from 1 to nk. There were k children waiting for Ms Swan at home. The children have recently learned about the orange and they decided…