"JXOI2018" game

 

 

    Note that the output should be the sum of all scenarios, not the expectation.

    We might as well map the dependencies, and we can find that all the points without in-degree have been checked by the water meter once, which is a necessary and sufficient condition for the game to end.

    So we just need to know how many points are not in-degree, and then arrange and calculate the ojbk.

     The former is a number theory problem, we greedily put a number/his smallest prime factor, if < L, it means that it has no in-degree. . (except 1)

    The latter is a basic permutation count of 23333

 

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e7,ha=1e9+7;
inline int add(int x,int y){ x+=y; return x>=ha?x-ha:x;}
inline int ksm(int x,int y){ int an=1; for(;y;y>>=1,x=x*(ll)x%ha) if(y&1) an=an*(ll)x%ha; return an;}
int zs[maxn/10],t=0,low[maxn+5],ans;
int L, R, N, jc [maxn + 5], ni [maxn + 5], n;
bool v[maxn+5];
inline int P(int x,int y){ return x<y?0:jc[x]*(ll)ni[x-y]%ha;}

inline void init(){
	low[1]=233;
	for(int i=2;i<=R;i++){
		if(!v[i]) zs[++t]=i,low[i]=i;
		for(int j=1,u;j<=t&&(u=zs[j]*i)<=R;j++){
			v[u]=1,low[u]=zs[j];
			if(!(i%zs[j])) break;
		}
	}
	
	jc[0]=1;
	for(int i=1;i<=N;i++) jc[i]=jc[i-1]*(ll)i%ha;
	ni [N] = ksm (jc [N], ha-2);
	for(int i=N;i;i--) ni[i-1]=ni[i]*(ll)i%ha;
}

inline void solve(){
	for(int i=L;i<=R;i++) if(i/low[i]<L) n++;
	ans=N*(ll)P(N,n)%ha*(ll)jc[N-n]%ha;
	for(int i=N-1;i>=n;i--) ans=add(ans,ha-P(i,n)*(ll)jc[N-n]%ha);
}

int main(){
	scanf("%d%d",&L,&R),N=R-L+1,init();
	solve(),printf("%d\n",ans);
	return 0;
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325813622&siteId=291194637