ICPC2017網絡賽(瀋陽)number number number

number number number

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


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
 

Source
 

Recommend
liuyiding
 

矩陣快速冪,通過計算髮現前幾組數據是4,12,33,88等

而Fn數列前幾項爲 0,1,1,2,3,5,8,13,21,34,55,89............發現規律F[2*n+4]-1  就是我們要找的公式



代碼如下:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long ll;
struct Matrix{
    ll matrix[5][5];
};
int n;//矩陣的階數
const int mod=998244353;
void init(Matrix &res)
{
    memset(res.matrix,0,sizeof(res.matrix));
    for(int i=0;i<n;i++)
        res.matrix[i][i]=1;
}
Matrix multiplicative(Matrix a,Matrix b)
{
    Matrix res;
    for(int i = 0 ; i < n ; i++){
        for(int j = 0 ; j < n ; j++){
            res.matrix[i][j]=0;
            for(int k = 0 ; k < n ; k++){
                res.matrix[i][j] = (res.matrix[i][j]%mod+a.matrix[i][k]*b.matrix[k][j]%mod)%mod;
            }
        }
    }
    return res;
}
Matrix pow(Matrix mx,ll m)
{
    Matrix res,base=mx;
    init(res);
    while(m)
    {
        if(m&1)
            res=multiplicative(res,base);
        base=multiplicative(base,base);
        m>>=1;
    }
    return res;
}
int main()
{
    Matrix mx,nx;
    ll m,ans;
    ll k;
    while(~scanf("%lld",&k))
    {
        n=2;
        m=2*k+4;
        mx.matrix[0][0]=mx.matrix[0][1]=mx.matrix[1][0]=1;
        mx.matrix[1][1]=0;
        nx=pow(mx,m-1);
        ans=(nx.matrix[0][0]*0+nx.matrix[0][1]*1)%mod;
        if(ans==0)
            printf("%lld\n",mod-1);
        else
            printf("%lld\n",ans-1);
    }

    return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章