D - Again Prime? No Time.

The problem statement is very easy. Given a number n you have to determine the largest power of m, not necessarily prime, that divides n!.
Input
The input file consists of several test cases. The first line in the file is the number of cases to handle. The following lines are the cases each of which contains two integers m (1 < m < 5000) and n (0 < n < 10000). The integers are separated by an space. There will be no invalid cases given and there are not more that 500 test cases.
Output
For each case in the input, print the case number and result in separate lines. The result is either an integer if m divides n! or a line ‘Impossible to divide’ (without the quotes). Check the sample input and output format.
Sample Input
2 2 10 2 100
Sample Output
Case 1: 8 Case 2: 97

英语读题还是有些问题的,比如那个!原来是阶乘的意思,不是简单的感叹号。

我以为此题是用公式,结果这个只要观察就行了,还是知识点不扎实,导致搞不清题目考的是什么

首先不是素数,所以要分解m质因数,至于如何找n!的相关数字的指数,要用到n/i+n/i/i+n/i/i/i...表示n的i的指数,n/i是I的倍数个数,N/I/I是I*I的倍数,以此类推,由于I*I的个数包含在了I的倍数里,相当于已经算过一次了,所以N/i/i不用乘以2,同理N/i/i/i不用乘以3,这个应该是数学初等数论里面的,听人说过。是非常基础的。

#include<bits/stdc++.h>
using namespace std;
const int minn=0x3f3f3f3f;
int initial=2;int ddcase=1;
int main()
{
    int n,m;
    int t;
    cin>>t;
    while(t--)

    {cin>>m>>n;initial=2;
    int p=0;int ans=minn;
    while(m!=1)
    {
        p=0;
        while(m%initial==0)
        {
            p++;
            m/=initial;
        }
        if(p)
        {
            int temp=n;int total=0;
            while(temp!=0)
            {
                total+=temp/initial;
                temp/=initial;
            }
            ans=min(ans,total/p);

        }
        initial++;
    }
    printf("Case %d:\n",ddcase++);
    if(ans) cout<<ans<<endl;
    else cout<<"Impossible to divide"<<endl;}
    return 0;
}

还有0x3f3f3f3f表示很大值

附上稍微具体点的https://blog.csdn.net/qq_37867156/article/details/81837545

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值