码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 4135 Co-prime

时间:2015-09-11 20:51:25      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

Co-prime

Time Limit: 1000ms
Memory Limit: 32768KB
This problem will be judged on HDU. Original ID: 4135
64-bit integer IO format: %I64d      Java class name: Main
 
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.
 

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

 
解题:容斥+数论
 
技术分享
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 typedef long long LL;
 4 vector<LL>g;
 5 LL solve(LL x,LL y) {
 6     g.clear();
 7     for(LL i = 2; i*i <= y; ++i) {
 8         if(y%i == 0) {
 9             while(y%i == 0) y /= i;
10             for(int j = g.size()-1; j >= 0; --j)
11                 if(abs(i*g[j] <= x)) g.push_back(-i*g[j]);
12             g.push_back(i);
13         }
14     }
15     if(y > 1) {
16         for(int i = g.size()-1; i >= 0; --i)
17             if(abs(y*g[i]) <= x) g.push_back(-y*g[i]);
18         g.push_back(y);
19     }
20     LL ret = 0;
21     for(int i = g.size()-1; i >= 0; --i) ret += x/g[i];
22     return x - ret;
23 }
24 int main() {
25     int t,cs = 1;
26     LL a,b,n;
27     scanf("%d",&t);
28     while(t--){
29         scanf("%I64d%I64d%I64d",&a,&b,&n);
30         printf("Case #%d: %I64d\n",cs++,solve(b,n) - solve(a-1,n));
31     }
32     return 0;
33 }
View Code

 

HDU 4135 Co-prime

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4802017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!