POJ 3047 Fibonacci
DEBUG很辛苦,且行, 且珍惜
原代码:
ans[0][0] = (ans[0][0] * a[flag][0][0] + ans[0][1] * a[flag][1][0]) % 10000;
ans[0][1] = (ans[0][0] * a[flag][0][1] + ans[0][1] * a[flag][1][1]) % 10000;
ans[1][0] = (ans[1][0] * a[flag][0][0] + ans[1][1] * a[flag][1][0]) % 10000;
ans[1][1] = (ans[1][0] * a[flag][0][1] + ans[1][1] * a[flag][1][1]) % 10000;
问题在于:修改后ans[][]的值再次调用,就不是原来的值了,会导致程序出错
QAQ
改进后:
ans[0][0] = (temp_1 * a[flag][0][0] + temp_2 * a[flag][1][0]) % 10000;
ans[0][1] = (temp_1 * a[flag][0][1] + temp_2 * a[flag][1][1]) % 10000;
ans[1][0] = (temp_3 * a[flag][0][0] + temp_4 * a[flag][1][0]) % 10000;
ans[1][1] = (temp_3 * a[flag][0][1] + temp_4 * a[flag][1][1]) % 10000;
算法思路:利用快速幂,实现大数范围的快速计算,不会超时
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm> using namespace std;
const int INF = 0x3f3f3f3f; int a[][][];
void test_print(){
for(int i = ; i < ; ++i){
printf("i = %d\n",i);
printf("%d\n\n",a[i][][]);
}
}
int main(){
int i, j, k;
int n;
a[][][] = ;
a[][][] = ;
a[][][] = ;
a[][][] = ;// n = 1 for(i = ; i < ; ++i){
a[i][][] = (a[i-][][] * a[i-][][] + a[i-][][] * a[i-][][]) % ;
a[i][][] = (a[i-][][] * a[i-][][] + a[i-][][] * a[i-][][]) % ;
a[i][][] = (a[i-][][] * a[i-][][] + a[i-][][] * a[i-][][]) % ;
a[i][][] = (a[i-][][] * a[i-][][] + a[i-][][] * a[i-][][]) % ;
}
// test_print();
while(EOF != scanf("%d",&n)){
if(- == n) break;
else if( == n){
printf("0\n");
continue;
}
int count = ;
int flag;
int ans[][];
int array[];
int flag_t = ;
bool init_ok = false;
memset(array, , sizeof(array));
while(n){
if(n % == ){
n /= ;
array[flag_t] = ;
} else{
n = (n - ) / ;
array[flag_t] = ;
}
++flag_t;
}
//for(i = 0; i < flag_t / 2; ++i) swap(array[i], array[flag_t - i -1 ]);
for(i = ; i < flag_t; ++i){
if(!array[i]) continue;
int flag = i;
if(!init_ok){
ans[][] = a[i][][];
ans[][] = a[i][][];
ans[][] = a[i][][];
ans[][] = a[i][][];
init_ok = true;
continue;
}
int temp_1 = ans[][];
int temp_2 = ans[][];
int temp_3 = ans[][];
int temp_4 = ans[][];
ans[][] = (temp_1 * a[flag][][] + temp_2 * a[flag][][]) % ;
ans[][] = (temp_1 * a[flag][][] + temp_2 * a[flag][][]) % ;
ans[][] = (temp_3 * a[flag][][] + temp_4 * a[flag][][]) % ;
ans[][] = (temp_3 * a[flag][][] + temp_4 * a[flag][][]) % ;
}
printf("%d\n",ans[][]);
}
return ;
}
POJ 3047 Fibonacci的更多相关文章
- 矩阵快速幂 POJ 3070 Fibonacci
题目传送门 /* 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 */ #include <cstdio> #include <algori ...
- POJ 3047 Bovine Birthday 日期定周求 泽勒公式
标题来源:POJ 3047 Bovine Birthday 意甲冠军:.. . 思考:式 适合于1582年(中国明朝万历十年)10月15日之后的情形 公式 w = y + y/4 + c/4 - 2* ...
- POJ 3070 Fibonacci
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- 矩阵经典题目六:poj 3070 Fibonacci
http://poj.org/problem?id=3070 按已构造好的矩阵,那么该矩阵的n次方的右上角的数便是f[n]. #include <stdio.h> #include < ...
- POJ 3070 Fibonacci(矩阵高速功率)
职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...
- poj 3070 Fibonacci (矩阵快速幂乘/模板)
题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...
- poj 3070 Fibonacci 矩阵快速幂
Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...
- POJ 3070 Fibonacci 【矩阵快速幂】
<题目链接> Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...
- poj 3070 Fibonacci 矩阵相乘
Fibonacci Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7715 Accepted: 5474 Descrip ...
随机推荐
- 指针和引用区别 C++
#include <iostream> using namespace std; int main(){ ; void cubeByPoint(int *);//指针传参声明 void c ...
- .net mvc Authorization Filter,Exception Filter与Action Filter
一:知识点部分 权限是做网页经常要涉及到的一个知识点,在使用MVC做权限设计时需要先了解以下知识: MVC中Url的执行是按照Controller->Action->View页面,但是我们 ...
- 帝国cms数据表详细说明
表 名 解释 phome_ecms_infoclass_news 新闻采集规则记录表 phome_ecms_infotmp_news 采集临时表 phome_ecms_news 新闻主数据记录表 ph ...
- Delphi中取整函数Round的Bug解决
Delphi中 Round函数有个Bug一旦参数是形如 XXX.5这样的数时如果 XXX 是奇数 那么就会 Round up如果 XXX 是偶数 那么就会 Round down例如 Round(17. ...
- 调色板QPalette类用法详解(附实例、源码)(很清楚:窗口背景色 前景色 按钮的颜色 按钮文本的颜色 )
http://blog.csdn.net/rl529014/article/details/51589096
- C#中WebClient使用DownloadString中文乱码的解决办法
原文:C#中WebClient中文乱码的解决办法 第一次尝试: string question = textBox1.Text.ToString(); WebClient client= new We ...
- 数据结构- 串的模式匹配算法:BF和 KMP算法
数据结构- 串的模式匹配算法:BF和 KMP算法 Brute-Force算法的思想 1.BF(Brute-Force)算法 Brute-Force算法的基本思想是: 1) 从目标串s 的第一个字 ...
- BZOJ 2761 不重复数字 (Hash)
题解:直接使用STL中的hash去重即可 #include <cstdio> #include <map> using namespace std; int ans[50010 ...
- 带你轻松玩转Git--图解三区结构
在上篇文章的结尾我们提到了Git 的三区结构,在版本控制体系中有这样两种体系结构,一种是两区结构一种是三区结构.接下来我们通过对Git三区的结构学习来帮助我们更好的去理解并运用Git. 两区结构是其他 ...
- IE浏览器下<A>标签不能显示背景图片
修改前 background: url('./img/active/legendBg.png')no-repeat 修改后 background: url('./img/active/legendBg ...