您的位置:首页 > 其它

hdu 4135 Co-prime 容斥原理

2015-08-27 23:08 489 查看

Co-prime

具有教科书性质的容斥原理应用实例。

能不重复、不遗漏地选出所有合数,也就能得到质数。

/** Aug 26, 2015 9:40:09 PM
* PrjName:hdu4135
* @author Semprathlon
*/
import java.io.*;
import java.util.*;
public class Main {
static int maxn=1001;
static int[] pri,fstp;
static Vector<Integer> vec=new Vector<Integer>();
static void get_prime(){
pri=new int[maxn];
fstp=new int[maxn];
for(int i=2;i<maxn;i++){
if (fstp[i]==0){
pri[++pri[0]]=i;
}
for(int j=1;j<=pri[0]&&i*pri[j]<maxn;j++){
int k=i*pri[j];
fstp[k]=pri[j];
if (i%pri[j]==0)
break;
}
}
}
static Vector<Integer> get_prime_factor(int n){
Vector<Integer> res=new Vector<Integer>();
res.clear();
for(int i=1;i<=pri[0]&&pri[i]*pri[i]<=n;i++)
if (n%pri[i]==0){
res.add(pri[i]);
while(n%pri[i]==0)
n/=pri[i];
}
if (n>1) res.add(n);
return res;
}
static long solve(long n,Vector<Integer> vec){
long res=0L;
final int m=vec.size();
for(long i=1L;i<(1L<<m);i++){
boolean tag=false;
long tmp=1L;
for(int j=0;j<m;j++)
if (((1L<<j)&i)>0){
tag^=true;
tmp*=vec.get(j).longValue();
}
res+=tag?n/tmp:-n/tmp;
}
return n-res;
}
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
get_prime();
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int T=in.nextInt(),cas=0;
while(T-->0){
long a=in.nextLong();
long b=in.nextLong();
int n=in.nextInt();
vec=get_prime_factor(n);
out.println("Case #"+(++cas)+": "+(solve(b,vec)-solve(a-1,vec)));
}
out.flush();
out.close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: