【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 10Output
2Input
3 8Output
2Note
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.
解題報(bào)告:
? ? 找規(guī)律、、走一遍流程會(huì)發(fā)現(xiàn)是有遞推關(guān)系的,再將關(guān)系化簡(jiǎn)一下就是一個(gè)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 ; }?
總結(jié)
以上是生活随笔為你收集整理的【CodeForces - 227C】Flying Saucer Segments (思维)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: ACMer的AC福音!手动扩栈外挂!(防
- 下一篇: musirc4.71.exe - mus