二进制枚举 40pts 求助!
查看原帖
二进制枚举 40pts 求助!
775490
JMCJimmyMaker楼主2023/6/17 17:33
//define and head file
#include <bits/stdc++.h>     //万能头文件
using namespace std;
//variables and structions
long long n, m, cnt = 0;
long long a[45];
//functions

//main function(run)
int main() {
	cin >> n >> m;
	for (int i = 0; i < n; i++) cin >> a[i];
	long long s = 1<<n;
	for (int u = 0; u <= s; u++)
	{
		long long sum = 0;
		for (int i = 0; i < n; i++)
		{
			if (u & (1<<i))
			{
				sum += a[i];
				//cout << i <<":" << a[i] << ";";
			}
		}
		if (sum <= m)
		{
			cnt++;
			//cout << endl << "sum=" << sum << endl;
		}
	}
	cout << cnt - 1; //我也不知道,减了才40
	return 0;
}

40pts

2023/6/17 17:33
加载中...