2017 ACM/ICPC Asia Regional Shenyang Online 1005 number number number(矩阵快速幂)

来源:互联网 发布:淘宝做什么类目好 编辑:程序博客网 时间:2024/06/02 05:57

原题:

number number number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0


Problem Description
We define a sequence F:

 F0=0,F1=1;
 Fn=Fn1+Fn2 (n2).

Give you an integer k, if a positive number n can be expressed by
n=Fa1+Fa2+...+Fak where 0a1a2ak, this positive number is mjfgood. Otherwise, this positive number is mjfbad.
Now, give you an integer k, you task is to find the minimal positive mjfbad 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 mjfbad number mod 998244353.
 

Sample Input
1
 

Sample Output
4


#include <iostream>#include <iomanip>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <deque>#include <stack>#include <string>#include <cmath>#include <vector>#include <utility>#include <set>#include <map>#include <sstream>#include <climits>//#pragma comment(linker, "/STACK:1024000000,1024000000")#define pi acos(-1.0)#define INF 2147483647using namespace std;typedef long long LL;typedef pair<int,int > P;const int MOD=998244353;struct Matrix{    LL a[100][100];    int r, c;} mat1,mat2;Matrix multi(Matrix x, Matrix y){    Matrix z;    memset(z.a, 0, sizeof(z.a));    z.r = x.r, z.c = y.c;    for(int i = 1; i <= x.r; i++)    {        for(int k = 1; k <= x.c; k++)        {            if(x.a[i][k] == 0)                continue;            for(int j = 1; j<= y.c; j++)                z.a[i][j] = (z.a[i][j] + (x.a[i][k] * y.a[k][j]) % MOD) % MOD;        }    }    return z;}int Matrix_mod(int n){    while(n)    {        if(n & 1)            mat2 = multi(mat1, mat2);        mat1 = multi(mat1, mat1);        n >>= 1;    }    return mat2.a[1][2] % MOD;}int main(){    LL N;    while(scanf("%lld", &N)!=EOF)    {        memset(mat2.a, 0, sizeof(mat2.a));        mat2.r = 2, mat2.c = 2;        for(int i = 1; i <= 2; i++)            mat2.a[i][i] = 1;        mat1.r = 2,mat1.c = 2;        mat1.a[1][1] = mat1.a[1][2] = mat1.a[2][1] = 1;        mat1.a[2][2] = 0;        cout<<Matrix_mod(5+(N-1)*2)-1<<endl;    }    return 0;}


阅读全文
1 0
原创粉丝点击