本文共 2615 字,大约阅读时间需要 8 分钟。
首先题目等价于求出满足运行二分程序后最后r=k的排列种数。
显然对于这样的二分程序,起作用的只有mid点,mid处的值决定了接下来要递归的子区间。
于是可以一遍二分求出有多少个mid点处的值<=m,有多少个mid点处的值>m,不妨设为x和y,
那么由组合数学可以推出最后的答案是 C(x,m)*C(y,n-m)*(n-x-y)!%MOD.
由于x和y很小,所以前面两个组合数可以暴力算出来。而后面的阶乘显然是不能直接求的。
打表的话n<=1e9,显然会MLE,于是把n分成100块,预处理出n=1e7,2e7...3e7...1e9的答案。
然后在块内暴力即可。
显然时间复杂度为O(logn+n/100).
# include # include # include # include # include # include # include # include # include # include # include # include using namespace std;# define lowbit(x) ((x)&(-x))# define pi acos(-1.0)# define eps 1e-8# define MOD 1000000007# define INF 1000000000# define mem(a,b) memset(a,b,sizeof(a))# define FOR(i,a,n) for(int i=a; i<=n; ++i)# define FO(i,a,n) for(int i=a; i PII;typedef vector VI;# pragma comment(linker, "/STACK:1024000000,1024000000")typedef long long LL;int Scan() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}const int N=105;//Code begin...int mod[N]={ 1,924724006,582347126,500419162,881147799,693776109,435873621,279027658,727951124,398578768,678364145,204828554,345795998,116118093,359401113,236930793,856493327,207383191,617606889,933753281,26701748,329394893,360779992,416008308,187501984,165706817,328891607,16385287,117411011,404196042,765064133,239669664,761588352,566114869,673499119,840260100,352356536,53839501,178657924,373444237,227300165,207172723,444208499,367531373,297449176,605324209,729265513,567907756,125889461,250743107,666666670,598576559,632705086,295855233,185718228,414607857,737215408,863388390,182290465,707552496,881713600,417895708,490627919,364521407,775935292,972492338,473340273,920880265,530581,696910290,64037482,649527920,756691728,283805222,711255329,825205499,263679166,341083474,914727729,919247968,465317279,960145703,274813468,393588827,65909169,521964827,794328994,484551338,521297378,54488990,591837535,255746228,25827429,177799409,92011129,469664591,35708489,197025781,288851931,254032854};int get(int x){ if (x==0) return 1; int i=mod[(x-1)/10000000], j=(x-1)%10000000; LL c=x/10000000*10000000+1; FOR(y,1,j) i=(LL)i*(c+y)%MOD; return i;}int main (){ int n, m, k, x=0, y=0; scanf("%d%d%d",&n,&m,&k); int l=1, r=n, mid; while (l<=r) { mid=(l+r)>>1; if (mid<=k) l=mid+1, ++x; else r=mid-1, ++y; } LL ans=1; for (int i=m; i>=m-x+1; --i) ans=ans*i%MOD; for (int i=n-m; i>=n-m-y+1; --i) ans=ans*i%MOD; printf("%lld\n",ans*get(n-x-y)%MOD); return 0;}
转载于:https://www.cnblogs.com/lishiyao/p/7010331.html