BZOJ5104 Fib數列(二次剩餘 BSGS)

2021-08-14 05:09:16 字數 1419 閱讀 3259

傳送門

題意: 給定a

,求fn

≡a(mod1e

9+9)

,輸出最小的

n 。

題解:

fib數列的通項:fn

=15√

((1+

5√2)

x−1−

5√2)

n)。5

是1e9

+9的一類二次剩餘。可以對其開根號:5≡

3830080162(

mod1e9

+9)

(開根號的方法:

設x=1+5

√2,t=5

√a那麼公式可表達為:xn

−(−1

x)n≡

t(mod1e9

+9)

。設t=

xn,則:t−

(−1)

n1t≡

t(mod1e9

+9)

不妨奇偶討論,兩邊同時乘上

t ,可得:

int n,s,t,t1,t2;

namespace findroot

friend

inline complex operator *(const complex &a,const complex &b)

};inline

bool check(int v)

inline

int power(complex a,int b)

inline

int calc(int k)

}const

int h=400009,bl=sqrt(mod)+1;

struct hash_table

inline

int query(int p)

}h1,h2,h3,h4;

inline

long

long findans(int bz,int rt,hash_table &hh1,hash_table&hh2)else

pw1=1ll*pw1*rt%mod;

}int now1=pw1;long

long pos=bl;

donow1=1ll*now1*pw1%mod;

pos+=bl;

}while(pos<=int_max);

return pos;

}int main()

n=1ll*n*sqrt5%mod;

int rt1=findroot::calc((1ll*n*n+4)%mod);

int rt2=findroot::calc((1ll*n*n-4)%mod);

long

long ans=int_max;

if(rt1)

if(rt2)

printf("%d\n",(ans1));

}

bzoj5104 Fib數列(BSGS 二次剩餘)

快afo了才第一次寫二次剩餘的題 顯然應該將fn寫成通項公式 具體是什麼寫起來不方便而且大家也都知道 設t 1 5 2 n,t 5n,然後可以得到t 1 t t 5n,兩邊同時乘t,移項,得到t2 5nt 1 n 0。分別討論n是奇數或偶數的情況,通過求根公式求t,寫個二次剩餘即可。includeu...

計蒜客 Fib數列問題二

用 fib n 表示斐波那契數列的第 n項,現在要求你求 fib n mod m。fib 1 1,fib 2 1。輸入格式 輸入 2 個整數 n 1 n 10 18 m 2 m 100000000 輸出格式 輸出 fib n 對 m 取模的值。樣例輸入 100000000 100000000 樣例輸...

BZOJ4282 慎二的隨機數列

省選前刷水記錄 構造 很顯然我們要讓那些未知的都加入最終答案 這樣子答案才最大 然後就很簡單了。很奇怪的是本地int main 不加int不會報錯 oj上會ce include include include using namespace std define ll long long int c...