How many integers can you find

Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4249    Accepted Submission(s):
1211

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 set is
{2,3}, so there is another set {2,3,4,6,8,9,10}, all the integers of the set can
be divided exactly by 2 or 3. As a result, you just output the number 7.
 
Input
  There are a lot of cases. For each case, the first
line contains two integers N and M. The follow line contains the M integers, and
all of them are different from each other. 0<N<2^31,0<M<=10, and the
M integer are non-negative and won’t exceed 20.
 
Output
  For each case, output the number.
 
Sample Input
12 2
2 3
 
Sample Output
7
题意:给n个数字,最大不会超过20的非负数,0忽略它可以。给你一个数字M,
问1-M-1中,有多少个数字能被这n数字中任何一个整除(只要满足其中一个能整除就行)。统计个数输出。
 
思路:容斥,简单容斥。一开始做zoj的一道题,果断数据太水,方法是不对的也能ac。
原来的思路是这样的,对n个数字,筛选掉ai倍数的数字,然后就容斥,但是明显这样的数据有问题
4 6,  这样容斥后得到的结果是4 6 -24,不对的,应该是4 6 -12,所以应该是 4 6    -(4*6)/gcd(4,6)

略坑略坑。

 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std; bool Hash[];
int f[],len,qlen;
__int64 Q[]; int gcd(int a,int b)
{
if(a<)a=-a;
if(b<)b=-b;
if(b==)return a;
int r;
while(b)
{
r=a%b;
a=b;
b=r;
}
return a;
}
void solve(__int64 m)
{
qlen = ;
Q[]=-;
for(int i=;i<=len;i++)
{
int k=qlen;
for(int j=;j<=k;j++)
Q[++qlen]=-*(Q[j]*f[i]/gcd(Q[j],f[i]));
}
__int64 sum = ;
for(int i=;i<=qlen;i++)
sum = sum+m/Q[i];
printf("%I64d\n",sum);
}
int main()
{
int m,x;
__int64 n;
while(scanf("%I64d%d",&n,&m)>)
{
n=n-;
memset(Hash,false,sizeof(Hash));
for(int i=;i<=m;i++)
{
scanf("%d",&x);
Hash[x]=true;
}
for(int i=;i<=;i++)
{
if(Hash[i]==true)
for(int j=i+i;j<=;j=j+i)
if(Hash[j]==true) Hash[j]=false;
}
len = ;
for(int i=;i<=;i++)if(Hash[i]==true) f[++len]=i;
solve(n);
}
return ;
}

HDU How many integers can you find 容斥的更多相关文章

  1. 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 ...

  2. 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 ...

  3. HDU 1796 How many integers can you find (容斥)

    题意:给定一个数 n,和一个集合 m,问你小于的 n的所有正数能整除 m的任意一个的数目. 析:简单容斥,就是 1 个数的倍数 - 2个数的最小公倍数 + 3个数的最小公倍数 + ...(-1)^(n ...

  4. How many integers can you find(容斥+dfs容斥)

    How many integers can you find Time Limit: 12000/5000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  5. hdu 6169 Senior PanⅡ Miller_Rabin素数测试+容斥

    Senior PanⅡ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Pr ...

  6. HDU - 5297:Y sequence (迭代&容斥)

    Yellowstar likes integers so much that he listed all positive integers in ascending order,but he hat ...

  7. Educational Codeforces Round 37 G. List Of Integers (二分,容斥定律,数论)

    G. List Of Integers time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  8. HDU - 4336:Card Collector(min-max容斥求期望)

    In your childhood, do you crazy for collecting the beautiful cards in the snacks? They said that, fo ...

  9. HDU - 5977 Garden of Eden (树形dp+容斥)

    题意:一棵树上有n(n<=50000)个结点,结点有k(k<=10)种颜色,问树上总共有多少条包含所有颜色的路径. 我最初的想法是树形状压dp,设dp[u][S]为以结点u为根的包含颜色集 ...

随机推荐

  1. 最大权闭合图最大获益(把边抽象为点)HDU3879

    题意:给出一个无向图,每个点都有点权值代表花费,每条边都有利益值,代表形成这条边就可以获得e[i]的利益,问选择那些点可以获得最大利益是多少? 分析:把边抽象成点,s与该点建边,容量是利益值,每个点与 ...

  2. sdutoj 2607 Mountain Subsequences

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2607 Mountain Subsequence ...

  3. android xutils

    http://blog.csdn.net/rishengcsdn/article/details/47279851/

  4. 移动端下拉刷新,iScroll.js用法(转载)

    本文转载自: iScroll.js 用法参考 (share)

  5. java 中 equals和==的区别

    public static void main(String[] args) { int n=0; int m=0; System.out.println(n==m); String str = ne ...

  6. 夺命雷公狗ThinkPHP项目之----企业网站27之网站前台单页的完成(从百度编辑器里面取出文章数据)

    我们的单页面里主要是为了可以取出文章分类表的栏目内容,废话先不说, 我们的实现要点: 1...获取get过来的栏目cate_id 2...然后用条件查询栏目表 <?php namespace H ...

  7. Perl中的替换(七)

    在Perl中使用s///进行替换操作,与m//进行查找操作类似. s/with (\w+)/against $1's team/;      ##第一个双斜线,表示被替代的文本.第二个双斜线,表示将替 ...

  8. C++笔试题(部分)

    1.简述C++11和Boost 2.struct和union与class的区别 3.为什么C++中调用被C编译器编译后的函数要加extern C声明? 4.以下代码哪里不对? #pragma regi ...

  9. 《高质量C++/C编程指南》陷阱 【转】

    作者:幻の上帝 出处:http://hi.baidu.com/frankhb1989/item/185f0a14823dd1f8dceeca2c 此文硬伤不少,且相对谭XX的书而言隐晦许多,不建议新手 ...

  10. SQL SERVER2000中订阅与发布的具体操作

    同步过程 一.准备工作,如果完成则可跳过. 1.内网DB服务器作为发布服务器,外网DB服务器作为订阅服务器. 发布服务器和订阅服务器上分别创建Windows用户jl,密码jl,隶属于administr ...