xtu 1242 Yada Number 容斥原理

时间:2023-02-12 22:39:51

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

题意:问1[,n]区间中,有多少个数,它的2,3,5,7,11,13的这几个因子数目之和为偶数

思路:预处理出所有的x,满足x只含有2,3,5,7,11,3这几个质因子,且数目为偶数。x的数目13000+;

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

    我用优先队列和map处理x;全用ll超时;有个地方会爆int,处理了下

 #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mod 1000000007
#define inf 999999999
#define pi 4*atan(1)
//#pragma comment(linker, "/STACK:102400000,102400000")
int p[]={,,,,,};
int num[],ji,ans;
struct is
{
int x;
int step;
bool operator <(const is a)const
{
return x>a.x;
}
};
priority_queue<is>q;
map<int,int>m;
int gcd(int x,int y)
{
return y==?x:gcd(y,x%y);
}
void init()
{
ji=;
is a;
a.x=;
m[]=;
a.step=;
q.push(a);
while(!q.empty())
{
is b=q.top();
if(b.x>1e9)
break;
q.pop();
if(b.step%==)
num[ji++]=b.x;
for(int i=;i<;i++)
{
is c;
ll gg=(ll)b.x*p[i];
if(gg>1e9)break;
c.step=b.step+;
c.x=(int)gg;
if(c.x<=1e9&&m[c.x]==)
q.push(c),m[c.x]=;
}
}
}
void dfs(int lcm,int pos,int step,int x)
{
if(lcm>x)
return;
if(pos==)
{
if(step==)
return;
if(step&)
ans+=x/lcm;
else
ans-=x/lcm;
return;
}
dfs(lcm,pos+,step,x);
dfs(lcm/gcd(p[pos],lcm)*p[pos],pos+,step+,x);
}
int main()
{
int x,y,z,i,t;
init();
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&x);
int Ans=;
for(i=;i<ji&&num[i]<=x;i++)
{
ans=;
dfs(,,,x/num[i]);
Ans+=(x/num[i]-ans);
}
printf("%d\n",Ans);
}
return ;
}