zoukankan      html  css  js  c++  java
  • 矩阵快速幂

     1 #include<stdio.h>
     2 #include<stdlib.h>
     3 #include<string.h>
     4 #include<algorithm>
     5 #include<iostream>
     6 using namespace std;
     7 struct node{
     8     int a[3][3];
     9 };
    10 struct node mulpity(struct node a,struct node b)
    11 {
    12     int i,j,k;
    13     struct node mid;
    14     memset(mid.a,0,sizeof(mid.a));
    15     for(i=0;i<2;i++)
    16         for(j=0;j<2;j++)
    17             for(k=0;k<2;k++)
    18             {
    19                 mid.a[i][j]=mid.a[i][j]+a.a[i][k]*b.a[k][j];
    20                 mid.a[i][j]=mid.a[i][j]%10000;
    21             }
    22     return mid;
    23 }
    24 int answer(int n)
    25 {
    26     int i,p,j;
    27     struct node a,b;
    28     memset(b.a,0,sizeof(b.a));
    29     b.a[0][0]=1;
    30     b.a[0][1]=1;
    31     b.a[1][0]=1;
    32     memset(a.a,0,sizeof(a.a));
    33     for(i=0;i<2;i++)
    34         a.a[i][i]=1;
    35     while(n)
    36     {
    37         if(n&1)
    38             a=mulpity(a,b);
    39         b=mulpity(b,b);
    40         n>>=1;
    41     }
    42     return a.a[0][1];
    43 }
    44 int main()
    45 {
    46     int i,p,j,n;
    47 
    48     while(scanf("%d",&n)!=EOF)
    49     {
    50         if(n==-1)
    51             break;
    52         printf("%d
    ",answer(n));
    53     }
    54     return 0;
    55 }
  • 相关阅读:
    第13周作业集
    软件工程结课作业
    第13次作业--邮箱的正则表达式
    第12次作业--你的生日
    第11次作业
    第10次作业
    找回感觉的练习
    第16周作业
    第15周作业
    第14周作业
  • 原文地址:https://www.cnblogs.com/daybreaking/p/12778813.html
Copyright © 2011-2022 走看看