题目链接:点击打开链接
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, . 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.
InputThe first and only line of input contains two space-separated integers a and b (0 ≤ a ≤ b ≤ 1018).
Output one line containing a single decimal digit — the last digit of the value that interests Koyomi.
2 4
2
0 10
0
107 109
2
In the first example, the last digit of is 2;
In the second example, the last digit of is 0;
In the third example, the last digit of is 2.
题意:给你两个数字,要求出较大数字阶乘除以较小数字阶乘的商的个位数字。
坑点:数据大,需用long long型。
暴力破解法:遍历从a+1乘到b,但每次乘的时候要将变量取余10,并且需将每次乘积结果取余10,因为最终结果只需要个位所以个位以上的位对结果没有影响,如果不进行取余,可能会存在乘法溢出导致结果错误。并且每次要对乘积的结果判断个位是否为0,若为0则跳出循环,因为只要个位出现了0,后面的乘积就一定是0了。如果没有该判断程序则会超时。代码如下:
#include<stdio.h>
int main()
{
long long a,b,s;
while(~scanf("%lld%lld",&a,&b))
{
s=1;
for(long long i=b;i>a;i--)
{
s=(s*(i%10))%10;//每次将i对10取余并对结果取余
if(!s) break;//判断s是否为0,若为0则直接跳出循环防止超时。
}
printf("%lld\n",s);
}
}
巧解:将输入的两个数a,b(b>a)分别取余10得到m,n。先判断a,b相差是否大于等于10,若是则直接输出0;若m>n则输出0(这一判断不能省略);否则遍历从m+1乘到n并且每次对10取余防止溢出,不过这里不会存在这个问题了。最后直接将结果输出即可。代码如下:
#include<stdio.h>
int main()
{
long long a,b;
while(~scanf("%lld%lld",&a,&b))
{
int m=a%100,n=b%100,s=1;
if(b-a>=10)
printf("0\n");
else if(m>n)
printf("0\n");
else
{
for(int i=n;i>m;i--)
s=(s*i)%10;
printf("%d\n",s);
}
}
}