传送门:【HDU】3271 SNIBB
题目分析:第一问直接求,第二问就二分。水题。
代码如下:
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std ;
#define rep( i , a , b ) for ( int i = ( a ) ; i < ( b ) ; ++ i )
#define For( i , a , b ) for ( int i = ( a ) ; i <= ( b ) ; ++ i )
#define rev( i , a , b ) for ( int i = ( a ) ; i >= ( b ) ; -- i )
#define clr( a , x ) memset ( a , x , sizeof a )
int q , x , y , b , m , k ;
int dp[33][301] ;
int digit[33] ;
int dfs ( int cur , int limit , int sum ) {
if ( !cur ) return sum == m ;
if ( !limit && ~dp[cur][sum] ) return dp[cur][sum] ;
int ans = 0 , n = limit ? digit[cur] : b - 1 ;
For ( i , 0 , n ) ans += dfs ( cur - 1 , limit && i == n , sum + i ) ;
return limit ? ans : dp[cur][sum] = ans ;
}
int calc ( int n ) {
int n1 = 0 ;
while ( n ) {
digit[++ n1] = n % b ;
n /= b ;
}
return dfs ( n1 , 1 , 0 ) ;
}
void solve () {
clr ( dp , -1 ) ;
if ( x > y ) swap ( x , y ) ;
int L = calc ( x - 1 ) , R = calc ( y ) ;
if ( q == 1 ) {
printf ( "%d\n" , R - L ) ;
} else {
scanf ( "%d" , &k ) ;
if ( k > R - L ) {
printf ( "Could not find the Number!\n" ) ;
return ;
}
int l = x , r = y ;
while ( l < r ) {
int m = l + ( r - l ) / 2 ;
if ( calc ( m ) - L >= k ) r = m ;
else l = m + 1 ;
}
printf ( "%d\n" , l ) ;
}
}
int main () {
int cas = 0 ;
while ( ~scanf ( "%d%d%d%d%d" , &q , &x , &y , &b , &m ) ) {
printf ( "Case %d:\n" , ++ cas ) ;
solve () ;
}
return 0 ;
}