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. C++新手之培养良好的编程风格

    内功深厚的武林高手出招往往平淡无奇.同理,编程高手也不会用奇门怪招写程序.良好的编程风格是产生高质量程序的前提. 下面以C++为例,来给大家介绍. 一. 命名约定 有不少人编程时用拼音给函数或变量命名 ...

  2. 从一个例子讲解拷贝构造函数与return

    #include "iostream" using namespace std; class Location { public: Location(, ) { X = xx; Y ...

  3. 侧菜单栏的实现SlidingPaneLayout

    SlidingPaneLayout分为两部分,上面的 左划出部分和没划出的时候 <?xml version="1.0" encoding="utf-8"? ...

  4. Socket get http request

    package wuyubao.firstsample; import java.io.BufferedReader; import java.io.IOException; import java. ...

  5. git批量删除分支

    要删除本地,首先要考虑以下三点 列出所有本地分支 搜索目标分支如:所有含有'dev'的分支 将搜索出的结果传给删除函数 所以我们可以得到: git br |grep 'dev' |xargs git ...

  6. php类与对象简单操作

    <?php /* * Created on 2015-8-25 * * To change the template for this generated file go to * Window ...

  7. jquery遍历对象,数组,集合

    1.jquery 遍历对象 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTM ...

  8. 夺命雷公狗---DEDECMS----8dedecms干掉首页和-文档页-栏目页的页面的广告

    我们首先来将首页生成静态页面,如下图所示: 成功后,如下显示: 如果成功后则在文件夹下多了一个index.html的文件.. 我们的首页静态页面是通过模版文件生成,所以我们只需要把模版文件的广告标签删 ...

  9. Spring+SpringMVC+MyBatis)

    用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合的 ...

  10. Android横竖屏切换继续播放视频

    只需要重新onSaveInstanceState方法,在其里面记住我们要记录的参数 package com.bawei.day07_videoview; import android.os.Bundl ...