2014 Super Training #7 F Power of Fibonacci --数学+逆元+快速幂
生活随笔
收集整理的這篇文章主要介紹了
2014 Super Training #7 F Power of Fibonacci --数学+逆元+快速幂
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原題:ZOJ 3774 ?http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3774
----------------------------------------------------------------------------------------------------------------------
這題比較復雜,看這篇比較詳細:http://blog.csdn.net/acdreamers/article/details/23039571
結論就是計算:
充分利用了快速冪及求逆元。
代碼:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #define Mod 1000000009 #define ll long long using namespace std; #define N 100007ll fac[N],A[N],B[N];void init() {int i;fac[0] = 1;for(i=1;i<N;i++)fac[i] = fac[i-1]*i%Mod;A[0] = B[0] = 1;for(i=1;i<N;i++){A[i] = A[i-1]*691504013 % Mod;B[i] = B[i-1]*308495997 % Mod;} }ll fastm(ll n,ll k,ll MOD) {ll res = 1LL;n %= MOD;while(k){if(k&1LL)res = (res*n)%MOD;k >>= 1;n = n*n%MOD;}return res; }ll Inv(ll n,ll MOD) {return fastm(n,MOD-2,MOD); }int main() {int cs;ll n,k;init();ll ans,r;scanf("%d",&cs);while(cs--){scanf("%lld%lld",&n,&k);ans = 0;for(r=0;r<=k;r++){ll t = A[k-r]*B[r] % Mod;ll x = fac[k]; // k!ll y = fac[k-r]*fac[r] % Mod; // (k-r)!*(r)!ll c = x*Inv(y,Mod) % Mod; // c = C(k,r) = x/y = x*Inv(y)ll tmp = t*(fastm(t,n,Mod)-1)%Mod*Inv(t-1,Mod)%Mod; //t(t^n-1)/(t-1) = t(t^n-1)*Inv(t-1)if(t == 1)tmp = n%Mod;tmp = tmp*c%Mod;if(r&1LL) // (-1)^rans -= tmp;elseans += tmp;ans %= Mod;}//ans = ans*(1/sqrt(5))^kll m = Inv(383008016,Mod)%Mod;ans = ans*fastm(m,k,Mod)%Mod;ans = (ans%Mod+Mod)%Mod;printf("%lld\n",ans);}return 0; } View Code?
轉載于:https://www.cnblogs.com/whatbeg/p/3836551.html
總結
以上是生活随笔為你收集整理的2014 Super Training #7 F Power of Fibonacci --数学+逆元+快速幂的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux Versus Windows
- 下一篇: 有旅游相关专业的人不,付费求解说词