【BZOJ】【4002】【JLOI2015】有意義的字符串


構造線性遞推式+矩陣乘法


  題解戳PoPoQQQ

  

為了自己以后看的方便手打一遍好了>_>

  求$( \frac{b+\sqrt{d}}{2} )^n$的整數部分對p取模后的值

  其中$b\mod 2=1,d\mod 4=1,b^2 \leq d<(b+1)^2,n\leq10^{18}$

 

思路:

構造數列$a_n=b*a_{n-1}+\frac{d-b^2}{4}*a_{n-2}$

其中$a_0=2,a_1=b$

然后我們求出這個數列的通項公式,得到$a_n=(\frac{b+\sqrt{d}}{2})^n+(\frac{b-\sqrt{d}}{2})^n$

因此得到$(\frac{b+\sqrt{d}}{2})^n=a_n-(\frac{b-\sqrt{d}}{2})^n$

由於$b \mod 2=1, d \mod 4=1$,因此$\frac{d-b^2}{4}$一定是個正整數,故我們可以利用矩陣乘法來求出這個數列的第$n$項

然后對於$80\%$的數據$b^2\leq d <(b+1)^2$,對於$20\%$的數據$b=1,d=5$,因此$(\frac{b-\sqrt{d}}{2})^n \in (-1,0]$

故后面那一項對答案有貢獻當且僅當 $d\not=b^2$且$n$為偶數

時間復雜度$O(log_2n)$

 

P.S.話說題目給的提示:題目名有意義的字符串“jxamfe”以及那個奇怪的模數我還是沒明白提示在哪了……>_>我討厭猜字謎QAQ

 1 /**************************************************************
 2     Problem: 4002
 3     User: Tunix
 4     Language: C++
 5     Result: Accepted
 6     Time:48 ms
 7     Memory:1272 kb
 8 ****************************************************************/
 9  
10 //Huce #5 A
11 #include<cmath>
12 #include<vector>
13 #include<cstdio>
14 #include<cstdlib>
15 #include<cstring>
16 #include<iostream>
17 #include<algorithm>
18 #define rep(i,n) for(int i=0;i<n;++i)
19 #define F(i,j,n) for(int i=j;i<=n;++i)
20 #define D(i,j,n) for(int i=j;i>=n;--i)
21 using namespace std;
22 typedef unsigned long long LL;
23 LL getll(){
24     LL v=0,sign=1; char ch=getchar();
25     while(ch<'0'||ch>'9') {if (ch=='-') sign=-1; ch=getchar();}
26     while(ch>='0'&&ch<='9') {v=v*10+ch-'0'; ch=getchar();}
27     return v*sign;
28 }
29 const int N=100010,INF=~0u>>2;
30 const LL MOD=7528443412579576937LL;
31 /*******************tamplate********************/
32 LL b,d,n;
33 struct matrix{
34     LL d[3][3];
35     LL* operator [] (int x){ return d[x];}
36     matrix(int x=0){
37         F(i,0,2) F(j,0,2)
38             if (i==j && i) d[i][j]=x;
39             else d[i][j]=0;
40     }
41 }a;
42 inline LL mul(LL a,LL b){
43     LL r=0;
44     for(;b;b>>=1,a=(a+a)%MOD)
45         if (b&1) r=(r+a)%MOD;
46     return r;
47 }
48 inline matrix operator * (matrix a,matrix b){
49     matrix c;
50     F(i,1,2) F(j,1,2) F(k,1,2) (c[i][j]+=mul(a[i][k],b[k][j]))%=MOD;
51     return c;
52 }
53 inline matrix Pow(matrix a,LL b){
54     matrix r(1);
55     for(;b;b>>=1,a=a*a) if (b&1) r=r*a;
56     return r;
57 }
58 int main(){
59     b=getll(); d=getll(); n=getll();
60     a[1][1]=0; a[1][2]=(d-b*b)/4;
61     a[2][1]=1; a[2][2]=b;
62     a=Pow(a,n);
63     cout<<((a[1][1]*2%MOD+mul(b,a[2][1])-(d!=b*b&&!(n&1)))%MOD+MOD)%MOD<<endl;
64     return 0;
65 }
View Code

4002: [JLOI2015]有意義的字符串

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 54  Solved: 28
[Submit][Status][Discuss]

Description

 B 君有兩個好朋友,他們叫寧寧和冉冉。

有一天,冉冉遇到了一個有趣的題目:輸入 b;d;n,求((b+sqrt(D)/2)^N的整數部分,請輸出結果 Mod 7528443412579576937 之后的結果吧。

Input

一行三個整數 b;d;n

 

Output

 一行一個數表示模 7528443412579576937 之后的結果。

Sample Input

1 5 9

Sample Output

76

HINT

 0 <b^2 < d< (b +1)2 < 10^18。

Source

[ Submit][ Status][ Discuss]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM