HDU 6053 TrickGCD (莫比乌斯函数)

题意:给一个序列A,要求构造序列B,使得 Bi <= Ai, gcd(Bi) > 1, 1 <= i <= n, 输出构造的方法数。

析:首先这个题直接暴力是不可能解决的,可以先找出最大值mmax和最小值mmin,然后枚举每个gcd,也就是最大公约数d,那么其他数就应该是d 2*d 3*d 4*d ...。如果把每个数的个数求出来。那么答案就是每一种 d 的数量的和,每一种的数量就是 a1 * a2 * a3 ... an,其中是 ai 是 第 i 个数所能取到 d 的数量。

这样做复杂度不但很高,而且还有重复的数,比如当d = 2时,假设所以选的B都是 6,6,6,6,....,6,那么当 d = 3 时或者是 d = 6时,同样还可能选择6,6,6,6,...,6,所以这样的算重了,需要把重复的删除,这里就用到莫比乌斯函数,通过它可以直接得到当d = x时是加上还是减去。

就算是去重了,时间复杂度还是很高,要再次进行优化,对于每个 d,我们可以直接求每个数所能选的最大数数量,并且可能通过区间直接进行求取,比如 t = Ai / d,那么它的范围就是 [t*d, t*d+d-1],这样就能对于每个区间进行处理,把复杂度降到 n*log(n)*log(n),因为还要用一次快速幂。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define FOR(x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
    return r > 0 && r <= n && c > 0 && c <= m;
}

int len;
int prime[maxn];
bool vis[maxn];
int mu[maxn];
int cnt[maxn*2];

void init(){
  mu[1] = 1;
  FOR(2, maxn){
    if(!vis[i]){
      prime[len++] = i;
      mu[i] = -1;
    }
    for(int j = 0; j < len && i * prime[j] < maxn; ++j){
      vis[i*prime[j]] = 1;
      if(i % prime[j])  mu[i*prime[j]] = -mu[i];
      else{
        mu[i*prime[j]] = 0;
        break;
      }
    }
  }
}

LL fast_pow(LL a, int n){
  LL res = 1;
  while(n){
    if(n&1)  res = res * a % mod;
    n >>= 1;
    a = a * a % mod;
  }
  return res;
}

int main(){
  init();
  int T;  cin >> T;
  for(int kase = 1; kase <= T; ++kase){
    ms(cnt, 0);
    scanf("%d", &n);
    int mmin = maxn, mmax = -1;
    FOR(0, n){
      int x;
      scanf("%d", &x);
      ++cnt[x];
      mmin = min(mmin, x);
      mmax = max(mmax, x);
    }
    for(int i = mmin; i <= mmax*2; ++i)  cnt[i] += cnt[i-1];
    LL ans = 0;
    for(int i = 2; i <= mmin; ++i){
      LL tmp = 1;
      int t = mmax / i;
      for(int j = 2; j <= t; ++j)
        tmp = tmp * fast_pow(j, cnt[j*i+i-1] - cnt[j*i-1]) % mod;
      ans = (ans - mu[i] * tmp + mod) % mod;
    }
    printf("Case #%d: %I64d
", kase, ans);
  }
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/7406874.html