CodeForces 803F :Coprime Subsequences 思维

传送门

题目描述

现在给定一个数列 a 包含 n 个正整数,问这个数列包含几个不同的不可约子序列。由于答案会很大,所以对 109 + 7 取模。

我们称序列 a1, a2… ak 是不可约的当且仅当gcd(a1,a2…,ak)等于 1。

分析

我们去算每一个数作为最小值的时候他的倍数有多少,然后去总数2 ^ n - 1 - (gcd>1 的子序列个数)

代码

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstring>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
const int INF = 0x3f3f3f3f;
const int N = 100010;
const int mod = 1e9 + 7;
ll a[N];
ll dp[N];
int n;
ll p[N];

int main(){
    
    
    scanf("%d",&n);
    p[0] = 1;
    for(int i = 1;i <= n;i++){
    
    
        p[i] = (p[i - 1] * 2ll) % mod;
        int x;
        scanf("%d",&x);
        a[x]++;
    }
    for(int i = N - 1;i;i--){
    
    
        ll sum = 0;
        for(int j = i;j < N;j += i) sum = sum + a[j];
        dp[i] = p[sum] - 1;
        for(int j = 2 * i;j < N;j += i) dp[i] = (dp[i] - dp[j] + mod) % mod;
    }
    printf("%lld\n",dp[1]);
    return 0;
}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/


猜你喜欢

转载自blog.csdn.net/tlyzxc/article/details/112982409