1005ac+ Problem Solving Ideas
Analysis:
1, in the title (1 <= A, B <=, 1 <= n <= 100,000,000), it is certain that brute force will not work.
2, (A * f (n-1) + B * F (n-2)) MoD 7 = (a%7*f (n-1) +b%7*f (n-2))%7
3, because F (i) and F (i+1) have only 49 combinations, because (f (i), (i+1) There are only 7 options, that is, only one of the 0,1,2,3,4,5,6. Therefore, the cycle <=49.
Solving:
1, the cycle is first, by the way, the first period of F (n) to find out.
2, using the cycle, the remaining f (n) is directly obtained.
Code:
#include <stdio.h>
#define M 52
int
Main
()
{
intA
,B
,N
,I
,F
[M
]={0
,1
,1
},T
;
while
(scanf
("%d%d%d"
,&A
,&B
,&N
),A
!=0
||B
!=0
||N
!=0
)
{
A
%=7
;B
%=7
;
for
(I
=3
;I
<M
;I
++)
{
F
[I
]=A
*F
[I
-1
]+B
*F
[I
-2
];
F
[I
]%=7
;
If
(F
[I
-1
]==F
[3
]&&F
[I
]==F
[4
]&&I
>4
)
Break
;
}
T
=I
-4
;
//Cycle
If
(N
<4
)Printf
("%d\n"
,F
[N
]);
elsePrintf
("%d\n"
,F
[(N
-4
)%T
+4
]); Note that it is not f[n%t]
}
Return0
;
}
Hangzhou Electric oj1005 Problem Solving