洛谷1036F Relatively Prime Powers(構造)(二分)
阿新 • • 發佈:2018-11-02
題意
定義一個數x合法為x無法表示成a^k(k!=1)。
給出T個詢問,求小於n內不合法的數的個數。
特性
不合法的數一定是一個數的幾次方,即如果所有的a^k的數。
題解
構造+二分
不妨構造出所有的a^k的數,但是這些數整容太龐大了。
我們考慮去掉所有a^2的數,這樣規模就控制在了可行範圍內。
最後的時候減掉n之內a^2的數就可以了,這些數有sqrt(n)個。
提醒一句,注意精度的問題。
程式碼
#include<cmath> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; const int MAXN=3000000; const ll LIM=1e18; ll f[1003500];int tot=0; inline bool check(ll x) { ll t=sqrt(x); return t*t<x; } int main() { for(ll i=2;i<=1000000;i++) for(ll x=i*i;x<=LIM/i;) { x*=i; if(check(x)) f[++tot]=x; } sort(f+1,f+tot+1); tot=unique(f+1,f+tot+1)-(f+1); int T;scanf("%d",&T); while(T--) { ll x;scanf("%lld",&x); ll ans=x-(upper_bound(f+1,f+tot+1,x)-f-1)-(ll)sqrt(x);//debug sqrt(x)需要ll由double轉為 ll printf("%lld\n",ans); } return 0; }