大鳳號裝甲空母【找規律+矩陣快速冪】

大鳳號裝甲空母
時間限制: 1 Sec 內存限制: 128 MB
提交: 108 解決: 15
[提交] [狀態] [命題人:admin]
題目描述
大鳳號航空母艦很喜歡算術。
它,是舊日本海軍中最為先進的航空母艦。
它,是舊日本海軍中最為短命的航空母艦。
同時,她還是最平的航空母艦(龍驤:你說啥?)
如此多第一 ……
一生二,二生三,三生萬物 ……
這也許就是大鳳喜歡算術的原因吧。
有一天,她看到了這樣一道題:

電探發現了來自遠處的魚雷,時間不多了。

輸入
一行由空格隔開的兩個非負整數,分別是n和p。

輸出
一行表示答案。

樣例輸入
復制樣例數據
5 97
樣例輸出
11

提示
時間限制: 1 Sec 內存限制: 128 MB
提交: 108 解決: 15
[提交] [狀態] [命題人:admin]
題目描述
大鳳號航空母艦很喜歡算術。
它,是舊日本海軍中最為先進的航空母艦。
它,是舊日本海軍中最為短命的航空母艦。
同時,她還是最平的航空母艦(龍驤:你說啥?)
如此多第一 ……
一生二,二生三,三生萬物 ……
這也許就是大鳳喜歡算術的原因吧。
有一天,她看到了這樣一道題:

電探發現了來自遠處的魚雷,時間不多了。

輸入
一行由空格隔開的兩個非負整數,分別是n和p。

輸出
一行表示答案。

樣例輸入
復制樣例數據
5 97
樣例輸出
11

