[Find the last digit when factorial of A divides factorial of B]
Given two numbers A and B. The task is to compute the last digit of the resulting F, where F= B! / A! .
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains two number A and B as input.
Output:
For each test case, print last digit of B! / A! (B factorial/ A factorial).
Constraints:
1<=T<=100
1<=A<=B<=1018
Example:
Input:
2
2 4
107 109
Output:
2
2
Explanation:
Input : A = , B = Output : Explanation : A! = and B! = . F = / = --> last digit =
解题思路:
B!/A!=B*(B-1)*...*(A+1)即只需要求出B*(B-1)*...*(A+1)的最后一位即可
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 ...
会发现只要A、B两个数字相差>4,那么他们最后一位一定是0
当A、B两个数字相差<4时,只需要循环求出B*(B-1)*...*(A+1)的最后一位数字即可。但是因为AB都是在1-10^18,所以我们只需要求这个式子中每一个数字的最后一位相乘的乘积即可。
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num,i,j;
scanf("%d",&num);
long long *Arr=(long long *)malloc(sizeof(long long)*2*num);
int *Brr=(int *)malloc(sizeof(int)*num);//存放结果
for(i=0;i<2*num;i++)//输入各个A、B
{
scanf("%d",&Arr[i]);
}
for(i=0;i<num;i++)//对每一个用例进行处理
{
for(j=0;j<2*num;j=j+2)
{
if(Arr[j+1]-Arr[j]>4)//相差大于4,则最后一位一定是0
{
Brr[i]=0;
}
else//相差小于等于4,则只需要(B*(B-1)*....*(A+1))其中只需要计算最后一位相乘的即可。
{
int mul=1,k;
for(k=Arr[j+1];k>Arr[j];k--)//一共有(Arr[2*i+1]-Arr[2*i])个数相乘
{
mul=mul*(Arr[k]%10);
}
Brr[i]=mul%10;
}
}
}
for(i=0;i<num;i++)
printf("%d\n",Brr[i]);
return 0;
}
[Find the last digit when factorial of A divides factorial of B]的更多相关文章
- Factorial
Factorial 计算阶乘 In mathematics, the factorial of a non-negative integer n, denoted by n!, is the pro ...
- 快速入门:Python简单实例100个(入门完整版)
Python3 100例 文章目录 Python3 100例 实例001:数字组合 实例002:“个税计算” 实例003:完全平方数 实例004:这天第几天 实例005:三数排序 实例006:斐波那契 ...
- python100实例
实例001:数字组合 题目 有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? 程序分析 遍历全部可能,把有重复的剃掉. total=0 for i in range(1 ...
- (转)C++语言的15个晦涩特性
原文链接: Evan Wallace 翻译: 伯乐在线- 敏敏 译文链接: http://blog.jobbole.com/54140/ 这个列表收集了 C++ 语言的一些晦涩(Obscure)特 ...
- JavaScript 精粹
数据类型 JavaScript 是 弱类型 语言,但并不是没有类型,JavaScript可以识别下面 7 种不同类型的值: 基本数据类型 Boolean Number String null unde ...
- java基础练习 字符串,控制流,日历,日期等
1,对基本控制流程的一些练习 package org.base.practice3; import org.junit.Test; /** * Created with IntelliJ IDEA. ...
- 计算(LnN!)的值
import java.util.*;import java.math.*;public class CaculatorLnN { public static void main(String[] a ...
- 每天写点shell——read的用法
1.read基本读取 #!/bin/bash #testing the read command echo -n "Enter you name:" #echo -n 让用户直接在 ...
- Google C++单元测试框架---Gtest框架简介(译文)
一.设置一个新的测试项目 在用google test写测试项目之前,需要先编译gtest到library库并将测试与其链接.我们为一些流行的构建系统提供了构建文件: msvc/ for Visual ...
随机推荐
- python中的“.T”操作
其实就是对一个矩阵的转置 看代码: a array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) a.T array([[1, 4, 7], [2, 5, 8], [3, 6, ...
- 实践作业2:黑盒测试实践——小组任务分工 Day 1
今日教学实验任务分配后,课下小组例会完成任务分工,具体分工如下: (1)系统需求分析--刘思佳 (2)设计测试用例--王俊杰 (3)编写.运行测试脚本--郜昌磊 (4)记录测试过程--吴慧杰 (5)记 ...
- echarts 点击方法总结,点任意一点获取点击数据,在多图联动中用生成标线举例
关于点击(包括左击,双击,右击等)echarts图形任意一点,获取相关的图形数据,尤其是多图,我想部分人遇到这个问题一直很头大.下面我用举例说明,如何在多图联动基础上,我们点击点击任意一个图上任意一点 ...
- angularjs 给封装的模态框元素传值,和实现兄弟传值
本例实现封装的元素所放的位置不同,而选择不同的传值,这里举例封装了bootstrap模态框,以后也方便大家去直接使用.方法举例如下:首先主页调用css/js有: <link rel=" ...
- Redis在centos6.4上的最详细图文安装教程
准备工作:一个redis3.0.0的安装包,没有可以点击下面的链接下载 https://pan.baidu.com/s/1kU5Ez2J 工具 安装环境 centos6.4 好了开始进入正题 输入 ...
- CentOS6.9-zabbix3.2启动失败原因及页面没有mysql选择项
环境内核信息: [root@zabbix- ~]# uname -a Linux lodboyedu- -.el6.x86_64 # SMP Tue Mar :: UTC x86_64 x86_64 ...
- C#中的Explicit和Implicit
今天在Review一个老项目的时候,看到一段奇怪的代码. if (dto.Payment == null) continue; var entity = entries.FirstOrDefault( ...
- linux kvm虚拟机使用
安装配置kvm 1.安装kvm软件包 [root@kvm ~]# yum install kvm python-virtinst libvirt tunctl bridge-utils virt-ma ...
- Android 夜间模式changeskin小结
@author vivian8725118 @CSDN http://blog.csdn.net/vivian8725118 @简书 http://www.jianshu.com/p/832e9776 ...
- Python的招牌菜xmlrpc
一.简单介绍 为了解决在系统的80port提供RPC的服务.而又不影响正在运行的WEB服务.人们想出了用HTTP协议传输RPC包的办法.对于差点儿是专门用于传输文本的HTTP协议.要在其上传输RPC封 ...