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;
}

HDU 5667 :Sequence的更多相关文章

  1. HDU 5312:Sequence

    Sequence  Accepts: 25  Submissions: 1442  Time Limit: 2000/2000 MS (Java/Others)  Memory Limit: 2621 ...

  2. HDU - 6395:Sequence (分块+矩阵)

    题面太丑了,就不复制了. 题意:F1=A: F2=B: Fn=D*Fn-1+C*Fn-2+P/i:求Fn. 思路:根据P/i的值划分区间,每个区间矩阵求. 带常数的矩阵: #include<bi ...

  3. HDU 5860 Death Sequence(死亡序列)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  4. HDU 1005 Number Sequence(数列)

    HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

  5. HDU 5860 Death Sequence(递推)

    HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...

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

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

  7. HDU 1005 Number Sequence(数论)

    HDU 1005 Number Sequence(数论) Problem Description: A number sequence is defined as follows:f(1) = 1, ...

  8. HDU 6078 - Wavel Sequence | 2017 Multi-University Training Contest 4

    /* HDU 6078 - Wavel Sequence [ DP ] | 2017 Multi-University Training Contest 4 题意: 给定 a[N], b[M] 要求满 ...

  9. HDU 6047 - Maximum Sequence | 2017 Multi-University Training Contest 2

    /* HDU 6047 - Maximum Sequence [ 单调队列 ] 题意: 起初给出n个元素的数列 A[N], B[N] 对于 A[]的第N+K个元素,从B[N]中找出一个元素B[i],在 ...

随机推荐

  1. P2564 生日礼物

    生日礼物 洛谷链接 题目描述: 在一段彩带上有不同颜色的彩珠,求出包含所有颜色彩珠的最短彩带长度. 思路: 我们可以把按彩珠的位置把所有彩珠排一下序,然后从1开始遍历这些彩珠,并记录出现过的颜色数目, ...

  2. ansible /usr/bin/python: not found

    使用ansible命令的时候出错 ansible all -m ping 出现报错 192.168.199.154 | FAILED! => { "changed": fal ...

  3. C# 时间与时间戳互转 13位|13位時間戳与日期换转

    这里直接上代码 懂C# 的程序猿 一看便知道如何使用的... /// <summary> /// 将Unix时间戳转换为DateTime类型时间 /// </summary> ...

  4. [JSOI2007]字符加密Cipher SA

    [JSOI2007]字符加密Cipher Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 7859  Solved: 3410[Submit][Stat ...

  5. jenkins配置发送邮件

    1.打开系统管理->系统设置,找到邮件设置,如下: 2.SMTP或者其他方式的发送邮件,可自行配置,一下列出了qq邮箱和163邮箱设置的地方,如下图: qq邮箱: 往下拉,找到如下图: 163邮 ...

  6. java web项目防止多用户重复登录解决方案

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本人声明.否则将追究法律责任.作者:永恒の_☆    地址:http://blog.csdn.net/chenghui031 ...

  7. 【python接口自动化】httpUtils

    # coding=utf8 import requests from common.logger import Logger import logging class httpUtils: logge ...

  8. SpringBoot重点详解--使用Junit进行单元测试

    目录 添加依赖与配置 ApplicationContext测试 Environment测试 MockBean测试 Controller测试 情况一 情况二 方法一 方法二 本文将对在Springboo ...

  9. hdu 1007 Quoit Design 分治求最近点对

    Quoit Design Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  10. 长度rem的使用

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...