小tips

一、二进制枚举

1:____builtin_popcount(i)函数的作用是求出i对应的二进制表达式中有几个1

2:当用visual studio编译的时候,需要加上这两行代码,该函数才能正常使用,在网上正式提交答案的时候,需要自行再把这两行删了。

#define __builtin_popcount __popcnt 
#define __builtin_popcountl __popcnt

3:对应题目链接:洛谷1036题

4:完整代码如下:

#include<iostream>
#include<algorithm>
#include<string>
#include<cstring>
#include<cmath>
#define __builtin_popcount __popcnt 
#define __builtin_popcountl __popcnt
using namespace std;

int nums[25];
bool func(int x) {
	int k = sqrt(x);
	for (int i = 2; i <= k; i++) {
		if (x % i == 0)return 0;
	}
	return 1;
}
int main() {
	int n, k, ans = 0;
	cin >> n >> k;
	for (int i = 0; i < n; i++) {
		cin >> nums[i];
	}
	int x = 1 << n;
	for (int i = 0; i < x; i++) {
		if (__builtin_popcount(i) == k) {
			int sum = 0;
			for (int j = 0; j < n; j++) {
				if(i&(1<<j))sum+=nums[j];
			}
			if (func(sum))ans++;
		}
	}
	cout << ans << endl;
	return 0;
}

二、二分快速幂模板

#include<iostream>
using namespace std;
#define ll long long
ll mod(ll a, ll b, ll c) {
	ll sum = 0;
	while (b) {
		if (b & 1)sum = (sum + a) % c;
		a = (a + a) % c;
		b >>=1;
	}
	return sum;
}
ll func(ll a, ll b, ll c) {
	ll ans = 1;
	while (b) {
		if (b & 1)ans = mod(ans, a, c);
		a = mod(a, a, c);
		b >>= 1;
	}
	return ans;
}

int main() {
	ll a, b, c;
	cin >> a >> b >> c;
	cout << a << "^" << b << " mod " << c << "=" << func(a, b, c) << endl;
	return 0;
}

 三、double保留二位小数并转string

string func(const double &val){
		char* chCode;
		chCode = new char[20];
		sprintf(chCode, "%.2lf", val);
		string str(chCode);
		delete[]chCode;
		return str;
	}

四 生成随机数函数

mt19937 gen{random_device{}()};
uniform_real_distribution<double> dis(0,1);
double x=gen(dis),y=gen(dis);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值