HDU 1796 Howmany integers can you find (容斥原理)
How many integers can you find
Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5664 Accepted Submission(s): 1630
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.
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.
2 3
- #include <cstdio>
- #include <iostream>
- #include <cstdlib>
- #include <algorithm>
- #include <ctime>
- #include <cmath>
- #include <string>
- #include <cstring>
- #include <stack>
- #include <queue>
- #include <list>
- #include <vector>
- #include <map>
- #include <set>
- using namespace std;
- const int INF=0x3f3f3f3f;
- const double eps=1e-;
- const double PI=acos(-1.0);
- #define maxn 500
- __int64 k[maxn];
- __int64 gcd(__int64 b,__int64 a)
- {
- return a==?b:gcd(a,b%a);
- }
- int main()
- {
- int n, m;
- while(~scanf("%d%d", &n, &m))
- {
- int t;
- n--;
- int cnt = ;
- for(int i = ; i < m; i++)
- {
- scanf("%d", &t);
- if(t> && t < n)
- k[cnt++] = t;
- }
- __int64 ans = ;
- for(int i = ; i < <<cnt; i++)
- {
- int num = ;
- __int64 lcm = ;
- for(int j = ; j < cnt; j++)
- {
- if(i & ( << j))
- {
- num++;
- lcm = k[j]/gcd(k[j], lcm) * lcm;
- }
- }
- if(num & )
- ans += n/lcm;
- else
- ans -= n/lcm;
- }
- printf("%I64d\n", ans);
- }
- return ;
- }
2. 递归
- #include<iostream>
- #include<cstdio>
- #include<algorithm>
- using namespace std;
- __int64 a[];
- int n,m;
- //cur表示
- __int64 sum=;
- __int64 gcd(__int64 b,__int64 a)
- {
- return a==?b:gcd(a,b%a);
- //while()
- }//最小公倍数
- void dfs(int cur,__int64 lcm,int id)//容斥原理公式
- {
- //lcm=lcm/gcd(lcm,a[cur])*a[cur];
- lcm=a[cur]/gcd(a[cur],lcm)*lcm;
- if(id&)//运用了快速幂的方法判断奇偶
- sum+=(n-)/lcm;
- else
- sum-=(n-)/lcm;
- // cout<<"id = "<<id<<" : "<<sum<<endl;
- for(int i=cur+;i<=m;i++)
- dfs(i,lcm,id+);
- }
- int main()
- {
- int t;
- while(~scanf("%d%d",&n,&t))
- {
- int i,x;
- m=;
- for(i=;i<=t;i++)
- {
- scanf("%d",&x);
- if(x)
- {
- a[++m]=x;
- }
- }
- sum=;
- for(i=;i<=m;i++)
- dfs(i,a[i],);
- printf("%I64d\n",sum);//容斥原理公式
- // cout<<sum<<endl;
- }
- return ;
- }
HDU 1796 Howmany integers can you find (容斥原理)的更多相关文章
- HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举)
HDU.1796 How many integers can you find ( 组合数学 容斥原理 二进制枚举) 题意分析 求在[1,n-1]中,m个整数的倍数共有多少个 与 UVA.10325 ...
- HDU 1796 How many integers can you find (状态压缩 + 容斥原理)
题目链接 题意 : 给你N,然后再给M个数,让你找小于N的并且能够整除M里的任意一个数的数有多少,0不算. 思路 :用了容斥原理 : ans = sum{ 整除一个的数 } - sum{ 整除两个的数 ...
- HDU 1796How many integers can you find(容斥原理)
How many integers can you find Time Limit:5000MS Memory Limit:32768KB 64bit IO Format:%I64d ...
- HDU 1796 容斥原理 How many integers can you find
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1796 处男容斥原理 纪念一下 TMD看了好久才明白DFS... 先贴代码后解释 #includ ...
- HDU 1796 How many integers can you find(容斥原理)
题目传送:http://acm.hdu.edu.cn/diy/contest_showproblem.php?cid=20918&pid=1002 Problem Description ...
- 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 ...
- HDU 1796 How many integers can you find(容斥原理+二进制/DFS)
How many integers can you find Time Limit: 12000/5000 MS (Java/Others) Memory Limit: 65536/32768 ...
- [容斥原理] hdu 1796 How many integers can you find
题意: 给一个N.然后给M个数,问1~N-1里面有多少个数能被这M个数中一个或多个数整除. 思路: 首先要N-- 然后对于每一个数M 事实上1~N-1内能被其整除的 就是有(N-1)/M[i]个 可是 ...
- HDU 1796 How many integers can you find(容斥原理)
题意 就是给出一个整数n,一个具有m个元素的数组,求出1-n中有多少个数至少能整除m数组中的一个数 (1<=n<=10^18.m<=20) 题解 这题是容斥原理基本模型. 枚举n中有 ...
随机推荐
- IIS缺少文件的解决方法
原文 http://cqyccmh.blog.163.com/blog/static/6068134720102211543944/ 今天解决了一个郁闷了很久的问题,之前实在没辙就只能重装系统,因为装 ...
- 电机转矩T=9550*P/N推导。
很奇怪,这个公式怎么来的,原来好多是基础物理的,也许我们初中高中物理书上多有,基础真的是很基础的基础. P=F*V (1) ,即功率=力*速度 T=F*R (2) ,即力矩=力*作用长度 ,在电机里 ...
- Java开发者工具
From:http://www.csdn.net/article/2015-03-26/2824317 1. Notepad++ Notepad++是用于编辑xml.脚本以及记笔记的最佳工具.这个工具 ...
- C++ 线程的创建,挂起,唤醒,终止
例子: 线程代码: DWORD __stdcall ThreadProc(LPVOID lpParameter) { CMultiThreadDlg * pdlg = (CMultiThreadDlg ...
- 【Android】使用FrameLayout布局实现霓虹灯效果
FrameLayout是五大布局中最简单的一个布局. 在这个布局中,整个界面被当成一块空白备用区域,所有的子元素都不能被指定放置的位置. 它们统统放于这块区域的左上角,并且后面的子元素直接覆盖在前面的 ...
- JAXB
注解
JAXB(Java API for XML Binding),它提供了一个便捷的方式高速Java对象XML转变.于JAX-WS(Java的WebService规范之中的一个)中,JDK1.6 自带的版 ...
- C++实现20个设计模式
http://c.chinaitlab.com/special/sjms/Index.html 一个月下来,把常见的20个设计模式好好复习并且逐个用C++实现了一遍,收获还是很大的,很多东西看上去明白 ...
- MySql5.1在Win7下的安装与重装问题的解决
痛苦啊痛苦,我也不知道这两天怎么了.上班没有精神,还打瞌睡,下班后又感觉很累.精力集中不起来. 这篇花了我好久的时间,我效率这么差,~\(≧▽≦)/~. 软件包下载 首先单击mysql-5.1.53- ...
- C# 调用存储过程传入表变量作为参数
首先在SQLServer定义一个自定义表类型: USE [ABC] GO CREATE TYPE [ABC].[MyCustomType] AS TABLE( ) NOT NULL, ) NULL, ...
- RabbitMQ消息队列安装和配置以及推送消息
好久没有写了,最近项目用到RabbitMQ,找了一些资料试验,最后终于成功了,把安装配置的步骤分享给大家. 一.Erlang安装具体过程: 1.双击otp_win32_R16801.exe(不同版本可 ...