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. spring boot注入error,Consider defining a bean of type 'xxx' in your configuration问题解决方案

    经常出现这问题一定是非spring生态圈的@标签 没被spring引入,如mybatis等 因为在默认情况下只能扫描与控制器在同一个包下以及其子包下的@Component注解,以及能将指定注解的类自动 ...

  2. asp.net MVC 4.0 Controller回顾——ModelBinding实现过程

    以DefaultModelBinder为例 为简单模型绑定(BindSimpleModel)和复杂模型绑定(BindComplexModel) public virtual object BindMo ...

  3. webpack.config.js====配置babel

    参考:https://www.jianshu.com/p/9808f550c6a91. 安装依赖babel-loader: 负责 es6 语法转化babel-preset-env: 包含 es6.7 ...

  4. WPF动画的几种模式

    最近在用WPF做简单动画,以下是几点经验总结: 1. 使用DispatcherTimer做动画 VB6的年代大家就用Timer做动画了,不用多解释,这个DispatcherTimer和本身的Timer ...

  5. js的内联和外部调用

    如上代码(实现轮播图的部分代码),若以内联形式写在html里,就不会出现异常.但是如果放在外部js文件中,并在html的head中调用,就会出现异常(不报错,但是轮播逻辑出现异常). 解决:不使用全局 ...

  6. 一张图掌握移动Web前端所有技术(大前端、工程化、预编译、自动化)

    你要的移动web前端都在这里! 大前端方向:移动Web前端.Native客户端.Node.js. 大前端框架:React.Vue.js.Koa  跨终端技术:HTML 5.CSS 3.JavaScri ...

  7. chart.js 使用方法 特别说明不是中文的

    以上是一个饼图的案例,其他统计类型查看文档 http://www.chartjs.org/docs/latest/charts/doughnut.html 注意看域名 chartjs.org  不是 ...

  8. form自动提交

    在做项目中,例如b2b的由商城跳往卖家中心的后台,当他既有会员账号又是卖家的时候,让他登录了商城,就不需要卖家登录的时候,处理过程中可以用到form表单自动登录. 实现简单思路:可以再普通会员登录的地 ...

  9. nginx配置文件语法高亮

    下载文件 nginx.vim https://vim.sourceforge.io/scripts/script.php?script_id=1886 安装 下载 nginx.vim 文件到 ~/.v ...

  10. 编程之美2015 资格赛 hihocoder 题目2: 回文字符序列

    思路:暴力搜,用BFS的方式,生成每一种可能,再对每一种可能进行判断是否回文,进行统计.严重超时!计算一个25个字符的,大概要20多秒! #include <iostream> #incl ...