HDU 5667 :Sequence
Sequence
\ \ \ \ 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.fn=⎩⎨⎧1,ab,abfn−1cfn−2,n=1n=2otherwise \ \ \ \ 他给了你几个数:nn,aa,bb,cc,你须要告诉他f_nfn模pp后的数值.
\ \ \ \ 第一行一个数T,为測试数据组数. \ \ \ \ 每组数据一行,一行五个正整数,按顺序为nn,aa,bb,cc,pp. \ \ \ \ 1\le T \le 10,1\le n\le 10^{18} 1≤T≤10,1≤n≤1018,1\le a,b,c\le 10^91≤a,b,c≤109,p是质数且p\le 10^9+7p≤109+7.
\ \ \ \ 对每组数据输出一行一个数,输出f_nfn对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的更多相关文章
- HDU 5312:Sequence
Sequence Accepts: 25 Submissions: 1442 Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 2621 ...
- HDU - 6395:Sequence (分块+矩阵)
题面太丑了,就不复制了. 题意:F1=A: F2=B: Fn=D*Fn-1+C*Fn-2+P/i:求Fn. 思路:根据P/i的值划分区间,每个区间矩阵求. 带常数的矩阵: #include<bi ...
- HDU 5860 Death Sequence(死亡序列)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 1005 Number Sequence(数列)
HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDU 5860 Death Sequence(递推)
HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...
- hdu 5667 BestCoder Round #80 矩阵快速幂
Sequence Accepts: 59 Submissions: 650 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- HDU 1005 Number Sequence(数论)
HDU 1005 Number Sequence(数论) Problem Description: A number sequence is defined as follows:f(1) = 1, ...
- 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] 要求满 ...
- 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],在 ...
随机推荐
- Spring整合Junit进行单元测试
I. 加入依赖包 Spring Test (如spring-test-2.5.4.jar) JUnit 4 Spring 其他相关包 II.新建Junit Test Case III.读取配置文件 @ ...
- iOS-----openGL--openGL ES iOS 入门篇4---> 离屏渲染
http://www.cnblogs.com/CoderAlex/p/6604618.html 通常情况下,我们使用openGL将渲染好的图片绘制到屏幕上,但有时候我们不想显示处理结果,这时候就需要使 ...
- 微信小程序页面跳转传参
1.传递参数方法 使用navigatior组件 <navigator url="/pages/pull/pull?title=lalla&name=cc" hov ...
- web 工程中利用Spring的 ApplicationContextAware接口自动注入bean
最常用的办法就是用 ClassPathXmlApplicationContext, FileSystemClassPathXmlApplicationContext, FileSystemXmlApp ...
- 聊聊 Spring Boot 2.0 的 WebFlux
聊聊 Spring Boot 2.0 的 WebFlux## 前言 对照下 Spring Web MVC ,Spring Web MVC 是基于 Servlet API 和 Servlet 容器设计的 ...
- luoguP1040 区间DP(记忆化 加分二叉树
dp[l][r]记录中序序列为l, l+1..r的最大加分值 root[l][r]记录这个序列的根节点 转移 i 为根节点 dp[l][r] = max(dp[l][i-1]*dp[l+1][r]+a ...
- 基于promise和script标签的jsonp
function Jsonp(url){ var url=url.indexOf('?')>-1?url+"&callback=callback":url+" ...
- url相关
#测试网址: http://localhost/blog/testurl.php?id=5 //获取域名或主机地址 echo$_SERVER['HTTP_HOST']."<br> ...
- Yii createCommand CURD操作
本文用作工作记录,也许有人会问为什么不用 Yii 的 Model 去操作 DB,原因很简单,Yii 的 Model 写法上是方便了很多,但是会执行多余的 SQL,打开 Yii 的执行 log 就会发现 ...
- Codeforces Gym101502 J-取数博弈
还有J题,J题自己并不是,套的板子,大家写的都一样,因为大家都是套板子过的,贴一下代码,等学会了写一篇博客... J.Boxes Game 代码: 1 //J. Boxes Game-取数博弈-不会, ...