hdu 1796 How many integers can you find (容斥原理)
来源:互联网 发布:七天网络怎么绑定 编辑:程序博客网 时间:2024/05/02 00:18
题意:
题意就是求,有多少个数小于n并且是所给m个数中任意一个的倍数。
题解:
容斥原理,我们假设Ai是某m中第i个数的倍数个数,但让这个倍数是小于你的。接着就是暴力位枚举,有点慢,dfs更快。
#include<iostream>#include<math.h>#include<stdio.h>#include<algorithm>#include<string.h>#include<vector>#include<queue>#include<map>#include<set>using namespace std;#define B(x) (1<<(x))void cmax(int& a,int b){ if(b>a)a=b; }void cmin(int& a,int b){ if(b<a)a=b; }typedef long long ll;const int oo=0x3f3f3f3f;const ll OO=1LL<<61;const int MOD=1000007;const int maxn=550;int a[12];ll gcd(ll a,ll b){ return b==0 ? a : gcd(b,a%b);}ll lmc(ll a,ll b){ if(a<b)swap(a,b); return a/gcd(a,b)*b;}int main(){ //freopen("E:\\read.txt","r",stdin); int n,mm,m,val; while(scanf("%d %d",&n,&mm)!=EOF){ m=0; for(int i=0;i<mm;i++){ scanf("%d",&val); if(val>0&&val<n)a[m++]=val; } n--; int msk=(1<<m)-1; ll cnt,bit,t,ans=0; for(int s=1;s<=msk;s++){ bit=0; t=1; for(int i=0;i<m;i++){ if(s&B(i)){ t=lmc(t,a[i]); bit++; } } cnt=n/t; if(bit&1) ans+=cnt; else ans-=cnt; } printf("%I64d\n",ans); } return 0;}
0 0
- hdu-1796-How many integers can you find(容斥)
- HDU 1796 How many integers can you find 【容斥】
- HDU 1796 How many integers can you find 容斥原理
- 【容斥原理】HDU-1796-How many integers can you find
- HDU 1796 How many integers can you find(容斥原理)
- hdu 1796 How many integers can you find(容斥原理)
- HDU 1796 容斥原理 How many integers can you find
- hdu 1796 How many integers can you find(容斥原理)
- hdu 1796 How many integers can you find (容斥原理)
- hdu 1796 How many integers can you find 容斥原理
- HDU 1796 How many integers can you find(组合数学-容斥原理)
- HDU 1796 How many integers can you find(组合数学-容斥原理)
- hdu 1796 How many integers can you find(容斥原理)
- hdu 1796 How many integers can you find[经典容斥原理]
- [容斥原理] hdu 1796 How many integers can you find
- hdu 1796 How many integers can you find【容斥原理】
- HDU 1796 How many integers can you find(容斥原理)
- HDU 1796 - How many integers can you find(容斥原理)
- JAVA获取控制台的输入的两个方法
- 在自己的main函数中执行JUnit4的测试程序
- drawing中画不固定圆
- dos命令特殊符号
- php中的绘图技术要点及应用总结
- hdu 1796 How many integers can you find (容斥原理)
- JSLink 套用版
- leetcode--Single Number
- Oracle中的伪列ROWNUM
- JAVA工具-实现java的String类转换为C中的char*数组
- drawing中画不固定直线
- 简单的窗体颜色改变
- 【黑马程序员】--c语言中的指针总结
- nginx之反向代理和负载均衡