Reprinted please indicate the source, thank you http://blog.csdn.net/acm_cxlove/article/details/7854526
By --- cxlove
Question: If there is no square factor, P ^ 2 will not divide any prime number P, such as 1.
, 5 = 5, 15 = 3*5 are all non-square factors, while 20 = 2 ^ 2*5 is not. Given an n
(1 <= n <10 ^ 12), calculates the number of non-square factors in the range [1, N.
Http://acm.uestc.edu.cn/problem.php? PID = 1, 1720
Water and water ~~~~ Bytes
Output the prime number table, and then find out the number of square factors in the interval. Apparently, this is the principle of rejection.
Previously, I used the enumeration method. It was always TLE, and I suspect that I would not write it again. Sorry.
Then, because of the overflow, the posture is incorrect. TLE and Wa are also a few times. It is really weak.
#include<iostream>#include<cstdio>#include<cstring>#include<queue>#include<cmath>#include<string>#include<vector>#include<algorithm>#include<map>#include<set>#define maxn 200005#define eps 1e-8#define inf 1<<30#define LL long long#define zero(a) fabs(a)<eps#define MOD 19901014#define N 1000005using namespace std;bool flag[N]={0};int prime[N],cnt=0;void Prime(){for(int i=2;i<N;i++){if(flag[i]) continue;prime[cnt++]=i;for(int j=2;j*i<N;j++)flag[i*j]=true;}}LL n,ans;LL dfs(int idx,LL num){LL ret=0;for(int i=idx;i<cnt&&(LL)prime[i]*prime[i]<=num;i++)ret+=num/prime[i]/prime[i]-dfs(i+1,num/prime[i]/prime[i]);return ret;}int main(){Prime();int t;scanf("%d",&t);while(t--){scanf("%lld",&n);printf("%lld\n",n-dfs(0,n));}return 0;}