|
亲!马上注册或者登录会查看更多内容!
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
推公式 + 找规律,可以参考一篇讲的很细致的博文:http://www.cnblogs.com/LETTers/archive/2012/04/18/2456089.html
- // reference to http://www.cnblogs.com/LETTers/archive/2012/04/18/2456089.html
- int lastDigit(int n)
- {
- // kMeta【i】 = last non zero digit of i!
- static const int kMeta[10] = {1, 1, 2, 6, 4, 2, 2, 4, 2, 8};
- // g(i) = replace multiples of 5 by 1 in i!, we get periodical last digit
- // g(m*10+n) % 10 = kMod5[n]
- static const int kMod5[10] = {6, 6, 2, 6, 4, 4, 4, 8, 4, 6};
- // to pair those 5, we need to div 2, which is also periodical as there are much more 2 than 5
- static const int kDiv2[10] = {0, 0, 6, 0, 2, 0, 8, 0, 4, 0};
- if(n < 10) return kMeta[n];
- int res = lastDigit(n/5) * 6 * kMod5[n%10] % 10;
- int cnt = n/5 % 4;
- while(cnt--) res = kDiv2[res];
- return res;
- }
复制代码
|
|