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<bits/stdc++.h>
 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<ji&&num[i]<=x;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 }
时间: 2024-10-05 19:27:21

xtu 1242 Yada Number 容斥原理的相关文章

xtu 1242 Yada Number 打表

Yada Number       Time Limit : 2000 MS   Memory Limit : 65536 KB 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,

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 no

XTU1242: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 no

ZOJ 3233 Lucky Number --容斥原理

这题被出题人给活活坑了,题目居然理解错了..哎,不想多说. 题意:给两组数,A组为幸运基数,B组为不幸运的基数,问在[low,high]区间内有多少个数:至少被A组中一个数整除,并且不被B中任意一个数整除.|A|<=15. 分析:看到A长度这么小,以及求区间内满足条件的个数问题,容易想到容斥原理,因为不被B中任意一个数整除,所以将B数组所有数取一个最小公倍数LCM,那么就变成了幸运数字都不会被这个LCM整除. 然后枚举子集,实现要将A中元素去除相互整除的情况,比如A = [2,4],这时因为被至

ZOJ 2836 Number Puzzle ( 容斥原理 )

ZOJ 2836 Number Puzzle( 容斥原理 ) #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long LL; #define CLR( a, b ) memset( a, b, sizeof(a) ) int m, n, A[11]; LL gcd( LL a, LL b ) { return b == 0 ? a :

xtu summer individual 6 B - Number Busters

Number Busters Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 382B64-bit integer IO format: %I64d      Java class name: (Any) Arthur and Alexander are number busters. Today they've got a competition.

hdu 4390 Number Sequence (容斥原理)

Number Sequence Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 790    Accepted Submission(s): 331 Problem Description Given a number sequence b1,b2-bn. Please count how many number sequences

[容斥原理] zoj 2836 Number Puzzle

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1836 Number Puzzle Time Limit: 2 Seconds      Memory Limit: 65536 KB Given a list of integers (A1, A2, ..., An), and a positive integer M, please find the number of positive integers th

HDU 4390 Number Sequence (容斥原理+组合计数)

HDU 4390 题意: 大概就是这样.不翻译了: Given a number sequence b1,b2-bn. Please count how many number sequences a1,a2,...,ansatisfy the condition thata1?a2?...?an=b1?b2?-?bn(ai,bi>1). 思路: 我们能够确定一件事:等号两边由同样数量的质因子组成. 假设ai能够等于1,答案就是把这些质因子分配进n个位置的方案数. 设左边的数字共由x个质因子组成