日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

广义Fibonacci数列找循环节

發布時間:2024/4/11 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 广义Fibonacci数列找循环节 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

今天將來學習如何求廣義Fibonacci數列的循環節。

?

問題:給定,滿足,求的循

?????環節長度。

?

來源:http://acdreamoj.sinaapp.com/?1075題

?

分析:我們知道矩陣的遞推關系如下

?

????

?

?????然后繼續有

?

????

?

???? 那么,現在的問題就轉化為求最小的,使得

?

?????

?

???? 所以我們可以先找出符合條件的一個,然后枚舉它的因子,找最小的。

?

?????

?

?????為了好解決問題,我們需要對矩陣進行相似對角化,即,我們先來求的特征值。

?

????

????

?????解得的特征值為

?

????

?

?????也就是說的相似對角矩陣為

?

????

?

? ? ? 因為我們知道,所以當時,,?由于

??

?????

?

??????繼續得到

?

???????

?

??????設,那么分情況討論:

?

?????? (1)是模的二次剩余,由費馬小定理得時,

?

???????(2)是模的二次非剩余,則有

?

???????????

?

?????????? 根據歐拉準則有

?

??????????

?

?????????? 那么繼續得到

?

???????????

?

???????????然后由費馬小定理有,同理有

?

?????????? 所以,當時,

?

?????? (3)時,由于不存在,所以無法完成相似對角化,好在這種情況不存在。

????

?

??????所以綜上所述:

???

??????是模的二次剩余時,枚舉的因子

????? 是模的二次非剩余時,枚舉的因子

?

????? 找最小的因子,使得

?

?????

?

????? 成立。

?

代碼:

#include <iostream> #include <string.h> #include <algorithm> #include <stdio.h> #include <math.h>using namespace std; typedef long long LL; const int N = 2; const LL MOD = 1000000007;LL fac[2][505]; int cnt,ct;LL pri[6] = {2, 3, 7, 109, 167, 500000003}; LL num[6] = {4, 2, 1, 2, 1, 1};struct Matrix {LL m[N][N]; } ;Matrix A; Matrix I = {1, 0, 0, 1};Matrix multi(Matrix a,Matrix b) {Matrix c;for(int i=0; i<N; i++){for(int j=0; j<N; j++){c.m[i][j] =0;for(int k=0; k<N; k++){c.m[i][j] += a.m[i][k] * b.m[k][j];c.m[i][j] %= MOD;}}}return c; }Matrix power(Matrix A,LL n) {Matrix ans = I, p = A;while(n){if(n & 1){ans = multi(ans,p);n--;}n >>= 1;p = multi(p,p);}return ans; }LL quick_mod(LL a,LL b) {LL ans = 1;a %= MOD;while(b){if(b & 1){ans = ans * a % MOD;b--;}b >>= 1;a = a * a % MOD;}return ans; }LL Legendre(LL a,LL p) {LL t = quick_mod(a,(p-1)>>1);if(t == 1) return 1;return -1; }void dfs(int dept,LL product = 1) {if(dept == cnt){fac[1][ct++] = product;return;}for(int i=0; i<=num[dept]; i++){dfs(dept+1,product);product *= pri[dept];} }bool OK(Matrix A,LL n) {Matrix ans = power(A,n);return ans.m[0][0] == 1 && ans.m[0][1] == 0 &&ans.m[1][0] == 0 && ans.m[1][1] == 1; }int main() {fac[0][0] = 1;fac[0][1] = 2;fac[0][2] = 500000003;fac[0][3] = 1000000006;LL a,b,c,d;while(cin>>a>>b>>c>>d){LL t = a * a + 4 * b;A.m[0][0] = a;A.m[0][1] = b;A.m[1][0] = 1;A.m[1][1] = 0;if(Legendre(t,MOD) == 1){for(int i=0; i<4; i++){if(OK(A,fac[0][i])){cout<<fac[0][i]<<endl;break;}}}else{ct = 0;cnt = 6;dfs(0,1);sort(fac[1],fac[1]+ct);for(int i=0;i<ct;i++){if(OK(A,fac[1][i])){cout<<fac[1][i]<<endl;break;}}}}return 0; }

?

?

?

?

總結

以上是生活随笔為你收集整理的广义Fibonacci数列找循环节的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。