本文主要是介绍ZOJ 2836 Number Puzzle 容斥、lcm,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
这题和HDU 1796差不多。
code:
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;typedef long long LL;
const int MAXN = 1e5+5;int n, m;
int a[15];
inline LL gcd(LL x, LL y)
{return y?gcd(y, x%y):x;
}
inline LL lcm(LL x, LL y)
{return x/gcd(x, y)*y;
}
LL calc(int t)
{LL v = 1;int cnt = 0;for(int i = 0;i < n; i++){if(t&(1<<i)){cnt++;v = lcm(v, (LL)a[i]);}}LL ret = (LL)m/v;if(cnt%2 == 0) ret = -ret;return ret;
}
void solve()
{LL res = 0;for(int i = 1;i < (1<<n); i++)res += calc(i);cout<<res<<endl;
}int main()
{ios::sync_with_stdio(false);while(cin>>n>>m){for(int i = 0;i < n; i++)cin>>a[i];solve();}return 0;
}
这篇关于ZOJ 2836 Number Puzzle 容斥、lcm的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!