HDU 1796 How many integers can you find 容斥原理
来源:互联网 发布:mac os sierra gm 编辑:程序博客网 时间:2024/05/16 14:31
WA了N久N久... 最后发现原来是自己有些特殊情况没有存好。
一个条件读入的数据要符合(1,N)这个区间范围内....
在处理的时候没有处理好,天真的没有排除,只是当有这样的数据的时候跳过.... 当然... 悲剧发生了= =
无限WA.....
使用容斥原理...
容斥原理就不讲了..
我用的位运算做的...等会儿再用dfs做一下....
一个条件读入的数据要符合(1,N)这个区间范围内....
在处理的时候没有处理好,天真的没有排除,只是当有这样的数据的时候跳过.... 当然... 悲剧发生了= =
无限WA.....
使用容斥原理...
容斥原理就不讲了..
我用的位运算做的...等会儿再用dfs做一下....
#include<iostream>#include<string.h>#include<cstdio>#include<algorithm>using namespace std;__int64 N,M;__int64 date[20];__int64 gcd( __int64 a,__int64 b ){ if( b==0 ) return a; else return gcd( b,a%b );}int getnum( int n ){ int sum=0; __int64 lcm=1; for( int i=0;i<M;i++ ) if( n&(1<<i) ) sum++,lcm=lcm*date[i]/gcd(lcm,date[i]); if( sum&1 ) return N/lcm; else return -(N/lcm);}int main(){ while( scanf("%I64d%I64d",&N,&M)!=EOF ) { __int64 ans=0,x; int d=0; for( int i=0;i<M;i++ ) { scanf( "%I64d",&x ); if( x>0&&x<N ) date[d++]=x;}N--;M=d; for( int i=1;i<(1<<M);i++ ) ans+=getnum(i); printf( "%I64d\n",ans ); } return 0;}
下面是dfs的版本,果然还是dfs快啊!!!
#include<iostream>using namespace std;__int64 N,M;__int64 d[22];__int64 gcd( __int64 a,__int64 b ){ if( b==0 ) return a; else return gcd( b,a%b );} void dfs( int cur,int count,__int64 lcm,__int64 &sum ){ lcm=lcm*d[cur]/gcd(d[cur],lcm); if( count&1 ) sum+=N/lcm; else sum-=N/lcm; for( int i=cur+1;i<M;i++ ) dfs( i,count+1,lcm,sum );}int main(){ while( scanf("%I64d%I64d",&N,&M)!=EOF ) { __int64 x,cnt=0; for( int i=0;i<M;i++ ) { scanf( "%I64d",&x ); if( x>0&&x<N ) d[cnt++]=x; } M=cnt;N--; __int64 ans=0; for( int i=0;i<M;i++ ) dfs( i,1,d[i],ans ); printf( "%I64d\n",ans ); } return 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(容斥原理)
- Javascript 设计模式学习笔记(2) - 继承(Inheritance) (下)
- jquery学习(三)--下拉左右选择菜单
- 项目打包前清除 SVN 标记
- 数据驱动设计
- 抽象类,接口简介
- HDU 1796 How many integers can you find 容斥原理
- 将左右下拉框中的内容左右移动
- struts2配置
- 软件开发(一)概述
- 第五周实验报告任务(1-1)
- win7 计算机右击管理打不开解决方案
- NYOJ 488 dfs
- linux/Windows中JAVA获取IP地址
- OOD沉思录 之 类和对象的关系--包含关系3