POJ 2773 happy2006 (素因子分解+容斥原理+二分)
来源:互联网 发布:什么是软件外包公司 编辑:程序博客网 时间:2024/04/30 10:43
各种拙计,比赛时连暴力版本都没写出来,too weak :-(
参考代码:http://blog.csdn.net/kg_second/article/details/8034188
#include <cstdio>#include <cstring>using namespace std;const int maxn =1000+5;#define MAX 1000000000bool hash[maxn];int prime[maxn];int a[11];int n,k,cnt,K;void init(){ cnt=0; memset(hash,0,sizeof(hash)); for(int i = 2 ;i < maxn;i ++) if(!hash[i]){ prime[cnt++] = i; for(int j= i+i ;j < maxn; j +=i) hash[j] = 1; }}void getprime()///素因子分解{ int tmp = n; k = 0; for(int i = 0;i < cnt&& prime[i] * prime[i] <= n; i++) if(tmp%prime[i]==0){ a[k++] = prime[i]; while(tmp % prime[i] == 0) tmp /= prime[i]; } if(tmp != 1) a[k++] = tmp;}int judge(int num)///求出num前与num互质的元素个数{ int sum=0; for(int i = 1; i < (1<<k); i++)///利用容斥原理 { int tmp = 1,t = 0; for(int j = 0;j < k; j++) if((1 << j) & i){ t++; tmp *= a[j]; } if(t%2) sum += num/tmp; else sum -= num/tmp; } return num-sum;}int main(){ init(); while(~scanf("%d%d",&n,&K)) { getprime(); int l=1,r=MAX,mid,ans; while(l<=r) { mid=(l+r)>>1; int pt=judge(mid); if(pt>=K){ if(pt==K) ans=mid; r=mid-1; } else l=mid+1; } printf("%d\n",ans); } return 0;}
- POJ 2773 happy2006 (素因子分解+容斥原理+二分)
- POJ 2773 Happy2006 (容斥原理+二分/欧拉)
- poj 2773 Happy2006【容斥原理】
- poj 2773 (容斥原理 + 素数分解 + 二分)
- poj2773-容斥原理(dfs+二分+素数表+素因子分解)
- POJ 3904 Sky Code(素因子分解+容斥)
- POJ1091 跳蚤 素因子分解+容斥原理
- HDU 2841 Visible Trees (素因子分解,容斥原理)
- POJ 1845 二分+素因子分解
- POJ 2773 二分 +容斥原理
- poj 2773 容斥原理+二分
- poj-2773(数论+容斥原理+二分)
- HDU 4135&HDU 4407--容斥原理--质因子分解
- poj1091(容斥原理+质因子分解)跳蚤
- poj 3904 容斥原理+质因数分解
- hdu GCD 【欧拉函数,素因子分解,筛选法,容斥原理】
- HDU 1695 GCD(素因子分解+容斥原理+欧拉函数)
- hdu 1695 GCD (欧拉函数+容斥原理+素因子分解)
- iOS: UIScrollView的属性canCancelContentTouches
- HDU 1711 Number Sequence(简单KMP)
- openstack 工作原理分析
- 将某字段的值显示为数字
- iOS: UIScrollView的属性contentInset
- POJ 2773 happy2006 (素因子分解+容斥原理+二分)
- 常用的正则表达式应用
- UVA 12535 Probability Through Experiments 解题报告
- iOS: UIScrollView的属性contentOffset
- Java---多线程断点下载
- uImage和zImage
- hdu 3516 四边形优化dp
- iOS: UIScrollView的属性decelerating
- HHVM(HipHop PHP)优化加速PHP代码:搭建提速五六倍的PHP服务器