HDU 5667 Sequence(矩阵快速幂)
Problem Description
Holion August will eat every thing he has found.
Now there are many foods,but he does not want to eat all of them at once,so he find a sequence.
fn=⎧⎩⎨⎪⎪1,ab,abfcn−1fn−2,n=1n=2otherwise
He gives you 5 numbers n,a,b,c,p,and he will eat fn foods.But there are only p foods,so you should tell him fn mod p.
Input
The first line has a number,T,means testcase.
Each testcase has 5 numbers,including n,a,b,c,p in a line.
1≤T≤10,1≤n≤1018,1≤a,b,c≤109,p is a prime number,and p≤109+7.
Output
Output one number for each case,which is fn mod p.
Sample Input
1
5 3 3 3 233
Sample Output
190
用矩阵快速幂的时候,注意对p-1取余
递推式:a[n]=c*a[n-1]+a[n-2]+1;
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long int LL;
struct Node
{
LL a[3][3];
}A,B,C;
LL p,n,a,b,c;
Node multiply(Node a,Node b)
{
Node c;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
c.a[i][j]=0;
for(int k=0;k<3;k++)
{
(c.a[i][j]+=(a.a[i][k]*b.a[k][j])%(p-1))%=(p-1);
}
}
}
return c;
}
Node get(Node a,LL x)
{
Node c;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
c.a[i][j]=(i==j?1:0);
for(x;x;x>>=1)
{
if(x&1) c=multiply(c,a);
a=multiply(a,a);
}
return c;
}
LL quick(LL x,LL y)
{
if(n>1&&y==0) y=p-1;
LL ans=1;
for(y;y;y>>=1)
{
if(y&1) ans=(ans*x)%p;
x=(x*x)%p;
}
return ans;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%lld%lld%lld%lld%lld",&n,&a,&b,&c,&p);
A.a[0][0]=0;A.a[1][0]=0;A.a[2][0]=1;
B.a[0][0]=c; B.a[0][1]=1; B.a[0][2]=1;
B.a[1][0]=1; B.a[1][1]=0; B.a[1][2]=0;
B.a[2][0]=0; B.a[2][1]=0; B.a[2][2]=1;
if(n==1) {cout<<1<<endl;continue;}
B=get(B,n-1);
B=multiply(B,A);
LL num=((B.a[0][0]%(p-1))*(b%(p-1)))%(p-1);
//cout<<num<<endl;
cout<<quick(a,num)<<endl;
}
return 0;
}
HDU 5667 Sequence(矩阵快速幂)的更多相关文章
- HDU 5667 Sequence 矩阵快速幂+费马小定理
题目不难懂.式子是一个递推式,并且不难发现f[n]都是a的整数次幂.(f[1]=a0;f[2]=ab;f[3]=ab*f[2]c*f[1]...) 我们先只看指数部分,设h[n]. 则 h[1]=0; ...
- HDU 5667 Sequence 矩阵快速幂
官方题解: 观察递推式我们可以发现,所有的fi都是a的幂次,所以我们可以对fi取一个以a为底的log,gi=loga fi 那么递推式变gi=b+c∗gi−1+ ...
- HDU 5667 构造矩阵快速幂
HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...
- HDU.2640 Queuing (矩阵快速幂)
HDU.2640 Queuing (矩阵快速幂) 题意分析 不妨令f为1,m为0,那么题目的意思为,求长度为n的01序列,求其中不含111或者101这样串的个数对M取模的值. 用F(n)表示串长为n的 ...
- HDU - 1005 Number Sequence 矩阵快速幂
HDU - 1005 Number Sequence Problem Description A number sequence is defined as follows:f(1) = 1, f(2 ...
- HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with ...
- HDU 1005 Number Sequence(矩阵快速幂,快速幂模板)
Problem Description A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1 ...
- HDU - 1005 -Number Sequence(矩阵快速幂系数变式)
A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) m ...
- E - Recursive sequence HDU - 5950 (矩阵快速幂)
题目链接:https://vjudge.net/problem/HDU-5950 思路: 构造矩阵,然后利用矩阵快速幂. 1 #include <bits/stdc++.h> 2 #inc ...
- hdu-5667 Sequence(矩阵快速幂+费马小定理+快速幂)
题目链接: Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
随机推荐
- 高性能网络 | 你所不知道的TIME_WAIT和CLOSE_WAIT
你遇到过TIME_WAIT的问题吗? 我相信很多都遇到过这个问题.一旦有用户在喊:网络变慢了.第一件事情就是,netstat -a | grep TIME_WAIT | wc -l 一下.哎呀妈呀 ...
- Nokia Imaging SDK滤镜使用入门
简要说明: 通过对示例图片分别运用 LomoFilter和SketchFilter两个滤镜(Nokia Imaging SDK 内置), 来学习滤镜的基本使用和参数的设置.本工程的代码量比较少,也很容 ...
- jvm垃圾收集器之Throughput GC
呃.HotSpot VM的GC组老人之一Jon Masamitsu很久之前就写过blog讲解这个:https://blogs.oracle.com/jonthecollector/entry/our_ ...
- spring cloud outh2
使用Spring Cloud Security OAuth2搭建授权服务http://www.blogjava.net/paulwong/archive/2016/09/16/431797.html? ...
- lua工具库penlight--09技术选择
模块化和粒度 在理想的世界,一个程序应该只加载它需要的库.Penlight需要额外100 Kb 的字节码来工作.它是简单但却乏味要加载你需要什么: local data = require 'pl.d ...
- ReentrantReadWriteLock读写锁的使用<转>
Lock比传统线程模型中的synchronized方式更加面向对象,与生活中的锁类似,锁本身也应该是一个对象.两个线程执行的代码片段要实现同步互斥的效果,它们必须用同一个Lock对象. 读写锁:分为读 ...
- JDK1.8与spring3.x的不兼容
今天运气很好,两次遇到了这个兼容性问题,spring3.x不支持 java 1.8 byte code format!! 九月 10, 2017 7:17:18 上午 org.apache.catal ...
- google cloud之查看任务任务过程
点击侧边栏的logging
- QTreeWidget 获取被双击的子项的层次路径
from PyQt5.QtWidgets import (QApplication, QWidget, QHBoxLayout, QTreeWidget, QTreeWidgetItem, QGrou ...
- 转载:Python十分钟入门
Python十分钟入门:http://python.jobbole.com/23425/