uva11440 Help Tomisu

本文介绍了一种算法,用于解决计数特定范围内具有大于指定阈值的质因子的整数的问题。通过预处理和利用数论中的欧拉函数性质,文章提供了一个高效的解决方案。

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

After wasting a significant time of his life in problem-setting, Mr.
Tomisu is now searching for glory: A glory that will make him famous
like Goldbach and rich like Bill Gates :). And he has chosen the field
of Number Theory as his prime interest. His creator did not make him
very bright and so he needs your help to solve an elementary problem,
using which he will begin his pursuit for glory! Tomisu has come to
know that finding out numbers having large prime factors are very
important in cryptography. Given two integers N and M, he aims to
count the number of integers X between 2 and N! (factorial N), having
the property that all prime factors of X are greater than M. Input The
input file contains at most 500 lines of inputs. Each line contains two
integers N (1 < N < 10000001) and M (1 ≤ M ≤ N and N −M ≤ 100000).
Input is terminated by a line containing two zeroes. This line should
not be processed. Output For each line of input produce one line of
output. This line contains the value T%100000007 (Modulo 100000007
value of T). Here T is the total number of numbers between 1 and N!
(factorial N) which have prime factors greater than M.

一个数所有质因子都小于m,等价于与m!互质。而对于大于m!的k,与m!互质等价于k%(m!)与m!互质。又因为n!是m!的整数倍,所以只需要求出来m!以内与m!互质的数【即phi(m!)】,再乘上n!/m!,即(m+1)* …* n。
记phi(m!)为pf(m),因为m!很大明显不能直接计算,可以递推预处理。
因为phi(x)=x(1-1/p1)(1-1/p2)…(1-1-pt),如果m不是质数,那么后面的部分都不发生变化,pf(m)=pf(m-1)* m。否则,就会多一个因子m,pf(m)=pf(m-1)* m* (1-1/m)=pf(m-1)* (m-1)。

#include<cstdio>
#include<cstring>
#define LL long long
const int mod=1e8+7,maxn=1e7;
int prm[1000010],phi_fac[10000010],tot;
bool have[10000010];
void makeprm()
{
    for (int i=2;i<=maxn;i++)
    {
        if (!have[i]) prm[++tot]=i;
        for (int j=1;j<=tot&&(LL)i*prm[j]<=maxn;j++)
        {
            have[i*prm[j]]=1;
            if (i%prm[j]==0) break;
        }
    }
}
void makephi()
{
    phi_fac[1]=phi_fac[2]=1;
    for (int i=3;i<=maxn;i++)
      if (!have[i]) phi_fac[i]=(LL)phi_fac[i-1]*(i-1)%mod;
      else phi_fac[i]=(LL)phi_fac[i-1]*i%mod;
}
int main()
{
    int i,j,k,m,n,p,q,x,y,z,ans;
    makeprm();
    makephi();
    while (scanf("%d%d",&n,&m)&&n)
    {
        ans=phi_fac[m];
        for (i=m+1;i<=n;i++) ans=(LL)ans*i%mod;
        printf("%d\n",(ans-1+mod)%mod);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值