Co-prime(hdu4135)
Co-prime
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3313 Accepted Submission(s): 1286
Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.
1 10 2
3 15 5
Case #2: 10
In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.
1 #include<stdio.h>
2 #include<algorithm>
3 #include<iostream>
4 #include<stdlib.h>
5 #include<string.h>
6 #include<vector>
7 #include<queue>
8 #include<stack>
9 using namespace std;
10 long long gcd(long long n,long long m);
11 bool prime[100005];
12 int ans[100005];
13 int bns[100005];
14 int dd[100005];
15 typedef long long LL;
16 int main(void)
17 {
18 int i,j,k;
19 scanf("%d",&k);
20 int s;
21 LL n,m,x;
22 for(i=2; i<=1000; i++)
23 {
24 if(!prime[i])
25 {
26 for(j=i; i*j<=100000; j++)
27 {
28 prime[i*j]=true;
29 }
30 }
31 }
32 int cnt=0;
33 for(i=2; i<=100000; i++)
34 {
35 if(!prime[i])
36 {
37 ans[cnt++]=i;
38 }
39 }
40 for(s=1; s<=k; s++)
41 {
42 int uu=0;
43 memset(dd,0,sizeof(dd));
44 scanf("%lld %lld %lld",&n,&m,&x);
45 while(x>=1&&uu<cnt)
46 {
47 if(x%ans[uu]==0)
48 {
49 dd[ans[uu]]=1;
50 x/=ans[uu];
51 }
52 else
53 {
54 uu++;
55 }
56 }
57 int qq=0;
58 for(i=2; i<=100000; i++)
59 {
60 if(dd[i])
61 {
62 bns[qq++]=i;
63 }
64 }
65 if(x!=1)
66 bns[qq++]=x;
67 n--;
68
69 LL nn=0;
70 LL mm=0;
71 for(i=1; i<=(1<<qq)-1; i++)
72 {
73 int xx=0; LL sum=1;
74 int flag=0;
75 for(j=0; j<qq; j++)
76 {
77 if(i&(1<<j))
78 {
79 xx++;
80 LL cc=gcd(sum,bns[j]);
81 sum=sum/cc*bns[j];
82 if(sum>m)
83 {
84 flag=1;
85 break;
86 }
87 }
88 }
89 if(flag)
90 continue;
91 else
92 {
93 if(xx%2==0)
94 {
95 nn-=n/sum;
96 mm-=m/sum;
97 }
98 else
99 {
100 nn+=n/sum;
101 mm+=m/sum;
102 }
103 }
104 }m-=mm;n-=nn;
105 printf("Case #%d: ",s);
106 printf("%lld\n",m-n);
107 }
108 return 0;
109 }
110 long long gcd(long long n,long long m)
111 {
112 if(m==0)
113 return n;
114 else if(n%m==0)
115 return m;
116 else return gcd(m,n%m);
117 }
Co-prime(hdu4135)的更多相关文章
- [HDU4135]CO Prime(容斥)
也许更好的阅读体验 \(\mathcal{Description}\) \(t\)组询问,每次询问\(l,r,k\),问\([l,r]\)内有多少数与\(k\)互质 \(0<l<=r< ...
- 【hdu4135】【hdu2841】【hdu1695】一类通过容斥定理求区间互质的方法
[HDU4135]Co-prime 题意 给出三个整数N,A,B.问在区间[A,B]内,与N互质的数的个数.其中N<=10^9,A,B<=10^15. 分析 容斥定理的模板题.可以通过容斥 ...
- Java 素数 prime numbers-LeetCode 204
Description: Count the number of prime numbers less than a non-negative number, n click to show more ...
- Prime Generator
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate ...
- POJ 2739. Sum of Consecutive Prime Numbers
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 20050 ...
- UVa 524 Prime Ring Problem(回溯法)
传送门 Description A ring is composed of n (even number) circles as shown in diagram. Put natural numbe ...
- Sicily 1444: Prime Path(BFS)
题意为给出两个四位素数A.B,每次只能对A的某一位数字进行修改,使它成为另一个四位的素数,问最少经过多少操作,能使A变到B.可以直接进行BFS搜索 #include<bits/stdc++.h& ...
- hdu 5901 count prime & code vs 3223 素数密度
hdu5901题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5901 code vs 3223题目链接:http://codevs.cn/problem ...
- 最小生成树 prime zoj1586
题意:在n个星球,每2个星球之间的联通需要依靠一个网络适配器,每个星球喜欢的网络适配器的价钱不同,先给你一个n,然后n个数,代表第i个星球喜爱的网络适配器的价钱,然后给出一个矩阵M[i][j]代表第i ...
- 最小生成树 prime poj1258
题意:给你一个矩阵M[i][j]表示i到j的距离 求最小生成树 思路:裸最小生成树 prime就可以了 最小生成树专题 AC代码: #include "iostream" #inc ...
随机推荐
- 4.1 python中调用rust程序
概述 使用rust-cpython将rust程序做为python模块调用: 通常为了提高python的性能: 参考 https://github.com/dgrunwald/rust-cpython ...
- idea maven 项目 遇到 "Module not specified" 解决方法
1. 原因:我这边出现的原因是 其他同事在提交代码是 将 这个文件夹也提交了,idea 会加载 .idea 里的配置(即 他的配置),而我的 maven 配置不同,导致出错. 2. 解决方法:删除这 ...
- Leetcode 78题-子集
LeetCode 78 网上已经又很多解这题的博客了,在这只是我自己的解题思路和自己的代码: 先贴上原题: 我的思路: 我做题的喜欢在本子或别处做写几个示例,以此来总结规律:下图就是我从空数组到数组长 ...
- 为什么volatile能保证有序性不能保证原子性
对于内存模型的三大特性:有序性.原子性.可见性. 大家都知道volatile能保证可见性和有序性但是不能保证原子性,但是为什么呢? 一.原子性.有序性.可见性 1.原子性: (1)原子的意思代表着-- ...
- shell脚本 mysqldump方式全备份mysql
一.简介 源码地址 日期:2018/10/8 介绍:mysqldump方式全备份脚本,并保存固定天数的全备份 效果图: 二.使用 适用:centos6+ 语言:中文 注意:使用前先查看脚本,修改对应变 ...
- Python 中更安全的 eval
问题 想要将一段列表形式的字符串转为 list,但是担心这个动态的字符串可能是恶意的代码?使用 eval 将带来安全隐患.比如: # 期望是 eval('[1, 2, 3]') # 实际上是 eval ...
- spring security oauth2搭建resource-server demo及token改造成JWT令牌
我们在上文讲了如何在spring security的环境中搭建基于oauth2协议的认证中心demo:https://www.cnblogs.com/process-h/p/15688971.html ...
- 基于MCRA-OMLSA的语音降噪(一):原理
前面的几篇文章讲了webRTC中的语音降噪.最近又用到了基于MCRA-OMLSA的语音降噪,就学习了原理并且软件实现了它.MCRA主要用于噪声估计,OMLSA是基于估计出来的噪声去做降噪.类比于web ...
- JetBrains又出神器啦!Fleet,体验飞一般的感觉
目录 简介 从eclipse到Fleet Fleet的特性 JetBrains Space 总结 简介 java开发的同学可能对于JetBrains这家公司并不陌生,因为JetBrains号称拥有世界 ...
- MemoryCache 如何清除全部缓存?
最近有个需求需要定时清理服务器上所有的缓存.本来以为很简单的调用一下 MemoryCache.Clear 方法就完事了.谁知道 MemoryCache 类以及 IMemoryCache 扩展方法都没有 ...