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 ...
随机推荐
- python列表元组
python列表元组 索引 切片 追加 删除 长度 循环 包含 定义一个列表 my_list = [] my_list = list() my_list = ['Michael', ' ...
- 设置windows密码只存在NTLM-Hash下
修改注册表 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Lsa 下添加名为"NoLMHash"的DWORD值,并设置为1 ...
- C#手机充值
C#手机充值系统开发(基于聚合数据) 说是手机充值系统有点装了,其实就是调用了聚合数据的支付接口,其实挺简单的事 但是我发现博客园竟然没有类似文章,我就个出头鸟把我的代码贡献出来吧 首先说准备工作: ...
- angularjs学习总结(快速预览版)
对html标签的增强 -> 指令 指令的本质是什么 声明的方式调用相应的脚本,实现一些操作,声明的所在的dom就是脚本的执行上下文? 自定义标签 -- 标签指令自定义属性 -- 属性指令特定格式 ...
- Maven本地
<localRepository></localRepository>
- 一劳永逸让windows 64位操作系统 禁止强制驱动签名
如何让WINDOWS7 64位直接加载“禁用强制驱动程序签名”方式启动 Windows Client 论坛 > Windows 7 问题 0 登录进行投票 因为开发需要,要装一台设备的驱动,但 ...
- 基于Visual C++2013拆解世界五百强面试题--题14-循环删除
有一个数组a[1000]存放0-1000,要求每隔二个数删除一个数,到末尾时循环到开头继续进行,求最后一个被删掉数的原始下标. 看到题目可以用循环链表保存这些数,然后循环删除,大大减少了一些复杂的边界 ...
- Sql语句之select 5种查询
select 5种子句:注意顺序where / group by /having / order by / limit / 清空表中的数据:truncate 表名: 导入表结构(不含数据): crea ...
- GCC编译四阶段
Linux下程序编译的四个阶段:预处理.编译.汇编.链接 GCC: GUN C Compiler 但现在不只局限于c语言 预处理(Preprocessing): gcc -E hello.c - ...
- SSIS: 使用最大ID和最大日期来增量更新表
简单三步: 1. 新增变量 MaxID和MaxCreateDate以及Variable 2. 放置一个 Execute SQL Task ,用SQL 来获取 MaxID和MaxCreateDat ...