当前位置:网站首页>Trailing Zeroes (II)
Trailing Zeroes (II)
2022-06-28 08:10:00 【Angeliaaa】
Find the number of trailing zeroes for the following function:
nCr * pq
where n, r, p, q are given. For example, if n = 10, r = 4, p = 1, q = 1, then the number is 210 so, number of trailing zeroes is 1.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case contains four integers: n, r, p, q (1 ≤ n, r, p, q ≤ 106, r ≤ n).
Output
For each test case, print the case number and the number of trailing zeroes.
Sample Input
2
10 4 1 1
100 5 40 5
Sample Output
Case 1: 1
Case 2: 6
题意: 求c(n,r)*p^q的后导0的个数。
思路:我们求一个数的后导0一般都是把这个数进行因子分解求2的个数与5的个数,然后较小的就是这个数末尾0的个数,因为本题要求的数是除法的形式,因为的分子上的2与5是可以和分母上的2与5抵消的,所以求2的个数就是分子上2的个数减去分母上2的个数,求5的个数就是分子上5的个数减去分母上5的个数,然后找到一个较小的即为结果,因为此题数据范围较大,所以先打一个表,要用到前缀和二维数组num【】【】,num【i】【0】数组来存储 从1到 i 中因子2的总个数,num【i】【1】数组来存储从1到 i 中因子5的总个数。最后进行处理就得到结果啦,代码如下:
#include<stdio.h>
#include<string>
#include<queue>
#include<math.h>
#define inf 0x3f3f3f3f
#define LL long long
#include<string.h>
#include<algorithm>
using namespace std;
int num[1000010][2]= {0};
void init()
{
num[1][0]=0;
num[1][1]=0;
for(int i=2; i<1000010; i++)
{
int x=i;
num[i][0]=num[i-1][0];
while(x%2==0)
{
num[i][0]++;
x=x/2;
}
int x1=i;
num[i][1]=num[i-1][1];
while(x1%5==0)
{
num[i][1]++;
x1=x1/5;
}
}
}
int main()
{
int T,cas=1;
init();
int n,r,p,q;
scanf("%d",&T);
while(T--)
{
int s2,s5;
scanf("%d %d %d %d",&n,&r,&p,&q);
s2=num[n][0]-num[r][0]-num[n-r][0]+(num[p][0]-num[p-1][0])*q;
s5=num[n][1]-num[r][1]-num[n-r][1]+(num[p][1]-num[p-1][1])*q;
printf("Case %d: ",cas++);
if(s2>s5)
printf("%d\n",s5);
else
printf("%d\n",s2);
}
return 0;
}
边栏推荐
- Redis02 -- an operation command of five data types for ending redis (it can be learned, reviewed, interviewed and collected for backup)
- Vagrant installation
- Redis cluster deployment and application scenarios
- Oracle RAC -- understanding of VIP
- LeetCode之三步问题
- 2022第六季完美童模 佛山赛区 初赛圆满落幕
- Prometheus + grafana + MySQL master-slave replication + host monitoring
- Upgrade HDP spark to spark 2.4.8 without upgrading ambari
- cuda和cudnn和tensorrt的理解
- Hidden scroll bar on PC
猜你喜欢

ROS notes (09) - query and setting of parameters

2022巴黎时装周儿童单元6.19武汉站圆满落幕

AI chief architect 8-aica-gao Xiang, in-depth understanding and practice of propeller 2.0

Upgrade HDP spark to spark 2.4.8 without upgrading ambari

Little artist huangxinyang was invited to participate in the Wuhan station of children's unit of Paris Fashion Week

MySQL two table connection principle (understand join buf)

三角变换公式

Unity gets the coordinate point in front of the current object at a certain angle and distance

The micro kernel zephyr is supported by many manufacturers!

Airflow2 configuration windows azure SSO details based on oauth2 protocol
随机推荐
2022巴黎时装周儿童单元6.19武汉站圆满落幕
Uvcgan: unt vision transformer cycle-consistent Gan for unpropared image-to-image translation
ROS 笔记(09)— 参数的查询和设置
Redis cerebral fissure
图像翻译/Transformer:ITTR: Unpaired Image-to-Image Translation with Transformers用Transfor进行非配对图像对图像的转换
PLSQL installation under Windows
Introduction, compilation, installation and deployment of Doris learning notes
B_QuRT_User_Guide(29)
Redis uses sentinel master-slave switching. What should the program do?
Connaissez - vous le protocole TCP (2)?
抗洪救灾,共克时艰,城联优品捐赠10万元爱心物资驰援英德
Unity 获取当前物体正前方,一定角度、距离的坐标点
887. egg drop
About RAC modifying scan IP
redis02——一篇终结redis的五种数据类型操作命令(可学习、复习、面试、收藏备用)
Resolution of Rac grid failure to start after server restart
Leetcode swing series
Airflow2.1.1 ultra detailed installation document
nlp序列完全可以模拟人脑智能
Two tips for block level elements