HDU 1005 Matrix Quick power solution cyclic Solution

Source: Internet
Author: User

Loop solution:

For the formula F [N] = A * f [n-1] + B * f [N-2]; the latter is only 7*7 = 49, why so, because the values of F [n-1] or F [N-2] are only 0, 1, 2, 3, 4, 5, 6, and A and B are fixed, so there are only 49 possible values. The relationship between each item and the first two items is known, so when the two consecutive items appear before, because the formula remains unchanged, the subsequent results must be consistent with the front. Therefore, the circular section appears at this time. Note that the circular section does not necessarily start with. However, can be used as a circular section, but it may not be the highest cyclic section. In addition, in a group of test data, F [N] has only 49 possible answers. The worst case is that all the situations are met, then, a circular section is generated in 50 operations. After finding the cycle, you can easily solve the problem. Here, we use as the loop section to determine the Code:

 1 #include <stdio.h> 2 #include <stdlib.h> 3  4 int main() 5 { 6     int a,b,n,i; 7     int m[1000]; 8     while(scanf("%d%d%d",&a,&b,&n),(a||b||n)) 9     {10         m[1] = 1;11         m[2] = 1;12         for(i = 3;i < 1000;i++)13         {14             m[i] = (a*m[i-1]+b*m[i-2])%7;15             if(m[i]==1 && m[i-1]==1)16                 break;17         }18         m[0] = m[i-2];19         /*printf("%d\n",(i-2));*/20         n = n%(i-2);21         printf("%d\n",m[n]);22     }23     return 0;24 }

 

Rapid matrix power solution:

The matrix is constructed based on the recursive formula F (n) = A * F (n-1) + BF (n-2. Like the rapid power of a common number, the power of a matrix can also be calculated using a similar method, but at this time it is multiplied by a matrix. Code:

 1 #include <stdio.h> 2 #include <stdlib.h> 3  4 typedef struct Mat{ 5     int num[3][3]; 6 }Mat; 7  8 Mat Mult(Mat a,Mat b)      /*Matrix a multi Matrix b*/ 9 {10     int i,j,k;11     Mat c;12     for(i=0 ;i<2 ;i++)13         for(j=0 ;j<2 ;j++)14         {15             c.num[i][j] = 0;16             for(k=0 ;k<2 ;k++)17                 c.num[i][j] = (c.num[i][j] + a.num[i][k]*b.num[k][j])%7; /*mod 7*/18         }19     return c;20 }21 22 Mat Mat_pow(Mat a,int n)    /*Matrix Quick power n*/23 {24     Mat Result;25     Result.num[0][0] = 1;26     Result.num[0][1] = 0;27     Result.num[1][0] = 0;28     Result.num[1][1] = 1;29     while(n != 0){30         if(n & 1)31             Result = Mult(Result,a);32         a = Mult(a,a);33         n >>= 1;34     }35     return Result;36 }37 38 int main()39 {40     int a,b;41     int n;42     Mat A;43     while(scanf("%d%d%d",&a,&b,&n),(a||b||n))44     {45         A.num[0][0] = a;46         A.num[0][1] = b;47         A.num[1][0] = 1;48         A.num[1][1] = 0;49         if(n==1 || n==2)50         {51             printf("1\n");52             continue;   /*can not be break*/53         }54         A = Mat_pow(A,n-2);55         printf("%d\n",(A.num[0][0]+A.num[0][1])%7);56     }57     return 0;58 }

 

HDU 1005 Matrix Quick power solution cyclic Solution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.