帮忙解释一下求互质个数的代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
int prime[30];
int top;
int cas;
LL co_prime(LL val)
{
LL ans = 0;
for (int i = 1; i < (1 << top); i++)
{
LL tmp = 1, flag = 0;
for (int j = 0; j < top; j++) if (i&(1 << j))
tmp *= prime[j], flag++;
if (flag & 1)
ans += val / tmp;
else
ans -= val / tmp;
}
return val - ans;
}
void work()
{
LL a, b;
int n;
scanf("%I64d%I64d%d", &a, &b, &n);
top = 0;
for (int i = 2; i*i <= n; i++) if (n%i==0)
{
prime[top++] = i;
while (n%i == 0) n /= i;
}
if (n > 1)
prime[top++] = n;
printf("Case #%d: %I64d\n", ++cas, co_prime(b) - co_prime(a-1));
}
int main()
{
int T;
scanf("%d", &T);
while (T--)
work();
}
里边的这几行代码怎么理解?明白容斥原理我不理解val/tmp代表什么?如果不能整除呢?
if (flag & 1)
ans += val / tmp;
else
ans -= val / tmp;