zoukankan      html  css  js  c++  java
  • light oj 1090

    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

    Output for Sample Input

    2

    10 4 1 1

    100 5 40 5

    Case 1: 1

    Case 2: 6

    题意:求组合数C(n,r)乘p的q次方末尾0个数。

    分析:大家都做过n!末尾0的个数这一题吧,这题类似。C(n,r)=n!/((n-r)!×r!);

    由末尾0,来自最基本因子2×5,分别求出式子中因子2,因子5的个数,取两者最小值即可

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #define LL long long
    using namespace std;
    int sum5, sum2;
    void five(int n)//计算n!中包含因子5的个数
    {
        while(n!=0)
            sum5+=n/5,n/=5;
    
    }
    void two(int n)//计算n!中包含因子2的个数
    {
        while(n!=0)
            sum2+=n/2,n/=2;
    }
    int main()
    {
        int T, t=1, n, r, p, q;
        scanf("%d", &T);
        while(T--)
        {
            sum2=sum5=0;
            scanf("%d%d%d%d", &n, &r, &p, &q);
            int s=p;
            while(s%5==0)//求p中因子5的个数
                sum5++,s/=5;
            while(p%2==0)//求p中因子2的个数
                sum2++,p/=2;
            sum5*=q;sum2*=q;
            five(-r);two(-r);
            five(n);two(n);
            five(r-n);two(r-n);
            printf("Case %d: %d
    ", t++, min(sum5, sum2));
        }
    }
  • 相关阅读:
    微信分享接口
    MySQL的语句执行顺序
    C++类内存分布
    static_cast, dynamic_cast, reinterpret_cast, const_cast区别比较
    c++内存中字节对齐问题详解
    msyql中子查询IN,EXISTS,ANY,ALL,SOME,UNION介绍
    使用valgrind检查内存
    GDB调试教程
    Linux一些经典书籍
    C++编程语言学习资料
  • 原文地址:https://www.cnblogs.com/zhulei2/p/8195583.html
Copyright © 2011-2022 走看看