题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5895

f(n)=f(n-2)+2*f(n-1)
f(n)*f(n-1)=f(n-2)*f(n-1)+2*f(n-1)*f(n-1);
2*f(n-1)*f(n-1)=f(n)*f(n-1)-f(n-2)*f(n-1);
累加可得 g(n) = f(n)*f(n+1)/2
 
然后这个公式:A^x % m = A^(x%phi(m)+phi(m)) % m (x >= phi(m))
 
反正比赛没做出来.
#include <bits/stdc++.h>
#define LL long long
using namespace std;
struct Maxtri{
LL v[][];
Maxtri(){memset(v,,sizeof(v));}
}ori;
LL n, y, x, s, mod ;
Maxtri mult(Maxtri a,Maxtri b){
Maxtri temp;
for(int i=;i<;i++){
for(int j=;j<;j++){
for(int k=;k<;k++){
temp.v[i][j] = (temp.v[i][j]+(a.v[i][k]*b.v[k][j])%mod)%mod;
}
}
}
return temp;
}
LL pow_mod(Maxtri a,LL n){
if(n==) return ;
if(n==) return ;
if(n==) return ;
n-=;
Maxtri ans;
for(int i=;i<;i++){
ans.v[i][i] = ;
}
while(n){
if(n&) ans = mult(ans,a);
a = mult(a,a);
n>>=;
}
return (ans.v[][]*+ans.v[][])%mod;
}
LL pow_mod1(LL a,LL n,LL mod){
LL ans = ;
while(n){
if(n&) ans = ans*a%mod;
a = a*a%mod;
n>>=;
}
return ans;
}
LL Phi(LL x)
{
LL ans = x;
for(LL i=2LL; i*i<=x; i++)
{
if(x % i == )
{
ans -= ans/i;
while(x % i == )
x /= i;
}
}
if(x > )
ans -= ans/x;
return ans;
}
int main(){
ori.v[][] = ,ori.v[][] = ;
ori.v[][] = ,ori.v[][] = ;
int tcase;
scanf("%d",&tcase);
while(tcase--){
scanf("%lld%lld%lld%lld",&n, &y, &x, &s);
s++;
LL phi = *Phi(s);
mod = *phi;
LL fn = pow_mod(ori,n*y);
LL fn1 = pow_mod(ori,n*y+);
LL ans = ((fn*fn1)%mod/);
ans+=phi;
printf("%lld\n",pow_mod1(x,ans,s)%s);
}
return ;
}

hdu 5895(矩阵快速幂+欧拉函数)的更多相关文章

  1. HDU 3221 矩阵快速幂+欧拉函数+降幂公式降幂

    装载自:http://www.cnblogs.com/183zyz/archive/2012/05/11/2495401.html 题目让求一个函数调用了多少次.公式比较好推.f[n] = f[n-1 ...

  2. HDU4549 M斐波那契数列 矩阵快速幂+欧拉函数+欧拉定理

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  3. [bzoj 1409] Password 矩阵快速幂+欧拉函数

    考试的时候想到了矩阵快速幂+快速幂,但是忘(bu)了(hui)欧拉定理. 然后gg了35分. 题目显而易见,让求一个数的幂,幂是斐波那契数列里的一项,考虑到斐波那契也很大,所以我们就需要欧拉定理了 p ...

  4. HDU 4549 矩阵快速幂+快速幂+欧拉函数

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  5. Product Oriented Recurrence(Codeforces Round #566 (Div. 2)E+矩阵快速幂+欧拉降幂)

    传送门 题目 \[ \begin{aligned} &f_n=c^{2*n-6}f_{n-1}f_{n-2}f_{n-3}&\\ \end{aligned} \] 思路 我们通过迭代发 ...

  6. hdu4549 矩阵快速幂 + 欧拉降幂

    R - M斐波那契数列 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit  ...

  7. Super A^B mod C (快速幂+欧拉函数+欧拉定理)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=1759 题目:Problem Description Given A,B,C, You should quick ...

  8. hdu 2814 快速求欧拉函数

    /** 大意: 求[a,b] 之间 phi(a) + phi(a+1)...+ phi(b): 思路: 快速求欧拉函数 **/ #include <iostream> #include & ...

  9. hdu 2824 The Euler function(欧拉函数)

    题目链接:hdu 2824 The Euler function 题意: 让你求一段区间的欧拉函数值. 题解: 直接上板子. 推导过程: 定义:对于正整数n,φ(n)是小于或等于n的正整数中,与n互质 ...

随机推荐

  1. CF785D Anton and School - 2 解题报告

    CF785D Anton and School - 2 题意:给定一个长度\(\le 2 \times 10e5\)由'('和')'组成的字符串,问有多少个子串(可以不连续),前半部分是由\('('\ ...

  2. 【bzoj2006】【NOI2015】超级钢琴

    2006: [NOI2010]超级钢琴 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 4292  Solved: 2195[Submit][Statu ...

  3. HDU 4722 数位dp

    Good Numbers Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  4. springMVC和mybatis的原理

    mybatis是什么? mybatis是一个持久层框架,是apache下的开源项目,前身是itbatis,是一个不完全的ORM框架,mybatis提供输入和输出的映射,需要程序员自己写sql语句,my ...

  5. shell unittest工具

    shUnit2:  https://github.com/kward/shunit2 用法非常简单,看看readme就行了.

  6. bzoj 2795 [Poi2012]A Horrible Poem hash+数论

    2795: [Poi2012]A Horrible Poem Time Limit: 50 Sec  Memory Limit: 128 MBSubmit: 640  Solved: 322[Subm ...

  7. webstorm 激活破解方法大全

    webstorm 作为最近最火的前端开发工具,也确实对得起那个价格,但是秉着勤俭节约的传统美德,我们肯定是能省则省啊. 方法一:(更新时间:2018/1/23)v3.3 注册时,在打开的License ...

  8. CollectionUtils.isEqualCollection的用法

    在使用Java的集合时,有些时候会需要比较两个集合是否相等,自己写方法其实也简单,但是既然有了好的实现,就不要自己造轮子了,只要了解这个轮子是什么原理就好了. public static boolea ...

  9. 简单实现VUE的双向数据绑定

    <!DOCTYPE html> <html> <head> <title>vue-双向数据绑定的简单实现</title> </head ...

  10. [LeetCode] 29. Divide Two Integers ☆☆

    Divide two integers without using multiplication, division and mod operator. If it is overflow, retu ...