Caddi Programming Contest 2021(AtCoder Beginner Contest 193)-C - Unexpressed-题解


General idea

Give you a number n, you can find some integers a and b greater than 1, if c = ab <nc=a^{b}<nc=ab<n , thenccc is an "expandable number".
Q 1 ~ n is "notto expand the number of" how much.


Problem-solving ideas

2 34 = 17 , 179 , 869 , 184 > 10 , 000 , 000 , 000 2^{34}=17,179,869,184>10,000,000,000 234=17,179,869,184>10,000,000,0 0 0 , which means that there are less than 35 eligible numbers in the range of 1e10 with a base of 2. When the base number increases by 10, there are less than 10 eligible numbers with the base 10 as the base in the 1e10 range. In short, the expandable number is less than the unexpandable number.
(In Example 2, there are 99,634 unexpandable numbers in the range of 100,000, and it can be seen that there are very few expandable numbers).

We only need to calculate the number of expandable numbers to calculate the answer.

When calculating the number of expandable numbers, it should be noted that b>2.
And n \sqrt{n}n The square of is already equal to n. Gu Dang base is greater than n \sqrt{n}n At that time, the base no longer needs to be increased. Even if it is a number of 1e10, the enumeration range of the base is only 2~1e5, which is within the allowable range of time.


AC code

#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
map<ll, bool> mp; //防止重复计算
int main()
{
    
    
    ll n, s = 0;
    cin >> n;
    ll k = sqrt(n);
    for (ll a = 2; a <= k; a++) //底数从2枚举到√n
    {
    
    
        ll b = 2; //指数从2开始枚举
        while (1)
        {
    
    
            ll c = pow(a, b);
            if (c > n) //当a^b大于n时,结束循环
                break;
            if (!mp[c]) //如果还没有出现过
            {
    
    
                mp[c] = 1; //记录下这个可展开数
                s++;
            }
            b++;
        }
    }
    printf("%lld\n", n - s);
    return 0;
}

Guess you like

Origin blog.csdn.net/Tisfy/article/details/114195632