码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 1695 - GCD

时间:2017-08-15 10:03:27      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:void   space   cpp   long   ret   pac   mob   amp   lld   

刷刷水题

#include <bits/stdc++.h>
using namespace std;
#define LL long long
const int MOD = 10007;
const int N = 1e5+5;
bool notp[N];
int prime[N], pnum, mu[N];
int sum[N];
void Mobius() {
	memset(notp, 0, sizeof(notp));
	mu[1] = 1;
	for (int i = 2; i < N; i++) {
		if (!notp[i]) prime[++pnum] = i, mu[i] = -1;
		for (int j = 1; prime[j]*i < N; j++) {
			notp[prime[j]*i] = 1;
			if (i%prime[j] == 0) {
				mu[prime[j]*i] = 0;
				break;
			}
			mu[prime[j]*i] = -mu[i];
		}
	}
	sum[0] = 0;
	for (int i = 1; i < N; i++) sum[i] = sum[i-1] + mu[i];
}
LL Cal(int n, int m, int k)
{
    if (k == 0) return 0;
    if (n > m) swap(n, m);
    n /= k, m /= k;
    if (n == 0) return 0;
    LL ans = 0;
    int last;
    for (int i = 1; i <= n; i = last+1)
    {
        last = min(n/(n/i), m/(m/i));
        ans += (LL)(sum[last] - sum[i-1]) * ( (LL)(n/i)*(n/i-1)/2 + (n/i) + (LL)(m/i-n/i)*(n/i) );
    }
    return ans;
}
int t, a, b, c, d, k;
int main()
{
    Mobius();
    scanf("%d", &t);
    for (int tt = 1; tt <= t; tt++)
    {
        scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
        printf("Case %d: %lld\n", tt, Cal(b, d, k));
    }
}

  

HDU 1695 - GCD

标签:void   space   cpp   long   ret   pac   mob   amp   lld   

原文地址:http://www.cnblogs.com/nicetomeetu/p/7362084.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!