【hdu 1695 】GCD 【容斥+欧拉 or mobius反演】

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

gcd 的一个性质 Gcd(a,b)==k –>> Gcd(a/k,b/k)==1
然后我们就可以转换问题 n=b/k;m=d/k;
求[1,n] 和[1,m]之间互质的个数(不重复)
我们令 x=min(n,m); y=max(n,m);
所以我们可以先求[1,x]与[1,x]之间的补不重复的互质的个数,对这个问题,其实就是 [1,x]的欧拉函数的值,剩下一部分[n+1,m] 与[1,n] 之间互质的个数,我们可以用容斥。
代码

#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN = 1e5+100;
const int MAXM = 1e7;
const int mod =1e9+7;
const int inf=0x3f3f3f3f;
LL e[MAXN];//欧拉筛法
void eular(){
    for(int i=0;i<MAXN;i++) e[i]=i;
    for(int i=2;i<MAXN;i++)
        if(e[i]==i)
            for(int j=i;j<MAXN;j+=i) e[j]=e[j]/i*(i-1);
}
LL p[30],ge;  // 求 m在[1,n]之间互质的个数
void getn(LL n){
    ge=0;
    for(LL i=2;i*i<=n;i++){
        if(n%i==0) p[ge++]=i;
        while(n%i==0) n/=i;
    }
    if(n>1) p[ge++]=n;
}
LL que[MAXN],top;
LL nop(LL m){
    top=0;
    que[top++]=-1;
    for(int i=0;i<ge;i++){
        int t=top;
        for(int j=0;j<t;j++)
            que[top++]=(-1)*que[j]*p[i];
    }
    LL ans=0;
    for(int i=1;i<top;i++) ans+=m/que[i];
    return m-ans;
}
LL a,b,c,d,k;
int main(){
    int t;cin>>t;
    eular();
    for(int i=1;i<=t;i++){
        scanf("%lld%lld%lld%lld%lld,&n",&a,&b,&c,&d,&k);
        LL ans=0;
        if(k==0)  ans=0;
        else {
                LL x=b/k;LL y=d/k;
        LL n=min(x,y);LL m=max(x,y);

        for(LL i=1;i<=n;i++) ans+=e[i];
    //  printf("%lld\n",ans);
        for(LL i=n+1;i<=m;i++){
            getn(i);
            ans+=nop(n);
        }
    }
        printf("Case %d: %lld\n",i,ans);
    }
    return 0;
}

第二种, 可以用mobius 反演。
前面步骤一样,转化为求[1,x] 和[1,y]之间的互质的对数。
我们可以定义函数f(n) 表示gcd(x,y)=n的对数。
F(n)=f(n * 1 )+f( n * 2) +f(n * 3 ) …..
F(n) 就表示为[1,x] 和[1,y]之间gcd为n的倍数的个数。
同时F(n)还可以用(y/n)*(x/n)来解决。
所以我们就可以用反演来求
f(1) = μ(1)* F(1) + μ(2)*F(2) + μ(3)*F(3) + …

刚学mobius ,感觉能够设对函数f(n)很重要,同时我们还要能够找到一个函数F(n) 使他既可以利用f(n)来表示出来,同时也可以用别的方法计算得到,这样我们才可以得出一个等式,然后才可以用反演来求f(n) 。
代码

#include<bits/stdc++.h>
using namespace std;
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 1e5+10;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

bool su[MAXN + 1];
int mu[MAXN + 1],prm[MAXN + 1], cnt;
inline void init(){  // 线性筛得到mobius函数
    su[0]=su[1]= true;
    mu[1]=1;
    for (int i=2;i<=MAXN;i++) {
        if (!su[i]){
            prm[++cnt]=i;
            mu[i]=-1;
        }
        for (int j=1;j<=cnt;j++) {
            int t=i*prm[j];
            if (t>MAXN) break;
            su[t]=true;
            if (i%prm[j]==0) {
                mu[t]=0;
                break;
            } else mu[t]=-mu[i];
        }
    }
}

LL mobius(int a,int b){
    LL ans=0;    
    for(LL i=1;i<=a;i++) 
        ans=ans+1ll*mu[i]*(a/i)*(b/i);
    LL temp=0;
    for(LL i=1;i<=a;i++)  // 去重
        temp+=1ll*mu[i]*(a/i)*(a/i);
    return ans-temp/2;
}

int main(){
    CLOSE();
//  fread();
//  fwrite();
    init();
    int T;cin>>T;int ncase=1;
    while(T--){
        printf("Case %d: ",ncase++);
        int a,b,c,d,k;  cin>>a>>b>>c>>d>>k;
        int x,y;x=b/k;y=d/k;
        if(k==0) {  puts("0"); continue; }
        if(x>y) swap(x,y);
        printf("%lld\n",mobius(x,y));
    }
    return 0;
}

方法三

Ans=i=1min(bk,dk)μ(i)bkidki A n s = ∑ i = 1 min ( b k , d k ) μ ( i ) ∗ ⌊ b k ∗ i ⌋ ∗ ⌊ d k ∗ i ⌋

可以用整除分块求 做到 o(Tsqrt(bk)) o ( T ∗ s q r t ( b k ) )

Code

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <list>
#include <string>
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define rep(i, l, r) for(int i = l; i < r; i++)
#define per(i, r, l) for(int i = r; i >= l; i--)

#include <ext/rope>
using namespace __gnu_cxx;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int>pii;

const int N = (int) 1e5 + 11;
const int M = (int) 1e6 + 11;
const int MOD = (int) 1e9 + 7;
const double EPS = (double) 1e-9;
const double PI = (double)acos(-1.0);
const int INF = (int) 0x3f3f3f3f;
const ll INFF = (ll) 0x3f3f3f3f3f3f3f3f;

void read(int &x){
    char ch = getchar(); x = 0;
    while(ch < '0' || ch > '9') ch = getchar();
    while(ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
}
/*-----------------------------------------------------------------------------------*/ 

bool su[N] = {1, 1, 0};
int prm[N], mu[N], sz = 0;
int summu[N];
void init(int n){
    mu[1] = 1;
    for(int i = 2; i <= n; i++){
        if(!su[i]){
            prm[++sz] = i;
            mu[i] = -1;
        }
        for(int j = 1; j <= sz; j++){
            ll t = i * prm[j];
            if(t > n) break;
            su[t] = 1;
            if(i % prm[j] == 0) {
                mu[t] = 0;
                break;
            }else {
                mu[t] = -mu[i];
            }
        }
    }
    summu[0] = 0;
    for(int i = 1; i <= n; i++) summu[i] = summu[i - 1] + mu[i];
}
ll solve(int n, int m, int c){
    n /= c; m /= c;  
    int z = min(n, m);
    ll ans = 0;
    for(int l = 1, r; l <= z; l = r + 1){
        r = min(n / (n / l), m / (m / l));
        ans += (summu[r] - summu[l - 1]) * 1ll * (n / l) * (m / l); 
    }       
    return ans;
}
int main(){
    #ifndef ONLINE_JUDGE
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w", stdout);
    #endif
    init(100000);
    int T;  scanf("%d", &T);
    int ncas = 1;
    while(T--){
        int a, b, c, d, gc; scanf("%d%d%d%d%d", &a, &b, &c, &d, &gc);
        //cout << a << " " << b <<" " << c <<" " << d <<" " << gc << " \n";
        ll ans = 0, t = 0;
        if(gc) ans = solve(b, d, gc);
        if(gc) t = solve(min(b, d), min(b, d), gc);
        printf("Case %d: %lld\n", ncas++, ans - t / 2);
    }   
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值