hdu1695—GCD(求两个区间内互质的数的对数)
来源:互联网 发布:软件开发外包管理制度 编辑:程序博客网 时间:2024/06/17 17:09
题目链接:传送门
GCD
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11844 Accepted Submission(s): 4465
Problem Description
Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y) = k. GCD(x, y) means the greatest common divisor of x and y. Since the number of choices may be very large, you're only required to output the total number of different number pairs.
Please notice that, (x=5, y=7) and (x=7, y=5) are considered to be the same.
Yoiu can assume that a = c = 1 in all test cases.
Please notice that, (x=5, y=7) and (x=7, y=5) are considered to be the same.
Yoiu can assume that a = c = 1 in all test cases.
Input
The input consists of several test cases. The first line of the input is the number of the cases. There are no more than 3,000 cases.
Each case contains five integers: a, b, c, d, k, 0 < a <= b <= 100,000, 0 < c <= d <= 100,000, 0 <= k <= 100,000, as described above.
Each case contains five integers: a, b, c, d, k, 0 < a <= b <= 100,000, 0 < c <= d <= 100,000, 0 <= k <= 100,000, as described above.
Output
For each test case, print the number of choices. Use the format in the example.
Sample Input
21 3 1 5 11 11014 1 14409 9
Sample Output
Case 1: 9Case 2: 736427HintFor the first sample input, all the 9 pairs of numbers are (1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (2, 3), (2, 5), (3, 4), (3, 5).
解题思路:求两个区间内gcd(x,y) = k,那么x/k与y/k互质,即求区间[1,b/k]与[1,d/k]内有多少对数互质,可以先求一个数与某个区间互质的数的个数。
#include <iostream>#include <cstring>#include <cstdio>#include <vector>using namespace std;typedef long long ll;const int N = 100300;const int INF = 0x3f3f3f3f;int solve(int n,int r){ vector<int>p; for(int i=2; i*i<=n; ++i) if(n%i == 0){ p.push_back (i); while(n%i == 0) n /= i; } if(n > 1) p.push_back(n); int sz = p.size(); int sum = 0; for(int i=1; i<(1<<sz); ++i){ int mult = 1,bits = 0; for (int j=0; j<sz; ++j) if (i&(1<<j)) { ++bits; mult *= (ll)p[j]; } int cur = r/mult; if (bits % 2 == 1) sum += cur; else sum -= cur; } return r - sum;}int main(){ int T,cas = 0; scanf("%d",&T); while( T-- ){ int a,b,c,d,k; scanf("%d%d%d%d%d",&a,&b,&c,&d,&k); if(k == 0) {printf("Case %d: %d\n",++cas,0);continue;} int t1 = b/k; int t2 = d/k; if( t1 > t2 ) swap(t1,t2); ll sum = 0; for( int i = 1 ; i <= t1 ; ++i ) sum += (ll)solve(i,t1); sum++; sum /= 2; for( int i = t1+1 ; i <= t2 ; ++i ) sum += (ll)solve(i,t1); printf("Case %d: %lld\n",++cas,sum); } return 0;}
阅读全文
0 0
- hdu1695—GCD(求两个区间内互质的数的对数)
- !HDU 5317 求区间里两个数的质因数个数的gcd的最大值-预处理
- 求两个数的公约数(gcd)
- 求逆序数的对数
- zoj 1577 (gcd,lcm)求 所有的对数。
- 求两个正整数的最大公约数(GCD)
- 求两个自然数的最大公约数(GCD)?
- 求数列的逆序数对数问题
- 求任意数的质数对数
- hdu 5869 (求区间不同gcd的个数)
- C Tricks(十九)—— 求以任意数为底的对数
- hdu 5726 gcd rmq 求相同gcd的区间数量
- 求复数的对数
- 求两个正整数的最大公约数GCD
- BZOJ 2818: Gcd区间内最大公约数 为素数的对数(欧拉函数的应用)
- HDU-A Mathematical Curiosity-求满足条件的数对数
- 求两个数的和(不用+-*/)
- 求两个数的最大公约数(一)
- ReactNative的介绍
- 本文实例讲述了js获取当前日期前七天的方法
- MenuItemFont、MenuItemLabel
- 心情小感悟
- MenuItemToggle
- hdu1695—GCD(求两个区间内互质的数的对数)
- 前端经典面试题---Ajax篇
- 【 zoj 3348 】Schedule 【网络流经典建图】
- LeetCode 404. Sum of Left Leaves
- Spring:BeanNotOfRequiredTypeException
- 学习spring
- EditBox
- Android中canvas常用方法
- struts in actin(Ted Husted 著)struts1 源码配置问题 记录