北方多校十四场_Easy Number_二分+容斥原理

来源:互联网 发布:java什么是反射 编辑:程序博客网 时间:2024/04/29 00:37

题目链接:https://nanti.jisuanke.com/t/15727


容斥原理公式:


#include<iostream>#include<cstdio>#include<algorithm>using namespace std;typedef long long LL;const int maxn = 12;const LL  inf  = 1e17;LL A[maxn];int T;int N, K;LL res;LL gcd(LL a, LL b){return b == 0 ? a : gcd(b, a%b);}// 容斥原理 dfs// it该层处理哪个数,now已有最小公倍数,ub上限// f这一项的正负号,这时的now是不是初始默认的那个1void dfs(int it, LL now, LL ub, int f, bool ori){if(now > ub) return;if(it == N){if(ori) return;res += f * ub / now;return;}dfs(it+1, now, ub, f, ori);dfs(it+1, now/gcd(now, A[it])*A[it], ub, -f, 0);}bool Judge(LL x){// 容斥原理,可别忘了初始化 resres = 0;dfs(0, 1, x, -1, 1);if(res >= K) return true;return false;}int main(){scanf("%d", &T);while(T--){scanf("%d %d", &N, &K);for(int i= 0; i< N; i++)scanf("%lld", A+i);LL lb = 0, ub = inf;while(lb <= ub){LL mid = (ub - lb) / 2 + lb;if(Judge(mid)) ub = mid - 1;else lb = mid + 1;}cout << ub + 1 << endl;}return 0;}


原创粉丝点击