hdu4135(容斥入门题)
来源:互联网 发布:苹果手机官方软件 编辑:程序博客网 时间:2024/06/05 21:58
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4135
题目意思:给定两个数 a b和一个数 n 求[a,b]范围内与n互素的数的个数
思路:我们可以用容斥计算出[1,B]之间和[1,A-1]之间的与N互质的数,然后相减即可。然后我们用一种快速的方法求出[1,X]之间的与N互质的数,首先我们求出N的质因子,[1,X]之间与N的质因子成倍数关系的数肯定与N不成互质关系。
code:
#include<iostream>#include<algorithm>#include<cmath>using namespace std;typedef long long ll;#define maxn 100000ll p[maxn];int len;ll a,b,n;void init(ll n)//分解质因数{ len=0; for(int i=2;i*i<=n;i++) { if(n%i==0) { p[len++]=i; while(n%i==0) n/=i; } } if(n>1) p[len++]=n;}ll solve(ll n){ ll y=(ll)(1<<len);//情况的数量为y-1 ll res=0; for(int i=1;i<y;i++) { ll flag=0; ll temp=1; for(int j=0;j<len;j++) if(i&(ll)(1<<j)) { flag++; temp*=p[j]; } if(flag&1) res+=n/temp; else res-=n/temp; } return res;}int main(){ ios::sync_with_stdio(false); int T; cin>>T; int kcas=1; while(T--) { cin>>a>>b>>n; init(n); ll ans=(b-solve(b))-(a-1-solve(a-1)); cout<<"Case #"<<kcas++<<": "<<ans<<endl; } return 0;}
阅读全文
0 0
- hdu4135(容斥原理入门题)
- hdu4135(容斥入门题)
- hdu4135(容斥原理)
- HDU4135 Co-prime(容斥原理)
- hdu4135(容斥原理)Co-prime
- hdu4135 容斥原理
- hdu4135题解 容斥
- HDU4135容斥原理
- hdu4135 容斥原理
- hdu4135 容斥
- hdu4135欧拉,容斥
- hdu4135 Co-prime(容斥)
- hdu4135 互斥+容斥原理
- (hdu4135)Co-prime(容斥定理+队列)
- hdu4135--Co-prime(欧拉函数+容斥原理)
- hdu4135 Co-prime(容斥原理两种写法)
- hdu1695 && hdu4135 容斥原理两连发
- HDU4135 Co-prime【容斥原理】
- 【模板】线段树区间最值
- 29muduo_net库源码分析(五)
- mysql 下载 mysql jdbc jar 下载 ,mysql-5.7.17 解压版安装
- ReactNative源码篇:渲染原理
- cocos2d-js PageView用法
- hdu4135(容斥入门题)
- Git_基础
- [LeetCode] 637. Average of Levels in Binary Tree
- MySQL 加锁处理分析
- css em
- xml头报错
- VUE.JS
- Ubuntu16.04下Mongodb安装部署步骤(图文详解)
- Android样式的开发:drawable汇总篇