DEBUG很辛苦,且行, 且珍惜

原代码:

  1. ans[0][0] = (ans[0][0] * a[flag][0][0] + ans[0][1] * a[flag][1][0]) % 10000;
  2. ans[0][1] = (ans[0][0] * a[flag][0][1] + ans[0][1] * a[flag][1][1]) % 10000;
  3. ans[1][0] = (ans[1][0] * a[flag][0][0] + ans[1][1] * a[flag][1][0]) % 10000;
  4. ans[1][1] = (ans[1][0] * a[flag][0][1] + ans[1][1] * a[flag][1][1]) % 10000;

问题在于:修改后ans[][]的值再次调用,就不是原来的值了,会导致程序出错

QAQ

改进后:

  1. ans[0][0] = (temp_1 * a[flag][0][0] + temp_2 * a[flag][1][0]) % 10000;
  2. ans[0][1] = (temp_1 * a[flag][0][1] + temp_2 * a[flag][1][1]) % 10000;
  3. ans[1][0] = (temp_3 * a[flag][0][0] + temp_4 * a[flag][1][0]) % 10000;
  4. 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[][][];
  11. void test_print(){
  12. for(int i = ; i < ; ++i){
  13. printf("i = %d\n",i);
  14. printf("%d\n\n",a[i][][]);
  15. }
  16. }
  17. int main(){
  18. int i, j, k;
  19. int n;
  20. a[][][] = ;
  21. a[][][] = ;
  22. a[][][] = ;
  23. a[][][] = ;// n = 1
  24.  
  25. for(i = ; i < ; ++i){
  26. a[i][][] = (a[i-][][] * a[i-][][] + a[i-][][] * a[i-][][]) % ;
  27. a[i][][] = (a[i-][][] * a[i-][][] + a[i-][][] * a[i-][][]) % ;
  28. a[i][][] = (a[i-][][] * a[i-][][] + a[i-][][] * a[i-][][]) % ;
  29. a[i][][] = (a[i-][][] * a[i-][][] + a[i-][][] * a[i-][][]) % ;
  30. }
  31. // test_print();
  32. while(EOF != scanf("%d",&n)){
  33. if(- == n) break;
  34. else if( == n){
  35. printf("0\n");
  36. continue;
  37. }
  38. int count = ;
  39. int flag;
  40. int ans[][];
  41. int array[];
  42. int flag_t = ;
  43. bool init_ok = false;
  44. memset(array, , sizeof(array));
  45. while(n){
  46. if(n % == ){
  47. n /= ;
  48. array[flag_t] = ;
  49. } else{
  50. n = (n - ) / ;
  51. array[flag_t] = ;
  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 = ; i < flag_t; ++i){
  57. if(!array[i]) continue;
  58. int flag = i;
  59. if(!init_ok){
  60. ans[][] = a[i][][];
  61. ans[][] = a[i][][];
  62. ans[][] = a[i][][];
  63. ans[][] = a[i][][];
  64. init_ok = true;
  65. continue;
  66. }
  67. int temp_1 = ans[][];
  68. int temp_2 = ans[][];
  69. int temp_3 = ans[][];
  70. int temp_4 = ans[][];
  71. ans[][] = (temp_1 * a[flag][][] + temp_2 * a[flag][][]) % ;
  72. ans[][] = (temp_1 * a[flag][][] + temp_2 * a[flag][][]) % ;
  73. ans[][] = (temp_3 * a[flag][][] + temp_4 * a[flag][][]) % ;
  74. ans[][] = (temp_3 * a[flag][][] + temp_4 * a[flag][][]) % ;
  75. }
  76. printf("%d\n",ans[][]);
  77. }
  78. return ;
  79. }

POJ 3047 Fibonacci的更多相关文章

  1. 矩阵快速幂 POJ 3070 Fibonacci

    题目传送门 /* 矩阵快速幂:求第n项的Fibonacci数,转置矩阵都给出,套个模板就可以了.效率很高啊 */ #include <cstdio> #include <algori ...

  2. POJ 3047 Bovine Birthday 日期定周求 泽勒公式

    标题来源:POJ 3047 Bovine Birthday 意甲冠军:.. . 思考:式 适合于1582年(中国明朝万历十年)10月15日之后的情形 公式 w = y + y/4 + c/4 - 2* ...

  3. POJ 3070 Fibonacci

    Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...

  4. 矩阵经典题目六:poj 3070 Fibonacci

    http://poj.org/problem?id=3070 按已构造好的矩阵,那么该矩阵的n次方的右上角的数便是f[n]. #include <stdio.h> #include < ...

  5. POJ 3070 Fibonacci(矩阵高速功率)

    职务地址:POJ 3070 用这个题学会了用矩阵高速幂来高速求斐波那契数. 依据上个公式可知,第1行第2列和第2行第1列的数都是第n个斐波那契数.所以构造矩阵.求高速幂就可以. 代码例如以下: #in ...

  6. poj 3070 Fibonacci (矩阵快速幂乘/模板)

    题意:给你一个n,输出Fibonacci (n)%10000的结果 思路:裸矩阵快速幂乘,直接套模板 代码: #include <cstdio> #include <cstring& ...

  7. poj 3070 Fibonacci 矩阵快速幂

    Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 for n ≥ 2. F ...

  8. POJ 3070 Fibonacci 【矩阵快速幂】

    <题目链接> Description In the Fibonacci integer sequence, F0 = 0, F1 = 1, and Fn = Fn − 1 + Fn − 2 ...

  9. poj 3070 Fibonacci 矩阵相乘

    Fibonacci Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7715   Accepted: 5474 Descrip ...

随机推荐

  1. NET Core个人博客

    NET Core重写个人博客站点小结 今天用ASP.NET Core重写了个人博客站点,原来是基于ASP.NET 4.5开发的.重写工作总体很顺利,最后成功发布到Ubunt+Nginx平台上.效果如下 ...

  2. Qt 设置背景图片3种方法(QPalette可以做无数的事情,一旦控件指定了调色板,就要乖乖听它的话;QPainter当场绘制当然也没有问题,还有就是QSS)

    方法1. setStylSheet{"QDialog{background-image:url()"}}  //使用styleSheet 这种方法的好处是继承它的dialog都会自 ...

  3. Spring Boot 属性配置和使用

    Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...

  4. C++的一些编程规范(基于google)

    1.所有头文件都应该使用#define 防止头文件被多重包含,命名格式可以参考<PROJECT>_<PATH>_<FILE>_H 2.使用前置声明尽量减少.h文件中 ...

  5. hdu4336 Card Collector 状态压缩dp

    Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...

  6. asp.net生成RSS

    经常看到博客.还有很多网站中有RSS订阅,今天就来玩玩asp.net生成RSS,在网上查找了相关资料 发现just soso,如下: aspx <?xml version="1.0&q ...

  7. 玩转Bootstarp(连载)

    一.Bootstarp是什么? 简单.灵活的用于搭建WEB页面的HTML.CSS.JS的工具集 (基于HTML5和CSS3) 总结:简洁强大的前端开发框架,可以让WEB开发更迅速.更简单 二.如何使用 ...

  8. 下载文件夹里面的所有文件,并压缩成.zip压缩包的形式

    http://www.aspsnippets.com/Articles/Download-multiple-files-as-Zip-Archive-File-in-ASPNet-using-C-an ...

  9. [爬虫]通过url获取连接地址中的数据

    1. 要想获取指定连接的数据,那么就得使用HtmlDocument对象,要想使用HtmlDocument对象就必需引用using HtmlAgilityPack; 2. 详细步骤如下:     步骤一 ...

  10. Extjs 3.0 htmleditor实现插入图片功能

    首先感谢前辈们的无私奉献.贴出参考地址 http://zhidao.baidu.com/link?url=Q0ZM405OFNy_xAHSut9TepRJxgXCxFayQttrQz1N82dlA1_ ...