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. $.ajax仿axios封装

    适用于对老项目维护时,用习惯的axios不能使用的情况 基础封装: 保留 then 的回调.baseHref.method 传 post || get || etc, function ajax(ob ...

  2. windows下使用VM虚拟机安装linux

    转载地址:http://blog.csdn.net/u013142781/article/details/50529030 安装过程中发现与下面的顺序有点不同,遇到的问题是: 在选择中文进行安装时,一 ...

  3. concurrent.futures模块与协程

    concurrent.futures  —Launching parallel tasks    concurrent.futures模块同时提供了进程池和线程池,它是将来的使用趋势,同样我们之前学习 ...

  4. Vue系列(2):Vue 安装

    前言:关于页面上的知识点,如有侵权,请看 这里 . 关键词:小白.Vue 安装.Vue目录结构.Vue 构建页面流程 ? 初学者安装 vue 用什么好 大家都知道,学 Vue 最好还是去官网学,官网写 ...

  5. zabbix中监控项报错

    报错信息: zabbix报错(Not all processes could be identified, non-owned process info will not be shown, you ...

  6. 性能调优--大事务与Alwayson 之间的关系

    最近性能调优的事比较多,所以摘一些比较有特点的 案例分享下. 业务系统用的是sql server 2016 ,搭建的ALWAYSON 两节点的 群集,今天早上突然辅助 副本的只读库出现大量的等待导致系 ...

  7. IOS 获取手机的屏幕宽度

    //屏幕的宽度 CGFloat screenW=[UIScreen mainScreen].bounds.size.width;

  8. codeforce 599C Day at the Beach

    Bi表示第i个块,那么就是说Bi max ≤ Bi+1 min,又因为Bi min ≤ Bi max, 因此只要判断前缀的最大值是否小于等于后缀. #include<bits/stdc++.h& ...

  9. SRM 670 div2 A B C div1 A(贪心,子问题合并)

    A Cdgame brute force... B Drbalance 贪心,每次选最前面的-变成+,相当于后面所有的负值+2. C Treestrat 考虑集中去抓一个Red Token,以这个To ...

  10. UVA Live Archive 4394 String painter(区间dp)

    区间dp,两个str一起考虑很难转移. 看了别人题解以后才知道是做两次dp. dp1.str1最坏情况下和str2完全不相同,相当于从空白串开始刷. 对于一个区间,有两种刷法,一起刷,或者分开来刷. ...