公式

F - x = a^b 解説 by en_translator


We introduce an rather brute-force solution.
As is evident from Sample 2, a total of 1001003332 integers can satisfy the condition. Also, since \((10^9)^2 = 10^{18}\), \(10^9\) integers among them can be represented as \(a^2\).

Therefore, there are only \(10^6\) integers that cannot be represented as \(a^2\). Thus, one can enumerate all of them and then add the number of those represented as \(a^2\).

This enumeration can be done for \(b=3,4,\dots,59\) and then stopped once \(a^b > N\) is satisfied; one can determine if an integer \(x\) can be represented as \(a^2\) by checking if \(( \lfloor \sqrt{x} \rfloor )^2=x\).

Sample code (C++):

#include<bits/stdc++.h>

using namespace std;

long long safe_pow(long long a,long long b){
  long long res=1;
  for(long long i=0;i<b;i++){
    double dres=res;
    dres*=a;
    if(dres>2e18){return 2e18;}
    res*=a;
  }
  return res;
}

long long sqrt_floor(long long x){
  long long l=0,r=2e9;
  while(l<=r){
    long long t=(l+r)/2;
    if(t*t>x){r=t-1;}
    else{l=t+1;}
  }
  return r;
}

int main(){
  long long n;
  cin >> n;
  set<long long> st;
  for(long long b=3;b<60;b++){
    for(long long a=2;;a++){
      long long x=safe_pow(a,b);
      if(x>n){break;}
      long long s=sqrt_floor(x);
      if(s*s!=x){st.insert(x);}
    }
  }
  long long res=st.size();
  res+=sqrt_floor(n);
  cout << res << "\n";
  return 0;
}

投稿日時:
最終更新: