2017 ACM/ICPC Asia Regional Shenyang Online:number number number hdu 6198矩阵快速幂

Posted ╰追憶似水年華ぃ╮

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2017 ACM/ICPC Asia Regional Shenyang Online:number number number hdu 6198矩阵快速幂相关的知识,希望对你有一定的参考价值。

 

Problem Description
We define a sequence F:

? F0=0,F1=1;
? Fn=Fn?1+Fn?2 (n2).

Give you an integer k, if a positive number n can be expressed by
n=Fa1+Fa2+...+Fak where 0a1a2?ak, this positive number is mjf?good. Otherwise, this positive number is mjf?bad.
Now, give you an integer k, you task is to find the minimal positive mjf?bad number.
The answer may be too large. Please print the answer modulo 998244353.
 

 

Input
There are about 500 test cases, end up with EOF.
Each test case includes an integer k which is described above. (1k109)
 

 

Output
For each case, output the minimal mjf?bad number mod 998244353.
 

 

Sample Input
1
 

 

Sample Output
4

 

思路:找规律,当k=1时,n=F5-1=4。k=2,n=F7-1=12。k=3,n=F9-1=33。所以大胆推测n=F(2*k+3)-1;再用矩阵快速幂输出F(2n+3)-1。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <vector>
 5 using namespace std;
 6 const int mod = 998244353;
 7 typedef long long LL;
 8 LL n;
 9 typedef vector<LL>vec;
10 typedef vector<vec>mat;
11 mat mul(mat &A, mat &B)
12 {
13     mat C(A.size(), vec(B[0].size()));///分配大小,A的行,B的列
14     for (int i = 0; i<A.size(); i++) ///矩阵A的行
15         for (int k = 0; k<B.size(); k++) ///矩阵B的行
16             for (int j = 0; j<B[0].size(); j++) ///矩阵B的列
17                 C[i][j] = (C[i][j] + A[i][k] * B[k][j] % mod + mod) % mod;
18     return C;
19 }
20 ///计算A^n
21 mat pow(mat A, LL n)
22 {
23     mat B(A.size(), vec(A.size()));///和矩阵A的大小相同
24     for (int i = 0; i<A.size(); i++)
25         B[i][i] = 1;
26     while (n>0)
27     {
28         if (n & 1) B = mul(B, A);
29         A = mul(A, A);
30         n >>= 1;
31     }
32     return B;
33 }
34 void solve()
35 {
36     mat A(2, vec(2));///2*2的矩阵
37     A[0][0] = 1;
38     A[0][1] = 1;
39     A[1][0] = 1;
40     A[1][1] = 0;
41     A = pow(A, n);
42     printf("%d\n", (A[1][0] % mod - 1 + mod) % mod);
43 }
44 int main()
45 {
46     while (~scanf("%lld", &n))
47     {
48         n = 2 * n + 3;
49         solve();
50     }
51 }

 

以上是关于2017 ACM/ICPC Asia Regional Shenyang Online:number number number hdu 6198矩阵快速幂的主要内容,如果未能解决你的问题,请参考以下文章

2017 ACM/ICPC Asia Regional Beijing Online

2017 ACM/ICPC Asia Regional Qingdao Online

2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路

hdu6206 Apple 2017 ACM/ICPC Asia Regional Qingdao Online

HDU 6198(2017 ACM/ICPC Asia Regional Shenyang Online)

HDU - 6215 2017 ACM/ICPC Asia Regional Qingdao Online J - Brute Force Sorting