您的位置:首页 > 其它

容斥原理(模板)

2016-09-22 11:12 211 查看
模板:递归版(好理解)

其实打表是更慢的而且很可能根本打不下,因为打表实际上是依次枚举所有的方案,如果被除数较多,就无法枚举方案数了

//容斥原理,结果存在sub中
long long sub = 0;
void dfs(int id,int deep,long long sum)
{
// cout<<sum<<endl;
long long temp;
for(int i = id; i < primenum; i ++)
{
temp = sum * prime[i];
// cout<<temp<<endl;
if(temp > n) //避免溢出
{
return;
}
if(deep % 2 == 0)
{
sub -= n / temp;
}
else
{
sub += n / temp;
}

dfs(i + 1,deep + 1,temp);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息