GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17385    Accepted Submission(s): 6699

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.

 
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.
 
Output
For each test case, print the number of choices. Use the format in the example.
 
Sample Input
2
1 3 1 5 1
1 11014 1 14409 9
 
Sample Output
Case 1: 9
Case 2: 736427

Hint

For 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).

 
Source
 
就是求 [1, b / k]  和 [1, d / k] 互质数的对数
 先用欧拉函数求出 相同区间的互质的对数 多出来的 用容斥去求
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int ans;
LL tot[maxn + ];
int prime[maxn+], phi[maxn+];
bool vis[maxn+];
void getphi()
{
ans = ;
phi[] = ;
for(int i=; i<=maxn; i++)
{
if(!vis[i])
{
prime[++ans] = i;
phi[i] = i - ;
}
for(int j=; j<=ans; j++)
{
if(i * prime[j] > maxn) break;
vis[i * prime[j]] = ;
if(i % prime[j] == )
{ phi[i * prime[j]] = phi[i] * prime[j]; break;
}
else
phi[i * prime[j]] = phi[i] * (prime[j] - );
}
}
} int get_cnt(int n, int m)
{
int ans = ;
for(int i = ; i * i <= n; i++)
{
if(n % i) continue;
while(n % i == ) n /= i;
prime[ans++] = i;
}
if(n != ) prime[ans++] = n;
int res = ;
for(int i = ; i < ( << ans); i++)
{
int tmp = , cnt2 = ;
for(int j = ; j < ans; j++)
{
if(((i >> j) & ) == ) continue;
tmp *= prime[j];
cnt2++;
}
if(cnt2 & ) res += m / tmp;
else res -= m / tmp;
}
return m - res;
} int main()
{
getphi();
int a, b, c, d, k;
for(int i = ; i < maxn; i++)
{
tot[i] = tot[i - ] + phi[i]; }
int T, kase = ;
cin >> T;
while(T--)
{
scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
if(k == )
{
printf("Case %d: 0\n", ++kase);
continue;
}
int n = b / k, m = d / k;
LL sum = tot[n > m ? m : n];
// cout << sum << endl;
if(m > n) swap(n, m);
for(int i =m + ; i <= n; i++)
{
sum += get_cnt(i, m);
}
printf("Case %d: %lld\n", ++kase, sum);
} return ;
}

GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17385    Accepted Submission(s): 6699

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.

 
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.
 
Output
For each test case, print the number of choices. Use the format in the example.
 
Sample Input
2
1 3 1 5 1
1 11014 1 14409 9
 
Sample Output
Case 1: 9
Case 2: 736427

Hint

For 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).

 
Source

GCD HDU - 1695 (欧拉 + 容斥)的更多相关文章

  1. D - GCD HDU - 1695 -模板-莫比乌斯容斥

    D - GCD HDU - 1695 思路: 都 除以 k 后转化为  1-b/k    1-d/k中找互质的对数,但是需要去重一下  (x,y)  (y,x) 这种情况. 这种情况出现 x  ,y ...

  2. HDU 4135 Co-prime 欧拉+容斥定理

    Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  3. hdu 1695 欧拉函数+容斥原理

    GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  4. hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion

    http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...

  5. GCD nyoj 1007 (欧拉函数+欧几里得)

    GCD  nyoj 1007 (欧拉函数+欧几里得) GCD 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 The greatest common divisor ...

  6. HDU 3970 Harmonious Set 容斥欧拉函数

    pid=3970">链接 题解:www.cygmasot.com/index.php/2015/08/17/hdu_3970 给定n  求连续整数[0,n), 中随意选一些数使得选出的 ...

  7. HDU 1695 GCD (容斥原理+欧拉函数)

    题目链接 题意 : 从[a,b]中找一个x,[c,d]中找一个y,要求GCD(x,y)= k.求满足这样条件的(x,y)的对数.(3,5)和(5,3)视为一组样例 . 思路 :要求满足GCD(x,y) ...

  8. HDU1695:GCD(容斥原理+欧拉函数+质因数分解)好题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题目解析: Given 5 integers: a, b, c, d, k, you're to ...

  9. HDU 4135 Co-prime(容斥:二进制解法)题解

    题意:给出[a,b]区间内与n互质的个数 思路:如果n比较小,我们可以用欧拉函数解决,但是n有1e9.要求区间内互质,我们可以先求前缀内互质个数,即[1,b]内与n互质,求互质,可以转化为求不互质,也 ...

随机推荐

  1. position fixed 相对于父级定位

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. H5 29-div和span标签

    29-div和span标签 --> 努力到无能为力, 拼搏到感动自己 --> 我是div 我是div 我是span 我是span --> --> 我是段落 我是标题 --> ...

  3. hdu 2063 给男女匹配 (匈牙利算法)

    来源:http://acm.hdu.edu.cn/showproblem.php?pid=2063 题意: 有k个组合a,b组合,代表a愿意与b坐过山车,共m个女生 n个男生,问有多少个满意的匹配 题 ...

  4. Telnet服务器和客户端请求处理

    Telnet服务器和客户端请求处理 本文的控制台项目是根据SuperSocket官方Telnet示例代码进行调试的,官方示例代码:Telnet示例. 开始我的第一个Telnet控制台项目之旅: 创建控 ...

  5. java总结:Java中获取系统时间(年、月、日)以及下拉菜单默认选择系统年、月、日的方法

    <!-- 获取系统当前的年.月.日 --> <%@ page import="java.util.*"%> <% Calendar calendar= ...

  6. Git分支合并:Merge、Rebase的选择

    git代码合并:Merge.Rebase的选择 - iTech - 博客园http://www.cnblogs.com/itech/p/5188932.html Git如何将一个分支的修改同步到另一个 ...

  7. redis 运维手册

    redis cli命令 - milkty - 博客园https://www.cnblogs.com/kongzhongqijing/p/6867960.html Redis多个数据库 - EasonJ ...

  8. [翻译]在asp.net core2.0 OpenID Connect Handler中丢失了声明(CLaims)?

    注:这是一篇翻译,来自这里.这篇文章讲述了在asp.net core2.0中使用openid connect handler的过程中解析不到你想要的claim时,你可以参考这篇文章. Missing ...

  9. scp复制文件到远程服务器上

    scp -P 22 -r 2028792_www  root@120.79.172.45:/usr/local/src Linux scp命令用于Linux之间复制文件和目录. scp是 secure ...

  10. laravel创建项目

    composer create-project --prefer-dist laravel/laravel=5.5.* blog