51nod 1799 two answers

Baseline Time Limit: 1 second Space Limit: 131072 KB Score: 40 Difficulty: Level 4 Algorithm Questions
collect
focus on
lyk is currently working on problems with binary answers.
For a positive integer sequence a with n distinct numbers from small to large (where the maximum value does not exceed n), to find a number m that has appeared in a, a correct binary program is like this :
 
1
2
3
4
5
6
l = 1 ;  r = n ;  mid = ( l + r ) / 2 ;
while  ( l <= r )
{
     if  ( a [ mid ] <= m )  l = mid +1 ;  else  r = mid -1 ;
     mid = ( l + r ) / 2 ;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

In the end a[r] must be equal to m.
But this harmonious program was disrupted by the bear child.
The bear boy shuffles the a array at the beginning. (There are n! possibilities)
lyk wants to know the expectation of final r=k.
Since decimals are cumbersome, all you need to do is multiply the answer by n! and modulo 1000000007.

In the example, there are 2 numbers, and there are two possibilities (1, 2) or (2, 1) for the sequence after being disrupted by the bear child, where (1, 2) after the above operations, r=1, (2 , 1) r=0 after the above operations. The expectation of r=k is 0.5, 0.5*2!=1, so output 1.



Input


3个整数n,m,k(1<=m<=n<=10^9,0<=k<=n)。


Output


one line for the answer


Input example


2 1 1


Output example


1


Ideas:
The key to changing the bisection range is mid. Assuming that mid>k at this time, there must be a[mid]>m, otherwise there must be a[mid]<=m.
Require r=k probability multiplied by n! Then it is equivalent to finding A(m , x ) * A ( n - m , y )*(nxy)! It is a permutation and combination problem. Because the factorial is too large to be calculated, it must be divided into pieces.
1e7--1e9 1e7 per segment.
Code:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int MOD = 1e9+7;
const int AX = 1e3+66;
int P[AX] = {682498929,491101308,76479948,723816384,67347853,27368307,625544428,199888908,888050723,927880474,281863274,661224977,623534362,970055531,261384175,195888993,66404266,547665832,109838563,933245637,724691727,368925948,268838846,136026497,112390913,135498044,217544623,419363534,500780548,668123525,128487469,30977140,522049725,309058615,386027524,189239124,148528617,940567523,917084264,429277690,996164327,358655417,568392357,780072518,462639908,275105629,909210595,99199382,703397904,733333339,97830135,608823837,256141983,141827977,696628828,637939935,811575797,848924691,131772368,724464507,272814771,326159309,456152084,903466878,92255682,769795511,373745190,606241871,825871994,957939114,435887178,852304035,663307737,375297772,217598709,624148346,671734977,624500515,748510389,203191898,423951674,629786193,672850561,814362881,823845496,116667533,256473217,627655552,245795606,586445753,172114298,193781724,778983779,83868974,315103615,965785236,492741665,377329025,847549272,698611116};
const int T = 1e7; //Do not use 1e7 as a constant here, it is likely to be wa
LL f( LL x ){
	int pos = x / T - 1;
	LL ans = P[pos];
	LL lim = T * (( x / T ));
	for( LL i = x ; i > lim ; i-- ){
		years = ( years * i ) % MOD;
	}
	return ans ;
}

int main(){
	LL n , m , k ;
	cin >> n >> m >> k ;
	int x = 0 , y = 0;
	LL res = 1LL;
	LL l = 1 , r = n ;
	LL mid ;
	while( l <= r ){
		mid = ( l + r ) >> 1 ;
		if( mid <= k ) { l = mid + 1; x++;}
		else { r = mid - 1 ; y ++; }
	}
	for( int i = m - x + 1 ; i <= m ; i ++ ){
		res *= i ;
		res% = MOD;
	}
	for( int i = n - m - y + 1 ; i <= n - m ; i ++ ){
		res *= i ;
		res% = MOD;
	}
	LL c3 ;
	if( n - x - y < 1e7 ){
		c3 = 1;
		for( int i = 2;  i <= n - x - y ; i++ ){
			c3 = c3 * i;
			c3% = MOD;
		}
	}else{
		c3 = f( n - x - y );		
	}
	res = res * c3 % MOD;
	cout << res << endl;
	return 0;
}



Guess you like

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