2017 icpc 沈阳赛区 1005.number number number(矩阵快速幂)

来源:互联网 发布:淘宝的隐藏优惠券插件 编辑:程序博客网 时间:2024/06/05 14:46



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

 【AC代码】

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>#include<stack>#include<queue>#include<deque>#include<set>#include<map>#include<cmath>#include<vector>using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<int, int> PII;#define pi acos(-1.0)#define eps 1e-10#define pf printf#define sf scanf#define lson rt<<1,l,m#define rson rt<<1|1,m+1,r#define e tree[rt]#define _s second#define _f first#define all(x) (x).begin,(x).end#define mem(i,a) memset(i,a,sizeof i)#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)#define mi ((l+r)>>1)#define sqr(x) ((x)*(x))const int inf=0x3f3f3f3f;const ll mod=998244353;ll ans[3][3],p[3][3];ll m,q;void multi(ll a[][3],ll b[][3]){    ll tmp[3][3];    mem(tmp,0);    for1(i,2)        for1(j,2)            for1(k,2)                tmp[i][j]=(tmp[i][j]+a[i][k]*b[k][j])%mod;    for1(i,2)        for1(j,2)            a[i][j]=tmp[i][j];}void init(){    mem(ans,0);    ans[1][1]=ans[2][2]=1;    p[1][1]=p[1][2]=p[2][1]=1,p[2][2]=0;}void quick(ll x){    while(x)    {        if(x&1)multi(ans,p);        multi(p,p);        x>>=1;    }}int main(){    while(~sf("%lld",&m))    {        m=2+m*2;        init();        quick(m);        //pf("%lld ")        q=(ans[1][1]-1+mod)%mod;        pf("%lld\n",q);    }    return 0;}


阅读全文
1 0