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 ...
随机推荐
- Ajax 生成流文件下载 以及复选框的实现
JQuery的ajax函数的返回类型只有xml.text.json.html等类型,没有“流”类型,所以我们要实现ajax下载,不能够使用相应的ajax函数进行文件下载.但可以用js生成一个form, ...
- DC游戏《斑鸠》原创赏析[转载]
游戏背景: 凤来之国本来只是边远地区的一个小国.但现在他们却自称为得到“神之力”的“神通者”,在“选民思想”“和平统一”之类的名义下开始了对各地的武力侵略. 事情的起因是因为凤来之 ...
- java.lang.VerifyError: Inconsistent stackmap frames at branch target
-XX:-UseSplitVerifier解决. 附带网址:http://stackoverflow.com/questions/12774672/java-7-inconsistent-stackm ...
- 为什么每个程序员都应该用Mac OS X?
1.Mac OS X 是基于 Unix 的.这一点太重要了,尤其是对开发人员,至少对于我来说很重要,这意味着Unix 下一堆好用的工具都可以随手捡到.如果你是个 windows 开发人员,我想你会在 ...
- PE框架学习之道:PE框架——style的配置
1.在style.xml中定义style <style id="NumberStyle"> <setting> <param name=&qu ...
- Google启封后依然不能用
门事件周年纪念日过后一段时间,狗狗启封了,但撸主的狗狗仍旧不能用,突然想起来之前帆樯时候改动了某些配置,比方hosts文件,这个文件的路径为:C:\Windows\System32\drivers\e ...
- CSS一些设置用法
今天就简单写点的知识点 1. CSS字体样式小结 1) text-indent :值 实现段落首行缩进功能 (在起初我们编写段落时为了实现首行缩进两个字符时用的是HTML的标签元素&nb ...
- JavaScript 高级程序设计(第3版)笔记——chapter6:面向对象的程序设计
一.创建对象 工厂模式.使用简单的函数创建对象,为对象添加属性和方法,然后返回对象.[问题:没有解决对象识别问题] 1 function createPerson(name, age) { 2 var ...
- js div 内容显示分页
由于工作需要 div固定大小 而内容不定 所以 如果内容过多自然就显示不出来了 所以 需要分页一类的功能下面是代码 <!DOCTYPE html PUBLIC "-//W3C//DTD ...
- SharePoint网站集功能介绍
SharePoint网站集功能介绍 https://support.office.com/zh-cn/article/%E5%90%AF%E7%94%A8%E6%88%96%E7%A6%81%E7%9 ...