Recursive sequence HDU - 5950

Recursive sequence

HDU - 5950

题意:求 f(n) = f(n?1)+2*f(n?2)+n4,其中 f(1)=a,f(2)=b

利用矩阵加速~

比较坑的是mod=2147493647。。。。不是2147483647,用int WA了多次=_=||

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 const ll mod=2147493647;
 5 const int maxn=7;
 6
 7 struct Matrix{
 8     ll n,m[maxn][maxn];
 9
10     void init(ll sz){n=sz;memset(m,0,sizeof(m));}
11     Matrix(ll sz){init(sz);}
12     void set_I(){
13         for(int i=0;i<n;i++) m[i][i]=1;
14     }
15     Matrix operator* (Matrix a){
16         Matrix ans(n);
17         for(ll k=0;k<n;k++)
18         for(ll j=0;j<n;j++)
19         for(ll i=0;i<n;i++){
20             ans.m[i][j]=(ans.m[i][j]+m[i][k]*a.m[k][j]%mod)%mod;
21         }
22         return ans;
23     }
24 };
25
26
27 int main(){
28     int t;
29     scanf("%d",&t);
30     Matrix a(7);
31     a.set_I();
32     a.m[0][1]=2;a.m[0][2]=1;a.m[0][3]=4;
33     a.m[0][4]=6;a.m[0][5]=4;a.m[0][6]=1;
34     a.m[1][0]=1;a.m[1][1]=0;
35     a.m[2][3]=4;a.m[2][4]=6;a.m[2][5]=4;a.m[2][6]=1;
36     a.m[3][4]=3;a.m[3][5]=3;a.m[3][6]=1;
37     a.m[4][5]=2;a.m[4][6]=1;
38     a.m[5][6]=1;
39     while(t--){
40         ll n,s,b;
41         scanf("%lld%lld%lld",&n,&s,&b);
42         s%=mod;
43         b%=mod;
44         if(n==1){
45             printf("%lld\n",s);
46             continue;
47         }
48         if(n==2){
49             printf("%lld\n",b);
50             continue;
51         }
52         Matrix ans(7),p(7);
53         ans.set_I();
54         p=a;
55         n=n-2;
56         while(n){
57             if(n&1) ans=ans*p;
58             n>>=1;
59             p=p*p;
60         }
61         ll res=0;
62         res=b*ans.m[0][0]%mod;
63         res=(res+s*ans.m[0][1]%mod)%mod;
64         res=(res+ans.m[0][2]*16%mod)%mod;
65         res=(res+ans.m[0][3]*8%mod)%mod;
66         res=(res+ans.m[0][4]*4%mod)%mod;
67         res=(res+ans.m[0][5]*2%mod)%mod;
68         res=(res+ans.m[0][6])%mod;
69         printf("%lld\n",res);
70     }
71     return 0;
72 }

时间: 2024-12-29 11:28:09

Recursive sequence HDU - 5950的相关文章

Recursive sequence HDU - 5950 (递推 矩阵快速幂优化)

题目链接 F[1] = a, F[2] = b, F[i] = 2 * F[i-2] + F[i-1] + i ^ 4, (i >= 3) 现在要求F[N] 类似于斐波那契数列的递推式子吧, 但是N最大能到int的最大值, 直接循环推解不了 所以就得用矩阵快速幂咯 现在就看转移矩阵长什么样了 Mi表示要求的矩阵 转移矩阵用A表示 A * Mi = Mi+1 矩阵Mi里面至少得有 F[i-1] F[i-2] i ^ 4 Mi+1就相应的有 F[i] F[i-1] (i+1)^4 (i+1)^4 =

HDU 5950 Recursive sequence 【递推+矩阵快速幂】 (2016ACM/ICPC亚洲区沈阳站)

Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 249    Accepted Submission(s): 140 Problem Description Farmer John likes to play mathematics games with his N cows. Recently, t

HDu 5950 Recursive sequence(矩阵快速幂)

Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1323    Accepted Submission(s): 589 Problem Description Farmer John likes to play mathematics games with his N cows. Recently,

hdu 5950 Recursive sequence

Recursive sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 5120    Accepted Submission(s): 2197 Problem Description Farmer John likes to play mathematics games with his N cows. Recently,

HDU 4914 Linear recursive sequence(矩阵乘法递推的优化)

题解见X姐的论文 矩阵乘法递推的优化,只是mark一下.. HDU 4914 Linear recursive sequence(矩阵乘法递推的优化)

Minimum Integer sequence HDU - 3522(扩展KMP)

Minimum Integer sequence HDU - 3522 题意: 几行代码看了一个多小时!!吐血!! 明天再来补题~ 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn=100010; 4 char s[maxn],t[maxn]; 5 int lens,lent; 6 int nex[maxn],ex[maxn]; 7 int best; 8 9 void getnex(char* t){ 10

Wow! Such Sequence! HDU多校联合赛第三场1007

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Problem Description Recently, Doge got a funny birthday present from his new friend, Protein Tiger from St. Beeze College. No, not cactuses. It's

Codeforces 536F Lunar New Year and a Recursive Sequence | BSGS/exgcd/矩阵乘法

我诈尸啦! 高三退役选手好不容易抛弃天利和金考卷打场CF,结果打得和shi一样--还因为queue太长而unrated了!一个学期不敲代码实在是忘干净了-- 没分该没分,考题还是要订正的 =v= 欢迎阅读本题解! P.S. 这几个算法我是一个也想不起来了 TAT 题目链接 Codeforces 536F Lunar New Year and a Recursive Sequence 新年和递推数列 题意描述 某数列\(\{f_i\}\)递推公式:\[f_i = (\prod_{j=1}^kf_{

HDU 5950 - Recursive sequence - [矩阵快速幂加速递推][2016ACM/ICPC亚洲区沈阳站 Problem C]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5950 Farmer John likes to play mathematics games with his N cows. Recently, they are attracted by recursive sequences. In each turn, the cows would stand in a line, while John writes two positive numbers