题意:求Fibonacci的第 n 项。

析:矩阵快速幂,如果不懂请看http://www.cnblogs.com/dwtfukgv/articles/5595078.html

是不是很好懂呢。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <stack>
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10 + 5;
const int mod = 1e9;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
struct Matrx{
LL x[3][3];
void init(){
memset(x, 0, sizeof(x));
}
}; Matrx mul(const Matrx &lhs, const Matrx &rhs){
Matrx c;
c.init();
for(int i = 0; i < 2; ++i){
for(int j = 0; j < 2; ++j){
for(int k = 0; k < 2; ++k){
c.x[i][j] += lhs.x[i][k] * rhs.x[k][j];
c.x[i][j] %= mod;
}
}
}
return c;
} Matrx fast_pow(Matrx x, LL b){
Matrx ans, k = x;
ans.x[0][0] = ans.x[10][0] = ans.x[0][1] = 1;
ans.x[1][1] = 0; while(b){
if(b & 1){
ans = mul(ans, k);
}
b >>= 1;
k = mul(k, k);
} return ans;
} int main(){
Matrx mx;
mx.x[0][0] = mx.x[0][1] = mx.x[1][0] = 1;
mx.x[1][1] = 0;
Matrx nx;
nx.init();
nx.x[0][0] = 1; nx.x[0][1] = 0;
int T; cin >> T;
while(T--){
LL n;
scanf("%d %lld", &m, &n);
printf("%d ", m);
if(1LL == n){ printf("1\n"); continue; }
else if(2LL == n){ printf("1\n"); continue; }
Matrx ans = fast_pow(mx, n-2);
ans = mul(ans, nx);
printf("%lld\n", ans.x[0][0]);
}
return 0;
}

  

这也是运用矩阵快速幂的思想写的。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <stack>
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100 + 5;
const int mod = 1e9;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
map<LL, LL> ans; LL dfs(LL n){
if(ans.count(n)) return ans[n];
LL k = n/2LL;
if(n % 2 == 0) return ans[n] = (dfs(k) * dfs(k) + dfs(k-1)* dfs(k-1)) % mod;
else return ans[n] = (dfs(k) * dfs(k+1) + dfs(k-1) * dfs(k)) % mod;
} int main(){
ans[0] = ans[1] = 1;
int T; cin >> T;
while(T--){
LL x;
scanf("%d %lld", &m, &x);
printf("%d %lld\n", m, dfs(x-1));
}
return 0;
}

  

最新文章

  1. kafka 集群安装与安装测试
  2. 直观友好的单个memcache监控工具:phpmemcache.php
  3. Zip it
  4. [转]LUA元表
  5. 挖掘机技术哪家强(c++实现)
  6. 基于Qt Phonon模块实现音乐播放器
  7. div整体布局分析
  8. CDZSC_2015寒假新人(1)——基础 d
  9. 解决VS2010批量替换时经常由于内存较低而导致VS2010自动关闭的问题
  10. Got minus one from a read call异常
  11. inux的进程-进程的概念和fork创建进程
  12. Valgrind检测内存泄露简介
  13. Nginx HTTP框架提供的其它变量
  14. TopN案例
  15. NIO原理及案例使用
  16. TerminateProcess的使用问题
  17. Java中的枚举Enum
  18. AspectJ框架基于注解的AOP实现
  19. ZeroMQ安装说明
  20. 中文分词器ICTCLAS使用方法(Java)

热门文章

  1. android webview js alert对话框 不能弹出 解决办法
  2. javascript实现继承的一种方式
  3. liux之我用过的zip解压命令
  4. Meta标签详解(转)
  5. java classpath、path用法
  6. WEXT driver的执行过程实现(iwpriv部分/softapcontroller)
  7. .Net刷新页面的小结
  8. 只用css实现“每列四行,加载完一列后数据自动填充到下一列”的效果
  9. 回调函数、Java接口回调 总结
  10. Fragment怎么实现TabHost