XTU 1242 Yada Number 容斥

时间:2023-03-09 19:05:06
XTU 1242 Yada Number 容斥

Yada Number

Problem Description:

Every positive integer can be expressed by multiplication of prime integers. Duoxida says an integer is a yada number if the total amount of 2,3,5,7,11,13 in its prime factors is even.

For instance, 18=2 * 3 * 3 is not a yada number since the sum of amount of 2, 3 is 3, an odd number; while 170 = 2 * 5 * 17 is a yada number since the sum of amount of 2, 5 is 2, a even number that satifies the definition of yada number.

Now, Duoxida wonders how many yada number are among all integers in [1,n].

Input

The first line contains a integer T(no more than 50) which indicating the number of test cases. In the following T lines containing a integer n. ()

Output

For each case, output the answer in one single line.

Sample Input

2
18
21

Sample Output

9
11












题意:

  给你一个n,问你1到n里面有多少个数满足 因子中是2,3,5,7,11,13的个数为偶数个

题解:

   预处理出所有的x,满足x只含有2,3,5,7,11,3这几个质因子,且数目为偶数。x的数目略大于10000

  注意加入0个的情况,即1.

对于一个数n,枚举所有的x,对于一个x,f(n/x)即求出[1,n/x]中不含有2,3,5,7,11,13作为因子的数有多少个,这个是经典的容斥问题。

 最后对所有的f(n/x)求和即可

#include<bits/stdc++.h>
using namespace std;
const int N = 3e6+, M = 1e6+, mod = 1e9+,inf = 1e9; typedef long long ll;
const ll maxn = 1e9;
int cnt = , ans,n;
ll b[N];
int a[] = {,,,,,};
ll gcd(ll a,ll b) {return b==?a:gcd(b,a%b);}
void dfs(ll x,int f,int num) {
if(num==) {
if(!f) b[cnt++] = x;
return ;
}
while(x<=maxn) {
dfs(x,f,num+);
x*=a[num];
f^=;
}
}
void init() {
dfs(,,);
sort(b,b+cnt);
} void inclu(int i,int num,ll tmp) {
if(tmp>n) return ;
if(i>=) {
if(num==) ans = ;
else {
if(num&) ans = ans+n/tmp;
else ans = ans-n/tmp;
}
return ;
}
inclu(i+,num,tmp);
inclu(i+,num+,tmp*a[i]/gcd(tmp,a[i]));
} void solve() {
int Ans = ;
scanf("%d",&n);
int tm = n;
for(int i=;i<cnt&&b[i]<=tm;i++) {
n = tm/b[i];
ans = ;
inclu(,,);
Ans+=(n - ans);
}
printf("%d\n",Ans);
}
int main() {
int T;
cnt = ;
init();
scanf("%d",&T);
while(T--) {
solve();
}
return ;
}