zoukankan      html  css  js  c++  java
  • F. Coprime Subsequences 莫比乌斯反演

    http://codeforces.com/contest/803/problem/F

    这题正面做了一发dp

    dp[j]表示产生gcd = j的时候的方案总数。

    然后稳稳地超时。

    考虑容斥。

    总答案数是2^n - 1

    那么需要减去gcd = 2的,减去gcd = 3的,减去gcd = 5的。加上gcd = 6的,那么gcd  = 4的呢?

    不用处理,因为这些在gcd = 2的时候减去就行。就是把他们的贡献统计到gcd = 2的哪里去。

    然后这个

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <algorithm>
    #include <assert.h>
    #define IOS ios::sync_with_stdio(false)
    using namespace std;
    #define inf (0x3f3f3f3f)
    typedef long long int LL;
    
    
    #include <iostream>
    #include <sstream>
    #include <vector>
    #include <set>
    #include <map>
    #include <queue>
    #include <string>
    #include <bitset>
    LL quick_pow(LL a, LL b, LL MOD) {
        LL ans = 1;
        LL base = a;
        while (b) {
            if (b & 1) {
                ans = ans * base % MOD;
            }
            b >>= 1;
            base = base * base % MOD;
        }
        return (ans - 1 + MOD) % MOD;
    }
    const int maxn = 100000 + 20;
    int cnt[maxn];
    int prime[maxn];//这个记得用int,他保存的是质数,可以不用开maxn那么大
    bool check[maxn];
    int total;
    int mu[maxn];
    void initprime() {
        mu[1] = 1; //固定的
        for (int i = 2; i <= maxn - 20; i++) {
            if (!check[i]) { //是质数了
                prime[++total] = i; //只能这样记录,因为后面要用
                mu[i] = -1; //质因数分解个数为奇数
            }
            for (int j = 1; j <= total; j++) { //质数或者合数都进行的
                if (i * prime[j] > maxn - 20) break;
                check[i * prime[j]] = 1;
                if (i % prime[j] == 0) {
                    mu[prime[j] * i] = 0;
                    break;
                }
                mu[prime[j] * i] = -mu[i];
                //关键,使得它只被最小的质数筛去。例如i等于6的时候。
                //当时的质数只有2,3,5。6和2结合筛去了12,就break了
                //18留下等9的时候,9*2=18筛去
            }
        }
    }
    
    void calc(int val) {
        int en = (int)sqrt(val * 1.0);
        bool flag = false;
        for (int i = 2; i <= en; ++i) {
            if (val % i == 0) {
                flag = true;
                cnt[i]++;
                if (val / i != i)
                    cnt[val / i]++;
            }
        }
        cnt[val]++;
    }
    const int MOD = 1e9 + 7;
    void work() {
        int n;
        cin >> n;
        for (int i = 1; i <= n; ++i) {
            int x;
            cin >> x;
            calc(x);
        }
        LL ans = quick_pow(2, n, MOD);
    //    cout << cnt[5] << endl;
        for (int i = 2; i <= maxn - 20; ++i) {
            ans = (ans + MOD + mu[i] * quick_pow(2, cnt[i], MOD)) % MOD;
        }
        cout << ans << endl;
    }
    
    int main() {
    #ifdef local
        freopen("data.txt", "r", stdin);
    //    freopen("data.txt", "w", stdout);
    #endif
        initprime();
        work();
        return 0;
    }
    View Code

    就是mobius函数。

  • 相关阅读:
    Docker的load,save和import,export的区别
    LeetCode 146. LRU 缓存机制
    mongoTemplate怎么获取MongoDB自动生成的主键_id
    $ajax()或$.get()中的请求成功时运行的函数success无法执行的解决办法
    使用$.get()请求controller出现 http://localhost:8080/../[object%20Object] 错误的问题解决
    Java利用Runtime调用Python脚本
    SpringMVC返回对象类型报错HttpMessageNotWritableException: No converter found for return value of type
    「题解」洛谷 P1801 黑匣子
    「题解」洛谷 P1717 钓鱼
    「题解」洛谷 P2571 [SCOI2010]传送带
  • 原文地址:https://www.cnblogs.com/liuweimingcprogram/p/6818754.html
Copyright © 2011-2022 走看看