這題在學長講完之後和看完題解之後才明白函數怎麼構造。
這題構造一個$f(n)$
$f(n)$ $=$ $n$除以 $2^{a}$ $*$ $5^{b}$ ,$a$ , $b$ 分别是 $n$ 質因數分解後$2,5$的個數。
然後就暴力算一算就好了。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
//處理出來n的質因子中,x的個數。
int prime(int n,int x)
{
int res=0;
while(n) res+=n/x,n/=x;
return res;
}
//f(1)到f(n)中不以5結尾的奇數的個數
int expect_5_end_odd(int n,int x)
{
if(!n) return 0;
return n/10+(n%10>=x)+expect_5_end_odd(n/5,x);
}
//以5結尾的數的個數。
int expect_5_end(int n,int x)
{
if(!n) return 0;
return expect_5_end(n/2,x)+expect_5_end_odd(n,x);
}
int t[4][4]={
6,2,4,8,//2^4 2 2^2 2^3 的最後一位
1,3,9,7,//3^4 3 3^2 3^3 的最後一位
1,7,9,3,//4……
1,9,1,9//5……
};
signed main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
m=n-m;
int prime_2=prime(n,2)-prime(m,2);
int prime_3=expect_5_end(n,3)-expect_5_end(m,3);
int prime_5=prime(n,5)-prime(m,5);
int prime_7=expect_5_end(n,7)-expect_5_end(m,7);
int prime_9=expect_5_end(n,9)-expect_5_end(m,9);
if(prime_2<prime_5){puts("5");continue;}
int res=1;
if(prime_2>prime_5) res*=t[0][(prime_2-prime_5)%4];
res=res*t[1][prime_3%4]*t[2][prime_7%4]*t[3][prime_9%4]%10;
printf("%lld\n",res);
}
return 0;
}