PS: I looked at the question again and found that there is no limit on the N of zoj... However, there is a limit of less than 10000 for the two methods, so the pre-processing of the previous method is not necessarily large enough. The second method is tough.
I have to say that because of this question, I know that the data of poj is very watery .. Zoj won't be able to pass this question, but it won't be able to pass through.
// if you do not understand it, you may find that large numbers will time out and search for them online, question <br/> // F (I) = f (I-1) * I % 10, I % 5 = 0; <br/> // F (I) = Mod (2, (n-1) % 4 + 1) * F (n) % 10, I % 5! = 0, n = I/5; <br/> // The value range of MOD (2, (n-1) % 4 + 1) is 2, 4, 6, 8 <br/> # include <iostream> <br/> # include <cmath> <br/> # include <stdio. h> <br/> # include <string> <br/> using namespace STD; <br/> // # define test <br/> long a [10001] = {0, 1, 2, 6, 4, 2 }, B [5] = {0, 2, 4, 8, 6}; <br/> int main () {<br/> # ifdef test <br/> freopen ("input.txt", "r", stdin); <br/> # endif <br/> long M, N; <br/> for (long I = 6; I <= 10000; I ++) {<br/> M = I % 5; <br/> N = I/5; <br/> If (M! = 0) A [I] = A [I-1] * I % 10; <br/> else a [I] = B [(n-1) % 4 + 1] * A [n] % 10; <br/>}< br/> while (CIN> N) {<br/> printf ("% 5D-> % d/N", n, a [n]); <br/>}< br/> return 0; <br/>}< br/>
Then we can see a method in the number theory template:
<Textarea Cols = "50" rows = "15" name = "code" class = "CPP"> # include <iostream> <br/> # include <cmath> <br/> # include <stdio. h> <br/> # include <cstring> <br/> using namespace STD; <br/> int lastdigit (char * BUF) <br/>{< br/> const int mod [20] = }; <br/> int Len = strlen (BUF), a [110], I, C, ret = 1; <br/> If (LEN = 1) <br/> return mod [Buf [0]-'0']; <br/> for (I = 0; I <Len; I ++) <br/> A [I] = B UF [len-1-i]-'0'; <br/> for (; Len-=! A [len-1]) <br/>{< br/> ret = RET * mod [A [1] % 2*10 + A [0] % 5; <br/> for (C = 0, I = len-1; I> = 0; I --) <br/> C = C * 10 + A [I], A [I] = C/5, C % = 5; <br/>}< br/> return ret + RET % 2*5; <br/>}< br/> int main () {<br/> char s [10]; <br/> while (CIN> S) {<br/> printf ("% 5S-> % d/N", S, lastdigit (s); <br/>}< br/> return 0; <br/>}< br/> </textarea>