标签:
1 0 10 10 3 2 0 10 10 1 2 1 0 10 2 1
Case 1: 1 Case 2: 10 Case 3: 4
1 #include <bits/stdc++.h> 2 using namespace std; 3 using LL = long long; 4 int dp[45][310],bit[45],op,x,y,b,m,k; 5 int dfs(int len,int sum,bool flag){ 6 if(-1 == len) return sum == m; 7 if(!flag && dp[len][sum] != -1) return dp[len][sum]; 8 int ret = 0,u = flag?bit[len]:(b - 1); 9 for(int i = 0; i <= u; ++i) 10 ret += dfs(len - 1,sum + i,flag && i == u); 11 if(!flag) dp[len][sum] = ret; 12 return ret; 13 } 14 int solve(int n){ 15 if(n <= 0) return n == m; 16 int len = 0; 17 while(n){ 18 bit[len++] = n%b; 19 n /= b; 20 } 21 return dfs(len - 1,0,true); 22 } 23 int main(){ 24 int cs = 1; 25 while(~scanf("%d%d%d%d%d",&op,&x,&y,&b,&m)){ 26 memset(dp,-1,sizeof dp); 27 if(x > y) swap(x,y); 28 int p = solve(x - 1),q = solve(y); 29 printf("Case %d:\n",cs++); 30 if(op == 1) printf("%d\n",q - p); 31 else{ 32 scanf("%d",&k); 33 if(q - p < k){ 34 puts("Could not find the Number!"); 35 continue; 36 } 37 int low = x,high = y,ans; 38 while(low <= high){ 39 int mid = (static_cast<LL>(low) + high)>>1; 40 if(solve(mid) - p >= k){ 41 ans = mid; 42 high = mid - 1; 43 }else low = mid + 1; 44 } 45 printf("%d\n",ans); 46 } 47 } 48 return 0; 49 }
标签:
原文地址:http://www.cnblogs.com/crackpotisback/p/4960349.html