hdu1695 GCD2 容斥原理 求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。

GCD
Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10992    Accepted Submission(s): 4157

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

/**
题目:hdu1695 GCD2
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695
题意:求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。
思路:
gcd(x,y)=k => gcd(x/k,y/k) = 1;

则求x/k与y/k互质对数。

即求:[1,b/k]与[1,d/k]之间互质的对数

设x属于[1,b/k], y属于[1,d/k];
枚举x,求x与y互质的对数。所以要预处理所有x的质因子。然后容斥处理。由于(x,y)=>(5,7),(7,5)是同一组。
所以:答案为ans += (d/k) - x在d/k中不互质的数 - (x-1);
*/
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<map>
#include<set>
#include<cmath>
#include<queue>
#define LL long long
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e5+10;
vector<int> prime[maxn];
int flag[maxn];
void init()
{
    memset(flag, 0, sizeof flag);
    for(ll i = 2; i < maxn; i++){
        if(flag[i]==0){
            prime[i].push_back(i);
            for(ll j = 2*i; j < maxn; j+=i){
                prime[j].push_back(i);
                flag[j] = 1;
            }
        }
    }
}
ll rc(int pos,int n)
{
    ll sum = 0;
    ll mult, ones;
    ll len = prime[pos].size();
    ll m = 1<<len;
    for(int i = 1; i < m; i++){
        ones = 0;
        mult = 1;
        for(int j = 0; j < len; j++){
            if(i&(1<<j)){
                ones++;
                mult = mult*prime[pos][j];
                if(mult>n) break;
            }
        }
        if(ones%2==0){
            sum -= n/mult-(pos-1)/mult;
        }else
        {
            sum += n/mult-(pos-1)/mult;
        }
    }
    return n-(pos-1)-sum;
}
int main()
{
    init();
    int T;
    int cas = 1;
    int a, b, c, d, k;
    cin>>T;
    while(T--)
    {
        scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
        if(k==0){
            printf("Case %d: 0\n",cas++);continue;
        }
        if(b>d) swap(b,d);///for b<=d;
        b = b/k;
        d = d/k;
        ll ans = 0;
        if(b>=1){
            ans += d;
        }
        for(int i = 2; i <= b; i++){
            ans += rc(i,d);
        }
        printf("Case %d: %lld\n", cas++,ans);
    }
    return 0;
}
时间: 2024-10-30 06:10:51

hdu1695 GCD2 容斥原理 求x属于[1,b]与y属于[1,d],gcd(x,y)=k的对数。(5,7)与(7,5)看作同一对。的相关文章

CodeForces 61E Enemy is weak 求i&lt;j&lt;k &amp;&amp; a[i]&gt;a[j]&gt;a[k] 的对数 树状数组

题目链接:点击打开链接 题意是求 i<j<k && a[i]>a[j]>a[k] 的对数 如果只有2元组那就是求逆序数的做法 三元组的话就用一个树状数组x表示 数字i前面有多少个比自己大的个数 然后每次给这个y数组求和,再把x中>a[i]的个数存入y中即可 #include <algorithm> #include <cctype> #include <cassert> #include <cstdio> #in

三元逆序对 求i&lt;j&lt;k &amp;&amp; a[i]&gt;a[j]&gt;a[k] 的对数 树状数组Codeforces 61E Enemy is weak

http://codeforces.com/problemset/problem/61/E E. Enemy is weak time limit per test 5 seconds memory limit per test 256 megabytes input standard input output standard output The Romans have attacked again. This time they are much more than the Persian

容斥原理 求M以内有多少个跟N是互质的

开始系统的学习容斥原理!通常我们求1-n中与n互质的数的个数都是用欧拉函数! 但如果n比较大或者是求1-m中与n互质的数的个数等等问题,要想时间效率高的话还是用容斥原理! 本题是求[a,b]中与n互质的数的个数,可以转换成求[1,b]中与n互质的数个数减去[1,a-1]与n互质的数的个数. #include<iostream> #include<cstdio> #include<cstring> using namespace std; #define LL long

hdu4153(容斥原理求质数)

传送门 ac代码: #include<bits/stdc++.h> #define per(i,a,b) for(int i=a;i<=b;i++) using namespace std; typedef long long ll; //#define int long long const ll inf =2333333333333333LL; const double eps=1e-8; int read(){ char ch=getchar(); int res=0,f=0; w

Codeforces 475D CGCDSSQ 求序列中连续数字的GCD=K的对数

题目链接:点击打开链接 #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <iostream> #include <cmath> using namespace std; typedef long long ll; template <class T> inline bool rd(T &ret)

hdu1695 GCD 莫比乌斯反演做法+枚举除法的取值 (5,7),(7,5)看做同一对

/** 题目:hdu1695 GCD 链接:http://acm.hdu.edu.cn/status.php 题意:对于给出的 n 个询问,每次求有多少个数对 (x,y) , 满足 a ≤ x ≤ b , c ≤ y ≤ d ,且 gcd(x,y) = k ,(5,7),(7,5)看做同一对, gcd(x,y) 函数为 x 和 y 的最大公约数. 本题默认:a = c = 1; 0 < a <= b <= 100,000, 0 < c <= d <= 100,000,

[bzoj2301: [HAOI2011]Problem b] 求

</pre><pre code_snippet_id="507886" snippet_file_name="blog_20141104_2_5383199" name="code" class="cpp">#include <iostream> #include <algorithm> #include <vector> #include <map> #

HDU 1695 GCD (数论-整数和素数,组合数学-容斥原理)

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(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 t

BZOJ 2301: [HAOI2011]Problem b(莫比乌斯反演 + 容斥原理 + 分块优化)

传送门 Problem 2301. – [HAOI2011]Problem b 2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 3671  Solved: 1643[Submit][Status][Discuss] Description 对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数. Input