hdu 2204 Eddy's爱好 容斥

本文介绍了一种使用质数分解和容斥原理解决特定数学问题的方法,并提供了一个C++实现示例,该示例利用深度优先搜索来计算特定条件下整数的选择总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据指数的质数分解进行容斥 比如 k = a^3 且 k = b ^ 5,那么 k = c ^ 15
所以 a 的可选个数 加 b的可选个数减去c的可减个数就是总的个数了因为 a^3和b^5有重复的

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#define LL long long
using namespace std;
int X[50] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61};
LL ans = 0, n;
void dfs(int pre, int index, int am)
{
     if(pre >= 62) return ;
     if(am != 0)
     {
         LL tmp = pow(n * 1.0, 1.0 / pre) + 1e-6;
         tmp--;
         if(tmp > 0)
         {
           if(am & 1) ans += tmp;
           else ans -= tmp;
         }
     }
     for(int i = index; X[i] < 61; i++)
         dfs(pre * X[i], i + 1, am + 1);
}
int main()
{
    while(scanf("%I64d", &n)!=EOF)
    {
        ans = 1;
        dfs(1, 0, 0);
        printf("%I64d\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值