hdu How many integers can you find

题意:找出小于n是m个数每个数的倍数的数的个数。

思路:用二进制表示是那几个数的倍数。 二进制进行容斥,去掉小于0的数。

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5
 6 __int64 n,m,g;
 7 __int64 a[1000],b[1000];
 8
 9 __int64 gcd(__int64 a,__int64 b)
10 {
11     return b==0?a:gcd(b,a%b);
12 }
13
14 int main()
15 {
16     while(scanf("%I64d%I64d",&n,&m)!=EOF)
17     {
18         memset(a,0,sizeof(a));
19         __int64 cnt=0;
20         for(int i=0; i<m; i++)
21         {
22             __int64 x;
23             scanf("%I64d",&x);
24             if(x)
25             a[cnt++]=x;
26         }
27         __int64 ans=0;
28         for(int i=1; i<(1<<cnt); i++)
29         {
30             __int64 xx=0;
31             __int64 x=1;
32             memset(b,0,sizeof(b));
33             for(int j=0; j<cnt; j++)
34             {
35                 if(i&(1<<j))
36                 {
37                     b[xx]=a[j];
38                     xx++;
39                     x*=a[j];
40                 }
41             }
42             if(xx>1)
43             {
44                 g=(b[0]*b[1])/gcd(b[0],b[1]);
45                 for(int k=2; k<xx; k++)
46                 {
47                     g=(g*b[k])/gcd(g,b[k]);
48                 }
49                 x=g;
50             }
51             if(xx%2!=0)
52             {
53                 ans+=((n-1)/x);
54             }
55             else
56             {
57                 ans-=((n-1)/x);
58             }
59         }
60         printf("%I64d\n",ans);
61     }
62     return 0;
63 }

时间: 2024-10-12 08:34:32

hdu How many integers can you find的相关文章

HDU 1796How many integers can you find(容斥原理)

How many integers can you find Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 1796 Description Now you get a number N, and a M-integers set, you should find out how many integers which are smal

hdu 4267 A Simple Problem with Integers

题目链接:hdu 4267 A Simple Problem with Integers 类似于题目:hdu 1556 Color the ball 的技巧实现树状数组的段更新点查询. 由于该题对于段的更新并不是连续的,从而可以构造多个树状数组.因为$k \in [1,10] $,从而可以把更新划分为如下类型: 1,2,3,4,5... ------------- 1,3,5,7,9... 2,4,6,8,10... ------------- 1,4,7,10,13... 2,5,8,11,1

HDU 1796 How many integers can you find (状态压缩 + 容斥原理)

题目链接 题意 : 给你N,然后再给M个数,让你找小于N的并且能够整除M里的任意一个数的数有多少,0不算. 思路 :用了容斥原理 : ans = sum{ 整除一个的数 } - sum{ 整除两个的数 } + sum{ 整除三个的数 }………………所以是奇加偶减,而整除 k 个数的数可以表示成 lcm(A1,A2,…,Ak) 的倍数的形式.所以算出最小公倍数, //HDU 1796 #include <cstdio> #include <iostream> #include <

HDU 1796 How many integers can you find (lcm + 容斥)

How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5526    Accepted Submission(s): 1584 Problem Description Now you get a number N, and a M-integers set, you should

HDU 1796 How many integers can you find(组合数学-容斥原理)

How many integers can you find Problem Description Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N=12, and M-integer

hdu 4267/poj 3468 A Simple Problem with Integers (分状态的树状数组)

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4283    Accepted Submission(s): 1334 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with

HDU 1796 How many integers can you find 容斥入门

How many integers can you find Problem Description Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that they can divided exactly by any integers in the set. For example, N=12, and M-integer

A Simple Problem with Integers 多树状数组分割,区间修改,单点求职。 hdu 4267

A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4032    Accepted Submission(s): 1255 Problem Description Let A1, A2, ... , AN be N elements. You need to deal with

hdu 1796 How many integers can you find 容斥定理

How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem Description Now you get a number N, and a M-integers set, you should find out how many integers which are small than N, that t