首页 > 代码库 > hdu_1005

hdu_1005

 1 #include <cstdio> 2  3 struct mat 4 { 5     int e[3][3]; 6     mat operator * (mat t){ 7         mat s; 8         for (int i = 1; i <= 2; ++i){ 9             for (int j = 1; j <= 2; ++j){10                 s.e[i][j] = 0;11                 for (int k = 1; k <= 2; ++k){12                     s.e[i][j] += e[i][k] * t.e[k][j] % 7; 13                 }14             }15         }16         return s;17     }18 };19 int A,B,n;20 21 int main(int argc, char const *argv[])22 {23     while(scanf("%d%d%d",&A,&B,&n)&&(A||B||n)){24         if(n == 1 || n == 2){25             printf("1\n");26             continue;27         }28         mat bas;29         bas.e[1][1] = A; bas.e[1][2] = B;30         bas.e[2][1] = 1; bas.e[2][2] = 0;31         n-=3;32         mat temp = bas;33         while(n){34             if(n&1){35                 n--;36                 bas = bas * temp;37             }38             temp = temp * temp;39             n>>=1;40         }41         printf("%d\n", (bas.e[1][1] + bas.e[1][2]) % 7 );42     }43     return 0;44 }

 

hdu_1005