GCD

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 5454    Accepted Submission(s): 1957

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

题意: 求(1,a) 和(1,b) 两个区间 公约数为k的对数的个数

思路:将a,b分别处以k,就能够转化为(1,a/k)和(1,b/k)两个区间两两互质的个数,能够先用欧拉函数求出(1,a)两两互质的个数,(a+1。b) 能够分解质因数。由于质因数的个数最多为7能够用容斥原理计算。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std; const int maxn = 10000+10;
const int maxxn = 100000+10;
typedef long long ll;
int a,b,gcd;
ll ans;
bool isPrime[maxn];
ll minDiv[maxxn],phi[maxxn],sum[maxxn];
vector<int> prime,cnt[maxxn],digit[maxxn]; void getPrime(){
prime.clear();
memset(isPrime,1,sizeof isPrime);
for(int i = 2;i < maxn; i++){
if(isPrime[i]){
prime.push_back(i);
for(int j = i*i; j < maxn; j+=i){
isPrime[j] = 0;
}
}
}
} void getPhi(){
for(ll i = 1; i < maxxn; i++){
minDiv[i] = i;
}
for(ll i = 2; i*i < maxxn; i++){
if(minDiv[i]==i){
for(int j = i*i; j < maxxn; j += i){
minDiv[j] = i;
}
}
}
phi[1] = 1;
sum[1] = 1;
for(ll i = 2; i < maxxn; i++){
phi[i] = phi[i/minDiv[i]];
if((i/minDiv[i])%minDiv[i]==0){
phi[i] *= minDiv[i];
}else{
phi[i] *= minDiv[i]-1;
}
sum[i] = phi[i]+sum[i-1];
}
} void getDigit(){
for(ll i = 1; i < maxxn; i++){
int x = i;
for(int j = 0; j < prime.size()&&x >= prime[j]; j++){
if(x%prime[j]==0){
digit[i].push_back(prime[j]);
int t = 0;
while(x%prime[j]==0){
t++;
x /= prime[j];
}
cnt[i].push_back(t);
}
}
if(x!=1){
digit[i].push_back(x);
cnt[i].push_back(1);
}
}
} int main(){
getPrime();
getPhi();
getDigit();
int ncase,T=1;
cin >> ncase;
while(ncase--){
int t1,t2;
scanf("%d%d%d%d%d",&t1,&a,&t2,&b,&gcd);
if(gcd==0){
printf("Case %d: 0\n",T++,ans);
continue;
}else{
if(a > b) swap(a,b);
a /= gcd,b /= gcd;
ans = sum[a];
for(ll i = a+1; i <= b; i++){
int d = digit[i].size();
int t = 0;
vector<int> di;
for(int k = 1; k < (1<<d); k++){
di.clear();
for(int f = 0; f < d; f++){
if(k&(1<<f)){
di.push_back(digit[i][f]);
}
}
int ji = 1;
for(int f = 0; f < di.size(); f++){
ji *= di[f];
}
if(di.size()%2==0){
t -= a/ji;
}else{
t += a/ji;
}
}
ans += a-t;
}
printf("Case %d: ",T++);
cout<<ans<<endl;
} }
return 0;
}

HDU1695-GCD(数论-欧拉函数-容斥)的更多相关文章

  1. hdu1695(莫比乌斯)或欧拉函数+容斥

    题意:求1-b和1-d之内各选一个数组成数对.问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个能够简化成1-b/k 和1-d/k 的互质有序数对的个数 ...

  2. hdu 1695 GCD(欧拉函数+容斥)

    Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD( ...

  3. HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演

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

  4. HDU 1695 GCD 欧拉函数+容斥定理

    输入a b c d k求有多少对x y 使得x在a-b区间 y在c-d区间 gcd(x, y) = k 此外a和c一定是1 由于gcd(x, y) == k 将b和d都除以k 题目转化为1到b/k 和 ...

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

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

  6. bzoj 2818 GCD 数论 欧拉函数

    bzoj[2818]Gcd Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. Input 一个整数N Output 如题 Samp ...

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

    F - GCD Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Stat ...

  8. 【bzoj2818】: Gcd 数论-欧拉函数

    [bzoj2818]: Gcd 考虑素数p<=n gcd(xp,yp)=p 当 gcd(x,y)=1 xp,yp<=n满足条件 p对答案的贡献: 预处理前缀和就好了 /* http://w ...

  9. 【poj 3090】Visible Lattice Points(数论--欧拉函数 找规律求前缀和)

    题意:问从(0,0)到(x,y)(0≤x, y≤N)的线段没有与其他整数点相交的点数. 解法:只有 gcd(x,y)=1 时才满足条件,问 N 以前所有的合法点的和,就发现和上一题-- [poj 24 ...

随机推荐

  1. js创建对象的几种常用方式小结

    第一种模式:工厂方式  var lev=function(){ return "666"; }; function Parent(){ var Child = new Object ...

  2. vijos 1047 送给圣诞夜的礼品 矩阵

    题目链接 描述 当小精灵们把贺卡都书写好了之后.礼品准备部的小精灵们已经把所有的礼品都制作好了.可是由于精神消耗的缘故,他们所做的礼品的质量越来越小,也就是说越来越不让圣诞老人很满意.可是这又是没有办 ...

  3. [原创]浅谈NT下Ring3无驱进入Ring0的方法

    原文链接:浅谈NT下Ring3无驱进入Ring0的方法 (测试环境:Windows 2000 SP4,Windows XP SP2.Windows 2003 未测试) 在NT下无驱进入Ring0是一个 ...

  4. Some good questions

    (一)#include <stdio.h>#include <stdlib.h>void getmemory(char *p){ p=(char *) malloc(100); ...

  5. 5.java.lang.IndexOutOfBoundsException(数组下标越界异常)

    数组下标越界异常 查看调用的数组或者字符串的下标值是不是超出了数组的范围,一般来说,显示(即直接用常数当下标)调用不太容易出这样的错,但隐式(即用变量表示下标)调用就经常出错了,还有一种情况,是程序中 ...

  6. 蓝桥杯之K好数问题

    问题描述 如果一个自然数N的K进制表示中任意的相邻的两位都不是相邻的数字,那么我们就说这个数是K好数.求L位K进制数中K好数的数目.例如K = 4,L = 2的时候,所有K好数为11.13.20.22 ...

  7. asp.net DropDownList实现ToolTip功能

    在绑定DropDownList控件时,可能出现绑定显示的文本过长以至于超过控件长度的内容看不到,这时候就需要使用ToolTip完成其功能,即鼠标放到相应选项后就可显示其完成内容. 首先,在页面引入jQ ...

  8. spm3 基本

    spm3 命令 spm init //初始化一个spm模块,会生成基本配置以及测试文件等(下图). //注 初始化以后一般需要 鲜执行一下 spm install 安装默认依赖模块 index.js就 ...

  9. 使用weight属性实现视图的居中显示

    有的时候我们开发一个产品的时候需要让其中某个控件的宽度或高度占据其父容器的宽度或高度的一半显示,这个时候由于设备尺寸的限制,做到在每个设备上都具有同样的效果的话,我们就需要用到weightSum属性和 ...

  10. access 语句错误

    一直说是语句错误,一直没有找出来是什么错误,原来access的语句需要在字段上套一个[],这是最正确的写法,关键是动软生成的是我们一贯用的,和标准还是有些差别的,害了我好久都不知道是哪里的问题