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中有 ...
随机推荐
- 2014.8.4我出的模拟赛【NTR酋长】
NTR酋长 (ntr.pas/.c/.cpp) 黄巨大终于如愿以偿的进入了czy的后宫中……但是czy很生气……他要在黄巨大走到他面前的必经之路上放上几个NTR酋长来阻挡黄巨大. 众所周知,NTR酋长 ...
- python局域网alive ip侦听
python局域网alive ip侦听 作者:vpoet mails:vpoet_sir@163.com 注:写着玩,欢迎copy # -*- coding: cp936 -*- # coding = ...
- Unity 移动MM自签名方式
在使用Unity接移动MM SDK的时候,最后有一个签名. 主要是把计费文件和版权文件放入APK的根目录. 搞了半天才知道前来这么简单..... 软件使用: apk签名工具apktool
- 不定义JQuery插件,不要说会JQuery[转载]
http://www.cnblogs.com/xcj26/p/3345556.html 不定义JQuery插件,不要说会JQuery 一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页 ...
- Android设备定制为永不锁屏
Android系统的锁屏时间存放在Setting数据库中,字段为Settings.System.SCREEN_OFF_TIMEOUT.查看 DatabaseHelper.java文件可以找到: fra ...
- Javascript中正则表达式的全局匹配模式
先看一道JavaScript题目,据说是国内某知名互联网企业的JavaScript笔试题,如果对正则的全局匹配模式不了解的话可能会对下面的输出结果感到疑惑. var str = "123#a ...
- php各种编译错误汇总
PHP编译安装时常见错误解决办法,php编译常见错误 This article is post on https://coderwall.com/p/ggmpfa configure: error: ...
- Splash界面布局与代码实现(一)
xml界面布局代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns ...
- jfreechart中文乱码问题解决方案(转)
参考网址:http://zhidao.baidu.com/link?url=y88rR1_aAHaFofonx9o_IaEu87MpkTQImsqDcy587eG55JkfQV6EzzzloIgXuQ ...
- Win7窗口操作
1.使用 Aero Shake 晃动最小化打开的窗口您可以使用 Aero Shake 晃动将所有打开的窗口快速最小化,只剩下您需要使用的窗口.然后,还可以轻松还原所有窗口.操作方法如下:1. 在需要保 ...