UESTC 1720 Number of non-square factors (number theory, refresh)

Source: Internet
Author: User

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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.