I. Lonely Numbers
In number world, two different numbers are friends if they have a lot in common, but also each one has unique perks.
More precisely, two different numbers a a a and b b b are friends if g c d ( a , b ) gcd(a,b) gcd(a,b), a g c d ( a , b ) \frac{a}{gcd(a,b)} gcd(a,b)a, b g c d ( a , b ) \frac{b}{gcd(a,b)} gcd(a,b)b can form sides of a triangle.
Three numbers a a a, b b b and c c c can form sides of a triangle if a + b > c a + b > c a+b>c, b + c > a b + c > a b+c>a and c + a > b c + a > b c+a>b.
In a group of numbers, a number is lonely if it doesn’t have any friends in that group.
Given a group of numbers containing all numbers from 1 , 2 , 3 , . . . , n 1, 2, 3, ..., n 1,2,3,...,n, how many numbers in that group are lonely?
Input
The first line contains a single integer t t t ( 1 ≤ t ≤ 1 0 6 ) (1 \leq t \leq 10^6) (1≤t≤106) - number of test cases.
On next line there are t t t numbers, n i n_i ni ( 1 ≤ n i ≤ 1 0 6 ) (1 \leq n_i \leq 10^6) (1≤ni≤106) - meaning that in case i i i you should solve for numbers 1 , 2 , 3 , . . . , n i 1, 2, 3, ..., n_i 1,2,3,...,ni
Output
For each test case, print the answer on separate lines: number of lonely numbers in group 1 , 2 , 3 , . . . , n i 1, 2, 3, ..., n_i 1,2,3,...,ni.
Example
Input
3
1 5 10
Output
1
3
3
Note
For first test case, 1 1 1 is the only number and therefore lonely.
For second test case where n = 5 n=5 n=5, numbers 1 1 1, 3 3 3 and 5 5 5 are lonely.
For third test case where n = 10 n=10 n=10, numbers 1 1 1, 5 5 5 and 7 7 7 are lonely.
code
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e6+10,INF=0x3f3f3f3f,mod=1e9+7;
typedef pair<int,int> PII;
int T=1;
int a[N];
bool st[N];
int b[N];
int c[N];
int d[N];
int sum[N];
int cnt;
void f(int n){
for(int i=2;i<=n;i++)
{
if(!st[i]) a[cnt++] = i;
if(a[cnt-1]*a[cnt-1]<=N) b[a[cnt-1]*a[cnt-1]]=1;
c[a[cnt-1]]=1;
for(int j=0;a[j]<=n/i;j++)
{
st[a[j]*i]=true;
if(i%a[j]==0) break;
}
}
}
void solve(){
int n;
cin>>n;
vector<int> t(n+5,0);
int Max=-1;
for(int i=0;i<n;i++) cin>>t[i],Max=max(Max,t[i]);
f(Max);
// cout<<Max<<endl;
for(int i=1;i<=Max;i++) sum[i]=sum[i-1]+b[i];
for(int i=1;i<=Max;i++){
d[i]=d[i-1]+c[i];
// cout<<"d["<<i<<"]:"<<d[i]<<endl;
}
int ans=1;
for(int i=0;i<n;i++){
cout<<ans+d[t[i]]-sum[t[i]]<<endl;
}
}
signed main(){
// cin>>T;
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
while(T--){
solve();
}
return 0;
}