zl程序教程

您现在的位置是:首页 >  其它

当前栏目

CF1619B Squares and Cubes 题解

and 题解
2023-06-13 09:12:17 时间

这道题就是容易想复杂,其实想明白很简单。

我的思路是,从 1\sqrt{n} 循环筛一下 完全平方数,再从 1 \sqrt[3]{n} 循环筛一下 完全立方数,去掉重复的数字,然后输出 size。

为了方便去重,我们用 set 来存储筛出的数。

Code:

#include <iostream>
#include <cmath>
#include <set>

int t, n;

int main()
{
    std::cin >> t;
    while (t--)
    {
        std::cin >> n;
        std::set <int> table;
        for (int i = 1; i * i <= n; i++)
        {
            table.insert(i * i);
        }
        for (int i = 1; i * i * i <= n; i++)
        {
            table.insert(i * i * i);
        }
        std::cout << table.size() << std::endl;
    }
    return 0;
}