数学: HDU Co-prime
2024-08-28 23:12:16
Co-prime
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 12 Accepted Submission(s) : 4
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
Problem Description
Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.
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.
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.
Input
The first line on input contains T (0 < T <= 100) the number of test cases, each of the next T lines contains three integers A, B, N where (1 <= A <= B <= 1015) and (1 <=N <= 109).
Output
For each test case, print the number of integers between A and B inclusive which are relatively prime to N. Follow the output format below.
Sample Input
2
1 10 2
3 15 5
Sample Output
Case #1: 5
Case #2: 10
Hint
In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}.
Source
The Third Lebanese Collegiate Programming Contest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
#include<iostream> #include<cstring> #include<cstdio> using namespace std; typedef long long LL; const int N = 1e5+5; LL f[N],prime[N],vis[N],cnt,k; void prime_factor(){ memset (vis,0, sizeof (vis)); vis[0]=vis[1] = 1,cnt = 0; for (LL i=2;i*i<N;i++) if (!vis[i]) for (LL j=i*i;j<N;j+=i) vis[j] = 1; for (LL i=0;i<N;i++) if (!vis[i]) prime[cnt++] = i; } LL poie(LL x){ LL ret = 0,sum,tmp; for (LL i=1;i<(1LL<<k);i++){ tmp = 1,sum=0; for (LL j=0;j<k;j++) if (i&(1LL<<j)){sum++,tmp*=f[j];} if (sum&1) ret += x/tmp; else ret -= x/tmp; } return ret; } void solve_question(LL A,LL B,LL n){ LL tmp = n; k = 0 ; for (LL i=0;prime[i]*prime[i]<= tmp;i++){ if (tmp%prime[i]==0) f[k++] = prime[i]; while (tmp%prime[i]==0) tmp/=prime[i]; } if (tmp > 1) f[k++] = tmp; LL ans =B-poie(B)-A+1+poie(A-1); printf ( "%I64d\n" ,ans); } int main(){ int T,Case=0; LL A,B,n; scanf ( "%d" ,&T); prime_factor(); while (T--){ scanf ( "%I64d %I64d %I64d" ,&A,&B,&n); printf ( "Case #%d: " ,++Case); solve_question(A,B,n); } } |
最新文章
- geotrellis使用(二十二)实时获取点状目标对应的栅格数据值
- [LeetCode] Single Number
- MySQL命令行导出数据库
- Oracle dbms_lock.sleep()存储过程使用技巧-场景-分析-实例
- user database的initial size和dbcc shrinkfile
- NoSQL分类及ehcache memcache redis 三大缓存的对比
- jQuery中的DOM操作<;思维导图>;
- 【原创】batch-GD, SGD, Mini-batch-GD, Stochastic GD, Online-GD -- 大数据背景下的梯度训练算法
- 设置nginx禁止通过IP访问服务器的方法
- 如何跳到系统设置界面-b
- SGU 128.Snake
- Jenkins的错误“error fetching remote repo origin”的问题解决
- UESTC_神秘绑架案 CDOJ 881
- myeclipse 2013 SR2 安装svn
- Mybatis的mapper代理开发dao方法
- linux 批量测试域名返回码脚本
- Orchard详解--第一篇 介绍
- iOS10 远程通知需要有entitlements的支持
- GLSL版本的区别和对比
- Oracle HAVING子句 - 转