P8764 [蓝桥杯 2021 国 BC] 二进制问题

发布于:2024-03-29 ⋅ 阅读:(44) ⋅ 点赞:(0)

很板的一道题目,注意就是数组别开的太小

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int,int>;
#define int long long
const int N = 1e5+10;
const int inf = 0x3f3f3f3f;
const int mod = 1e9+7;
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int lcm(int a,int b){return a*b/gcd(a,b);}
int qmi(int a,int b,int mod){int res=1;while(b){if(b&1)res=res*a%mod;b>>=1;a=a*a%mod;}return res;}


int n,q,m;

// int e[N],ne[N],w[N],h[N],idx;
// void add(int a,int b,int c){
	// e[idx] = b,ne[idx] = h[a],w[idx] = c,h[a] = idx++;
// }

int a[N];
int dp[95][60];
int k;
int dfs(int len,int s,int limit){
	if(s>k)return 0;
	if(!len)return s==k;
	if(!limit&&~dp[len][s])return dp[len][s];
	int up = limit?a[len]:1;
	int res = 0;
	for(int i=0;i<=up;++i){
		res+=dfs(len-1,s+i,limit&&i==up);
	}
	
	if(!limit)return dp[len][s]=res;
	return res;
	
	
}





int cal(int x){
	memset(dp,-1,sizeof dp);
	int len = 0;
	while(x)a[++len] = x%2,x/=2;
	return dfs(len,0,1);
}


void solve()
{
	cin>>n>>k;
	cout<<cal(n);

}

signed main()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	int _;
	//cin>>_;
	_ = 1;
	while(_--)solve();
	return 0;
}


网站公告

今日签到

点亮在社区的每一天
去签到