跳到正文

剪贴板

剪贴板 ptwvdu2b

原文
剪贴板信息

剪贴板信息

收录
更新
C++
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 25;
int n, m;
LL k, a[N], res, C[N][N];
int main()
{
    // Read n, m, k [omitted]
    // Precalculate the binomial coefficients [omitted]
    // Read in array a [omitted]
    // Enumerate the set of cicadas
	for(int i = 1; i < (1 << n); i ++) {
		int cnt = 0, v = 1;
		for(int j = 0; j < n; j ++)
			if(i >> j & 1)
				cnt ++, v = -v;
		if(cnt >= m) {
			LL s = 1;
			bool flag = true;
			for(int j = 0; j < n; j ++)
				if(i >> j & 1) {
					LL g = gcd(a[j], s);
					if(a[j] / g > k / s) { // When j is added, the total lcm of the numbers exceeds k
						flag = false;
						break;
					}
					s *= a[j] / g; // Update lcm
				}
			if(flag) s = k / s; // If lcm does not exceed k, there are k / s valid years
			else s = 0; // Otherwise, there are 0 valid years
			if(m & 1) v = -v; // Adjust the sign according to m's parity
			res += v * C[cnt][m] * s; // Accumulate the contribution of the current subset to the final answer
		}
	}
	printf("%lld\n", res);
	return 0;
}