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);
}
}