Skip to content

Instantly share code, notes, and snippets.

@devteampentagon
Created November 14, 2016 21:24
Show Gist options
  • Select an option

  • Save devteampentagon/683c8eb5df6915587dcb9ee1ca3f2d66 to your computer and use it in GitHub Desktop.

Select an option

Save devteampentagon/683c8eb5df6915587dcb9ee1ca3f2d66 to your computer and use it in GitHub Desktop.
Last Non Zero Digit of Factorial
#include <bits/stdc++.h>
#define MEM(a,b) memset((a),(b),sizeof(a))
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIn(a,b) ((a)<(b)?(a):(b))
#define MIn4(a,b,c,d) MIn(MIn(MIn(a,b),c),d)
#define In freopen("In.txt", "r", stdin);
#define Out freopen("out.txt", "w", stdout);
#define i64 long long
#define u64 long long unsigned
#define sz 1000000
using namespace std;
int PTwo(int N)
{
int T[]= {6,2,4,8};
if(N==0) return 1;
return T[N%4];
}
int LastNZDigit(int N)
{
int A[]= {1,1,2,6,4};
if(N<5) return A[N];
return (PTwo(N/5)*LastNZDigit(N/5)*LastNZDigit(N%5))%10;
}
int main()
{
int n;
while(cin >> n)
cout << LastNZDigit(n) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment