当先锋百科网

首页 1 2 3 4 5 6 7

传送门


考虑有一些房间是必须要去的
其实就是把 [ l , r ] [l,r] [l,r]的数做线性筛后剩下的那些质数和没被筛去的合数
设有 x x x
然后考虑实际上要找到就是每个情况最后一个要去的在第几个
考虑枚举最后一个数的位置
a n s = ∑ i = 1 n ( i − 1 x − 1 ) ∗ x ! ∗ ( n − x ) ! ans=\sum_{i=1}^{n}{i-1\choose x-1}*x!*(n-x)! ans=i=1n(x1i1)x!(nx)!

预处理组合数,质数和筛 [ l , r ] [l,r] [l,r]都可以做到 O ( n ) O(n) O(n)

#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ob==ib)?EOF:*ib++;
}
#define gc getchar
inline int read(){
    char ch=gc();
    int res=0,f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
#define ll long long
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define cs const
#define poly vector<int>
cs int mod=1e9+7;
inline int add(int a,int b){return a+b>=mod?a+b-mod:a+b;}
inline void Add(int &a,int b){a=add(a,b);}
inline int dec(int a,int b){return a>=b?a-b:a-b+mod;}
inline void Dec(int &a,int b){a=dec(a,b);}
inline int mul(int a,int b){return 1ll*a*b>=mod?1ll*a*b%mod:a*b;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,a=mul(a,a))(b&1)?(res=mul(res,a)):0;return res;}
inline void chemx(int &a,int b){a<b?a=b:0;}
inline void chemn(int &a,int b){a>b?a=b:0;}
cs int N=10000007;
int fac[N],ifac[N],pr[N],tot;
bitset<N> vis;
inline void init(int len){
    ifac[0]=fac[0]=1;
    for(int i=1;i<=len;i++)fac[i]=mul(fac[i-1],i);
    ifac[len]=ksm(fac[len],mod-2);
    for(int i=len-1;i;i--)ifac[i]=mul(ifac[i+1],i+1);
    for(int i=2;i<=len;i++){
        if(!vis[i])pr[++tot]=i;
        for(int j=1;j<=tot&&i*pr[j]<=len;j++){
            vis[i*pr[j]]=1;
            if(i%pr[j]==0)break;
        }
    }
}
inline int C(int n,int m){
    if(n<m)return 0;
    return mul(fac[n],mul(ifac[m],ifac[n-m]));
}
int n,x;
int main(){
    int l=read(),r=read();n=r-l+1;
    init(r);
    vis.reset();
    for(int i=l;i<=r;i++){
        if(!vis[i])x++;
        for(int j=1;j<=tot&&i*pr[j]<=r;j++){
            vis[i*pr[j]]=1;
            if(i%pr[j]==0)break;
        }
    }
    int res=0;
    for(int i=1;i<=n;i++)
        Add(res,mul(i,mul(C(i-1,x-1),mul(fac[x],fac[n-x]))));
    cout<<res;
}