codeforces 869B

B. The Eternal Immortality
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

Examples
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.

 

这题的思路还是挺好想的,只要对个位进行运算就行了,当个位为0的时候,直接跳出。

附高手ac代码:

#include <iostream>
using namespace std;
long long a, b, s=1, i;
int main() {
    cin>>a>>b;
    for(i=a+1; i<=b && s; i++) s = s*i%10;
    cout<<s;
    return 0;
}

  

附我ac代码(鲜明对比啊):

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#define ll long long
using namespace std;
const ll mod = 10;
ll qadd(ll a,ll b) {
	ll res = 0;
	while(b) {
		if(b&1) 
			res = (res+a)%mod;
		b>>=1;
		a = (a+a)%mod;
		
	}
	return res;
}
int main() {
	ios::sync_with_stdio(false);
	ll a,b;
	ll ans=1;
	cin>>a>>b;
	if(a==0) a=1;
	if(b==0) b=1;
	if(b-a>=10) puts("0");
	else {
		while(a!=b) {
		//	cout<<1<<endl;
			ans=qadd(ans,b)%mod;
		//	cout<<ans<<" "<<b<<endl;
			b--;
		}
		cout<<ans<<endl;
	}
//	ans=ans%mod;
	
	return 0;
}

  

posted @ 2017-10-07 13:12  euzmin  阅读(250)  评论(0编辑  收藏  举报