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

莫比乌斯二连 HDU 5212 Code & HDU 1695 GCD

时间:2017-08-23 13:31:48      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:lis   ems   莫比乌斯反演   a20   dba   模板题   gcd   ase   ns2   

 

莫比乌斯的模板题

都是差不多的

 F(m)为gcd(i,j) = m(i∈[1,m],j∈[1,n])的个数

 f(m) = ∑(m\d) F(d)  意义为gcd(i,j)为m的倍数的个数

运用莫比乌斯反演得到

  F(m) = ∑(m\d)μ(d/m) * f(d) 

 

技术分享
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;

#define MOD 10007
const int ListSize = 10005;
int PrimeSize,n;
int isPrime[ListSize],Mu[ListSize],Prime[ListSize];
int total[ListSize],f[ListSize],a[ListSize];

void Init(){
  Mu[1] = 1;
  memset(isPrime,1,sizeof(isPrime));
  for (int i=2;i<=ListSize;i++){
    if (isPrime[i]){
      PrimeSize ++;
      Prime[PrimeSize] = i;
      Mu[i] = -1;
    }
    for (int j=1;j<=PrimeSize && Prime[j] * i<= ListSize;j++){
      isPrime[i*Prime[j]] = 0;
      if (i % Prime[j] == 0){
          Mu[i*Prime[j]] = 0;
          break;
      }
      else {
         Mu[i*Prime[j]] = -Mu[i];
      }
    }
  }
}

int main(){
  // freopen("test.in","r",stdin);
  Init();
  while (cin >> n){
    int maxn = 0;
    memset(total,0,sizeof(total));
    memset(f,0,sizeof(f));

    for (int i=0;i<n;i++){
      scanf("%d",&a[i]);
      total[a[i]] ++;
      maxn = max(maxn,a[i]);
    }
    for (int i=1;i<=maxn;i++){
      for (int j=i;j<=maxn;j+=i){
        f[i] += total[j];
      }
    }
    LL ans = 0,temp;
    for (int i=1;i<=maxn;i++){
      temp = 0;
      for (int j=i;j<=maxn;j+=i){
        temp = (temp + Mu[j/i] * f[j] * f[j] % MOD) % MOD;
      }
      ans = (ans + temp * 1ll * i % MOD * (i-1) % MOD) % MOD;
    }
    cout << ans << endl;
  }

}
View Code
技术分享
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;

const int ListSize = 100005;
int PrimeSize;
int isPrime[ListSize],Mu[ListSize],Prime[ListSize];
int Sum[ListSize];
int a,b,c,d,k,T;
void Init(){
  Mu[1] = 1; Sum[0] = 0;
  memset(isPrime,1,sizeof(isPrime));
  for (int i=2;i<=ListSize;i++){
    if (isPrime[i]){
      PrimeSize ++;
      Prime[PrimeSize] = i;
      Mu[i] = -1;
    }
    for (int j=1;j<=PrimeSize && Prime[j] * i<= ListSize;j++){
      isPrime[i*Prime[j]] = 0;
      if (i % Prime[j] == 0){
          Mu[i*Prime[j]] = 0;
          break;
      }
      else {
         Mu[i*Prime[j]] = -Mu[i];
      }
    }
  }
  for (int i=1;i<ListSize;i++){
    Sum[i] = Sum[i-1] + Mu[i];
  }
}

int main(){
  // freopen("test.in","r",stdin);
  Init();
  cin >> T;
  for (int times = 1; times <= T; times ++){
    cin >> a >> b >> c >> d >> k;
    cout << "Case " << times << ": ";
    if (k == 0){
      cout << "0" << endl; continue;
    }

    b = b / k;
    d = d / k;

    if (b > d){
      swap(b,d);
    }

    LL ans1 = 0;
    int last;
    for (int i=1;i<=b;i=last+1){
      last = min(b/(b/i),d/(d/i));
      ans1 += (LL)(Sum[last] - Sum[i-1]) * (b/i) * (d/i);
    }
    LL ans2 = 0;
    for (int i=1;i<=b;i=last+1){
      last = b/(b/i);
      ans2 += (LL) (Sum[last] - Sum[i-1]) * (b/i) * (b/i);
    }
    LL ans = ans1 - ans2 / 2;
    cout << ans << endl;
  }

}
View Code

 

莫比乌斯二连 HDU 5212 Code & HDU 1695 GCD

标签:lis   ems   莫比乌斯反演   a20   dba   模板题   gcd   ase   ns2   

原文地址:http://www.cnblogs.com/ToTOrz/p/7417370.html

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