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. iOS开发ReactiveCocoa学习笔记(三)

    RAC常用用法: 1.监听按钮的点击事件: UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame ...

  2. MATLAB之画确定区域内不重合的随机圆

    MATLAB之画确定区域内不重合的随机圆 程序要求:在确定区域内,画互不重合的圆. 知识点: (1)A=p'; 转置运算 (2)ones(a,b)产生a行b列全1数组 (3)rand(a,b)产生a行 ...

  3. JavaScript获取样式值的几种方法学习总结

    本人经过整理总结出以下获取样式值的方法,如有错误请各位大佬指正. 有四种方法:style,currentStyle,getComputedStyle,rules 与 cssRules方法. 1. st ...

  4. [转]Linux中如何读写硬盘上指定物理扇区

    读指定物理扇区: dd  if=<源设备>  of=<输出设备或文件>   skip=<指定扇区值>  bs=512 count=1 写指定物理扇区: dd   i ...

  5. 创建一个自己的GitHub,创建自己的开源项目

    作者是一个大学在读学生,自己在平时的学习中,GitHub上的开源项目给自己提供了很大的帮助.GitHub是目前使用最广泛的分布式项目管理软件,GitHub上面托管了许多非常优秀的开源项目.我觉得每一个 ...

  6. GBase数据库存储过程——批量查询多个数据表的磁盘占用情况

    --清理历史表,可选 DROP TABLE IF EXISTS `dap_model`.`data_statics`; CREATE TABLE `dba`.`data_statics` ( `TAB ...

  7. c++ vector & 二维数组 & MessageBox

    vector: https://www.cnblogs.com/mr-wid/archive/2013/01/22/2871105.html c++ 二维数组: int **p; p = new in ...

  8. MYSQL短索引

    优化MYSQL时,可以尽量使用短索引,如果只是为了提高读取的速度,可以优先使用聚合索引,把几个字段聚集在一起,当然缺点在于操作(写)的时候会降低效率,短索引一般都是开头几个字符基本不同的时候,可以考虑 ...

  9. python_43_移动文件指针补充

    #移动文件指针补充 ''' 文件对象.seek((offset,where)) offset:移动的偏移量,单位为字节.等于正数时向文件尾方向移动,等于负数时向文件头方向移动文件指针 where:指针 ...

  10. oracle用户创建及权限设置(转)

    权限: create session create table unlimited tablespace connect resource dba 例: #sqlplus /nolog SQL> ...