提示
在這里插入圖片描述
解題思路:
通過打表,得
在這里插入圖片描述
可以推出對于?xn?\lfloor x^n \rfloor?xn?的值dp[n]dp[n]dp[n]滿足:
dp[n]=f[n]+f[n?2]dp[n]=f[n]+f[n-2]dp[n]=f[n]+f[n?2]dp[n]=f[n?1]+2?f[n?2]dp[n]=f[n-1]+2*f[n-2]dp[n]=f[n?1]+2?f[n?2]
nn%2==1n時:dp[n]dp[n]dp[n]
否則 :dp[n]?1dp[n]-1dp[n]?1
因此可以通過矩陣快速冪來快速計算dp[n]dp[n]dp[n],可得:
∣dp[n]f[n]f[n?1]000000∣\begin{vmatrix} dp[n] & f[n] & f[n-1]\\ 0 & 0 & 0 \\ 0 & 0 & 0 \end{vmatrix}?dp[n]00?f[n]00?f[n?1]00?? =∣0f[1]f[0]000000∣\begin{vmatrix} 0& f[1] & f[0]\\ 0 & 0 & 0 \\ 0 & 0 & 0 \end{vmatrix}?000?f[1]00?f[0]00??*∣000111211∣n\begin{vmatrix} 0 & 0 & 0\\ 1 & 1 & 1 \\ 2 & 1 & 1 \end{vmatrix}^n?012?011?011??n
因此通過矩陣快速冪求得dp[n]dp[n]dp[n]的值,再根據nnn的奇偶分類討論即可。
ps:需特判一下nnn為0的情況
代碼:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <sstream>
#include <iomanip>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
struct node
{ll arr[3][3];node() {ms(arr);}
};
ll p;
node mul(node a,node b) {node c;for(int i=0;i<3;i++) {for(int j=0;j<3;j++) {for(int k=0;k<3;k++) {c.arr[i][j]=(c.arr[i][j]+(a.arr[i][k]*b.arr[k][j])%p)%p;}}}return c;
}
ll quickpow(ll n) {node a,b;a.arr[0][0]=a.arr[0][1]=a.arr[0][2]=a.arr[2][2]=0;a.arr[1][0]=a.arr[1][1]=a.arr[1][2]=a.arr[2][1]=1;a.arr[2][0]=2;b.arr[0][0]=b.arr[0][1]=1;b.arr[0][2]=b.arr[1][0]=b.arr[1][1]=b.arr[1][2]=0;b.arr[2][0]=b.arr[2][1]=b.arr[2][2]=0;while(n) {if(n&1) b=mul(b,a);a=mul(a,a);n>>=1;}return b.arr[0][0]%p;
}
int main() 
{#ifndef ONLINE_JUDGEfreopen("in.txt", "r", stdin);#endif//freopen("out.txt", "w", stdout);//ios::sync_with_stdio(0),cin.tie(0);ll n;scanf("%lld %lld",&n,&p);ll ans=quickpow(n);if(n==0) printf("%lld\n",1%p);else {if(n%2==1) printf("%lld\n",ans);else {printf("%lld\n",(ans-1+p)%p);}}return 0;
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/536282.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/536282.shtml
英文地址,請注明出處:http://en.pswp.cn/news/536282.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

codevs5429 多重背包【多重背包+單調隊列】

5429 多重背包 時間限制: 1 s 空間限制: 256000 KB 題目等級 : 鉆石 Diamond 題目描述 Description 你有一個容量為M的背包&#xff0c;和N種物品。 每種物品都有三個屬性&#xff0c;vi&#xff0c;wi&#xff0c;與ci&#xff0c;分別表示這種物品的體積、價值和件數。 你的…

generator 1【矩陣快速冪】

題目描述 You are given four positive integers x0,x1,a,bx_0, x_1, a, bx0?,x1?,a,b. And you know xia?xi?1b?xi?2x_i a \cdot x_{i-1} b \cdot x_{i-2}xi?a?xi?1?b?xi?2? for all i≥2i \ge 2i≥2. Given two positive integers n, and MOD, please calcul…

Give Candies【快速冪+歐拉】

Give Candies 時間限制: 1 Sec 內存限制: 128 MB 提交: 243 解決: 92 [提交] [狀態] [命題人:admin] 題目描述 There are N children in kindergarten. Miss Li bought them N candies。To make the process more interesting, Miss Li comes up with the rule: All the childr…

Save the Room【找規律】

Save the Room 時間限制: 1 Sec 內存限制: 128 MB 提交: 149 解決: 90 [提交] [狀態] [命題人:admin] 題目描述 Bob is a sorcerer. He lives in a cuboid room which has a length of A, a width of B and a height of C, so we represent it as ABC. One day, he finds that …

Participate in E-sports【Java大數+二分】

Participate in E-sports 時間限制: 2 Sec 內存限制: 128 MB 提交: 194 解決: 53 [提交] [狀態] [命題人:admin] 題目描述 Jessie and Justin want to participate in e-sports. E-sports contain many games, but they don’t know which one to choose, so they use a way to…

Poor God Water【矩陣快速冪】

Poor God Water 時間限制: 1 Sec 內存限制: 128 MB 提交: 102 解決: 50 [提交] [狀態] [命題人:admin] 題目描述 God Water likes to eat meat, fish and chocolate very much, but unfortunately, the doctor tells him that some sequence of eating will make them poisonou…

Transport Ship【多重背包】

Transport Ship 時間限制: 1 Sec 內存限制: 128 MB 提交: 175 解決: 65 [提交] [狀態] [命題人:admin] 題目描述 There are N different kinds of transport ships on the port. The i^th kind of ship can carry the weight of V[i] and the number of the ith kind of ship i…

洛谷P2015 二叉蘋果樹【樹形dp】

P2015 二叉蘋果樹 時間限制 1.00s 內存限制 125.00MB 題目描述 有一棵蘋果樹&#xff0c;如果樹枝有分叉&#xff0c;一定是分2叉&#xff08;就是說沒有只有1個兒子的結點&#xff09; 這棵樹共有N個結點&#xff08;葉子點或者樹枝分叉點&#xff09;&#xff0c;編號為1-N,…

洛谷P2014【樹形dp】

P2014 選課 時間限制 1.00s 內存限制 125.00MB 題目描述 在大學里每個學生&#xff0c;為了達到一定的學分&#xff0c;必須從很多課程里選擇一些課程來學習&#xff0c;在課程里有些課程必須在某些課程之前學習&#xff0c;如高等數學總是在其它課程之前學習。現在有N門功課&…

洛谷P2016 戰略游戲【樹形dp】

P2016 戰略游戲 時間限制 1.00s 內存限制 125.00MB 題目描述 Bob喜歡玩電腦游戲&#xff0c;特別是戰略游戲。但是他經常無法找到快速玩過游戲的辦法。現在他有個問題。 他要建立一個古城堡&#xff0c;城堡中的路形成一棵樹。他要在這棵樹的結點上放置最少數目的士兵&#x…

Shell Pyramid【數學+二分】

Shell Pyramid 時間限制: 1 Sec 內存限制: 128 MB 提交: 291 解決: 95 [提交] [狀態] [命題人:admin] 題目描述 In the 17th century, with thunderous noise, dense smoke and blazing fire, battles on the sea were just the same as those in the modern times. But at tha…

Degree Sequence of Graph G【模擬】

Degree Sequence of Graph G 時間限制: 1 Sec 內存限制: 128 MB 提交: 362 解決: 92 [提交] [狀態] [命題人:admin] 題目描述 Wang Haiyang is a strong and optimistic Chinese youngster. Although born and brought up in the northern inland city Harbin, he has deep lov…

Simple Addition expression【打表+二分】

Simple Addition expression 時間限制: 1 Sec 內存限制: 128 MB 提交: 355 解決: 80 [提交] [狀態] [命題人:admin] 題目描述 A luxury yacht with 100 passengers on board is sailing on the sea in the twilight. The yacht is ablaze with lights and there comes out laug…

洛谷P2622 關燈問題II【狀壓dp+bfs】

P2622 關燈問題II 題目描述 現有n盞燈&#xff0c;以及m個按鈕。每個按鈕可以同時控制這n盞燈——按下了第i個按鈕&#xff0c;對于所有的燈都有一個效果。按下i按鈕對于第j盞燈&#xff0c;是下面3中效果之一&#xff1a;如果a[i][j]為1&#xff0c;那么當這盞燈開了的時候&am…

洛谷P1879 [USACO06NOV]玉米田Corn Fields【狀壓dp】

P1879 [USACO06NOV]玉米田Corn Fields 時間限制 1.00s 內存限制 125.00MB 題目描述 Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yummy corn for the cows on a number…

LEAGUE TABLES【模擬】

LEAGUE TABLES 時間限制: 1 Sec 內存限制: 128 MB 提交: 349 解決: 150 [提交] [狀態] [命題人:admin] 題目描述 League football (known in some circles as soccer) has been played in England since 1888 and is the most popular winter game through most of Europe, jus…

MUSICAL CHAIRS【模擬】

MUSICAL CHAIRS 時間限制: 1 Sec 內存限制: 128 MB 提交: 386 解決: 76 [提交] [狀態] [命題人:admin] 題目描述 Musical chairs is a game frequently played at children’s parties. Players are seated in a circle facing outwards. When the music starts, the players h…

Bomb HDU - 3555【數位dp】

Bomb HDU - 3555 The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the time bomb. The number sequence of the time bomb counts from 1 to N. If the current number sequence includes the sub-sequence “49”, the power…

不要62 HDU - 2089【數位dp】

不要62 HDU - 2089 杭州人稱那些傻乎乎粘嗒嗒的人為62&#xff08;音&#xff1a;laoer&#xff09;。 杭州交通管理局經常會擴充一些的士車牌照&#xff0c;新近出來一個好消息&#xff0c;以后上牌照&#xff0c;不再含有不吉利的數字了&#xff0c;這樣一來&#xff0c;就可…

PACKING【二維01背包】

PACKING 時間限制: 1 Sec 內存限制: 128 MB 提交: 278 解決: 24 [提交] [狀態] [命題人:admin] 題目描述 It was bound to happen. Modernisation has reached the North Pole. Faced with escalating costs for feeding Santa Claus and the reindeer, and serious difficulti…