B. The Eternal Immortality
Problem Statement
Even if the world is full of counterfeits, I still regard it as wonderful.
Pile up herbs and incense, and arise again from the flames and ashes of its predecessor — as is known to many, the phoenix does it like this.
The phoenix has a rather long lifespan, and reincarnates itself once every a! years. Here a! denotes the factorial of integer a, that is, a! = 1 × 2 × … × a. Specifically, 0! = 1.
Koyomi doesn’t care much about this, but before he gets into another mess with oddities, he is interested in the number of times the phoenix will reincarnate in a timespan of b! years, that is,b!a! . Note that when b ≥ a this value is always integer.
As the answer can be quite large, it would be enough for Koyomi just to know the last digit of the answer in decimal representation. And you’re here to provide Koyomi with this knowledge.
Input
The first and only line of input contains two space-separated integers a and b (0 ≤ a ≤ b ≤ 1018).
Output
Output one line containing a single decimal digit — the last digit of the value that interests Koyomi.
Examples
Example 1
Input
4 2
Output
2
Example 2
Input
0 10
Output
0
Example 3
Input
107 109
Output
2
Note
In the first example, the last digit of 4!2!=12 is 2;
In the second example, the last digit of 10!0!=3628800 is 0;
In the third example, the last digit of 109!107!=11772 is 2.
题意
给你a和b(a ≤ b),问你b!a!的最后一位是多少。
思路
首先我们可以推出结论,如果b-a≥5的话那最后一位肯定是0,因为b-a≥5的话,就是5个连续的自然数相乘,那其中必有一个为5的倍数(2的倍数就更不用说了)所以最后一位肯定是0。如果b-a<5的话,那直接暴力就好啦!
Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline void readInt(int &x) {
x=0;int f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
x*=f;
}
inline void readLong(ll &x) {
x=0;int f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
x*=f;
}
/*================Header Template==============*/
ll a,b;
ll ans=1;
int main() {
readLong(a);
readLong(b);
if(b-a>=5)
puts("0");
else {
for(ll p=a+1;p<=b;p++) {
ans=ans*p;
ans%=10;
}
printf("%lld\n",ans);
}
return 0;
}