Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2887    Accepted Submission(s): 969

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
 
Source
 
 //https://blog.csdn.net/V5ZSQ/article/details/51302603

 #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
typedef long long ll;
struct ma{
ll m[][];
ma()
{
memset(m,,sizeof(m));
}
};
ma ma_m(ma a,ma b,ll p)
{
ma c;
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{ for(int k=;k<;k++)
{
c.m[i][j] = (c.m[i][j]+(a.m[i][k]*b.m[k][j])%p+p)%p;
} }
}
return c;
}
ma ma_q(ma a,ll k,ll p)
{
ma ret;
for(int i=;i<;i++)
{
ret.m[i][i]=;
}
while(k)
{
if(k&)
{
ret=ma_m(ret,a,p);
}
a=ma_m(a,a,p);
k>>=;
}
return ret;
}
ll qu(ll a,ll b,ll p)
{
a%=p;
ll ret=;
while(b)
{
if(b&)
{
ret=ret*a%p;
}
a=a*a%p;
b>>=;
}
return ret%p;
}
int main()
{
ll t;
ll n,a,b,c,p;
scanf("%lld",&t);
while(t--)
{
scanf("%lld %lld %lld %lld %lld",&n,&a,&b,&c,&p);
if(a%p==)
{
printf("0\n");
}
else if(n==)
{
printf("1\n");
}
else if(n==)
{
printf("%lld\n",qu(a,b,p));
}
else {
p--;
ma aa;
aa.m[][]=c;aa.m[][]=;aa.m[][]=b;
aa.m[][]=;aa.m[][]=;aa.m[][]=;
aa.m[][]=;aa.m[][]=;aa.m[][]=;
aa=ma_q(aa,n-,p);
ll t=aa.m[][]*b+aa.m[][];
p++;
printf("%lld\n",qu(a,t,p)); }
}
return ;
}

hdu 5667的更多相关文章

  1. hdu 5667 BestCoder Round #80 矩阵快速幂

    Sequence  Accepts: 59  Submissions: 650  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536 ...

  2. HDU 5667 构造矩阵快速幂

    HDU 5667 构造矩阵快速幂 题目描述 解析 我们根据递推公式 设 则可得到Q的指数关系式 求Q构造矩阵 同时有公式 其中φ为欧拉函数,且当p为质数时有 代码 #include <cstdi ...

  3. HDU 5667 Sequence【矩阵快速幂+费马小定理】

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5667 题意: Lcomyn 是个很厉害的选手,除了喜欢写17kb+的代码题,偶尔还会写数学题.他找到 ...

  4. 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; ...

  5. HDU 5667 Sequence 矩阵快速幂

    官方题解: 观察递推式我们可以发现,所有的fi​​都是a的幂次,所以我们可以对f​i​​取一个以a为底的log,g​i​​=log​a​​ f​i​​ 那么递推式变g​i​​=b+c∗g​i−1​​+ ...

  6. HDU 5667 Sequence(矩阵快速幂)

    Problem Description Holion August will eat every thing he has found. Now there are many foods,but he ...

  7. HDU 5667 :Sequence

    Sequence  Accepts: 59  Submissions: 650  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536 ...

  8. HDU 5667 Sequence

    指数有递推式,可以通过矩阵快速幂来求解.再用下面这公式快速幂取模即可. (C是素数) #include<cstdio> #include<cstring> #include&l ...

  9. Hdu 5289-Assignment 贪心,ST表

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=5289 Assignment Time Limit: 4000/2000 MS (Java/Others) ...

随机推荐

  1. linux cached过高导致性能变低

    场景: 拿到了客户50个文件,平均每个文件大概40M左右的txt,文件在S3上,需要导入到数据库,40M解析出来大概是80W条左右的数据. 描述: 在刚开始执行导入时,因为数据验证复杂程度不同,每个文 ...

  2. CentOS 6.0 系统 LAMP(Apache+MySQL+PHP)安装步骤

    一.安装 MySQL 首先来进行 MySQL 的安装.打开超级终端,输入: [root@localhost ~]# yum install mysql mysql-server 安装完毕,让 MySQ ...

  3. springBoot jpa uuid生成策略

    实体类 import org.hibernate.annotations.GenericGenerator; import javax.persistence.*; @Entity @Table(na ...

  4. Mysql数据库操作语句总结(一)

    下面的内容来源于链接 https://www.cnblogs.com/bchjazh/p/5997728.html,  个人在此基础上进一步添加了一点东西. 个人操作数据库:  mysql 5.5.2 ...

  5. iOS - 协议实现的例子

    在实际开发中,协议的应用非常广泛,以下是实际应用的例子. 1.协议的定义: myProtocolDelegate.h // // myProtocolDelegate.h // zlwPlayerAp ...

  6. linux下配置Nginx,支持thinkphp

    前言引入 一个刚入行的朋友,刚换工作,入职了一个新公司.新公司一个php开发,就是他.俨然老板把他当成公司扛把子了,把服务器都给了他,让他部署整个php的开发环境.那个朋友是wamp爱好者.然后面对l ...

  7. cms-写帖子内容实现

    写帖子后台: mapper: <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperP ...

  8. java Vamei快速教程01

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! Java是完全面向对象的语言.Java通过虚拟机的运行机制,实现“跨平台”的理念. ...

  9. ABAP Netweaver和git的快捷方式

    Netweaver Jerry的SAPGUI收藏夹管理工具:链接 git 我笔记本上有很多github仓库,每次切换仓库,我不想敲很长的cd命令.比如现在我需要手敲下面的命令进入一个Java仓库: c ...

  10. Unity结合Flask实现排行榜功能

    业余做的小游戏,排行榜本来是用PlayerPrefs存储在本地,现在想将数据放在服务器上.因为功能很简单,就选择了小巧玲珑的Flask来实现. 闲话少叙.首先考虑URL的设计.排行榜无非是一堆分数sc ...