HDU 5674 Function(斐波那契模数列循环节)
来源:互联网 发布:淘宝卖家多少分一个钻 编辑:程序博客网 时间:2024/06/02 02:46
Problem Description
There is a function
f(n)=(a+√b)^n+(a−√b)^n. a and b are integers
(1≤a,b≤1,000,000).
Maybe the function looks complex but it is actually an integer.The question is to calculate
f(x^y).The answer can bevery large,so just output the answer mod 1,000,000,007.
Input
There are multiple test cases. The first line of input contains an integer
T(1≤T≤200)
indicating the number of test cases. For each test case:
One line contains four integers
a,b (1≤a,b≤1,000,000), x(1≤x≤50),y(1≤y≤10^18).
Output
For each test case, output one integer.
Sample Input
3
3 5 1 1
3 5 2 1
1 1 49 99999
Sample Output
6
28
160106184
斐波那契数列循环节的定理就不在这里证明了,结论就是:假设MOD为模数,则在f[2]!=MOD时,(MOD+1)(MOD-1)一定为其一个循环节.这道题f[n]明显是裴波那契数列通项的形式,所以可以利用裴波那契数列的特征方程来构造裴波那契数列 f[n]=2a*f[n-1]+(b-a^2)*f[n-2]; 之后就可以利用循环节+快速幂求出答案了.
#include <stdio.h>#include <iostream>#include <algorithm>#include <string>#include <string.h>using namespace std;#define ll long long #define F(x,a,b) for (int x=a;x<=b;x++)#define _fast F(i,1,2)F(j,1,2)F(k,1,2) b[i][j]=((b[i][j]+(a[i][k]%MOD)*(a[k][j]%MOD))%MOD+MOD)%MOD;#define _reset F(i,1,2)F(j,1,2) {a[i][j]=b[i][j];b[i][j]=0;}#define _c F(i,1,2)F(j,1,2)F(k,1,2) b[i][j]=((b[i][j]+(a[i][k]%MOD)*(c[k][j]%MOD))%MOD+MOD)%MOD;ll a[4][4],b[4][4],c[4][4];ll f[3];const ll MOD = 1e9+7;void Init(ll aa,ll bb){ a[1][1]=aa;a[1][2]=bb; a[2][1]=1;a[2][2]=0; for (int i=1;i<=2;i++) for (int j=1;j<=2;j++) c[i][j]=a[i][j];}ll multi(ll x,ll y,ll mod){ ll ans=0; while (y) { if (y&1) ans=(ans+x+mod)%mod; x=((x<<1)+mod)%mod; y=y>>1; } return ans;}void fastmat(ll x){ if (x<=1) return; if (x&1) {fastmat(x-1);_c _reset } else { fastmat(x/2); _fast _reset }}ll _f(ll x,ll k, ll mod){ if (!k) return 1; if (k&1) return multi(_f(x,k-1,mod),x,mod); else { ll t=_f(x,k/2,mod); return multi(t,t,mod); }}void out(){ for (int i=1;i<=2;i++) { for (int j=1;j<=2;j++) printf("%I64d ",a[i][j]); printf("\n"); }}int main(){ int T; scanf("%d",&T); while (T--) { ll aa,bb,x,y; scanf("%I64d%I64d%I64d%I64d",&aa,&bb,&x,&y); f[1]=aa*2;f[2]=(aa*aa+bb)*2; ll a1=2*aa,b1=bb-aa*aa; Init(a1,b1); if (b1==0) { ll tt=_f(x,y,MOD-1); printf("%I64d\n",_f(f[1],tt,MOD)); }else { ll mmod=(MOD-1)*(MOD+1); ll tt=_f(x,y,mmod); if (!tt) { fastmat(mmod-2); printf("%I64d\n",(a[1][1]%MOD*f[2]%MOD+a[1][2]%MOD*f[1]%MOD)%MOD); }else { if (tt<=2){printf("%I64d\n",f[tt]%MOD);} else { fastmat(tt-2); printf("%I64d\n",((a[1][1]%MOD)*(f[2]%MOD)+(a[1][2]%MOD)*(f[1]%MOD))%MOD); //out(); } } } }}
- HDU 5674 Function(斐波那契模数列循环节)
- HDU 4794 Arnold 斐波那契数列循环节
- HDU 4794 斐波那契数列循环节
- hdu 1021 斐波那契数列取模(循环节)
- HDU 2814 斐波那契数列的循环节问题
- HDU 3977 Evil teacher(斐波那契数列循环节)
- 斐波那契数列循环实现
- HDU2813Interesting Fibonacci(斐波那契数列+循环节)
- 51nod1195斐波那契数列的循环节
- hdu 3117 斐波那契数列
- hdu 2070 斐波那契数列
- hdu 5018 斐波那契数列
- HDU 2046 斐波那契数列
- hdu 2516 (斐波那契数列)
- M斐波那契数列 HDU
- for循环语句练习题1斐波那契数列
- 利用无穷循环实现斐波那契数列
- 循环-小蜜蜂-斐波那契数列-大数加法
- 2.12.2_编辑贝赛尔曲线
- Linux环境下非root用户安装Python及相关库
- 2.14_图像合成
- Android 数据库,解决Failed to read row 6, column -1 from a CursorWindow which has 11 rows, 14 columns.
- mybatis choose when用法
- HDU 5674 Function(斐波那契模数列循环节)
- 项目解析-spring异步线程池配置与使用
- Java并发编程:Lock(上)
- 2.15.1_通过剪辑区域来擦除图像
- Android Studio Gradle 基础
- 图-最小生成数-prims算法
- Map 和HashMap
- nyoj_257郁闷的c小加(一)
- C# js帮助类