generator 1【矩阵快速幂】

题目描述
You are given four positive integers x0,x1,a,bx_0, x_1, a, bx0,x1,a,b. And you know xi=a⋅xi−1+b⋅xi−2x_i = a \cdot x_{i-1} + b \cdot x_{i-2}xi=axi1+bxi2 for all i≥2i \ge 2i2.

Given two positive integers n, and MOD, please calculate xnx_nxn modulo MOD.

Does the problem look simple? Surprise! The value of n may have many many digits!

输入描述:
The input contains two lines.
The first line contains four integers x0,x1,a,bx_0, x_1, a, bx0,x1,a,b (1≤x0,x1,a,b≤109)(1 \le x_0, x_1, a, b \le 10^9)(1x0,x1,a,b109)
The second line contains two integers nnn, MOD (1≤n&lt;10(106),109&lt;MOD≤2×109,nhasnoleadingzero)(1 \le n &lt; 10^{(10^6)}, 10^9 &lt; MOD \le 2 \times 10^9 , n\ has\ no\ leading\ zero)(1n<10(106),109<MOD2×109,n has no leading zero).

输出描述:
Print one integer representing the answer.

示例1
输入
1 1 1 1
10 1000000001
输出
89
说明
The resulting sequence x is Fibonacci sequence. The 11-th item is 89.

示例2
输入
1315 521 20185 5452831
9999999999999999999999999999999999999 1000000007
输出
914730061

题目大意:
已知公式xi=a⋅xi−1+b⋅xi−2x_i=a \cdot x_{i-1} + b \cdot x_{i-2}xi=axi1+bxi2
第一行输入x0,x1,a,bx_0,x_1,a,bx0,x1,a,b
第二行输入n,pn,pn,p
xn%px_n\%pxn%p的值

解题思路:
由于此题nnn的数据范围很大,而且又知道公式,所以可以很容易想到是矩阵快速幂的裸题,但由于nnn的范围很大,所以需将二进制的快速幂写法改为十进制。
由于xi=a⋅xi−1+b⋅xi−2x_i=a \cdot x_{i-1} + b \cdot x_{i-2}xi=axi1+bxi2
所以
∣xn+1xn00∣=∣x0x100∣∗∣a1b0∣n\begin{vmatrix} x_{n+1} &amp; x_n \\ 0 &amp; 0 \end{vmatrix}=\begin{vmatrix} x_0 &amp; x_1 \\ 0 &amp; 0 \end{vmatrix}*\begin{vmatrix} a &amp; 1 \\ b &amp; 0 \end{vmatrix}^nxn+10xn0=x00x10ab10n
因此直接敲矩阵快速幂即可。

代码:

#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[2][2];node() {ms(arr);}
};
ll p;
node mul(node a,node b) {node c;for(int i=0;i<2;i++) {for(int j=0;j<2;j++) {for(int k=0;k<2;k++) {c.arr[i][j]=(c.arr[i][j]+(a.arr[i][k]*b.arr[k][j])%p)%p;}}}return c;
}
char str[1001000];
node b1,base;
int len;
ll quickpow() {for(int i=len-1;i>=0;i--) {int x=(int)(str[i]-'0');node nape2;nape2.arr[0][0]=base.arr[0][0];nape2.arr[0][1]=base.arr[0][1];nape2.arr[1][0]=base.arr[1][0];nape2.arr[1][1]=base.arr[1][1];while(x) {if(x&1) b1=mul(b1,nape2);nape2=mul(nape2,nape2);x>>=1;}node nape1=mul(base,base);base=mul(base,base);base=mul(base,base);base=mul(base,base);base=mul(nape1,base);}return b1.arr[0][1]%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 x0,x1,a,b;scanf("%I64d %I64d %I64d %I64d",&x0,&x1,&a,&b);scanf("%s",str);scanf("%I64d",&p);base.arr[0][0]=a;base.arr[0][1]=1LL;base.arr[1][0]=b;base.arr[1][1]=0;b1.arr[0][0]=x1;b1.arr[0][1]=x0;b1.arr[1][0]=0;b1.arr[1][1]=0;len=strlen(str);printf("%I64d\n",quickpow());return 0;
}

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

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

相关文章

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…

机器人军团【动态规划】

机器人军团 时间限制: 1 Sec 内存限制: 64 MB 提交: 279 解决: 139 [提交] [状态] [命题人:admin] 题目描述 邪狼&#xff1a;“怎么感觉这些机器人比我还聪明&#xff1f;不是说人工智能永远不能超越人类吗&#xff1f;” 天顶星人&#xff1a;“你们真是目光短浅&#xff0c…

【动态规划】抄近路

【动态规划】抄近路 时间限制: 1 Sec 内存限制: 64 MB 提交: 105 解决: 68 [提交] [状态] [命题人:admin] 题目描述 “最近不知道怎么回事&#xff0c;感觉我们这个城市变成了一个神奇的地方&#xff0c;有时在路上走着走着人就消失了&#xff01;走着走着突然又有人出现了&…