12 2 2 3
7
Source
#include <cstdio>
#include <cstring>
#include <algorithm>
#define ll long long
using namespace std;
int a[25], b[25];
ll ans;
int n, m, cnt;
ll gcd(ll a, ll b)
{
return b ? gcd(b, a % b) : a;
}
ll lcm(int a, int b)
{
return (ll) a * b / gcd(a, b);
}
void DFS(int idx, ll cur, int sgin)
{
for(int i = idx; i < cnt; i++)
{
ll tmp = lcm(cur, (ll)b[i]);
ans += (ll) sgin * (n / tmp);
DFS(i + 1, tmp, -sgin);
}
}
int main()
{
while(scanf("%d %d", &n, &m) != EOF)
{
n --;
cnt = 0;
bool flag = false;
for(int i = 0; i < m; i++)
{
scanf("%d", &a[i]);
if(a[i] != 0)
b[cnt ++] = a[i];
if(a[i] == 1)
flag = true;
}
if(flag)
{
printf("%d\n", n);
continue;
}
sort(b, b + cnt);
ans = 0;
DFS(0, 1, 1);
printf("%I64d\n", ans);
}
}版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 1796 How many integers can you find (lcm + 容斥)
原文地址:http://blog.csdn.net/tc_to_top/article/details/47700383