Lightoj 1090 - Trailing Zeroes (II)

Trailing Zeroes (II)

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
题目大意:
给你n, r, p, q四个数,问带入C(n,r)*p^q尾部有多少个0;
分析:
题目让我们找尾部有几个0,我们只需要找2和5的个数就行了(注意:10也能把2和5整除),我们只需开一个二维数组,用前缀和记录2和5的数目(当然你也可以开二个一维数组,分别记录2和5),它们二个谁小,小的那个的个数就是尾部0的个数。
(刚开始看这道题时,被题目所给范围吓住了1e6,
脑子有些短路,就只想对一个数取膜,怎么想都不对,到最后没办法了,只能看看别人怎么想的。当时瞬间哭了,我这脑子到底怎么想的,为什么就只想到取膜了。)
代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+10;
int num[1000010][2];
void init()
{
    memset(num,0,sizeof(num));
    for(int i=2;i<1000010;i++)
    {
        int x=i;
        num[i][0]=num[i-1][0];//记录2的个数
        while(x%2==0)
        {
            num[i][0]++;//当x能把2整除,就加1
            x=x/2;
        }
        int x1=i;
        num[i][1]=num[i-1][1];//同上,记录5
        while(x1%5==0)
        {
            num[i][1]++;//同上
            x1=x1/5;
        }
    }
}
int main()
{
    init();
    int t,n,r,q,p,k=1;
    cin>>t;
    while(t--)
    {
        cin>>n>>r>>p>>q;
        int s2=num[n][0]-num[r][0]-num[n-r][0]+(num[p][0]-num[p-1][0])*q;//计算2的总数目
        int s5=num[n][1]-num[r][1]-num[n-r][1]+(num[p][1]-num[p-1][1])*q;//同上
        cout<<"Case "<<k++<<": ";
        if(s2>s5)//要小的
            cout<<s5<<endl;
        else
            cout<<s2<<endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sigma函数是指一个数字的所有因子之和。给定一个数字n,需要求出有多少个数字的Sigma函数是偶数。\[2\] 为了解决这个问题,可以先筛选出n范围内的素数(范围在10^6即可),然后对n进行素因子分解。对于每个因子,如果它的Sigma函数中连乘的每一项都是偶数,那么整个Sigma函数就是偶数。具体实现中,可以判断每个因子的平方根是否为偶数,如果是偶数,则减去(平方根+1)/2。\[1\] 另外,还可以使用O(1)的做法来解决这个问题。根据观察,所有的完全平方数及其两倍的值都会导致Sigma函数为偶数。因此,可以直接计算n的平方根,然后减去(平方根+1)/2即可得到结果。\[3\] #### 引用[.reference_title] - *1* [Sigma Function](https://blog.csdn.net/PNAN222/article/details/50938232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [【LightOJ1336】Sigma Function(数论)](https://blog.csdn.net/qq_30974369/article/details/79009498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值