Editorial for Chữ số tận cùng khác 0
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Lưu ý: Các code mẫu dưới đây chỉ mang tính tham khảo và có thể không AC được bài tập này
Code mẫu của happyboy99x
#include<cstdio> /* http://math.stackexchange.com/questions/130352/last-non-zero-digit-of-a-factorial D(N) = 4D(N/5)D(N%10) if tens digit of N is odd D(N) = 6D(N/5)D(N%10) if tens digit of N is even */ const int d[] = {1, 1, 2, 6, 4, 2, 2, 4, 2, 8}; int D(int n) { if(n < 10) return d[n]; return (n%100/10%2 ? 4 : 6) * D(n/5)*D(n%10)%10; } int main() { int n; scanf("%d", &n); printf("%d\n", D(n)); return 0; }
Code mẫu của RR
var nn,i,n,res,lt2,lt5:longint; t1:array[0..10] of longint; function powInFac(n,k:longint):longint; begin if n<k then exit(0); exit(n div k+powInFac(n div k,k)); end; function power(a,k:longint):longint; var mid:longint; begin if k=0 then exit(1); if k=1 then exit(a); mid:=power(a,k shr 1); mid:=(mid*mid) mod 10; if k and 1=0 then exit(mid) else exit((mid*a) mod 10); end; function get(n:longint):longint; var res:longint; begin res:=1; while (n>0) do begin res:=(res*power(t1[9],n div 10)*t1[n mod 10]) mod 10; n:=n shr 1; end; exit(res); end; begin t1[0]:=1; for i:=1 to 9 do if (i and 1=1) and (i<>5) then t1[i]:=t1[i-1]*i else t1[i]:=t1[i-1]; read(n); nn:=n; lt2:=powInFac(n,2); lt5:=powInFac(n,5); res:=power(2,lt2-lt5); res:=(res*get(n)) mod 10; n:=nn div 5; while (n>0) do begin res:=(res*get(n)) mod 10; n:=n div 5; end; writeln(res); end.
Code mẫu của hieult
#include <stdio.h> //#include <conio.h> int a[]={6,8,4,2,6,8,4}; int luythua(int k) { if(k==0) return 1; return luythua(k-1)*5; } int tinh(int n) { int duoi=6; if(n==1||n==0) return 1; int x=n%10; int y=n/5; if(x==0) duoi=6; else { for(int i=2;i<=x;i++) duoi=duoi*i; if(x>=5) duoi=duoi/5; } duoi=duoi%10; for(int i=0;i<=3;i++) { if(a[i]==duoi) { duoi=a[i+y%4]; break; } } return (duoi*tinh(n/5))%10; } main() { int n; scanf("%d",&n); printf("%d",tinh(n)); // getch(); }
Code mẫu của khuc_tuan
#include <stdio.h> #include <string.h> #define MAXN 1000 long a[MAXN], l; void a_div_5(void) { int i, plus; plus = 0; for (i=0; i<l; i++) { a[i] = a[i]*2+plus; plus = a[i]/10; a[i] = a[i]%10; } if (plus>0) a[l++] = plus; l--; for (i=0; i<l; i++) a[i] = a[i+1]; } int main(void) { char buffer[MAXN]; long mod[20]={1,1,2,1,4,2,2,4,2,3,4,4,3,4,1,3,3,1,3,2}; int result, n, i, plus, j, one; FILE *f, *g; f = stdin; g = stdout; while ( fscanf(f, "%s", buffer) != EOF) { l = strlen(buffer); for (i=0; i<l; i++) a[i] = buffer[l-1-i] - '0'; one = l==1 && (a[0]==1 || a[0]==0); result = 1; while (l>0) { if (l==1) result = result * mod[a[0]] % 5; else result = result * mod[a[0] + 10*(a[1]%2) ] % 5; a_div_5(); } if (one || result%2==0) fprintf(g, "%d\n", result); else fprintf(g, "%d\n", result+5); } return 0; }
Comments