Sequence

 Accepts: 59
 Submissions: 650
 Time Limit: 2000/1000 MS (Java/Others)
 Memory Limit: 65536/65536 K (Java/Others)
问题描写叙述
\ \ \ \    Lcomyn 是个非常厉害的选手,除了喜欢写17kb+的代码题,偶尔还会写数学题.他找到了一个数列:

f_n=\left\{\begin{matrix} 1 ,&n=1 \\ a^b,&n=2 \\ a^bf_{n-1}^cf_{n-2},&otherwise \end{matrix}\right.f​n​​=​⎩​⎨​⎧​​​1,​a​b​​,​a​b​​f​n−1​c​​f​n−2​​,​​​n=1​n=2​otherwise​​

\ \ \ \    他给了你几个数:nn,aa,bb,cc,你须要告诉他f_nf​n​​模pp后的数值.
输入描写叙述
\ \ \ \    第一行一个数T,为測试数据组数.

\ \ \ \    每组数据一行,一行五个正整数,按顺序为nn,aa,bb,cc,pp.

\ \ \ \ 1\le T \le 10,1\le n\le 10^{18}    1≤T≤10,1≤n≤10​18​​,1\le a,b,c\le 10^91≤a,b,c≤10​9​​,p是质数且p\le 10^9+7p≤10​9​​+7.
输出描写叙述
\ \ \ \    对每组数据输出一行一个数,输出f_nf​n​​对pp取模后的数值.
输入例子
1
5 3 3 3 233
输出例子
190

发现f序列就是a的不同指数的形式。所以对每个f对a取对数。发现就是f[n]=b+c*f[n-1]+f[n-2]。

构造矩阵,高速幂搞。

注意由于是在指数上。所以模的值须要是欧拉函数p,由于p是质数。所以直接是p-1。

代码:

#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <map>
using namespace std;
typedef long long ll; #define INF 0x333f3f3f
#define repp(i, n, m) for (int i = n; i <= m; i++)
#define rep(i, n, m) for (int i = n; i < m; i++)
#define sa(n) scanf("%d", &(n)) const ll mod = 100000007;
const int maxn = 5e5 + 5;
const double PI = acos(-1.0); ll n, a, b, c, p; struct ma
{
ll val[4][4];
ma operator *(const ma &b)
{
int i, j, k;
ma res;
memset(res.val, 0, sizeof(res.val)); for (k = 1; k <= 3; k++)
{
for (i = 1; i <= 3; i++)
{
for (j = 1; j <= 3; j++)
{
res.val[i][j] += (this->val[i][k] * b.val[k][j]) % (p - 1);
res.val[i][j] %= (p - 1);
}
}
}
return res;
}
}; ll po(ll x, ll y)
{
ll res = 1;
while (y)
{
if (y & 1)
res = res*x%p;
x = x*x%p;
y >>= 1;
}
return res;
} ma po_matrix(ma &x, ll y)
{
ma res;
res.val[1][1] = 1, res.val[1][2] = 0, res.val[1][3] = 0;
res.val[2][1] = 0, res.val[2][2] = 1, res.val[2][3] = 0;
res.val[3][1] = 0, res.val[3][2] = 0, res.val[3][3] = 1;
while (y)
{
if (y & 1)
res = res*x;
x = x*x;
y >>= 1;
}
return res;
} void solve()
{
ll i, j, k;
scanf("%lld%lld%lld%lld%lld", &n, &a, &b, &c, &p); ll res;
ma r;
if (n == 1)
{
puts("1");
}
else if (n == 2)
{
res = po(a, b);
printf("%lld\n", res);
}
else
{
r.val[1][1] = c, r.val[1][2] = 1, r.val[1][3] = b;
r.val[2][1] = 1, r.val[2][2] = 0, r.val[2][3] = 0;
r.val[3][1] = 0, r.val[3][2] = 0, r.val[3][3] = 1; r = po_matrix(r, n - 2);
res = r.val[1][3] + r.val[1][1] * b;
res = po(a, res);
printf("%lld\n", res);
}
} int main()
{
#ifndef ONLINE_JUDGE
freopen("i.txt", "r", stdin);
freopen("o.txt", "w", stdout);
#endif int t;
scanf("%d", &t); while (t--)
{
solve();
} return 0;
}

最新文章

  1. MSSQL部分补丁的列表及下载地址(持续更新)
  2. vs 2005 thread 无法调试
  3. lintcode-【简单题】链表求和
  4. Hibernate中的PO
  5. iOS - Swift NSPoint 位置
  6. ASP.NET MVC 中将FormCollection与实体间转换方法【转】
  7. 012-ViewState状态保持
  8. 【Egret】3d 服务器配置
  9. 使用docker+consul+nginx集成分布式的服务发现与注册架构
  10. 读HashMap 源码(jdk11)的见解
  11. 毕设之c#多线程学习(官方+转载)
  12. Python中crypto模块进行AES加密和解密
  13. 【GPU编解码】GPU硬编码 (转)
  14. [leetcode]131. Palindrome Partitioning字符串分割成回文子串
  15. CF993E Nikita and Order Statistics 【fft】
  16. Python基础部分的疑惑解析(1)
  17. centos7 远程连接mongodb时,27017端口连接不上的解决办法
  18. python开发_类型转换convert
  19. 反射方式,获取出集合ArrayList类的class文件对象
  20. Codeforces 620E New Year Tree【线段树傻逼题】

热门文章

  1. DS-博客作业06--图
  2. Python之自动单元测试之一(unittest使用实例)
  3. 96. Unique Binary Search Trees(I 和 II)
  4. java面试题之HashMap和TreeMap的区别
  5. 基于Redis Sentinel的Redis集群(主从&amp;Sharding)高可用方案
  6. zabbix基于LNMP安装
  7. mysql的简单介绍
  8. Spring定义的五种事务隔离级别
  9. ibatis 字段类型为int时如何避免默认值得干扰
  10. TinyXML2使用教程(转)