ural1057 Amount of Degrees

时间:2024-04-13 11:33:37

链接

这题有一点小坑点 就是AX^B  A只能为0或者1  ,剩下的就比较好做的了。

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 100000
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
LL dp[][][];
int d[];
LL dfs(int i,int e,int k,int b)
{
if(i==-)
return k==;
if(!e&&dp[i][k][b] != -) return dp[i][k][b];
int mk = e?d[i]:;
int ans = ;
mk = min(,mk);
for(int j = ;j <= mk ;j++)
{
if(k-j>=)
{
ans+=dfs(i-,e&&j==d[i],k-j,b);
}
}
return e?ans:dp[i][k][b] = ans;
}
LL cal(int x,int k,int b)
{
int g = ;
while(x)
{
d[g++] = x%b;
x/=b;
}
return dfs(g-,,k,b);
}
int main()
{
int x,y,k,b;
memset(dp,-,sizeof(dp));
while(cin>>x>>y)
{
cin>>k>>b;
cout<<cal(y,k,b)-cal(x-,k,b)<<endl;
}
return ;
}