• BZOJ 2005 [Noi2010]能量采集 (数学+容斥 或 莫比乌斯反演)


    2005: [Noi2010]能量采集

    Time Limit: 10 Sec  Memory Limit: 552 MB
    Submit: 4493  Solved: 2695
    [Submit][Status][Discuss]

    Description

    栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量。在这些植物采集能量后,
    栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起。 栋栋的植物种得非常整齐,一共有n列,每列
    有m棵,植物的横竖间距都一样,因此对于每一棵植物,栋栋可以用一个坐标(x, y)来表示,其中x的范围是1至n,
    表示是在第x列,y的范围是1至m,表示是在第x列的第y棵。 由于能量汇集机器较大,不便移动,栋栋将它放在了
    一个角上,坐标正好是(0, 0)。 能量汇集机器在汇集的过程中有一定的能量损失。如果一棵植物与能量汇集机器
    连接而成的线段上有k棵植物,则能量的损失为2k + 1。例如,当能量汇集机器收集坐标为(2, 4)的植物时,由于
    连接线段上存在一棵植物(1, 2),会产生3的能量损失。注意,如果一棵植物与能量汇集机器连接的线段上没有植
    物,则能量损失为1。现在要计算总的能量损失。 下面给出了一个能量采集的例子,其中n = 5,m = 4,一共有20
    棵植物,在每棵植物上标明了能量汇集机器收集它的能量时产生的能量损失。 在这个例子中,总共产生了36的能
    量损失。

    Input

    仅包含一行,为两个整数n和m。

    Output

    仅包含一个整数,表示总共产生的能量损失。

    Sample Input

    【样例输入1】
    5 4
    【样例输入2】
    3 4

    Sample Output

    【样例输出1】
    36
    【样例输出2】
    20
    对于100%的数据:1 ≤ n, m ≤ 100,000。

    HINT

     

    Source

    析:首先能看出来,对于每个点,gcd(i,i),就说明它是第几个点,所以我们可以用 f(x) 表示最大公因数包含x的点对的个数,为什么不直接表示最大公因数是x的点对的个数呢,因为实在是不好求啊,所以换一种表示方法,很明显f(x) = (m/x) * (n/x),那么答案应该是什么呢,这里需要用容斥,减去后面的就好了。

    也可以用莫比乌斯反演来求,F(x)表示gc(i, j)==x的点对的个数,G(x)表示gcd(i, j)=x的倍数的个数。然后就可以用莫比乌斯反演来求了

    代码如下:

    数论 + 容斥

    #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>
    #include <bitset>
    #include <numeric>
    #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 cl clear()
    //#define all 1,n,1
    #define FOR(i,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 LL LNF = 1e17;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e5 + 10;
    const int maxm = 3e5 + 10;
    const int 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;
    }
    
    LL f[maxn];
    
    int main(){
      scanf("%d %d", &n, &m);
      int mmin = min(n, m);
      LL ans = 0;
      for(int i = mmin; i; --i){
        f[i] = (LL)(m/i) * (n/i);
        for(int j = i + i; j <= mmin; j += i)
          f[i] -= f[j];
        ans += f[i] * (2 * i - 1);
      }
      printf("%lld
    ", ans);
      return 0;
    }
    

      

    莫比乌斯反演:

    #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>
    #include <bitset>
    #include <numeric>
    #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 cl clear()
    //#define all 1,n,1
    #define FOR(i,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 LL LNF = 1e17;
    const double inf = 1e20;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 1e5 + 10;
    const int maxm = 3e5 + 10;
    const int 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;
    }
    
    bool vis[maxn];
    int prime[maxn];
    int mu[maxn];
    
    void Moblus(){
      mu[1] = 1;
      int tot = 0;
      for(int i = 2; i < maxn; ++i){
        if(!vis[i])  prime[tot++] = i, mu[i] = -1;
        for(int j = 0; j < tot; ++j){
          if(i * prime[j] >= maxn)  break;
          vis[i*prime[j]] = 1;
          if(i % prime[j] == 0){
            mu[i*prime[j]] = 0;
            break;
          }
          else  mu[i*prime[j]] = -mu[i];
        }
      }
    }
    
    LL G[maxn];
    
    int main(){
      Moblus();
      scanf("%d %d", &n, &m);
      for(int i = 1; i < maxn; ++i)  G[i] = (LL)(m/i) * (n/i);
      LL ans = 0;
      int k = min(n, m);
      for(int i = 1; i <= k; ++i){
        LL num = 0;
        for(int j = 1; j * i <= k; ++j)
          num += mu[j] * G[j*i];
        ans += num * (2*i-1);
      }
      printf("%lld
    ", ans);
      return 0;
    }
    

      

  • 相关阅读:
    devise 异步发邮件
    ubuntutweak for lucid
    960gs blueprint
    Amoeba for mysql 0.31发布(读写分离、负载均衡、Failover、数据切分)
    Google App Servlet容器转型 – 从Tomcat到Jetty
    DBeaver
    用simple from暂不用formtastic
    [SQL Server]ORDER BY的问题
    PHP pathinfo() 函数
    php中使用head进行二进制流输出,让用户下载PDF等附件的方法
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/8331110.html
Copyright © 2020-2023  润新知