Number SequenceTime
limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 122527 Accepted Submission (s): 29758
Problem Descriptiona number sequence is defined as follows:
F (1) = 1, f (2) = 1, f (n) = (A * F (n-1) + B * F (n-2)) MoD 7.
Given A, B, and N, you is to calculate the value of f (n).
Inputthe input consists of multiple test cases. Each test case contains 3 integers a, b and N in a single line (1 <= A, b <=, 1 <= n <= 100,000,000). Three zeros signal the end of the input and this test case are not a is processed.
Outputfor each test case, print the value of f (n) in a single line.
Sample Input
1 1 31 2 100 0 0
Sample Output
25
Authorchen, Shunbao
SourceZJCPC2004
recommendjgshining | We have carefully selected several similar problems for you:1008 1021 1019 1003 1009
Sometimes you wonder if the author of this topic is mentally wrong? Only 20% of the pass rate, more than 120,000 Acmer submitted only 30,000 less than the pass rate! You want the topic must be very difficult, actually not, the topic is very pit!(1 <= A, B <=, 1 <= n <= 100,000,000)Inside the value range of n is one billion, if the direct return to the words! Sure stack overflow ..... (because I tried!) finally on the Internet to check the idea:because the result of f (n) is modulo 7, then the result of f (n) is only possible in {0,1,2,3,4,5,6,}, so that the result of f (n) and F (n-1) is only 7*7 = 49, and the reasonable use of periodicity can greatly improve the program run time.
Don't want to say, this question more say a word all make me sick!! Thanks to Kim Yong Qing bo friends answer! http://blog.csdn.net/jinyongqing/article/details/21537175
Import Java.io.*;import java.util.*;p ublic class main{public static void Main (string[] args) { Scanner input = new Scanner (system.in); int a, B; int n; while (Input.hasnext ()) { a = Input.nextint (); b = Input.nextint (); n = input.nextint (); if (a = = 0 & b = = 0 & n = 0) system.exit (0); int f[] = new INT[50]; for (int i = 1; i < i++) { if (i = = 1 | | i = = 2) { f[i] = 1; } else { F[i] = (A * F[i-1] + b * f[i-2])% 7; } SYSTEM.OUT.PRINTLN (f[n%]);} }
Hdu-1005-number Sequence (Java Edition + consideration period + nausea water problem!)