codeforces-869B(模拟&技巧)

题目链接:点击打开链接

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.

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.

Example
Input
2 4
Output
2
Input
0 10
Output
0
Input
107 109
Output
2
Note

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);
        }
    }
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值