HDU 1695 GCD 解题报告(欧拉函数 + 容斥原理)

GCD

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


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, b/k]与区间[1, d/k]中的互质的数的对数。当然,k=0特判一下。
    假设m=b/k, n=d/k, n<=m。我们可以先求区间[1, n]里互质数的对数,然后加上区间[1, n]与区间[n+1, m]互质的数的对数。
    前半段我们可以使用欧拉函数,预处理并求和。后半段我们可以使用容斥原理。
    容斥原理的方法借鉴于改博文: http://www.cppblog.com/vici/archive/2011/09/05/155103.html
    时间最快是46ms,代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;
const int maxn = 111111;
bool good[maxn];
int deg[maxn], cnt[maxn];
int cas = 1;
int phi[maxn];
LL sum[maxn];

void all_phi()
{
	sum[1] = phi[1] = 1;
	for (int i = 2; i < maxn; i++)
	{
		if (!phi[i])
		{
			for (int j = i; j <= maxn; j += i)
			{
				if (!phi[j])
					phi[j] = j;
				phi[j] = phi[j] / i*(i - 1);
			}
		}
		// 求欧拉函数的和,即 i 与比 i 小的数组成的互质数的对数
		sum[i] = sum[i - 1] + phi[i];
	}
}

LL co_prime_pairs(int n, int m)
{
	// 求 1-n 与 (n+1)-m 中非互质的数的对数, n<=m
	for (int i = 0; i <= n; i++)
		good[i] = true, deg[i] = cnt[i] = 0;

	LL ans = 0;
	for (int i = 2; i <= n; i++)
	{
		if (good[i])
		{
			if (deg[i] == 0) deg[i]++;

			int add = (m / i - n / i)*(deg[i] & 1 ? 1 : -1);
			cnt[i] += add;

			for (int j = 2; i*j <= n; j++)
			{
				if (deg[i] == 1)
				if (j%i == 0)
					good[i*j] = false;
				else
					deg[i*j]++;
				cnt[i*j] += add;
			}
		}

		ans += cnt[i];
	}

	return ans;
}

void work()
{
	int a, b, c, t;
	scanf("%d%d%d%d%d", &t, &a, &t, &b, &c);

	if (c == 0) { printf("Case %d: 0\n", cas++); return; }

	a /= c;
	b /= c;
	if (a > b) swap(a, b);

	LL res = sum[a]; // [1, a]与[1, a]

	res -= co_prime_pairs(a, b); // [1, a]与[a+1, b]
	res += (LL)a*(b - a);

	printf("Case %d: %I64d\n", cas++, res);
}

int main()
{
	all_phi();
	int T;
	scanf("%d", &T);
	while (T--)
		work();
}

    杭电上有很多0ms的代码,我也很好奇。所以特地搜索了一下别人题解,果然发现了一段很快的代码(15ms),以及一个没有听过的名词:莫比乌斯反演。
    我正在学习这段代码,有什么收获会继续补充,代码如下:
#include <iostream>  
#include <stack>  
#include <queue>  
#include <cstdio>  
#include <cstdlib>  
#include <cmath>  
#include <set>  
#include <vector>  
#include <cstring>  
#include <algorithm>  
  
#define INF 0x3fffffff  
#define N 100000  
#define M 1000010  
#define LL long long  
#define mod 95041567  
  
using namespace std;  
  
int mu[N + 10], prim[N + 10];  
bool vis[N + 10];  
  
void Mobius(){  
    memset(vis, 0, sizeof(vis));  
    mu[1] = 1, mu[0] = 0;  
    int tot = 0;  
    for(int i = 2; i <= N; ++ i){  
        if(! vis[i]){  
            prim[tot ++] = i;  
            mu[i] = -1;  
        }  
        for(int j = 0; j < tot; ++ j){  
            if(i * prim[j] > N) break;  
            vis[i * prim[j]] = 1;  
            if(i % prim[j] == 0){  
                mu[i * prim[j]] = 0;  
                break;  
            }  
            else mu[i * prim[j]] = -mu[i];  
        }  
    }  
    for(int i = 2; i <= N; ++ i) mu[i] += mu[i - 1];  
}  
  
int main()  
{  
    Mobius();  
  //  freopen("in.txt","r",stdin);  
    int t, cnt = 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) {  
            printf("Case %d: 0\n", ++ cnt);  
            continue;  
        }  
        b /= k, d /= k;  
        int n = min(b, d), m = max(b, d);  
        if(n <= 1){  
            if(n == 1) printf("Case %d: %d\n", ++ cnt, m);  
            else printf("Case %d: 0\n", ++ cnt);  
            continue;  
        }  
        a = n, c = m;  
        int mid = sqrt(n * 1.0);  
        LL sum = 1;  
        k = 2;  
        for(int i = 2; i <= mid + 1; ++ i){  
            int v = (i - 1);  
            sum += ((LL)(n / v) * (m / v) - (LL)(n / v) * (n / v) / 2) *  (mu[v] - mu[v - 1]);  
            b = a;  
            a = n / i;  
            if(a < mid) a = mid;  
            if(a == b) continue;  
            sum -= (LL)v * v / 2 * (mu[b] - mu[a]);  
            while(m / k > b) ++ k;  
            while(1){  
                bool f = 0;  
                c = m / k;  
                if(c <= a) c = a, f = 1;  
                sum += (LL)(mu[b] - mu[c]) * v * (k - 1);  
                b = c;  
                if(f) break;  
                ++ k;  
            }  
        }  
        printf("Case %d: %I64d\n", ++ cnt, sum);  
    }  
    return 0;  
}  

    受到了HDU 2841 题一篇题解的启示,求解该题有了一个新的方法,时间125MS,代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

typedef long long LL;
const int maxn = 100010;
int deg[maxn];
int cas = 1;

LL cal(int n, int m)
{
    for(int i=0;i<=n;i++) deg[i]=0;

    LL ans = 0;
    for(int i=1;i<=n;i++)
    {
        deg[i]+=((long long)m*i/n);
        ans+=deg[i];

        for(int j=2;i*j<=n;j++)
            deg[i*j]-=deg[i];
    }

    return ans;
}

void work()
{
    int n,m,t,k;
    scanf("%d%d%d%d%d", &t, &n, &t, &m, &k);
    if(k==0)
    {
        printf("Case %d: 0\n", cas++);
        return;
    }
    n/=k;
    m/=k;
    printf("Case %d: %I64d\n", cas++, cal(n, m)+cal(m, n)-cal(min(n,m), min(n,m)));
}

int main()
{
    int T;
    scanf("%d\n", &T);
    while(T--)
        work();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值