0
点赞
收藏
分享

微信扫一扫

1081. 度的数量 (数位dp)

桑二小姐 2022-04-30 阅读 15

题目
题意: 求给定区间 [X,Y] 中满足下列条件的整数个数:这个数恰好等于 K 个互不相等的 B 的整数次幂之和。
思路: 看似很复杂,我们仔细想想,类比二进制,把数字换成B进制就好了。如果转换以后恰好有k个1,那行,不然寄。比如11转3进制,有两个3^0,要求互不相等,那就寄了。恰好k个1就行。其余地方套用数位dp的模板即可。
时间复杂度: O(能过)
代码:

// Problem: 度的数量
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/1083/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<complex>
#include<cstring>
#include<cmath>
#include<vector>
#include<map>
#include<unordered_map>
#include<list>
#include<set>
#include<queue>
#include<stack>
#define OldTomato ios::sync_with_stdio(false),cin.tie(nullptr),cout.tie(nullptr)
#define fir(i,a,b) for(int i=a;i<=b;++i) 
#define mem(a,x) memset(a,x,sizeof(a))
#define p_ priority_queue
// round() 四舍五入 ceil() 向上取整 floor() 向下取整
// lower_bound(a.begin(),a.end(),tmp,greater<ll>()) 第一个小于等于的
// #define int long long //QAQ
using namespace std;
typedef complex<double> CP;
typedef pair<int,int> PII;
typedef long long ll;
// typedef __int128 it;
const double pi = acos(-1.0);
const int INF = 0x3f3f3f3f;
const ll inf = 1e18;
const int N = 2e5+10;
const int M = 1e6+10;
const int mod = 1e9+7;
const double eps = 1e-6;
inline int lowbit(int x){ return x&(-x);}
template<typename T>void write(T x)
{
    if(x<0)
    {
        putchar('-');
        x=-x;
    }
    if(x>9)
    {
        write(x/10);
    }
    putchar(x%10+'0');
}
template<typename T> void read(T &x)
{
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
int n,m,k,T;
int b;
int a[32];
int f[32][32]; //枚举i位,还需要有多少位b进制是1时的方案数.
int dfs(int cur,int sum,bool limit,bool lead)
{
	if(sum<0) return 0;
	if(cur==-1) return sum==0;
	if(!limit && !lead && ~f[cur][sum]) return f[cur][sum];
	int up = limit?a[cur]:(b-1);
	int ans = 0;
	for(int i=0;i<=min(up,1);++i)
	{
		ans += dfs(cur-1,sum-(i==1),limit&&i==up,lead&&i==0);
	}
	if(!lead && !limit) f[cur][sum] = ans;
	return ans;
}
int fun(int x)
{
	int pos=0;
	for(;x;x/=b) a[pos++]=x%b;
	return dfs(pos-1,k,1,1);
}
void solve()
{
   int l,r;
   cin>>l>>r>>k>>b;
   cout<<fun(r)-fun(l-1);
}
signed main(void)
{  
   mem(f,-1);
   T = 1;
   // OldTomato; cin>>T;
   // read(T);
   while(T--)
   {
   	 solve();
   }
   return 0;
}

举报

相关推荐

0 条评论