SPOJ:Factorial Modulo(数论)

本文探讨了如何找出能被整数a整除但不能被整数b整除的数的阶乘个数,通过计算最小的x!来解决该问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

FCDC - Factorial Modulo


You are given 2 integers a, b. Find the number of i for which i! is divisble by a but not b. if i! is divisible by a and b, then you should not count that i.

Input

One line that contains a and b.

Output

Output the result in one line.

Example

Input:
2 3

Output:
1

Constraints

 1 ≤ a ≤ b ≤ 107

Explanation

2! is the only factorial which is divisible by 2 and not divisible by 3.

题意:给出a,b,问有多少个数的阶乘可以整除a而不能整除b。

思路:加如x!可以整除a,那么(x+1)!,(x+2)!......都可以整除a,那么找出各自最小的x!然后作差即可。

# include <bits/stdc++.h>
using namespace std;

int get(int a, int b)//计算最小的x,使得X!分解质因数有a个b。
{
    int dp[35]={0};
    for(int i=1;;++i)
    {
        dp[i] = i+dp[i/a];
        if(dp[i] >= b)
            return i*a;
    }
}
int cal(int n)
{
    int t = 0;
    for(int i=2; i<=sqrt(n); ++i)
    {
        if(n%i==0)
        {
            int icount = 0;
            while(n%i==0)
            {
                n /= i;
                ++icount;
            }
            t = max(t, get(i, icount));//取最大的。
        }
    }
    if(n > 1) t = max(t, n);
    return t;
}
int main()
{
    int a, b, t1, t2;
    scanf("%d%d",&a,&b);
    if(b==1) return puts("0");
    t1 = cal(a);
    t2 = cal(b);
    if(a==1) printf("%d\n",t2-1);
    else printf("%d\n",max(t2-t1, 0));
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值