这题有一点小坑点 就是AX^B A只能为0或者1 ,剩下的就比较好做的了。
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set> 10 using namespace std; 11 #define N 100000 12 #define LL long long 13 #define INF 0xfffffff 14 const double eps = 1e-8; 15 const double pi = acos(-1.0); 16 const double inf = ~0u>>2; 17 LL dp[35][35][11]; 18 int d[35]; 19 LL dfs(int i,int e,int k,int b) 20 { 21 if(i==-1) 22 return k==0; 23 if(!e&&dp[i][k][b] != -1) return dp[i][k][b]; 24 int mk = e?d[i]:1; 25 int ans = 0; 26 mk = min(1,mk); 27 for(int j = 0 ;j <= mk ;j++) 28 { 29 if(k-j>=0) 30 { 31 ans+=dfs(i-1,e&&j==d[i],k-j,b); 32 } 33 } 34 return e?ans:dp[i][k][b] = ans; 35 } 36 LL cal(int x,int k,int b) 37 { 38 int g = 0; 39 while(x) 40 { 41 d[g++] = x%b; 42 x/=b; 43 } 44 return dfs(g-1,1,k,b); 45 } 46 int main() 47 { 48 int x,y,k,b; 49 memset(dp,-1,sizeof(dp)); 50 while(cin>>x>>y) 51 { 52 cin>>k>>b; 53 cout<<cal(y,k,b)-cal(x-1,k,b)<<endl; 54 } 55 return 0; 56 }