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

题意:问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,处理了下

 1 #include
 2 using namespace std;
 3 #define ll long long
 4 #define mod 1000000007
 5 #define inf 999999999
 6 #define pi 4*atan(1)
 7 //#pragma comment(linker, "/STACK:102400000,102400000")
 8 int p[10]={
     2,3,5,7,11,13};
 9 int num[20010],ji,ans;
10 struct is
11 {
12     int x;
13     int step;
14     bool operator <(const is a)const
15     {
16         return x>a.x;
17     }
18 };
19 priority_queue<is>q;
20 map<int,int>m;
21 int gcd(int x,int y)
22 {
23     return y==0?x:gcd(y,x%y);
24 }
25 void init()
26 {
27     ji=0;
28     is a;
29     a.x=1;
30     m[1]=1;
31     a.step=0;
32     q.push(a);
33     while(!q.empty())
34     {
35         is b=q.top();
36         if(b.x>1e9)
37         break;
38         q.pop();
39         if(b.step%2==0)
40         num[ji++]=b.x;
41         for(int i=0;i<6;i++)
42         {
43             is c;
44             ll gg=(ll)b.x*p[i];
45             if(gg>1e9)break;
46             c.step=b.step+1;
47             c.x=(int)gg;
48             if(c.x<=1e9&&m[c.x]==0)
49             q.push(c),m[c.x]=1;
50         }
51     }
52 }
53 void dfs(int lcm,int pos,int step,int x)
54 {
55     if(lcm>x)
56     return;
57     if(pos==6)
58     {
59         if(step==0)
60         return;
61         if(step&1)
62         ans+=x/lcm;
63         else
64         ans-=x/lcm;
65         return;
66     }
67     dfs(lcm,pos+1,step,x);
68     dfs(lcm/gcd(p[pos],lcm)*p[pos],pos+1,step+1,x);
69 }
70 int main()
71 {
72     int x,y,z,i,t;
73     init();
74     int T;
75     scanf("%d",&T);
76     while(T--)
77     {
78         scanf("%d",&x);
79         int Ans=0;
80         for(i=0;i)
81         {
82             ans=0;
83             dfs(1,0,0,x/num[i]);
84             Ans+=(x/num[i]-ans);
85         }
86         printf("%d\n",Ans);
87     }
88     return 0;
89 }

 

转载于:https://www.cnblogs.com/jhz033/p/5523743.html

你可能感兴趣的:(xtu 1242 Yada Number 容斥原理)