先贴代码后解释
#include#include using namespace std; #define LL long long #define N 11 LL num[N],ans,n; int m,cnt; LL gcd(LL a,LL b) { int t; while(b) { t=a%b; a=b; b=t; } return a; } void dfs(int id,bool flag,LL LCM) { LCM=num[id]/gcd(num[id],LCM)*LCM; if(flag)ans+=n/LCM; else ans-=n/LCM; int i; for(i=id+1;i
DFS看不懂的话 一定去自己按树模拟一下,这是对难解的代码的最好方法之一
第一:小于n的数 所以n--别忘
第二:flag为1,说明是奇数层,加,为0,说明是偶数层要减
第三:小心 题中说的是非负数 所以判断是不是0
第四:注意DFS做容斥的方法,模拟一下 真的好理解