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中有 ...
随机推荐
- 超大批量删除redis中无用key+配置
目前线上一个单实例redis中无用的key太多,决定删除一部分. 1.删除指定用户的key,使用redis的pipeline 根据一定条件把需要删除的用户统计出来,放到一个表里面,表为 del_use ...
- MFC DestroyWindow窗口对象和窗口句柄的销毁
考虑单窗口情况: 假设自己通过new创建了一个窗口对象pWnd,然后pWnd->Create.则销毁窗口的调用次序: 1. 手工调用pWnd->DestroyWindow(): 2. De ...
- 【HDU 5510 Bazinga】字符串
2015沈阳区域赛现场赛第2题 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5510 题意:给定一个由字符串组成的序列,一共n个元素,每个元素是一个不 ...
- 在Eclipse中用SWT设计界面
摘自http://www.tulaoshi.com/n/20160129/1488574.html 在Eclipse中用SWT设计界面 1. 为什么要使用SWT? SWT是一个IBM开发的跨平台GU ...
- 【转】android电池(五):电池 充电IC(PM2301)驱动分析篇
关键词:android 电池 电量计 PL2301任务初始化宏 power_supply 中断线程化 平台信息:内核:linux2.6/linux3.0系统:android/android4.0 ...
- Objective C - UIColor
UIColor+Hex.h #import <UIKit/UIKit.h> @interface UIColor (Hex) + (UIColor *) colorWithHexStrin ...
- Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作
详细的Spring MVC框架搭配在这个连接中: Maven 工程下 Spring MVC 站点配置 (一) Maven 工程下 Spring MVC 站点配置 (二) Mybatis数据操作 这篇主 ...
- [Uva247][Tarjan求强连通分量][Calling Circles]
题目大意: 例如:A跟B打电话,B跟C打电话,C跟A打电话..D跟E打电话,E跟D不打电话.则A,B,C属于同一个电话圈,D,E分别属于一个电话圈,问有多少个电话圈. 分析 就是裸的求强连通分量,直接 ...
- css设置
box-size允许您以特定的方式定义匹配某个区域的特定元素. content-box(默认):宽度和高度分别应用到元素的内容框.在宽度和高度之外绘制元素的内边距和边框.border-box:为元素设 ...
- C# 单例模式(转)
C#设计模式学习笔记-单例模式 最近在学设计模式,学到创建型模式的时候,碰到单例模式(或叫单件模式),现在整理一下笔记. 在<Design Patterns:Elements of Resuab ...