- number number number
Time Limit: 2000/1000 MS (Java/Others) Memory
Limit: 32768/32768 K (Java/Others) Total Submission(s): 637Accepted Submission(s): 407
Problem Description We define a sequence F:? F0=0,F1=1; ? Fn=Fn?1+Fn?2 (n≥2).
Give you an integer k, if a positive number n can be expressed by
n=Fa1+Fa2+…+Fak where 0≤a1≤a2≤?≤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 moduloInput There are about 500 test cases, end up with EOF. Each test case
includes an integer k which is described above. (1≤k≤109)Output For each case, output the minimal mjf?bad number mod
Sample Input
1
Sample Output
4
Source 2017 ACM/ICPC Asia Regional Shenyang Online前提:矩阵快速幂+斐波那契数列推荐题目(poj3070(斐波那契的矩阵表示))
思路:
* 题目要求找bad number,枚举钱四项分别是 4,12,33,88,斐波那契数列为0 , 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89*
对应的下标分别是0,1,2,3,4,5,6,7,8,9,10,11
第一个bad number: 4=f4+f3-1;
第二个bad number :12=f5+f6-1;
第三个bad number: 33 =f7+f8-1;
* 因而推导出一下公式: a(n)=f(2*n+2)+f(2*n+1)-1*
代码如下:
#include <bits/stdc++.h>
using namespace std;
const int mod =998244353;
struct matrix
{long long int a[2][2];
};
matrix multply(matrix x,matrix y)
{matrix result ;memset(result.a,0,sizeof(result.a));int i,j,k;for (i=0;i<2;i++)for (j=0;j<2;j++)for (k=0;k<2;k++){result.a[i][j]+=x.a[i][k]*y.a[k][j];if(result.a[i][j]>=mod)result.a[i][j]%=mod;}return result;
}
matrix pow_mod (matrix a,int n)
{matrix result;memset(result.a,0,sizeof(result.a));for (int i=0;i<2;i++)result.a[i][i]=1;while (n){if(n&1)result=multply(result,a);a=multply(a,a);n>>=1;}return result ;}int main ()
{long long int n;while (cin>>n){n=2*n+1;long long int ans;matrix a;a.a[0][0]=a.a[0][1]=a.a[1][0]=1;a.a[1][1]=0;a=pow_mod(a,n);ans =a.a[0][0]+a.a[0][1]-1;
// int i,j;
// for (i=0;i<2;i++)
// for (j=0;j<2;j++)
// if(!j)
// cout<<a.a[i][j]<<" ";
// else
// cout <<a.a[i][j]<<endl;if(ans>=mod)ans%=mod;cout<<ans<<endl;}return 0;
}