hdu1796 How many integers can you find(整除个数统计)
来源:互联网 发布:央视2012年网络春晚 编辑:程序博客网 时间:2024/05/21 10:52
关键词:整除个数统计
题意:统计[1,n)中能够被a[1…m]中至少一个整除的数的总数
m<=10
解法:
结论:[1,n)中能够被x整除的个数:(n-1)/x
因此
1.
2.
由容斥原理:ans=
复杂度:O(
拓展:
1.统计b[1,2…n]中所有数的因子在数组中出现的个数(每个数中仅记做一次)
解法:遍历b[i]的所有因子,遇到因子d时,beinum[d]++即可
复杂度:O(
2.统计b[1,2…n]中能够被a[1…m]中至少一个整除的数的总数
b数组的取值范围是1-N
解法:输入b[i]时,将b[i]的所有因子k的beinum[k]++,统计出1-max(b[i])中每个数的beinum:b数组中是其倍数的个数。在统计整除个数时,在代码标星的两行将(n-1)/tlcm改为beinum[tlcm]即可
复杂度:O(
#include<stdio.h>#include<iostream>#include<algorithm>#include<string.h>#include<vector>#define lowbit(x) x&(-x)#define mem(a,b) memset(a,b,sizeof(a))#define ll long longusing namespace std;#define rad 10000const int maxn =100;int gcd(int a,int b){ return (b==0)?a:gcd(b,a%b);}int lcm(int a,int b){ return a*b/gcd(a,b);}int n,m,ans;int a[maxn];bool cmp(int a,int b){ return a>b;}int main(){ //freopen("a.txt","r",stdin); while(scanf("%d%d",&n,&m)!=EOF){ for(int i=0;i<m;i++) scanf("%d",&a[i]); sort(a,a+m,cmp); if(!a[m-1]) m--; ans=0; for(int i=1;i<(1<<m);i++){ int tlcm=1,tmp=0;//最小公倍数 for(int j=0;(1<<j)<=i;j++){ if((1<<j)&i){ tlcm=lcm(tlcm,a[j]); if(tmp) tmp=0; else tmp=1; } } if(tmp) ans=ans+(n-1)/tlcm;//*** else ans=ans-(n-1)/tlcm;//*** } printf("%d\n",ans); }}
0 0
- hdu1796 How many integers can you find(整除个数统计)
- hdu1796 How many integers can you find
- HDU1796-How many integers can you find
- hdu1796 How many integers can you find
- Hdu1796 How many integers can you find
- hdu1796 How many integers can you find----容斥原理
- hdu1796--How many integers can you find--容斥原理
- hdu1796---How many integers can you find(容斥原理)
- HDU1796 How many integers can you find【容斥定理】
- 容斥定理 hdu1796 How many integers can you find
- hdu1796 How many integers can you find 容斥原理
- hdu1796 How many integers can you find 容斥原理
- 【容斥原理】HDU1796 How many integers can you find
- hdu1796 How many integers can you find(容斥)
- HDU1796 How many integers can you find[容斥定理]
- HDU1796 How many integers can you find (容斥原理)
- HDU1796 How many integers can you find(容斥)
- hdu1796 How many integers can you find (容斥原理)
- 安装Linux Mint 17后要做的20件事
- 2010年山东省第一届ACM大学生程序设计竞赛——Fairy tale
- PyQt5的菜单栏,工具栏,状态栏的合并
- UVA 12186 Another Crisis
- 数字集成电路设计-17-svunit环境构建
- hdu1796 How many integers can you find(整除个数统计)
- 包扩展
- linux应用开发-时间编程
- Android WebView使用【初级使用】
- STl中的排序算法详细解析
- Ubuntu14.04配置adb无法认到
- Android开发中Unable to instantiate activity ComponentInfo java.lang.NullPointerException解决方案
- 将“引用”作为函数返回值类型的格式、好处和需要遵守的规则?
- [LeetCode]*105.Construct Binary Tree from Preorder and Inorder Traversal