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中有 ...
随机推荐
- linux系统下,11款常见远程桌面控制软件
linux系统下,11款常见远程桌面控制软件 一. Grdc 它是一个用GTK+编写的,适用于gnome桌面环境的远程桌面访问软件.看图: 常见功能: 1.提供全屏,窗口化的远程控制.支持高分辨率下的 ...
- HDU 5775 Bubble Sort(线段树)(2016 Multi-University Training Contest 4 1012)
原址地址:http://ibupu.link/?id=31 Problem Description P is a permutation of the integers from 1 to N(ind ...
- Python 字符串相关操作
# 1 * 重复输出字符串 print('hello'*2) # 2 [] ,[:] 通过索引获取字符串中字符,这里和列表的切片操作是相同的,具体内容见列表 print('helloworld'[2: ...
- C# 封装-属性
属性使封装更容易 可以使用属性(properties),这些方法对其他对象来说就像是字段,可以用属性来获取或设置一个后备字段,后备字段就是由属性所设置的一个字段名 private int number ...
- php各种编译错误汇总
PHP编译安装时常见错误解决办法,php编译常见错误 This article is post on https://coderwall.com/p/ggmpfa configure: error: ...
- 实现接口IDisposable的示例
想使用using(...), 如: using (Getter process = new Getter()) { //... } 就必须给类实现接口IDisposable public sealed ...
- Asp.net MVC razor语法参考
Razor语法的快捷参考http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx/ 只是copy下来便于查阅! I ...
- Fragment 之 PagerAdapter
package com.edaixi.main.adapter; import android.content.Context; import android.support.v4.view.Page ...
- python-整理-面向对象
python的类和perl的类有相似之处,类的方法的第一个参数是表示类的对象自己,相当于c#的this python中定义类 class person: ''示例类,人'' count=0; def ...
- Mobile Matrices
This is an attempt to compile a list of relevant specifications for all modern smart phones and mobi ...