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 }
时间: 2024-10-06 01:15:23

hdu_1005的相关文章

hdu_1005 Number Sequence

// 题目数据有问题 这样都能过? 如果没有循环就不行 但是数组也开不了那么大 所以数据或者数据范围有问题 #include<bits/stdc++.h> using namespace std; int main(){ int a[10000],x,y,n,m,i; a[1]=a[2]=1; while(scanf("%d%d%d",&x,&y,&n)){ if(x==0 && y==0 && n==0)break;

HDU_1021:Fibonacci Again

Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2). Input Input consists of a sequence of lines, each containing an integer n. (n < 1,000,000). Output Print the word "yes" if