求助HDU1746
  • 板块学术版
  • 楼主FLY_lai
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/7/14 16:29
  • 上次更新2024/7/14 16:30:10
查看原帖
求助HDU1746
488052
FLY_lai楼主2024/7/14 16:29

题目大意:给定一个集合,求 1∼n−11\sim n-1 有多少个能被集合中至少一个数整除。n<231,∣S∣≤10n<2^{31},|S|\le 10

#include <iostream>
#include <algorithm>

using namespace std;
typedef long long ll;

ll n, m;
ll a[15];

ll gcd(ll x, ll y) {
	if (y == 0)
		return x;
	return gcd(y, x % y);
}
ll lcm(ll x, ll y) {
	return x / gcd(x, y) * y;
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= m; i++)
		cin >> a[i];
	ll ans = 0;
	for (int S = 0; S < (1 << m); S++) {
		ll L = 1, cnt = 0;
		for (int i = 0; i < m; i++)
			if ((S >> i) & 1) {
				cnt++;
				L = lcm(L, a[i + 1]);
				if (L > n)
					break;
			}
		if (cnt % 2 == 1)
			ans -= n / L;
		else
			ans += n / L;
	}
	cout << n - 1 - ans << endl;
	return 0;
}
2024/7/14 16:29
加载中...