您的位置:首页 > 其它

51nod 1358 浮点型矩阵快速幂(板子

2017-07-13 22:46 309 查看
1358 浮波那契


基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题


 收藏


 关注

TengBieBie已经学习了很多关于斐波那切数列的性质,所以他感到一些些厌烦。现在他遇到了一个新的数列,这个数列叫做Float-Bonacci。这里有一个关于Float-Bonacci的定义。



对于一个具体的n,TengBieBie想要快速计算FB(n).

但是TengBieBie对FB的了解非常少,所以他向你求助。

你的任务是计算FB(n).FB(n)可能非常大,请输出FB(n)%1,000,000,007 (1e9+7)即可。

Input
输入共一行,在一行中给出一个整数n (1<=n<=1,000,000,000)。


Output
对于每一个n,在一行中输出FB(n)%1,000,000,007 (1e9+7)。


Input示例
5


Output示例
2

G(n)=G(n-10)+G(n-34)

//china no.1
#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
using namespace std;

#define pi acos(-1)
#define endl '\n'
#define rand() srand(time(0));
#define me(x) memset(x,0,sizeof(x));
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define close() ios::sync_with_stdio(0); cin.tie(0);
typedef long long LL;
const int INF=0x3f3f3f3f;
const LL LINF=0x3f3f3f3f3f3f3f3fLL;
const int dx[]={-1,0,1,0,-1,-1,1,1};
const int dy[]={0,1,0,-1,1,-1,1,-1};
const int maxn=1e3+5;
const int maxx=1e5+100;
const double EPS=1e-7;
const int MOD=10000007;
#define mod(x) ((x)%MOD);
template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}
template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}
template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}
template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}
//typedef tree<pt,null_type,less< pt >,rb_tree_tag,tree_order_statistics_node_update> rbtree;
long long gcd(long long a , long long b){if(b==0) return a;a%=b;return gcd(b,a);}
#define FOR(x,n,i) for(int i=x;i<=n;i++)
#define FOr(x,n,i) for(int i=x;i<n;i++)
#define W while

inline int Scan()
{
int res=0,ch,flag=0;
if((ch=getchar())=='-')flag=1;
else if(ch>='0' && ch<='9')res=ch-'0';
while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-'0';
return flag ? -res : res;
}

LL a[maxx],bb[maxx],c[maxx],n,k,mod=1e9+7,t;
int ssize = 34;

struct Matrix
{
LL  m[35][35];
void init(){
memset(m, 0, sizeof m);
}
void setOne()
{
init();
for(int i=1;i<=ssize;i++) m[i][i]=1;
}
void unit()
{
m[1][10]=m[1][34] = 1;
for(int i=1;i<=33;i++)
m[i+1][i] = 1;
}
void print()
{
for(int i=1;i<=ssize;i++)
{
for(int j=1;j<=ssize;j++)
cout << m[i][j] << " ";
cout << endl;
}
cout << endl;
}

} I,A,B,T,b,res;

Matrix Mul(Matrix a,Matrix b)  //
{
int i,j,k;
Matrix c;
for(int i=1;i<=ssize;i++)
{
for(int j=1;j<=ssize;j++)
{
c.m[i][j]=0;
for(int k=1;k<=ssize;k++)
{
c.m[i][j]+=(a.m[i][k]*b.m[k][j]);
c.m[i][j]%=mod;
}
}
}
return c;
}

void quickPow(LL n)
{
while(n)
{
if(n&1) res=Mul(res,b);
n>>=1;
b=Mul(b,b);
}
}
int main()
{
res.init();
res.setOne();
//res.print();
cin>>n;
if(n<=4)
{
puts("1");
return 0;
}
n-=4;
n*=10;
b.unit();
quickPow(n);
LL ans=0;
//res.print();
FOR(1,34,i)
ans+=res.m[1][i];
cout<<ans%mod<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: