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;
算法思路:利用快速幂,实现大数范围的快速计算,不会超时
1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <iostream> 5 #include <algorithm> 6 7 using namespace std; 8 const int INF = 0x3f3f3f3f; 9 10 int a[40][2][2]; 11 void test_print(){ 12 for(int i = 0; i < 40; ++i){ 13 printf("i = %d\n",i); 14 printf("%d\n\n",a[i][0][1]); 15 } 16 } 17 int main(){ 18 int i, j, k; 19 int n; 20 a[0][0][0] = 1; 21 a[0][0][1] = 1; 22 a[0][1][0] = 1; 23 a[0][1][1] = 0;// n = 1 24 25 for(i = 1; i < 40; ++i){ 26 a[i][0][0] = (a[i-1][0][0] * a[i-1][0][0] + a[i-1][0][1] * a[i-1][1][0]) % 10000; 27 a[i][0][1] = (a[i-1][0][0] * a[i-1][0][1] + a[i-1][0][1] * a[i-1][1][1]) % 10000; 28 a[i][1][0] = (a[i-1][1][0] * a[i-1][0][0] + a[i-1][1][1] * a[i-1][1][0]) % 10000; 29 a[i][1][1] = (a[i-1][1][0] * a[i-1][0][1] + a[i-1][1][1] * a[i-1][1][1]) % 10000; 30 } 31 // test_print(); 32 while(EOF != scanf("%d",&n)){ 33 if(-1 == n) break; 34 else if(0 == n){ 35 printf("0\n"); 36 continue; 37 } 38 int count = 0; 39 int flag; 40 int ans[2][2]; 41 int array[65]; 42 int flag_t = 0; 43 bool init_ok = false; 44 memset(array, 0, sizeof(array)); 45 while(n){ 46 if(n % 2 == 0){ 47 n /= 2; 48 array[flag_t] = 0; 49 } else{ 50 n = (n - 1) / 2; 51 array[flag_t] = 1; 52 } 53 ++flag_t; 54 } 55 //for(i = 0; i < flag_t / 2; ++i) swap(array[i], array[flag_t - i -1 ]); 56 for(i = 0; i < flag_t; ++i){ 57 if(!array[i]) continue; 58 int flag = i; 59 if(!init_ok){ 60 ans[0][0] = a[i][0][0]; 61 ans[0][1] = a[i][0][1]; 62 ans[1][0] = a[i][1][0]; 63 ans[1][1] = a[i][1][1]; 64 init_ok = true; 65 continue; 66 } 67 int temp_1 = ans[0][0]; 68 int temp_2 = ans[0][1]; 69 int temp_3 = ans[1][0]; 70 int temp_4 = ans[1][1]; 71 ans[0][0] = (temp_1 * a[flag][0][0] + temp_2 * a[flag][1][0]) % 10000; 72 ans[0][1] = (temp_1 * a[flag][0][1] + temp_2 * a[flag][1][1]) % 10000; 73 ans[1][0] = (temp_3 * a[flag][0][0] + temp_4 * a[flag][1][0]) % 10000; 74 ans[1][1] = (temp_3 * a[flag][0][1] + temp_4 * a[flag][1][1]) % 10000; 75 } 76 printf("%d\n",ans[0][1]); 77 } 78 return 0; 79 }
转载于:https://www.cnblogs.com/wushuaiyi/p/3873324.html
POJ 3047 Fibonacci相关推荐
- 矩阵快速幂 POJ 3070 Fibonacci
题目传送门 1 /* 2 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 3 */ 4 #include <cstdio> 5 #include ...
- POJ 3070 Fibonacci(矩阵快速幂入门、模板)
? 题目链接:http://poj.org/problem?id=3070 ? 这题就是让求斐波那契数列的第n项,但是题目中n很大,所以打表和直接求都会TLE,对于这个题我们可以用矩阵快速幂,下面 ...
- 矩阵经典题目六:poj 3070 Fibonacci
http://poj.org/problem?id=3070 按已构造好的矩阵,那么该矩阵的n次方的右上角的数便是f[n]. #include <stdio.h> #include < ...
- POJ 3070 Fibonacci
裸奔的矩阵乘法,当模板了. #include <iostream>#include <cstring>#include <cstdio> using namespa ...
- POJ - 3070 Fibonacci
题目已经告诉你斐波那契矩阵算法了... 所以需要用到矩阵快速幂,看起来名字很diao但其实和普通快速幂基本一毛一样... 1 #include <iostream> 2 #include ...
- POJ 3047 蔡勒公式
传送门 题目大意: 给出年月日,推算这一天是星期几 分析: 蔡勒公式:(只适用于1582年10月15日之后的日期) w = y + y/4 + c/4 - 2*c + 26 * (m+1)/10 + ...
- poj 3047 Bovine Birthday
题目大意:给出年月日,求这一天是星期几 Java Calender类直接水过...注意,Java的Calendar类中月份从0月开始,所以月份要记得减一 下面直接贴代码: import java.ut ...
- 一起开心2020暑假训练第一周
hdu 1576 A/B oj传送 题解: Poj 1061 青蛙的约会 oj传送 题解: hdu 1525 Euclid's Game oj传送 题解: Poj 3070 Fibonacci oj传 ...
- 矩阵相关操作和矩阵快速幂
矩阵相关操作和矩阵快速幂 矩阵基本运算以及快速幂模板 POJ - 3070. Fibonacci Hdu - 1757A. Simple Math Problem Codeforces - 185A. ...
最新文章
- Java-查看JVM从哪个JAR包中加载指定类
- 深入理解ROS技术 【4】ROS下的模块详解(181-232)
- 2015 UESTC 数据结构专题H题 秋实大哥打游戏 带权并查集
- 关于快速开发和设计应用系统的一些个人的意见
- 解决SecureCRT与SecureFX中文乱码问题
- Java基础常见的面试题
- iphone怎么查看wifi密码_WiFi密码忘了怎么办?一秒找回密码
- 实力吊打国家黑客:从密码喷洒到完全控制网络只需几天
- 组策略下发URL地址时的问题
- java lambda_Java 8 Lambda 表达式 ( 中 )- 外部参数
- 洛谷 P1854 花店橱窗布置
- element-ui表格合并数据相同行
- 数据模型的作用和数据模型的三个要素:
- 枚举所有IE窗口,实现自动登录
- PAT 1021 Deepest Root (25分) 从测试点3超时到满分再到代码优化
- spring mvc
- github python100天_GitHub - 1814931012/Python-100-Days: Python - 100天从新手到大师
- win10 分屏方法
- 一个新的王者在8月即将加冕——Treasure project(TPC)
- div添加滚动条css属性代码
热门文章
- excel相乘后求和_excel怎么求和相乘,5种超实用的Excel求和方式
- Note_Logistics_Day14 ClickHouse 快速入门
- 利用Ubuntu搭建私有云
- 第一原理法则的黄金圈法则_与困难的人公开工作的5条黄金法则
- ROS图像与OpenCV图像相互转换
- 使用Python抓取婚恋网数据并用决策树生成择偶观
- 【知识星球】云、IT、数据、计算存储相关售前资料
- django+vue+nginx+frp搭建漫画网站之获取访客真实ip(二)
- 【本人秃顶程序员】好好讲一讲:到底什么是Java架构师——《架构师的自我修养》
- 电话销售邀约客户技巧 预约客户见面的方法